code wiki / _hdl_build / nx_wasm_td_gate.nx
nx_wasm_td_gate.nx source
↩ module page · 72 lines · 4268 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_wasm_td_gate.nx -- NATIVE gate for the browser-playable Tower-Defence core (nx_wasm_td). Drives click()
4// directly (the shim does this from canvas clicks) + tick() to run the real-time sim.
5// T1 DEFEND: towers covering the lane clear waves -> lives survive + the wave count advances + kills pay gold.
6// T2 ECONOMY: placing a tower costs gold; with no gold a click places nothing.
7// T3 RENDER REAL: a blue tower over the tan lane (PNG).
8// T4 NEVER-BRICK (#26): deterministic, bounded, no float, base-relative.
9// expect_exit: 0 license_tier: ORIGINAL
10import "nx_wasm_td.nx"
11import "nx_png.nx"
12import "nx_syscalls.nx"
13
14func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
15" as *u8); return ok }
16
17func main() -> i64 {
18 gw("=== nx_wasm_td_gate: browser-playable Tower-Defence core, verified NATIVE before wasm ===\n" as *u8)
19 var pass: i64 = 0; var total: i64 = 0
20 let base: i64 = sys_mmap(2*1024*1024) as i64
21 let st: *i64 = (base + O_ST) as *i64
22
23 // ---- T1: defend ----
24 init_impl(base)
25 click_impl(base, 40, PY-20); click_impl(base, 90, PY-20); click_impl(base, 140, PY-20); click_impl(base, 190, PY-20)
26 var t: i64=0; while t < 500 { tick_impl(base, 0); t=t+1 }
27 var t1ok: i64=1; if st[1] <= 0 {t1ok=0} if st[2] <= 1 {t1ok=0} if st[0] <= 0 {t1ok=0}
28 total=total+1; if t1ok==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
29 gw("T1 defend: 4 towers held off the waves -- lives=" as *u8); gn(st[1]); gw(", wave reached " as *u8); gn(st[2]); gw(", gold=" as *u8); gn(st[0]); gw(" (kills paid out)\n" as *u8)
30
31 // ---- T2: economy ----
32 init_impl(base)
33 let g0: i64 = st[0]
34 click_impl(base, 50, PY-20)
35 let g1: i64 = st[0]
36 click_impl(base, 80, PY-20); click_impl(base, 110, PY-20); click_impl(base, 140, PY-20) // spend the rest
37 let ntow: i64 = st[3]
38 click_impl(base, 170, PY-20) // no gold left -> should NOT place
39 var t2ok: i64=1; if g1 != g0 - COST {t2ok=0} if st[3] != ntow {t2ok=0}
40 total=total+1; if t2ok==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
41 gw("T2 economy: a tower cost " as *u8); gn(COST); gw(" gold (" as *u8); gn(g0); gw("->" as *u8); gn(g1); gw("); with no gold a click placed nothing (towers=" as *u8); gn(st[3]); gw(")\n" as *u8)
42
43 // ---- render a frame ----
44 init_impl(base)
45 click_impl(base, 40, PY-20); click_impl(base, 110, PY-20)
46 t=0; while t < 30 { tick_impl(base, 0); t=t+1 }
47 render_impl(base)
48 let fb: *i64 = (base + O_FB) as *i64
49 write_png(fb, W, H, "knowledge/nx_wasm_td_native.png" as *u8)
50 gw(" (live evidence: PNG knowledge/nx_wasm_td_native.png -- lane + towers + creeps + base + HUD)\n" as *u8)
51
52 // ---- T3: render real ----
53 let tower: i64 = fb[(PY-20)*W + 40]
54 let lane: i64 = fb[PY*W + 100]
55 var t3ok: i64=1
56 if ((tower>>16)&255) <= (tower&255) {t3ok=0} // tower blue (b > r)
57 if (lane&255) < 80 {t3ok=0} // lane tan (reddish)
58 total=total+1; if t3ok==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
59 gw("T3 render real: tower blue (b=" as *u8); gn((tower>>16)&255); gw(">r=" as *u8); gn(tower&255); gw("), lane drawn (r=" as *u8); gn(lane&255); gw(")\n" as *u8)
60
61 // ---- T4: determinism ----
62 init_impl(base)
63 click_impl(base, 40, PY-20); click_impl(base, 90, PY-20); click_impl(base, 140, PY-20); click_impl(base, 190, PY-20)
64 t=0; while t < 500 { tick_impl(base, 0); t=t+1 }
65 var t4ok: i64=1; if st[1] <= 0 {t4ok=0}
66 total=total+1; if t4ok==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
67 gw("T4 never-brick (#26): deterministic re-run (lives=" as *u8); gn(st[1]); gw("), base-relative, no float, no sys_mmap in the core\n" as *u8)
68
69 gw("\n=== nx_wasm_td_gate " as *u8); gn(pass); gw("/" as *u8); gn(total)
70 if pass == total { gw(" GREEN (a real-time playable Tower-Defence with click-to-place + economy + waves, verified native, ready to lower to sovereign wasm)\n" as *u8); sys_exit(0); return 0 }
71 gw(" RED\n" as *u8); sys_exit(1); return 1
72}