code wiki / _hdl_build / nx_lerp_smooth_gate.nx

nx_lerp_smooth_gate.nx source

↩ module page · 72 lines · 3963 B

1// nx_lerp_smooth_gate.nx -- proves the sovereign peer render-smoothing primitive (nx_lerp_smooth), the EXACT 2// code the wasm game uses for smooth peer motion. Native nx_cc->nxasm, NO node. 3// 1) interpolation endpoints: alpha 0=prev, 128=midpoint, 256=cur 4// 2) monotone glide: as alpha advances, rendered moves prev->cur monotonically, each per-frame step bounded 5// 3) bounded extrapolation: past cur it keeps gliding (no freeze) but is CLAMPED (no runaway) 6// 4) reaches cur in ~5 frames (the ~80ms sync at ~60fps) 7// 5) anti-tautology (neg): rendered actually depends on alpha (not constant); works for downward motion too 8// license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_gate_emit_lib.nx" 11import "nx_lerp_smooth.nx" 12 13func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 14 15func main() -> i64 { 16 g_puts("nx_lerp_smooth gate (sovereign peer-render smoothing, the wasm's exact code, NO node)\n" as *u8) 17 var pass: i64 = 0; var total: i64 = 0 18 let P: i64 = 0; let C: i64 = 100 19 20 // 1) endpoints 21 var r1: i64 = 1 22 if ls_interp(P, C, 0) != 0 { r1 = 0 } 23 if ls_interp(P, C, 128) != 50 { r1 = 0 } 24 if ls_interp(P, C, 256) != 100 { r1 = 0 } 25 pass = pass + g_check("interpolation endpoints (0=prev, 128=mid, 256=cur)" as *u8, r1); total=total+1 26 27 // 2) monotone glide + bounded per-frame step (alpha 0 -> cur) 28 var a: i64 = 0 29 var prev_r: i64 = ls_interp(P, C, a) 30 var mono: i64 = 1 31 var max_step: i64 = 0 32 var frames: i64 = 0 33 while a < 256 { 34 a = ls_advance(a) 35 let r: i64 = ls_interp(P, C, a) 36 if r < prev_r { mono = 0 } // never moves backward 37 let st: i64 = g_abs(r - prev_r); if st > max_step { max_step = st } 38 prev_r = r 39 frames = frames + 1 40 } 41 g_puts(" [measure] glide prev->cur in " as *u8); g_pn(frames); g_puts(" frames, max per-frame step=" as *u8); g_pn(max_step); g_puts(" (raw jump would be 100)\n" as *u8) 42 var r2: i64 = 1 43 if mono != 1 { r2 = 0 } 44 if max_step > 25 { r2 = 0 } // each frame moves <= ~1/4 of the gap (smooth, not a teleport) 45 pass = pass + g_check("monotone glide, each frame-step << the raw 100-voxel jump" as *u8, r2); total=total+1 46 47 // 3) bounded extrapolation past cur (no freeze, no runaway) 48 let ex: i64 = ls_interp(P, C, 320) // 25% past cur 49 var r3: i64 = 1 50 if ex <= 100 { r3 = 0 } // it DID glide past cur (kept moving on a dropped sync) 51 if ex > 130 { r3 = 0 } // but bounded (<= 125 by design) 52 if ls_advance(320) != 320 { r3 = 0 } // clamped: never advances past the cap (no runaway) 53 if ls_advance(300) != 320 { r3 = 0 } // and clamps the overshoot exactly to the cap 54 g_puts(" [measure] extrapolated at cap = " as *u8); g_pn(ex); g_puts(" (cur=100, bounded +25%)\n" as *u8) 55 pass = pass + g_check("bounded extrapolation (glides past cur, clamped, no runaway)" as *u8, r3); total=total+1 56 57 // 4) reaches cur in ~5 frames 58 var a2: i64 = 0; var f2: i64 = 0 59 while a2 < 256 { a2 = ls_advance(a2); f2 = f2 + 1 } 60 pass = pass + g_check("reaches cur in <= 6 frames (the ~80ms sync)" as *u8, f2 <= 6); total=total+1 61 62 // 5) anti-tautology + downward motion 63 var r5: i64 = 1 64 if ls_interp(0, 100, 64) == ls_interp(0, 100, 192) { r5 = 0 } // alpha actually matters 65 if ls_interp(100, 0, 0) != 100 { r5 = 0 } // downward: prev 66 if ls_interp(100, 0, 256) != 0 { r5 = 0 } // downward: cur 67 pass = pass + g_check("anti-tautology: rendered depends on alpha; downward motion ok" as *u8, r5); total=total+1 68 69 g_puts("---- lerp-smooth gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 70 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 71 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 72}