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