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-night.om

965 lines · 23329 bytes Raw Blame History
1note look-night.om: the night, ported from native/kernel/ui/night.c at aienux 4b24df65 (integer for integer).
2note Needs look-paint.om (pk*, Lg, Nfrm, Nsky, Slin, Ssrgb) and look-sky.om (skmake, sktwink, Sktw*, Skg*) before it;
3note pkinit must have run (the integrator does that once; ntinit does not).
4note API: ntinit; ntat t; ntbase x y w h; ntlights x y w h shim voice; ntfaded x y l; ntstarbox i (into Pa 0..3);
5note ntholes; ntcount. Readable state after ntat: Nthol (count), Nthole (x y e rs per hole, 16ths; hole 0 at 0..3, hole 1 at
6note 4..7), Ntlens (x y w h px, w=0 none), Ntrip (0 ripple_r px, 1 ripple_w px (0 none), 2 ripple_c2, 3 ripple_s2 Q14).
7note Milliseconds: FADE_END 6800, HOLES 2600, SPIRAL 6000, MERGE 12000, RIPPLE 3600, GONE 15200, ORBIT 5200.
8global Ntapp uchar 519901
9global Ntaw long 1
10global Ntt ulong 1
11global Nthol ulong 1
12global Nthole long 8
13global Ntlens long 4
14global Ntrip long 4
15global Ntdisp long 1024
16global Ntlit uchar 1024
17global Ntband uchar 256
18global Ntpow2 ulong 257
19global Ntsep long 1
20global Ntsxy long 2
21global Ntwarp long 3
22global Ntcor long 12
23global Ntwt long 4
24global Ntlr long 8
25global Ntlit0 ulong 257
26global Ntsz long 5
27global Ntcoarse ulong 1
28global Ntdone uint 131072
29global Ntstamp uint 1
30global Ntbw long 1
31global Ntb long 4
32global Ntd long 4
33global Ntg long 5
34note nts: S(v) = (v * k + 128) / 256, the 1920 x 1080 design unit scaled to this screen.
35fn nts v long to long
36ret div add mul v get Lg 2 128 256
37end
38note ntpow2: 2^(x / 65536) in Q16 (night.c pow2_q16).
39fn ntpow2 x ulong to ulong
40n = shr x 16
41f = and x 65535
42k = shr f 8
43g = and f 255
44m = add get Ntpow2 k shr mul sub get Ntpow2 add k 1 get Ntpow2 k g 8
45ret shl m n
46end
47note ntgauss: exp(-(v / width)^2) in Q16 (night.c gauss).
48fn ntgauss v long width long to ulong
49if eq width 0
50width = 1
51end
52q = div mul v 256 width
53if or gt q 4095 lt q -4095
54ret 0
55end
56ret pkexpneg ulong shr mul q q 8
57end
58note ntfar: the farthest screen corner from the centre, pixels.
59fn ntfar to long
60far = long 0
61for i ulong 4
62px = long 0
63if eq and i 1 1
64px = get Lg 0
65end
66py = long 0
67if eq shr i 1 1
68py = get Lg 1
69end
70dx = sub px get Lg 3
71dy = sub py get Lg 4
72far = pkmax far long pkisqrt ulong add mul dx dx mul dy dy
73end
74ret far
75end
76fn ntappx x long y long h long far long to uchar
77x = mul x 4
78y = mul y 4
79dx = sub x get Lg 3
80dy = sub y get Lg 4
81dx = long pkisqrt ulong add mul dx dx mul dy dy
82dx = div mul dx 255 far
83x = div mul x 640 h
84y = div mul y 640 h
85dy = long shr pkfbm x y 909 4 8
86ret uchar div add mul dy 6 mul dx 4 10
87end
88note ntappear: the appearance map, one byte per 4 x 4 cell (night.c appear_map).
89fn ntappear to void
90aw = ulong get Ntaw 0
91ah = ulong add div get Lg 1 4 1
92h = get Lg 1
93far = ntfar
94if eq far 0
95far = 1
96end
97for y ulong ah
98for x ulong aw
99put Ntapp add mul y aw x ntappx long x long y h far
100end
101end
102end
103note ntfaded: how far pixel (x, y) with luma l has faded in, 0..256 (ck_night_faded).
104fn ntfaded x long y long l ulong to ulong
105if ge get Ntt 0 6800
106ret 256
107end
108x = pkclamp x 0 sub get Lg 0 1
109y = pkclamp y 0 sub get Lg 1 1
110y = add mul shr y 2 get Ntaw 0 shr x 2
111x = mul long get Ntapp y 12
112x = add add 300 x mul sub 255 long l 8
113dt = sub long get Ntt 0 x
114if le dt 0
115ret 0
116end
117if ge dt 1365
118ret 256
119end
120ret pksmooth shr mul dt 48 8
121end
122fn ntfadepx c ulong x long y long to ulong
123if eq c 0
124note pkscale 0 f is 0 for every f: one black fades to itself.
125ret 0
126end
127l = pkmax pkmax long and shr c 16 255 long and shr c 8 255 long and c 255
128f = ntfaded x y ulong l
129if ge f 256
130ret c
131end
132ret pkscale c f
133end
134note ntfrow / ntcrow: one row of the base, pixels x0..x1-1, fading / plain copy.
135fn ntfrow y long x0 long x1 long to void
136base = mul y get Lg 0
137for i ulong ulong sub x1 x0
138x = add x0 long i
139p = add base x
140c = ulong get Nsky p
141if ne c 0
142c = ntfadepx c x y
143end
144put Nfrm p uint c
145end
146end
147fn ntcrow y long x0 long x1 long to void
148base = mul y get Lg 0
149for i ulong ulong sub x1 x0
150p = add base add x0 long i
151put Nfrm p get Nsky p
152end
153end
154note ntinit: tables, the sky (skmake), the appearance map, state at t = 0 (ck_night_init).
155fn ntinit to void
156note The ripple's light over one black: pkadd for every light value, computed once (most of
157note the sky is one black, so the per-pixel add becomes a table read; same bytes).
158for a ulong 257
159put Ntlit0 a pkadd 0 5663920 a
160end
161put Ntsz 0 mul sub get Lg 0 1 16
162put Ntsz 1 mul sub get Lg 1 1 16
163put Ntsz 2 sub get Lg 0 1
164put Ntsz 3 sub get Lg 1 1
165put Ntsz 4 mul sub get Lg 1 1 get Lg 0
166put Ntcoarse 0 0
167put Ntbw 0 div add get Lg 0 7 8
168put Ntstamp 0 0
169for i ulong 131072
170put Ntdone i 0
171end
172for i ulong 256
173c = shr ulong add 16384 pkcos mul i 256 1
174c2 = shr mul c c 14
175c6 = shr mul shr mul c2 c2 14 c2 14
176put Ntband i uchar shr mul c6 255 14
177end
178e2 = ulong 1073741824
179for i ulong 256
180put Ntpow2 i shr add e2 8192 14
181e2 = shr mul e2 1076652777 30
182end
183put Ntpow2 256 131072
184put Ntaw 0 add div get Lg 0 4 1
185skmake
186ntappear
187ntat 0
188end
189fn ntroot8 l ulong to ulong
190for i ulong 3
191l = pkisqrt shl l 30
192end
193ret l
194end
195note ntorbit: the orbit angle (turns / 65536) at t; the separation (16ths) goes to Ntsep (night.c orbit_angle).
196fn ntorbit t ulong a0 long to ulong
197if lt t 6000
198put Ntsep 0 a0
199ret div mul sub t 2600 65536 5200
200end
201left = div shl sub 12000 t 30 6000
202r = ntroot8 left
203r2 = shr mul r r 30
204r5 = shr mul shr mul r2 r2 30 r 30
205put Ntsep 0 long shr mul ulong a0 r2 30
206ret add 42850 shr mul 120989 sub 1073741824 r5 30
207end
208fn ntat2 t ulong ef long rf long a0 long cx long cy long to void
209ang = ntorbit t a0
210v = shr mul ef long pksmooth pkprog t 2600 5200 8
211put Nthole 2 v
212put Nthole 6 v
213v = shr mul rf long pksmooth pkprog t 4000 6000 8
214put Nthole 3 v
215put Nthole 7 v
216v = div get Ntsep 0 2
217hx = shr mul v pkcos ang 14
218hy = shr mul v pksin ang 15
219put Nthole 0 add cx hx
220put Nthole 1 add cy hy
221put Nthole 4 sub cx hx
222put Nthole 5 sub cy hy
223put Nthol 0 2
224end
225fn ntat1 t ulong ef long rf long cx long cy long to void
226tm = sub t 12000
227rs0 = div mul rf 323 256
228rs = long pksmooth pkprog tm 0 180
229rs = add rs0 shr mul sub mul rf 2 rs0 rs 8
230wob = long pkexpneg div mul tm 256 260
231wob = shr mul pksin div mul tm 65536 95 wob 16
232wob = div mul mul rs wob 7 100
233rs = add rs shr wob 14
234keep = sub 256 long pksmooth pkprog t 12900 15200
235put Nthole 0 cx
236put Nthole 1 cy
237ef = div mul ef 362 256
238put Nthole 2 shr mul ef keep 8
239keep = shr mul keep keep 8
240put Nthole 3 shr mul rs keep 8
241put Nthol 0 1
242end
243note ntunion: Ntlens = Ntlens U (x y w h), the lens rect in pixels.
244fn ntunion x long y long w long h long to void
245if or le get Ntlens 2 0 le get Ntlens 3 0
246put Ntlens 0 x
247put Ntlens 1 y
248put Ntlens 2 w
249put Ntlens 3 h
250ret
251end
252x0 = pkmin get Ntlens 0 x
253y0 = pkmin get Ntlens 1 y
254x1 = pkmax add get Ntlens 0 get Ntlens 2 add x w
255y1 = pkmax add get Ntlens 1 get Ntlens 3 add y h
256put Ntlens 0 x0
257put Ntlens 1 y0
258put Ntlens 2 sub x1 x0
259put Ntlens 3 sub y1 y0
260end
261note ntholebox: hole i's own lens box (x y w h, unclipped) into Pa 0..3; false when it has none.
262fn ntholebox i ulong to bool
263b = mul i 4
264e = get Nthole add b 2
265rs = get Nthole add b 3
266if and le e 0 le rs 0
267ret false
268end
269reach = add shr div mul e 44 10 4 2
270rs = add shr div mul rs 16 10 4 2
271reach = pkmax reach rs
272put Pa 0 sub shr get Nthole b 4 reach
273put Pa 1 sub shr get Nthole add b 1 4 reach
274put Pa 2 add mul 2 reach 1
275put Pa 3 add mul 2 reach 1
276ret true
277end
278fn ntlensbox to void
279for i ulong get Nthol 0
280if ntholebox i
281x = get Pa 0
282y = get Pa 1
283w = get Pa 2
284ntunion x y w get Pa 3
285end
286end
287if ne get Ntlens 2 0
288x0 = pkmax get Ntlens 0 0
289y0 = pkmax get Ntlens 1 0
290x1 = pkmin add get Ntlens 0 get Ntlens 2 get Lg 0
291y1 = pkmin add get Ntlens 1 get Ntlens 3 get Lg 1
292if or le x1 x0 le y1 y0
293put Ntlens 0 0
294put Ntlens 1 0
295put Ntlens 2 0
296put Ntlens 3 0
297else
298put Ntlens 0 x0
299put Ntlens 1 y0
300put Ntlens 2 sub x1 x0
301put Ntlens 3 sub y1 y0
302end
303end
304end
305note ntriptab: Ntdisp / Ntlit over the ripple width; amp in Ntsxy 0, env in Ntsxy 1.
306fn ntriptab reach long w long lambda long to void
307n = ulong add mul 2 reach 1
308for i ulong n
309o = sub long i reach
310s = and div mul o 65536 lambda 65535
311s = shr mul get Ntsxy 0 pksin ulong s 14
312put Ntdisp i shr mul s long ntgauss o w 16
313s = long ntgauss o div mul w 6 10
314s = mul mul s get Ntsxy 1 46
315put Ntlit i uchar shr s 24
316end
317end
318fn ntripple t ulong last ulong to void
319if or lt t 12000 ge t 15600
320ret
321end
322tr = sub t 12000
323w = pkclamp nts 60 4 200
324reach = div mul w 22 10
325far = add ntfar reach
326rr = div mul far long tr 3600
327put Ntrip 0 rr
328put Ntrip 1 w
329env = sub 256 pksmooth pkprog tr 2160 3600
330env = shr mul pksmooth pkprog tr 0 220 env 8
331amp = sub 256 div mul 150 rr far
332amp = shr mul mul nts 6 16 amp 8
333put Ntsxy 0 shr mul amp long env 8
334put Ntsxy 1 long env
335ntriptab reach w pkmax nts 110 8
336put Ntrip 2 pkcos mul 2 last
337put Ntrip 3 pksin mul 2 last
338end
339note ntat: the state at t ms (ck_night_at): holes, lens rect, ripple tables.
340fn ntat t ulong to void
341put Ntt 0 t
342put Ntstamp 0 add get Ntstamp 0 1
343put Nthol 0 0
344put Ntlens 0 0
345put Ntlens 1 0
346put Ntlens 2 0
347put Ntlens 3 0
348put Ntrip 0 0
349put Ntrip 1 0
350put Ntrip 2 0
351put Ntrip 3 0
352ef = mul nts 60 16
353rf = mul nts 20 16
354a0 = mul nts 270 16
355cx = add mul get Lg 3 16 8
356cy = add mul get Lg 4 16 8
357last = ntorbit 11999 a0
358if and ge t 2600 lt t 12000
359ntat2 t ef rf a0 cx cy
360else
361if and ge t 12000 lt t 15200
362ntat1 t ef rf cx cy
363end
364end
365ntlensbox
366ntripple t last
367end
368note ntsample: the sky at (sx, sy) in 16ths, bilinear (night.c sample). The four taps are
369note read in place; when they agree (most of the sky is one black) no mix is needed, as pkmix
370note itself would answer: it returns d when d equals c.
371fn ntsample sx long sy long to ulong
372sx = pkclamp sub sx 8 0 mul sub get Lg 0 1 16
373sy = pkclamp sub sy 8 0 mul sub get Lg 1 1 16
374ix = shr sx 4
375iy = mul shr sy 4 get Lg 0
376ix1 = pkmin add ix 1 sub get Lg 0 1
377iy1 = iy
378if lt shr sy 4 sub get Lg 1 1
379iy1 = add iy get Lg 0
380end
381a = ulong get Nsky add iy ix
382b = ulong get Nsky add iy ix1
383c = ulong get Nsky add iy1 ix
384d = ulong get Nsky add iy1 ix1
385if and eq a b and eq c d eq a c
386ret a
387end
388fx = mul ulong and sx 15 16
389a = pkmix a b fx
390c = pkmix c d fx
391ret pkmix a c mul ulong and sy 15 16
392end
393fn ntlens1 e long dx long dy long d2 long out long to void
394in = div mul mul e e 484 100
395if lt d2 64
396d2 = 64
397end
398f = div mul mul e e 65536 d2
399if gt d2 in
400w = div mul sub out d2 65536 sub out in
401f = shr mul f shr mul w w 16 16
402end
403put Ntsxy 0 sub get Ntsxy 0 shr mul dx f 16
404put Ntsxy 1 sub get Ntsxy 1 shr mul dy f 16
405end
406fn ntlens0 X long Y long b ulong to void
407e = get Nthole add b 2
408if le e 0
409ret
410end
411dx = sub X get Nthole b
412dy = sub Y get Nthole add b 1
413d2 = add mul dx dx mul dy dy
414out = div mul mul e e 1936 100
415if lt d2 out
416ntlens1 e dx dy d2 out
417end
418end
419fn ntlensat X long Y long to void
420for i ulong get Nthol 0
421ntlens0 X Y mul i 4
422end
423end
424fn ntwrip2 dx long dy long d2 long r16 long i long to void
425q = mul sub mul dx dx mul dy dy get Ntrip 2
426q = add q mul mul 2 mul dx dy get Ntrip 3
427q = div q d2
428m = add 5734 shr mul q 10650 14
429m = shr mul get Ntdisp i m 14
430put Ntsxy 0 sub get Ntsxy 0 div mul m dx r16
431put Ntsxy 1 sub get Ntsxy 1 div mul m dy r16
432put Ntwarp 2 long get Ntlit i
433end
434fn ntwripple X long Y long to void
435dx = sub X add mul get Lg 3 16 8
436dy = sub Y add mul get Lg 4 16 8
437d2 = add mul dx dx mul dy dy
438r16 = long pkisqrt ulong d2
439reach = div mul get Ntrip 1 22 10
440i = add sub shr r16 4 get Ntrip 0 reach
441if gt r16 0
442if and ge i 0 le i mul 2 reach
443if lt i 1024
444ntwrip2 dx dy d2 r16 i
445end
446end
447end
448end
449note ntwarp: where (X, Y) in 16ths samples from: offset in Ntwarp 0 1, ripple light in Ntwarp 2 (night.c warp).
450fn ntwarp X long Y long to void
451put Ntsxy 0 0
452put Ntsxy 1 0
453put Ntwarp 2 0
454ntlensat X Y
455if ne get Ntrip 1 0
456ntwripple X Y
457end
458put Ntwarp 0 get Ntsxy 0
459put Ntwarp 1 get Ntsxy 1
460end
461fn ntfinal c ulong x long y long light ulong to ulong
462if lt get Ntt 0 6800
463c = ntfadepx c x y
464end
465if ne light 0
466if and eq c 0 le light 256
467ret get Ntlit0 light
468end
469ret pkadd c 5663920 light
470end
471ret c
472end
473note ntring: the shadow disc and the ring of a hole of radius rs at distance r (16ths) over c.
474fn ntring c ulong r long rs long to ulong
475c = pkmix c 0 pkcover sub r rs
476ring = pkabs sub r div mul rs 104 100
477a = ulong 0
478if lt ring 22
479a = ulong mul sub 22 ring 6
480end
481if gt r rs
482if lt r div mul rs 3 2
483ring = sub div mul rs 3 2 r
484a = add a ulong div mul ring 46 add div rs 2 1
485end
486end
487ret pkadd c 16770244 a
488end
489fn ntshadow c ulong X long Y long b ulong to ulong
490rs = get Nthole add b 3
491if le rs 0
492ret c
493end
494dx = sub X get Nthole b
495dy = sub Y get Nthole add b 1
496dx = add mul dx dx mul dy dy
497if ge dx div mul mul rs rs 9 4
498ret c
499end
500ret ntring c long pkisqrt ulong dx rs
501end
502note ntnearpx: one pixel next to a hole, exact warp plus the shadow disc and ring (night.c near_px).
503fn ntnearpx x long y long to ulong
504X = add mul x 16 8
505Y = add mul y 16 8
506note ntwarp in place: the lens of every hole, then the ripple, into Ntsxy and Ntwarp 2.
507put Ntsxy 0 0
508put Ntsxy 1 0
509put Ntwarp 2 0
510for i ulong get Nthol 0
511ntlens0 X Y mul i 4
512end
513if ne get Ntrip 1 0
514ntwripple X Y
515end
516c = ntsample add X get Ntsxy 0 add Y get Ntsxy 1
517c = ntfinal c x y ulong get Ntwarp 2
518for i ulong get Nthol 0
519c = ntshadow c X Y mul i 4
520end
521ret c
522end
523fn ntnear1 X long Y long b ulong to bool
524reach = div mul get Nthole add b 3 3 2
525reach = add pkmax get Nthole add b 2 reach 128
526dx = sub X get Nthole b
527dy = sub Y get Nthole add b 1
528ret lt add mul dx dx mul dy dy mul reach reach
529end
530fn ntnearhole x long y long to bool
531x = add mul x 16 8
532y = add mul y 16 8
533for i ulong get Nthol 0
534if ntnear1 x y mul i 4
535ret true
536end
537end
538ret false
539end
540fn ntinrip bx long by long to bool
541if eq get Ntrip 1 0
542ret false
543end
544reach = add div mul get Ntrip 1 22 10 1
545lo = pkmax 0 sub get Ntrip 0 reach
546hi = add get Ntrip 0 reach
547cx = get Lg 3
548cy = get Lg 4
549nx = sub pkclamp cx bx add bx 8 cx
550ny = sub pkclamp cy by add by 8 cy
551if gt add mul nx nx mul ny ny mul hi hi
552ret false
553end
554nx = pkmax pkabs sub bx cx pkabs sub add bx 8 cx
555ny = pkmax pkabs sub by cy pkabs sub add by 8 cy
556ret ge add mul nx nx mul ny ny mul lo lo
557end
558note ntmeetl: does the s x s box at (x, y) meet the lens rect.
559fn ntmeetl x long y long s long to bool
560if ge x add get Ntlens 0 get Ntlens 2
561ret false
562end
563if ge y add get Ntlens 1 get Ntlens 3
564ret false
565end
566if le add x s get Ntlens 0
567ret false
568end
569if le add y s get Ntlens 1
570ret false
571end
572ret true
573end
574note ntdrect: Ntd = (x y w h) ∩ clip ∩ screen as x0 y0 x1 y1; false if empty.
575fn ntdrect x long y long w long h long to bool
576put Ntd 0 pkmax x pkmax 0 get Lg 5
577put Ntd 1 pkmax y pkmax 0 get Lg 6
578x = pkmin get Lg 0 add get Lg 5 get Lg 7
579put Ntd 2 pkmin add x w x
580y = pkmin get Lg 1 add get Lg 6 get Lg 8
581put Ntd 3 pkmin add y h y
582if le get Ntd 2 get Ntd 0
583ret false
584end
585if le get Ntd 3 get Ntd 1
586ret false
587end
588ret true
589end
590note ntbclip: Ntb = (bx by 8 8) clipped to Ntd, as x0 y0 x1 y1; false if empty.
591fn ntbclip bx long by long to bool
592put Ntb 0 pkmax bx get Ntd 0
593put Ntb 1 pkmax by get Ntd 1
594put Ntb 2 pkmin add bx 8 get Ntd 2
595put Ntb 3 pkmin add by 8 get Ntd 3
596if le get Ntb 2 get Ntb 0
597ret false
598end
599if le get Ntb 3 get Ntb 1
600ret false
601end
602ret true
603end
604fn ntnearrow y long to void
605p = mul y get Lg 0
606n = ulong sub get Ntb 2 get Ntb 0
607for i ulong n
608x = add get Ntb 0 long i
609put Nfrm add p x uint ntnearpx x y
610end
611end
612fn ntnearblk to void
613n = ulong sub get Ntb 3 get Ntb 1
614for j ulong n
615ntnearrow add get Ntb 1 long j
616end
617end
618fn ntcorners bx long by long to void
619for c ulong 4
620X = mul add bx mul long and c 1 8 16
621Y = mul add by mul long shr c 1 8 16
622ntwarp X Y
623put Ntcor c get Ntwarp 0
624put Ntcor add c 4 get Ntwarp 1
625put Ntcor add c 8 get Ntwarp 2
626end
627end
628note ntmix4: the bilinear mix of four taps at fractions sx, sy (sixteenths), as ntsample.
629fn ntmix4 a ulong b ulong c ulong d ulong sx long sy long to ulong
630a = pkmix a b mul ulong and sx 15 16
631c = pkmix c d mul ulong and sx 15 16
632ret pkmix a c mul ulong and sy 15 16
633end
634note ntgpx: one pixel of a grid block at wx (odd sixteenths across the block): the warp and
635note light interpolated between the row's left and right edge values Ntlr (ntgridrow), then the
636note sky sample (ntsample in place: clamp, four taps, bilinear only when the taps differ) and
637note the fade or the ripple light (ntfinal in place). The edge form equals the four-corner dot
638note of night.c exactly: (16-wx)(c0(16-wy) + c2 wy) + wx(c1(16-wy) + c3 wy).
639fn ntgpx x long y long wx long to ulong
640wi = sub 16 wx
641sx = mul get Ntlr 0 wi
642sx = add sx mul get Ntlr 3 wx
643sy = mul get Ntlr 1 wi
644sy = add sy mul get Ntlr 4 wx
645sx = add mul x 16 shr sx 8
646sy = add mul y 16 shr sy 8
647if lt sx 0
648sx = 0
649end
650if gt sx get Ntsz 0
651sx = get Ntsz 0
652end
653if lt sy 0
654sy = 0
655end
656if gt sy get Ntsz 1
657sy = get Ntsz 1
658end
659ix = shr sx 4
660wi = mul shr sy 4 get Lg 0
661wi = add wi ix
662note ix becomes the step to the right tap: 1, or 0 on the last column (pkmin ix+1 w-1).
663if lt ix get Ntsz 2
664ix = 1
665else
666ix = 0
667end
668a = ulong get Nsky wi
669b = ulong get Nsky add wi ix
670note The row below exists while the tap lies above the last row (pkmin iy+1 h-1).
671if lt wi get Ntsz 4
672wi = add wi get Lg 0
673end
674c = ulong get Nsky wi
675d = ulong get Nsky add wi ix
676if ne a b
677a = ntmix4 a b c d sx sy
678else
679if ne c d
680a = ntmix4 a b c d sx sy
681else
682if ne a c
683a = ntmix4 a b c d sx sy
684end
685end
686end
687if lt get Ntt 0 6800
688a = ntfadepx a x y
689end
690note The light, interpolated like the warp; sx is free now and holds it.
691wi = sub 16 wx
692sx = mul get Ntlr 2 wi
693sx = add sx mul get Ntlr 5 wx
694sx = shr sx 8
695if ne sx 0
696if and eq a 0 le sx 256
697ret get Ntlit0 ulong sx
698end
699ret pkadd a 5663920 ulong sx
700end
701ret a
702end
703fn ntgridrow y long bx long wy long to void
704p = mul y get Lg 0
705wi = sub 16 wy
706for k ulong 3
707b = mul k 4
708v = mul get Ntcor b wi
709v = add v mul get Ntcor add b 2 wy
710put Ntlr k v
711v = mul get Ntcor add b 1 wi
712v = add v mul get Ntcor add b 3 wy
713put Ntlr add k 3 v
714end
715n = ulong sub get Ntb 2 get Ntb 0
716for i ulong n
717x = add get Ntb 0 long i
718put Nfrm add p x uint ntgpx x y add mul 2 sub x bx 1
719end
720end
721fn ntgridblk bx long by long to void
722n = ulong sub get Ntb 3 get Ntb 1
723for j ulong n
724y = add get Ntb 1 long j
725ntgridrow y bx add mul 2 sub y by 1
726end
727end
728note ntcoarse: the ripple's band drawn at 2 x 2 (one sample for four pixels): same warp, same
729note light, a quarter of the samples. Ntcoarse 0 keeps every pixel (the C look exactly);
730note look-show.om turns it on while the ripple runs on a large screen.
731fn ntgridrow2 y long bx long wy long to void
732p = mul y get Lg 0
733wi = sub 16 wy
734for k ulong 3
735b = mul k 4
736v = mul get Ntcor b wi
737v = add v mul get Ntcor add b 2 wy
738put Ntlr k v
739v = mul get Ntcor add b 1 wi
740v = add v mul get Ntcor add b 3 wy
741put Ntlr add k 3 v
742end
743x = get Ntb 0
744while lt x get Ntb 2 bound 8
745c = ntgpx x y add mul 2 sub x bx 1
746put Nfrm add p x uint c
747if lt add x 1 get Ntb 2
748put Nfrm add p add x 1 uint c
749end
750x = add x 2
751end
752end
753fn ntrowdup y long to void
754note Row y of the block's columns copied onto row y + 1.
755w = get Lg 0
756p = add mul y w get Ntb 0
757n = ulong sub get Ntb 2 get Ntb 0
758for i ulong n
759x = add p long i
760put Nfrm add x w get Nfrm x
761end
762end
763fn ntgridblk2 bx long by long to void
764y = get Ntb 1
765while lt y get Ntb 3 bound 8
766ntgridrow2 y bx add mul 2 sub y by 1
767if lt add y 1 get Ntb 3
768ntrowdup y
769end
770y = add y 2
771end
772end
773note ntblock: one 8 x 8 block of the warped base (night.c ck_night_base block loop).
774note ntwhole: is the 8 x 8 block at (bx, by) entirely inside the draw rect Ntd.
775fn ntwhole bx long by long to bool
776if or lt bx get Ntd 0 lt by get Ntd 1
777ret false
778end
779ret and le add bx 8 get Ntd 2 le add by 8 get Ntd 3
780end
781fn ntblock bx long by long to void
782lens = false
783if ne get Ntlens 2 0
784lens = ntmeetl bx by 8
785end
786if not lens
787if not ntinrip bx by
788ret
789end
790end
791if not ntbclip bx by
792ret
793end
794note Rectangles composed in one frame overlap (the ripple's bands meet the lens box); a block
795note drawn whole earlier this frame is the same block again, so it is left alone. Only whole
796note blocks are stamped: a clipped one may be covered further by a later rectangle.
797whole = ntwhole bx by
798idx = ulong add mul shr by 3 get Ntbw 0 shr bx 3
799if whole
800if eq get Ntdone idx get Ntstamp 0
801ret
802end
803put Ntdone idx get Ntstamp 0
804end
805if lens
806if ntnearhole add bx 4 add by 4
807ntnearblk
808ret
809end
810end
811ntcorners bx by
812if and not lens ne get Ntcoarse 0 0
813ntgridblk2 bx by
814else
815ntgridblk bx by
816end
817end
818fn ntblocks to void
819by = sub get Ntd 1 rem get Ntd 1 8
820while lt by get Ntd 3 bound 1024
821bx = sub get Ntd 0 rem get Ntd 0 8
822while lt bx get Ntd 2 bound 1024
823ntblock bx by
824bx = add bx 8
825end
826by = add by 8
827end
828end
829note ntbase: Nsky as seen now into Nfrm, inside (x y w h) ∩ clip (ck_night_base).
830fn ntbase x long y long w long h long to void
831if not ntdrect x y w h
832ret
833end
834fading = lt get Ntt 0 6800
835n = ulong sub get Ntd 3 get Ntd 1
836for i ulong n
837y = add get Ntd 1 long i
838if fading
839ntfrow y get Ntd 0 get Ntd 2
840else
841ntcrow y get Ntd 0 get Ntd 2
842end
843end
844if and eq get Ntlens 2 0 eq get Ntrip 1 0
845ret
846end
847ntblocks
848end
849note ntstarbox: the 2 x 2 pixel box of twinkle star i into Pa 0..3 (x y w h) (ck_night_star_box).
850fn ntstarbox i ulong to void
851put Pa 0 shr sub get Sktw mul i 8 8 4
852put Pa 1 shr sub get Sktw add mul i 8 1 8 4
853put Pa 2 2
854put Pa 3 2
855end
856fn ntholes to ulong
857ret get Nthol 0
858end
859note ntcount: the number of twinkle stars.
860fn ntcount to ulong
861ret get Sktwn 0
862end
863fn ntmeetd x long y long s long to bool
864if ge x get Ntd 2
865ret false
866end
867if ge y get Ntd 3
868ret false
869end
870if le add x s get Ntd 0
871ret false
872end
873if le add y s get Ntd 1
874ret false
875end
876ret true
877end
878fn nttw1 i ulong to void
879ntstarbox i
880if not ntmeetd get Pa 0 get Pa 1 2
881ret
882end
883if ne get Ntlens 2 0
884if ntmeetl get Pa 0 get Pa 1 2
885ret
886end
887end
888b = mul i 8
889per = ulong get Sktw add b 6
890tw = rem add get Ntt 0 ulong get Sktw add b 7 per
891tw = shr ulong add 16384 pksin div mul tw 65536 per 7
892a = shr mul shr mul tw tw 8 120 8
893tw = ntfaded shr get Sktw b 4 shr get Sktw add b 1 4 255
894a = shr mul a tw 8
895sktwink i a
896end
897note ntglpx: one galaxy pixel: Ntg 0 sweep, 1 map base, 2 shimmer, 3 voice, 4 tint.
898fn ntglpx x long row long to void
899p = add get Ntg 1 x
900l = ulong get Skglit p
901if eq l 0
902ret
903end
904a = ulong sub 256 get Ntg 0
905a = and add a ulong get Skgpha p 255
906a = shr mul l ulong get Ntband a 8
907a = shr mul a ulong get Ntg 2 8
908a = add a shr mul shr mul l l 8 ulong get Ntg 3 9
909if ne a 0
910p = add row x
911put Nfrm p uint pkadd ulong get Nfrm p ulong get Ntg 4 a
912end
913end
914fn ntgrow g ulong y long x0 long x1 long to void
915b = mul g 4
916n = ulong sub y get Skgbox add b 1
917n = mul n ulong get Skgbox add b 2
918put Ntg 1 sub long add get Skgoff g n get Skgbox b
919base = mul y get Lg 0
920n = ulong sub x1 x0
921for i ulong n
922ntglpx add x0 long i base
923end
924end
925fn ntgset g ulong shim ulong voice ulong to void
926per = get Skgper g
927put Ntg 0 long div mul rem get Ntt 0 per 256 per
928put Ntg 2 long shim
929put Ntg 3 long voice
930put Ntg 4 long get Skgtint g
931end
932fn ntgal g ulong shim ulong voice ulong to void
933b = mul g 4
934if eq get Skgbox add b 2 0
935ret
936end
937ntgset g shim voice
938x0 = pkmax get Skgbox b get Ntd 0
939y0 = pkmax get Skgbox add b 1 get Ntd 1
940x1 = add get Skgbox b get Skgbox add b 2
941x1 = pkmin x1 get Ntd 2
942y1 = add get Skgbox add b 1 get Skgbox add b 3
943y1 = pkmin y1 get Ntd 3
944if or le x1 x0 le y1 y0
945ret
946end
947for j ulong ulong sub y1 y0
948ntgrow g add y0 long j x0 x1
949end
950end
951note ntlights: twinkles and galaxy glow over the base inside (x y w h) ∩ clip (ck_night_lights).
952fn ntlights x long y long w long h long shim ulong voice ulong to void
953if not ntdrect x y w h
954ret
955end
956for i ulong get Sktwn 0
957nttw1 i
958end
959if and eq shim 0 eq voice 0
960ret
961end
962for g ulong 6
963ntgal g shim voice
964end
965end