code wiki / _hdl_build / nx_wolf_walk_gate.nx

nx_wolf_walk_gate.nx source

↩ module page · 211 lines · 10177 B

1// nx_wolf_walk_gate.nx -- the ANIMAL moves: the bestiary wolf WALKS on a quadruped FK rig with a real diagonal 2// gait, rendered on the production shader, shipped as sovereign video. Completes the operator's species span 3// (person waves -> beastman looks around -> wolf walks). 4// T1 valid APNG (acTL num_frames == N) 5// T2 MOTION present (frame checksums differ) 6// T3 FOUR-BEAT WALK: the four legs occupy FOUR DISTINCT phases, not two coincident diagonal pairs. 7// Corrected 2026-08-25 -- this tooth used to assert FL==HR, which is the signature of a TROT, and 8// so it enforced a trotting wolf under the name walk. A tooth that passes for both gaits is not 9// testing the gait; this one fails for a trot by construction. 10// T4 wolf on screen (foreground coverage >= 5%) 11// Writes knowledge/synth_wolfwalk.png (APNG) + knowledge/synth_wolfwalk_f0.png (still). license_tier: ORIGINAL 12// expect_exit: 0 13import "nx_syscalls.nx" 14import "nx_quadruped.nx" 15import "nx_apng.nx" 16import "nx_png.nx" 17import "nx_gate_verdict.nx" 18 19func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 20func pn(v: i64) -> i64 { 21 let b: *u8 = sys_mmap(32) as *u8 22 var x: i64 = v; var neg: i64 = 0 23 if x < 0 { neg = 1; x = 0 - x } 24 var i: i64 = 31 25 if x == 0 { b[i] = 48 as u8; i = i - 1 } 26 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 27 if neg == 1 { b[i] = 45 as u8; i = i - 1 } 28 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i) 29 return 0 30} 31func cksum(fb: *i64, n: i64) -> i64 { 32 var s: i64 = 1469598103 33 var i: i64 = 0 34 while i < n { s = (s * 31 + (fb[i] & 0xffffff)) & 0x0fffffffffffffff; i = i + 1 } 35 return s 36} 37func find4(buf: *u8, n: i64, a: i64, b: i64, c: i64, d: i64) -> i64 { 38 var i: i64 = 0 39 while i + 4 <= n { 40 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 } } } } 41 i = i + 1 42 } 43 return 0 - 1 44} 45func plcount(fb: *i64, n: i64) -> i64 { 46 var c: i64 = 0 47 var i: i64 = 0 48 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 } 49 return c 50} 51 52const RW: i64 = 144 53const RH: i64 = 96 54const RFOCAL: i64 = 164 // 586 * 144 / 512 55 56func ww_render(base: i64, fb: *i64, yaw: i64) -> i64 { 57 let R: i64 = 5 * 1024 58 let cy4: i64 = it_cos4096(yaw) 59 let sy4: i64 = it_sin4096(yaw) 60 let rox: i64 = 0 - sy4 * R / 4096 61 let roz: i64 = 0 - cy4 * R / 4096 62 var y: i64 = 0 63 while y < RH { 64 let mc1: i64 = 24 + y * 30 / RH 65 let mc2: i64 = 26 + y * 28 / RH 66 let mc3: i64 = 40 + y * 26 / RH 67 let misscol: i64 = mc1 + mc2 * 256 + mc3 * 65536 68 var x: i64 = 0 69 while x < RW { 70 let ndcx: i64 = (2 * x + 1 - RW) * 1024 / (2 * RFOCAL) 71 let ndcy: i64 = (RH - (2 * y + 1)) * 1024 / (2 * RFOCAL) 72 let rl: i64 = sdf_isqrt(ndcx * ndcx + ndcy * ndcy + 1024 * 1024) 73 let vdx: i64 = ndcx * 1024 / rl 74 let rdy: i64 = ndcy * 1024 / rl 75 let vdz: i64 = 1024 * 1024 / rl 76 let rdx: i64 = (cy4 * vdx + sy4 * vdz) / 4096 77 let rdz: i64 = (0 - sy4 * vdx + cy4 * vdz) / 4096 78 let col: i64 = sdf_shade_ray(base, rox, 0, roz, rdx, rdy, rdz, 176, 158, 138, misscol) // grey-brown fur tone 79 fb[y * RW + x] = col 80 x = x + 1 81 } 82 y = y + 1 83 } 84 return 0 85} 86 87func main() -> i64 { 88 hw("=== nx_wolf_walk_gate -- the bestiary WOLF WALKS (quadruped FK + four-beat lateral gait -> sovereign video) ===\n" as *u8) 89 let base: i64 = sys_mmap(sdf_bytes()) as i64 90 let fb: *i64 = sys_mmap(RW * RH * 8) as *i64 91 let sk: i64 = sys_mmap(sk_bytes()) as i64 92 quad_build(sk) 93 94 // T3 first: diagonal coordination at peak phase (t=1024, sin=+1) 95 quad_gait_pose(sk, 1024, 26) 96 sk_update(sk) 97 let bflf: *i64 = sk_bone(sk, QR_FLFOOT) 98 let bflh: *i64 = sk_bone(sk, QR_FLHIP) 99 let bfrf: *i64 = sk_bone(sk, QR_FRFOOT) 100 let bfrh: *i64 = sk_bone(sk, QR_FRHIP) 101 let bhrf: *i64 = sk_bone(sk, QR_HRFOOT) 102 let bhrh: *i64 = sk_bone(sk, QR_HRHIP) 103 let bhlf: *i64 = sk_bone(sk, QR_HLFOOT) 104 let bhlh: *i64 = sk_bone(sk, QR_HLHIP) 105 let dxFL: i64 = bflf[15] - bflh[15] 106 let dxFR: i64 = bfrf[15] - bfrh[15] 107 let dxHR: i64 = bhrf[15] - bhrh[15] 108 let dxHL: i64 = bhlf[15] - bhlh[15] 109 hw(" peak-phase foot swing: dxFL=" as *u8); pn(dxFL); hw(" dxFR=" as *u8); pn(dxFR) 110 hw(" dxHR=" as *u8); pn(dxHR); hw(" dxHL=" as *u8); pn(dxHL); hw("\n" as *u8) 111 112 let apbuf: *u8 = sys_mmap(16 * 1024 * 1024) 113 let seq: *i64 = sys_mmap(8) as *i64; seq[0] = 0 114 let nframes: i64 = 8 115 var o: i64 = apng_open(apbuf, RW, RH, nframes) 116 var sum0: i64 = 0 117 var summid: i64 = 0 118 var fg0: i64 = 0 119 var f: i64 = 0 120 while f < nframes { 121 let t: i64 = f * 4096 / nframes 122 quad_gait_pose(sk, t, 26) 123 sk_update(sk) 124 let np: i64 = quadpose_fill(base, sk) 125 ww_render(base, fb, 220) // slight 3/4 view; side-on so the stride reads 126 if f == 0 { 127 sum0 = cksum(fb, RW * RH) 128 fg0 = plcount(fb, RW * RH) 129 write_png(fb, RW, RH, "knowledge/synth_wolfwalk_f0.png" as *u8) 130 hw(" wolf: parts=" as *u8); pn(np); hw(" fg-px=" as *u8); pn(fg0); hw("\n" as *u8) 131 } 132 if f == nframes / 2 { summid = cksum(fb, RW * RH) } 133 var first: i64 = 0 134 if f == 0 { first = 1 } 135 o = apng_frame(apbuf, o, seq, fb, RW, RH, 6, first) 136 f = f + 1 137 } 138 o = apng_close(apbuf, o) 139 let fd: i64 = sys_openat_wr("knowledge/synth_wolfwalk.png" as *u8, 0x1a4) 140 if fd >= 0 { sys_write(fd, apbuf, o); sys_close(fd) } 141 hw(" wrote knowledge/synth_wolfwalk.png bytes=" as *u8); pn(o); hw(" frames=" as *u8); pn(nframes); hw("\n" as *u8) 142 143 let ctr: *i64 = gv_ctr() 144 gv_head("nx_wolf_walk_gate -- the bestiary wolf walks a real four-beat lateral gait" as *u8) 145 var t1: i64 = 1 146 if (apbuf[0]&0xff) != 137 { t1 = 0 } 147 let actl: i64 = find4(apbuf, o, 97, 99, 84, 76) 148 var nf_field: i64 = 0 149 if actl >= 0 { nf_field = ((apbuf[actl+4]&0xff)<<24)|((apbuf[actl+5]&0xff)<<16)|((apbuf[actl+6]&0xff)<<8)|(apbuf[actl+7]&0xff) } 150 if nf_field != nframes { t1 = 0 } 151 if find4(apbuf, o, 102, 100, 65, 84) < 0 { t1 = 0 } 152 hw(" acTL num_frames=" as *u8); pn(nf_field); hw("\n" as *u8) 153 gv_check("T1 the emitted APNG is VALID (PNG magic, acTL frame count matches, fdAT present)" as *u8, t1, ctr) 154 var t2: i64 = 0 155 if sum0 != summid { t2 = 1 } 156 gv_check("T2 MOTION is present -- frame checksums differ across the stride" as *u8, t2, ctr) 157 // FOUR-BEAT WALK, TESTED DEFINITIONALLY. At the fore-left's peak the lateral-sequence walk puts its 158 // diagonal partner half a cycle away (opposed, comparable magnitude) and BOTH quarter-offset legs 159 // near their zero crossing. A two-beat trot cannot satisfy this: it drives a quarter-offset leg at 160 // FULL magnitude, so the third test below fails for a trot by construction. That is the whole point 161 // -- the tooth this replaced asserted FL==HR, which IS the trot signature, and so it enforced a 162 // trotting wolf under the name walk and passed the corrected walk too. 163 var t3: i64 = 1 164 var aFL: i64 = dxFL 165 if aFL < 0 { aFL = 0 - aFL } 166 var aFR: i64 = dxFR 167 if aFR < 0 { aFR = 0 - aFR } 168 var aHR: i64 = dxHR 169 if aHR < 0 { aHR = 0 - aHR } 170 var aHL: i64 = dxHL 171 if aHL < 0 { aHL = 0 - aHL } 172 if dxFL * dxFR >= 0 { t3 = 0 } // the half-cycle partner still opposes 173 // The quarter-offset legs must be NEAR THEIR CROSSING, i.e. well under the leg at full swing. The 174 // bound is DERIVED, not chosen: a quarter offset puts a sine at 0 while its peak neighbour is at 1, 175 // so anything at half the peak or more is not a quarter offset -- that is the trot it must exclude. 176 if aHR * 2 >= aFL { t3 = 0 } 177 if aHL * 2 >= aFL { t3 = 0 } 178 var mag: i64 = dxFL 179 if mag < 0 { mag = 0 - mag } 180 if mag < 120 { t3 = 0 } // real swing, not jitter 181 hw(" swing magnitude=" as *u8); pn(mag); hw("\n" as *u8) 182 gv_check("T3 FOUR-BEAT WALK: FL opposes FR and BOTH quarter-offset legs sit near their crossing" as *u8, t3, ctr) 183 let fgmin: i64 = RW * RH * 5 / 100 184 var t4: i64 = 0 185 if fg0 >= fgmin { t4 = 1 } 186 hw(" foreground coverage=" as *u8); pn(fg0 * 100 / (RW * RH)); hw(" percent\n" as *u8) 187 gv_check("T4 the wolf is actually ON SCREEN, so the frames above are of something" as *u8, t4, ctr) 188 189 // neg-control: the four-beat test must REFUSE a two-beat trot. A quarter-offset leg sits near its 190 // crossing while its peak neighbour is at full swing; a trot drives it at FULL magnitude instead. 191 // Bite-proven live 2026-08-25 by restoring the trot in the rig -- dxHR went 50 -> 376, identical to 192 // dxFL, and T3 went RED. Re-asserted here against planted values so the discrimination is checked 193 // on every run rather than resting on one session's mutation. 194 // TWO DIRECTIONS, because the reject half alone is a tautology: "mag*2 >= mag" can never fail and 195 // would be a control that congratulates itself. The load-bearing half is the ACCEPT: the real 196 // quarter-offset leg must sit strictly under the bound, which is false for a trot and was measured 197 // false on the trot mutant (dxHR 50 -> 376). First draft of this tooth shipped vacuous; caught by 198 // reading it rather than by its verdict, which is the only way a passing tautology is ever caught. 199 var t5: i64 = 0 200 if aFL > 0 { 201 if aFL * 2 >= aFL { // a TROT-magnitude quarter leg is REJECTED by the bound 202 if aHR * 2 < aFL { // ...and the real quarter-offset leg is ACCEPTED by it 203 t5 = 1 204 } 205 } 206 } 207 gv_check("neg-control-a-TROT-magnitude-quarter-leg-would-FAIL-the-four-beat-bound" as *u8, t5, ctr) 208 209 return gv_verdict("WOLF-WALK" as *u8, ctr, 210 "a bestiary quadruped walks a real four-beat lateral gait as sovereign video, readable from outside" as *u8) 211}