code wiki / _hdl_build / nx_game_web_emit.nx
nx_game_web_emit.nx source
↩ module page · 43 lines · 4066 B
1// nx_game_web_emit.nx -- NISHI GAME ECOSYSTEM, emitter rung 1: MECHANISTIC, spec-driven emission of a
2// self-contained PLAYABLE BROWSER game (canvas front-end + JS logic back-end), LLM-FREE at runtime. Same
3// "data spec in -> playable game out" autonomy as nx_game_emit, but targeting the browser (nishifamily.com)
4// instead of terminal ASCII, and parameterized so a DIFFERENT spec yields a DIFFERENT game = real
5// generation (not a hand-cloned title). This rung covers the ARCADE/ACTION collect-class; later rungs add
6// genre-classes (puzzle/tower-defence/turn-based/...) mapped from the ingested registry. The emitted JS is
7// single-quoted (no escaping) and authored BY this organ. 100% sovereign emit. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9
10func p_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
11func p_wn(fd: i64, v: i64) -> i64 {
12 if v == 0 { let z: *u8=sys_mmap(1); z[0]=48 as u8; sys_write(fd,z,1); return 0 }
13 let d: *u8=sys_mmap(24); var m: i64=v; var k: i64=0
14 while m>0 { d[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
15 let b: *u8=sys_mmap(24); var i: i64=0; while i<k { b[i]=d[k-1-i]; i=i+1 }
16 sys_write(fd,b,k); return 0
17}
18
19// emit a complete playable collect-arcade game to `path`, parameterized by the spec.
20func emit_arcade(path: *u8, title: *u8, gw: i64, gh: i64, ng: i64, nh: i64, seed: i64) -> i64 {
21 let fd: i64 = sys_openat_wr(path, 0x1a4)
22 if fd < 0 { return 0 - 1 }
23 p_w(fd, "<!doctype html><html><head><meta charset=utf-8><title>" as *u8); p_w(fd, title)
24 p_w(fd, "</title><style>body{background:#10141e;color:#e8e8f0;font-family:sans-serif;text-align:center}canvas{background:#1b2030;margin-top:8px;image-rendering:pixelated}</style></head><body><h2>" as *u8)
25 p_w(fd, title)
26 p_w(fd, "</h2><p id=hud>arrows / WASD - collect all gold, avoid red</p><canvas id=c></canvas><script>" as *u8)
27 p_w(fd, "var GW=" as *u8); p_wn(fd, gw); p_w(fd, ",GH=" as *u8); p_wn(fd, gh)
28 p_w(fd, ",CELL=28,NG=" as *u8); p_wn(fd, ng); p_w(fd, ",NH=" as *u8); p_wn(fd, nh)
29 p_w(fd, ",SEED=" as *u8); p_wn(fd, seed); p_w(fd, ";" as *u8)
30 p_w(fd, "var cv=document.getElementById('c');cv.width=GW*CELL;cv.height=GH*CELL;var cx=cv.getContext('2d');" as *u8)
31 p_w(fd, "var s=SEED;function rnd(){s=(s*1103515245+12345)&2147483647;return s;}" as *u8)
32 p_w(fd, "var px=0,py=0,goals=[],haz=[],won=0,lost=0,score=0;" as *u8)
33 p_w(fd, "function occ(a,x,y){for(var i=0;i<a.length;i++)if(a[i].x==x&&a[i].y==y)return 1;return 0;}" as *u8)
34 p_w(fd, "function place(a,n){var t=0;while(a.length<n&&t<9999){t++;var x=rnd()%GW,y=rnd()%GH;if((x==0&&y==0)||occ(goals,x,y)||occ(haz,x,y))continue;a.push({x:x,y:y});}}" as *u8)
35 p_w(fd, "place(goals,NG);place(haz,NH);" as *u8)
36 p_w(fd, "function draw(){cx.clearRect(0,0,cv.width,cv.height);var i;for(i=0;i<goals.length;i++){cx.fillStyle='#ffd24a';cx.fillRect(goals[i].x*CELL+6,goals[i].y*CELL+6,CELL-12,CELL-12);}for(i=0;i<haz.length;i++){cx.fillStyle='#e0503a';cx.fillRect(haz[i].x*CELL+4,haz[i].y*CELL+4,CELL-8,CELL-8);}cx.fillStyle='#5ad27a';cx.fillRect(px*CELL+4,py*CELL+4,CELL-8,CELL-8);}" as *u8)
37 p_w(fd, "function hud(){var h=document.getElementById('hud');if(won)h.textContent='YOU WIN! score '+score;else if(lost)h.textContent='HIT HAZARD - reload to retry';else h.textContent='collected '+score+'/'+NG;}" as *u8)
38 p_w(fd, "function step(dx,dy){if(won||lost)return;var nx=px+dx,ny=py+dy;if(nx<0||ny<0||nx>=GW||ny>=GH)return;px=nx;py=ny;var i;for(i=0;i<goals.length;i++)if(goals[i].x==px&&goals[i].y==py){goals.splice(i,1);score++;break;}for(i=0;i<haz.length;i++)if(haz[i].x==px&&haz[i].y==py){lost=1;}if(score>=NG)won=1;draw();hud();}" as *u8)
39 p_w(fd, "document.addEventListener('keydown',function(e){var k=e.key;if(k=='ArrowUp'||k=='w')step(0,-1);else if(k=='ArrowDown'||k=='s')step(0,1);else if(k=='ArrowLeft'||k=='a')step(-1,0);else if(k=='ArrowRight'||k=='d')step(1,0);});" as *u8)
40 p_w(fd, "draw();hud();</script></body></html>" as *u8)
41 sys_close(fd)
42 return 0
43}