code wiki / _hdl_build / nx_creature_motion_gate.nx

nx_creature_motion_gate.nx source

↩ module page · 150 lines · 6712 B

1// nx_creature_motion_gate.nx -- SWAP + MOTION UNIFIED, proven: a swapped creature (horned, eared, tailed body) 2// performs a MOVE and ships as a sovereign animated PNG, rendered on the production skin shader. This is the 3// convergence of the two threads -- one reusable synthetic asset, diversified by swap slots, given motion->video. 4// T1 valid APNG (acTL num_frames == N) 5// T2 MOTION present (frame checksums differ -- the creature actually moves) 6// T3 SWAP present (creaturepose part count > the 36 base parts -- horns/ears/tail were added) 7// T4 body on screen (foreground coverage >= 4%) 8// Writes knowledge/synth_beastman.png (APNG). license_tier: ORIGINAL expect_exit: 0 9import "nx_syscalls.nx" 10import "nx_sdfrender.nx" 11import "nx_skeleton.nx" 12import "nx_figure_render.nx" 13import "nx_movelib.nx" 14import "nx_creature_motion.nx" 15import "nx_apng.nx" 16 17func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func pn(v: i64) -> i64 { 19 let b: *u8 = sys_mmap(32) as *u8 20 var x: i64 = v; var neg: i64 = 0 21 if x < 0 { neg = 1; x = 0 - x } 22 var i: i64 = 31 23 if x == 0 { b[i] = 48 as u8; i = i - 1 } 24 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 25 if neg == 1 { b[i] = 45 as u8; i = i - 1 } 26 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i) 27 return 0 28} 29func cksum(fb: *i64, n: i64) -> i64 { 30 var s: i64 = 1469598103 31 var i: i64 = 0 32 while i < n { s = (s * 31 + (fb[i] & 0xffffff)) & 0x0fffffffffffffff; i = i + 1 } 33 return s 34} 35func find4(buf: *u8, n: i64, a: i64, b: i64, c: i64, d: i64) -> i64 { 36 var i: i64 = 0 37 while i + 4 <= n { 38 if (buf[i]&0xff)==a { if (buf[i+1]&0xff)==b { if (buf[i+2]&0xff)==c { if (buf[i+3]&0xff)==d { return i } } } } 39 i = i + 1 40 } 41 return 0 - 1 42} 43func plcount(fb: *i64, n: i64) -> i64 { 44 var c: i64 = 0 45 var i: i64 = 0 46 while i < n { let px: i64 = fb[i]; let r: i64 = px & 255; let b: i64 = (px >> 16) & 255; if r >= b { c = c + 1 } i = i + 1 } 47 return c 48} 49 50const RW: i64 = 128 51const RH: i64 = 96 52const RFOCAL: i64 = 146 53 54func cg_render(base: i64, fb: *i64, yaw: i64) -> i64 { 55 let R: i64 = 5 * 1024 56 let cy4: i64 = it_cos4096(yaw) 57 let sy4: i64 = it_sin4096(yaw) 58 let rox: i64 = 0 - sy4 * R / 4096 59 let roz: i64 = 0 - cy4 * R / 4096 60 var y: i64 = 0 61 while y < RH { 62 let mc1: i64 = 26 + y * 34 / RH 63 let mc2: i64 = 28 + y * 32 / RH 64 let mc3: i64 = 44 + y * 28 / RH 65 let misscol: i64 = mc1 + mc2 * 256 + mc3 * 65536 66 var x: i64 = 0 67 while x < RW { 68 let ndcx: i64 = (2 * x + 1 - RW) * 1024 / (2 * RFOCAL) 69 let ndcy: i64 = (RH - (2 * y + 1)) * 1024 / (2 * RFOCAL) 70 let rl: i64 = sdf_isqrt(ndcx * ndcx + ndcy * ndcy + 1024 * 1024) 71 let vdx: i64 = ndcx * 1024 / rl 72 let rdy: i64 = ndcy * 1024 / rl 73 let vdz: i64 = 1024 * 1024 / rl 74 let rdx: i64 = (cy4 * vdx + sy4 * vdz) / 4096 75 let rdz: i64 = (0 - sy4 * vdx + cy4 * vdz) / 4096 76 let col: i64 = sdf_shade_ray(base, rox, 0, roz, rdx, rdy, rdz, 150, 182, 120, misscol) // greenish beast skin 77 fb[y * RW + x] = col 78 x = x + 1 79 } 80 y = y + 1 81 } 82 return 0 83} 84 85func main() -> i64 { 86 hw("=== nx_creature_motion_gate -- a SWAPPED creature (horns/ears/tail) performs a MOVE -> sovereign video ===\n" as *u8) 87 let base: i64 = sys_mmap(sdf_bytes()) as i64 88 let fb: *i64 = sys_mmap(RW * RH * 8) as *i64 89 let sk: i64 = sys_mmap(sk_bytes()) as i64 90 rig_build(sk) 91 let move: i64 = sys_mmap(move_bytes()) as i64 92 move_gen(move, 5) // LOOKAROUND: head yaw sweep -> horns/ears visibly swing (bone-anchored) 93 let period: i64 = (mv_hdr(move))[1] 94 95 let apbuf: *u8 = sys_mmap(16 * 1024 * 1024) 96 let seq: *i64 = sys_mmap(8) as *i64; seq[0] = 0 97 let nframes: i64 = 8 98 var delay_cs: i64 = period / nframes / 10 99 if delay_cs < 4 { delay_cs = 4 } 100 var o: i64 = apng_open(apbuf, RW, RH, nframes) 101 var sum0: i64 = 0 102 var summid: i64 = 0 103 var nparts0: i64 = 0 104 var fg0: i64 = 0 105 var f: i64 = 0 106 while f < nframes { 107 let t: i64 = f * period / nframes 108 move_pose_at(move, sk, t) 109 sk_update(sk) 110 let np: i64 = creaturepose_fill(base, sk, 1, 1, 1, 100) // horns + ears + tail, normal build 111 cg_render(base, fb, 0) 112 if f == 0 { 113 sum0 = cksum(fb, RW * RH) 114 nparts0 = np 115 fg0 = plcount(fb, RW * RH) 116 hw(" beastman: parts=" as *u8); pn(np); hw(" (36 body + horns/ears/tail) fg-px=" as *u8); pn(fg0); hw("\n" as *u8) 117 } 118 if f == nframes / 2 { summid = cksum(fb, RW * RH) } 119 var first: i64 = 0 120 if f == 0 { first = 1 } 121 o = apng_frame(apbuf, o, seq, fb, RW, RH, delay_cs, first) 122 f = f + 1 123 } 124 o = apng_close(apbuf, o) 125 let fd: i64 = sys_openat_wr("knowledge/synth_beastman.png" as *u8, 0x1a4) 126 if fd >= 0 { sys_write(fd, apbuf, o); sys_close(fd) } 127 hw(" wrote knowledge/synth_beastman.png bytes=" as *u8); pn(o); hw(" frames=" as *u8); pn(nframes); hw("\n" as *u8) 128 129 var fails: i64 = 0 130 var t1: i64 = 1 131 if (apbuf[0]&0xff) != 137 { t1 = 0 } 132 let actl: i64 = find4(apbuf, o, 97, 99, 84, 76) 133 var nf_field: i64 = 0 134 if actl >= 0 { nf_field = ((apbuf[actl+4]&0xff)<<24)|((apbuf[actl+5]&0xff)<<16)|((apbuf[actl+6]&0xff)<<8)|(apbuf[actl+7]&0xff) } 135 if nf_field != nframes { t1 = 0 } 136 if find4(apbuf, o, 102, 100, 65, 84) < 0 { t1 = 0 } 137 if t1 == 1 { hw("T1 PASS valid APNG (acTL num_frames=" as *u8); pn(nf_field); hw(")\n" as *u8) } 138 else { fails = fails + 1; hw("T1 FAIL APNG invalid\n" as *u8) } 139 if sum0 != summid { hw("T2 PASS motion present (the creature moves; head+horns swing)\n" as *u8) } 140 else { fails = fails + 1; hw("T2 FAIL no motion\n" as *u8) } 141 if nparts0 > 36 { hw("T3 PASS swap slots present (part count " as *u8); pn(nparts0); hw(" > 36 base = horns/ears/tail added)\n" as *u8) } 142 else { fails = fails + 1; hw("T3 FAIL no swap slots (parts=" as *u8); pn(nparts0); hw(")\n" as *u8) } 143 let fgmin: i64 = RW * RH * 4 / 100 144 if fg0 >= fgmin { hw("T4 PASS creature on screen (" as *u8); pn(fg0 * 100 / (RW * RH)); hw(" percent coverage)\n" as *u8) } 145 else { fails = fails + 1; hw("T4 FAIL not visible (fg=" as *u8); pn(fg0); hw(")\n" as *u8) } 146 147 if fails == 0 { hw("CREATURE-MOTION-GATE 4/4 GREEN -- swap + motion UNIFIED: a horned/eared/tailed creature performs a move as sovereign video\n" as *u8); sys_exit(0); return 0 } 148 hw("CREATURE-MOTION-GATE RED fails=" as *u8); pn(fails); hw("\n" as *u8) 149 sys_exit(1); return 1 150}