code wiki / _hdl_build / nx_creaturegen_gate.nx

nx_creaturegen_gate.nx source

↩ module page · 73 lines · 3907 B

1// nx_creaturegen_gate.nx -- prove procedural ANIMALS via the shared layered-anatomy method. 2// T1 a creature builds (skeleton+muscle parts) and renders as a coherent quadruped skin envelope 3// T2 ★VARIETY: different seeds -> different creatures (render L1 large -- varied proportions) 4// T3 determinism + gallery PNG knowledge/nx_creaturegen.png (3 creatures) 5// license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_png.nx" 8import "nx_creaturegen.nx" 9 10func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 12func lum(px: i64) -> i64 { return (px&255)+((px>>8)&255)+((px>>16)&255) } 13func L1(a: *i64, b: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { var d: i64=(a[i]&255)-(b[i]&255); if d<0{d=0-d} s=s+d; i=i+1 } return s } 14 15const YAW: i64 = 4800 16const CZ: i64 = 4 17 18func main() -> i64 { 19 hw("=== nx_creaturegen_gate -- procedural animals via layered anatomy (skeleton->muscle->skin) ===\n" as *u8) 20 var fails: i64 = 0 21 let W: i64 = as_w(); let H: i64 = as_h(); let npx: i64 = W*H 22 let P: *i64 = sys_mmap(96*AS_STRIDE*8) as *i64 23 let f0: *i64 = sys_mmap(npx*8) as *i64 24 let f1: *i64 = sys_mmap(npx*8) as *i64 25 let f2: *i64 = sys_mmap(npx*8) as *i64 26 27 let n0: i64 = creaturegen_build(P, 4) 28 anatstack_render(P, n0, YAW, CZ, 1, 45, 172, 140, 106, f0) 29 let n1: i64 = creaturegen_build(P, 29) 30 anatstack_render(P, n1, YAW, CZ, 1, 45, 158, 132, 104, f1) 31 let n2: i64 = creaturegen_build(P, 63) 32 anatstack_render(P, n2, YAW, CZ, 1, 45, 180, 150, 116, f2) 33 hw(" parts/creature="); pn(n0); hw("\n" as *u8) 34 35 var body: i64 = 0 36 var i: i64 = 0 37 while i < npx { if lum(f0[i]) > 150 { body = body + 1 } i=i+1 } 38 hw(" creature0 body px="); pn(body); hw("\n" as *u8) 39 var T1: i64 = 0 40 if n0 > 24 { if body > 9500 { T1 = 1 } } 41 if T1 == 1 { hw("T1 PASS a quadruped builds + renders (skin envelope over skeleton+muscle)\n" as *u8) } 42 else { fails=fails+1; hw("T1 FAIL body="); pn(body); hw("\n" as *u8) } 43 44 let d01: i64 = L1(f0, f1, npx); let d02: i64 = L1(f0, f2, npx); let d12: i64 = L1(f1, f2, npx) 45 hw(" variety L1: c0-c1="); pn(d01); hw(" c0-c2="); pn(d02); hw(" c1-c2="); pn(d12); hw("\n" as *u8) 46 var T2: i64 = 0 47 if d01 > 400000 { if d02 > 400000 { if d12 > 400000 { T2 = 1 } } } 48 if T2 == 1 { hw("T2 PASS VARIETY: different seeds -> different creatures\n" as *u8) } 49 else { fails=fails+1; hw("T2 FAIL creatures too similar\n" as *u8) } 50 51 let chk: *i64 = sys_mmap(npx*8) as *i64 52 let nc: i64 = creaturegen_build(P, 29) 53 anatstack_render(P, nc, YAW, CZ, 1, 45, 158, 132, 104, chk) 54 var ddet: i64 = 0 55 i = 0 56 while i < npx { if chk[i]!=f1[i] { ddet=ddet+1 } i=i+1 } 57 var T3: i64 = 0 58 if ddet == 0 { T3 = 1 } 59 if T3 == 1 { hw("T3 PASS determinism (same seed -> same creature)\n" as *u8) } 60 else { fails=fails+1; hw("T3 FAIL ddet="); pn(ddet); hw("\n" as *u8) } 61 62 let GW: i64 = W*3 63 let gal: *i64 = sys_mmap(GW*H*8) as *i64 64 var y: i64 = 0 65 while y < H { var x: i64=0; while x<W { gal[y*GW+x]=f0[y*W+x]; gal[y*GW+W+x]=f1[y*W+x]; gal[y*GW+W*2+x]=f2[y*W+x]; x=x+1 } y=y+1 } 66 write_png(gal, GW, H, "knowledge/nx_creaturegen.png" as *u8) 67 hw("T4 gallery -> knowledge/nx_creaturegen.png (seed4 | seed29 | seed63)\n" as *u8) 68 69 if fails == 0 { hw("CREATUREGEN-GATE 3/3 GREEN -- procedural animals: seeded quadrupeds from the SAME layered anatomy (skeleton->muscle->skin), sovereign, no ML, no assets\n" as *u8); sys_exit(0); return 0 } 70 hw("CREATUREGEN-GATE RED fails="); pn(fails); hw("\n" as *u8) 71 sys_exit(1) 72 return 1 73}