code wiki / (root) / nx_build_place.nx

nx_build_place.nx source

↩ module page · 49 lines · 4176 B

1// nx_build_place.nx -- SOVEREIGN VISUAL ENGINE rung V3: the BUILD-AND-PLACE board. 2// 3// The operator's repeated ask: a spatial click-to-PLACE game UI (place factories / 4// stores on a map), NOT a slider dashboard. This organ EMITS a complete, self- 5// contained interactive canvas page (the .nx organ authors all HTML/JS -- no 6// Claude-written JS): a tile grid, a palette (Factory/Store/Bulldoze), click-to- 7// place, and live counts. Sprites drawn as fillRect compositions in the emitted 8// JS. Single-quoted JS/unquoted attrs -> no escaping. license_tier: ORIGINAL. 9 10import "nx_syscalls.nx" 11const K_MAGIC_65536: i64 = 65536 12 13func bp_app(out: *u8, off: *i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { out[off[0]] = s[i]; off[0] = off[0] + 1; i = i + 1 } return 0 } 14 15// build the interactive build-and-place page into out; returns length 16func nx_build_place_page(out: *u8, out_cap: i64) -> i64 { 17 let off: *i64 = (sys_mmap(8)) as *i64 18 off[0] = 0 19 bp_app(out, off, "<!doctype html><html><head><meta charset=utf-8><title>Nishi Build and Place</title><style>" as *u8) 20 bp_app(out, off, "body{background:#1a1a22;color:#dde;font-family:system-ui,sans-serif;text-align:center;margin:18px}" as *u8) 21 bp_app(out, off, "button{padding:8px 14px;margin:4px;border:0;border-radius:6px;background:#0b2545;color:#fff;cursor:pointer;font-size:.95rem}" as *u8) 22 bp_app(out, off, "button.sel{background:#2a8a55}#info{margin-left:12px;color:#9fb}canvas{margin-top:10px;border:2px solid #444;background:#3a5a3a;cursor:pointer;image-rendering:pixelated}</style></head><body>" as *u8) 23 bp_app(out, off, "<h2>Nishi &mdash; Build and Place (100% sovereign)</h2>" as *u8) 24 bp_app(out, off, "<div><button id=bF class=sel onclick=sel('F')>Factory</button><button id=bS onclick=sel('S')>Store</button><button id=bX onclick=sel('X')>Bulldoze</button><span id=info></span></div>" as *u8) 25 bp_app(out, off, "<canvas id=c width=480 height=384></canvas>" as *u8) 26 bp_app(out, off, "<script>var COLS=10,ROWS=8,CS=48,grid=[],cur='F',i;for(i=0;i<COLS*ROWS;i++)grid[i]=0;" as *u8) 27 bp_app(out, off, "var ctx=document.getElementById('c').getContext('2d');" as *u8) 28 bp_app(out, off, "function sel(t){cur=t;['F','S','X'].forEach(function(k){document.getElementById('b'+k).className=(k==t)?'sel':''})}" as *u8) 29 bp_app(out, off, "function cell(x,y){return y*COLS+x}" as *u8) 30 bp_app(out, off, "function dFactory(x,y){ctx.fillStyle='#787c84';ctx.fillRect(x+6,y+18,36,24);ctx.fillStyle='#50545c';ctx.fillRect(x+6,y+12,36,7);ctx.fillRect(x+30,y+4,7,10);ctx.fillStyle='#cde6ff';ctx.fillRect(x+12,y+24,7,7);ctx.fillRect(x+28,y+24,7,7)}" as *u8) 31 bp_app(out, off, "function dStore(x,y){ctx.fillStyle='#965a46';ctx.fillRect(x+6,y+20,36,22);ctx.fillStyle='#c8463c';ctx.fillRect(x+4,y+14,40,7);ctx.fillStyle='#e6dcb4';ctx.fillRect(x+18,y+28,12,14)}" as *u8) 32 bp_app(out, off, "function render(){var cx,cy;for(cy=0;cy<ROWS;cy++)for(cx=0;cx<COLS;cx++){var x=cx*CS,y=cy*CS;ctx.fillStyle=((cx+cy)%2==0)?'#3c5a3c':'#365436';ctx.fillRect(x,y,CS,CS);ctx.strokeStyle='#2a402a';ctx.strokeRect(x,y,CS,CS);var v=grid[cell(cx,cy)];if(v==1)dFactory(x,y);if(v==2)dStore(x,y)}" as *u8) 33 bp_app(out, off, "var nf=0,ns=0;for(i=0;i<grid.length;i++){if(grid[i]==1)nf++;if(grid[i]==2)ns++}document.getElementById('info').textContent='Factories: '+nf+' Stores: '+ns}" as *u8) 34 bp_app(out, off, "document.getElementById('c').onclick=function(e){var r=this.getBoundingClientRect();var cx=Math.floor((e.clientX-r.left)/CS),cy=Math.floor((e.clientY-r.top)/CS);if(cx<0||cy<0||cx>=COLS||cy>=ROWS)return;var k=cell(cx,cy);grid[k]=(cur=='F')?1:((cur=='S')?2:0);render()};" as *u8) 35 bp_app(out, off, "render();</script></body></html>\n" as *u8) 36 out[off[0]] = 0 as u8 37 return off[0] 38} 39 40func nx_build_place_save(path: *u8) -> i64 { 41 let out: *u8 = sys_mmap(K_MAGIC_65536) 42 let len: i64 = nx_build_place_page(out, K_MAGIC_65536) 43 let fd: i64 = sys_openat_wr(path, 420) 44 if fd < 0 { return 0 - 1 } 45 var wr: i64 = 0 46 while wr < len { let c: i64 = sys_write(fd, out + wr, len - wr); if c <= 0 { break } wr = wr + c } 47 sys_close(fd) 48 return wr 49}