interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / allen / look-mind.om

1088 lines · 20685 bytes Raw Blame History
1note SPDX-License-Identifier: MIT
2note Allen's look, part 4: Allen's presence, the mind of light. Handwritten Omega port of
3note native/kernel/ui/mind.h and mind.c (aienux 4b24df65), integer for integer: about a hundred
4note sharp points on and inside a sphere joined by fine threads, turning round a compact heart,
5note with sparks running along the threads. mdat moves it to a time; mddraw only reads.
6note Needs look-paint.om (pkmin pkmax pkabs pkclamp pksin pkcos pkisqrt pkhash pkexpneg pkprog
7note pkeaseout pksmooth pkcover pkmix, Lg, Nfrm). The mind's own thread and point mix coverage
8note (never add light), so they are private here (mdthread, mdpoint), as in mind.c.
9note Statements are short on purpose: every literal and operator of a statement holds one of the
10note function's 16 value slots until the statement ends.
11note Mdv state slots: 0 cx 1 cy 2 R (px) 3 k 4 ne 5 born 6 t 7 rng (32-bit xorshift) 8 spawn
12note 9 mode (0 idle 1 listen 2 think 3 speak) 10 angle (32-bit wrap, 1/256 of a 65536th)
13note 11 spin 12 squeeze 13 heart 14 voice 15 nspark 16 alpha 17 hx 18 hy 19 hr (16ths)
14note 20 form (0..256 this frame) 21 ca 22 sa 23 ct 24 st 25 scale 26 R16 27 x0 28 x1 29 y0 30 y1
15note (the frame's bounding box in 16ths) 31 ta (thread alpha this draw).
16note Mdhome: home[i][c] at 3*i+c (unit sphere, Q14). Mdea/Mdeb: the edges. Mdact: how lit each
17note point is. Sparks as parallel arrays Mdse (edge) Mdsdur (ms) Mdsback (runs eb to ea) Mdst0.
18note Mdpx Mdpy (16ths) Mdlum Mdnear: the frame for mddraw. Mdwords/Mdkeys: when the last four
19note pulses began (ms, 0 none). Mdnow/Mdwas: the box now and the frame before; mddirty writes
20note their union to Mdbox. Mdclip: the draw rect. Mdp: draw scratch. Mdtab: the mode tables
21note (0..3 spins, 4..7 squeezes, 8..11 hearts, 12..15 spark rates, by mode).
22global Mdv long 32
23global Mdhome long 360
24global Mdea long 300
25global Mdeb long 300
26global Mdact long 120
27global Mdse long 64
28global Mdsdur long 64
29global Mdsback long 64
30global Mdst0 long 64
31global Mdpx long 120
32global Mdpy long 120
33global Mdlum long 120
34global Mdnear long 120
35global Mdwords long 4
36global Mdkeys long 4
37global Mdnow long 4
38global Mdwas long 4
39global Mdbox long 4
40global Mdclip long 4
41global Mdp long 16
42global Mdtab long 16
43fn mdrnd to long
44r = get Mdv 7
45s = shl r 13
46s = and s 4294967295
47r = xor r s
48s = shr r 17
49r = xor r s
50s = shl r 5
51s = and s 4294967295
52r = xor r s
53put Mdv 7 r
54ret r
55end
56fn mdhome i long c long to long
57j = mul i 3
58j = add j c
59ret get Mdhome j
60end
61fn mddist2 i long j long to long
62dx = sub mdhome i 0 mdhome j 0
63dy = sub mdhome i 1 mdhome j 1
64dz = sub mdhome i 2 mdhome j 2
65dx = mul dx dx
66dy = mul dy dy
67dz = mul dz dz
68ret add add dx dy dz
69end
70fn mdhasedge a long b long to bool
71nedge = get Mdv 4
72for i ulong ulong nedge
73ea = get Mdea i
74eb = get Mdeb i
75if eq ea a
76if eq eb b
77ret true
78end
79end
80if eq ea b
81if eq eb a
82ret true
83end
84end
85end
86ret false
87end
88fn mdput i long c long v long to void
89j = mul i 3
90j = add j c
91put Mdhome j v
92end
93note one home point: the first 96 on a Fibonacci lattice over the sphere, the last 24 inside
94fn mdnode i long to void
95y = long 0
96th = ulong 0
97ln = long 0
98if lt i 96
99y = mul 2 i
100y = add y 1
101y = mul y 16384
102y = div y 96
103y = sub 16384 y
104th = mul ulong i 25033
105ln = long rem pkhash i 1 31 5900
106ln = add ln 13434
107else
108h1 = pkhash i 2 31
109th = pkhash i 3 31
110y = long rem h1 32768
111y = sub 16384 y
112ln = long rem pkhash i 4 31 5734
113ln = add ln 4915
114end
115rr = mul y y
116rr = sub 268435456 rr
117rr = long pkisqrt ulong rr
118x = mul rr pkcos th
119x = shr x 14
120z = mul rr pksin th
121z = shr z 14
122x = mul x ln
123mdput i 0 shr x 14
124y = mul y ln
125mdput i 1 shr y 14
126z = mul z ln
127mdput i 2 shr z 14
128end
129note the nearest point to i not yet joined to it, or -1
130fn mdnearest i long to long
131best = long -1
132bd = long 0
133for j ulong 120
134jj = long j
135if ne jj i
136if not mdhasedge i jj
137d = mddist2 i jj
138if lt best 0
139best = jj
140bd = d
141else
142if lt d bd
143best = jj
144bd = d
145end
146end
147end
148end
149end
150ret best
151end
152fn mdedges to void
153for i ulong 120
154ii = long i
155want = long 3
156if lt ii 96
157want = 2
158end
159for w ulong ulong want
160best = mdnearest ii
161if ge best 0
162nedge = get Mdv 4
163if lt nedge 300
164put Mdea nedge ii
165put Mdeb nedge best
166put Mdv 4 add nedge 1
167end
168end
169end
170end
171end
172fn mdtables to void
173put Mdtab 0 349
174put Mdtab 1 520
175put Mdtab 2 1400
176put Mdtab 3 600
177put Mdtab 4 0
178put Mdtab 5 4
179put Mdtab 6 16
180put Mdtab 7 0
181put Mdtab 8 50
182put Mdtab 9 70
183put Mdtab 10 120
184put Mdtab 11 80
185put Mdtab 12 2
186put Mdtab 13 6
187put Mdtab 14 25
188put Mdtab 15 12
189end
190note (cx, cy) its centre and k the scale from Lg; born the time it starts to gather (ms)
191fn mdinit born long to void
192for i ulong 32
193put Mdv i 0
194end
195for i ulong 120
196put Mdact i 0
197end
198for i ulong 4
199put Mdwords i 0
200put Mdkeys i 0
201put Mdnow i 0
202put Mdwas i 0
203put Mdbox i 0
204end
205k = get Lg 2
206put Mdv 0 get Lg 3
207put Mdv 1 get Lg 4
208r = mul 118 k
209r = add r 128
210put Mdv 2 div r 256
211put Mdv 3 k
212put Mdv 5 born
213put Mdv 7 2654435769
214put Mdv 11 349
215mdtables
216for i ulong 120
217mdnode long i
218end
219mdedges
220end
221fn mdmode m long t long to void
222put Mdv 9 m
223end
224fn mdword t long to void
225put Mdwords 3 get Mdwords 2
226put Mdwords 2 get Mdwords 1
227put Mdwords 1 get Mdwords 0
228if eq t 0
229put Mdwords 0 1
230else
231put Mdwords 0 t
232end
233end
234fn mdkey t long to void
235put Mdkeys 3 get Mdkeys 2
236put Mdkeys 2 get Mdkeys 1
237put Mdkeys 1 get Mdkeys 0
238if eq t 0
239put Mdkeys 0 1
240else
241put Mdkeys 0 t
242end
243end
244fn mdvoice lvl long to void
245if gt lvl 256
246lvl = 256
247end
248put Mdv 14 lvl
249end
250note the thread leaving from that the spark takes: e or the next one round that touches from
251fn mdsedge e long from long to long
252nedge = get Mdv 4
253tries = long 0
254note at most ne + 1 passes: tries reaches ne or an edge of from is found
255while true bound 400
256if eq get Mdea e from
257break
258end
259if eq get Mdeb e from
260break
261end
262if ge tries nedge
263break
264end
265tries = add tries 1
266e = add e 1
267e = rem e nedge
268end
269ret e
270end
271note a spark on a thread leaving from, or on any thread when from is out of range
272fn mdspark from long to void
273ns = get Mdv 15
274nedge = get Mdv 4
275if ge ns 64
276ret
277end
278if eq nedge 0
279ret
280end
281e = rem mdrnd nedge
282back = long 0
283if lt from 120
284e = mdsedge e from
285if eq get Mdeb e from
286back = 1
287end
288else
289back = and mdrnd 1
290end
291put Mdse ns e
292put Mdsback ns back
293put Mdst0 ns get Mdv 6
294if eq get Mdv 9 2
295e = rem mdrnd 260
296put Mdsdur ns add e 240
297else
298e = rem mdrnd 480
299put Mdsdur ns add e 420
300end
301put Mdv 15 add ns 1
302end
303fn mdtoward cur long want long dt long ms long to long
304d = sub want cur
305step = pkmin dt ms
306step = mul d step
307step = div step ms
308if eq step 0
309if ne d 0
310if ne dt 0
311step = 1
312if lt d 0
313step = -1
314end
315end
316end
317end
318ret add cur step
319end
320note (1 - (off/width)^2)^2, 0..256
321fn mdpulse off long width long to long
322if le off neg width
323ret 0
324end
325if ge off width
326ret 0
327end
328q = mul off off
329q = mul q 256
330w2 = mul width width
331q = div q w2
332q = sub 256 q
333q = mul q q
334ret shr q 8
335end
336note ulong (t * 65536 as uint32) / period
337fn mdphase t long period long to ulong
338p = mul t 65536
339p = and p 4294967295
340ret ulong div p period
341end
342note spin, squeeze and heart eased toward the mode's
343fn mdease md long dt long t long to void
344put Mdv 11 mdtoward get Mdv 11 get Mdtab md dt 700
345w = add md 4
346put Mdv 12 mdtoward get Mdv 12 get Mdtab w dt 500
347w = add md 8
348wh = get Mdtab w
349ms = long 400
350if eq md 3
351w = mul get Mdv 14 120
352w = div w 256
353wh = add wh w
354ms = 80
355end
356if eq md 2
357w = pksin mdphase t 900
358w = add w 16384
359w = mul w 30
360w = shr w 15
361wh = add wh w
362end
363put Mdv 13 mdtoward get Mdv 13 wh dt ms
364end
365note copy spark slot a over slot b
366fn mdscopy a long b long to void
367put Mdse b get Mdse a
368put Mdsdur b get Mdsdur a
369put Mdsback b get Mdsback a
370put Mdst0 b get Mdst0 a
371end
372note spark i arrives: its point lights, and the spark may go on
373fn mdarrived md long i long to void
374e = get Mdse i
375dest = get Mdeb e
376if ne get Mdsback i 0
377dest = get Mdea e
378end
379put Mdact dest 256
380ns = sub get Mdv 15 1
381put Mdv 15 ns
382mdscopy ns i
383r = rem mdrnd 100
384go = false
385if eq md 2
386if lt r 70
387go = true
388end
389else
390if lt r 18
391go = true
392end
393end
394if go
395mdspark dest
396end
397end
398fn mdarrive md long t long to void
399i = long 0
400note each pass moves i on or removes one of the at most 64 sparks that were there
401while lt i get Mdv 15 bound 200
402age = sub t get Mdst0 i
403if lt age get Mdsdur i
404i = add i 1
405else
406mdarrived md i
407end
408end
409end
410fn mddecay dt long to void
411f = mul dt 256
412f = div f 260
413f = long pkexpneg ulong f
414for i ulong 120
415a = mul get Mdact i f
416put Mdact i shr a 16
417end
418end
419note the shape this frame: breathing, drawn in while thinking, gathered in while being born
420fn mdshape t long form long to void
421b = pksin mdphase t 5500
422b = mul b 1966
423b = shr b 14
424sq = mul get Mdv 12 65536
425sq = div sq 256
426scale = add 65536 b
427scale = sub scale sq
428gather = long pkeaseout form
429gather = sub 256 gather
430b = mul gather 560
431b = div b 256
432b = add b 256
433scale = mul scale b
434put Mdv 25 shr scale 8
435sw = mul gather gather
436sw = shr sw 8
437sw = mul sw 256
438b = shr get Mdv 10 8
439b = add b sw
440b = and b 4294967295
441put Mdv 16 long pksmooth mul form 3
442put Mdv 21 pkcos ulong b
443put Mdv 22 pksin ulong b
444put Mdv 23 pkcos 3641
445put Mdv 24 pksin 3641
446put Mdv 26 mul get Mdv 2 16
447put Mdv 27 mul get Mdv 0 16
448put Mdv 28 get Mdv 27
449put Mdv 29 mul get Mdv 1 16
450put Mdv 30 get Mdv 29
451end
452note turn about the upright axis: rz
453fn mdrz i long to long
454x = mul mdhome i 0 get Mdv 22
455z = mul mdhome i 2 get Mdv 21
456x = add x z
457ret shr x 14
458end
459fn mdrx i long to long
460x = mul mdhome i 0 get Mdv 21
461z = mul mdhome i 2 get Mdv 22
462x = sub x z
463ret shr x 14
464end
465note c*16 + 8 + (v * persp >> 14) * R16 / 16384 * scale / 65536
466fn mdscreen c long v long persp long to long
467v = mul v persp
468v = shr v 14
469v = mul v get Mdv 26
470v = div v 16384
471v = mul v get Mdv 25
472v = div v 65536
473c = mul c 16
474c = add c 8
475ret add c v
476end
477note (uint32_t) of the nearness then capped at 256: a negative value wraps past 256
478fn mdnear32 v long to long
479if lt v 0
480ret 256
481end
482if gt v 256
483ret 256
484end
485ret v
486end
487fn mdproj i long to void
488rz = mdrz i
489y = mdhome i 1
490ry = mul y get Mdv 23
491fz = mul rz get Mdv 24
492ry = sub ry fz
493ry = shr ry 14
494fz = mul y get Mdv 24
495y = mul rz get Mdv 23
496fz = add fz y
497fz = shr fz 14
498persp = mul fz 3
499persp = div persp 16
500persp = add persp 16384
501put Mdpx i mdscreen get Mdv 0 mdrx i persp
502put Mdpy i mdscreen get Mdv 1 ry persp
503fz = add fz 16384
504fz = mul fz 256
505put Mdnear i mdnear32 div fz 32768
506end
507fn mdrad i long to long
508x = mdhome i 0
509x = mul x x
510y = mdhome i 1
511y = mul y y
512z = mdhome i 2
513z = mul z z
514x = add add x y z
515x = long pkisqrt ulong x
516x = mul x 256
517ret div x 16384
518end
519note the light a word pulse of this age gives a point at rad
520fn mdwpulse rad long age long to long
521o = mul age 300
522o = div o 600
523o = sub rad o
524p = mdpulse o 70
525o = sub 900 age
526p = mul p o
527p = div p 900
528p = mul p 150
529ret shr p 8
530end
531note the light a key pulse of this age gives a point at ny
532fn mdkpulse ny long age long to long
533o = mul age 600
534o = div o 500
535o = sub 300 o
536o = sub ny o
537p = mdpulse o 60
538o = sub 600 age
539p = mul p o
540p = div p 600
541p = mul p 90
542ret shr p 8
543end
544note light from the pulses: words outward from the heart, keys upward from below
545fn mdpulses rad long ny long t long to long
546extra = long 0
547for w ulong 4
548wt = get Mdwords w
549if ne wt 0
550if le wt t
551age = sub t wt
552if lt age 900
553extra = add extra mdwpulse rad age
554end
555end
556end
557wt = get Mdkeys w
558if ne wt 0
559if le wt t
560age = sub t wt
561if lt age 600
562extra = add extra mdkpulse ny age
563end
564end
565end
566end
567ret extra
568end
569fn mdlight i long t long to void
570rad = mdrad i
571ny = mdhome i 1
572ny = mul ny 256
573ny = div ny 16384
574l = mdpulses rad ny t
575nr = get Mdnear i
576nr = mul nr nr
577nr = div nr 256
578nr = mul nr 180
579nr = div nr 256
580l = add l nr
581l = add l 34
582nr = mul get Mdact i 120
583nr = div nr 256
584l = add l nr
585if gt l 256
586l = 256
587end
588put Mdlum i l
589end
590fn mdgrow i long to void
591put Mdv 27 pkmin get Mdv 27 get Mdpx i
592put Mdv 28 pkmax get Mdv 28 get Mdpx i
593put Mdv 29 pkmin get Mdv 29 get Mdpy i
594put Mdv 30 pkmax get Mdv 30 get Mdpy i
595end
596note the native-pixel heart and the box round everything
597fn mdboxup to void
598hx = mul get Mdv 0 16
599hx = add hx 8
600hy = mul get Mdv 1 16
601hy = add hy 8
602put Mdv 17 hx
603put Mdv 18 hy
604put Mdv 19 24
605x0 = sub get Mdv 27 48
606x0 = pkmin x0 sub hx 40
607x1 = add get Mdv 28 48
608x1 = pkmax x1 add hx 40
609y0 = sub get Mdv 29 48
610y0 = pkmin y0 sub hy 40
611y1 = add get Mdv 30 48
612y1 = pkmax y1 add hy 40
613put Mdnow 0 sub shr x0 4 1
614put Mdnow 1 sub shr y0 4 1
615x1 = sub x1 x0
616x1 = shr x1 4
617put Mdnow 2 add x1 3
618y1 = sub y1 y0
619y1 = shr y1 4
620put Mdnow 3 add y1 3
621end
622note sparks are born at the mode's rate
623fn mdspawn md long dt long to void
624w = add md 12
625w = mul get Mdtab w dt
626spawn = add get Mdv 8 w
627note spawn grows by at most 25*250 per frame
628while ge spawn 1000 bound 999999
629spawn = sub spawn 1000
630mdspark 120
631end
632put Mdv 8 spawn
633end
634fn mdat t long to void
635dt = long 0
636if gt t get Mdv 6
637dt = sub t get Mdv 6
638end
639if gt dt 250
640dt = 250
641end
642put Mdv 6 t
643for i ulong 4
644put Mdwas i get Mdnow i
645end
646if lt t get Mdv 5
647put Mdv 16 0
648for i ulong 4
649put Mdnow i 0
650end
651ret
652end
653md = and get Mdv 9 3
654mdease md dt t
655dt2 = mul get Mdv 11 dt
656dt2 = add dt2 get Mdv 10
657put Mdv 10 and dt2 4294967295
658born = get Mdv 5
659dt2 = add born 3000
660form = pkprog ulong t ulong born ulong dt2
661put Mdv 20 form
662if ge form 192
663mdspawn md dt
664end
665mdarrive md t
666mddecay dt
667mdshape t form
668for i ulong 120
669mdproj long i
670mdlight long i t
671mdgrow long i
672end
673mdboxup
674end
675fn mdcopy4 which long to void
676for i ulong 4
677if eq which 0
678put Mdbox i get Mdnow i
679else
680put Mdbox i get Mdwas i
681end
682end
683end
684note what needs redrawing: the union of where it was and where it is, into Mdbox
685fn mddirty to void
686if le get Mdwas 2 0
687mdcopy4 0
688ret
689end
690if le get Mdwas 3 0
691mdcopy4 0
692ret
693end
694if le get Mdnow 2 0
695mdcopy4 1
696ret
697end
698if le get Mdnow 3 0
699mdcopy4 1
700ret
701end
702x0 = pkmin get Mdwas 0 get Mdnow 0
703y0 = pkmin get Mdwas 1 get Mdnow 1
704put Mdbox 0 x0
705put Mdbox 1 y0
706x1 = add get Mdwas 0 get Mdwas 2
707x1 = pkmax x1 add get Mdnow 0 get Mdnow 2
708put Mdbox 2 sub x1 x0
709x1 = add get Mdwas 1 get Mdwas 3
710x1 = pkmax x1 add get Mdnow 1 get Mdnow 3
711put Mdbox 3 sub x1 y0
712end
713note ---- drawing: coverage-mixed points and threads inside Mdclip and the screen ----
714note Mdp 1 cx 2 cy 3 radius 4 color 5 alpha 6 inner2 7 outer2 9 x0 10 y0 11 x1 12 y1 (the pixel box)
715fn mdpbox bx long by long bw long bh long to bool
716x0 = pkmax bx get Mdclip 0
717x0 = pkmax x0 0
718y0 = pkmax by get Mdclip 1
719y0 = pkmax y0 0
720x1 = add get Mdclip 0 get Mdclip 2
721x1 = pkmin x1 add bx bw
722x1 = pkmin x1 get Lg 0
723y1 = add get Mdclip 1 get Mdclip 3
724y1 = pkmin y1 add by bh
725y1 = pkmin y1 get Lg 1
726if le x1 x0
727ret false
728end
729if le y1 y0
730ret false
731end
732put Mdp 9 x0
733put Mdp 10 y0
734put Mdp 11 x1
735put Mdp 12 y1
736ret true
737end
738note mix the point's colour into frame word idx with this coverage
739fn mdpmix idx ulong cov ulong to void
740a = mul cov ulong get Mdp 5
741a = shr a 8
742put Nfrm idx uint pkmix ulong get Nfrm idx ulong get Mdp 4 a
743end
744fn mdprow y long to void
745dy = mul y 16
746dy = add dy 8
747dy = sub dy get Mdp 2
748dy = mul dy dy
749x = get Mdp 9
750xe = get Mdp 11
751idx = mul ulong y ulong get Lg 0
752idx = add idx ulong x
753note xe - x pixels, at most the screen width
754while lt x xe bound 4096
755dx = mul x 16
756dx = add dx 8
757dx = sub dx get Mdp 1
758dx = mul dx dx
759dx = add dx dy
760if lt dx get Mdp 7
761cov = ulong 256
762full = false
763if ge get Mdp 3 8
764if le dx get Mdp 6
765full = true
766end
767end
768if not full
769dx = long pkisqrt ulong dx
770cov = pkcover sub dx get Mdp 3
771end
772if ne cov 0
773mdpmix idx cov
774end
775end
776x = add x 1
777idx = add idx 1
778end
779end
780note a filled geometric point: constant light inside, one-pixel coverage at its boundary
781fn mdpoint cx long cy long radius long color long alpha long to void
782if le radius 0
783ret
784end
785if eq alpha 0
786ret
787end
788bx = sub cx radius
789bx = sub bx 8
790bx = shr bx 4
791by = sub cy radius
792by = sub by 8
793by = shr by 4
794bw = add cx radius
795bw = add bw 8
796bw = shr bw 4
797bw = sub bw bx
798bw = add bw 1
799bh = add cy radius
800bh = add bh 8
801bh = shr bh 4
802bh = sub bh by
803bh = add bh 1
804if not mdpbox bx by bw bh
805ret
806end
807put Mdp 1 cx
808put Mdp 2 cy
809put Mdp 3 radius
810put Mdp 4 color
811put Mdp 5 alpha
812bx = sub radius 8
813bx = pkmax bx 0
814put Mdp 6 mul bx bx
815bx = add radius 8
816put Mdp 7 mul bx bx
817by = get Mdp 10
818note at most the screen height rows
819while lt by get Mdp 12 bound 4096
820mdprow by
821by = add by 1
822end
823end
824note Mdp 0 width 1 x0 2 y0 3 len 4 grad 5 radius 6 a0 7 a1 8 steep (after the swaps)
825fn mdtin x long y long to bool
826if lt x get Mdclip 0
827ret false
828end
829if lt y get Mdclip 1
830ret false
831end
832if ge x add get Mdclip 0 get Mdclip 2
833ret false
834end
835if ge y add get Mdclip 1 get Mdclip 3
836ret false
837end
838if lt x 0
839ret false
840end
841if lt y 0
842ret false
843end
844if ge x get Lg 0
845ret false
846end
847if ge y get Lg 1
848ret false
849end
850ret true
851end
852fn mdtpx X long Y long yc long a long to void
853x = X
854y = Y
855if ne get Mdp 8 0
856x = Y
857y = X
858end
859if not mdtin x y
860ret
861end
862yc = mul Y 16
863yc = add yc 8
864yc = sub yc get Mdp 2
865yc = pkabs yc
866yc = sub yc get Mdp 5
867a = mul a long pkcover yc
868a = shr a 8
869x = add mul y get Lg 0 x
870put Nfrm x uint pkmix ulong get Nfrm x 7900415 ulong a
871end
872fn mdtcol X long to void
873along = mul X 16
874along = add along 8
875along = sub along get Mdp 1
876yc = mul along get Mdp 4
877yc = shr yc 16
878yc = add yc get Mdp 2
879a = sub get Mdp 7 get Mdp 6
880a = mul a along
881a = div a get Mdp 3
882a = add a get Mdp 6
883y = sub yc get Mdp 5
884y = sub y 8
885y = shr y 4
886ye = add yc get Mdp 5
887ye = add ye 8
888ye = shr ye 4
889put Mdp 2 yc
890note radius/8 + 2 rows
891while le y ye bound 4096
892mdtpx X y yc a
893y = add y 1
894end
895put Mdp 2 get Mdp 9
896end
897fn mdtrun x0 long y0 long x1 long y1 long a0 long a1 long to void
898ln = sub x1 x0
899ln = pkmax ln 1
900grad = sub y1 y0
901grad = mul grad 65536
902grad = div grad ln
903put Mdp 1 x0
904put Mdp 2 y0
905put Mdp 9 y0
906put Mdp 3 ln
907put Mdp 4 grad
908put Mdp 6 a0
909put Mdp 7 a1
910grad = mul grad grad
911grad = add grad 4294967296
912grad = long pkisqrt ulong grad
913grad = shr grad 8
914grad = mul grad get Mdp 0
915put Mdp 5 shr grad 9
916x0 = add x0 7
917x0 = shr x0 4
918x1 = sub x1 8
919x1 = shr x1 4
920if ne get Mdp 8 0
921x0 = pkmax x0 get Mdclip 1
922ln = add get Mdclip 1 get Mdclip 3
923else
924x0 = pkmax x0 get Mdclip 0
925ln = add get Mdclip 0 get Mdclip 2
926end
927x1 = pkmin x1 sub ln 1
928note at most the screen's major extent of columns
929while le x0 x1 bound 4096
930mdtcol x0
931x0 = add x0 1
932end
933end
934note a thread whose light runs from a0 to a1, coverage mixed (crossings never add up)
935fn mdthread x0 long y0 long x1 long y1 long a0 long a1 long to void
936if le get Mdp 0 0
937ret
938end
939if eq a0 0
940if eq a1 0
941ret
942end
943end
944put Mdp 8 0
945t = pkabs sub y1 y0
946if gt t pkabs sub x1 x0
947put Mdp 8 1
948t = x0
949x0 = y0
950y0 = t
951t = x1
952x1 = y1
953y1 = t
954end
955if gt x0 x1
956t = x0
957x0 = x1
958x1 = t
959t = y0
960y0 = y1
961y1 = t
962t = a0
963a0 = a1
964a1 = t
965end
966mdtrun x0 y0 x1 y1 a0 a1
967end
968fn mdtalpha n long to long
969l = mul get Mdnear n 60
970l = div l 256
971l = add l 16
972l = mul l get Mdlum n
973l = shr l 8
974l = mul l get Mdv 31
975ret shr l 8
976end
977fn mdthreads to void
978put Mdp 0 div get Mdv 3 16
979nedge = get Mdv 4
980for i ulong ulong nedge
981a = get Mdea i
982b = get Mdeb i
983mdthread get Mdpx a get Mdpy a get Mdpx b get Mdpy b mdtalpha a mdtalpha b
984end
985end
986fn mdpoints to void
987al = get Mdv 16
988for i ulong 120
989c = long pkmix 2115728 3174592 ulong get Mdact i
990a = mul get Mdlum i al
991mdpoint get Mdpx i get Mdpy i 16 c shr a 8
992end
993end
994note the k-th of a spark's three fading points
995fn mdsparkpt a long b long g long k long to void
996x = sub get Mdpx b get Mdpx a
997x = mul x g
998x = shr x 8
999x = add x get Mdpx a
1000y = sub get Mdpy b get Mdpy a
1001y = mul y g
1002y = shr y 8
1003y = add y get Mdpy a
1004g = shr 200 k
1005g = mul g get Mdv 16
1006g = shr g 8
1007k = mul k 4
1008mdpoint x y sub 16 k 3174592 g
1009end
1010fn mdsparkdraw i long to void
1011e = get Mdse i
1012a = get Mdea e
1013b = get Mdeb e
1014if ne get Mdsback i 0
1015a = get Mdeb e
1016b = get Mdea e
1017end
1018f = sub get Mdv 6 get Mdst0 i
1019f = mul f 256
1020f = div f get Mdsdur i
1021for k long 3
1022g = mul k 22
1023g = sub f g
1024g = pkclamp g 0 256
1025mdsparkpt a b g k
1026end
1027end
1028fn mdheart to void
1029h = get Mdv 13
1030c = long pkmix 2115728 3174592 ulong h
1031a = add 90 h
1032a = pkmin a 256
1033a = mul a get Mdv 16
1034mdpoint get Mdv 17 get Mdv 18 get Mdv 19 c shr a 8
1035end
1036note does the mind's box meet the draw rect
1037fn mdmeets to bool
1038x1 = add get Mdnow 0 get Mdnow 2
1039x1 = pkmin x1 add get Mdclip 0 get Mdclip 2
1040if le x1 pkmax get Mdnow 0 get Mdclip 0
1041ret false
1042end
1043x1 = add get Mdnow 1 get Mdnow 3
1044x1 = pkmin x1 add get Mdclip 1 get Mdclip 3
1045if le x1 pkmax get Mdnow 1 get Mdclip 1
1046ret false
1047end
1048ret true
1049end
1050note the figure into the part (x, y, w, h) of the frame, held inside the clip Lg[5..8]
1051fn mddraw x long y long w long h long to void
1052if eq get Mdv 16 0
1053ret
1054end
1055x0 = pkmax x get Lg 5
1056y0 = pkmax y get Lg 6
1057w = add x w
1058w = pkmin w add get Lg 5 get Lg 7
1059h = add y h
1060h = pkmin h add get Lg 6 get Lg 8
1061if le w x0
1062ret
1063end
1064if le h y0
1065ret
1066end
1067put Mdclip 0 x0
1068put Mdclip 1 y0
1069put Mdclip 2 sub w x0
1070put Mdclip 3 sub h y0
1071if not mdmeets
1072ret
1073end
1074x0 = sub get Mdv 20 128
1075x0 = mul x0 2
1076x0 = long pksmooth x0
1077x0 = mul x0 get Mdv 16
1078put Mdv 31 shr x0 8
1079if ne get Mdv 31 0
1080mdthreads
1081end
1082mdpoints
1083w = get Mdv 15
1084for i ulong ulong w
1085mdsparkdraw long i
1086end
1087mdheart
1088end