code wiki / _hdl_build / nx_wasm_craft_relief_adopt_t143.nx
nx_wasm_craft_relief_adopt_t143.nx source
↩ module page · 4795 lines · 262144 B
1// nx_wasm_craft.nx -- NISHI CRAFT: a GENERATED 3D VOXEL WORLD, playable first-person. The operator's
2// 2026-05-19 vision ("build minecraft equivalent on the lowest possible hardware") finally EMITTING:
3// procedural terrain (integer value-noise heightmap, biome banding, water, trees) in a 64x32x64 block
4// grid, rendered by a per-pixel integer DDA raycast (true 3D: block faces, face shading, distance fog,
5// sky), walked in first person (collision, auto-step-up, water float), and EDITED live (break/place at
6// the crosshair). Every pixel and every world byte is NishiLang; base-relative so the identical code
7// runs natively (gate, PNG proof) and as wasm (browser page); input arrives through nx_input_abstract
8// (keyboard/gamepad/touch all work on day one).
9//
10// Controls (via the input layer's semantic actions): W/S or Up/Down = walk, A/D or Left/Right = turn,
11// R/F = look up/down (bound to the MENU/PAUSE bits -- the 8-action mask is fully spent; widening it
12// to 16 actions is the filed follow-up), E = break block, B = place stone. Touch d-pad works: center
13// tap = break.
14//
15// license_tier: ORIGINAL
16import "nx_syscalls.nx"
17import "nx_itrig.nx"
18import "nx_input_abstract.nx"
19import "nx_atmosphere.nx"
20import "nx_weather_lib.nx"
21import "nx_softbind.nx"
22import "nx_skeleton.nx"
23import "nx_skin_ita.nx"
24import "nx_nxa_rig_motion_context.nx"
25import "nx_nxa_anim_lib.nx" // GE31/GE23: the ONE NXA clip evaluator -- the cast's joint rows are computed HERE, not in page JS
26import "nx_morf_draw_core.nx"
27import "nx_nxa_morf_draw_admit.nx"
28import "_hdl_build/nx_entity_store.nx"
29import "_hdl_build/nx_gamesave_core.nx"
30import "nx_indoorgen_data.nx"
31// GE14a: the PURE staged-terrain core (six parameter fields -> spline -> relief/erosion).
32// Allocator-free and syscall-free, which is the only reason it can live in this wasm target --
33// nx_worldpipe's hydrology and erosion BAKES allocate and stay NAS-side (rung GE14b).
34import "nx_worldpipe_core.nx"
35// THE ONE GEN-TIME RECIPE ALLOWLIST (2026-09-04): init_impl_v's gok9 chain, nx_world_snap and nx_game_page_emit's
36// NXGP passes all read genp_gen_ok from this lib, so an index admitted to the engine reaches the instrument and
37// the page in the same edit. Pure arithmetic, wasm-safe. nx_wasm_craft_gate T90 pins every P_* slot to it.
38import "nx_genp_allow_lib.nx"
39// GE32: frame-budget telemetry -- the window, percentiles and spike count are computed HERE from the raw
40// rAF interval the page feeds through perf_frame, so the served HUD and the pacing referee gate read ONE
41// ruler. Allocation-free and syscall-free like everything else in this target.
42import "nx_perf_lib.nx"
43import "nx_beach_release_recipe_move_t72.nx"
44import "nx_beach_contact_sweep.nx"
45const WATER_MAGIC_16777216: i64 = 16777216
46const EJRN_HASH_MUL: i64 = 40503
47const WATER_MAGIC_33554431: i64 = 33554431
48const TERRAIN_HASH_X: i64 = 374761393
49const TERRAIN_HASH_Z: i64 = 668265263
50const TERRAIN_HASH_SEED: i64 = 1442695041
51const SAVE_CK_MASK: i64 = 4611686018427387903
52const TERRAIN_HASH_MIX: i64 = 1274126177
53const RAY_HUGE: i64 = 1099511627776
54const WATER_MAGIC_65536: i64 = 65536
55const WATER_MAGIC_4096: i64 = 4096
56const WATER_MAGIC_1048576: i64 = 1048576
57const WATER_MAGIC_1536: i64 = 1536
58const WATER_MAGIC_6144: i64 = 6144
59const GENE_MASK_TEMPERAMENT: i64 = 57344
60const SAVE_CK_SEED: i64 = 1469598103
61const SAVE_CK_PRIME: i64 = 1099511
62const WATER_MAGIC_2400: i64 = 2400
63const WATER_MAGIC_2500: i64 = 2500
64const WATER_MAGIC_1400: i64 = 1400
65const CRAFT_DEFAULT_SEED: i64 = 20260728
66
67// ★4K-CLASS NATIVE, SCALED TO THE HARDWARE (operator 2026-08-14: "significantly worse ... up and
68// beyond 4k resolution ... scaling to the available hardware"). The WebGL path used to render at true
69// device resolution while this rasteriser was pinned at 480x300 and CSS-stretched to 1920 -- a 4x linear
70// upscale, which is exactly what "significantly worse" was.
71// W/H are the FRAMEBUFFER, and the renderer derives rw=W/q, rh=H/q, foc=W/(2q) -- so FOV is invariant
72// under q BY CONSTRUCTION and the adaptive controller is the thing that scales to the hardware.
73// 1920x1200 keeps the old 16:10 aspect EXACTLY, so framing is unchanged -- it is 4x the linear detail
74// and 16x the AREA of the 480x300 this replaces.
75// ★THE SAFETY PROPERTY THAT MAKES THIS NON-REGRESSIVE: at q=8 this is rw=240, rh=150, foc=120 --
76// byte-for-byte the old default. The initial q is 8, so the first frame costs what it always did; the
77// controller only refines toward q=1 (one ray per pixel) when frames measure under 11ms.
78// ★CEILING CHOSEN FROM THE MEMORY ENVELOPE, NOT TASTE: pixels are i64 because this language has no
79// 32-bit pointer type (MEASURED: 0 hits for `as *u32` across 23,072 sources, coverage complete), so
80// the framebuffer costs W*H*8 = 18.4 MB. A 3840x2400 ceiling would cost 73.7 MB to expose a q=1 that
81// is unreachable anyway -- 9.2M DDA rays per frame is seconds, not milliseconds. Real 4K needs 4-byte
82// pixels plus SIMD, not a bigger constant here; raising W alone buys memory, not detail.
83// ★THE CEILING IS BOUNDED BY THE WASM LINEAR MEMORY, AND THAT BOUND IS NOT NEGOTIABLE BY TASTE.
84// The WAT backends size linear memory FROM THIS MODULE (2026-08-18): they read the
85// `static nx_wasm_pages_req` contract row below at emit time, so the page count has ONE
86// owner (this file) instead of the three drifting copies that shipped 192-vs-364 and
87// fail-closed the lane. (The AHEAD reconciliation that gated this: the 21 "lost" strings
88// were superseded rewords of nx_parse diagnostics, proven live by fixture.) A 1920x1200 framebuffer at O_FB needs
89// 3,590,528 + 18,432,000 = 22,022,528 bytes, i.e. 9.4 MB PAST THE END, so every render trapped with
90// "index out of bounds" and /craft went black. Shipped and caught in production 2026-08-14.
91// 1200x750 keeps the 16:10 aspect and needs 3,590,528 + 7,200,000 = 10,790,528, leaving 1,792,384
92// bytes of headroom inside the same 192 pages. It is still 2.5x the linear detail and 6.25x the AREA
93// of the 480x300 this replaced.
94// ★AND IT KEEPS EXACT PARITY WITH THE OLD DEFAULT: at Q_START=5, rw = 1200/5 = 240, rh = 750/5 = 150,
95// foc = 1200/(2*5) = 120 -- byte-for-byte the pre-change frame, so no machine is worse off.
96// WASM_MEM_BYTES below mirrors the backend's page count so nx_wasm_craft_gate can REFUSE a ceiling
97// that does not fit; the real remedy is for the backend to size pages from the module's own arena
98// rather than a constant, and that is owed.
99// ★THE RENDER CEILING IS THE PAGE'S OWN DISPLAY CAP, NOT A TASTE (operator 2026-08-17: "why these
100// arbitrary resolution rates"): the emitted page has always capped the canvas at min(98vw,1920px) --
101// the ceiling below and that cap were TWO constants describing ONE thing, and at 1200 the render
102// ceiling silently bound first on every desktop display. Now the ceiling IS the cap (1920 wide at
103// the shipped 16:10 aspect); which fraction of it a device actually renders is decided PER DEVICE by
104// the adaptive controller against that device's own measured budget -- no resolution number below
105// this line is chosen by hand.
106const W: i64 = 1920
107const H: i64 = 1200
108const WASM_PAGE_BYTES: i64 = 65536
109// pages = ceil(CRAFT_TOTAL / page) + the previously PROVEN slack. ceil alone is 339 (339*65536 =
110// 22,216,704 >= CRAFT_TOTAL 22,216,352); the +25 pages carry forward the old configuration's
111// measured-working headroom class (~1.6MB) as the runtime/shim allowance -- honestly UNMEASURED
112// whether anything needs it, so it is kept, named, and gate T60 pins the fit while the wasm-vm
113// gate's PAINTS tooth proves the module actually runs inside what it declares.
114const WASM_PAGES: i64 = 364
115const WASM_MEM_BYTES: i64 = WASM_PAGES * WASM_PAGE_BYTES
116// MODULE-OWNED PAGE SIZING, BLOCKED AT THE ENCODER (2026-08-18): the WAT backends
117// (wat_mem_pages in nx_wasm/nx_wasm_scalararm) READ a module-declared
118// `static nx_wasm_pages_req: i64 = WASM_PAGES` and size linear memory from it --
119// mechanism shipped and bite-proven. But nxasm_x86 refuses ANY nonzero-init
120// writable static (G3_NONZERO_INIT_STATIC_UNSUPPORTED_BOTH_LANES: GNU .rodata
121// would fault the first write, nxasm ignores .data wholesale), so declaring it
122// here breaks every NATIVE importer of this engine (the gate). Until nxasm
123// grows .data placement (debt filed with this exact spec), the page count rides
124// the backends' WAT_MEM_PAGES_DEFAULT and the vm-gate's FITS tooth is the
125// drift catcher -- it refused the 192-era wasm and will refuse the next drift.
126// DO NOT re-add the static until `nx_sov_build_run nx_wasm_craft_gate` compiles.
127const RES: i64 = 2 // ray per RESxRES pixel block (240x150 rays)
128const RW: i64 = 240
129const RH: i64 = 150
130const FOC: i64 = 120 // ~90 degree fov at RW=240
131// ★THE ADAPTIVE RANGE, WIDENED AT BOTH ENDS so "scale to the hardware" is real in both directions.
132// q divides the framebuffer: q=1 is 1920x1200 (one ray per pixel) and q=24 is 80x50.
133// Q_START=8 reproduces the OLD default exactly (1920/8=240 x 1200/8=150 rays, foc 1920/16=120), so
134// weak hardware is never worse off than it was and strong hardware can now actually be used.
135const Q_MAX: i64 = 24
136const Q_START: i64 = 5
137
138// ★A DEPTH BUFFER THAT COSTS ZERO BYTES. The compare board's skinned-characters-in-world row names its
139// blocker as "seed a depth buffer from the voxel pass so terrain occludes limbs correctly".
140// ⚠THE CONTRACT SYMBOL IS DELIBERATELY NOT SPELLED HERE. The compare generator measures whether the
141// symbol EXISTS in source, and it does not strip comments -- so naming it in prose flipped the published
142// row to LANDED with no capability behind it (measured: coverage jumped 500 to 525 permil on a comment).
143// A completion signal that keys on a name rewards writing the name; a scanner that does not skip comments
144// measures the documentation, not the code. Ship the function, then the row flips honestly.
145// A separate buffer cannot exist here: the WAT backend emits 192 wasm pages = 12,582,912
146// bytes and the arena already uses 10,790,528, leaving 1,792,384 -- while a full-resolution depth plane
147// would need another W*H*8 = 7,200,000. Shipping one would black out /craft exactly as the 1920x1200
148// framebuffer did on 2026-08-14.
149// The room was already there: a pixel is an i64 carrying a 24-bit packed colour, so 40 bits sit unused
150// every frame. The voxel pass ALREADY computes the crossing-t it needs for shading and then throws it
151// away. Depth now rides bits 32..55 of the same word and is stripped in one pass before the frame is
152// published, so no consumer -- page blit, gate frame scans, PNG proof -- ever sees a changed format.
153const FB_COLOR_MASK: i64 = 16777215
154const FB_DEPTH_SHIFT: i64 = 32
155const FB_DEPTH_MAX: i64 = 16777215
156
157// ★THE WIDTH THE HUD PIXEL SIZES WERE AUTHORED AT. Every HUD length below is scaled by rw/HUD_DESIGN_W
158// rather than held as a raw pixel count, for the same reason mob_draw's billboard cap is a fraction: a
159// constant pixel size means a different FRACTION OF THE SCREEN at every resolution, and the canvas is
160// sized to rw x rh and upscaled by the browser, so the fraction is what the player actually sees.
161const HUD_DESIGN_W: i64 = 480
162const HUD_PIP_MAX: i64 = 24
163const HUD_HOTBAR_N: i64 = 7
164
165const WX: i64 = 128 // world blocks (v2: 4x the area, taller)
166const WY: i64 = 48
167const WZ: i64 = 128
168const WATER_H: i64 = 9
169
170// FARMING-LIFE blocks (roadmap B1/B2): wetness lives IN the id and crops are separate stage ids,
171// so the v6 save format is UNTOUCHED -- voxels + the edit journal already persist every farm cell.
172const B_FARM: i64 = 12 // tilled farmland, dry
173const B_CROP0: i64 = 13 // crop sprout (placed by SEEDS onto farmland)
174const B_CROP1: i64 = 14 // crop growing
175const B_CROP2: i64 = 15 // crop MATURE -- breaking it is the HARVEST
176const B_FARMW: i64 = 16 // farmland, wet (rain or water within the 5x5 ring)
177
178// ★THE FRAMEBUFFER MOVED TO THE END OF THE ARENA so it can GROW without shifting a single other
179// offset. Every block below (O_ST .. O_GENP) keeps its absolute address, which is what makes a 16x
180// resolution change a 4-constant edit instead of a 12-offset relayout nobody could verify.
181// The 1,152,000 bytes at offset 0 are now unused and reclaimable -- stated, not hidden.
182const O_FB: i64 = 3592832 // W*H i64 packed R|G<<8|B<<16 (at the arena tail; see CRAFT_TOTAL)
183const O_ST: i64 = 1152000 // 64 state slots
184const O_INPUT: i64 = 1152512 // nx_input_abstract arena (64 words)
185const O_VOX: i64 = 1153024 // WX*WY*WZ u8 blocks
186const O_MOBS: i64 = 1939456 // nx_entity_store arena (certified part): en_bytes(16,7)=1632
187const MOB_CAP: i64 = 16
188const MOB_NCOMP: i64 = 7
189const MOB_BYTES: i64 = 1632
190const O_SPEC: i64 = 1941120 // GAME SPEC block (64 words) -- AFTER the arena's 1632B end
191const O_EJRN: i64 = 1941760 // EDIT JOURNAL: 8192 packed i64 hash slots at ABSOLUTE world
192 // coords (v6 streaming). Reuses the pre-v6 save staging spot.
193const EJRN_SLOTS: i64 = 8192
194const EJRN_BYTES: i64 = 65536
195const O_SAVE_LEGACY:i64=2730336 // retained old image region; live soft-body state starts after its v6 extent
196const SAVE_HDR: i64 = 128
197const SAVE_RECIPE_COUNT:i64=17
198const SAVE_RECIPE_BYTES:i64=(SAVE_RECIPE_COUNT+1)*8
199const SAVE_BODY_V6: i64 = 853600 // legacy voxels + mobs + journal
200const SAVE_BODY: i64 = SAVE_BODY_V6 + SAVE_RECIPE_BYTES
201const SAVE_BODY_V5: i64 = 788064 // pre-streaming body (load-compat: v5 saves still open)
202const SAVE_LEN: i64 = SAVE_HDR + SAVE_BODY
203const O_MOBAUX: i64 = 2729952 // 16 mobs x (dx,dz,dy) last-applied step, i64 triples
204 // (384B, fits the 544B rounding slack past the save image;
205 // save format untouched). NON-PERSISTED display truth.
206 // The page's heading/speed used to be a JS derivative of
207 // positions -- a workaround; the ORGAN owns motion, so it
208 // exports the applied step incl the vertical component
209 // (climb cadence needs dy).
210// GAIT-1 -- 16 mobs x 1 word FACING (it-units, IT_PI = a half turn; forward = (sin,cos) in x,z,
211// the SAME convention the camera basis at mob_draw and the wander drive already use, so this
212// engine has ONE yaw convention and not two). 128B in the same free band as O_MOBVY:
213// 2729696 + 128 = O_MOBVY, and O_EJRN ends at 2007296, so 2007296..2729696 is unallocated --
214// the region is carved exactly the way O_MOBVY was and the save format is untouched for the
215// same reason. NON-PERSISTED: a zero arena faces +z, and every write is a deterministic
216// function of arena state, so the T4/T17 two-arena checksums hold by construction.
217// ⚠WHY A STATE WORD AND NOT A DERIVATIVE OF THE LAST STEP: a heading recomputed from the
218// current step every tick IS the wobble. The steering below is an 8-direction bang-bang signal
219// whose per-axis sign can flip in a single tick, so any facing derived from it inherits every
220// one of those flips. The low-pass has to live somewhere; this is that somewhere, one word.
221const O_MOBHDG: i64 = 2729696
222const O_MOBVY: i64 = 2729824 // GE12: 16 mobs x 1 word VERTICAL VELOCITY Q8/tick (128B, the slack
223 // directly BEFORE O_MOBAUX: 2729824 + 128 = O_MOBAUX; free since
224 // O_EJRN ends at 2007296). NON-PERSISTED: a zero arena is AT REST
225 // by construction and every write derives from deterministic
226 // state, so the T4/T17 two-arena checksums hold.
227const O_SOFT: i64 = 3584064 // A4 soft-body springs: 16 mobs x SB_PTS(6, R7 regions) x SD_STRIDE words.
228 // NON-PERSISTED display truth (pure function of root motion;
229 // re-seated on init/load/shift/summon -- the MOBAUX pattern).
230const O_NEED: i64 = 3588672 // C1 needs: 16 mobs x 4 words (hunger, energy, social, spare).
231 // NON-PERSISTED; re-seeded deterministically on init/load.
232const O_MIND: i64 = 3589184 // C3/C5 THE INNER LIFE: 16 girls x 24 words -- 8 THOUGHT
233 // slots (id<<8 | mag+128, expiry-tick) then 4 GOSSIP slots
234 // (subj<<16 | pred<<8 | conf, born-tick). NON-PERSISTED;
235 // every write derives from deterministic state, so the
236 // T4/T17 two-arena checksums hold by construction.
237const O_PASS: i64 = 3592256 // THE TRAVELER'S PASSPORT (the isekai carry): the
238 // CHARACTER crosses worlds while each world keeps its own
239 // v6 save. Wire = 12 words (magic ver charseed broken
240 // placed harv plant found lost ticks worldmask ck);
241 // words 16-24 = the in-arena delta SNAPSHOT so a world
242 // reload never double-counts a deed into the ledger.
243const PASS_MAGIC: i64 = 20260802001
244const PASS_LEN: i64 = 192 // 24 wire words: [0]magic [1]ver [2]charseed [3]broken
245 // [4]placed [5]harv [6]plant [7]found [8]lost [9]ticks
246 // [10]worldmask [11]party-count [12..20]=3x(kind,disg,
247 // gene) -- COMPANIONS CROSS WORLDS with their bond and
248 // bred genome intact -- [21]ck. Snapshot at words 32+.
249const O_GENP: i64 = 3592640 // W1c GEN-TIME RECIPE OVERRIDES (192B): [0] magic, [1] n,
250 // then idx,val pairs x11. Written by the page (NXGP block)
251 // or a native harness BEFORE init_v; init applies them
252 // AFTER wc_spec identity defaults and BEFORE genworld, so
253 // terrain-shape rows (0..6, 59, 60) become world DATA.
254 // Zero region (fresh arena) = no overrides by construction.
255const GENP_MAGIC: i64 = 20260813001
256const GENP_MAXP: i64 = 11
257// ★WRITTEN AS THE ARITHMETIC, NOT AS A TYPED-OUT TOTAL. The framebuffer TAILS the arena, so the
258// total is the pre-framebuffer arena plus the frame itself. Typing the sum by hand is what let the
259// ceiling silently outgrow the wasm linear memory: a hand-computed 22,022,528 looks exactly as
260// authoritative as a correct one. As arithmetic it re-derives itself whenever W, H or O_FB move,
261// and nx_wasm_craft_gate asserts it against WASM_MEM_BYTES so it can never again exceed the module's
262// own memory without the gate going RED first.
263// ★R21 SKINNED-CHARACTER RIG REGION (wc_mob_mesh). ONE rig per arena, shared by every girl: geometry is
264// species data, pose+genome are per-girl -- the same split the body canon uses (species = parameters,
265// individuals = seeded variation). The rig TAILS the framebuffer so every existing offset is untouched.
266const O_RIG: i64 = O_FB + W*H*8
267// == sk_bytes() (256 + 32*256 + 2048*64 + 2048*24 + 64). A const cannot call a func, so the value is
268// written out ONCE here and the gate asserts RIG_BYTES == sk_bytes() -- the derived-constant discipline:
269// the duplicate exists, and it CANNOT drift without a RED.
270const RIG_BYTES: i64 = 188736
271const O_PROJ: i64 = O_RIG + RIG_BYTES
272const PROJ_MAXV: i64 = 211 // projected-vertex slots == RIG_VERT_TOTAL (declared beside the
273 // rig tables; a const cannot forward-reference, so the value is
274 // written out ONCE here and gate T67 asserts the two are EQUAL
275 // and that the built rig fits -- the same derived-constant
276 // discipline as RIG_BYTES/T62. A silent overflow here is the
277 // pmboard zero-byte class: the projection loop would write past
278 // O_PROJ into the skin scratch with no error at all.
279const PROJ_BYTES: i64 = PROJ_MAXV * 24
280const O_SKIN3: i64 = O_PROJ + PROJ_BYTES // 3-word sRGB scratch for nx_skin_ita's out3 (wasm has no
281 // mmap; every buffer is an arena offset)
282// =====================================================================================================
283// H1 -- STRAND GUIDES IN THE ENGINE (2026-08-31). Operator standing order: "i dont want js powering any of
284// this it should be emitting all the way from the first byte up via nishi lang and nishi ecosystem". The
285// per-girl hair guide simulation -- rest curves from the asset's own HSTR section, colliders FITTED from the
286// asset's own rig (VERT/SKEL/DYNA), a per-tick solver (spring-to-rest + damping + gravity + the world's wind
287// field + the girl's own motion as an inertial drag), follow-the-leader length projection alternating with
288// capsule push-out, DFTL velocity correction, and the 2-nearest-guide render weights -- lives HERE as arena
289// state. The page may do exactly two things with it: copy the asset's raw section BYTES into the scratch
290// below once at load (never a computed value), and copy the export block OUT to a GL texture each frame.
291// Every number a visitor sees on a strand is computed in this file.
292// UNITS: the NXA's bind (mesh) space, z up, integers. Blocks -> bind units through the page's own pairing
293// (HG_UNITS_PER_178CM per 1.78 m of stature; GPE_UNITS_PER_178CM in the emitter is the same pairing).
294// TICK: HG_TPS per second -- the fixed sim cadence the page's accumulator steps (16.6667 ms) and
295// nx_hairdyn_lib's hd_ticks_per_s() derives (sqrt(SB_MHZ_TICK_SQ)/1000 = 60).
296// PLANT: SB_K_HAIR / SB_C_HAIR over SD_G -- the gate-pinned hair plant of nx_softbind, unchanged, so every
297// guide answers with the stiffness and damping the retired one-chain page solver had.
298// FRAME: each guide is solved in HER bind space with the root pinned to its rest root; her world step
299// enters as the viscous drag a world-frame damper exerts (the incumbent lag at walking speed), her heading
300// rotates the world wind and step into bind space (the page's own R(-hdg).(m.x,m.z,-m.y) composition).
301// Colliders are the BIND-pose body (the same v1 limit the dress overlay declares: an extreme swing can clip).
302// =====================================================================================================
303const WC_CAST_N: i64 = 12 // the cast genmobs places (placed < WC_CAST_N); hair state is sized to it
304const HG_MOBS: i64 = WC_CAST_N
305const HG_NP: i64 = 8 // points per HSTR strand: the section contract (GR_PTS in nx_nxa_groom)
306const HG_W: i64 = HG_NP*3 // words per strand (GR_WORDS)
307const HG_NSEG: i64 = HG_NP - 1
308const HG_REND_MAX: i64 = 1200 // render strands: the page's shipped NXH_GUIDES budget, PRESERVED and moved here
309const HG_SIM: i64 = 128 // simulated guides per girl. PICKED, named as picked: 1200/128 = 9.4 render
310 // strands per guide sits inside the guide/follow band real-time hair ships
311 // (TressFX: 4..16 follow hairs per simulated guide); bounded above by the
312 // arena fit (T60) and the WebGL2 texture floor (HG_MOBS*HG_SIM/HG_TEX_SR rows)
313const HG_TEX_SR: i64 = 4 // strands per guide-texture row: rows = strands/4 keep every hair texture under
314 // the WebGL2 MAX_TEXTURE_SIZE minimum (2048) at HG_REND_MAX and HG_MOBS*HG_SIM
315const HG_TAB_W: i64 = 8 // render-strand table row: [rest-idx nb1 nb2 w1 w2 lockw spanz 0]
316const HG_CAP_MAX: i64 = 32 // collider slots (mask bits): head neck 2 shoulders 2 arms torso + DYNA anchors
317const HG_CAP_W: i64 = 10 // ax ay az bx by bz r kind sx sy
318const HG_KIND_CAP: i64 = 0
319const HG_KIND_ELL: i64 = 1
320const HG_Q12: i64 = WATER_MAGIC_4096 // ellipse-scale / segment-parameter fixed point: the engine's fx4096
321const HG_SKEL_W: i64 = 8 // words per SKEL joint: parent x y z + 4 (the 64-byte joint record the page reads)
322const HG_DYNA_MINW: i64 = 8 // a DYNA row carries side x y z k c max infl before an anchor is readable
323const HG_DYNA_MAXW: i64 = 64 // a row wider than this is not this format (nx_nxa_dyna writes 12); bounds the count check
324const HG_DYNA_INFL: i64 = 7 // DYNA word: influence radius (nx_nxa_dyna payload layout)
325const HG_HDR_BYTES: i64 = 512
326const HG_H_LOADED: i64 = 0
327const HG_H_NSTR: i64 = 1
328const HG_H_NREND: i64 = 2
329const HG_H_NSIM: i64 = 3
330const HG_H_SKIN: i64 = 4
331const HG_H_LOCKW: i64 = 5
332const HG_H_NCAP: i64 = 6
333const HG_H_SEGLEN: i64 = 7
334const HG_H_SPAN: i64 = 8
335const HG_H_TICKS: i64 = 9
336const HG_H_PUSHES: i64 = 10
337const HG_H_INSIDE: i64 = 11
338const HG_H_MAXDISP: i64 = 12
339const HG_H_H: i64 = 13
340const HG_H_ZMN: i64 = 14
341const HG_H_HJ: i64 = 15
342const HG_H_RC: i64 = 16
343const HG_H_NOCOLL: i64 = 17 // gate lever: 1 skips the push-out (the neg-control that proves it fires)
344const HG_H_RESTIN: i64 = 18 // rest nodes inside a collider BODY at load: the clipping the operator sees at rest
345const HG_H_WINDOFF: i64 = 19 // gate lever: 1 ignores the wind field
346const HG_H_GUST: i64 = 20
347const HG_H_TICKED: i64 = 21 // girls ticked on the last hairtick
348const HG_RC_NOSTR: i64 = 0 - 1 // load refusals, each naming its conjunct
349const HG_RC_HSTRCAP: i64 = 0 - 2
350const HG_RC_NOVERT: i64 = 0 - 3
351const HG_RC_VERTCAP: i64 = 0 - 4
352const HG_RC_NOSKEL: i64 = 0 - 5
353const HG_RC_SKELCAP: i64 = 0 - 6
354// arena regions: tail the skin scratch, every byte accounted, CRAFT_TOTAL re-derived below
355const O_HGHDR: i64 = O_SKIN3 + 24
356const O_HGREST: i64 = O_HGHDR + HG_HDR_BYTES
357const O_HGRL: i64 = O_HGREST + HG_REND_MAX*HG_W*8
358const O_HGCAP: i64 = O_HGRL + HG_SIM*HG_NSEG*8
359const O_HGMASK: i64 = O_HGCAP + HG_CAP_MAX*HG_CAP_W*8
360const O_HGSIMIDX: i64 = O_HGMASK + HG_SIM*8
361const O_HGTAB: i64 = O_HGSIMIDX + HG_SIM*8
362const HG_BLK_BYTES: i64 = HG_MOBS*HG_SIM*HG_W*8
363const O_HGX: i64 = O_HGTAB + HG_REND_MAX*HG_TAB_W*8 // positions
364const O_HGP: i64 = O_HGX + HG_BLK_BYTES // previous positions (velocity = X - P)
365const O_HGD: i64 = O_HGP + HG_BLK_BYTES // THE EXPORT: displacement from rest, the buffer the page copies per frame
366const O_HGSCR: i64 = O_HGD + HG_BLK_BYTES // solver scratch: correction (HG_W) + collider probe (6) + point (3)
367const HG_SCR_WORDS: i64 = HG_W + 9
368// GE32 (2026-09-02): the frame-budget telemetry window -- PF_WORDS i64 owned by nx_perf_lib, fed by the page's
369// perf_frame door once per frame, read back through the perf_* doors. Tails the hair scratch; CRAFT_TOTAL below.
370const O_PERF: i64 = O_HGSCR + HG_SCR_WORDS*8
371// GE30 PER-WORKER TILE SCRATCH. The ray pass used ONE scratch (O_ST slots 46-50) for its five wray
372// outputs, which is correct for one thread and a DATA RACE the instant a second worker renders a band
373// of the same frame: both would write the same five words and each would read the other's crossing-t.
374// Every worker therefore gets its own TILE_W-word slot -- camera basis at +0 (8 words, written by
375// wc_cam so the band pass and the overlay pass share ONE camera ruler) and the ray outs at +TILE_HOUT.
376// APPENDED at the arena tail, so no existing offset moves and the v6 save image is untouched.
377const O_TILE: i64 = O_PERF + PF_WORDS*8
378const TILE_W: i64 = 32 // i64 words per worker slot
379const TILE_HOUT: i64 = 8 // word offset of the 5 wray outs inside a slot
380const TILE_MAX: i64 = 32 // the most workers a band pass may be cut into; the thread ladder
381 // (knowledge/tier_ladder.conf) picks the count from measured
382 // hardware and the page clamps to this, so it can never ask for a
383 // scratch slot that does not exist.
384// ---- GE55 RAW UNIFORM BLOCK (2026-09-05, operator: no JS, no Python, WebGPU as a pure SUBMISSION PIPE) ----
385// The sim packs its OWN state for the GPU. 40 little-endian 32-bit words, 160 B, APPENDED at the arena tail like
386// every region before it (no existing offset moves). The words are RAW: fixed-point ints, Q12 angles, packed RGB,
387// flags. This lane carries no f32 lowering (measured 2026-09-05: nx_compile_wat has no f32 ops), so every float the
388// shader needs (cam/256, sin and cos of yaw and pitch, the palette light dl2 = .22+.78*pow(df,.6), the sky unpack,
389// the sun direction) is DERIVED ON THE GPU from the one IR (nx_world_shader_src), never in JS and never here.
390// Word index = byte offset / 4, chosen so the WGSL struct {20 scalars, pal as 3 vec4u, palf as 2 vec4u} lays out
391// IDENTICALLY under the uniform address-space rules (nx_wgsl layout is the ONE ruler; a gate pins both sides).
392// GE53 THE SURFACE SOURCE: one Q8 height per column (WX x WZ i64 slots), filled by gencol from the SAME
393// eroded, ocean-shaped field the voxel column is cut from, BEFORE the one line that rounds it to whole
394// blocks. Under P_SURF the raycaster marches this field (bilinear between columns = sub-block precision)
395// instead of the cell grid, and the page uploads it as a texture so every tier reads ONE surface. Placed
396// BEFORE the raw block so CRAFT_TOTAL == O_URAW + URAW_BYTES (T91) still holds; 128 KB inside T60's headroom.
397const O_HQ: i64 = O_TILE + TILE_MAX*TILE_W*8
398const HQ_BYTES: i64 = WX*WZ*8
399const O_URAW: i64 = O_HQ + HQ_BYTES
400const URAW_WORDS: i64 = 44 // GE53: 41 words (r_surf at 40) padded to the vec4u boundary
401const URAW_BYTES: i64 = 176 // URAW_WORDS*4: a vec4u-aligned struct, so the 3-word tail pad is real (was 160 beside WORDS=44 -- pack_uniforms zeroed 176 B into a 160 B block; corrected 2026-09-06 when the GE53 shader shipped its 41-word struct)
402const UR_CAMX: i64 = 0 // Q8 world units (the page used camx()/256)
403const UR_CAMY: i64 = 1
404const UR_CAMZ: i64 = 2
405const UR_TNOW: i64 = 3
406const UR_YAW: i64 = 4 // Q12 radians (the page used yaw()/4096)
407const UR_PITCH: i64 = 5
408const UR_SUNEL: i64 = 6 // Q12 sine of the sun elevation (cal_sunel)
409const UR_DAYT: i64 = 7
410const UR_DLEN: i64 = 8
411const UR_RAIN: i64 = 9
412const UR_SKT: i64 = 10 // packed RGB (spec word 7)
413const UR_SKH: i64 = 11 // packed RGB (spec word 8)
414const UR_SUN: i64 = 12 // spec word 9 (the GPU applies the df>.04 daylight guard)
415const UR_CLD: i64 = 13 // spec word 10
416const UR_WMAX: i64 = 14
417const UR_SELB: i64 = 15 // sel_block(S_SEL)
418const UR_HOK: i64 = 16
419const UR_NPCT: i64 = 17 // npc target t, or -1
420const UR_BROKEN: i64 = 18 // edit counters (broken + placed): the bridge refreshes the voxel texture when their sum moves
421const UR_PLACED: i64 = 19
422const UR_PAL: i64 = 20 // 12 packed RGB words (spec 16..27), vec4u-aligned at byte 80
423const UR_PALF: i64 = 32 // 5 packed RGB words (spec 34..38) at byte 128
424const UR_RESW: i64 = 37 // canvas width in px -- the door tells the sim through set_res (the sim has no window)
425const UR_RESH: i64 = 38
426const UR_SURF: i64 = 40 // Continuous terrain representation.
427const UR_WATER_Q8: i64 = 41 // Water surface from P_WATER, Q8 world units; words42..43 remain padding.
428const UR_YFLIP: i64 = 39 // 1 on the WebGPU door (top-left fragment origin), 0 on WebGL2 -- set_yflip
429// Dedicated contact scratch: two six-word bounds, one three-word result, three audit words.
430// Persisted SAVE_* ranges precede this region and remain unchanged.
431const CONTACT_BOX_WORDS: i64 = 6
432const CONTACT_RESULT_WORDS: i64 = 3
433const CONTACT_STATE_WORDS: i64 = 3
434const O_CONTACT: i64 = O_URAW + URAW_BYTES
435// Transient journal batch index owns its own fixed arena span; no persisted offsets move.
436const EJ_INDEX_BUCKETS: i64 = EJRN_SLOTS*2
437const O_EJ_INDEX: i64 = O_CONTACT + (2*CONTACT_BOX_WORDS+CONTACT_RESULT_WORDS+CONTACT_STATE_WORDS)*8
438const O_EJ_NEXT: i64 = O_EJ_INDEX + EJ_INDEX_BUCKETS*8
439const CRAFT_TOTAL: i64 = O_EJ_NEXT + EJRN_SLOTS*8
440// ---- GE31/GE23 THE CAST CLIP EXTENSION (2026-09-06) --------------------------------------------
441// The ENGINE evaluates the cast's animation clip (nx_nxa_anim_lib) from each girl's own path-length
442// stride phase and exports the finished dual-quaternion joint rows; the page's animEval became a copy.
443// WHY AN EXTENSION BEYOND CRAFT_TOTAL RATHER THAN A REGION INSIDE IT: (1) T91 pins O_URAW as the arena
444// tail and T60 pins CRAFT_TOTAL inside the backend's DECLARED pages; (2) the vm-gate runs init and
445// render inside those declared pages, and nothing here is touched until the page has grown memory
446// and called nxa_ingest; (3) the clip does not fit anyway -- ref9e's ANIM section is 113,465 words
447// (nx_asset_floor_gate, 2026-09-06), 887 KB against a 282 KB headroom, so a region inside the base
448// arena would have been a lie about the asset. The backend declares WASM_PAGES initial pages and cannot
449// read a module static (G3, see WASM_PAGES above), so the size gets ONE owner the honest way: the
450// engine exports wasm_pages_req() DERIVED from CRAFT_EXT_TOTAL and the page grows memory to it before
451// init -- the MT twin already declares max = 2x, so growth is inside its ceiling by construction.
452// Every cap below is named for its purpose, derived from the SHIPPED asset, and refused BY NAME in
453// wc_nxa_ingest when exceeded; an asset over a cap is the next ladder rung, never a silent truncation.
454const NXA_NJ_CAP: i64 = 128 // ref9e 104 joints; the corpus tops out at 370 (nx_nxa_play) = the next rung
455const NXA_ANIM_CAP_W: i64 = 131072 // 1 MiB of key words; ref9e ANIM measured 113,465 words
456const NXA_POSE_CAP_W: i64 = 2048 // ref9e POSE measured 835 words (one frozen stand pose)
457const NXA_HDR_W: i64 = 16
458const NXA_H_LOADED: i64 = 0
459const NXA_H_NJ: i64 = 1
460const NXA_H_DUR: i64 = 2
461const NXA_H_MPB: i64 = 3
462const NXA_H_RJ: i64 = 4
463const NXA_H_POSE: i64 = 5
464const NXA_H_RV: i64 = 6 // six words: x0 y0 vx16 vy16 t0 span (nx_nxa_anim_lib NA_RV_W)
465const NXA_H_RC: i64 = 12
466const NXA_H_NAW: i64 = 13
467// Formerly unused header words; existing region offsets remain unchanged.
468const NXA_H_NPW: i64 = 14
469const NXA_H_MORF_TOKEN: i64 = 15
470const NXA_MOB_W: i64 = 4 // per girl: idle weight q12, clip-time adjustment ms, two spare
471const NXA_M_IW: i64 = 0
472const NXA_M_CTADJ: i64 = 1
473// the page's SHIPPED idle behaviour, carried not re-chosen (nx_game_page_emit castFrame, 2026-08-30):
474// pose-hold eases in at 5/s and out at 2.5/s, the clip eases to the nearest double-support at 6/s
475// while the hold blends in below 0.95, and a hold under 0.004 snaps to zero.
476const NXA_IW_IN_TENTHS_PER_S: i64 = 50
477const NXA_IW_OUT_TENTHS_PER_S: i64 = 25
478const NXA_SNAP_TENTHS_PER_S: i64 = 60
479const NXA_SNAP_IW_MAX_Q12: i64 = 3891
480const NXA_IW_FLOOR_Q12: i64 = 16
481const NXA_DT_MS_MAX: i64 = 1000 // a tab that slept a minute eases one second, it does not slam
482const NXA_RC_NOT_LOADED: i64 = 0 - 1
483const NXA_RC_NO_MOB: i64 = 0 - 2
484const NXA_RC_SLOT: i64 = 0 - 3
485const NXA_RC_NJ_ZERO: i64 = 0 - 11
486const NXA_RC_NJ_CAP: i64 = 0 - 12
487const NXA_RC_NAW_ZERO: i64 = 0 - 13
488const NXA_RC_NAW_CAP: i64 = 0 - 14
489const NXA_RC_NPW_CAP: i64 = 0 - 15
490const NXA_RC_POSE_OVERRUN: i64 = 0 - 16
491const NXA_RC_TRACKS: i64 = 0 - 20 // plus nx_nxa_anim_lib's own rc: -21 bad tracks, -22 overrun
492// Dedicated compiler-static reservation: one WebAssembly allocation page.
493// Build the plain and shared doors with the source-exported arena/static limits.
494// The compiler MUST refuse actual static data beyond this end; a page budget is
495// not a claim that future globals fit. Native importers keep ordinary globals.
496const CRAFT_STATIC_RESERVE_PAGES: i64 = 1
497const CRAFT_STATIC_BASE_PAGES: i64 = WASM_PAGES
498const CRAFT_STATIC_LIMIT_PAGES: i64 = CRAFT_STATIC_BASE_PAGES+CRAFT_STATIC_RESERVE_PAGES
499const CRAFT_STATIC_BASE: i64 = CRAFT_STATIC_BASE_PAGES*WASM_PAGE_BYTES
500const CRAFT_STATIC_LIMIT: i64 = CRAFT_STATIC_LIMIT_PAGES*WASM_PAGE_BYTES
501const O_NXA: i64 = CRAFT_STATIC_LIMIT
502const O_NXA_SKEL: i64 = O_NXA + NXA_HDR_W*8
503const O_NXA_TIDX: i64 = O_NXA_SKEL + NXA_NJ_CAP*NA_SKEL_W*8
504const O_NXA_PQ: i64 = O_NXA_TIDX + NXA_NJ_CAP*NA_IDX_W*8
505const O_NXA_PD: i64 = O_NXA_PQ + NXA_NJ_CAP*4*8
506const O_NXA_MOB: i64 = O_NXA_PD + NXA_NJ_CAP*3*8
507const O_NXA_SCR: i64 = O_NXA_MOB + WC_CAST_N*NXA_MOB_W*8
508const O_NXA_OUT: i64 = O_NXA_SCR + NA_SCR_W*8
509const O_NXA_POSE: i64 = O_NXA_OUT + WC_CAST_N*NXA_NJ_CAP*NA_ROW_W*8
510const O_NXA_ANIM: i64 = O_NXA_POSE + NXA_POSE_CAP_W*8
511// Append the ordering sidecar so all existing asset and output offsets remain stable.
512const O_NXA_ORDER: i64 = O_NXA_ANIM + NXA_ANIM_CAP_W*8
513
514// Contact-distance state is appended after the existing clip regions. The dynamic
515// morph bank follows the derived extension end and keeps its existing offset API.
516const O_NXA_GAIT: i64 = O_NXA_ORDER+NXA_NJ_CAP*8
517const NXA_GAIT_CAP: i64 = NXA_ANIM_CAP_W/NA_CH2_KEY_W
518const O_NXA_GAIT_GENERATION: i64 = O_NXA_GAIT+NA_GAIT_INPUT_W*8
519const O_NXA_GAIT_HEADER: i64 = O_NXA_GAIT_GENERATION+8
520const O_NXA_GAIT_TABLE: i64 = O_NXA_GAIT_HEADER+NA_GAIT_HEADER_W*8
521const NXA_M_PATH_Q14: i64 = 2
522const NXA_M_MOVING: i64 = 3
523const NXA_PATH_Q14: i64 = NA_Q12*4
524const NXA_SCALE_BASE: i64 = 940
525const NXA_SCALE_STEP: i64 = 30
526const NXA_SCALE_DEN: i64 = 1000
527
528const WC_STEER_W: i64 = 5
529const O_MOB_STEER: i64 = O_NXA_GAIT_TABLE+NXA_GAIT_CAP*NA_GAIT_ROW_W*8
530const O_NXA_LOCOMOTION: i64 = O_MOB_STEER+MOB_CAP*WC_STEER_W*8
531const NXA_LOCOMOTION_RAW: i64 = 0
532const NXA_LOCOMOTION_FORWARD: i64 = 1
533const INSPECTION_WORDS: i64=18
534const O_INSPECTION: i64=O_NXA_LOCOMOTION+8
535const IC_MODE:i64=0
536const IC_HANDLE:i64=1
537const IC_GENERATION:i64=2
538const IC_ACTOR:i64=3
539const IC_X:i64=4
540const IC_Y:i64=5
541const IC_Z:i64=6
542const IC_YAW:i64=7
543const IC_PITCH:i64=8
544const IC_TX:i64=9
545const IC_TY:i64=10
546const IC_TZ:i64=11
547const IC_DISTANCE:i64=12
548const IC_ORBIT:i64=13
549const IC_ELEVATION:i64=14
550const IC_HW:i64=15
551const IC_HH:i64=16
552const IC_HD:i64=17
553// The serialized v7 image owns a disjoint arena. Existing physics/render offsets stay fixed.
554const O_SAVE:i64=O_INSPECTION+INSPECTION_WORDS*8
555const CRAFT_BYTES:i64=O_SAVE+SAVE_LEN
556const CRAFT_EXT_TOTAL:i64=CRAFT_BYTES
557// the engine's OWN page request -- derived, the one place the extended size is written
558const WASM_PAGES_REQ: i64 = (CRAFT_EXT_TOTAL + WASM_PAGE_BYTES - 1)/WASM_PAGE_BYTES
559// LOAD-TIME INPUT SCRATCH inside the framebuffer: the page copies the asset's raw HSTR/VERT/SKEL/DYNA section
560// bytes here and calls hairload, which consumes them before it returns; the next render overwrites the frame.
561// The caps are addresses inside a region the engine already owns (W*H*8 = 18.4 MB), not guesses about an
562// asset: DYNA 1 MiB holds 10,922 rows, HSTR 4 MiB 21,845 strands, VERT 8 MiB 349,525 vertices, SKEL 2 MiB
563// 32,767 joints. hairload REFUSES by name when a count exceeds its cap rather than reading past it.
564const HG_MIB: i64 = 1048576
565const HG_SCR_HSTR: i64 = O_FB
566const HG_SCR_HSTR_CAP: i64 = 4*HG_MIB
567const HG_SCR_VERT: i64 = O_FB + 4*HG_MIB
568const HG_SCR_VERT_CAP: i64 = 8*HG_MIB
569const HG_SCR_SKEL: i64 = O_FB + 12*HG_MIB
570const HG_SCR_SKEL_CAP: i64 = 2*HG_MIB
571const HG_SCR_DYNA: i64 = O_FB + 14*HG_MIB
572const HG_SCR_DYNA_CAP: i64 = HG_MIB
573// plant + units
574const HG_TPS: i64 = 60 // see the header: the page accumulator's 16.6667 ms, hd_ticks_per_s()
575const HG_UNITS_PER_178CM: i64 = 171530 // the page's pairing (GPE_UNITS_PER_178CM), named as the same number
576const HG_CM_178: i64 = 178
577const HG_CM_PER_M: i64 = 100
578const HG_UPB: i64 = HG_UNITS_PER_178CM*HG_CM_PER_M/HG_CM_178 // bind units per world block (1 block = 1 m): 96365
579const HG_G_CMS2: i64 = 981 // standard gravity, cm/s^2
580const HG_G: i64 = HG_G_CMS2*HG_UNITS_PER_178CM/(HG_CM_178*HG_TPS*HG_TPS) // 262 units/tick^2 -- the dress lane's NXGD_G derivation
581const HG_FTL_S_PERMIL: i64 = 1000 - SB_C_HAIR*1000/SD_G // DFTL s_damping (Mueller 2012 "Fast Simulation of Inextensible Hair
582 // and Fur", band .9..1): 942, from the plant's own damping, not picked
583const HG_FENCE_DIV: i64 = 4 // the chain's small-angle validity fence: seglen/4 (SD_Q8/4 in the retired page solver)
584const HG_PROJ_ITERS: i64 = 3 // length/collision alternations per point; the LAST op is always the length projection
585const HG_LOCK_W_MIN: i64 = 900 // the ribbon builder's lock half-width floor (w09 = max(900, span*.045)), PRESERVED
586const HG_LOCK_W_PERMIL: i64 = 45
587const HG_SKIN_NUM: i64 = 16 // skin = lockw x 1.6: half-width + the .6 child scatter -- the farthest a render vertex sits off its guide
588const HG_SKIN_DEN: i64 = 10
589const HG_REACH_DIV: i64 = 2 // collider mask reach = strand length / 2: twice the static fence (L/4), for overshoot
590const HG_GUST_TICKS: i64 = 2*HG_TPS // gust envelope cadence 0.5 Hz: the incumbent shader sway (3.1 rad/s), PRESERVED
591const HG_GUST_SALT: i64 = 3400 // whash seed offset (the weather lib salts with seed + 3300)
592const HG_TORSO_GATE_X: i64 = 15000 // the MFS torso gate (axb < 15000.), the same half-width the paint bands use
593// Drillis-Contini stature ratios (permil of the MEASURED stature), the landmarks the page derives its bands from
594const HG_R_HEADBAND: i64 = 885
595const HG_R_HEAD_J: i64 = 955
596const HG_R_ACROMION: i64 = 818
597const HG_R_CHEST: i64 = 720
598const HG_R_HIP: i64 = 520
599const HG_R_ELBOW: i64 = 630
600const HG_R_NECK_LO: i64 = 840
601const HG_R_SH_LO: i64 = 770
602const HG_R_SH_HI: i64 = 860
603const HG_R_ARM_LO: i64 = 600
604const HG_R_ARM_HI: i64 = 800
605const HG_R_ARM_XMIN: i64 = 60
606const HG_R_ELBOW_XMIN: i64 = 50
607
608// C3 thought ids (RimWorld's inspectable-mood law: a mood is a LEDGER, never a scalar mystery)
609const TH_RAIN: i64 = 1
610const TH_FEAR: i64 = 2
611const TH_MET: i64 = 3
612const TH_SLEPT: i64 = 4
613const TH_HUNGRY: i64 = 5
614const TH_SAWTAKEN: i64 = 6
615const TH_HEARD_TAKEN: i64 = 7
616const TH_HEARD_FRIEND: i64 = 8
617const TH_COMPANY: i64 = 9
618// C5 gossip predicates (Talk of the Town's law -- the differentiator no shipped game has)
619const GO_FRIEND: i64 = 1
620const GO_TAKEN: i64 = 2
621const SAVE_MAGIC: i64 = 20260728001
622const SAVE_VER: i64 = 7 // generation recipe trailer, existing body retained
623const SAVE_VER6: i64 = 6 // streaming origin and edit journal
624 // v5 still LOADS (origin 0, empty journal); others REFUSED
625const SAVE_VER5: i64 = 5
626// mob components (nx_entity_store comp slots)
627const MC_X: i64 = 0
628const MC_Y: i64 = 1
629const MC_Z: i64 = 2
630const MC_KIND: i64 = 3
631const MC_PH: i64 = 4
632const MC_DISG: i64 = 5 // 1 = a HUMAN girl disguised as a monster girl (the lore:
633 // monster girls took the world; humans hide among them --
634 // her features are a worn costume, not grown)
635const MC_GENE: i64 = 6 // 20-bit APPEARANCE GENOME rolled at spawn -- every girl is
636 // GENERATED, not hand-built: bits 0-2 hair hue, 3-4 hair
637 // length, 5-6 height, 7-8 outfit tint, 9-10 skin tone,
638 // 11-12 feature size. The slot bred inheritance plugs into.
639
640// state slots
641const S_SEED: i64 = 0
642const S_CX: i64 = 1 // camera pos Q8 (256 = one block)
643const S_CY: i64 = 2
644const S_CZ: i64 = 3
645const S_YAW: i64 = 4 // it-units (IT_PI = half turn)
646const S_PITCH: i64 = 5
647const S_T: i64 = 6
648const S_BROKEN: i64 = 7
649const S_PLACED: i64 = 8
650const S_HX: i64 = 9 // crosshair DDA hit cache
651const S_HY: i64 = 10
652const S_HZ: i64 = 11
653const S_HFACE: i64 = 12 // 0 none, 1 +x, 2 -x, 3 +y, 4 -y, 5 +z, 6 -z
654const S_HOK: i64 = 13 // 1 = in reach
655const S_VY: i64 = 14 // vertical velocity Q8/tick (jump + gravity)
656const S_GROUNDED: i64 = 15 // 1 = standing on a surface (jump allowed)
657const S_SEL: i64 = 16 // hotbar slot 0..4 (Q cycles; sel_block maps to a block type)
658const S_LBX: i64 = 17 // last-broken cell (probes/particles; the hit cache retargets
659const S_LBY: i64 = 18 // every frame, so "what did I just break" needs its own slots)
660const S_LBZ: i64 = 19
661const S_Q: i64 = 20 // ray pixel size 2..5 -- ADAPTIVE: the organ tunes itself from
662const S_FMA: i64 = 21 // the surface's reported frame ms (EMA x16); shim only reports
663const S_WMAX: i64 = 22 // highest solid y in the world -- rays above it heading up/flat
664 // are SKY immediately (kills the 110-step horizon marches)
665const S_SPX: i64 = 23 // spawn point (H key + void-fall auto-respawn return here)
666const S_SPY: i64 = 24
667const S_SPZ: i64 = 25
668const S_NPCT: i64 = 26 // crosshair NPC target: dense index of the girl under the
669 // gaze (occlusion-aware, walls block), -1 = none
670const S_FOUND: i64 = 27 // disguised humans FOUND (met + identified = under protection)
671const S_LOST: i64 = 28 // found humans TAKEN by wardens (the cost of not protecting)
672const S_BCD: i64 = 29 // birth cooldown ticks (community pacing)
673const S_WOX: i64 = 30 // WORLD ORIGIN of the window, in blocks (v6 streaming): the
674const S_WOZ: i64 = 31 // arena is a 128x48x128 CACHE over unbounded absolute terrain
675const S_EJDROP: i64 = 32 // edits REFUSED because the journal was full (loud, never silent)
676const S_HARV: i64 = 33 // MATURE crops harvested (the farming loop's payoff counter)
677const S_PLANT: i64 = 34 // seeds planted
678const S_GROWN: i64 = 35 // crops that reached maturity (world truth, gate-readable)
679const S_VAR: i64 = 36 // which world identity this arena runs (the passport's
680 // visited-mask bit; set once at init)
681const S_GENP: i64 = 37
682const S_TGT: i64 = 38 // DISPLAY-DERIVED frame budget, EMA-x16 units (0 = shipped
683 // defaults): the page measures the device's real rAF interval
684 // once and reports it via target_ms; every controller line then
685 // derives from the DEVICE instead of a hand-picked millisecond // W1c: gen-time override rows APPLIED at init (world truth --
686 // a gate can read whether the recipe actually reached genworld)
687const S_BLDG: i64 = 39 // W3 settlements: buildings stamped into this arena (a
688 // monotone stamp-event counter -- a window slide that
689 // repaints a building recounts it; right after init it IS
690 // the number of buildings in the window, which is what the
691 // gate reads). NON-PERSISTED world truth.
692const S_BLDGX: i64 = 40 // last-stamped building anchor, arena-local (gate probe)
693const S_BLDGY: i64 = 41 // its floor height
694const S_BLDGZ: i64 = 42
695const S_RESW: i64 = 44 // GE55: canvas size and origin, written by the door (set_res / set_yflip)
696const S_RESH: i64 = 45
697const S_YFLIP: i64 = 46
698const S_FACE_RADIUS: i64 = 49
699const S_FACE_GAIN: i64 = 50
700const FACING_RADIUS_MAX_Q8: i64 = 2147483647
701const S_AGEN: i64 = 47
702const S_DPAPPLIED: i64 = 48 // GE59: the P_DAYPHASE value last applied to S_T (0 = never) -- one re-base per distinct poke, never per tick // GE57: arena GENERATION -- +1 per wc_shift that moves. The GPU tiers key every voxel-texture upload on it: a 16-block re-base drawn against the previous texture is the transport/fall-through the operator sees (2026-09-05).
703const S_GT_KEEP: i64 = 43 // ground-truth capture: when 1, render_impl leaves the depth plane in
704 // the framebuffer's high bits instead of stripping it (nx_world_snap
705 // gt verb reads it and emits depth/normal channels). Zeroed by init, so
706 // the page and every gate keep the published colour-only format.
707const SHIFT_STRIDE: i64 = 16 // one window slide, in blocks
708const SHIFT_MARGIN: i64 = 32 // player this close to a window edge -> the world slides under them
709
710// spec slots (the game-identity DATA the engine reads -- nx_voxel_world's style-is-a-spec doctrine)
711const P_TBASE: i64 = 0 // terrain base height
712const P_TAMP1: i64 = 1 // large-noise amplitude
713const P_TAMP2: i64 = 2 // detail-noise amplitude
714const P_WATER: i64 = 3 // flood level
715const P_SNOW: i64 = 4 // snow line (99 = never)
716const P_TREE: i64 = 5 // tree sparsity modulus (0 = no trees)
717const P_CAVETH: i64 = 6 // cave carve threshold (lower = more cavern)
718const P_SKYTOP: i64 = 7 // packed zenith colour
719const P_SKYHOR: i64 = 8 // packed horizon colour
720const P_SUN: i64 = 9 // 1 = sun disc
721const P_CLOUD: i64 = 10 // 1 = cloud deck
722// WEATHER EXPORTS (2026-08-26): written each tick by wc_weather_tick from nx_weather_lib -- the
723// gate-proven continuous system (rates preserved to P_RAINS, front persistence, typed precip,
724// gradient wind). The page reads these five slots exactly as it reads the sky tones.
725const P_WXCOV: i64 = 11 // cloud coverage Q12 (drives the cloud density band)
726const P_WXWINDX: i64 = 12 // wind x component, Q12-scaled speed*cos(dir)
727const P_WXWINDZ: i64 = 13 // wind z component
728const P_WXTYPE: i64 = 14 // precip type this eighth: 0 none, 1 rain, 2 snow
729const P_WXTEMP: i64 = 15 // temperature Q12 (relative scale, freezing=1024)
730const P_PAL: i64 = 16 // +blocktype = packed base colour (0..11)
731const P_MOBC: i64 = 28 // +kind*2+half = packed mob colours (3 kinds x 2 tones)
732const P_PAL2: i64 = 34 // +b-12 = packed colour for farm blocks 12..16 (5 rows, 34-38)
733const P_DAYLEN: i64 = 40 // A1 calendar: ticks per day (ALL timing is spec DATA)
734const P_SEASD: i64 = 41 // days per season (4 seasons cycle)
735const P_GROWE: i64 = 42 // farm-tick cadence: one grow/hydrate pass every N ticks
736const P_GROWW: i64 = 43 // crop speed on WET farmland (Minecraft stencil speed)
737const P_GROWD: i64 = 44 // crop speed on DRY farmland
738const P_RAINS: i64 = 45 // +season = rain permil per day-eighth (4 rows, 45-48)
739// ---------------------------------------------------------------------------------------------
740// ART STYLE IS DATA (2026-09-01). Spec idx 49 -- one of the TWO dead holes this plane has carried
741// since it was laid out (39 and 49; world_recipes.conf names them both). 49 is the safer of the
742// two: P_PAL2's five farm rows 34-38 could plausibly widen by one, while P_RAINS' four rows are
743// four SEASONS and cannot. It sits inside the 7..58 band the recipe plane pokes POST-BOOT, which
744// is where a LOOK belongs -- a style is a property of how the world is drawn, never of how the
745// ground was generated, so it must not become gen-time data.
746// THE VOCABULARY IS ADOPTED, NOT INVENTED. buildroot/runtime/nx_voxel_world.nx line 2 has read
747// "Style is a spec field, NOT a hardcode -- the engine is not locked to one look" since it was
748// written, with const SP_STYLE and STYLE_REAL / STYLE_TOON / STYLE_FLAT, and
749// _hdl_build/nx_world_style_gate.nx already PROVES the three produce three DISTINCT frames from
750// one spec. That organ has SEVEN importers and EVERY ONE OF THEM IS A GATE (measured 2026-09-01,
751// nx_absent matches=8 coverage_complete=1 corpus_complete=1): the doctrine was proven and never
752// shipped. This is that doctrine reaching the SERVED engine, under the SAME three ids so the two
753// implementations cannot drift into two vocabularies for one idea.
754// SCOPE, STATED SO IT CANNOT BE OVERREAD: this is the SHADING axis. The world is still made of
755// voxels -- vox_off() -> O_VOX is still the only world door, and a second REPRESENTATION is a
756// separate, larger contract. A toon or flat world is a voxel world that stops looking photoreal;
757// it is not a world that stopped being cubes.
758// 0 = REAL, and 0 is what wc_spec's zeroing loop (while k < WC_SPECN) already writes for every
759// identity, so every world published before this renders BYTE-IDENTICALLY BY CONSTRUCTION rather
760// than by a preserved branch anyone could later delete.
761const P_STYLE: i64 = 49
762const P_DAYPHASE: i64 = 39 // GE59 (2026-09-05): the boot clock as DATA -- the OTHER dead hole (world_recipes.conf names 39 and 49). Post-boot band, so it cannot be read at init: apply2_impl re-bases S_T to it on the next tick, once per distinct value; 0 (the zeroing loop) leaves the DAYLEN/4 boot phase, so every existing world is byte-identical by construction. If P_PAL2 ever widens, this slot moves with the ONE named const.
763const WC_STYLE_REAL: i64 = 0 // textured, smooth light -- the shipped look, unchanged
764const WC_STYLE_TOON: i64 = 1 // banded light (anime/cel)
765const WC_STYLE_FLAT: i64 = 2 // untextured facets (artsy low-poly)
766const WC_STYLE_SOFT: i64 = 3 // SMOOTH TERRAIN: ground lit by a heightfield normal interpolated across the block (no grid bevel, no per-block facet)
767// SOFT neighbour scan: how far above/below the hit cell a neighbour column's top is searched. Bounded by
768// where the heightfield normal SATURATES -- a rise of 3 blocks over 1 already lights as a near-vertical
769// face, so a taller step is a cliff and reads correctly as the flat fallback. Derived, not tasted.
770const WC_SOFT_SCAN: i64 = 3
771const WC_STYLE_MAX: i64 = 3 // the highest id this engine owns; set_style REFUSES above it
772// nx_voxel_world's toon_band works on a 0..255 "bright"; wshade2 carries light in PERMIL
773// (wface_l returns 420..1000). Each of its three constants is therefore converted x1000/255 and
774// NOTHING here is tasted: 150 -> 588.2 -> 588, 105 -> 411.8 -> 412, 235 -> 921.6 -> 922.
775const WC_TOON_CUT: i64 = 588
776const WC_TOON_LO: i64 = 412
777const WC_TOON_HI: i64 = 922
778// FLAT is DERIVED, not chosen. The texture term enters this shader as
779// light = light*(870 + tex*260/256)/1000
780// which is exactly 1.000 at tex = 128, so 128 IS this shader's own no-op texel and "flat" is
781// spelled "hold tex at the value that makes the texture term vanish". No second constant is
782// introduced, and the incumbent does the same thing from the other side: its texel_col computes
783// its noise term ONLY under STYLE_REAL and adds zero otherwise.
784const WC_TEX_NEUTRAL: i64 = 128
785const P_ADV: i64 = 50 // +i = smart-object ADVERTISEMENTS (C1): packed
786 // block<<16 | need<<8 | strength; 6 rows 50-55, 0 = end.
787 // NPC minds read OBJECTS as data -- adding a row changes
788 // village life with zero new NPC code (The Sims' law).
789const P_HISTAGE: i64 = 60 // DECLARED geomorphic stage (Strahler bands): 0 = mature/
790 // equilibrium (HI 300-600 permil), 1 = youthful (HI >= 600).
791 // A world DECLARES its stage and gate T53 checks the
792 // MEASUREMENT matches -- widening one band until everything
793 // fits would hide a young landscape. (Ported 2026-08-02:
794 // existed only on the laptop line; the NAS gate ran 52 teeth
795 // while local ran 53 -- the LOCAL-IS-NOT-A-SUPERSET fork.)
796const P_TREEMAX: i64 = 59 // tree altitude ceiling (0 = the historic hardcoded 19)
797const P_TREESPEC: i64 = 61 // W2 SPECIES GENOME (packed, 0 = legacy 4-block stamp):
798 // bits 0-3 trunk base, 4-7 trunk variance, 8-11 canopy
799 // radius (<=4, the widened seam scan bound), 12-14 shape
800 // (0 legacy, 1 dome, 2 pine), 15-17 leaf-drop density.
801 // Bits 18-20 UNDERSTORY density + 21-22 kind (0 tufts,
802 // 1 shrubs, 2 mixed) -- W2b ground vegetation in the same
803 // genome; tree bits 0..17 zero = legacy forest preserved.
804 // A world declares its FOREST the way it declares its
805 // terrain -- species = parameters, individuals = seeded
806 // variation inside the envelope (the Infinigen law).
807const P_WARD: i64 = 58 // warden daytime hunt radius in BLOCKS (0 = the 12-block
808 // default). A world declares its own danger; the arriving
809 // traveler's LEVEL scales it further (isekai: new world,
810 // new challenge, and the challenge KNOWS who showed up).
811const P_OUTFIT: i64 = 57 // outfit mode: 0 = identity keeps everyone clothed;
812 // 1 = MIXED (beach): genome bits 18-19 pick each girl's
813 // beachwear -- clothed / topless / nude -- stable per
814 // girl, bred like every trait. Identity DATA decides
815 // whether the dimension is active at all.
816const P_CURI: i64 = 56 // curiosity threshold: a girl approaches the player only
817 // while her loneliness exceeds this (HABITUATION -- the
818 // operator's "they just follow me around": company
819 // satisfies, satisfied girls walk on with their lives.
820 // Fear never habituates; shy girls always keep distance).
821
822// GAME-FEEL TUNABLES -- every one carries a row in knowledge/gamefeel_oracle.conf (banked
823// reference bands: biomechanics, shipped-sim constants, modding-community defaults) and
824// nx_gamefeel_oracle_gate REFUSES any value outside its cited band. Feel is derived, not tasted.
825const P_BLDG: i64 = 62 // W3 settlement density: site-cell modulus (0 = none; the
826 // recipe plane can override it, idx 62 is gen-time data)
827const P_BLDGSPEC: i64 = 63 // low 4 bits = archetype row in nx_indoorgen_data
828const P_ERODE: i64 = 64 // E1 TALUS EROSION BUDGET in relaxation ticks (0 = OFF).
829 // Gen-time recipe DATA (world_recipes.conf spec idx 64),
830 // applied to the spec BEFORE genworld reads it. 0 does not
831 // merely default to nothing: it takes the branch out
832 // entirely, so a world that does not opt in generates the
833 // byte-identical terrain it generated before this existed.
834const P_ERTAL: i64 = 65 // DERIVED talus threshold, WC_ER_Q units. NEVER authored:
835 // wc_er_talus MEASURES it as the terrain's own mean per-cell
836 // largest 4-neighbour drop -- wp_erode_talus_derived's rule
837 // -- so material sheds where the ground is steeper than the
838 // ground's own average and no repose angle is picked by
839 // taste. Sentinels: 0 = not yet measured, WC_ER_FLAT = -1 =
840 // measured and the world has NO relief, which is the case
841 // wp_erode_bake_derived REFUSES rather than reporting a bake
842 // it did not do.
843// GE13b -- CLOTHING IS DATA (2026-08-30). The 3-value outfit (0 clothed / 1 topless / 2 nude) WAS
844// the whole wardrobe, and SWIMWEAR-ONLY rendered identically to a clothed village because no
845// swimwear garment existed anywhere in the engine (operator: "random dresses not beach outfits").
846// The wardrobe is now a GARMENT TABLE in this same spec plane: rows P_GARM .. P_GARM+GAR_N-1, one
847// packed word per garment, seeded by wc_spec for EVERY identity before its branch (an identity or a
848// recipe row overrides a garment exactly the way it overrides the sky). Garment id 0 = nothing worn
849// and is not a row; id i lives at sp[P_GARM + i - 1]. A ZERO row means "this garment does not exist
850// here" -- the selector then hands out nothing in that slot, which is what makes the gate's
851// missing-swim-row neg-control bite instead of silently reading a bare word as a garment.
852// bits 0-1 COVERAGE bit0 covers the top, bit1 the bottom: 1 top / 2 bottom / 3 full (one-piece,
853// dresses). The legacy 0/1/2 is DERIVED from the worn set's OR of these bits.
854// bits 2-4 CATEGORY 0 beach-swim 1 beach-cover 2 casual 3 evening
855// bits 8-16 HUE BASE degrees 0..359, where this garment's palette starts
856// bits 17-22 HUE SPAN degrees/4: genome bits 7-8 (the documented OUTFIT-TINT bits, MG_OUTFIT_SH)
857// pick one of four stops across it -- a girl's swimsuit colour is BRED like
858// her hair, and a bikini top and bottom share the stop, so the set matches
859// bits 23-26 SATURATION /15 bits 27-30 LIGHTNESS /15 (integer HSL, wc_hsl_rgb)
860// Index 69 stays UNOWNED on purpose: nx_world_recipe_gate T19 pins "spec idx 69 refuses" as the
861// recipe validator's upper edge, and putting a garment there would turn a pinned edge into a row.
862const P_GARM: i64 = 70
863const GAR_N: i64 = 9
864const GAR_BIKINI_TOP: i64 = 1
865const GAR_BIKINI_BOT: i64 = 2
866const GAR_ONEPIECE: i64 = 3
867const GAR_SARONG: i64 = 4
868const GAR_SUNDRESS: i64 = 5
869const GAR_TEE: i64 = 6
870const GAR_SHORTS: i64 = 7
871const GAR_SKIRT: i64 = 8
872const GAR_DRESS: i64 = 9
873const GAR_COV_TOP: i64 = 1
874const GAR_COV_BOT: i64 = 2
875const GAR_COV_FULL: i64 = 3
876const GAR_CAT_SWIM: i64 = 0
877const GAR_CAT_COVER: i64 = 1
878const GAR_CAT_CASUAL: i64 = 2
879const GAR_CAT_EVENING: i64 = 3
880const GAR_CAT_NONE: i64 = 4 // what mobgarc reports for a girl wearing nothing
881const GAR_CAT_SH: i64 = 2
882const GAR_CAT_MASK: i64 = 7
883const GAR_HUE_SH: i64 = 8
884const GAR_HUE_MASK: i64 = 511
885const GAR_SPAN_SH: i64 = 17
886const GAR_SPAN_MASK: i64 = 63
887const GAR_SPAN_UNIT_DEG: i64 = 4
888const GAR_SAT_SH: i64 = 23
889const GAR_LIT_SH: i64 = 27
890const GAR_NIB_MASK: i64 = 15
891const GAR_TINT_STOPS: i64 = 4 // MG_OUTFIT_MASK + 1: two genome bits, four stops
892const GAR_SLOT_SH: i64 = 8 // outfit word = upper id << GAR_SLOT_SH | lower id
893const GAR_ID_MASK: i64 = 255
894const HSL_DEG_FULL: i64 = 360
895const HSL_SECTOR_DEG: i64 = 60
896const MG_BEACH_SH: i64 = 18 // the beachwear genome bits the MIXED beach breeds (P_OUTFIT)
897const MG_BEACH_MASK: i64 = 3
898const MOB_POSE_SLEEP: i64 = 2 // mob_pose's published sleep id (the pose door's contract)
899const B_WATER: i64 = 4 // the water block (wshade2's shimmer branch, the shore ramp)
900const WC_SPECN: i64 = 80 // includes GA_DUNE at slot 79 within the existing 640-byte spec allocation
901 // rows 70-78 (69 left unowned, see P_GARM). Was a bare 64 inside wc_spec.
902 // O_EJRN - O_SPEC = 640 B = 80 words, so this grows without
903 // moving a single other offset (the same law O_GENP was
904 // placed under).
905// GE13 -- THE OCEAN IS DATA (wsp_beach_ocean). One packed spec row turns any identity's far
906// side into a sea: bits 0-1 EDGE (0=+z 1=-z 2=+x 3=-x), bits 2-11 SHORELINE absolute
907// coordinate + 512 (WORLD space, so the streaming window regenerates it consistently),
908// bits 12-17 SLOPE width in blocks (the shore ramps down across this run), bits 18-23
909// SEABED height. 0 = no ocean: wsp_beach_ocean returns the height UNTOUCHED, so every
910// ocean-less identity generates byte-identically BY CONSTRUCTION (the KAT the gate holds).
911const P_OCEAN: i64 = 66
912// GE14a -- THE STAGED PROCGEN BIND. 0 = OFF, and OFF is the LEGACY TERRAIN BYTE-FOR-BYTE by
913// construction (wheight_raw returns before touching any of it). Non-zero = ON, and the value IS
914// the VERTICAL DIVISOR that maps nx_worldpipe_core's height space into this engine's block
915// column -- one row carrying both the switch and the scale, the shape P_ERODE already uses.
916// WHY A DIVISOR AND NOT A TASTED CONSTANT: the core's spline runs about -430..+420 before
917// relief, while a column here is 1..WY-8 blocks, so SOME map is required and the only honest
918// place for it is the recipe -- a world declares how much of its own vertical band it wants to
919// use, and nx_wasm_craft_gate asserts the resulting spread actually FILLS that band instead of
920// clamping flat at either end (a divisor that collapses the world is a measurable defect, not a
921// matter of taste).
922const P_WPIPE: i64 = 67
923// GE14a -- THE HORIZONTAL SCALE, and it is not a cosmetic knob: it is the unit conversion
924// between two coordinate systems, and MEASUREMENT is what proved it necessary. The staged
925// core's parameter fields have wavelengths of 1300..3400 units (continentalness the longest),
926// while a world window here is 128 BLOCKS. At 1 unit per block the whole window samples
927// essentially ONE POINT of the continent field, so the spline contributes a constant and only
928// the short ridge/detail terms vary: T79 measured spread 9 at divisor 30 and spread 10 at
929// divisor 18 -- a 40 percent smaller divisor bought ONE BLOCK, which is the signature of a
930// term that is constant across the sample, not of a scale that needs tuning. No vertical
931// divisor can fix that; the window has to span more of the field. 0 or 1 = one unit per block
932// (the legacy sampling); N = N units per block, so a 128-block window spans 128*N units.
933const P_WPSCALE: i64 = 68
934const P_SURF: i64 = 69 // GE53 SURFACE SOURCE: 0 = the cell grid (all voxel, byte-identical to
935 // every world shipped before this row), 1 = the continuous heightfield
936 // (no voxel ground: the ray marches the Q8 field). An identity declares
937 // it as DATA in wc_spec. All voxel or no voxel -- there is no partial.
938// The field is bilinear BETWEEN columns, so a sample a quarter block apart can never skip a column: the
939// marcher's step and the gradient's baseline are both this one span (Q8 units). DERIVED from the field's
940// own resolution (one column per block), not tuned.
941const WC_SURF_STEP_Q8: i64 = 64
942const WC_GAIT_BOB_Q8: i64 = 7 // thorax oscillation AMPLITUDE (~2.7cm, 5.5cm p2p --
943 // the band is peak-to-peak/2; 14 was a p2p-vs-amplitude
944 // conflation the recorder exposed as 11.3cm of chest)
945// RESPIRATION -- the idle drive. Derived, not tasted: resting adult breathing is 12-16/min;
946// at 14/min on the fixed 60Hz tick that is 60*60/14 = 257 ticks per cycle (0.233 Hz, inside the
947// 0.20-0.30 Hz band this rung is gated on). Amplitude is scaled off the GAIT constant above so
948// the two drives share one ruler: 7 q8 ~ 2.7cm => 1 q8 ~ 0.386cm, so 2 q8 ~ 0.77cm, inside the
949// declared 0.4-0.8cm resting-excursion band. A third value would have been a guess; this is an
950// arithmetic consequence of a constant already cited.
951const WC_BREATH_TICKS: i64 = 257
952// ★ AMPLITUDE, NOT PEAK-TO-PEAK -- and I got this wrong ONCE ALREADY on the gait constant.
953// Conversion, derived from the cited gait row: 7 q8 amplitude is documented as ~2.7cm, and
954// 7/256*100cm-per-block = 2.73cm, so the unit is q8 world units and p2p = 2*amp/256*100 cm.
955// At 2 q8 that is 1.56cm p2p -- OUTSIDE the 0.4-0.8cm resting band this rung is gated on, i.e.
956// 2x too large, the SAME p2p-vs-amplitude conflation the recorder caught in WC_GAIT_BOB_Q8 (14
957// -> 7). At 1 q8: 2/256*100 = 0.78cm p2p, inside the band. Caught by doing the arithmetic in the
958// unit the CRITERION is written in, before it shipped visibly.
959// ⚠KNOWN LIMIT, to be measured not assumed: 1 q8 is near the world-q8 resolution floor, so the
960// anchor steps coarsely; the spring integrates and should low-pass it, but ?breath=1 reports the
961// actual excursion so this is decided by measurement rather than by my expectation.
962const WC_BREATH_Q8: i64 = 1
963// speed at which GAIT fully owns the anchor. PROVISIONAL and deliberately marked so: the units
964// of the per-tick mob delta were never measured, so this is the one number here without a
965// derivation. The mobspd door added below exists to REPLACE it with a measurement rather than
966// leave a guess sitting in the engine pretending to be a constant.
967const WC_BREATH_CUT_Q8: i64 = 24
968const WC_STEP_EASE_UP: i64 = 48 // mob Y climb rate Q8/tick (a 1-block rise ~ 5 ticks)
969const WC_NIGHT_X8: i64 = 5 // night begins at this eighth of the day (5/8)
970
971const EYE: i64 = 435 // eye height Q8 (~1.7 blocks)
972const SPEED: i64 = 40 // walk Q8 per tick
973// GAIT DRIVE CONSTANTS -- declared HERE, above their first reader in the mob tick. The compiler
974// refuses a module const used before its declaration because it would silently read 0, and for the
975// gait accumulator that would have meant a permanently frozen drive that still compiled, still ran,
976// and looked exactly like the metronome it was written to replace.
977const WC_PH_PER_TICK: i64 = 5 // the free-running advance MC_PH still uses for WANDER
978const WC_PH_ANG: i64 = 214 // angle-units per phase unit (the incumbent's drive scaling)
979const WC_NEED_GAIT: i64 = 3 // the needs row's spare slot, named now that it carries a value
980// ---- THE GAIT MODEL, CITED (knowledge/gamefeel_oracle.conf, gait_* rows) -----------------------
981// Every number below is a published normative measurement carrying its cohort and its instrument.
982// None is a taste, and none may be "tuned" without moving the conf row and its source with it.
983// ⚠UNIT DISCIPLINE, AND THIS FILE HAS ALREADY PAID ONCE FOR GETTING IT WRONG (WC_GAIT_BOB_Q8 went
984// 14 -> 7 on exactly this): the gait literature reports RANGE of motion -- rRoM, ROM -- which is
985// PEAK-TO-PEAK. The AMPLITUDE is HALF of it. Every constant here is stored as the PUBLISHED RANGE
986// in DECIDEGREES so it can be compared straight against the paper, and the halving happens in
987// exactly one function (wc_amp_ddeg) so no caller can forget it.
988const WC_DDEG_HALFTURN: i64 = 1800 // decidegrees in IT_PI (a half turn): the ddeg -> it-unit scale
989// STRIDE = 2 x step length 0.73 m (mean, 21-30 age band, markerless normative database).
990// 1 BLOCK = 1 m is NOT assumed here, it is read off the engine: EYE = 435 q8 = 1.70 blocks is the
991// player's eye height and adult eye height is ~1.6-1.7 m, so a block IS a metre to within the
992// figure's own tolerance. 1.46 m = 1.46 blocks = 374 q8.
993// ★THE PUBLISHED TRIPLE RECONCILES, WHICH IS A FREE AUDIT OF THE SOURCE: step 0.73 m x cadence
994// 108.40 steps/min / 60 = 1.319 m/s against that table's own reported gait speed of 1.30 m/s.
995const WC_STRIDE_Q8: i64 = 374
996// PELVIS transverse rotation, rRoM = 5.2 deg (n=201 healthy adults, 5 km/h, surface topography).
997// ⚠SCOPE, STATED: surface topography estimates the pelvis from PSIS surface markers and reads
998// LOWER than bone-pin and marker-cluster methods, which put pelvic transverse range nearer 10 deg.
999// 5.2 is therefore a FLOOR from one named instrument, not a settled value, and it is used as the
1000// reference the genome varies AROUND rather than as a ceiling.
1001const WC_PELVIS_ROT_DDEG: i64 = 52
1002// THORACIC counter-rotation, rRoM at T7 = 14.6 deg -- and this is the paper's headline finding,
1003// explicitly CONTRARY to the previous functional understanding that T7 is the near-static point of
1004// intersection: T7/T8 carry the LARGEST rotary amplitude of every vertebra C7..L4, and "the largest
1005// rotary opposition was also found between the pelvis and T7/T8". The thorax rotates roughly THREE
1006// TIMES the pelvis, against it.
1007// ★THIS PHASE RELATIONSHIP IS THE MECHANISM READ AS "SWINGING HIPS". It is not an amplitude: what
1008// the eye sees is the pelvis and the shoulders twisting in OPPOSITION about the line of travel.
1009// It is encoded as a SIGN below and therefore needs no constant of its own -- deliberately absent.
1010const WC_THORAX_ROT_DDEG: i64 = 146
1011// ELBOW flexion-extension ROM through the gait cycle = 29.7 deg (SD 10.2). The forearm is NOT
1012// rigid in walking; a locked elbow is the "stiff arms" report stated as a measurement, and this
1013// row is what makes it a defect rather than an opinion.
1014const WC_ELBOW_ROM_DDEG: i64 = 297
1015// TURN ALLOWANCE, expressed as heading change per STRIDE rather than per tick, so it is
1016// speed-coupled BY CONSTRUCTION: a standing girl cannot turn and a walking one turns in
1017// proportion to the ground she covers. ⚠DERIVED-BY-ARGUMENT, NOT MEASURED, AND LABELLED SO: a
1018// walking body re-places its feet once per stride and cannot change heading by more than about a
1019// quarter turn in that time. Cross-check against the two rulers this engine already owns -- at
1020// the cited stride and a mob's 4 q8/tick it works out at 68 it-units/tick = 57 deg/s, a little
1021// over half the player's own TURN slew of 140 it-units/tick = 117 deg/s, and inside the 60-90
1022// deg/s that comfortable walking turns are usually quoted at. What would SETTLE it is a cited
1023// walking turn-rate band in gamefeel_oracle.conf; there is not one, and inventing a row that
1024// looked cited would be worse than naming the gap.
1025const WC_TURN_PER_STRIDE: i64 = IT_PI/2
1026const TURN: i64 = 140 // yaw it-units per tick (keyboard turn / legacy dir path)
1027const PITCHV: i64 = 90 // pitch it-units per tick (R/F keyboard look)
1028const PITCH_MAX: i64 = 5000
1029const LOOKS: i64 = 6 // mouselook: it-units per accumulated look count
1030const JUMPV: i64 = 66 // jump takeoff velocity Q8/tick
1031const GRAV: i64 = 9 // gravity Q8/tick^2
1032const VTERM: i64 = 120 // terminal fall velocity Q8/tick -- ONE ruler for the player AND every mob
1033 // (GE12 mob_step_gait; was an inline 120 in apply2_impl)
1034// FOOTSTRIKE/LANDING IMPULSE unit bridge -- ARITHMETIC of two existing conventions, not a tuning.
1035// Mob positions are world-q8 (256/block) and the softbind anchor is fed pos/4 (the
1036// sb_tick_mob_gene call in mob_tick), so ONE MODEL UNIT = 4 world-q8. sd_impulse's own contract
1037// (nx_softdyn: "model-units/tick * 256") takes model-q8/tick velocity kicks. Therefore one
1038// world-q8/tick of mob velocity = SD_Q8/4 solver velocity units, and a mob velocity change dv
1039// converts as dv*WC_IMP_SCALE with no free parameter anywhere in the chain.
1040const WC_IMP_SCALE: i64 = SD_Q8/4
1041const MAXSTEPS: i64 = 110 // DDA cells per ray (view distance)
1042const REACH_STEPS: i64 = 20 // crosshair edit reach in DDA cells
1043
1044func wst(base: i64) -> *i64 { return (base + O_ST) as *i64 }
1045func wsp(base: i64) -> *i64 { return (base + O_SPEC) as *i64 }
1046func wgp(base: i64) -> *i64 { return (base + O_GENP) as *i64 }
1047func mobp(base: i64) -> *i64 { return (base + O_MOBS) as *i64 }
1048func wfb(base: i64) -> *i64 { return (base + O_FB) as *i64 }
1049func wvx(base: i64) -> *u8 { return (base + O_VOX) as *u8 }
1050func winp(base: i64) -> *i64 { return (base + O_INPUT) as *i64 }
1051func wpack(r: i64, g: i64, b: i64) -> i64 { return r | (g << 8) | (b << 16) }
1052func wclamp(v: i64, lo: i64, hi: i64) -> i64 { if v < lo { return lo } if v > hi { return hi } return v }
1053
1054func vin(x: i64, y: i64, z: i64) -> i64 {
1055 if x < 0 { return 0 } if x >= WX { return 0 }
1056 if y < 0 { return 0 } if y >= WY { return 0 }
1057 if z < 0 { return 0 } if z >= WZ { return 0 }
1058 return 1
1059}
1060func vget(base: i64, x: i64, y: i64, z: i64) -> i64 {
1061 if vin(x, y, z) == 0 { return 0 }
1062 let v: *u8 = wvx(base)
1063 return (v[(y*WZ + z)*WX + x] & 0xff) as i64
1064}
1065func vset(base: i64, x: i64, y: i64, z: i64, b: i64) -> i64 {
1066 if vin(x, y, z) == 0 { return 0 }
1067 let v: *u8 = wvx(base)
1068 v[(y*WZ + z)*WX + x] = b as u8
1069 return 1
1070}
1071
1072// ---------- edit journal (v6 streaming): every PLAYER edit recorded at ABSOLUTE world coords ----------
1073// One packed i64 hash slot per touched cell: (key+1)<<8 | block, key = ax+2^24 (25b) : az+2^24 (25b)
1074// : ay (6b). The journal is the persistent truth for touched land; the window is a cache over
1075// (seed + journal) -- the voxchunk law (virgin land from the seed, touched land from history) wired
1076// into the live game. Bounded EJRN_SLOTS edits; a FULL journal REFUSES the whole edit LOUDLY
1077// (S_EJDROP counts refusals) so world and history can never disagree.
1078func ejp(base: i64) -> *i64 { return (base + O_EJRN) as *i64 }
1079func ej_key(ax: i64, ay: i64, az: i64) -> i64 {
1080 return ((ax + WATER_MAGIC_16777216) << 31) | ((az + WATER_MAGIC_16777216) << 6) | ay
1081}
1082// Existing wire remains 56-bit incremented coordinate tag plus one block byte.
1083const EJRN_KEY_MAX: i64 = 72057594037927935
1084const EJRN_TAG_MASK: i64 = 0-256
1085func ej_key_representable(ax: i64, ay: i64, az: i64) -> i64 {
1086 if ax < 0-WATER_MAGIC_16777216 || ax >= WATER_MAGIC_16777216 { return 0 }
1087 if az < 0-WATER_MAGIC_16777216 || az >= WATER_MAGIC_16777216 { return 0 }
1088 if ay < 0 || ay >= 64 { return 0 }
1089 if ej_key(ax,ay,az) == EJRN_KEY_MAX { return 0 }
1090 return 1
1091}
1092func ej_tag_matches(cur: i64, key: i64) -> i64 {
1093 if cur == 0 { return 0 }
1094 return (cur & EJRN_TAG_MASK) == ((key+1) << 8)
1095}
1096func ej_index_bucket(tag: i64) -> i64 {
1097 // Fold high coordinate bits before the bucket mask; raw-key low bits omit X.
1098 let k: i64 = tag >> 8
1099 let mixed: i64 = (k ^ (k >> 17) ^ (k >> 37))*EJRN_HASH_MUL
1100 return (mixed ^ (mixed >> 29)) & (EJ_INDEX_BUCKETS-1)
1101}
1102// A context is valid only for one synchronous mutation batch. It never survives
1103// save_apply, init, direct journal writes or another begin. No hidden active cache.
1104func ej_batch_begin(base: i64, available_bytes: i64) -> *i64 {
1105 if available_bytes < CRAFT_TOTAL { return 0 as *i64 }
1106 let heads: *i64 = (base+O_EJ_INDEX) as *i64
1107 let next: *i64 = (base+O_EJ_NEXT) as *i64
1108 let e: *i64 = ejp(base)
1109 var i: i64 = 0
1110 while i < EJ_INDEX_BUCKETS { heads[i]=0;i=i+1 }
1111 i=0
1112 while i < EJRN_SLOTS {
1113 next[i]=0
1114 let cur: i64 = e[i]
1115 if cur != 0 {
1116 let h: i64 = ej_index_bucket(cur & EJRN_TAG_MASK)
1117 next[i]=heads[h]
1118 heads[h]=i+1
1119 }
1120 i=i+1
1121 }
1122 return heads
1123}
1124func ej_index_get(base: i64, heads: *i64, ax: i64, ay: i64, az: i64) -> i64 {
1125 if ej_key_representable(ax,ay,az) == 0 { return 0-1 }
1126 let e: *i64 = ejp(base)
1127 let next: *i64 = (base+O_EJ_NEXT) as *i64
1128 let key: i64 = ej_key(ax,ay,az)
1129 var at: i64 = heads[ej_index_bucket((key+1)<<8)]
1130 var winner: i64 = 0-1
1131 var result: i64 = 0-1
1132 while at > 0 {
1133 let slot: i64 = at-1
1134 if ej_tag_matches(e[slot],key) == 1 {
1135 if slot > winner { winner=slot;result=e[slot]&255 }
1136 }
1137 at=next[slot]
1138 }
1139 return result
1140}
1141func ej_index_put(base: i64, heads: *i64, ax: i64, ay: i64, az: i64, b: i64) -> i64 {
1142 if ej_key_representable(ax,ay,az) == 0 { return 0-2 }
1143 if b < 0 || b > 255 { return 0-2 }
1144 let e: *i64 = ejp(base)
1145 let next: *i64 = (base+O_EJ_NEXT) as *i64
1146 let key: i64 = ej_key(ax,ay,az)
1147 let tag: i64 = (key+1)<<8
1148 let bucket: i64 = ej_index_bucket(tag)
1149 var at: i64 = heads[bucket]
1150 var matched: i64 = 0
1151 while at > 0 {
1152 let slot: i64 = at-1
1153 if ej_tag_matches(e[slot],key) == 1 { e[slot]=tag|b;matched=1 }
1154 at=next[slot]
1155 }
1156 if matched == 1 { return 1 }
1157 // New keys retain the original persisted hash-probe insertion order.
1158 var i: i64 = (key*EJRN_HASH_MUL)&(EJRN_SLOTS-1)
1159 var n: i64 = 0
1160 while n < EJRN_SLOTS {
1161 if e[i] == 0 {
1162 e[i]=tag|b
1163 next[i]=heads[bucket]
1164 heads[bucket]=i+1
1165 return 1
1166 }
1167 i=(i+1)&(EJRN_SLOTS-1);n=n+1
1168 }
1169 return 0-1
1170}
1171func ej_put(base: i64, ax: i64, ay: i64, az: i64, b: i64) -> i64 {
1172 if ej_key_representable(ax,ay,az) == 0 { return 0-2 }
1173 if b < 0 || b > 255 { return 0-2 }
1174 let index: *i64 = ej_batch_begin(base,CRAFT_EXT_TOTAL)
1175 if (index as i64) == 0 { return 0-3 }
1176 return ej_index_put(base,index,ax,ay,az,b)
1177}
1178func ej_get(base: i64, ax: i64, ay: i64, az: i64) -> i64 {
1179 if ej_key_representable(ax,ay,az) == 0 { return 0-1 }
1180 let index: *i64 = ej_batch_begin(base,CRAFT_EXT_TOTAL)
1181 if (index as i64) == 0 { return 0-1 }
1182 return ej_index_get(base,index,ax,ay,az)
1183}
1184// overlay journal entries falling inside the local box [lx0..lx1) x [lz0..lz1) -- edits WIN over regen
1185func ej_overlay(base: i64, lx0: i64, lx1: i64, lz0: i64, lz1: i64) -> i64 {
1186 let s: *i64 = wst(base)
1187 let e: *i64 = ejp(base)
1188 var i: i64 = 0
1189 while i < EJRN_SLOTS {
1190 let cur: i64 = e[i]
1191 if cur != 0 {
1192 let k: i64 = (cur >> 8) - 1
1193 let ay: i64 = k & 63
1194 let az: i64 = ((k >> 6) & WATER_MAGIC_33554431) - WATER_MAGIC_16777216
1195 let ax: i64 = ((k >> 31) & WATER_MAGIC_33554431) - WATER_MAGIC_16777216
1196 let lx: i64 = ax - s[S_WOX]
1197 let lz: i64 = az - s[S_WOZ]
1198 if lx >= lx0 { if lx < lx1 { if lz >= lz0 { if lz < lz1 {
1199 vset(base, lx, ay, lz, cur & 255)
1200 } } } }
1201 }
1202 i = i + 1
1203 }
1204 return 0
1205}
1206// the PLAYER-EDIT write path: journal at absolute coords FIRST (a full journal refuses the whole
1207// edit -- world and history stay consistent), then the window write. Gen/save paths keep raw vset.
1208func wc_edit(base: i64, x: i64, y: i64, z: i64, b: i64) -> i64 {
1209 if vin(x,y,z) == 0 { return 0 }
1210 let s: *i64 = wst(base)
1211 if ej_put(base, x + s[S_WOX], y, z + s[S_WOZ], b) < 0 {
1212 s[S_EJDROP] = s[S_EJDROP] + 1
1213 return 0
1214 }
1215 return vset(base, x, y, z, b)
1216}
1217// solid for collision: everything but air, water and CROPS (plants are walked through, not on)
1218func vsolid(base: i64, x: i64, y: i64, z: i64) -> i64 {
1219 let b: i64 = vget(base, x, y, z)
1220 if b == 0 { return 0 }
1221 if b == 4 { return 0 }
1222 if b >= B_CROP0 { if b <= B_CROP2 { return 0 } }
1223 return 1
1224}
1225
1226// ---------- worldgen: integer value noise -> heightmap -> columns -> trees ----------
1227func whash(x: i64, z: i64, sd: i64) -> i64 {
1228 var n: i64 = x*TERRAIN_HASH_X + z*TERRAIN_HASH_Z + sd*TERRAIN_HASH_SEED
1229 n = n & SAVE_CK_MASK
1230 n = (n ^ (n >> 13)) * TERRAIN_HASH_MIX
1231 n = n & SAVE_CK_MASK
1232 return (n ^ (n >> 16)) % 256
1233}
1234// bilinear value noise, lattice spacing `sc`, result 0..255
1235func wnoise(x: i64, z: i64, sc: i64, sd: i64) -> i64 {
1236 let cx: i64 = x / sc
1237 let cz: i64 = z / sc
1238 let fx: i64 = x % sc
1239 let fz: i64 = z % sc
1240 let c00: i64 = whash(cx, cz, sd)
1241 let c10: i64 = whash(cx+1, cz, sd)
1242 let c01: i64 = whash(cx, cz+1, sd)
1243 let c11: i64 = whash(cx+1, cz+1, sd)
1244 let v0: i64 = c00 + (c10 - c00)*fx/sc
1245 let v1: i64 = c01 + (c11 - c01)*fx/sc
1246 return v0 + (v1 - v0)*fz/sc
1247}
1248// the RAW two-octave surface: what wheight was in full before E1, unchanged and still the only
1249// thing the talus measurement is allowed to read (a threshold derived from an already-eroded
1250// surface would be measuring its own output).
1251const P_DUNE: i64 = GA_DUNE
1252func wc_dune_relief_q(sp: *i64, x: i64, z: i64, sd: i64) -> i64 {
1253 let packed: i64 = sp[P_DUNE]
1254 if packed == 0 { return 0 }
1255 let phase: i64 = whash(0,0,sd)*2*IT_PI/256
1256 let relief: i64 = wp_dune_wave_q(packed,x,z,phase)
1257 if sp[P_OCEAN] == 0 { return relief }
1258 let wavelength: i64 = (packed >> GA_DUNE_WAVE_SHIFT) & GA_DUNE_WAVE_MASK
1259 return wp_dune_coast_q(relief,0-wsp_ocean_d(sp,x,z),wavelength)
1260}
1261
1262func wheight_raw(base:i64,x:i64,z:i64,sd:i64)->i64 {
1263 let sp:*i64=wsp(base)
1264 let pipeline:i64=sp[P_WPIPE]
1265 // Existing staged producer remains unchanged; mixed relief is refused by init admission.
1266 if pipeline!=0 {
1267 WP_SEED=sd;WP_TEMP_BIAS=0;WP_CONT_BIAS=0;WP_MOUNT_SCALE=WP_MAGIC_1400
1268 var scale:i64=sp[P_WPSCALE]
1269 if scale<=0{scale=1}
1270 return wclamp(sp[P_TBASE]+wp_height_raw(x*scale,z*scale)/pipeline,1,WY-8)
1271 }
1272 let n1:i64=wnoise(x,z,16,sd)
1273 let n2:i64=wnoise(x,z,5,sd+7)
1274 let legacy:i64=sp[P_TBASE]+n1*sp[P_TAMP1]/WP_HEIGHT_Q+n2*sp[P_TAMP2]/WP_HEIGHT_Q
1275 if sp[P_DUNE]==0{return wclamp(legacy,1,WY-8)}
1276 let lower:i64=WP_HEIGHT_Q;let upper:i64=(WY-8)*WP_HEIGHT_Q
1277 let base_q:i64=wclamp(sp[P_TBASE]*WP_HEIGHT_Q+n1*sp[P_TAMP1]+n2*sp[P_TAMP2],lower,upper)
1278 let raised_q:i64=wclamp(base_q+wc_dune_relief_q(sp,x,z,sd),lower,upper)
1279 return wclamp(wp_dune_raw_delta(legacy,base_q,raised_q),1,WY-8)
1280}
1281// ---------- E1: TALUS EROSION, EVALUATED POINTWISE (2026-08-25) ----------
1282// THE WORLD SCROLLS, SO EROSION HERE CANNOT BE A BAKED GRID. gencol regenerates strips at
1283// ABSOLUTE coordinates as the window slides (s[S_WOX] = s[S_WOX] + dx), which is exactly why the
1284// header above gencol calls the terrain "a pure function of (absolute position, seed) -> unbounded
1285// land". nx_worldpipe's wp_erode is the baked-grid shape and its OWN docstring states the cost:
1286// it returns zero "outside the baked span", a case "a caller must not be able to tell apart from
1287// no erosion here, because that is what they are". Baking it here would put eroded ground behind
1288// the player and raw noise in front of him with a hard seam between the two. Storing a grid is
1289// not available either -- "wasm has no mmap; every buffer is an arena offset", and the
1290// framebuffer TAILS the arena (T60), so a 128x128 heightfield would have to move WASM_MEM_BYTES.
1291// SO THE OPERATOR IS EVALUATED, NOT STORED. k passes of a radius-1 stencil have a domain of
1292// dependence of radius k, so the unrolled composition below returns at (x,z) exactly what a
1293// k-pass relaxation over an INFINITE grid would put there. No seam can exist because there is no
1294// grid to have an edge, and nothing has to be saved, shifted or invalidated when the world slides.
1295// The pass is thermal (talus) erosion in its symmetric PAIRWISE-EXCHANGE form: across each of the
1296// 4 edges material moves from the higher cell to the lower one ONLY where the drop exceeds the
1297// talus threshold. wc_er_flux is ANTISYMMETRIC -- flux(-d) == -flux(d) exactly, truncation toward
1298// zero included -- so both ends of every edge move by equal and opposite amounts and the pass
1299// CONSERVES MASS. That is what makes this erosion and not a blur: a blur lowers everything, this
1300// cuts steep faces back and fills the hollows immediately below them, and it leaves ground gentler
1301// than the threshold untouched entirely.
1302const WC_ER_Q: i64 = 256 // fixed point the pass runs in. World heights are
1303 // small integers (craft relief ~22 blocks), so a
1304 // pass done in whole blocks could only ever move 0
1305 // or >=1 and would quantise the effect away.
1306const WC_ER_STABLE_Q: i64 = WC_ER_Q / 4 // DERIVED, not tasted: an explicit 4-neighbour
1307 // exchange h' = h + r*sum(h_n - h) is stable iff
1308 // 4r <= 1, so r = 1/4 is the marginal limit and
1309 // oscillates AT it.
1310const WC_ER_RATE_Q: i64 = WC_ER_STABLE_Q / 2 // WRITTEN AS THE ARITHMETIC: half the stability
1311 // limit. This factor of 2 is the ONLY judgement in
1312 // the whole operator and it is named right here;
1313 // the per-world lever is the tick budget, which is
1314 // recipe DATA, not code.
1315const WC_ER_MAXK: i64 = 3 // STRUCTURAL, not a clamp: three unrolled levels
1316 // are written below and no fourth one exists. Cost
1317 // is 5^k raw height evaluations per column -- 125
1318 // at k=3; 625 at k=4 would put worldgen past a
1319 // second and a visitor would meet a blank canvas.
1320const WC_ER_FLAT: i64 = 0 - 1 // P_ERTAL sentinel: measured, and there is no relief
1321
1322// THE SAME SURFACE AT SUB-BLOCK RESOLUTION -- AND THE FIX FOR A MEASURED, GATE-CAUGHT DEFECT.
1323// wheight_raw's two truncating divisions by 256 round the land to WHOLE BLOCKS before anything
1324// can weather it, and feeding that pre-rounded field to the pass made the first cut of this
1325// operator measurably do nothing. With the input quantised, every neighbour drop is a multiple of
1326// 256; the MEASURED talus came out at 264, so a one-block step (256) sits BELOW the threshold and
1327// does not move at all, and a two-block step moves (512-264)/8 = 31 of the 128 needed to change a
1328// block. The wasm-VM gate reported moved=0 of 576 with HI 477 -> 477 and REFUSED the ship. That is
1329// the tooth doing its job, and the answer is not a bigger rate -- it is to weather the CONTINUOUS
1330// field and quantise only the result, which is what erosion does to real ground anyway.
1331func wheight_q(base: i64, x: i64, z: i64, sd: i64) -> i64 {
1332 let sp: *i64 = wsp(base)
1333 let n1: i64 = wnoise(x, z, 16, sd)
1334 let n2: i64 = wnoise(x, z, 5, sd + 7)
1335 var hq: i64 = sp[P_TBASE]*WC_ER_Q + n1*sp[P_TAMP1] + n2*sp[P_TAMP2]
1336 hq = wclamp(hq,WC_ER_Q,(WY-8)*WC_ER_Q)
1337 return wclamp(hq+wc_dune_relief_q(sp,x,z,sd), WC_ER_Q, (WY - 8)*WC_ER_Q)
1338}
1339// signed transfer INTO this cell across one edge, given the neighbour-minus-self drop d.
1340// Antisymmetric by construction, so the two ends of an edge always move by equal and opposite
1341// amounts. That identity is what conserves mass, and the gate ASSERTS it rather than assuming the
1342// dialect truncates the way this reasoning needs.
1343func wc_er_flux(d: i64, t: i64) -> i64 {
1344 if d > t { return (d - t)*WC_ER_RATE_Q/WC_ER_Q }
1345 if d < 0 - t { return (d + t)*WC_ER_RATE_Q/WC_ER_Q }
1346 return 0
1347}
1348// THE THRESHOLD IS MEASURED, NEVER AUTHORED. Mean over a FIXED reference patch of each cell's
1349// largest 4-neighbour drop -- wp_erode_talus_derived's rule, applied to the surface actually being
1350// eroded. The patch is the absolute [0,WX)x[0,WZ) square rather than the player's current window,
1351// so the threshold stays a pure function of (spec, seed) and does not drift as the world slides
1352// under him. Result is in WC_ER_Q units; a world with no relief measures 0 and turns erosion off.
1353func wc_er_talus(base: i64, sd: i64) -> i64 {
1354 var sum: i64 = 0
1355 var z: i64 = 0
1356 while z < WZ {
1357 var x: i64 = 0
1358 while x < WX {
1359 let h: i64 = wheight_q(base, x, z, sd)
1360 var m: i64 = 0
1361 let da: i64 = wheight_q(base, x - 1, z, sd) - h
1362 if da > m { m = da }
1363 if 0 - da > m { m = 0 - da }
1364 let db: i64 = wheight_q(base, x + 1, z, sd) - h
1365 if db > m { m = db }
1366 if 0 - db > m { m = 0 - db }
1367 let dc: i64 = wheight_q(base, x, z - 1, sd) - h
1368 if dc > m { m = dc }
1369 if 0 - dc > m { m = 0 - dc }
1370 let dd: i64 = wheight_q(base, x, z + 1, sd) - h
1371 if dd > m { m = dd }
1372 if 0 - dd > m { m = 0 - dd }
1373 sum = sum + m
1374 x = x + 1
1375 }
1376 z = z + 1
1377 }
1378 return sum/(WX*WZ) // already in WC_ER_Q units: the field it measured is
1379}
1380// memoised into the spec so the sweep above runs ONCE per arena, never once per column. The
1381// sentinel makes "measured and flat" distinguishable from "not measured yet", which is the whole
1382// reason a flat world does not pay the sweep on every single query.
1383func wc_er_arm(base: i64, sd: i64) -> i64 {
1384 let sp: *i64 = wsp(base)
1385 if sp[P_ERTAL] != 0 { return sp[P_ERTAL] }
1386 let t: i64 = wc_er_talus(base, sd)
1387 if t <= 0 { sp[P_ERTAL] = WC_ER_FLAT } else { sp[P_ERTAL] = t }
1388 return sp[P_ERTAL]
1389}
1390// ONE PASS, UNROLLED THREE DEEP. No recursion anywhere: each level is its own function and calls
1391// only the level below it, so the maximum budget is a property of the SOURCE that a reader can
1392// count, rather than a runtime clamp that has to be trusted.
1393func wc_er1(base: i64, x: i64, z: i64, sd: i64, t: i64) -> i64 {
1394 let h: i64 = wheight_q(base, x, z, sd)
1395 var a: i64 = h
1396 a = a + wc_er_flux(wheight_q(base, x - 1, z, sd) - h, t)
1397 a = a + wc_er_flux(wheight_q(base, x + 1, z, sd) - h, t)
1398 a = a + wc_er_flux(wheight_q(base, x, z - 1, sd) - h, t)
1399 a = a + wc_er_flux(wheight_q(base, x, z + 1, sd) - h, t)
1400 return a
1401}
1402func wc_er2(base: i64, x: i64, z: i64, sd: i64, t: i64) -> i64 {
1403 let h: i64 = wc_er1(base, x, z, sd, t)
1404 var a: i64 = h
1405 a = a + wc_er_flux(wc_er1(base, x - 1, z, sd, t) - h, t)
1406 a = a + wc_er_flux(wc_er1(base, x + 1, z, sd, t) - h, t)
1407 a = a + wc_er_flux(wc_er1(base, x, z - 1, sd, t) - h, t)
1408 a = a + wc_er_flux(wc_er1(base, x, z + 1, sd, t) - h, t)
1409 return a
1410}
1411func wc_er3(base: i64, x: i64, z: i64, sd: i64, t: i64) -> i64 {
1412 let h: i64 = wc_er2(base, x, z, sd, t)
1413 var a: i64 = h
1414 a = a + wc_er_flux(wc_er2(base, x - 1, z, sd, t) - h, t)
1415 a = a + wc_er_flux(wc_er2(base, x + 1, z, sd, t) - h, t)
1416 a = a + wc_er_flux(wc_er2(base, x, z - 1, sd, t) - h, t)
1417 a = a + wc_er_flux(wc_er2(base, x, z + 1, sd, t) - h, t)
1418 return a
1419}
1420// THE HEIGHT THE PLAYER WALKS ON. gencol builds every column from this and gentrees plants from
1421// it, so a change here is a change to the GROUND, not a shader drawn over it. All 7 call sites are
1422// worldgen or init; the renderer reads voxels, so the 5^k cost is paid once per strip and never
1423// per frame.
1424func wheight(base: i64, x: i64, z: i64, sd: i64) -> i64 {
1425 let sp: *i64 = wsp(base)
1426 var k: i64 = sp[P_ERODE]
1427 if k <= 0 { return wheight_raw(base, x, z, sd) }
1428 if k > WC_ER_MAXK { k = WC_ER_MAXK }
1429 let t: i64 = wc_er_arm(base, sd)
1430 if t <= 0 { return wheight_raw(base, x, z, sd) }
1431 var he: i64 = 0
1432 if k == 1 { he = wc_er1(base, x, z, sd, t) }
1433 if k == 2 { he = wc_er2(base, x, z, sd, t) }
1434 if k >= 3 { he = wc_er3(base, x, z, sd, t) }
1435 return wclamp((he + WC_ER_Q/2)/WC_ER_Q, 1, WY - 8)
1436}
1437// GE53: the SAME eroded height wheight returns, in WC_ER_Q units and NOT rounded -- the one value the
1438// surface source keeps that the voxel column throws away at wheight's last line.
1439func wheight_qe(base: i64, x: i64, z: i64, sd: i64) -> i64 {
1440 let sp: *i64 = wsp(base)
1441 var k: i64 = sp[P_ERODE]
1442 if k <= 0 { return wheight_q(base, x, z, sd) }
1443 if k > WC_ER_MAXK { k = WC_ER_MAXK }
1444 let t: i64 = wc_er_arm(base, sd)
1445 if t <= 0 { return wheight_q(base, x, z, sd) }
1446 var he: i64 = 0
1447 if k == 1 { he = wc_er1(base, x, z, sd, t) }
1448 if k == 2 { he = wc_er2(base, x, z, sd, t) }
1449 if k >= 3 { he = wc_er3(base, x, z, sd, t) }
1450 return wclamp(he, WC_ER_Q, (WY - 8)*WC_ER_Q)
1451}
1452// the ocean ramp in Q units -- the same arithmetic wsp_beach_ocean applies to whole blocks, so the field
1453// and the column agree on where the beach meets the sea.
1454func wsp_beach_ocean_q(sp: *i64, ax: i64, az: i64, hq: i64) -> i64 {
1455 let oc: i64 = sp[P_OCEAN]
1456 if oc == 0 { return hq }
1457 let d: i64 = wsp_ocean_d(sp, ax, az)
1458 if d <= 0 { return hq }
1459 let slopew: i64 = (oc >> 12) & 63
1460 let bedq: i64 = ((oc >> 18) & 63)*WC_ER_Q
1461 var t: i64 = bedq
1462 if d < slopew { t = hq - (hq - bedq)*d/slopew }
1463 if t < bedq { t = bedq }
1464 if t > hq { t = hq }
1465 return t
1466}
1467func hq_get(base: i64, x: i64, z: i64) -> i64 {
1468 if x < 0 { return 0 - 1 }
1469 if z < 0 { return 0 - 1 }
1470 if x >= WX { return 0 - 1 }
1471 if z >= WZ { return 0 - 1 }
1472 let p: *i64 = (base + O_HQ + (z*WX + x)*8) as *i64
1473 return p[0]
1474}
1475func hq_set(base: i64, x: i64, z: i64, v: i64) -> i64 {
1476 let p: *i64 = (base + O_HQ + (z*WX + x)*8) as *i64
1477 p[0] = v
1478 return 0
1479}
1480func hq_off() -> i64 { return O_HQ }
1481// wc_surface_source -- GE53's named symbol: the terrain SURFACE height (the top of the ground, Q8 world
1482// units) at a Q8 position, bilinear between the four surrounding columns. -1 outside the window. A
1483// column's Q height is the base of its top block, so the surface sits one block above it.
1484func wc_surface_source(base: i64, pxq: i64, pzq: i64) -> i64 {
1485 let cx: i64 = pxq >> 8
1486 let cz: i64 = pzq >> 8
1487 let fx: i64 = pxq & 255
1488 let fz: i64 = pzq & 255
1489 let h00: i64 = hq_get(base, cx, cz)
1490 let h10: i64 = hq_get(base, cx + 1, cz)
1491 let h01: i64 = hq_get(base, cx, cz + 1)
1492 let h11: i64 = hq_get(base, cx + 1, cz + 1)
1493 if h00 < 0 { return 0 - 1 }
1494 if h10 < 0 { return 0 - 1 }
1495 if h01 < 0 { return 0 - 1 }
1496 if h11 < 0 { return 0 - 1 }
1497 let top: i64 = h00*(256 - fx)*(256 - fz) + h10*fx*(256 - fz) + h01*(256 - fx)*fz + h11*fx*fz
1498 return top/65536 + 256
1499}
1500// terrain-class blocks: the ground the field replaces. Structures (wood, leaves, crops, water) stay cells.
1501func wterrain(b: i64) -> i64 {
1502 if b == 1 { return 1 }
1503 if b == 2 { return 1 }
1504 if b == 3 { return 1 }
1505 if b == 5 { return 1 }
1506 if b == 8 { return 1 }
1507 if b == 9 { return 1 }
1508 if b == 10 { return 1 }
1509 if b == 11 { return 1 }
1510 return 0
1511}
1512// trilinear 3D value noise 0..255 (cave carver)
1513func wnoise3(x: i64, y: i64, z: i64, sc: i64, sd: i64) -> i64 {
1514 let cx: i64 = x / sc
1515 let cy3: i64 = y / sc
1516 let cz: i64 = z / sc
1517 let fx: i64 = x % sc
1518 let fy: i64 = y % sc
1519 let fz: i64 = z % sc
1520 let a00: i64 = whash(cx + cy3*97, cz, sd)
1521 let a10: i64 = whash(cx + 1 + cy3*97, cz, sd)
1522 let a01: i64 = whash(cx + cy3*97, cz + 1, sd)
1523 let a11: i64 = whash(cx + 1 + cy3*97, cz + 1, sd)
1524 let b00: i64 = whash(cx + (cy3+1)*97, cz, sd)
1525 let b10: i64 = whash(cx + 1 + (cy3+1)*97, cz, sd)
1526 let b01: i64 = whash(cx + (cy3+1)*97, cz + 1, sd)
1527 let b11: i64 = whash(cx + 1 + (cy3+1)*97, cz + 1, sd)
1528 let a0: i64 = a00 + (a10 - a00)*fx/sc
1529 let a1: i64 = a01 + (a11 - a01)*fx/sc
1530 let av: i64 = a0 + (a1 - a0)*fz/sc
1531 let b0: i64 = b00 + (b10 - b00)*fx/sc
1532 let b1: i64 = b01 + (b11 - b01)*fx/sc
1533 let bv: i64 = b0 + (b1 - b0)*fz/sc
1534 return av + (bv - av)*fy/sc
1535}
1536// recompute the world's highest solid layer (init + save-load; place maintains it incrementally)
1537func wc_wmax(base: i64) -> i64 {
1538 let s: *i64 = wst(base)
1539 let vxp: *u8 = (base + O_VOX) as *u8
1540 var y: i64 = WY - 1
1541 while y >= 0 {
1542 var i: i64 = y*WZ*WX
1543 let end: i64 = (y + 1)*WZ*WX
1544 while i < end {
1545 if (vxp[i] & 0xff) != 0 { s[S_WMAX] = y; return y }
1546 i = i + 1
1547 }
1548 y = y - 1
1549 }
1550 s[S_WMAX] = 0
1551 return 0
1552}
1553
1554// generate ONE local column from ABSOLUTE world coords: the terrain is a pure function of
1555// (absolute position, seed) -> unbounded land, deterministic everywhere, no stored world needed
1556// for virgin ground (the voxchunk regen law wired into the live game).
1557// GE13 -- shore distance: how many blocks SEAWARD of the shoreline (ax,az) sits; <= 0 = inland,
1558// -1 = this identity has no ocean row at all.
1559func wsp_ocean_d(sp: *i64, ax: i64, az: i64) -> i64 {
1560 let oc: i64 = sp[P_OCEAN]
1561 if oc == 0 { return 0 - 1 }
1562 let edge: i64 = oc & 3
1563 let shore: i64 = ((oc >> 2) & 1023) - 512
1564 if edge == 0 { return az - shore }
1565 if edge == 1 { return shore - az }
1566 if edge == 2 { return ax - shore }
1567 return shore - ax
1568}
1569// GE45 REVIEW FRAMING (2026-09-04) -- the sea bearing of an identity, in yaw it-units (yaw 0 looks toward +z,
1570// x = sin, z = cos: the convention summon() and the page's review steering already share), or -1 when the
1571// identity carries no ocean row. Read by the spawn (the first frame faces the sea), by summon() (the review
1572// ring stands on the seaward arc so the shore is behind the girl the camera frames) and exported as the
1573// sea_yaw door for the HUD and the gate. Data: the same P_OCEAN row wsp_ocean_d decodes -- no second coastline.
1574func wsp_sea_yaw(sp: *i64) -> i64 {
1575 let oc: i64 = sp[P_OCEAN]
1576 if oc == 0 { return 0 - 1 }
1577 let edge: i64 = oc & 3
1578 if edge == 0 { return 0 }
1579 if edge == 1 { return IT_PI }
1580 if edge == 2 { return IT_PI/2 }
1581 return IT_PI*3/2
1582}
1583// the seaward review arc: the summoned cast spans one quarter turn centred on the sea bearing. A quarter turn
1584// is the widest arc whose every member still stands between the player and the shore (tan 45 degrees = 1, so
1585// the gate can assert it with integer arithmetic: |dx| <= dz for a +z sea).
1586const WC_REVIEW_ARC: i64 = IT_PI/2
1587// GE13 -- the ocean shaping rule (gameengine rung wsp_beach_ocean), applied to the PURE noise
1588// height wherever a column sits seaward of the shoreline: ramp the ground from its natural
1589// height down to the SEABED across the SLOPE width, then hold the seabed. gencol's existing
1590// flood rule (P_WATER) then fills the water and its sand rule paints the wet band -- beach,
1591// shallows and open sea all emerge from ONE data row, no per-world code. P_OCEAN == 0 returns
1592// h untouched, so every existing world is byte-identical by construction.
1593func wsp_beach_ocean(sp: *i64, ax: i64, az: i64, h: i64) -> i64 {
1594 let oc: i64 = sp[P_OCEAN]
1595 if oc == 0 { return h }
1596 let d: i64 = wsp_ocean_d(sp, ax, az)
1597 if d <= 0 { return h }
1598 let slopew: i64 = (oc >> 12) & 63
1599 let bed: i64 = (oc >> 18) & 63
1600 var t: i64 = bed
1601 if d < slopew { t = h - (h - bed)*d/slopew }
1602 if t < bed { t = bed }
1603 if t > h { t = h }
1604 return t
1605}
1606
1607// Runtime position validity uses the same terrain field and structural body sweep.
1608// This does not change the existing point footprint into a wider avatar.
1609func wc_player_position_clear(base: i64, x: i64, y: i64, z: i64) -> i64 {
1610 if x < 0 || z < 0 || x >= WX*256 || z >= WZ*256 { return 0 }
1611 if wsp(base)[P_SURF] == 1 {
1612 let field: i64=wc_surface_source(base,x,z)
1613 if field < 0 || y-EYE < field { return 0 }
1614 }
1615 wc_player_body(base,x,y,z)
1616 beach_contact_world_sweep(base,wc_contact_body(base),wc_contact_obstacle(base),wc_contact_result(base),1,0)
1617 let contact: *BeachContactResult=wc_contact_result(base)
1618 if contact.initial_overlap == 1 { return 0 }
1619 return 1
1620}
1621// Return 0 unchanged,1 recovered,-1 unresolved. No arbitrary teleport across X/Z.
1622// A restore can carry a valid checksum yet contain an old penetrated camera.
1623
1624// Profile storage occupies the verified unused O_ST tail; existing 64-word ABI is unchanged.
1625const S_MOVE_MODE: i64 = 51
1626const S_MOVE_GRAV_MM: i64 = 52
1627const S_MOVE_JUMP_MM: i64 = 53
1628const S_MOVE_WALK_MM: i64 = 54
1629const S_MOVE_TERM_MM: i64 = 55
1630const S_MOVE_STEP_Q8: i64 = 56
1631const S_MOVE_REPEAT: i64 = 57
1632const S_MOVE_VNUM: i64 = 58
1633const S_MOVE_RY: i64 = 59
1634const S_MOVE_RX: i64 = 60
1635const S_MOVE_RZ: i64 = 61
1636const S_MOVE_LAST_JUMP: i64 = 62
1637const S_MOVE_REV: i64 = 63
1638const WC_MOVE_HZ: i64 = 60
1639const WC_MOVE_Q8: i64 = 256
1640const WC_MOVE_MM: i64 = 1000
1641const WC_MOVE_DEN: i64 = WC_MOVE_MM*WC_MOVE_HZ*WC_MOVE_HZ
1642const WC_MOVE_HDEN: i64 = WATER_MAGIC_4096*WC_MOVE_MM*WC_MOVE_HZ
1643// A request cannot traverse more than the vertical arena extent in one tick.
1644const WC_MOVE_SPEED_MAX_MM: i64 = WY*WC_MOVE_MM*WC_MOVE_HZ
1645const WC_MOVE_GRAV_MAX_MM: i64 = WC_MOVE_SPEED_MAX_MM*WC_MOVE_HZ
1646const WC_MOVE_RC_PROFILE: i64 = 0 - 81
1647const WC_MOVE_RC_ACTIVE: i64 = 0 - 82
1648
1649func wc_move_reset(base: i64) -> i64 {
1650 let s: *i64 = wst(base)
1651 s[S_MOVE_VNUM] = 0; s[S_MOVE_RY] = 0
1652 s[S_MOVE_RX] = 0; s[S_MOVE_RZ] = 0
1653 s[S_MOVE_LAST_JUMP] = 0
1654 return 0
1655}
1656func wc_move_valid(mode: i64, grav: i64, launch: i64, walk: i64,
1657 terminal: i64, step: i64, repeat: i64) -> i64 {
1658 if mode == 0 {
1659 if grav != 0 || launch != 0 || walk != 0 || terminal != 0 || step != 0 || repeat != 0 { return WC_MOVE_RC_PROFILE }
1660 return 0
1661 }
1662 if mode != 1 { return WC_MOVE_RC_PROFILE }
1663 if grav <= 0 || grav > WC_MOVE_GRAV_MAX_MM { return WC_MOVE_RC_PROFILE }
1664 if launch <= 0 || launch > WC_MOVE_SPEED_MAX_MM { return WC_MOVE_RC_PROFILE }
1665 if walk < 0 || walk > WC_MOVE_SPEED_MAX_MM { return WC_MOVE_RC_PROFILE }
1666 if terminal <= 0 || terminal > WC_MOVE_SPEED_MAX_MM { return WC_MOVE_RC_PROFILE }
1667 if step < 0 || step >= WY*WC_MOVE_Q8 { return WC_MOVE_RC_PROFILE }
1668 if repeat < 0 || repeat > 1 { return WC_MOVE_RC_PROFILE }
1669 return 0
1670}
1671func wc_move_commit_validated(base: i64, mode: i64, grav: i64, launch: i64, walk: i64,
1672 terminal: i64, step: i64, repeat: i64) -> i64 {
1673 let s: *i64 = wst(base)
1674 s[S_MOVE_MODE] = mode
1675 s[S_MOVE_GRAV_MM] = grav; s[S_MOVE_JUMP_MM] = launch
1676 s[S_MOVE_WALK_MM] = walk; s[S_MOVE_TERM_MM] = terminal
1677 s[S_MOVE_STEP_Q8] = step; s[S_MOVE_REPEAT] = repeat
1678 wc_move_reset(base)
1679 s[S_MOVE_REV] = s[S_MOVE_REV] + 1
1680 return 0
1681}
1682func wc_move_set(base: i64, mode: i64, grav: i64, launch: i64, walk: i64,
1683 terminal: i64, step: i64, repeat: i64) -> i64 {
1684 let valid: i64 = wc_move_valid(mode,grav,launch,walk,terminal,step,repeat)
1685 if valid != 0 { return valid }
1686 let s: *i64 = wst(base)
1687 if s[S_MOVE_MODE] == mode && s[S_MOVE_GRAV_MM] == grav && s[S_MOVE_JUMP_MM] == launch && s[S_MOVE_WALK_MM] == walk && s[S_MOVE_TERM_MM] == terminal && s[S_MOVE_STEP_Q8] == step && s[S_MOVE_REPEAT] == repeat { return 0 }
1688 if s[S_GROUNDED] != 1 || s[S_VY] != 0 { return WC_MOVE_RC_ACTIVE }
1689 if s[S_MOVE_VNUM] != 0 || s[S_MOVE_RY] != 0 || s[S_MOVE_RX] != 0 || s[S_MOVE_RZ] != 0 { return WC_MOVE_RC_ACTIVE }
1690 let held: i64 = ia_held(winp(base))
1691 if (held & (IA_UP | IA_DOWN | IA_LEFT | IA_RIGHT | IA_JUMP)) != 0 { return WC_MOVE_RC_ACTIVE }
1692 return wc_move_commit_validated(base,mode,grav,launch,walk,terminal,step,repeat)
1693}
1694func wc_move_boot(base: i64, world: i64) -> i64 {
1695 if world != BEACH_RELEASE_WORLD { return 0 }
1696 // init preflights immutable recipe data before any arena mutation; grounded is established by motion.
1697 return wc_move_commit_validated(base,BEACH_MOVE_MODE,BEACH_MOVE_GRAV_MM,BEACH_MOVE_JUMP_MM,
1698 BEACH_MOVE_WALK_MM,BEACH_MOVE_TERM_MM,BEACH_MOVE_STEP_Q8,BEACH_MOVE_REPEAT)
1699}
1700func wc_move_horizontal(base: i64, axis: i64, direction: i64) -> i64 {
1701 let s: *i64 = wst(base)
1702 if s[S_MOVE_MODE] == 0 { return direction*SPEED/WATER_MAGIC_4096 }
1703 var slot: i64 = S_MOVE_RX
1704 if axis == 2 { slot = S_MOVE_RZ }
1705 if direction == 0 { s[slot] = 0; return 0 }
1706 let sum: i64 = s[slot] + direction*s[S_MOVE_WALK_MM]*WC_MOVE_Q8
1707 let delta: i64 = sum/WC_MOVE_HDEN
1708 s[slot] = sum - delta*WC_MOVE_HDEN
1709 return delta
1710}
1711func wc_move_vertical_request(base: i64, jump: i64) -> i64 {
1712 let s: *i64 = wst(base)
1713 var launch: i64 = jump
1714 if s[S_MOVE_REPEAT] == 0 && s[S_MOVE_LAST_JUMP] == 1 { launch = 0 }
1715 s[S_MOVE_LAST_JUMP] = jump
1716 var vn: i64 = s[S_MOVE_VNUM]
1717 if launch == 1 && s[S_GROUNDED] == 1 {
1718 vn = s[S_MOVE_JUMP_MM]*WC_MOVE_Q8*WC_MOVE_HZ
1719 s[S_MOVE_RY] = 0
1720 }
1721 vn = vn - s[S_MOVE_GRAV_MM]*WC_MOVE_Q8
1722 let terminal: i64 = s[S_MOVE_TERM_MM]*WC_MOVE_Q8*WC_MOVE_HZ
1723 if vn < 0 - terminal { vn = 0 - terminal }
1724 s[S_MOVE_VNUM] = vn
1725 let sum: i64 = s[S_MOVE_RY] + vn
1726 let delta: i64 = sum/WC_MOVE_DEN
1727 s[S_MOVE_RY] = sum - delta*WC_MOVE_DEN
1728 return delta
1729}
1730func movement_profile_set(mode: i64, grav_mm_s2: i64, jump_mm_s: i64,
1731 walk_mm_s: i64, terminal_mm_s: i64, step_q8: i64, held_repeat: i64) -> i64 {
1732 return wc_move_set(0,mode,grav_mm_s2,jump_mm_s,walk_mm_s,terminal_mm_s,step_q8,held_repeat)
1733}
1734func movement_profile_field(field: i64) -> i64 {
1735 if field < 0 || field > S_MOVE_REV-S_MOVE_MODE { return WC_MOVE_RC_PROFILE }
1736 return wst(0)[S_MOVE_MODE+field]
1737}
1738func movement_tick_hz() -> i64 { return WC_MOVE_HZ }
1739func movement_velocity_denominator() -> i64 { return WC_MOVE_DEN }
1740
1741func wc_player_recover(base: i64) -> i64 {
1742 let s: *i64=wst(base)
1743 let x: i64=s[S_CX];let y: i64=s[S_CY];let z: i64=s[S_CZ]
1744 if wc_player_position_clear(base,x,y,z) == 1 { return 0 }
1745 if x < 0 || z < 0 || x >= WX*256 || z >= WZ*256 { return -1 }
1746 var floor: i64=y-EYE
1747 if wsp(base)[P_SURF] == 1 {
1748 let field: i64=wc_surface_source(base,x,z)
1749 if field < 0 { return -1 }
1750 if field > floor { floor=field }
1751 }
1752 if floor < 0 { floor=0 }
1753 var ceiling: i64=floor
1754 while ceiling+EYE+128 <= WY*256 {
1755 let stand: i64=wc_player_support(base,x,z,ceiling)
1756 if stand >= y && stand <= ceiling+EYE {
1757 if wc_player_position_clear(base,x,stand,z) == 1 {
1758 s[S_CY]=stand;s[S_VY]=0;s[S_GROUNDED]=1;wc_move_reset(base)
1759 return 1
1760 }
1761 }
1762 // Include the continuous field first, then every possible structural cell top.
1763 ceiling=((ceiling >> 8)+1)*256
1764 }
1765 return -1
1766}
1767func player_position_clear() -> i64 {
1768 let s: *i64=wst(0)
1769 return wc_player_position_clear(0,s[S_CX],s[S_CY],s[S_CZ])
1770}
1771func player_recover() -> i64 { return wc_player_recover(0) }
1772
1773// Same column owner for normal generation and non-destructive restore rebuild.
1774func wc_hq_column(base: i64, x: i64, z: i64, ax: i64, az: i64, sd: i64) -> i64 {
1775 let sp: *i64=wsp(base)
1776 if sp[P_SURF] == 1 { hq_set(base,x,z,wsp_beach_ocean_q(sp,ax,az,wheight_qe(base,ax,az,sd))) }
1777 return 0
1778}
1779func wc_hq_restore(base: i64) -> i64 {
1780 if wsp(base)[P_SURF] != 1 { return 0 }
1781 let s: *i64=wst(base)
1782 var z: i64=0
1783 while z < WZ {
1784 var x: i64=0
1785 while x < WX {
1786 wc_hq_column(base,x,z,x+s[S_WOX],z+s[S_WOZ],s[S_SEED])
1787 x=x+1
1788 }
1789 z=z+1
1790 }
1791 return 0
1792}
1793
1794func gencol(base: i64, x: i64, z: i64, ax: i64, az: i64, sd: i64) -> i64 {
1795 let sp: *i64 = wsp(base)
1796 let wl: i64 = sp[P_WATER]
1797 let h: i64 = wsp_beach_ocean(sp, ax, az, wheight(base, ax, az, sd)) // GE13: the ocean is data
1798 wc_hq_column(base,x,z,ax,az,sd) // Same height owner as coherent save restoration.
1799 var y: i64 = 0
1800 while y < WY {
1801 var b: i64 = 0
1802 if y <= h {
1803 b = 3 // stone
1804 if y > h - 3 { b = 2 } // dirt cap
1805 if y == h {
1806 b = 1 // grass
1807 if h <= wl { b = 5 } // sand at/below waterline
1808 if h >= sp[P_SNOW] { b = 8 } // snow high up
1809 }
1810 // caves: carve inside the crust (never the floor, never the top 2 = no surface holes,
1811 // and never under water columns so lakes stay sealed)
1812 if b == 3 { if y >= 2 { if y <= h - 2 { if h > wl {
1813 if wnoise3(ax, y, az, 7, sd + 55) > sp[P_CAVETH] { b = 0 }
1814 } } } }
1815 // ores seed the remaining stone: coal common, iron deeper, gold deepest
1816 if b == 3 {
1817 let orr: i64 = whash(ax*7 + y*131, az*11 + y*17, sd + 31) % 997
1818 if orr < 22 { b = 9 }
1819 if y < 14 { if orr >= 22 { if orr < 32 { b = 10 } } }
1820 if y < 9 { if orr >= 32 { if orr < 38 { b = 11 } } }
1821 }
1822 }
1823 if y > h { if y <= wl { b = 4 } } // flood low ground
1824 vset(base, x, y, z, b)
1825 y = y + 1
1826 }
1827 return 0
1828}
1829// paint a voxel ONLY inside the regen box -- a tree may never overwrite terrain (and its journal
1830// overlay) outside the strip being regenerated
1831func vset_box(base: i64, x: i64, y: i64, z: i64, b: i64, lx0: i64, lx1: i64, lz0: i64, lz1: i64) -> i64 {
1832 if x < lx0 { return 0 }
1833 if x >= lx1 { return 0 }
1834 if z < lz0 { return 0 }
1835 if z >= lz1 { return 0 }
1836 return vset(base, x, y, z, b)
1837}
1838// GE17 wsp_palm_shape -- THE PALM (W2 shape id 3, mirroring nx_treegen species 3: dep0=0 trunk-
1839// only + "a ring of LONG radial frond blobs from the trunk top" -- ONE species canon, two lanes,
1840// the PG36 rule). A bare, gently curved trunk with NO branches below the crown, then a frond
1841// rosette. Everything DERIVED, nothing tuned:
1842// LEAN -- phototropic, TOWARD OPEN WATER when the world declares an ocean (P_OCEAN edge
1843// bits: shoreline palms lean seaward for light), hash-picked axis otherwise. Tip
1844// displacement = th9/3 (tan ~18deg of trunk height, the shoreline coconut habit); profile
1845// quadratic in height (uniform curvature): lean(dy) = dy*dy/(3*th9), which lands the tip at
1846// exactly th9/3 with no constant of its own.
1847// FRONDS -- 8 directions (4 axial + 4 diagonal, the voxel ring), length = the genome's own
1848// canopy radius cr9, arch droop(r) = r*r/(2*cr9): the tip ends half the frond length below
1849// the crown, the same quadratic droop nx_treegen's palm crown uses (seg*seg*11 there).
1850// Painting is clipped to the regen box (vset_box) so it is seam-safe under the widened scan
1851// margin gentrees derives for shape 3. Trunk = wood (6), fronds + apical bud = leaves (7);
1852// on the shore identity those palette rows read driftwood-brown and scrub-green already.
1853func wsp_palm_shape(base: i64, sp: *i64, tx: i64, tz: i64, h2: i64, th9: i64, cr9: i64,
1854 ax: i64, az: i64, sd: i64, lx0: i64, lx1: i64, lz0: i64, lz1: i64) -> i64 {
1855 var pldx: i64 = 0
1856 var pldz: i64 = 0
1857 let poc9: i64 = sp[P_OCEAN]
1858 if poc9 != 0 {
1859 let pe9: i64 = poc9 & 3
1860 if pe9 == 0 { pldz = 1 }
1861 if pe9 == 1 { pldz = 0 - 1 }
1862 if pe9 == 2 { pldx = 1 }
1863 if pe9 == 3 { pldx = 0 - 1 }
1864 }
1865 if poc9 == 0 {
1866 let pd9: i64 = whash(ax*19 + 13, az*23 + 29, sd + 251) % 4
1867 if pd9 == 0 { pldx = 1 }
1868 if pd9 == 1 { pldx = 0 - 1 }
1869 if pd9 == 2 { pldz = 1 }
1870 if pd9 == 3 { pldz = 0 - 1 }
1871 }
1872 var pox: i64 = 0
1873 var poz: i64 = 0
1874 var pty: i64 = 1
1875 while pty <= th9 {
1876 let pl9: i64 = pty*pty/(3*th9)
1877 pox = pldx*pl9
1878 poz = pldz*pl9
1879 vset_box(base, tx + pox, h2 + pty, tz + poz, 6, lx0, lx1, lz0, lz1)
1880 pty = pty + 1
1881 }
1882 let pcx: i64 = tx + pox
1883 let pcz: i64 = tz + poz
1884 let ptop: i64 = h2 + th9
1885 vset_box(base, pcx, ptop + 1, pcz, 7, lx0, lx1, lz0, lz1)
1886 var pfd: i64 = 0
1887 while pfd < 8 {
1888 var fdx9: i64 = 0
1889 var fdz9: i64 = 0
1890 if pfd == 0 { fdx9 = 1 }
1891 if pfd == 1 { fdx9 = 0 - 1 }
1892 if pfd == 2 { fdz9 = 1 }
1893 if pfd == 3 { fdz9 = 0 - 1 }
1894 if pfd == 4 { fdx9 = 1; fdz9 = 1 }
1895 if pfd == 5 { fdx9 = 1; fdz9 = 0 - 1 }
1896 if pfd == 6 { fdx9 = 0 - 1; fdz9 = 1 }
1897 if pfd == 7 { fdx9 = 0 - 1; fdz9 = 0 - 1 }
1898 var pr9: i64 = 1
1899 while pr9 <= cr9 {
1900 vset_box(base, pcx + fdx9*pr9, ptop + 1 - pr9*pr9/(2*cr9), pcz + fdz9*pr9, 7, lx0, lx1, lz0, lz1)
1901 pr9 = pr9 + 1
1902 }
1903 pfd = pfd + 1
1904 }
1905 return 0
1906}
1907// trees whose TRUNK falls in the +-2-expanded ABSOLUTE range paint their blocks CLIPPED to the
1908// local box, so a tree crossing a strip seam is completed by whichever strip regenerates later.
1909// The trunk condition is PURE (grass by the gen rules: above water, below snow) -- no window peeking.
1910func gentrees(base: i64, lx0: i64, lx1: i64, lz0: i64, lz1: i64, sd: i64) -> i64 {
1911 let sp: *i64 = wsp(base)
1912 if sp[P_TREE] == 0 { return 0 }
1913 let s: *i64 = wst(base)
1914 let wl: i64 = sp[P_WATER]
1915 // scan expanded to +-4 (was +-2): a SPECIES canopy reaches up to radius 4, so a trunk that
1916 // far outside the box can paint inside it -- the strip-seam completion law, wider. Legacy
1917 // worlds are unchanged: an R=2 stamp 3-4 cells out paints nothing in-box (no-op scan rows).
1918 // GE17: a palm's phototropic lean displaces its crown by up to (tb+tv)/3 beyond the frond
1919 // radius, so a palm-genome world widens the seam scan by that maximum lean -- DERIVED from
1920 // the same genome rows the grower reads, not a second constant. Every other shape keeps
1921 // the historic +-4 margin bit-exactly, and the extra scan rows are no-ops by the vset_box
1922 // clip (the standing seam law, same argument as the +-2 -> +-4 widening above).
1923 var mg9: i64 = 4
1924 if ((sp[P_TREESPEC] >> 12) & 7) == 3 { mg9 = 4 + ((sp[P_TREESPEC] & 15) + ((sp[P_TREESPEC] >> 4) & 15))/3 }
1925 var az: i64 = s[S_WOZ] + lz0 - mg9
1926 while az <= s[S_WOZ] + lz1 + mg9 - 1 {
1927 var ax: i64 = s[S_WOX] + lx0 - mg9
1928 while ax <= s[S_WOX] + lx1 + mg9 - 1 {
1929 let hr9: i64 = wheight(base, ax, az, sd)
1930 let h2: i64 = wsp_beach_ocean(sp, ax, az, hr9) // GE13: no trees grow on the sea
1931 // GE17: DRY LAND is where the ocean rule was a NO-OP (h2 == hr9). Seaward of the
1932 // shoreline the ramp lowered this column -- that whole band is the beach slope and
1933 // its wet sand, and vegetation stays off it (palms out of the water, off the wet
1934 // band, by the ocean row's own geometry rather than a second coastline rule).
1935 // Worlds without an ocean row have h2 == hr9 everywhere BY CONSTRUCTION
1936 // (wsp_beach_ocean returns h untouched), so this conjunct changes nothing there.
1937 var trunk: i64 = 0
1938 // the tree altitude ceiling is a SPEC ROW, not an engine constant: hardcoded 19
1939 // silently capped forests for any identity whose land sits higher (gate T52 found
1940 // it -- vale's P_TREE 43->19 barely moved the trunk count because the CEILING, not
1941 // the density row, was binding). 0 = the historic 19, so craft/depths are unchanged.
1942 var tmax: i64 = sp[P_TREEMAX]
1943 if tmax <= 0 { tmax = 19 }
1944 if h2 < tmax { if h2 > wl { if h2 < sp[P_SNOW] { if h2 == hr9 {
1945 if whash(ax*3 + 7, az*5 + 11, sd + 99) % sp[P_TREE] == 0 { trunk = 1 }
1946 } } } }
1947 if trunk == 1 {
1948 let tx: i64 = ax - s[S_WOX]
1949 let tz: i64 = az - s[S_WOZ]
1950 let ts9: i64 = sp[P_TREESPEC]
1951 // W2b: the TREE branch keys on the tree bits (0..17) alone -- a genome carrying
1952 // only understory bits (18+) keeps the LEGACY forest exactly (T57's wood-equality
1953 // tooth), and understory grows in its own pass below regardless of trunk sites.
1954 if (ts9 & 262143) == 0 {
1955 // legacy stamp, byte-for-byte: every identity-only world is unchanged
1956 var ty: i64 = h2 + 1
1957 while ty <= h2 + 4 { vset_box(base, tx, ty, tz, 6, lx0, lx1, lz0, lz1); ty = ty + 1 }
1958 var lz: i64 = tz - 2
1959 while lz <= tz + 2 {
1960 var lx: i64 = tx - 2
1961 while lx <= tx + 2 {
1962 var keep: i64 = 1
1963 if lx == tx { if lz == tz { keep = 0 } }
1964 if keep == 1 {
1965 vset_box(base, lx, h2 + 4, lz, 7, lx0, lx1, lz0, lz1)
1966 if lx >= tx - 1 { if lx <= tx + 1 { if lz >= tz - 1 { if lz <= tz + 1 {
1967 vset_box(base, lx, h2 + 5, lz, 7, lx0, lx1, lz0, lz1)
1968 } } } }
1969 }
1970 lx = lx + 1
1971 }
1972 lz = lz + 1
1973 }
1974 vset_box(base, tx, h2 + 6, tz, 7, lx0, lx1, lz0, lz1)
1975 } else {
1976 // W2 SPECIES GROWER: the individual varies inside the species envelope by
1977 // position hash -- deterministic, seam-safe (paint clipped to the box),
1978 // asset-free. Shapes: 1 dome (squashed integer ellipsoid), 2 pine (diamond
1979 // rings widening down from the tip). Density drops leaves by hash for airy
1980 // canopies. All bounds y-clamped by vset's own vin guard.
1981 let tb9: i64 = ts9 & 15
1982 let tv9: i64 = (ts9 >> 4) & 15
1983 var cr9: i64 = (ts9 >> 8) & 15
1984 if cr9 < 1 { cr9 = 1 }
1985 if cr9 > 4 { cr9 = 4 }
1986 let sh9: i64 = (ts9 >> 12) & 7
1987 let dn9: i64 = (ts9 >> 15) & 7
1988 let hv9: i64 = whash(ax*13 + 5, az*17 + 3, sd + 141)
1989 var th9: i64 = tb9 + hv9 % (tv9 + 1)
1990 if th9 < 2 { th9 = 2 }
1991 if sh9 == 3 {
1992 // GE17: shape 3 = PALM, the frond-rosette grower (wsp_palm_shape).
1993 // dn9 (leaf drop) is deliberately unused there: fronds are already
1994 // sparse lines, not a volume to thin.
1995 wsp_palm_shape(base, sp, tx, tz, h2, th9, cr9, ax, az, sd, lx0, lx1, lz0, lz1)
1996 } else {
1997 var ty2: i64 = h2 + 1
1998 while ty2 <= h2 + th9 { vset_box(base, tx, ty2, tz, 6, lx0, lx1, lz0, lz1); ty2 = ty2 + 1 }
1999 let top9: i64 = h2 + th9
2000 var dy9: i64 = 0 - cr9
2001 while dy9 <= cr9 + 1 {
2002 var dz9: i64 = 0 - cr9
2003 while dz9 <= cr9 {
2004 var dx9: i64 = 0 - cr9
2005 while dx9 <= cr9 {
2006 var put9: i64 = 0
2007 if sh9 == 2 {
2008 if dy9 <= 1 {
2009 var pr9: i64 = 1 - dy9
2010 if pr9 > cr9 { pr9 = cr9 }
2011 var ad9: i64 = dx9
2012 if ad9 < 0 { ad9 = 0 - ad9 }
2013 var az9: i64 = dz9
2014 if az9 < 0 { az9 = 0 - az9 }
2015 if ad9 + az9 <= pr9 { put9 = 1 }
2016 }
2017 } else {
2018 if dx9*dx9 + dz9*dz9 + dy9*dy9*2 <= cr9*cr9 { put9 = 1 }
2019 }
2020 if put9 == 1 { if dn9 > 0 {
2021 if whash(ax*29 + dx9*3 + dy9*7, az*31 + dz9*5, sd + 177) % 8 < dn9 { put9 = 0 }
2022 } }
2023 if put9 == 1 {
2024 var istrunk9: i64 = 0
2025 if dx9 == 0 { if dz9 == 0 { if dy9 < 0 { istrunk9 = 1 } } }
2026 if istrunk9 == 0 { vset_box(base, tx + dx9, top9 + dy9, tz + dz9, 7, lx0, lx1, lz0, lz1) }
2027 }
2028 dx9 = dx9 + 1
2029 }
2030 dz9 = dz9 + 1
2031 }
2032 dy9 = dy9 + 1
2033 }
2034 }
2035 }
2036 }
2037 // W2b UNDERSTORY (genome bits 18-20 density, 21-22 kind 0=tufts 1=shrubs 2=mixed):
2038 // ground vegetation per column, seeded independently of trunks, same grass belt,
2039 // trunk columns excluded; shrub = plus-clump radius 1 (inside the +-4 seam scan).
2040 let ud9: i64 = (sp[P_TREESPEC] >> 18) & 7
2041 if ud9 > 0 { if trunk == 0 { if h2 > wl { if h2 < sp[P_SNOW] { if h2 == hr9 {
2042 if whash(ax*41 + 3, az*43 + 9, sd + 203) % 8 < ud9 {
2043 let uk9: i64 = (sp[P_TREESPEC] >> 21) & 3
2044 let ux5: i64 = ax - s[S_WOX]
2045 let uz5: i64 = az - s[S_WOZ]
2046 var shrub9: i64 = 0
2047 if uk9 == 1 { shrub9 = 1 }
2048 if uk9 == 2 { if whash(ax*7 + 1, az*11 + 5, sd + 209) % 2 == 0 { shrub9 = 1 } }
2049 vset_box(base, ux5, h2 + 1, uz5, 7, lx0, lx1, lz0, lz1)
2050 if shrub9 == 1 {
2051 // vegetation FILLS AIR, never replaces solids: a clump reaching into a
2052 // neighbor column must not eat its trunk or its surface (T57 measured the
2053 // unguarded version eating 792 wood blocks). Deterministic under the fixed
2054 // scan order -- a later trunk overwrites a leaf, so wood wins either way.
2055 if vget(base, ux5 + 1, h2 + 1, uz5) == 0 { vset_box(base, ux5 + 1, h2 + 1, uz5, 7, lx0, lx1, lz0, lz1) }
2056 if vget(base, ux5 - 1, h2 + 1, uz5) == 0 { vset_box(base, ux5 - 1, h2 + 1, uz5, 7, lx0, lx1, lz0, lz1) }
2057 if vget(base, ux5, h2 + 1, uz5 + 1) == 0 { vset_box(base, ux5, h2 + 1, uz5 + 1, 7, lx0, lx1, lz0, lz1) }
2058 if vget(base, ux5, h2 + 1, uz5 - 1) == 0 { vset_box(base, ux5, h2 + 1, uz5 - 1, 7, lx0, lx1, lz0, lz1) }
2059 if vget(base, ux5, h2 + 2, uz5) == 0 { vset_box(base, ux5, h2 + 2, uz5, 7, lx0, lx1, lz0, lz1) }
2060 }
2061 }
2062 } } } } }
2063 ax = ax + 1
2064 }
2065 az = az + 1
2066 }
2067 return 0
2068}
2069
2070// ---------- W3 SETTLEMENTS (GR6, the Infinigen-Indoors expansion, 2026-08-17) ----------
2071// Room LAYOUTS are solved OFFLINE by nx_indoorgen -- declarative constraints through the estate's
2072// part-placement solver, quantized, accept-rule-checked, REFUSED when unsatisfiable -- and land
2073// here as the imported data table (nx_indoorgen_data.nx). The engine only STAMPS: sites ride a
2074// coarse grid of BLDG_SITE-block cells hashed on ABSOLUTE coords (window-independent, so a
2075// building regenerates identically when the streaming window slides back -- the tree seam law at
2076// building scale); jitter, rotation and layout variant come from the same hash; and a building
2077// always fits INSIDE its own site cell (jitter 8 + max half-footprint 5 < SITE/2 = 20), so two
2078// sites can never collide. Terrain must be near-flat (4 rotated corners within 1 of the anchor
2079// height), dry and below the snowline, or the site deterministically stays empty. Painting is
2080// CLIPPED to the regen box (vset_box), and ej_overlay runs after every strip regen -- players can
2081// remodel a building and their edits win, exactly as they do against trees.
2082const BLDG_SITE: i64 = 40 // site-cell stride, blocks
2083const IG_SCAN0: i64 = 10 // W3 solve: first candidate anchor offset inside the site cell
2084const IG_SCANSTEP: i64 = 10 // candidate grid step: 3x3 covers offsets 10/20/30; footprint
2085 // half-extent (bw/2+1 <= 4) keeps every candidate's shell inside
2086 // its own cell, so neighbor cells can never overlap -- the
2087 // no-overlap rule holds by construction, not by test
2088const IG_MAXSLOPE: i64 = 2 // pre-declared accept rule: corners may deviate <=2 blocks from
2089 // the anchor; the IG_FOUND skirt reaches ground at exactly this
2090 // bound, so an accepted site is grounded. An unsatisfiable cell
2091 // stamps NOTHING (refuse, never float -- the constraint-solve law)
2092const IG_FOUND: i64 = 3 // foundation skirt courses below the floor (IG_MAXSLOPE + 1)
2093
2094// floor division for ABSOLUTE (possibly negative) coords -- truncating division reads the cell
2095// left of origin as cell 0 twice, which would double-place one building at the world seam
2096func igfloor(a: i64, b: i64) -> i64 {
2097 var q: i64 = a/b
2098 if a % b < 0 { q = q - 1 }
2099 return q
2100}
2101func ig_rotx(r: i64, x: i64, z: i64) -> i64 {
2102 if r == 1 { return z }
2103 if r == 2 { return 0 - x }
2104 if r == 3 { return 0 - z }
2105 return x
2106}
2107func ig_rotz(r: i64, x: i64, z: i64) -> i64 {
2108 if r == 1 { return 0 - x }
2109 if r == 2 { return 0 - z }
2110 if r == 3 { return x }
2111 return z
2112}
2113func genbuildings(base: i64, lx0: i64, lx1: i64, lz0: i64, lz1: i64, sd: i64) -> i64 {
2114 let sp: *i64 = wsp(base)
2115 if sp[P_BLDG] <= 0 { return 0 }
2116 let s: *i64 = wst(base)
2117 let wl: i64 = sp[P_WATER]
2118 let arch: i64 = sp[P_BLDGSPEC] & 15
2119 let dims: i64 = igd_dims(arch)
2120 let bw: i64 = dims & 255
2121 let bd: i64 = (dims >> 8) & 255
2122 let bh: i64 = (dims >> 16) & 255
2123 let mats: i64 = igd_mats(arch)
2124 let mwall: i64 = mats & 255
2125 let mfloor: i64 = (mats >> 8) & 255
2126 let mroof: i64 = (mats >> 16) & 255
2127 let dxo: i64 = igd_doorx(arch)
2128 let gx0: i64 = igfloor(s[S_WOX] + lx0 - BLDG_SITE + 1, BLDG_SITE)
2129 let gx1: i64 = igfloor(s[S_WOX] + lx1 + BLDG_SITE - 1, BLDG_SITE)
2130 let gz0: i64 = igfloor(s[S_WOZ] + lz0 - BLDG_SITE + 1, BLDG_SITE)
2131 let gz1: i64 = igfloor(s[S_WOZ] + lz1 + BLDG_SITE - 1, BLDG_SITE)
2132 var gz: i64 = gz0
2133 while gz <= gz1 {
2134 var gx: i64 = gx0
2135 while gx <= gx1 {
2136 var put: i64 = 1
2137 if whash(gx*7 + 1, gz*13 + 5, sd + 331) % sp[P_BLDG] != 0 { put = 0 }
2138 let rot: i64 = whash(gx*17 + 2, gz*19 + 6, sd + 499) % 4
2139 let bv: i64 = whash(gx*23 + 4, gz*29 + 8, sd + 541) % IGD_NVAR
2140 // W3 SOLVE: the anchor is not rolled, it is SEARCHED. 3x3 candidates per cell,
2141 // flattest wins (first-min on the fixed scan order = deterministic), and wheight is
2142 // pure noise over absolute coords, so the choice is window-independent (T73's law).
2143 // The measured failure this replaces: one hash-jittered roll + a dh>1 veto rejected
2144 // EVERY vale and craft cell (probe 2026-08-18: dhmax 2..12 across 36 cells) -- a
2145 // stamp with a veto is not a solve, and the village never existed.
2146 var axc: i64 = 0
2147 var azc: i64 = 0
2148 var h: i64 = 0
2149 var bestd9: i64 = 99
2150 if put == 1 {
2151 var co9: i64 = 0
2152 while co9 < 9 {
2153 let cox9: i64 = gx*BLDG_SITE + IG_SCAN0 + (co9 % 3)*IG_SCANSTEP
2154 let coz9: i64 = gz*BLDG_SITE + IG_SCAN0 + (co9 / 3)*IG_SCANSTEP
2155 let hh9: i64 = wsp_beach_ocean(sp, cox9, coz9, wheight(base, cox9, coz9, sd)) // GE13: a site in the sea reads its SHAPED height -> the h<=wl veto below refuses it
2156 var dmax9: i64 = 0
2157 var ci: i64 = 0
2158 while ci < 4 {
2159 var clx: i64 = 0 - 1
2160 var clz: i64 = 0 - 1
2161 if ci == 1 { clx = bw }
2162 if ci == 2 { clz = bd }
2163 if ci == 3 { clx = bw; clz = bd }
2164 let cax: i64 = cox9 + ig_rotx(rot, clx - bw/2, clz - bd/2)
2165 let caz: i64 = coz9 + ig_rotz(rot, clx - bw/2, clz - bd/2)
2166 var dh: i64 = wsp_beach_ocean(sp, cax, caz, wheight(base, cax, caz, sd)) - hh9
2167 if dh < 0 { dh = 0 - dh }
2168 if dh > dmax9 { dmax9 = dh }
2169 ci = ci + 1
2170 }
2171 if dmax9 < bestd9 { bestd9 = dmax9; axc = cox9; azc = coz9; h = hh9 }
2172 co9 = co9 + 1
2173 }
2174 if bestd9 > IG_MAXSLOPE { put = 0 }
2175 if h <= wl { put = 0 }
2176 if h >= sp[P_SNOW] { put = 0 }
2177 if h + bh + 2 >= WY { put = 0 }
2178 }
2179 if put == 1 {
2180 // floor+foundation ring at h, walls h+1..h+bh (door and windows carved as air,
2181 // interior cleared of terrain and canopy), roof plate at h+bh+1
2182 var lz: i64 = 0 - 1
2183 while lz <= bd {
2184 var lx: i64 = 0 - 1
2185 while lx <= bw {
2186 let ax: i64 = axc + ig_rotx(rot, lx - bw/2, lz - bd/2) - s[S_WOX]
2187 let az: i64 = azc + ig_rotz(rot, lx - bw/2, lz - bd/2) - s[S_WOZ]
2188 var ring: i64 = 0
2189 if lx == 0 - 1 { ring = 1 }
2190 if lx == bw { ring = 1 }
2191 if lz == 0 - 1 { ring = 1 }
2192 if lz == bd { ring = 1 }
2193 var fb2: i64 = mfloor
2194 if ring == 1 { fb2 = mwall }
2195 vset_box(base, ax, h, az, fb2, lx0, lx1, lz0, lz1)
2196 // foundation skirt: IG_FOUND courses below the floor on the ring, so a
2197 // site accepted at the IG_MAXSLOPE bound is grounded on every side. On
2198 // flat ground the courses embed in terrain (a real footing, seen when
2199 // mined) -- bounded, deterministic, no reads.
2200 if ring == 1 {
2201 var fy9: i64 = h - 1
2202 while fy9 >= h - IG_FOUND {
2203 if fy9 >= 0 { vset_box(base, ax, fy9, az, mwall, lx0, lx1, lz0, lz1) }
2204 fy9 = fy9 - 1
2205 }
2206 }
2207 var wy2: i64 = h + 1
2208 while wy2 <= h + bh {
2209 var wb2: i64 = 0
2210 if ring == 1 {
2211 wb2 = mwall
2212 if lx == dxo { if lz == 0 - 1 { if wy2 <= h + 2 { wb2 = 0 } } }
2213 if wy2 == h + 2 { if lz == bd/2 {
2214 if lx == 0 - 1 { wb2 = 0 }
2215 if lx == bw { wb2 = 0 }
2216 } }
2217 }
2218 vset_box(base, ax, wy2, az, wb2, lx0, lx1, lz0, lz1)
2219 wy2 = wy2 + 1
2220 }
2221 vset_box(base, ax, h + bh + 1, az, mroof, lx0, lx1, lz0, lz1)
2222 lx = lx + 1
2223 }
2224 lz = lz + 1
2225 }
2226 // the solved furniture rows, rotated with the shell
2227 var fi: i64 = 0
2228 while fi < igd_nf(arch, bv) {
2229 let fr: i64 = igd_f(arch, bv, fi)
2230 let fax: i64 = axc + ig_rotx(rot, (fr & 255) - bw/2, ((fr >> 8) & 255) - bd/2) - s[S_WOX]
2231 let faz: i64 = azc + ig_rotz(rot, (fr & 255) - bw/2, ((fr >> 8) & 255) - bd/2) - s[S_WOZ]
2232 vset_box(base, fax, h + ((fr >> 24) & 255), faz, (fr >> 16) & 255, lx0, lx1, lz0, lz1)
2233 fi = fi + 1
2234 }
2235 // world truth: count the stamp when its anchor lands inside the regen box
2236 let axl: i64 = axc - s[S_WOX]
2237 let azl: i64 = azc - s[S_WOZ]
2238 if axl >= lx0 { if axl < lx1 { if azl >= lz0 { if azl < lz1 {
2239 s[S_BLDG] = s[S_BLDG] + 1
2240 s[S_BLDGX] = axl
2241 s[S_BLDGY] = h
2242 s[S_BLDGZ] = azl
2243 } } } }
2244 }
2245 gx = gx + 1
2246 }
2247 gz = gz + 1
2248 }
2249 return 0
2250}
2251
2252func genworld(base: i64, sd: i64) -> i64 {
2253 let s: *i64 = wst(base)
2254 var z: i64 = 0
2255 while z < WZ {
2256 var x: i64 = 0
2257 while x < WX {
2258 gencol(base, x, z, x + s[S_WOX], z + s[S_WOZ], sd)
2259 x = x + 1
2260 }
2261 z = z + 1
2262 }
2263 gentrees(base, 0, WX, 0, WZ, sd)
2264 genbuildings(base, 0, WX, 0, WZ, sd)
2265 return 0
2266}
2267
2268// ---------- v6 STREAMING: slide the window one stride along x or z ----------
2269// The world is unbounded; the arena is the cache. Order matters: move kept voxels -> bump the
2270// origin -> regen the incoming strip from absolute coords -> repaint its trees -> overlay the
2271// journal (edits WIN) -> translate every arena-space coordinate (player, spawn, mobs) -> rebuild
2272// wmax -> invalidate the crosshair cache. One call = 16 blocks; ticks trigger it at the margins.
2273// Preserve the retained continuous-height cache at the same absolute world coordinates.
2274// Incoming columns are regenerated by gencol after the origin advances.
2275func wc_hq_shift(base: i64, dx: i64, dz: i64) -> i64 {
2276 var z: i64 = 0
2277 var zend: i64 = WZ
2278 var zs: i64 = 1
2279 if dz < 0 { z = WZ - 1; zend = 0 - 1; zs = 0 - 1 }
2280 while z != zend {
2281 var x: i64 = 0
2282 var xend: i64 = WX
2283 var xs: i64 = 1
2284 if dx < 0 { x = WX - 1; xend = 0 - 1; xs = 0 - 1 }
2285 while x != xend {
2286 let sx: i64 = x + dx
2287 let sz: i64 = z + dz
2288 if sx >= 0 { if sx < WX { if sz >= 0 { if sz < WZ {
2289 hq_set(base, x, z, hq_get(base, sx, sz))
2290 } } } }
2291 x = x + xs
2292 }
2293 z = z + zs
2294 }
2295 return 0
2296}
2297
2298func wc_shift(base: i64, dx: i64, dz: i64) -> i64 {
2299 if dx==0&&dz==0 { return 0 }
2300 let s: *i64 = wst(base)
2301 let v: *u8 = wvx(base)
2302 let sd: i64 = s[S_SEED]
2303 if dx != 0 {
2304 wc_hq_shift(base, dx, 0)
2305 var y: i64 = 0
2306 while y < WY {
2307 var z: i64 = 0
2308 while z < WZ {
2309 let row: i64 = (y*WZ + z)*WX
2310 if dx > 0 {
2311 var x: i64 = 0
2312 while x < WX - dx { v[row + x] = v[row + x + dx]; x = x + 1 }
2313 } else {
2314 var x2: i64 = WX - 1
2315 while x2 >= 0 - dx { v[row + x2] = v[row + x2 + dx]; x2 = x2 - 1 }
2316 }
2317 z = z + 1
2318 }
2319 y = y + 1
2320 }
2321 s[S_WOX] = s[S_WOX] + dx
2322 var sx0: i64 = 0
2323 var sx1: i64 = 0 - dx
2324 if dx > 0 { sx0 = WX - dx; sx1 = WX }
2325 var rz: i64 = 0
2326 while rz < WZ {
2327 var rx: i64 = sx0
2328 while rx < sx1 { gencol(base, rx, rz, rx + s[S_WOX], rz + s[S_WOZ], sd); rx = rx + 1 }
2329 rz = rz + 1
2330 }
2331 gentrees(base, sx0, sx1, 0, WZ, sd)
2332 genbuildings(base, sx0, sx1, 0, WZ, sd)
2333 ej_overlay(base, sx0, sx1, 0, WZ)
2334 }
2335 if dz != 0 {
2336 wc_hq_shift(base, 0, dz)
2337 var y2: i64 = 0
2338 while y2 < WY {
2339 let plane: i64 = y2*WZ*WX
2340 if dz > 0 {
2341 var z2: i64 = 0
2342 while z2 < WZ - dz {
2343 let dstr: i64 = plane + z2*WX
2344 let srcr: i64 = plane + (z2 + dz)*WX
2345 var x3: i64 = 0
2346 while x3 < WX { v[dstr + x3] = v[srcr + x3]; x3 = x3 + 1 }
2347 z2 = z2 + 1
2348 }
2349 } else {
2350 var z3: i64 = WZ - 1
2351 while z3 >= 0 - dz {
2352 let dstr2: i64 = plane + z3*WX
2353 let srcr2: i64 = plane + (z3 + dz)*WX
2354 var x4: i64 = 0
2355 while x4 < WX { v[dstr2 + x4] = v[srcr2 + x4]; x4 = x4 + 1 }
2356 z3 = z3 - 1
2357 }
2358 }
2359 y2 = y2 + 1
2360 }
2361 s[S_WOZ] = s[S_WOZ] + dz
2362 var sz0: i64 = 0
2363 var sz1: i64 = 0 - dz
2364 if dz > 0 { sz0 = WZ - dz; sz1 = WZ }
2365 var rz2: i64 = sz0
2366 while rz2 < sz1 {
2367 var rx2: i64 = 0
2368 while rx2 < WX { gencol(base, rx2, rz2, rx2 + s[S_WOX], rz2 + s[S_WOZ], sd); rx2 = rx2 + 1 }
2369 rz2 = rz2 + 1
2370 }
2371 gentrees(base, 0, WX, sz0, sz1, sd)
2372 genbuildings(base, 0, WX, sz0, sz1, sd)
2373 ej_overlay(base, 0, WX, sz0, sz1)
2374 }
2375 // translate every arena-space coordinate by the slide
2376 s[S_CX] = s[S_CX] - dx*256
2377 s[S_CZ] = s[S_CZ] - dz*256
2378 s[S_SPX] = wclamp(s[S_SPX] - dx*256, 2*256, (WX - 2)*256)
2379 s[S_SPZ] = wclamp(s[S_SPZ] - dz*256, 2*256, (WZ - 2)*256)
2380 s[S_LBX] = s[S_LBX] - dx
2381 s[S_LBZ] = s[S_LBZ] - dz
2382 let m: *i64 = mobp(base)
2383 var k: i64 = 0
2384 while k < en_count(m) {
2385 let h: i64 = en_nth(m, k)
2386 // Changing the resident cache must not move an entity in world space.
2387 en_set(m, h, MC_X, en_get(m, h, MC_X) - dx*256)
2388 en_set(m, h, MC_Z, en_get(m, h, MC_Z) - dz*256)
2389 k = k + 1
2390 }
2391 wc_wmax(base)
2392 wc_steer_reset(base)
2393 wc_soft_seat_all(base) // the world slid under every girl: re-seat springs
2394 if dx != 0 { s[S_AGEN] = s[S_AGEN] + 1 } else { if dz != 0 { s[S_AGEN] = s[S_AGEN] + 1 } } // GE57: a shift that MOVED is a new arena generation (a zero shift is not)
2395 s[S_HOK] = 0
2396 return 0
2397}
2398
2399// ---------- DDA raycast ----------
2400// marches from (S_CX,S_CY,S_CZ) along (dx,dy,dz) (any scale). On hit writes out[0..4] =
2401// cellx,celly,cellz,face,tcross and returns the step count; returns -1 on miss (sky).
2402// skipw=1 treats water as air (the transparency continuation ray). The voxel read is INLINED --
2403// this loop runs ~2-8M times a second and a per-step call was the measured overhead.
2404// GE53: march the continuous surface. pos(t) = cam + d*t/65536 (Q8). Quarter-cell steps along the fastest
2405// axis, then six bisections at the crossing -- sub-block precision on a field that is itself sub-block.
2406// Returns the cells travelled (the fog term); writes the column's top cell, face 3 and t. -1 = no ground.
2407func wray_surf_origin(base: i64, dx: i64, dy: i64, dz: i64, maxsteps: i64, out: *i64, view: i64) -> i64 {
2408 let s: *i64 = wst(base)
2409 var cxq: i64 = s[S_CX]
2410 if view==1{cxq=wc_view(base,0)}
2411 var cyq: i64 = s[S_CY]
2412 if view==1{cyq=wc_view(base,1)}
2413 var czq: i64 = s[S_CZ]
2414 if view==1{czq=wc_view(base,2)}
2415 var dm: i64 = dx
2416 if dm < 0 { dm = 0 - dm }
2417 var ady: i64 = dy
2418 if ady < 0 { ady = 0 - ady }
2419 var adz: i64 = dz
2420 if adz < 0 { adz = 0 - adz }
2421 if ady > dm { dm = ady }
2422 if adz > dm { dm = adz }
2423 if dm == 0 { return 0 - 1 }
2424 let dt: i64 = WC_SURF_STEP_Q8*WATER_MAGIC_65536/dm // a quarter cell along the fastest axis
2425 var t: i64 = 0
2426 var tprev: i64 = 0
2427 var step: i64 = 0
2428 let nmax: i64 = maxsteps*4
2429 var hit: i64 = 0
2430 while step < nmax {
2431 t = t + dt
2432 let py: i64 = cyq + dy*t/WATER_MAGIC_65536
2433 let px: i64 = cxq + dx*t/WATER_MAGIC_65536
2434 let pz: i64 = czq + dz*t/WATER_MAGIC_65536
2435 if dy >= 0 { if py > WY*256 { return 0 - 1 } } // climbing above every column: sky
2436 if px < 0 - 8*256 { return 0 - 1 }
2437 if px > (WX + 8)*256 { return 0 - 1 }
2438 if pz < 0 - 8*256 { return 0 - 1 }
2439 if pz > (WZ + 8)*256 { return 0 - 1 }
2440 let hs: i64 = wc_surface_source(base, px, pz)
2441 if hs >= 0 { if py <= hs { hit = 1; step = nmax } }
2442 if hit == 0 { tprev = t; step = step + 1 }
2443 }
2444 if hit == 0 { return 0 - 1 }
2445 var lo: i64 = tprev
2446 var hi: i64 = t
2447 var k: i64 = 0
2448 while k < 6 {
2449 let mid: i64 = (lo + hi)/2
2450 let py2: i64 = cyq + dy*mid/WATER_MAGIC_65536
2451 let px2: i64 = cxq + dx*mid/WATER_MAGIC_65536
2452 let pz2: i64 = czq + dz*mid/WATER_MAGIC_65536
2453 let h2: i64 = wc_surface_source(base, px2, pz2)
2454 var below: i64 = 0
2455 if h2 >= 0 { if py2 <= h2 { below = 1 } }
2456 if below == 1 { hi = mid } else { lo = mid }
2457 k = k + 1
2458 }
2459 let th: i64 = hi
2460 let hx: i64 = cxq + dx*th/WATER_MAGIC_65536
2461 let hz: i64 = czq + dz*th/WATER_MAGIC_65536
2462 let cx: i64 = hx >> 8
2463 let cz: i64 = hz >> 8
2464 // the colour cell: the column's top SOLID block, found by scanning down from the hit cell -- so it
2465 // matches whatever gencol cut, whichever rounding convention built the column (the field rounds,
2466 // wheight_raw truncates; guessing the index from the field put the cell one block up, in air)
2467 let hy: i64 = cyq + dy*th/WATER_MAGIC_65536
2468 var yy: i64 = hy >> 8
2469 if yy > WY - 1 { yy = WY - 1 }
2470 var run9: i64 = 1
2471 while run9 == 1 {
2472 if yy <= 0 { run9 = 0 }
2473 if run9 == 1 { if vget(base, cx, yy, cz) != 0 { run9 = 0 } }
2474 if run9 == 1 { yy = yy - 1 }
2475 }
2476 out[0] = cx
2477 out[1] = yy
2478 out[2] = cz
2479 out[3] = 3
2480 out[4] = th
2481 return th/(dt*(256/WC_SURF_STEP_Q8)) + 1
2482}
2483// GE53: under P_SURF the ground is the field and structures stay cells -- the nearer hit wins. With the
2484// row OFF this is exactly the cell marcher (wray3 with no terrain skip), so every shipped world is untouched.
2485func wray_surf(base: i64, dx: i64, dy: i64, dz: i64, maxsteps: i64, out: *i64) -> i64 {return wray_surf_origin(base,dx,dy,dz,maxsteps,out,0)}
2486
2487func wray2_origin(base: i64, dx: i64, dy: i64, dz: i64, maxsteps: i64, out: *i64, skipw: i64, view: i64) -> i64 {
2488 if wsp(base)[P_SURF] != 1 { return wray3_origin(base, dx, dy, dz, maxsteps, out, skipw, 0, view) }
2489 let ss: i64 = wray_surf_origin(base, dx, dy, dz, maxsteps, out, view)
2490 let s0: i64 = out[0]
2491 let s1: i64 = out[1]
2492 let s2: i64 = out[2]
2493 let s3: i64 = out[3]
2494 let s4: i64 = out[4]
2495 let sd: i64 = wray3_origin(base, dx, dy, dz, maxsteps, out, skipw, 1, view)
2496 if sd < 0 {
2497 if ss < 0 { return 0 - 1 }
2498 out[0] = s0
2499 out[1] = s1
2500 out[2] = s2
2501 out[3] = s3
2502 out[4] = s4
2503 return ss
2504 }
2505 if ss < 0 { return sd }
2506 if out[4] <= s4 { return sd }
2507 out[0] = s0
2508 out[1] = s1
2509 out[2] = s2
2510 out[3] = s3
2511 out[4] = s4
2512 return ss
2513}
2514func wray2(base: i64, dx: i64, dy: i64, dz: i64, maxsteps: i64, out: *i64, skipw: i64) -> i64 {return wray2_origin(base,dx,dy,dz,maxsteps,out,skipw,0)}
2515
2516func wray3_origin(base: i64, dx: i64, dy: i64, dz: i64, maxsteps: i64, out: *i64, skipw: i64, skipt: i64, view: i64) -> i64 {
2517 let s: *i64 = wst(base)
2518 var cxq: i64 = s[S_CX]
2519 if view==1{cxq=wc_view(base,0)}
2520 var cyq: i64 = s[S_CY]
2521 if view==1{cyq=wc_view(base,1)}
2522 var czq: i64 = s[S_CZ]
2523 if view==1{czq=wc_view(base,2)}
2524 var cellx: i64 = cxq >> 8
2525 var celly: i64 = cyq >> 8
2526 var cellz: i64 = czq >> 8
2527 let HUGE: i64 = RAY_HUGE
2528 var stpx: i64 = 1
2529 if dx < 0 { stpx = 0-1 }
2530 var stpy: i64 = 1
2531 if dy < 0 { stpy = 0-1 }
2532 var stpz: i64 = 1
2533 if dz < 0 { stpz = 0-1 }
2534 // t units: pos_q8(t) = pos + d*t/65536; crossing one cell on axis c costs 256*65536/|dc|
2535 var tdx: i64 = HUGE
2536 if dx != 0 { tdx = 256*WATER_MAGIC_65536 / dx; if tdx < 0 { tdx = 0 - tdx } }
2537 var tdy: i64 = HUGE
2538 if dy != 0 { tdy = 256*WATER_MAGIC_65536 / dy; if tdy < 0 { tdy = 0 - tdy } }
2539 var tdz: i64 = HUGE
2540 if dz != 0 { tdz = 256*WATER_MAGIC_65536 / dz; if tdz < 0 { tdz = 0 - tdz } }
2541 var bx: i64 = (cellx << 8) - cxq // distance to boundary, Q8 (negative side)
2542 if stpx == 1 { bx = bx + 256 }
2543 var tmx: i64 = HUGE
2544 if dx != 0 { tmx = bx*WATER_MAGIC_65536 / dx; if tmx < 0 { tmx = 0 - tmx } }
2545 var by: i64 = (celly << 8) - cyq
2546 if stpy == 1 { by = by + 256 }
2547 var tmy: i64 = HUGE
2548 if dy != 0 { tmy = by*WATER_MAGIC_65536 / dy; if tmy < 0 { tmy = 0 - tmy } }
2549 var bz: i64 = (cellz << 8) - czq
2550 if stpz == 1 { bz = bz + 256 }
2551 var tmz: i64 = HUGE
2552 if dz != 0 { tmz = bz*WATER_MAGIC_65536 / dz; if tmz < 0 { tmz = 0 - tmz } }
2553 let vxp: *u8 = (base + O_VOX) as *u8
2554 var step: i64 = 0
2555 while step < maxsteps {
2556 var face: i64 = 0
2557 var tcross: i64 = 0
2558 if tmx <= tmy { if tmx <= tmz {
2559 cellx = cellx + stpx
2560 tcross = tmx
2561 tmx = tmx + tdx
2562 face = 1
2563 if stpx == 1 { face = 2 } // stepped +x => hit the -x... face 2 = west-lit
2564 } }
2565 if face == 0 { if tmy <= tmz {
2566 celly = celly + stpy
2567 tcross = tmy
2568 tmy = tmy + tdy
2569 face = 3
2570 if stpy == 1 { face = 4 } // stepped up => hit the block's bottom
2571 } }
2572 if face == 0 {
2573 cellz = cellz + stpz
2574 tcross = tmz
2575 tmz = tmz + tdz
2576 face = 5
2577 if stpz == 1 { face = 6 }
2578 }
2579 if celly >= WY { if stpy >= 0 { return 0 - 1 } } // heading skyward above the world
2580 if celly > s[S_WMAX] { if stpy >= 0 { return 0 - 1 } } // above every solid block, not diving
2581 if cellx < 0-8 { return 0 - 1 }
2582 if cellx > WX+8 { return 0 - 1 }
2583 if cellz < 0-8 { return 0 - 1 }
2584 if cellz > WZ+8 { return 0 - 1 }
2585 if celly < 0 { return 0 - 1 }
2586 var b: i64 = 0
2587 if cellx >= 0 { if cellx < WX { if celly < WY { if cellz >= 0 { if cellz < WZ {
2588 b = (vxp[(celly*WZ + cellz)*WX + cellx] & 0xff) as i64
2589 } } } } }
2590 if skipw == 1 { if b == 4 { b = 0 } }
2591 if skipt == 1 { if wterrain(b) == 1 { b = 0 } } // GE53: the ground is the field, not these cells
2592 if b != 0 {
2593 out[0] = cellx
2594 out[1] = celly
2595 out[2] = cellz
2596 out[3] = face
2597 out[4] = tcross // crossing t: hit point = pos + d*t/WATER_MAGIC_65536 (Q8)
2598 return step + 1
2599 }
2600 step = step + 1
2601 }
2602 return 0 - 1
2603}
2604
2605func wray3(base: i64, dx: i64, dy: i64, dz: i64, maxsteps: i64, out: *i64, skipw: i64, skipt: i64) -> i64 {return wray3_origin(base,dx,dy,dz,maxsteps,out,skipw,skipt,0)}
2606
2607func wray(base: i64, dx: i64, dy: i64, dz: i64, maxsteps: i64, out: *i64) -> i64 {
2608 return wray2(base, dx, dy, dz, maxsteps, out, 0)
2609}
2610
2611// block base colour -- from the SPEC PALETTE (the game's look is data, not code)
2612func wcol(base: i64, b: i64) -> i64 {
2613 if b < 0 { return wpack(255, 0, 255) }
2614 if b >= 12 { if b <= 16 { return wsp(base)[P_PAL2 + b - 12] } }
2615 if b > 16 { return wpack(255, 0, 255) }
2616 if b > 11 { return wpack(255, 0, 255) }
2617 return wsp(base)[P_PAL + b]
2618}
2619// hotbar slot -> placeable block type
2620func sel_block(sel: i64) -> i64 {
2621 if sel == 1 { return 2 } // dirt
2622 if sel == 2 { return 6 } // wood
2623 if sel == 3 { return 7 } // leaves
2624 if sel == 4 { return 5 } // sand
2625 if sel == 5 { return B_FARM } // the HOE: tills the targeted grass/dirt top
2626 if sel == 6 { return B_CROP0 } // SEEDS: plant onto farmland
2627 return 3 // stone
2628}
2629// face brightness permil: top lit, bottom dark, x/z sides distinct so edges READ as 3d
2630func wface_l(face: i64) -> i64 {
2631 if face == 3 { return 1000 }
2632 if face == 4 { return 420 }
2633 if face == 1 { return 760 }
2634 if face == 2 { return 700 }
2635 if face == 5 { return 620 }
2636 return 560
2637}
2638func wsky(base: i64, syy: i64) -> i64 {
2639 let sp: *i64 = wsp(base)
2640 let hor: i64 = sp[P_SKYHOR]
2641 let top: i64 = sp[P_SKYTOP]
2642 // DERIVED, NOT PICKED (2026-08-29). RH is the q=8 fossil: line 91 of this file declares that
2643 // no resolution number below it is chosen by hand, and RW/RH/FOC three lines under that are
2644 // exactly 1920/8, 1200/8 and 1920/16. This function divided the horizon-to-zenith ramp by that
2645 // frozen 150 while the frame is wc_rh(base) tall, so for any q < 8 the ramp COMPLETED in the
2646 // bottom 150 rows and every row above saturated at P_SKYTOP -- the two spec rows describe a
2647 // gradient the visitor never saw. At q=1, which is exactly what nx_world_snap forces through
2648 // WS_Q_NATIVE for published world art, that is 87.5% of the sky column flat.
2649 // base is already a parameter here and wc_rh is the declared ONE OWNER of the written extent,
2650 // so the derivation needs no new plumbing and no hoist.
2651 var rh: i64 = wc_rh(base)
2652 if rh < 1 { rh = 1 } // q is 1..24 so this cannot fire; it exists so
2653 // that a bad q can never divide by zero here
2654 let t: i64 = wclamp(syy + rh/2, 0, rh) // 0 bottom .. rh top
2655 let r: i64 = (hor & 255) + ((top & 255) - (hor & 255))*t/rh
2656 let g: i64 = ((hor >> 8) & 255) + (((top >> 8) & 255) - ((hor >> 8) & 255))*t/rh
2657 let b: i64 = ((hor >> 16) & 255) + (((top >> 16) & 255) - ((hor >> 16) & 255))*t/rh
2658 return wpack(r, g, b)
2659}
2660// clouds: a flat noise plane above the world, sampled where an upward ray crosses it, slowly
2661// drifting with time. Returns 0..255 coverage for blending into the sky.
2662func wcloud(camx: i64, camy: i64, camz: i64, dx: i64, dy: i64, dz: i64, t: i64) -> i64 {
2663 if dy < 40 { return 0 }
2664 let plane: i64 = 56*256 // cloud deck above the 48-high world
2665 let tq: i64 = (plane - camy)*WATER_MAGIC_65536/dy
2666 if tq <= 0 { return 0 }
2667 let cx: i64 = (camx + dx*tq/WATER_MAGIC_65536) >> 9 // 2-block cloud texels
2668 let cz: i64 = (camz + dz*tq/WATER_MAGIC_65536) >> 9
2669 let n: i64 = wnoise(cx + t/40, cz + t/90, 6, 777)
2670 if n < 148 { return 0 }
2671 var cov: i64 = (n - 148)*255/60
2672 if cov > 235 { cov = 235 }
2673 return cov
2674}
2675// sky with a SUN: disc + glow around the sun's projected ray-pixel position (sunon=0 -> plain sky)
2676func wsky2(base: i64, syy: i64, px: i64, py: i64, sunx: i64, suny: i64, sunon: i64) -> i64 {
2677 let base2: i64 = wsky(base, syy)
2678 if sunon == 0 { return base2 }
2679 let ddx: i64 = px - sunx
2680 let ddy: i64 = py - suny
2681 let d2: i64 = ddx*ddx + ddy*ddy
2682 if d2 < 36 { return wpack(255, 250, 224) } // the disc
2683 if d2 < 900 { // the glow
2684 let w2: i64 = 255 - d2*255/900
2685 var r: i64 = (base2 & 255) + (255 - (base2 & 255))*w2/300
2686 var g: i64 = ((base2 >> 8) & 255) + (250 - ((base2 >> 8) & 255))*w2/340
2687 var b: i64 = ((base2 >> 16) & 255) + (224 - ((base2 >> 16) & 255))*w2/460
2688 return wpack(wclamp(r,0,255), wclamp(g,0,255), wclamp(b,0,255))
2689 }
2690 // ordered 2x2 dither breaks the gradient banding lines
2691 if ((px ^ py) & 1) == 1 {
2692 return wpack(wclamp((base2 & 255) + 2, 0, 255), wclamp(((base2 >> 8) & 255) + 2, 0, 255), wclamp(((base2 >> 16) & 255) + 2, 0, 255))
2693 }
2694 return base2
2695}
2696// textured face shading: per-texel hash variation from the HIT POINT's in-face UV (Q8 fractions),
2697// per-type character (wood grain, leaf speckle, water shimmer, ore mottle), edge BEVEL so the
2698// block grid reads crisply, then face light and step fog. All integer, all deterministic.
2699// corner AO: darken a face edge when a solid block sits beside it in the face's outward layer --
2700// the classic soft-corner cue that makes voxel scenes read as lit geometry instead of flat tiles
2701func wao(base: i64, cx2: i64, cy2b: i64, cz2: i64, face: i64, u: i64, v: i64) -> i64 {
2702 var ox2: i64 = cx2
2703 var oy2: i64 = cy2b
2704 var oz2: i64 = cz2
2705 if face == 1 { ox2 = ox2 + 1 }
2706 if face == 2 { ox2 = ox2 - 1 }
2707 if face == 3 { oy2 = oy2 + 1 }
2708 if face == 4 { oy2 = oy2 - 1 }
2709 if face == 5 { oz2 = oz2 + 1 }
2710 if face == 6 { oz2 = oz2 - 1 }
2711 var ao: i64 = 0
2712 // u-axis neighbours (u maps to x on y/z faces, to z on x faces)
2713 if u < 44 {
2714 var hit1: i64 = 0
2715 if face == 1 { hit1 = vsolid(base, ox2, oy2, oz2 - 1) }
2716 if face == 2 { hit1 = vsolid(base, ox2, oy2, oz2 - 1) }
2717 if face == 3 { hit1 = vsolid(base, ox2 - 1, oy2, oz2) }
2718 if face == 4 { hit1 = vsolid(base, ox2 - 1, oy2, oz2) }
2719 if face == 5 { hit1 = vsolid(base, ox2 - 1, oy2, oz2) }
2720 if face == 6 { hit1 = vsolid(base, ox2 - 1, oy2, oz2) }
2721 if hit1 == 1 { ao = ao + (44 - u)*30/44 }
2722 }
2723 if u > 211 {
2724 var hit2: i64 = 0
2725 if face == 1 { hit2 = vsolid(base, ox2, oy2, oz2 + 1) }
2726 if face == 2 { hit2 = vsolid(base, ox2, oy2, oz2 + 1) }
2727 if face == 3 { hit2 = vsolid(base, ox2 + 1, oy2, oz2) }
2728 if face == 4 { hit2 = vsolid(base, ox2 + 1, oy2, oz2) }
2729 if face == 5 { hit2 = vsolid(base, ox2 + 1, oy2, oz2) }
2730 if face == 6 { hit2 = vsolid(base, ox2 + 1, oy2, oz2) }
2731 if hit2 == 1 { ao = ao + (u - 211)*30/44 }
2732 }
2733 // v-axis neighbours (v maps to z on top/bottom faces, to y on side faces)
2734 if v < 44 {
2735 var hit3: i64 = 0
2736 if face == 3 { hit3 = vsolid(base, ox2, oy2, oz2 - 1) }
2737 if face == 4 { hit3 = vsolid(base, ox2, oy2, oz2 - 1) }
2738 if face == 1 { hit3 = vsolid(base, ox2, oy2 - 1, oz2) }
2739 if face == 2 { hit3 = vsolid(base, ox2, oy2 - 1, oz2) }
2740 if face == 5 { hit3 = vsolid(base, ox2, oy2 - 1, oz2) }
2741 if face == 6 { hit3 = vsolid(base, ox2, oy2 - 1, oz2) }
2742 if hit3 == 1 { ao = ao + (44 - v)*30/44 }
2743 }
2744 if v > 211 {
2745 var hit4: i64 = 0
2746 if face == 3 { hit4 = vsolid(base, ox2, oy2, oz2 + 1) }
2747 if face == 4 { hit4 = vsolid(base, ox2, oy2, oz2 + 1) }
2748 if face == 1 { hit4 = vsolid(base, ox2, oy2 + 1, oz2) }
2749 if face == 2 { hit4 = vsolid(base, ox2, oy2 + 1, oz2) }
2750 if face == 5 { hit4 = vsolid(base, ox2, oy2 + 1, oz2) }
2751 if face == 6 { hit4 = vsolid(base, ox2, oy2 + 1, oz2) }
2752 if hit4 == 1 { ao = ao + (v - 211)*30/44 }
2753 }
2754 if ao > 45 { ao = 45 }
2755 return ao
2756}
2757func wshade2(base: i64, b0: i64, face: i64, steps: i64, syy: i64, hpx: i64, hpy: i64, hpz: i64, t2: i64, clx: i64, cly: i64, clz: i64) -> i64 {
2758 var u: i64 = hpx & 255
2759 var v: i64 = hpz & 255
2760 if face == 1 { u = hpz & 255; v = hpy & 255 }
2761 if face == 2 { u = hpz & 255; v = hpy & 255 }
2762 if face == 5 { u = hpx & 255; v = hpy & 255 }
2763 if face == 6 { u = hpx & 255; v = hpy & 255 }
2764 // grass block SIDES are dirt with a grass strip at the top edge (the classic block identity)
2765 var b: i64 = b0
2766 if b0 == 1 { if face != 3 { if face != 4 {
2767 b = 2
2768 if v > 206 { b = 1 }
2769 } } }
2770 var tex: i64 = 0
2771 if b == 6 { // wood: vertical grain stripes
2772 tex = whash(u/12 + (hpx >> 8)*7 + (hpz >> 8)*13, 3, 99) % 256
2773 }
2774 if b != 6 {
2775 var amp: i64 = 70
2776 if b == 1 { amp = 95 }
2777 if b == 2 { amp = 85 }
2778 if b == 7 { amp = 130 }
2779 if b == 5 { amp = 55 }
2780 var us: i64 = u/16
2781 var vs: i64 = v/16
2782 if b == 4 { // water: time-drifted shimmer
2783 us = us + t2/3
2784 vs = vs - t2/5
2785 amp = 110
2786 }
2787 tex = whash(us + (hpx >> 8)*31, vs + (hpy >> 8)*17 + (hpz >> 8)*7, b*131) % 256
2788 tex = 128 + (tex - 128)*amp/130
2789 }
2790 // ART STYLE (P_STYLE), read once for this texel. An id this engine does not own falls through
2791 // BOTH style tests below and renders REAL. That is the fail-safe direction and it is chosen,
2792 // not inherited: REAL is the shipped look, so a garbage value poked into the plane degrades to
2793 // "unchanged", never to an undrawn or half-drawn world.
2794 let wst9: i64 = wsp(base)[P_STYLE]
2795 if wst9 == WC_STYLE_FLAT { tex = WC_TEX_NEUTRAL }
2796 var light: i64 = wface_l(face)
2797 // sun-directional face light (sun worlds only): east faces catch it, west faces shadow
2798 if wsp(base)[P_SUN] == 1 {
2799 if face == 1 { light = light*112/100 }
2800 if face == 2 { light = light*92/100 }
2801 if face == 5 { light = light*104/100 }
2802 }
2803 light = light*(870 + tex*260/256)/1000
2804 let wsurf9: i64 = wsp(base)[P_SURF]
2805 var smooth9: i64 = 0
2806 if wst9 == WC_STYLE_SOFT { smooth9 = 1 }
2807 if wsurf9 == 1 { smooth9 = 1 }
2808 if smooth9 == 0 {
2809 if u < 14 { light = light*82/100 } // edge bevel: the block grid (OFF on a smooth ground)
2810 if u > 241 { light = light*82/100 }
2811 if v < 14 { light = light*82/100 }
2812 if v > 241 { light = light*82/100 }
2813 }
2814 var ao2: i64 = 0
2815 if wsurf9 == 0 { ao2 = wao(base, clx, cly, clz, face, u, v) } // GE53: a continuous surface has no cell corners to darken
2816 if ao2 > 0 { light = light*(100 - ao2)/100 } // soft corners where geometry meets
2817 if wst9 == WC_STYLE_SOFT { if face == 3 {
2818 // ground: the heightfield normal replaces the flat top-face light; texture and AO re-applied
2819 light = wsoft_ground_light(base, clx, cly, clz, u, v)*(870 + tex*260/256)/1000
2820 if ao2 > 0 { light = light*(100 - ao2)/100 }
2821 } }
2822 if wsurf9 == 1 { if face == 3 {
2823 light = wsurf_light(base, hpx, hpz)*(870 + tex*260/256)/1000 // GE53: the field's own gradient lights the ground
2824 } }
2825 // TOON banding sits AFTER face light, texture and AO and BEFORE colour and fog -- the same
2826 // point in the pipeline at which the incumbent bands its own "bright". Fog must stay OUTSIDE
2827 // the band, or distance itself would quantise into two steps and the horizon would read as a
2828 // wall rather than as depth. Written as ONE assignment through a temporary rather than as two
2829 // ifs on the same variable: two ifs where the first mutates the value the second tests is the
2830 // desync idiom this estate has been bitten by, and it costs nothing to be immune to it.
2831 if wst9 == WC_STYLE_TOON {
2832 var lb9: i64 = WC_TOON_HI
2833 if light < WC_TOON_CUT { lb9 = WC_TOON_LO }
2834 light = lb9
2835 }
2836 let c: i64 = wcol(base, b)
2837 var r: i64 = (c & 255) * light / 1000
2838 var g: i64 = ((c >> 8) & 255) * light / 1000
2839 var bl: i64 = ((c >> 16) & 255) * light / 1000
2840 let sk: i64 = wsky(base, syy)
2841 var fog: i64 = steps*280/MAXSTEPS
2842 if fog > 236 { fog = 236 }
2843 r = r + ((sk & 255) - r)*fog/256
2844 g = g + (((sk >> 8) & 255) - g)*fog/256
2845 bl = bl + (((sk >> 16) & 255) - bl)*fog/256
2846 return wpack(wclamp(r,0,255), wclamp(g,0,255), wclamp(bl,0,255))
2847}
2848
2849// THE GAME SPEC WRITER -- two complete game identities as DATA over one engine.
2850// v0 = NISHI CRAFT (sunlit overworld). v1 = NISHI DEPTHS (twilight cavern plateau: no sun/clouds/
2851// trees, ember horizon, vast cave nets, mossy basalt, glowing ores and glow-mobs).
2852func wc_spec(base: i64, v: i64) -> i64 {
2853 let sp: *i64 = wsp(base)
2854 var k: i64 = 0
2855 while k < WC_SPECN { sp[k] = 0; k = k + 1 } // E1: was a bare 64. This loop is what re-arms
2856 // P_ERTAL to its not-yet-measured sentinel on
2857 // every identity load, so leaving it at 64 would
2858 // have carried one world's talus into the next.
2859 // GE13b THE WARDROBE -- identity-independent DATA, written before any identity branch so every
2860 // world owns the same table by default and may override a row below (or through a recipe row
2861 // once the page validator admits 70-78). Palettes are hue RANGES, never one colour: the genome
2862 // picks the stop. Swimwear vivid and wide (resort colour), the cover-up sun-bleached, casual
2863 // muted, the evening dress deep and narrow -- the one dress every girl used to wear.
2864 // coverage category hue span sat lit
2865 sp[P_GARM + GAR_BIKINI_TOP - 1] = gar_pack(GAR_COV_TOP, GAR_CAT_SWIM, 330, 120, 13, 8)
2866 sp[P_GARM + GAR_BIKINI_BOT - 1] = gar_pack(GAR_COV_BOT, GAR_CAT_SWIM, 330, 120, 13, 8)
2867 sp[P_GARM + GAR_ONEPIECE - 1] = gar_pack(GAR_COV_FULL, GAR_CAT_SWIM, 180, 100, 12, 6)
2868 sp[P_GARM + GAR_SARONG - 1] = gar_pack(GAR_COV_BOT, GAR_CAT_COVER, 20, 60, 9, 11)
2869 sp[P_GARM + GAR_SUNDRESS - 1] = gar_pack(GAR_COV_FULL, GAR_CAT_CASUAL, 40, 80, 8, 12)
2870 sp[P_GARM + GAR_TEE - 1] = gar_pack(GAR_COV_TOP, GAR_CAT_CASUAL, 200, 160, 6, 10)
2871 sp[P_GARM + GAR_SHORTS - 1] = gar_pack(GAR_COV_BOT, GAR_CAT_CASUAL, 30, 40, 5, 6)
2872 sp[P_GARM + GAR_SKIRT - 1] = gar_pack(GAR_COV_BOT, GAR_CAT_CASUAL, 340, 80, 7, 7)
2873 sp[P_GARM + GAR_DRESS - 1] = gar_pack(GAR_COV_FULL, GAR_CAT_EVENING, 250, 90, 10, 4)
2874 if v == 3 {
2875 // v3 = NISHI VALE, the MEDIEVAL VILLAGE: rolling green highland, deep woods, cold
2876 // snowline, cave-riddled stone. THIRD WORLD, ZERO ENGINE CODE -- the recombination
2877 // claim at n=3. Wardens run wide here (P_WARD), so a traveler arrives to REAL stakes.
2878 sp[P_TBASE] = 8
2879 sp[P_TAMP1] = 22
2880 sp[P_TAMP2] = 7
2881 sp[P_WATER] = 7
2882 sp[P_SNOW] = 26
2883 sp[P_TREE] = 11 // deep woods, tuned to the PUBLISHED
2884 // 500-800 trees/ha band -- 19 measured 372/ha,
2885 // barely half a real forest (T53; ported from
2886 // the laptop line 2026-08-02, same fork as
2887 // P_HISTAGE -- the fix existed and never landed)
2888 sp[P_TREEMAX] = 25 // woods climb to the snowline here
2889 sp[P_HISTAGE] = 0 // MEASURED, not assumed: symmetric value-
2890 // noise yields HI ~594 permil = mature
2891 sp[P_CAVETH] = 168 // more cavern than craft's 182
2892 sp[P_SKYTOP] = wpack(86, 122, 176)
2893 sp[P_SKYHOR] = wpack(196, 200, 206)
2894 sp[P_SUN] = 1
2895 sp[P_CLOUD] = 1
2896 sp[P_PAL + 1] = wpack(72, 124, 56) // highland grass
2897 sp[P_PAL + 2] = wpack(94, 72, 50) // loam
2898 sp[P_PAL + 3] = wpack(118, 116, 120) // quarry stone
2899 sp[P_PAL + 4] = wpack(44, 82, 140) // cold water
2900 sp[P_PAL + 5] = wpack(190, 178, 150) // riverbank
2901 sp[P_PAL + 6] = wpack(88, 62, 36) // timber
2902 sp[P_PAL + 7] = wpack(40, 96, 44) // dark canopy
2903 sp[P_PAL + 8] = wpack(238, 240, 246) // snow
2904 sp[P_PAL + 9] = wpack(66, 66, 72)
2905 sp[P_PAL + 10] = wpack(176, 142, 118)
2906 sp[P_PAL + 11] = wpack(224, 188, 92)
2907 sp[P_MOBC + 0] = wpack(206, 160, 96)
2908 sp[P_MOBC + 1] = wpack(160, 118, 62)
2909 sp[P_MOBC + 2] = wpack(120, 158, 190)
2910 sp[P_MOBC + 3] = wpack(78, 110, 146)
2911 sp[P_MOBC + 4] = wpack(178, 120, 160)
2912 sp[P_MOBC + 5] = wpack(134, 86, 122)
2913 sp[P_PAL2 + 0] = wpack(88, 60, 38)
2914 sp[P_PAL2 + 1] = wpack(120, 186, 90)
2915 sp[P_PAL2 + 2] = wpack(92, 168, 58)
2916 sp[P_PAL2 + 3] = wpack(230, 200, 82)
2917 sp[P_PAL2 + 4] = wpack(58, 42, 30)
2918 sp[P_DAYLEN] = 72000
2919 sp[P_SEASD] = 3
2920 sp[P_GROWE] = 2400
2921 sp[P_GROWW] = 5 // a harder land: crops take longer
2922 sp[P_GROWD] = 1
2923 sp[P_RAINS + 0] = 420 // wet spring
2924 sp[P_RAINS + 1] = 220
2925 sp[P_RAINS + 2] = 380
2926 sp[P_RAINS + 3] = 180
2927 sp[P_ADV + 0] = (B_CROP2 << 16) | 9
2928 sp[P_ADV + 1] = (1 << 16) | 3
2929 sp[P_ADV + 2] = (4 << 16) | (2 << 8) | 2
2930 sp[P_CURI] = 400
2931 sp[P_OUTFIT] = 0
2932 sp[P_WARD] = 20 // wardens hunt from 20 blocks by DAY here
2933 sp[P_BLDG] = 2 // W3: the medieval village -- densest settlement
2934 sp[P_BLDGSPEC] = 2 // stonecot archetype
2935 return 0
2936 }
2937 if v == 2 {
2938 sp[P_SURF] = 1 // GE53: NISHI SHORE is the NO-VOXEL world -- its ground is the continuous field
2939 // v2 = NISHI SHORE, the REVIEW STAGE (operator 2026-08-02): near-flat warm sand, a
2940 // sea, no trees/caves/snow -- body and sprite rendering judged WITHOUT environment
2941 // distraction, with the outfit dimension ACTIVE (mixed beach). One engine, one more
2942 // identity: this whole world is 30 data rows, which IS the recombination claim.
2943 sp[P_TBASE] = 11
2944 sp[P_TAMP1] = 2
2945 sp[P_TAMP2] = 1
2946 sp[P_DUNE] = BEACH_DUNE_RECIPE
2947 sp[P_WATER] = 9
2948 sp[P_SNOW] = 99
2949 sp[P_TREE] = 0
2950 sp[P_CAVETH] = 255
2951 // GE13: the sea this identity always PROMISED -- P_WATER 9 under base 11 never dipped
2952 // below the waterline, so the data declared a shore the terrain shape could not
2953 // produce (the operator's 2026-08-27 frame: a beach with no ocean). One row fixes it:
2954 // ocean toward +z, shoreline at absolute z=100 (inside the boot window, beyond the
2955 // review stage and both gate fixture plateaus), 20-block beach ramp, seabed 4 =>
2956 // up to 5 blocks of swimmable water at flood level 9.
2957 sp[P_OCEAN] = 0 + (612 << 2) + (20 << 12) + (4 << 18) // edge=+z shore=100+512 slope=20 bed=4
2958 sp[P_SKYTOP] = wpack(110, 168, 230)
2959 sp[P_SKYHOR] = wpack(214, 226, 240)
2960 sp[P_SUN] = 1
2961 sp[P_CLOUD] = 1
2962 sp[P_PAL + 1] = wpack(226, 204, 148) // shore sand (the "grass" slot)
2963 sp[P_PAL + 2] = wpack(206, 180, 128) // packed sand
2964 sp[P_PAL + 3] = wpack(180, 170, 158) // beach rock
2965 sp[P_PAL + 4] = wpack(56, 140, 196) // warm sea
2966 sp[P_PAL + 5] = wpack(232, 214, 160) // dry sand
2967 sp[P_PAL + 6] = wpack(150, 110, 70) // driftwood
2968 sp[P_PAL + 7] = wpack(96, 150, 96) // beach scrub
2969 sp[P_PAL + 8] = wpack(240, 240, 244)
2970 sp[P_PAL + 9] = wpack(120, 116, 110)
2971 sp[P_PAL + 10] = wpack(176, 142, 118)
2972 sp[P_PAL + 11] = wpack(224, 188, 92)
2973 sp[P_MOBC + 0] = wpack(240, 178, 84)
2974 sp[P_MOBC + 1] = wpack(196, 138, 56)
2975 sp[P_MOBC + 2] = wpack(96, 206, 196)
2976 sp[P_MOBC + 3] = wpack(64, 158, 150)
2977 sp[P_MOBC + 4] = wpack(238, 138, 186)
2978 sp[P_MOBC + 5] = wpack(190, 100, 144)
2979 sp[P_PAL2 + 0] = wpack(94, 64, 40)
2980 sp[P_PAL2 + 1] = wpack(120, 186, 90)
2981 sp[P_PAL2 + 2] = wpack(92, 168, 58)
2982 sp[P_PAL2 + 3] = wpack(230, 200, 82)
2983 sp[P_PAL2 + 4] = wpack(64, 46, 32)
2984 sp[P_DAYLEN] = 72000
2985 sp[P_SEASD] = 3
2986 sp[P_GROWE] = 2400
2987 sp[P_GROWW] = 6
2988 sp[P_GROWD] = 1
2989 sp[P_RAINS + 0] = 60 // a review stage stays mostly sunny
2990 sp[P_RAINS + 1] = 40
2991 sp[P_RAINS + 2] = 60
2992 sp[P_RAINS + 3] = 40
2993 sp[P_ADV + 0] = (4 << 16) | (2 << 8) | 4 // the water's edge is the gathering place
2994 sp[P_ADV + 1] = (1 << 16) | 3
2995 sp[P_CURI] = 350
2996 sp[P_OUTFIT] = 1
2997 sp[P_BLDG] = 4 // W3: sparse driftwood huts along the shore
2998 sp[P_BLDGSPEC] = 1 // hut archetype
2999 return 0
3000 }
3001 // FARMING-LIFE rows, shared by every identity (the loop itself is identity-independent DATA)
3002 sp[P_PAL2 + 0] = wpack(94, 64, 40) // tilled farmland, dry
3003 sp[P_PAL2 + 1] = wpack(120, 186, 90) // crop sprout
3004 sp[P_PAL2 + 2] = wpack(92, 168, 58) // crop growing
3005 sp[P_PAL2 + 3] = wpack(230, 200, 82) // crop MATURE (reads golden at a glance)
3006 sp[P_PAL2 + 4] = wpack(64, 46, 32) // farmland wet (soaked dark soil)
3007 sp[P_DAYLEN] = 72000 // 20 real minutes at the fixed 60Hz sim
3008 // tick -- the shipped-sim benchmark band
3009 // (Minecraft 20:00/day, Stardew 14:33,
3010 // Vintage Story ~24:00): a day you can
3011 // farm INSIDE, not a light show
3012 sp[P_SEASD] = 3
3013 sp[P_GROWE] = 2400 // one growth/hydration roll per farm cell
3014 // per ~40s -- Minecraft's random-tick
3015 // cadence (~47s/block avg) on our grid;
3016 // wet stencil den=5 => a watered crop
3017 // matures in ~6.7 min =~ 1/3 day (the
3018 // Minecraft wheat band)
3019 sp[P_GROWW] = 6
3020 sp[P_GROWD] = 1
3021 sp[P_RAINS + 0] = 340 // spring
3022 sp[P_RAINS + 1] = 180 // summer
3023 sp[P_RAINS + 2] = 300 // autumn
3024 sp[P_RAINS + 3] = 120 // winter
3025 sp[P_ADV + 0] = (B_CROP2 << 16) | 9 // a mature field draws foragers (need 0 = hunger)
3026 sp[P_ADV + 1] = (1 << 16) | 3 // grass meadow: light grazing
3027 sp[P_ADV + 2] = (4 << 16) | (2 << 8) | 2 // water's edge: where girls gather to talk
3028 sp[P_CURI] = 350
3029 if v == 1 {
3030 sp[P_TBASE] = 22
3031 sp[P_TAMP1] = 8
3032 sp[P_TAMP2] = 4
3033 sp[P_WATER] = 5
3034 sp[P_SNOW] = 99
3035 sp[P_TREE] = 0
3036 sp[P_CAVETH] = 148
3037 sp[P_SKYTOP] = wpack(28, 22, 40)
3038 sp[P_SKYHOR] = wpack(96, 48, 40)
3039 sp[P_SUN] = 0
3040 sp[P_CLOUD] = 0
3041 sp[P_PAL + 1] = wpack(74, 122, 84) // moss
3042 sp[P_PAL + 2] = wpack(88, 62, 48) // packed earth
3043 sp[P_PAL + 3] = wpack(98, 90, 104) // basalt
3044 sp[P_PAL + 4] = wpack(60, 190, 180) // glow-water
3045 sp[P_PAL + 5] = wpack(120, 100, 80)
3046 sp[P_PAL + 6] = wpack(70, 50, 36)
3047 sp[P_PAL + 7] = wpack(60, 90, 60)
3048 sp[P_PAL + 8] = wpack(200, 200, 210)
3049 sp[P_PAL + 9] = wpack(58, 58, 66)
3050 sp[P_PAL + 10] = wpack(198, 150, 116)
3051 sp[P_PAL + 11] = wpack(255, 206, 96) // gold veins GLOW down here
3052 sp[P_MOBC + 0] = wpack(255, 128, 64) // ember
3053 sp[P_MOBC + 1] = wpack(200, 84, 40)
3054 sp[P_MOBC + 2] = wpack(124, 220, 255) // ice-glow
3055 sp[P_MOBC + 3] = wpack(80, 160, 200)
3056 sp[P_MOBC + 4] = wpack(204, 124, 255) // violet
3057 sp[P_MOBC + 5] = wpack(150, 84, 200)
3058 return 0
3059 }
3060 sp[P_TBASE] = 4
3061 sp[P_TAMP1] = 17
3062 sp[P_TAMP2] = 5
3063 sp[P_WATER] = 9
3064 sp[P_SNOW] = 20
3065 sp[P_TREE] = 43
3066 sp[P_CAVETH] = 182
3067 // DEFAULT SKY TONES DERIVED, NOT PICKED (2026-08-26): zenith = 1 airmass, horizon = the
3068 // Kasten-Young 38-airmass bound, each channel single-scattered by lambda^-4 and display-
3069 // encoded -- nx_atmosphere's own worked example lands at (80,117,176) where the retired
3070 // hand-picked default sat at (86,122,176): the palette was approximating the physics.
3071 // Per-world identity overrides above KEEP their deliberate looks; only the default derives.
3072 let am_t: i64 = nx_atm_airmass_q(0)
3073 let am_h: i64 = nx_atm_airmass_q(NX_ATM_Q)
3074 sp[P_SKYTOP] = wpack(nx_atm_display_255(nx_atm_channel_q(NX_LAMBDA_R_NM, am_t)),
3075 nx_atm_display_255(nx_atm_channel_q(NX_LAMBDA_G_NM, am_t)),
3076 nx_atm_display_255(nx_atm_channel_q(NX_LAMBDA_B_NM, am_t)))
3077 sp[P_SKYHOR] = wpack(nx_atm_display_255(nx_atm_channel_q(NX_LAMBDA_R_NM, am_h)),
3078 nx_atm_display_255(nx_atm_channel_q(NX_LAMBDA_G_NM, am_h)),
3079 nx_atm_display_255(nx_atm_channel_q(NX_LAMBDA_B_NM, am_h)))
3080 sp[P_SUN] = 1
3081 sp[P_CLOUD] = 1
3082 sp[P_PAL + 1] = wpack(84, 158, 62) // grass
3083 sp[P_PAL + 2] = wpack(122, 86, 56) // dirt
3084 sp[P_PAL + 3] = wpack(126, 126, 132) // stone
3085 sp[P_PAL + 4] = wpack(52, 96, 190) // water
3086 sp[P_PAL + 5] = wpack(206, 190, 132) // sand
3087 sp[P_PAL + 6] = wpack(104, 74, 40) // wood
3088 sp[P_PAL + 7] = wpack(52, 122, 44) // leaves
3089 sp[P_PAL + 8] = wpack(232, 238, 244) // snow
3090 sp[P_PAL + 9] = wpack(72, 74, 80) // coal
3091 sp[P_PAL + 10] = wpack(176, 142, 118) // iron
3092 sp[P_PAL + 11] = wpack(224, 188, 92) // gold
3093 sp[P_MOBC + 0] = wpack(240, 178, 84)
3094 sp[P_MOBC + 1] = wpack(196, 138, 56)
3095 sp[P_MOBC + 2] = wpack(96, 206, 196)
3096 sp[P_MOBC + 3] = wpack(64, 158, 150)
3097 sp[P_MOBC + 4] = wpack(238, 138, 186)
3098 sp[P_MOBC + 5] = wpack(190, 100, 144)
3099 sp[P_BLDG] = 3 // W3: scattered cottages in the green hills
3100 sp[P_BLDGSPEC] = 0 // cottage archetype
3101 return 0
3102}
3103
3104// mobs: 12 creatures spawned on grass, deterministic, through the CERTIFIED entity store
3105// (handle = index+generation, stale-handle-proof by construction -- the part's gate owns that claim)
3106func genmobs(base: i64, sd: i64) -> i64 {
3107 let m: *i64 = mobp(base)
3108 en_init(m, MOB_CAP, MOB_NCOMP)
3109 wc_steer_reset(base)
3110 var i: i64 = 0
3111 var placed: i64 = 0
3112 while i < 200 {
3113 if placed < WC_CAST_N {
3114 let mx: i64 = 4 + whash(i*13 + 5, i*7 + 1, sd + 400) % (WX - 8)
3115 let mz: i64 = 4 + whash(i*17 + 3, i*29 + 9, sd + 500) % (WZ - 8)
3116 let h: i64 = wheight(base, mx, mz, sd)
3117 if h < wsp(base)[P_SNOW] - 1 { if vget(base, mx, h, mz) == 1 {
3118 let hd: i64 = en_spawn(m)
3119 if hd > 0 {
3120 en_set(m, hd, MC_X, mx*256 + 128)
3121 en_set(m, hd, MC_Y, wc_contact_floor_q(base,mx*256+128,mz*256+128,WY-1))
3122 en_set(m, hd, MC_Z, mz*256 + 128)
3123 en_set(m, hd, MC_KIND, whash(mx, mz, sd + 600) % 3)
3124 en_set(m, hd, MC_PH, whash(mz, mx, sd + 700) % WATER_MAGIC_4096)
3125 var dg: i64 = 0
3126 if whash(mx + 13, mz + 7, sd + 800) % 3 == 0 { dg = 1 }
3127 en_set(m, hd, MC_DISG, dg)
3128 let g1: i64 = whash(mx*3 + 1, mz*5 + 9, sd + 900)
3129 let g2: i64 = whash(mz*7 + 3, mx*11 + 5, sd + 901)
3130 // HAIR-HUE NIBBLE (2026-08-30, measured in-page: hist(gv&7)=[12,0,0,0,0,0,0,0]): bits 0-3 are
3131 // the declared hair-hue field (MC_GENE layout, L296) yet g1*4096 + g2*16 can never set them, so
3132 // every girl ever spawned shared ONE hair colour (blonde under the old palette, black under the
3133 // melanin one). Roll the low nibble from its own hash lane; the two existing lanes are untouched.
3134 let g3: i64 = whash(mx*13 + 7, mz*17 + 11, sd + 902) % 16
3135 en_set(m, hd, MC_GENE, (g1*WATER_MAGIC_4096 + g2*16 + g3) % WATER_MAGIC_1048576)
3136 placed = placed + 1
3137 }
3138 } }
3139 }
3140 i = i + 1
3141 }
3142 return placed
3143}
3144
3145// ---------- init / tick / render ----------
3146
3147// A spawn-relative scene fixture, written once into the same voxel buffer the GPU uploads.
3148// Preflight prevents partial stamps and preserves existing non-foliage geometry.
3149func wc_spawn_bush(base: i64, forward: i64, lateral: i64, radius: i64, height: i64, material: i64) -> i64 {
3150 if radius < 1 { return 0 - 1 }
3151 if height < 1 { return 0 - 1 }
3152 if radius >= WX/2 { return 0 - 1 }
3153 if height >= WY { return 0 - 1 }
3154 if material != BEACH_RELEASE_MATERIAL { return 0 - 1 }
3155 let s: *i64 = wst(base)
3156 let sy: i64 = it_sin4096(s[S_YAW])
3157 let co: i64 = it_cos4096(s[S_YAW])
3158 let cx: i64 = (s[S_SPX] >> 8) + (sy*forward + co*lateral)/4096
3159 let cz: i64 = (s[S_SPZ] >> 8) + (co*forward - sy*lateral)/4096
3160 if cx-radius < 0 { return 0 - 1 }
3161 if cx+radius >= WX { return 0 - 1 }
3162 if cz-radius < 0 { return 0 - 1 }
3163 if cz+radius >= WZ { return 0 - 1 }
3164 let raw: i64 = wheight(base, cx+s[S_WOX], cz+s[S_WOZ], s[S_SEED])
3165 let ground: i64 = wsp_beach_ocean(wsp(base), cx+s[S_WOX], cz+s[S_WOZ], raw)
3166 if ground <= wsp(base)[P_WATER] { return 0 - 1 }
3167 if ground+height >= WY { return 0 - 1 }
3168 var phase: i64 = 0
3169 var count: i64 = 0
3170 while phase < 2 {
3171 var y: i64 = 1
3172 while y <= height {
3173 var dz: i64 = 0-radius
3174 while dz <= radius {
3175 var dx: i64 = 0-radius
3176 while dx <= radius {
3177 var ax: i64 = dx; if ax < 0 { ax = 0-ax }
3178 var az: i64 = dz; if az < 0 { az = 0-az }
3179 if ax+az <= radius+height-y {
3180 if phase == 0 {
3181 let old: i64 = vget(base, cx+dx, ground+y, cz+dz)
3182 if old != 0 { if old != material { return 0 - 1 } }
3183 count = count+1
3184 } else {
3185 vset(base, cx+dx, ground+y, cz+dz, material)
3186 }
3187 }
3188 dx = dx+1
3189 }
3190 dz = dz+1
3191 }
3192 y = y+1
3193 }
3194 phase = phase+1
3195 }
3196 wc_wmax(base)
3197 return count
3198}
3199
3200// Validate the effective recipe before mutating the current arena.
3201const WC_INIT_DUNE_INVALID: i64 = 0-79
3202const WC_INIT_DUNE_COMPOSITION: i64 = 0-80
3203func wc_dune_preflight(base: i64, v: i64) -> i64 {
3204 var packed: i64 = 0
3205 if v == BEACH_RELEASE_WORLD { packed = BEACH_DUNE_RECIPE }
3206 // Current wc_spec zeroes both slots and never assigns a nonzero default.
3207 var pipeline: i64 = 0; var erosion: i64 = 0
3208 let gp: *i64 = wgp(base)
3209 if gp[0] == GENP_MAGIC {
3210 var count: i64 = gp[1]
3211 if count < 0 { count = 0 }; if count > GENP_MAXP { count = GENP_MAXP }
3212 var i: i64 = 0
3213 while i < count {
3214 let idx: i64 = gp[2+i*2]; let value: i64 = gp[3+i*2]
3215 if idx == P_DUNE {
3216 if genp_dune_valid(value) == 0 { return WC_INIT_DUNE_INVALID }
3217 packed = value
3218 }
3219 if idx == P_WPIPE { pipeline = value }
3220 if idx == P_ERODE { erosion = value }
3221 i = i + 1
3222 }
3223 }
3224 if genp_dune_valid(packed) == 0 { return WC_INIT_DUNE_INVALID }
3225 if packed != 0 {
3226 if pipeline != 0 || erosion != 0 { return WC_INIT_DUNE_COMPOSITION }
3227 }
3228 return 0
3229}
3230
3231func init_impl_v(base: i64, v: i64, seed: i64) -> i64 {
3232 let dune_rc: i64 = wc_dune_preflight(base,v)
3233 if dune_rc != 0 { return dune_rc }
3234 if v == BEACH_RELEASE_WORLD {
3235 let mv: i64 = wc_move_valid(BEACH_MOVE_MODE,BEACH_MOVE_GRAV_MM,BEACH_MOVE_JUMP_MM,BEACH_MOVE_WALK_MM,BEACH_MOVE_TERM_MM,BEACH_MOVE_STEP_Q8,BEACH_MOVE_REPEAT)
3236 if mv != 0 { return mv }
3237 }
3238 wc_inspection_end(base)
3239 wc_spec(base, v)
3240 // W1c: gen-time recipe overrides -- AFTER identity defaults, BEFORE genworld reads them.
3241 // Allowlist = the gen-time slots only (0..6 terrain shape, 59 treemax, 60 histage, 61
3242 // treespec, 62 settlement density, 63 settlement archetype); style
3243 // slots keep the post-boot poke lane so the two channels never blur. A zero region is a
3244 // clean no-op; the applied count lands in S_GENP below (announce-in-world-truth, sect-30d).
3245 let gp9: *i64 = wgp(base)
3246 let spg9: *i64 = wsp(base)
3247 var gpn9: i64 = 0
3248 if gp9[0] == GENP_MAGIC {
3249 var gk9: i64 = gp9[1]
3250 if gk9 < 0 { gk9 = 0 }
3251 if gk9 > GENP_MAXP { gk9 = GENP_MAXP }
3252 var gi9: i64 = 0
3253 while gi9 < gk9 {
3254 let gx9: i64 = gp9[2 + gi9*2]
3255 // THE ONE ALLOWLIST (nx_genp_allow_lib, 2026-09-04): this chain was hand-enumerated here and mirrored,
3256 // with drift, in nx_world_snap and nx_game_page_emit; all three now read genp_gen_ok, and T90 pins each
3257 // P_* slot to it. Gen-time because these rows shape what genworld is about to read -- poked after boot
3258 // they would be the silent no-op this whitelist exists for.
3259 let gok9: i64 = genp_dune_gen_ok(gx9)
3260 if gok9 == 1 { spg9[gx9] = gp9[3 + gi9*2]; gpn9 = gpn9 + 1 }
3261 gi9 = gi9 + 1
3262 }
3263 }
3264 let s: *i64 = wst(base)
3265 let facing_radius: i64 = s[S_FACE_RADIUS]
3266 let facing_gain: i64 = s[S_FACE_GAIN]
3267 var k: i64 = 0
3268 while k < 64 { s[k] = 0; k = k + 1 }
3269 wc_set_facing_policy(base,facing_radius,facing_gain)
3270 let ej0: *i64 = ejp(base)
3271 var kj: i64 = 0
3272 while kj < EJRN_SLOTS { ej0[kj] = 0; kj = kj + 1 }
3273 s[S_SEED] = seed
3274 s[S_VAR] = v // after the zero loop -- an earlier set
3275 // here was wiped (the zero-loop-order class)
3276 s[S_GENP] = gpn9 // same zero-loop-order law as S_VAR above
3277 s[S_T] = wsp(base)[P_DAYLEN]/4 // first frame = MID-MORNING light (the
3278 // visitor's first impression; a dawn-dark
3279 // spawn read as night, screenshot-caught)
3280 genworld(base, seed)
3281 wc_wmax(base)
3282 genmobs(base, seed)
3283 // A4/C1: zero the spring + need regions (init may reuse a dirty arena), then seat and seed
3284 let sb0i: *i64 = (base + O_SOFT) as *i64
3285 var kzi: i64 = 0
3286 while kzi < MOB_CAP*SB_PTS*SD_STRIDE { sb0i[kzi] = 0; kzi = kzi + 1 }
3287 let nd0i: *i64 = (base + O_NEED) as *i64
3288 var kni: i64 = 0
3289 while kni < MOB_CAP*4 { nd0i[kni] = 0; kni = kni + 1 }
3290 wc_soft_seat_all(base)
3291 wc_need_seed(base)
3292 wc_mind_zero(base)
3293 // spawn on the TALLEST central column: from a local peak every direction is a vista,
3294 // never a cliff wall filling the first frame (the eyeball found exactly that)
3295 var sx: i64 = WX/2
3296 var sz: i64 = WZ/2
3297 var sh: i64 = 0
3298 var qz: i64 = WZ/2 - 12
3299 while qz < WZ/2 + 12 {
3300 var qx: i64 = WX/2 - 12
3301 while qx < WX/2 + 12 {
3302 let qh: i64 = wheight(base, qx, qz, seed)
3303 if qh > sh { sh = qh; sx = qx; sz = qz }
3304 qx = qx + 1
3305 }
3306 qz = qz + 1
3307 }
3308 // W2b: a spawn column may carry understory at feet height -- clear it. GUARDED so legacy
3309 // (understory-less) worlds stay byte-identical; gen-time vegetation is not journaled, so a
3310 // later window-slide repaint is harmless (the player has long moved).
3311 if ((wsp(base)[P_TREESPEC] >> 18) & 7) > 0 {
3312 vset(base, sx, sh + 1, sz, 0)
3313 vset(base, sx, sh + 2, sz, 0)
3314 }
3315 s[S_CX] = sx*256 + 128
3316 s[S_CZ] = sz*256 + 128
3317 s[S_CY] = (sh+1)*256 + EYE
3318 // Establish support from the same continuous field before querying the bounded floor.
3319 if wsp(base)[P_SURF] == 1 {
3320 let spawn_floor: i64 = wc_surface_source(base, s[S_CX], s[S_CZ])
3321 if spawn_floor >= 0 { s[S_CY] = spawn_floor + EYE }
3322 }
3323 let spawn_stand: i64 = wstand_here(base,s[S_CX],s[S_CZ])
3324 if spawn_stand >= 0 { s[S_CY] = spawn_stand }
3325 s[S_SPX] = s[S_CX]
3326 s[S_SPY] = s[S_CY]
3327 s[S_SPZ] = s[S_CZ]
3328 s[S_YAW] = 0
3329 // GE45 review framing (2026-09-04): an identity with an ocean row spawns FACING the sea. The shore identity's
3330 // +z edge is yaw 0 already, so this is byte-identical there and correct for any other edge a recipe declares.
3331 let sy0: i64 = wsp_sea_yaw(wsp(base))
3332 if sy0 >= 0 { s[S_YAW] = sy0 }
3333 s[S_PITCH] = 0
3334 s[S_Q] = Q_START
3335 if v == BEACH_RELEASE_WORLD {
3336 let release_cells: i64 = wc_spawn_bush(base, BEACH_RELEASE_FORWARD, BEACH_RELEASE_LATERAL, BEACH_RELEASE_RADIUS, BEACH_RELEASE_HEIGHT, BEACH_RELEASE_MATERIAL)
3337 // The release fixture is non-critical: a crowded or submerged site must not
3338 // prevent the world's input bindings and remaining startup from completing.
3339 // The fixture uses the world's foliage material; it must not recolor every leaf.
3340
3341 }
3342 ia_init(winp(base))
3343 ia_bind(winp(base), 82, IA_LUP) // R = look up (real action bits since seq1136)
3344 ia_bind(winp(base), 70, IA_LDOWN) // F = look down (G = party invite, below)
3345 ia_bind(winp(base), 32, IA_JUMP) // space = jump (touch center zone jumps too)
3346 ia_bind(winp(base), 81, IA_MOD) // Q = cycle the hotbar selection
3347 ia_bind(winp(base), 72, IA_HOME) // H = return to spawn (edge-fall rescue)
3348 return wc_move_boot(base,v)
3349}
3350func init_impl(base: i64, seed: i64) -> i64 { return init_impl_v(base, 0, seed) }
3351
3352// return to spawn: position + velocity reset, world untouched (the operator's reset button)
3353func wc_respawn(base: i64) -> i64 {
3354 let s: *i64 = wst(base)
3355 s[S_CX] = s[S_SPX]
3356 s[S_CY] = s[S_SPY]
3357 s[S_CZ] = s[S_SPZ]
3358 s[S_VY] = 0
3359 wc_move_reset(base)
3360 s[S_GROUNDED] = 1
3361 return 0
3362}
3363
3364// highest solid y at a column (collision floor), -1 if none
3365func wground(base: i64, x: i64, z: i64) -> i64 {
3366 var y: i64 = WY - 1
3367 while y >= 0 {
3368 if vsolid(base, x, y, z) == 1 { return y }
3369 y = y - 1
3370 }
3371 return 0 - 1
3372}
3373
3374// Collision uses the same continuous terrain field as rendering; non-terrain solids remain cells.
3375func wc_contact_floor_below_q(base: i64, xq: i64, zq: i64, top: i64, ceiling: i64) -> i64 {
3376 let sp: *i64 = wsp(base)
3377 var y: i64 = top
3378 if y >= WY { y = WY - 1 }
3379 var floor: i64 = 0 - 1
3380 if sp[P_SURF] == 1 {
3381 floor = wc_surface_source(base,xq,zq)
3382 if floor < 0 { return 0 - 1 }
3383 if floor > ceiling { floor = 0 - 1 }
3384 }
3385 while y >= 0 {
3386 let b: i64 = vget(base,xq >> 8,y,zq >> 8)
3387 var eligible: i64 = vsolid(base,xq >> 8,y,zq >> 8)
3388 if sp[P_SURF] == 1 { if wterrain(b) == 1 { eligible = 0 } }
3389 if eligible == 1 {
3390 let celltop: i64 = (y+1)*256
3391 if celltop <= ceiling {
3392 if celltop > floor { floor = celltop }
3393 return floor
3394 }
3395 }
3396 y = y - 1
3397 }
3398 return floor
3399}
3400
3401// Unbounded column query remains available to existing callers.
3402func wc_contact_floor_q(base: i64, xq: i64, zq: i64, top: i64) -> i64 {
3403 return wc_contact_floor_below_q(base,xq,zq,top,WY*256)
3404}
3405
3406// try to stand at (nxq, nzq): returns the required cam y (Q8) or -1 if the move is blocked
3407func wstand(base: i64, nxq: i64, nzq: i64, cyq: i64) -> i64 {
3408 // Preserve the existing step allowance; overhead surfaces outside it cannot be support.
3409 let step_allowance: i64 = 384
3410 let floor: i64 = wc_contact_floor_below_q(base, nxq, nzq, WY - 1, cyq - EYE + step_allowance)
3411 var stand: i64 = floor + EYE
3412 let wl2: i64 = wsp(base)[P_WATER]
3413 let wsurf: i64 = (wl2 + 1)*256 + EYE - 96 // float slightly sunk in water
3414 if stand < wsurf { if vget(base, nxq >> 8, wl2, nzq >> 8) == 4 { stand = wsurf } }
3415 // VOID COLUMN = WALL: no ground below means off the world's edge -- refuse the step (the
3416 // walk-off-and-vanish bug: wstand happily computed a hover height over nothing)
3417 if floor < 0 { return 0 - 1 }
3418 if stand - cyq > step_allowance { return 0 - 1 } // more than a 1.5-block rise = wall
3419 let heady: i64 = (stand + 128) >> 8
3420 if vsolid(base, nxq >> 8, heady, nzq >> 8) == 1 { return 0 - 1 }
3421 return stand
3422}
3423
3424// stand height for the CURRENT column (no wall refusal -- the vertical-physics target)
3425func wstand_here(base: i64, xq: i64, zq: i64) -> i64 {
3426 let floor: i64 = wc_contact_floor_below_q(base, xq, zq, WY - 1, wst(base)[S_CY] - EYE)
3427 if floor < 0 { return 0 - 1 }
3428 var stand: i64 = floor + EYE
3429 let wl3: i64 = wsp(base)[P_WATER]
3430 let wsurf: i64 = (wl3 + 1)*256 + EYE - 96
3431 if stand < wsurf { if vget(base, xq >> 8, wl3, zq >> 8) == 4 { stand = wsurf } }
3432 return stand
3433}
3434
3435// mob floor: first solid scanning DOWN from just above the mob's feet -- wground scans from the
3436// SKY and reads an overhanging leaf canopy as "ground 5 blocks up", which froze every mob under a
3437// tree (measured: phase advanced 982/tick = the refusal branch, position never moved).
3438// body clearance: a girl is ~2 blocks tall -- the two cells above the ground must be AIR or
3439// she stands with her head/shoulders inside an overhang (invisible as SDF; as a MESH her
3440// buried parts discard and the poking limbs read as floating shards -- operator-reported).
3441// GE12b -- BODY CLEARANCE. mob_fits (below) proves only her OWN cell's air column, but a girl
3442// is a CYLINDER of radius MOB_R_Q8, so standing near a cell edge her body overlaps the
3443// neighbouring solid: the operator's 2026-08-27 frame caught exactly that -- a girl leaning
3444// THROUGH a cliff wall with her shins inside the blocks. These clamp her fractional position
3445// away from any solid neighbour at either body course. Integer, deterministic, and a pure
3446// no-op in open ground (no solid neighbour => the position is returned untouched).
3447// RADIUS IS NOT A NEW NUMBER: npc_pick already models her body as r=.35 blocks for hit
3448// detection, and .35*256 = 89.6 -> 90 q8. One body, one radius; a second value here would be
3449// two rulers for one girl (she could be hittable where she is not present, and vice versa).
3450const MOB_R_Q8: i64 = 90
3451func mob_solid2(base: i64, x: i64, g: i64, z: i64) -> i64 {
3452 if vsolid(base, x, g + 1, z) == 1 { return 1 }
3453 if vsolid(base, x, g + 2, z) == 1 { return 1 }
3454 return 0
3455}
3456func mob_clear_x(base: i64, nx: i64, nz: i64, g: i64) -> i64 {
3457 let cx: i64 = nx >> 8
3458 let cz: i64 = nz >> 8
3459 let fr: i64 = nx - cx*256
3460 var r: i64 = nx
3461 if fr < MOB_R_Q8 { if mob_solid2(base, cx - 1, g, cz) == 1 { r = cx*256 + MOB_R_Q8 } }
3462 if fr > 256 - MOB_R_Q8 { if mob_solid2(base, cx + 1, g, cz) == 1 { r = cx*256 + 256 - MOB_R_Q8 } }
3463 return r
3464}
3465func mob_clear_z(base: i64, nx: i64, nz: i64, g: i64) -> i64 {
3466 let cx: i64 = nx >> 8
3467 let cz: i64 = nz >> 8
3468 let fr: i64 = nz - cz*256
3469 var r: i64 = nz
3470 if fr < MOB_R_Q8 { if mob_solid2(base, cx, g, cz - 1) == 1 { r = cz*256 + MOB_R_Q8 } }
3471 if fr > 256 - MOB_R_Q8 { if mob_solid2(base, cx, g, cz + 1) == 1 { r = cz*256 + 256 - MOB_R_Q8 } }
3472 return r
3473}
3474func mob_fits(base: i64, x: i64, g: i64, z: i64) -> i64 {
3475 if vsolid(base, x, g + 1, z) == 1 { return 0 }
3476 if vsolid(base, x, g + 2, z) == 1 { return 0 }
3477 return 1
3478}
3479
3480func mob_ground(base: i64, x: i64, ytop: i64, z: i64) -> i64 {
3481 var y: i64 = ytop
3482 if y > WY - 1 { y = WY - 1 }
3483 while y >= 0 {
3484 if vsolid(base, x, y, z) == 1 { return y }
3485 y = y - 1
3486 }
3487 return 0 - 1
3488}
3489
3490// GE12 -- GAIT-PHASED VERTICAL LOCOMOTION (gameengine rung mob_step_gait). A mob's Y no longer
3491// EASES linearly toward the stand height (the 64 q8/tick glide the operator caught 2026-08-26:
3492// a climb glided and a drop floated). Two named regimes, one ruler each:
3493// STEP UP -> the cited stair-ascent rate WC_STEP_EASE_UP (gamefeel row step_ease_up_q8), and
3494// only in a tick where a stride step landed -- the caller is the move branch, so the
3495// rise is gait-phased BY CONSTRUCTION, never a free-running ramp;
3496// DROP -> the PLAYER'S OWN integrator (v -= GRAV per tick, clamped at VTERM, landing snaps to
3497// the stand height and zeroes v): every falling body in the world obeys one gravity.
3498// vyp[k] is her vertical velocity (O_MOBVY). Returns the new Y (q8). All inputs are arena state,
3499// so replay stays bit-identical (T4/T17). ph/gait are untouched: this is Y only.
3500func mob_step_gait(vyp: *i64, k: i64, cy0: i64, ng: i64) -> i64 {
3501 var vy9: i64 = vyp[k]
3502 var ny9: i64 = cy0
3503 if ng > cy0 {
3504 ny9 = cy0 + WC_STEP_EASE_UP
3505 if ny9 > ng { ny9 = ng }
3506 vy9 = 0
3507 }
3508 if ng < cy0 {
3509 vy9 = vy9 - GRAV
3510 if vy9 < 0 - VTERM { vy9 = 0 - VTERM }
3511 ny9 = cy0 + vy9
3512 if ny9 <= ng { ny9 = ng; vy9 = 0 }
3513 }
3514 if ng == cy0 { vy9 = 0 }
3515 vyp[k] = vy9
3516 return ny9
3517}
3518
3519// NPC HIT DETECTION: march the view ray in half-block samples up to 7 blocks; a solid voxel
3520// blocks it (walls occlude, real detection); a sample inside a girl's body cylinder (r=.35,
3521// h=1.7) targets her. Returns the dense index or -1. The same pass PUSHES the player out of
3522// bodies (they have PRESENCE now -- no more walking through people).
3523func npc_pick(base: i64, fx: i64, fy: i64, fz: i64) -> i64 {
3524 let s: *i64 = wst(base)
3525 let m: *i64 = mobp(base)
3526 var t: i64 = 1
3527 while t <= 14 {
3528 let px2: i64 = s[S_CX] + fx*t*128/WATER_MAGIC_4096
3529 let py2: i64 = s[S_CY] + fy*t*128/WATER_MAGIC_4096
3530 let pz2: i64 = s[S_CZ] + fz*t*128/WATER_MAGIC_4096
3531 if vsolid(base, px2 >> 8, py2 >> 8, pz2 >> 8) == 1 { return 0 - 1 }
3532 var k: i64 = 0
3533 while k < en_count(m) {
3534 let h: i64 = en_nth(m, k)
3535 var ddx: i64 = px2 - en_get(m, h, MC_X)
3536 if ddx < 0 { ddx = 0 - ddx }
3537 var ddz: i64 = pz2 - en_get(m, h, MC_Z)
3538 if ddz < 0 { ddz = 0 - ddz }
3539 let dy2: i64 = py2 - en_get(m, h, MC_Y)
3540 if ddx < 90 { if ddz < 90 { if dy2 >= 0 { if dy2 < 440 { return k } } } }
3541 k = k + 1
3542 }
3543 t = t + 1
3544 }
3545 return 0 - 1
3546}
3547
3548// ---------- A1: the integer tick calendar (day/night + 4 seasons) ----------
3549// Pure functions of (S_T, spec) -> bit-exact across arenas by construction. Every phase compare
3550// CROSS-MULTIPLIES (the banked integer-truncation law: never divide a value before comparing it).
3551// RECOMBINATION CONTRACT: a different game = a different spec; a recombined identity turns a
3552// subsystem OFF by zeroing its rows, so every consumer guards the zero (endless day, no rain,
3553// no farm clock) instead of trapping on a modulo-by-zero. Off is a state, never a crash.
3554func cal_dayt(base: i64) -> i64 {
3555 let dl: i64 = wsp(base)[P_DAYLEN]
3556 if dl <= 0 { return 0 }
3557 let s: *i64 = wst(base)
3558 return s[S_T] % dl
3559}
3560func cal_day(base: i64) -> i64 {
3561 let dl: i64 = wsp(base)[P_DAYLEN]
3562 if dl <= 0 { return 0 }
3563 let s: *i64 = wst(base)
3564 return s[S_T] / dl
3565}
3566func cal_season(base: i64) -> i64 {
3567 let sd: i64 = wsp(base)[P_SEASD]
3568 if sd <= 0 { return 0 }
3569 return (cal_day(base) / sd) % 4
3570}
3571// night = the last 3/8 of each day: dayt*8 >= DAYLEN*5, cross-multiplied
3572func cal_night(base: i64) -> i64 {
3573 if wsp(base)[P_DAYLEN] <= 0 { return 0 }
3574 if cal_dayt(base)*8 >= wsp(base)[P_DAYLEN]*WC_NIGHT_X8 { return 1 }
3575 return 0
3576}
3577// sun elevation as a Q12 sine: rises 0 at dawn, +4096 at noon, negative through the night
3578func cal_sunel(base: i64) -> i64 {
3579 let dl: i64 = wsp(base)[P_DAYLEN]
3580 if dl <= 0 { return 4096 } // calendar off = eternal noon
3581 let dt2: i64 = cal_dayt(base)
3582 if dt2*8 < dl*WC_NIGHT_X8 { return it_sin4096(dt2*8*IT_PI/(dl*WC_NIGHT_X8)) }
3583 return 0 - it_sin4096((dt2*8 - dl*WC_NIGHT_X8)*IT_PI/(dl*(8 - WC_NIGHT_X8)))
3584}
3585// deterministic weather: ONE roll per absolute day-eighth from (seed, segment, season table) --
3586// the same world replays the same storms forever
3587func cal_rain(base: i64) -> i64 {
3588 let dl: i64 = wsp(base)[P_DAYLEN]
3589 if dl <= 0 { return 0 }
3590 let s: *i64 = wst(base)
3591 let seg: i64 = s[S_T]*8/dl
3592 // DELEGATED (2026-08-26) to nx_weather_lib's gate-proven wx_rain: same seed lineage (the
3593 // +3300 salt fold), same one-roll-per-absolute-day-eighth semantics (seg IS the eighth), and
3594 // the gate PROVES the long-run rate stays inside each P_RAINS row's binomial envelope -- this
3595 // delegation cannot change any world's climate, and it gains the front persistence, typed
3596 // precipitation and gradient wind the old coin structurally lacked.
3597 return wx_rain(s[S_SEED] + 3300, seg, wsp(base)[P_RAINS + cal_season(base)])
3598}
3599
3600// the per-tick weather export writer: recomputes the eighth's state (pure fns -- cheap, replay
3601// exact) and publishes it in the spec block for the page and every other consumer.
3602func wc_weather_tick(base: i64) -> i64 {
3603 let dl: i64 = wsp(base)[P_DAYLEN]
3604 if dl <= 0 { return 0 }
3605 let s: *i64 = wst(base)
3606 let seg: i64 = s[S_T]*8/dl
3607 let sd: i64 = s[S_SEED] + 3300
3608 let row: i64 = wsp(base)[P_RAINS + cal_season(base)]
3609 let sp: *i64 = wsp(base)
3610 sp[P_WXCOV] = wx_coverage(sd, seg, row)
3611 let ws: i64 = wx_wind_speed(sd, seg)
3612 let wd: i64 = wx_wind_dir_q(sd, seg)
3613 // direction Q -> components via the sim's own it_cos4096/it_sin4096 (angle = wd scaled to tau)
3614 sp[P_WXWINDX] = ws*it_cos4096(wd*IT_PI*2/WX_Q)/4096
3615 sp[P_WXWINDZ] = ws*it_sin4096(wd*IT_PI*2/WX_Q)/4096
3616 sp[P_WXTYPE] = wx_precip(sd, seg, row, cal_season(base), cal_night(base))
3617 sp[P_WXTEMP] = wx_temp(cal_season(base), cal_night(base), sd, seg)
3618 return 0
3619}
3620// write bst into MC_DISG bits 3-5 WITHOUT touching the disguise/met bits (0-2) or the
3621// FRIENDSHIP byte (bits 6-13) -- the old (dg & 7) mask silently erased any upper state
3622func dg_bst(dg: i64, b: i64) -> i64 { return dg - ((dg >> 3) & 7)*8 + b*8 }
3623
3624// ---------- B1/B2: soil + crops. THE EDIT JOURNAL IS THE SPARSE ACTIVE LIST ----------
3625// Every farm cell was player-touched, so it is already journaled at absolute coords: no scan,
3626// no extra memory, and growth writes go through the same journal so world and history can
3627// never disagree. Growth is Minecraft's integer stencil: p = 1/(floor(25/speed)+1), speed
3628// from soil wetness; rolls are seed+coord+epoch hashes -> replay-safe by construction.
3629func wc_grow_set_indexed(base: i64, index: *i64, lx: i64, ly: i64, lz: i64, b: i64) -> i64 {
3630 let s: *i64 = wst(base)
3631 if ej_index_put(base,index, lx + s[S_WOX], ly, lz + s[S_WOZ], b) < 0 {
3632 s[S_EJDROP] = s[S_EJDROP] + 1
3633 return 0
3634 }
3635 return vset(base, lx, ly, lz, b)
3636}
3637func wc_grow_set(base: i64, lx: i64, ly: i64, lz: i64, b: i64) -> i64 {
3638 let index: *i64 = ej_batch_begin(base,CRAFT_EXT_TOTAL)
3639 if (index as i64) == 0 { return 0 }
3640 return wc_grow_set_indexed(base,index,lx,ly,lz,b)
3641}
3642// water within the 5x5 ring at soil level or one below (ponds sit lower than their fields)
3643func wc_wet_near(base: i64, lx: i64, ly: i64, lz: i64) -> i64 {
3644 var dz2: i64 = 0 - 2
3645 while dz2 <= 2 {
3646 var dx2: i64 = 0 - 2
3647 while dx2 <= 2 {
3648 if vget(base, lx + dx2, ly, lz + dz2) == 4 { return 1 }
3649 if vget(base, lx + dx2, ly - 1, lz + dz2) == 4 { return 1 }
3650 dx2 = dx2 + 1
3651 }
3652 dz2 = dz2 + 1
3653 }
3654 return 0
3655}
3656func wc_farm_tick(base: i64) -> i64 {
3657 let s: *i64 = wst(base)
3658 let e: *i64 = ejp(base)
3659 let sp: *i64 = wsp(base)
3660 let index: *i64 = ej_batch_begin(base,CRAFT_EXT_TOTAL)
3661 if (index as i64) == 0 { return 0-3 }
3662 let rain: i64 = cal_rain(base)
3663 var i: i64 = 0
3664 while i < EJRN_SLOTS {
3665 let cur: i64 = e[i]
3666 if cur != 0 {
3667 let b: i64 = cur & 255
3668 if b >= B_FARM { if b <= B_FARMW {
3669 let k: i64 = (cur >> 8) - 1
3670 let ay: i64 = k & 63
3671 let az: i64 = ((k >> 6) & WATER_MAGIC_33554431) - WATER_MAGIC_16777216
3672 let ax: i64 = ((k >> 31) & WATER_MAGIC_33554431) - WATER_MAGIC_16777216
3673 let lx: i64 = ax - s[S_WOX]
3674 let lz: i64 = az - s[S_WOZ]
3675 // only cells inside the window tick (offscreen farms pause -- chunk law, honest)
3676 if vin(lx, ay, lz) == 1 { if vget(base, lx, ay, lz) == b {
3677 if b == B_FARM { if rain == 1 { wc_grow_set_indexed(base,index, lx, ay, lz, B_FARMW) } }
3678 if b == B_FARM { if rain == 0 { if wc_wet_near(base, lx, ay, lz) == 1 {
3679 wc_grow_set_indexed(base,index, lx, ay, lz, B_FARMW)
3680 } } }
3681 if b == B_FARMW { if rain == 0 { if wc_wet_near(base, lx, ay, lz) == 0 {
3682 wc_grow_set_indexed(base,index, lx, ay, lz, B_FARM)
3683 } } }
3684 if b >= B_CROP0 { if b <= B_CROP1 {
3685 var spd: i64 = sp[P_GROWD]
3686 let un: i64 = vget(base, lx, ay - 1, lz)
3687 if un == B_FARMW { spd = sp[P_GROWW] }
3688 if rain == 1 { if un == B_FARM { spd = sp[P_GROWW] } }
3689 if spd < 1 { spd = 1 }
3690 let den: i64 = 25/spd + 1
3691 if whash(ax*131 + ay, az*97, s[S_SEED] + s[S_T]/sp[P_GROWE]) % den == 0 {
3692 wc_grow_set_indexed(base,index, lx, ay, lz, b + 1)
3693 if b + 1 == B_CROP2 { s[S_GROWN] = s[S_GROWN] + 1 }
3694 }
3695 } }
3696 } }
3697 } }
3698 }
3699 i = i + 1
3700 }
3701 return 0
3702}
3703
3704// ---------- A4/C1 shared state maintenance (springs + needs) ----------
3705// GAIT DRIVE IS STILL A FREE-RUNNING CLOCK (debt 1785770635 STAYS OPEN) -- and the reason is now
3706// MEASURED rather than guessed, because the obvious fix was tried on 2026-08-25 and REFUTED by
3707// nx_wasm_craft_gate T36 before it could ship. Advancing MC_PH by distance travelled makes the
3708// tissue drive track her real cadence, which is correct and is what the oracle band wants. It is
3709// ALSO CIRCULAR: line ~1849 derives her wander heading FROM ph (dx = sin(ph*3), dz = cos(ph*3)),
3710// so a girl who stops moving freezes her phase, and a frozen phase freezes her heading -- she can
3711// never restart. The gate caught it exactly: the bold girl's approach distance stayed at her START
3712// value (1024, was 577) and the run went 52/53 RED. THE REAL FIX IS TO SPLIT THE TWO PHASES: a
3713// wander phase that must free-run for behaviour, and a gait phase that must track travel for
3714// tissue. That needs one word of per-mob state (accumulated stride distance), which touches the
3715// entity schema and therefore the save-format teeth (T15, T18, T50) -- a deliberate change, not a
3716// drive-by. Reverted here so the tree stays GREEN; the diagnosis is the deliverable.
3717func wc_soft_seat_all(base: i64) -> i64 {
3718 let m: *i64 = mobp(base)
3719 let sb0: *i64 = (base + O_SOFT) as *i64
3720 var k: i64 = 0
3721 while k < en_count(m) {
3722 let h: i64 = en_nth(m, k)
3723 if h > 0 { sb_seat_mob(sb0, k, en_get(m, h, MC_X)/4, en_get(m, h, MC_Y)/4, en_get(m, h, MC_Z)/4) }
3724 k = k + 1
3725 }
3726 return 0
3727}
3728// ---------- THE TRAVELER'S PASSPORT (isekai): character + party cross worlds ----------
3729func passp(base: i64) -> *i64 { return (base + O_PASS) as *i64 }
3730func wc_pass_ck(p: *i64) -> i64 {
3731 return gsc_fold_words(SAVE_CK_SEED, p, 0, 21, SAVE_CK_PRIME, SAVE_CK_MASK)
3732}
3733func wc_party_count(base: i64) -> i64 {
3734 let m: *i64 = mobp(base)
3735 var n: i64 = 0
3736 var k: i64 = 0
3737 while k < en_count(m) {
3738 let h: i64 = en_nth(m, k)
3739 if h > 0 { if (en_get(m, h, MC_DISG) & 4) == 4 { n = n + 1 } }
3740 k = k + 1
3741 }
3742 return n
3743}
3744// fold the session's deltas into the lifetime ledger (snapshot words 32+ make this idempotent:
3745// a world reload or a second build never double-counts a deed)
3746func wc_pass_sync(base: i64) -> i64 {
3747 let p: *i64 = passp(base)
3748 let s: *i64 = wst(base)
3749 if p[0] != PASS_MAGIC {
3750 var z: i64 = 0
3751 while z < 48 { p[z] = 0; z = z + 1 }
3752 p[0] = PASS_MAGIC
3753 p[1] = 1
3754 p[2] = whash(s[S_SEED], s[S_T], 20260802)
3755 // snapshot stays ZERO on creation: a traveler born mid-world ABSORBS what he has
3756 // already done here as his first deeds. Baselining at current values silently
3757 // discarded them (gate T50 caught exactly that: 7 harvests -> ledger 0).
3758 }
3759 var d: i64 = s[S_BROKEN] - p[32]
3760 if d > 0 { p[3] = p[3] + d }
3761 p[32] = s[S_BROKEN]
3762 d = s[S_PLACED] - p[33]
3763 if d > 0 { p[4] = p[4] + d }
3764 p[33] = s[S_PLACED]
3765 d = s[S_HARV] - p[34]
3766 if d > 0 { p[5] = p[5] + d }
3767 p[34] = s[S_HARV]
3768 d = s[S_PLANT] - p[35]
3769 if d > 0 { p[6] = p[6] + d }
3770 p[35] = s[S_PLANT]
3771 d = s[S_FOUND] - p[36]
3772 if d > 0 { p[7] = p[7] + d }
3773 p[36] = s[S_FOUND]
3774 d = s[S_LOST] - p[37]
3775 if d > 0 { p[8] = p[8] + d }
3776 p[37] = s[S_LOST]
3777 d = s[S_T] - p[38]
3778 if d > 0 { p[9] = p[9] + d }
3779 p[38] = s[S_T]
3780 p[10] = p[10] | (1 << s[S_VAR])
3781 // the party rides along: up to 3 companions' (kind, bond-word, genome)
3782 let m: *i64 = mobp(base)
3783 var pc: i64 = 0
3784 var k: i64 = 0
3785 while k < en_count(m) {
3786 let h: i64 = en_nth(m, k)
3787 if h > 0 { if (en_get(m, h, MC_DISG) & 4) == 4 { if pc < 3 {
3788 p[12 + pc*3] = en_get(m, h, MC_KIND)
3789 p[13 + pc*3] = en_get(m, h, MC_DISG)
3790 p[14 + pc*3] = en_get(m, h, MC_GENE)
3791 pc = pc + 1
3792 } } }
3793 k = k + 1
3794 }
3795 p[11] = pc
3796 p[21] = wc_pass_ck(p)
3797 return PASS_LEN
3798}
3799// arriving traveler: validate, re-baseline the snapshot, and WALK THE PARTY OFF THE BOAT --
3800// each companion respawns beside the player with bond and genome intact. Refusal touches
3801// nothing: -1 magic, -2 version, -3 checksum (the save doors' law).
3802func wc_pass_apply(base: i64) -> i64 {
3803 let p: *i64 = passp(base)
3804 if p[0] != PASS_MAGIC { return 0 - 1 }
3805 if p[1] != 1 { return 0 - 2 }
3806 if wc_pass_ck(p) != p[21] { return 0 - 3 }
3807 let s: *i64 = wst(base)
3808 p[32] = s[S_BROKEN]
3809 p[33] = s[S_PLACED]
3810 p[34] = s[S_HARV]
3811 p[35] = s[S_PLANT]
3812 p[36] = s[S_FOUND]
3813 p[37] = s[S_LOST]
3814 p[38] = s[S_T]
3815 p[10] = p[10] | (1 << s[S_VAR])
3816 let m: *i64 = mobp(base)
3817 var pc: i64 = 0
3818 while pc < p[11] { if pc < 3 {
3819 let hn: i64 = en_spawn(m)
3820 if hn > 0 {
3821 let ang: i64 = pc*2*IT_PI/3 + IT_PI/6
3822 var ax: i64 = s[S_CX] + it_sin4096(ang)*3*256/WATER_MAGIC_4096
3823 var az: i64 = s[S_CZ] + it_cos4096(ang)*3*256/WATER_MAGIC_4096
3824 ax = wclamp(ax, 2*256, (WX - 2)*256)
3825 az = wclamp(az, 2*256, (WZ - 2)*256)
3826 let gy: i64 = mob_ground(base, ax >> 8, WY - 1, az >> 8)
3827 en_set(m, hn, MC_X, ax)
3828 en_set(m, hn, MC_Z, az)
3829 en_set(m, hn, MC_Y, wc_contact_floor_q(base,ax,az,WY-1))
3830 en_set(m, hn, MC_KIND, p[12 + pc*3])
3831 en_set(m, hn, MC_DISG, p[13 + pc*3])
3832 en_set(m, hn, MC_GENE, p[14 + pc*3])
3833 en_set(m, hn, MC_PH, whash(p[14 + pc*3], pc, s[S_SEED]) % WATER_MAGIC_4096)
3834 }
3835 pc = pc + 1
3836 } }
3837 wc_soft_seat_all(base)
3838 wc_need_seed(base)
3839 p[21] = wc_pass_ck(p)
3840 return 0
3841}
3842
3843// level + party-invite as BASE-PARAMETERIZED cores (the doors below are thin wrappers, so the
3844// gate exercises the same code the game runs -- one definition, zero drift)
3845func pass_level_of(base: i64) -> i64 {
3846 let p: *i64 = passp(base)
3847 if p[0] != PASS_MAGIC { return 1 }
3848 var sc: i64 = p[5]*4 + p[6] + p[7]*20 + p[3]/8 + p[4]/8 + p[9]/72000*3
3849 var lv: i64 = 1
3850 while sc >= lv*10 { sc = sc - lv*10; lv = lv + 1 }
3851 return lv
3852}
3853func party_of(base: i64) -> i64 {
3854 let s: *i64 = wst(base)
3855 if s[S_NPCT] < 0 { return 0 - 1 }
3856 let m: *i64 = mobp(base)
3857 let h: i64 = en_nth(m, s[S_NPCT])
3858 if h <= 0 { return 0 - 1 }
3859 let dg: i64 = en_get(m, h, MC_DISG)
3860 if ((dg >> 6) & 255) < 40 { return 0 - 2 } // a bond, not a kidnapping
3861 if (dg & 4) == 4 {
3862 en_set(m, h, MC_DISG, dg - 4) // toggle: she may also LEAVE
3863 return 0
3864 }
3865 if wc_party_count(base) >= 3 { return 0 - 3 }
3866 en_set(m, h, MC_DISG, dg | 4)
3867 th_add(base, s[S_NPCT], TH_MET, 20, 14400)
3868 return 1
3869}
3870
3871// outfit for girl k: identity data gates the dimension; her genome picks within it.
3872// 0 clothed · 1 topless · 2 nude (all figures are the one adult mesh)
3873// THE POSE DOOR (operator 2026-08-02: "interact with me and the environment in various poses
3874// beyond the one" -- the pose PIPELINE's first rung). The engine ALREADY computes behavioral
3875// states (T44: sleep/flee via MC_DISG bits 3-5; movement via O_MOBAUX deltas) and the renderer
3876// ignores every one of them: sleeping girls play the walk clip, idle girls walk in place. Same
3877// class as the face: THE DATA EXISTS AND DRIVES NOTHING -- so the consumer contract comes first.
3878// Pose ids (the page consumes; the coming NXA pose BANK indexes by these):
3879// 0 idle-stand (breath only) 1 walk 2 sleep-lying 3 flee-run 4 sit 5 lie-awake
3880// 4/5 are RESERVED rows: behaviors from the towel/smart-object chain select them; publishing
3881// the id space now means new poses arrive as DATA against a stable contract, not an engine edit.
3882func mob_pose(base: i64, k: i64) -> i64 {
3883 let m: *i64 = mobp(base)
3884 let st: i64 = (en_get(m, en_nth(m, k), MC_DISG) >> 3) & 7
3885 if st == 2 { return 2 }
3886 if st == 4 { return 3 }
3887 if mob_spd_q8i(base, k) > 0 { return 1 }
3888 return 0
3889}
3890func mobpose(k: i64) -> i64 { return mob_pose(0, k) }
3891
3892// GE13b THE SELECTOR: what a girl wears, from the world's outfit mode, her genome and her pose.
3893// Returns the OUTFIT word: upper garment id << GAR_SLOT_SH | lower garment id, 0 = nothing in that
3894// slot; one-piece and dresses cover both and ride the LOWER slot. Every branch is a table LOOKUP --
3895// no colour, no shape, no category is decided here; a world that rewrites a garment row rewrites
3896// what these ids mean, and a row the world does not carry (0) is not wearable.
3897// mode 0 (identity keeps everyone clothed): CASUAL/EVENING by the beach bits 18-19 -- tee+shorts,
3898// tee+skirt, sundress, evening dress. Before this every girl wore the one dress.
3899// mode 1 (MIXED beach): the incumbent bits, garment by garment -- 0 bikini top+bottom, 1 one-piece,
3900// 2 bikini bottom only (topless), 3 nothing (nude). Girl-for-girl the SAME 0/1/2 the old rule
3901// produced; gate T85 proves it against mob_outfit_v1 below.
3902// mode 2 (SWIMWEAR-ONLY): bit 18 picks bikini or one-piece; never a cover-up, never casual.
3903// mode 3 (NUDE BEACH): nothing worn.
3904// POSE OVERLAY: asleep (mob_pose SLEEP) on a beach with swim bottoms on, she wraps a SARONG
3905// (beach-cover) over them -- same coverage bit, so her legacy value never flickers -- unless
3906// she is in water, where a wrap is never worn (swim => swim).
3907func wc_mob_in_water(base: i64, k: i64) -> i64 {
3908 let m: *i64 = mobp(base)
3909 let h: i64 = en_nth(m, k)
3910 if vget(base, en_get(m, h, MC_X) >> 8, en_get(m, h, MC_Y) >> 8, en_get(m, h, MC_Z) >> 8) == B_WATER { return 1 }
3911 return 0
3912}
3913func wc_outfit(base: i64, k: i64) -> i64 {
3914 let md: i64 = wsp(base)[P_OUTFIT]
3915 let m: *i64 = mobp(base)
3916 let gb: i64 = (en_get(m, en_nth(m, k), MC_GENE) >> MG_BEACH_SH) & MG_BEACH_MASK
3917 var up: i64 = 0
3918 var lo: i64 = 0
3919 var beach: i64 = 0
3920 if md == 0 {
3921 if gb == 0 { up = GAR_TEE; lo = GAR_SHORTS }
3922 if gb == 1 { up = GAR_TEE; lo = GAR_SKIRT }
3923 if gb == 2 { lo = GAR_SUNDRESS }
3924 if gb == 3 { lo = GAR_DRESS }
3925 }
3926 if md == 1 {
3927 beach = 1
3928 if gb == 0 { up = GAR_BIKINI_TOP; lo = GAR_BIKINI_BOT }
3929 if gb == 1 { lo = GAR_ONEPIECE }
3930 if gb == 2 { lo = GAR_BIKINI_BOT }
3931 }
3932 if md == 2 {
3933 beach = 1
3934 if (gb & 1) == 0 { up = GAR_BIKINI_TOP; lo = GAR_BIKINI_BOT }
3935 if (gb & 1) == 1 { lo = GAR_ONEPIECE }
3936 }
3937 if beach == 1 { if lo > 0 { if gar_cat(gar_row(base, lo)) == GAR_CAT_SWIM { if gar_cov(gar_row(base, lo)) == GAR_COV_BOT {
3938 if mob_pose(base, k) == MOB_POSE_SLEEP { if wc_mob_in_water(base, k) == 0 { lo = GAR_SARONG } }
3939 } } } }
3940 // a row the world does not carry is not a garment: the slot empties (the neg-control's lever)
3941 if gar_row(base, up) == 0 { up = 0 }
3942 if gar_row(base, lo) == 0 { lo = 0 }
3943 return (up << GAR_SLOT_SH) | lo
3944}
3945// 0 clothed / 1 topless / 2 nude, DERIVED from the worn set -- the contract every existing consumer
3946// (the page's uOF bands, T49, T77) keeps, now a consequence of the wardrobe instead of the wardrobe.
3947func mob_outfit(base: i64, k: i64) -> i64 {
3948 let o: i64 = wc_outfit(base, k)
3949 return gar_legacy(gar_cov(gar_row(base, o >> GAR_SLOT_SH)) | gar_cov(gar_row(base, o & GAR_ID_MASK)))
3950}
3951// THE PRE-WARDROBE RULE, kept RUNNABLE as the gate's oracle: T85 proves mob_outfit == mob_outfit_v1
3952// for every girl in every mode, so no consumer sees a different girl. Not called by the game.
3953func mob_outfit_v1(base: i64, k: i64) -> i64 {
3954 let md9: i64 = wsp(base)[P_OUTFIT]
3955 if md9 == 0 { return 0 }
3956 // GE13: per-world outfit FLAGS, all data -- 1 = MIXED (genome bits decide; the incumbent
3957 // beach), 2 = SWIMWEAR-ONLY (dimension active, variety off), 3 = NUDE BEACH (all bare).
3958 if md9 == 2 { return 0 }
3959 if md9 == 3 { return 2 }
3960 let m: *i64 = mobp(base)
3961 let g: i64 = (en_get(m, en_nth(m, k), MC_GENE) >> 18) & 3
3962 if g <= 1 { return 0 }
3963 if g == 2 { return 1 }
3964 return 2
3965}
3966
3967func wc_need_seed(base: i64) -> i64 {
3968 let m: *i64 = mobp(base)
3969 let s: *i64 = wst(base)
3970 let nd0: *i64 = (base + O_NEED) as *i64
3971 var k: i64 = 0
3972 while k < en_count(m) {
3973 nd0[k*4] = 300 + (whash(k, 11, s[S_SEED]) % 200)
3974 nd0[k*4 + 1] = 700
3975 nd0[k*4 + 2] = 300 + (whash(k, 23, s[S_SEED]) % 200)
3976 nd0[k*4 + 3] = 0
3977 k = k + 1
3978 }
3979 return 0
3980}
3981
3982// ---------- C3/C5: the inner life (thought ledger + gossip records) ----------
3983func mindp(base: i64) -> *i64 { return (base + O_MIND) as *i64 }
3984func wc_mind_zero(base: i64) -> i64 {
3985 let mp: *i64 = mindp(base)
3986 var i: i64 = 0
3987 while i < MOB_CAP*24 { mp[i] = 0; i = i + 1 }
3988 return 0
3989}
3990// add/refresh a thought: same id refreshes in place; else a dead slot; else rotate one out
3991func th_add(base: i64, k: i64, id: i64, mag: i64, ttl: i64) -> i64 {
3992 let mp: *i64 = mindp(base)
3993 let s: *i64 = wst(base)
3994 let b: i64 = k*24
3995 var slot: i64 = 0 - 1
3996 var i: i64 = 0
3997 while i < 8 {
3998 if (mp[b + i*2] >> 8) == id { slot = i }
3999 i = i + 1
4000 }
4001 if slot < 0 {
4002 i = 0
4003 while i < 8 {
4004 if slot < 0 { if mp[b + i*2 + 1] <= s[S_T] { slot = i } }
4005 i = i + 1
4006 }
4007 }
4008 if slot < 0 { slot = (s[S_T] + k) % 8 }
4009 mp[b + slot*2] = id*256 + (mag + 128)
4010 mp[b + slot*2 + 1] = s[S_T] + ttl
4011 return 0
4012}
4013// mood = the sum of LIVE thoughts, clamped -- always decomposable back into its reasons
4014func th_mood(base: i64, k: i64) -> i64 {
4015 let mp: *i64 = mindp(base)
4016 let s: *i64 = wst(base)
4017 let b: i64 = k*24
4018 var mo: i64 = 0
4019 var i: i64 = 0
4020 while i < 8 {
4021 if mp[b + i*2] != 0 { if mp[b + i*2 + 1] > s[S_T] { mo = mo + (mp[b + i*2] & 255) - 128 } }
4022 i = i + 1
4023 }
4024 if mo > 100 { mo = 100 }
4025 if mo < 0 - 100 { mo = 0 - 100 }
4026 return mo
4027}
4028// a gossip fact: (subject, predicate, confidence, born). A better-known copy never downgrades.
4029func go_add(base: i64, k: i64, subj: i64, pred: i64, conf: i64) -> i64 {
4030 let mp: *i64 = mindp(base)
4031 let s: *i64 = wst(base)
4032 let b: i64 = k*24 + 16
4033 var slot: i64 = 0 - 1
4034 var free: i64 = 0 - 1
4035 var oldest: i64 = 0
4036 var oldt: i64 = SAVE_CK_MASK
4037 var i: i64 = 0
4038 while i < 4 {
4039 let w: i64 = mp[b + i*2]
4040 if w == 0 { if free < 0 { free = i } }
4041 if w != 0 { if (w >> 16) == subj { if ((w >> 8) & 255) == pred { slot = i } } }
4042 if mp[b + i*2 + 1] < oldt { oldt = mp[b + i*2 + 1]; oldest = i }
4043 i = i + 1
4044 }
4045 if slot >= 0 { if (mp[b + slot*2] & 255) >= conf { return 0 } }
4046 if slot < 0 { slot = free }
4047 if slot < 0 { slot = oldest }
4048 mp[b + slot*2] = subj*WATER_MAGIC_65536 + pred*256 + conf
4049 mp[b + slot*2 + 1] = s[S_T]
4050 return 0
4051}
4052func go_newest(base: i64, k: i64) -> i64 {
4053 let mp: *i64 = mindp(base)
4054 let b: i64 = k*24 + 16
4055 var best: i64 = 0 - 1
4056 var bt: i64 = 0 - 1
4057 var i: i64 = 0
4058 while i < 4 {
4059 if mp[b + i*2] != 0 { if mp[b + i*2 + 1] > bt { bt = mp[b + i*2 + 1]; best = i } }
4060 i = i + 1
4061 }
4062 return best
4063}
4064
4065// ---- GAIT: THE PARAMETRIC DRIVE ----------------------------------------------------------------
4066// decidegrees -> it-units. This is nx_quadruped's qdeg idiom at 10x resolution; that organ's
4067// quad_gait_pose_g is the working precedent in this estate for "a gait is DATA, amplitudes and
4068// per-limb phase offsets are the gait's parameters". It is deliberately NOT imported: it calls
4069// sys_mmap and this engine targets wasm, so the IDIOM is reused and the code is not duplicated.
4070func wc_ddeg(d: i64) -> i64 { return d*IT_PI/WC_DDEG_HALFTURN }
4071// AMPLITUDE from a PUBLISHED RANGE. The halving lives here, once, so no caller can forget it and
4072// no future reader has to work out which of the two a stored constant is.
4073func wc_amp_ddeg(range_ddeg: i64) -> i64 { return wc_ddeg(range_ddeg)/2 }
4074// STRIDE PHASE: one full cycle per WC_STRIDE_Q8 of PATH TRAVELLED. Distance-driven by
4075// construction, which is what makes pelvic rotation phase-locked to stride -- the coupling is
4076// structural, not a tuning. Two consequences fall out for free: a girl who stops has a FROZEN
4077// stride phase, so she stands instead of marching on the spot; and a girl who walks faster takes
4078// the same-length strides more often, so cadence emerges from speed instead of being a constant.
4079// ⚠THIS IS A SECOND, DIFFERENT QUANTITY FROM wc_gait_ang -- NOT A DUPLICATE RULER. wc_gait_ang
4080// drives the THORAX BOB, which is the SECOND HARMONIC of the stride (two steps per cycle, each
4081// with its own vertical rise and fall), and its scale is calibrated against the gait_bob_q8 row.
4082// ★THEIR RATIO IS A CHECKABLE CLAIM AND IT DOES NOT CURRENTLY HOLD. wc_gait_ang implies one bob
4083// per 962 q8 of travel, hence one stride per 1924 q8 = 7.5 m, against the cited 1.46 m -- the
4084// incumbent gait drive is calibrated 5.1x too long, and separately it is calibrated at SPEED (40
4085// q8/tick) which no mob ever reaches: mobs walk at 4-5 q8/tick, so the bob it produces in the
4086// live world is ~0.25 Hz where walking thorax oscillation is ~1.8-2 Hz.
4087// ⛔THAT DISAGREEMENT IS NAMED HERE AND DELIBERATELY NOT FIXED BY EDITING THE SHARED CONSTANT.
4088// wc_gait_ang feeds the soft-body anchor that gate teeth T40 and T54 are calibrated against, and
4089// silently moving a constant another tooth is calibrated on is how a green gate stops meaning
4090// anything. The renderer takes the cited stride; the bob keeps its own calibration; the gap goes
4091// on the board as a measurement with its arithmetic attached rather than into a quiet edit.
4092func wc_stride_ang(g_q8: i64) -> i64 { return g_q8*2*IT_PI/WC_STRIDE_Q8 }
4093// shortest signed representative of an angle difference, in (-IT_PI, IT_PI].
4094func wc_angwrap(d: i64) -> i64 {
4095 var r: i64 = d % (2*IT_PI)
4096 if r > IT_PI { r = r - 2*IT_PI }
4097 if r < 0 - IT_PI { r = r + 2*IT_PI }
4098 return r
4099}
4100// THE HEADING LOW-PASS. This one function is the whole wobble fix AND the fluidity parameter.
4101// WHAT IT REPLACES: nothing. There was no heading state at all -- the body's yaw was the PLAYER'S
4102// camera yaw, so every girl pivoted with the camera while translating along her own path, and the
4103// steering that produced that path is an 8-direction bang-bang controller (dx = +/-4 per axis with
4104// an independent per-axis deadband) whose sign can flip in a single tick and whose diagonal is
4105// 41 percent faster than its axis-aligned step. Facing derived from that signal inherits all of it.
4106// HOW IT STEERS WITHOUT atan2: there is no it_atan2 anywhere in the corpus (proven, corpus_complete
4107// =1), and adding one would be a second trigonometric approximation beside the incumbent. It is not
4108// needed. The CROSS PRODUCT of the current forward with the desired direction is sin(error) scaled
4109// by |desired|, which already carries both the SIGN and, near convergence, the MAGNITUDE of the
4110// error -- so a proportional controller can be built from the sin/cos this engine already has.
4111// cr < 0 / cr > 0 -> which way to turn
4112// dt < 0 -> the target is BEHIND: turn at the full allowance, sign broken deterministically
4113// The step is proportional to sin(error), so it goes to zero AS the error goes to zero: the
4114// approach is asymptotic and cannot overshoot, which is what "fluid" means here as opposed to a
4115// bang-bang controller that snaps (locked) or an unclamped one that rings (floppy).
4116// The allowance is speed-coupled: mx = 0 when she is not moving, so nobody spins on the spot.
4117func wc_hdg_budget(hdg: i64, dx: i64, dz: i64, budget: i64) -> i64 {
4118 if budget <= 0 { return hdg }
4119 let mag: i64 = sd_isqrt(dx*dx + dz*dz)
4120 if mag <= 0 { return hdg }
4121 let fx: i64 = it_sin4096(hdg % (2*IT_PI))
4122 let fz: i64 = it_cos4096(hdg % (2*IT_PI))
4123 let cr: i64 = fx*dz - fz*dx
4124 let dot: i64 = fx*dx + fz*dz
4125 var step: i64 = 0 - budget*cr/(mag*WATER_MAGIC_4096)
4126 if dot < 0 {
4127 step = budget
4128 if cr > 0 { step = 0 - budget }
4129 }
4130 if step == 0 { if cr > 0 { step = 0 - 1 } }
4131 if step == 0 { if cr < 0 { step = 1 } }
4132 if step > budget { step = budget }
4133 if step < 0 - budget { step = 0 - budget }
4134 var next: i64 = hdg + step
4135 if next < 0 { next = next + 2*IT_PI }
4136 if next >= 2*IT_PI { next = next - 2*IT_PI }
4137 return next
4138}
4139// Forward-walk clips cancel travel along body heading. Preserve the authored intent
4140// and its existing turn budget; fractional world-axis remainders retain slow motion.
4141func wc_steer_reset(base:i64)->i64 {
4142 let state:*i64=(base+O_MOB_STEER) as *i64
4143 var i:i64=0
4144 while i<MOB_CAP*WC_STEER_W {state[i]=0;i=i+1}
4145 return 0
4146}
4147
4148// The resident loader declares an inspected clip policy after each successful ingest.
4149// Calibration supplies distance, not permission to reinterpret unknown clip direction.
4150func wc_nxa_locomotion(base:i64,mode:i64)->i64 {
4151 let policy:*i64=(base+O_NXA_LOCOMOTION) as *i64
4152 policy[0]=NXA_LOCOMOTION_RAW
4153 wc_steer_reset(base)
4154 if mode==NXA_LOCOMOTION_RAW {return 0}
4155 if mode!=NXA_LOCOMOTION_FORWARD {return 0-1}
4156 if nxa_hp(base)[NXA_H_LOADED]!=1 {return 0-2}
4157 policy[0]=mode
4158 return mode
4159}
4160func nxa_locomotion(mode:i64)->i64 {return wc_nxa_locomotion(0,mode)}
4161func wc_forward_policy(base:i64,k:i64)->i64 {
4162 if k<0||k>=WC_CAST_N {return 0}
4163 let policy:*i64=(base+O_NXA_LOCOMOTION) as *i64
4164 if policy[0]!=NXA_LOCOMOTION_FORWARD {return 0}
4165 if nxa_hp(base)[NXA_H_LOADED]!=1||wc_gait_header(base)[7]!=1 {return 0}
4166 return 1
4167}
4168func wc_motion_step(base:i64,k:i64,heading:i64,dx:i64,dz:i64)->*i64 {
4169 if wc_forward_policy(base,k)==1 {return wc_forward_steer(base,k,heading,dx,dz)}
4170 let state:*i64=(base+O_MOB_STEER+k*WC_STEER_W*8) as *i64
4171 state[0]=0;state[1]=0;state[2]=dx;state[3]=dz;state[4]=en_nth(mobp(base),k)
4172 return state
4173}
4174
4175func wc_forward_steer(base:i64,k:i64,heading:i64,dx:i64,dz:i64)->*i64 {
4176 let state:*i64=(base+O_MOB_STEER+k*WC_STEER_W*8) as *i64
4177 // Dense iteration can swap-remove or recycle an entity; fractions belong to its handle.
4178 let handle:i64=en_nth(mobp(base),k)
4179 if state[4]!=handle {state[0]=0;state[1]=0;state[4]=handle}
4180 state[2]=0;state[3]=0
4181 if dx==0&&dz==0 {return state}
4182 let magnitude:i64=sd_isqrt((dx*dx+dz*dz)*NA_Q12*NA_Q12)
4183 let xq:i64=magnitude*it_sin4096(heading)/NA_Q12+state[0]
4184 let zq:i64=magnitude*it_cos4096(heading)/NA_Q12+state[1]
4185 state[2]=xq/NA_Q12;state[3]=zq/NA_Q12
4186 state[0]=xq-state[2]*NA_Q12;state[1]=zq-state[3]*NA_Q12
4187 return state
4188}
4189func wc_hdg_step(hdg: i64, dx: i64, dz: i64) -> i64 {
4190 let mag: i64 = sd_isqrt(dx*dx + dz*dz)
4191 return wc_hdg_budget(hdg,dx,dz,mag*WC_TURN_PER_STRIDE/WC_STRIDE_Q8)
4192}
4193func wc_facing_step(hdg: i64, dx: i64, dz: i64, player_dx: i64, player_dz: i64, radius: i64, budget: i64) -> i64 {
4194 if dx != 0 || dz != 0 { return wc_hdg_step(hdg,dx,dz) }
4195 if radius <= 0 || budget <= 0 { return hdg }
4196 if player_dx*player_dx + player_dz*player_dz >= radius*radius { return hdg }
4197 return wc_hdg_budget(hdg,player_dx,player_dz,budget)
4198}
4199func wc_hdg_of(base: i64, k: i64) -> i64 { let hp: *i64 = (base + O_MOBHDG) as *i64; return hp[k] }
4200func wc_set_facing_policy(base: i64, radius: i64, gain: i64) -> i64 {
4201 if radius <= 0 || radius > FACING_RADIUS_MAX_Q8 || gain <= 0 || gain > IT_PI { return 0 - 1 }
4202 let state: *i64 = wst(base)
4203 state[S_FACE_RADIUS] = radius
4204 state[S_FACE_GAIN] = gain
4205 return 0
4206}
4207func set_facing_policy(radius: i64, gain: i64) -> i64 { return wc_set_facing_policy(0,radius,gain) }
4208func mob_heading(k: i64) -> i64 {
4209 if k < 0 || k >= en_count(mobp(0)) { return 0 - 1 }
4210 return wc_hdg_of(0,k)
4211}
4212// the girl's own stride phase, in it-units, from the path length she has actually walked
4213func wc_mob_sph(base: i64, k: i64) -> i64 {
4214 let nd: *i64 = (base + O_NEED) as *i64
4215 return wc_stride_ang(nd[k*4 + WC_NEED_GAIT]) % (2*IT_PI)
4216}
4217// ---- THE POSE, AS FIVE PURE FUNCTIONS OF STRIDE PHASE -------------------------------------------
4218// Extracted out of wc_mob_mesh deliberately: the renderer needs them and the GATE needs them, and a
4219// gate that recomputed these angles from the same formula would be testing a COPY of the mechanism
4220// rather than the mechanism. One definition, two callers, and a tooth that fails when the shipping
4221// code changes. They take only the phase, so they are trivially testable at any phase without a
4222// world, a camera, a rig or a framebuffer.
4223// SIGN CONVENTION, stated once: pelvis LEADS (+), thorax TRAILS (-, applied as a local yaw on the
4224// chest bone whose parent is the pelvis), legs and arms swing in elevation, elbow only flexes.
4225func wc_gp_pelvis(sph: i64) -> i64 { return wc_amp_ddeg(WC_PELVIS_ROT_DDEG)*it_sin4096(sph % (2*IT_PI))/WATER_MAGIC_4096 }
4226func wc_gp_thorax(sph: i64) -> i64 { return wc_amp_ddeg(WC_THORAX_ROT_DDEG)*it_sin4096(sph % (2*IT_PI))/WATER_MAGIC_4096 }
4227func wc_gp_leg(sph: i64) -> i64 { return it_sin4096(sph % (2*IT_PI))*(IT_PI/6)/WATER_MAGIC_4096 }
4228func wc_gp_arm(sph: i64) -> i64 { return it_sin4096(sph % (2*IT_PI))*(IT_PI/12)/WATER_MAGIC_4096 }
4229// (1 - cos)/2 scaled by the cited ROM: 0 at phase 0, ROM at the half cycle, never negative, and no
4230// free parameter -- the anatomical floor at zero and the published range fix both ends between them.
4231func wc_gp_elbow(sph: i64) -> i64 { return wc_ddeg(WC_ELBOW_ROM_DDEG)*(WATER_MAGIC_4096 - it_cos4096(sph % (2*IT_PI)))/(2*WATER_MAGIC_4096) }
4232
4233// mob wander: smooth deterministic drift (phase-driven sin/cos), terrain-following, player-curious
4234// within 6 blocks. Pure integer -- the T4 determinism checksum covers mobs by construction.
4235// Residency describes available collision data, not whether the entity exists.
4236// The inset retains the previous movement boundary and its neighbour-cell footprint.
4237func wc_mob_resident(base: i64, h: i64) -> i64 {
4238 let m: *i64 = mobp(base)
4239 if en_valid(m,h)!=1 { return 0 }
4240 let x: i64 = en_get(m,h,MC_X)
4241 let z: i64 = en_get(m,h,MC_Z)
4242 if x<2*256||x>(WX-2)*256||z<2*256||z>(WZ-2)*256 { return 0 }
4243 return 1
4244}
4245func mob_resident(k: i64) -> i64 {
4246 let m: *i64 = mobp(0)
4247 return wc_mob_resident(0,en_nth(m,k))
4248}
4249// Both movement intent and the taking phase select the same authored hunt target.
4250func wc_warden_target(base:i64,hw:i64,night:i64)->i64 {
4251 let m:*i64=mobp(base)
4252 var bestj: i64 = 0 - 1
4253 // the world declares its danger (P_WARD), night widens it, and the ARRIVING
4254 // TRAVELER'S LEVEL widens it further: a veteran isekai's stakes rise with him
4255 var wbase: i64 = wsp(base)[P_WARD]
4256 if wbase <= 0 { wbase = 12 }
4257 var bestd: i64 = wbase*256 + pass_level_of(base)*128
4258 if night == 1 { bestd = bestd + 8*256 } // the night belongs to the wardens
4259 var kj: i64 = 0
4260 while kj < en_count(m) {
4261 let hj: i64 = en_nth(m, kj)
4262 if (en_get(m, hj, MC_DISG) & 3) == 3 {
4263 var ddx2: i64 = en_get(m, hj, MC_X) - en_get(m, hw, MC_X)
4264 if ddx2 < 0 { ddx2 = 0 - ddx2 }
4265 var ddz2: i64 = en_get(m, hj, MC_Z) - en_get(m, hw, MC_Z)
4266 if ddz2 < 0 { ddz2 = 0 - ddz2 }
4267 if ddx2 + ddz2 < bestd { bestd = ddx2 + ddz2; bestj = kj }
4268 }
4269 kj = kj + 1
4270 }
4271 return bestj
4272}
4273
4274func mob_tick(base: i64) -> i64 {
4275 let m: *i64 = mobp(base)
4276 let s: *i64 = wst(base)
4277 let nd: *i64 = (base + O_NEED) as *i64
4278 let vyp: *i64 = (base + O_MOBVY) as *i64 // GE12 vertical velocity per mob
4279 let night: i64 = cal_night(base)
4280 var k: i64 = 0
4281 while k < en_count(m) {
4282 let h: i64 = en_nth(m, k)
4283 if h > 0 {
4284 var x: i64 = en_get(m, h, MC_X)
4285 var z: i64 = en_get(m, h, MC_Z)
4286 var ph: i64 = en_get(m, h, MC_PH) + 5
4287 // FOOTSTRIKE IMPULSE: bank the pre-move vertical velocity. After the movement code
4288 // below, (vy_pre < 0 && vyp[k] == 0) is precisely "GE12's integrator arrested a
4289 // fall this tick" -- ground contact -- and |vy_pre| IS the ground-reaction delta-v.
4290 let vy_pre: i64 = vyp[k]
4291 // C1 NEEDS drift (integer, staggered by handle -- the village never moves in lockstep)
4292 let nb: i64 = k*4
4293 if (s[S_T] + h) % 8 == 0 {
4294 if nd[nb] < 1000 { nd[nb] = nd[nb] + 2 } // hunger grows
4295 if nd[nb + 2] < 1000 { nd[nb + 2] = nd[nb + 2] + 3 } // loneliness grows
4296 if nd[nb + 1] > 0 { nd[nb + 1] = nd[nb + 1] - 1 } // energy drains
4297 }
4298 // C3: lived moments become LEDGER ENTRIES (staggered; each refreshes in place)
4299 if (s[S_T] + h) % 128 == 0 {
4300 let bst0: i64 = (en_get(m, h, MC_DISG) >> 3) & 7
4301 if cal_rain(base) == 1 { if bst0 != 2 { th_add(base, k, TH_RAIN, 0 - 8, 1200) } }
4302 if nd[nb] > 700 { th_add(base, k, TH_HUNGRY, 0 - 10, 900) }
4303 if night == 1 { if bst0 == 2 { th_add(base, k, TH_SLEPT, 6, 3600) } }
4304 }
4305 // BEHAVIOR LAYER (C1/C2 -- lives picked by NEEDS x the spec ADVERTISEMENT table,
4306 // not a dice roll): state in MC_DISG bits 3-5, repicked on the staggered 240-tick
4307 // clock. 0=WANDER 1=SOCIALIZE 2=REST 3=FORAGE 4=FLEE. At night the village SLEEPS
4308 // (the C2 schedule) -- while the wardens hunt wider. The old spec-weights TODO is
4309 // RESOLVED: behavior now reads needs + P_ADV rows, both data.
4310 let dg: i64 = en_get(m, h, MC_DISG)
4311 var bst: i64 = (dg >> 3) & 7
4312 if (s[S_T] + h*37) % 240 == 0 {
4313 var sc1: i64 = 220 + (whash(h*911, s[S_T]/240, s[S_SEED]) % 64)
4314 // C3 feedback: the ledger BENDS the life -- a glad girl seeks company, a sad
4315 // one withdraws to rest. Mood is data the argmax reads, not a scripted branch.
4316 let mo9: i64 = th_mood(base, k)
4317 var sc2: i64 = nd[nb + 2]
4318 if mo9 > 0 { sc2 = sc2 + mo9 }
4319 var sc3: i64 = 1000 - nd[nb + 1]
4320 if mo9 < 0 { sc3 = sc3 - mo9*2 }
4321 var sc4: i64 = 0
4322 var ai: i64 = 0
4323 while ai < 6 && wc_mob_resident(base,h)==1 {
4324 let ad: i64 = wsp(base)[P_ADV + ai]
4325 if ad != 0 { if ((ad >> 8) & 255) == 0 {
4326 let ab: i64 = (ad >> 16) & 255
4327 var fnd: i64 = 0
4328 var az2: i64 = (z >> 8) - 3
4329 while az2 <= (z >> 8) + 3 {
4330 var ax2: i64 = (x >> 8) - 3
4331 while ax2 <= (x >> 8) + 3 {
4332 let gy2: i64 = mob_ground(base, ax2, (en_get(m, h, MC_Y) >> 8) + 2, az2)
4333 if gy2 >= 0 {
4334 if vget(base, ax2, gy2, az2) == ab { fnd = 1 }
4335 if vget(base, ax2, gy2 + 1, az2) == ab { fnd = 1 }
4336 }
4337 ax2 = ax2 + 1
4338 }
4339 az2 = az2 + 1
4340 }
4341 if fnd == 1 {
4342 let cand: i64 = nd[nb]*(ad & 255)/8
4343 if cand > sc4 { sc4 = cand }
4344 }
4345 } }
4346 ai = ai + 1
4347 }
4348 bst = 0
4349 var top: i64 = sc1
4350 if sc2 > top { top = sc2; bst = 1 }
4351 if sc3 > top { top = sc3; bst = 2 }
4352 if sc4 > top { top = sc4; bst = 3 }
4353 if night == 1 { bst = 2 }
4354 en_set(m, h, MC_DISG, dg_bst(dg, bst))
4355 }
4356 if (dg & 3) == 3 {
4357 // a hidden human checks for hunters -- night widens the danger radius
4358 var wr: i64 = 8*256
4359 if night == 1 { wr = 12*256 }
4360 var wnd: i64 = wr
4361 var wnx: i64 = 0
4362 var wnz: i64 = 0
4363 var kf: i64 = 0
4364 while kf < en_count(m) {
4365 let hf: i64 = en_nth(m, kf)
4366 if en_get(m, hf, MC_KIND) == 2 { if (en_get(m, hf, MC_DISG) & 1) == 0 {
4367 var fdx: i64 = en_get(m, hf, MC_X) - x
4368 if fdx < 0 { fdx = 0 - fdx }
4369 var fdz: i64 = en_get(m, hf, MC_Z) - z
4370 if fdz < 0 { fdz = 0 - fdz }
4371 if fdx + fdz < wnd { wnd = fdx + fdz; wnx = en_get(m, hf, MC_X); wnz = en_get(m, hf, MC_Z) }
4372 } }
4373 kf = kf + 1
4374 }
4375 if wnd < wr { bst = 4; en_set(m, h, MC_DISG, dg_bst(en_get(m, h, MC_DISG), 4)) }
4376 }
4377 var dx: i64 = it_sin4096(ph*3 % (2*IT_PI))*5/WATER_MAGIC_4096
4378 var dz: i64 = it_cos4096(ph*3 % (2*IT_PI))*5/WATER_MAGIC_4096
4379 if bst == 1 {
4380 // SOCIALIZE: walk to the nearest other girl; inside 1.2 blocks, stand together
4381 var snd: i64 = SAVE_CK_MASK
4382 var snx: i64 = x
4383 var snz: i64 = z
4384 var snk: i64 = 0 - 1
4385 var ks: i64 = 0
4386 while ks < en_count(m) {
4387 let hs: i64 = en_nth(m, ks)
4388 if hs != h {
4389 var sdx: i64 = en_get(m, hs, MC_X) - x
4390 if sdx < 0 { sdx = 0 - sdx }
4391 var sdz: i64 = en_get(m, hs, MC_Z) - z
4392 if sdz < 0 { sdz = 0 - sdz }
4393 if sdx + sdz < snd { snd = sdx + sdz; snx = en_get(m, hs, MC_X); snz = en_get(m, hs, MC_Z); snk = ks }
4394 }
4395 ks = ks + 1
4396 }
4397 dx = 0
4398 dz = 0
4399 if snd > 300 {
4400 if snx > x + 32 { dx = 4 }
4401 if snx < x - 32 { dx = 0 - 4 }
4402 if snz > z + 32 { dz = 4 }
4403 if snz < z - 32 { dz = 0 - 4 }
4404 }
4405 if snd <= 300 {
4406 ph = ph - 4 // stilled gait: talking, not marching
4407 if (s[S_T] + h) % 4 == 0 { // company relieves loneliness
4408 nd[nb + 2] = nd[nb + 2] - 10
4409 if nd[nb + 2] < 0 { nd[nb + 2] = 0 }
4410 }
4411 // C5: CONVERSATION MOVES FACTS. One fact passes speaker->listener with
4412 // confidence -15; below conf 30 the predicate may FLIP (1<->2) -- rumor
4413 // decays into misinformation BY MECHANIC, and hearing it leaves a thought.
4414 if (s[S_T] + h) % 128 == 0 {
4415 th_add(base, k, TH_COMPANY, 5, 1200)
4416 let gi: i64 = go_newest(base, k)
4417 if gi >= 0 { if snk >= 0 {
4418 let gw: i64 = mindp(base)[k*24 + 16 + gi*2]
4419 let conf2: i64 = (gw & 255) - 15
4420 if conf2 > 0 {
4421 var pred2: i64 = (gw >> 8) & 255
4422 if conf2 < 30 { if whash(s[S_T], k*31 + snk, s[S_SEED] + 777) % 3 == 0 {
4423 pred2 = 3 - pred2
4424 } }
4425 go_add(base, snk, gw >> 16, pred2, conf2)
4426 if pred2 == GO_TAKEN { th_add(base, snk, TH_HEARD_TAKEN, 0 - 10, 7200) }
4427 if pred2 == GO_FRIEND { th_add(base, snk, TH_HEARD_FRIEND, 6, 7200) }
4428 }
4429 } }
4430 }
4431 }
4432 }
4433 if bst == 2 {
4434 dx = 0
4435 dz = 0
4436 ph = ph - 4 // REST: seated stillness
4437 if (s[S_T] + h) % 4 == 0 { // sleep restores; night sleep restores MORE
4438 var rg: i64 = 2
4439 if night == 1 { rg = 8 }
4440 nd[nb + 1] = nd[nb + 1] + rg
4441 if nd[nb + 1] > 1000 { nd[nb + 1] = 1000 }
4442 }
4443 }
4444 if bst == 3 {
4445 // FORAGE: graze rhythm -- steps then a pause, head-down cadence; grazing feeds her
4446 if (s[S_T] + h) % 16 >= 6 { dx = 0; dz = 0; ph = ph - 3 }
4447 if (s[S_T] + h) % 8 == 0 {
4448 nd[nb] = nd[nb] - 12
4449 if nd[nb] < 0 { nd[nb] = 0 }
4450 }
4451 }
4452 if bst == 4 {
4453 // FLEE: straight away from the nearest warden, fast
4454 var wnd2: i64 = SAVE_CK_MASK
4455 var wax: i64 = x
4456 var waz: i64 = z
4457 var kf2: i64 = 0
4458 while kf2 < en_count(m) {
4459 let hf2: i64 = en_nth(m, kf2)
4460 if en_get(m, hf2, MC_KIND) == 2 { if (en_get(m, hf2, MC_DISG) & 1) == 0 {
4461 var fdx2: i64 = en_get(m, hf2, MC_X) - x
4462 if fdx2 < 0 { fdx2 = 0 - fdx2 }
4463 var fdz2: i64 = en_get(m, hf2, MC_Z) - z
4464 if fdz2 < 0 { fdz2 = 0 - fdz2 }
4465 if fdx2 + fdz2 < wnd2 { wnd2 = fdx2 + fdz2; wax = en_get(m, hf2, MC_X); waz = en_get(m, hf2, MC_Z) }
4466 } }
4467 kf2 = kf2 + 1
4468 }
4469 dx = 0
4470 dz = 0
4471 if wax > x { dx = 0 - 6 }
4472 if wax <= x { dx = 6 }
4473 if waz > z { dz = 0 - 6 }
4474 if waz <= z { dz = 6 }
4475 }
4476 // PERSONALITY (genome bits 13-15, INHERITED): the bold<->shy spectrum photographers
4477 // know -- engagement is a RANGE, not one script. Near the player: shy girls (0-1)
4478 // retreat and keep 5 blocks; watchful ones (2-3) freeze and observe, backing off
4479 // only if crowded; warm ones (4-5) approach to a polite arm's length; bold ones
4480 // (6-7) come right to your side. FORAGE also reads it: bold girls gather TOWARD
4481 // you (the obvious pick-up), shy girls drift their gathering away (the oblivious).
4482 let pdx: i64 = s[S_CX] - x
4483 let pdz: i64 = s[S_CZ] - z
4484 var pd: i64 = pdx
4485 if pd < 0 { pd = 0 - pd }
4486 var pdz2: i64 = pdz
4487 if pdz2 < 0 { pdz2 = 0 - pdz2 }
4488 let bold: i64 = (en_get(m, h, MC_GENE) >> 13) & 7
4489 // HABITUATION: the approach/watch drive runs only while she is lonely enough to
4490 // care (the player is a smart object too -- company below relieves the need, and a
4491 // satisfied girl walks on). Shy retreat is FEAR, not curiosity: never gated.
4492 var curi: i64 = 1
4493 if nd[nb + 2] < wsp(base)[P_CURI] { curi = 0 }
4494 if bold <= 1 { curi = 1 }
4495 if curi == 1 { if bst != 2 { if bst != 4 { if pd + pdz2 < 6*256 {
4496 var twd: i64 = 0 // -1 retreat, 0 hold, +1 approach
4497 var stopd: i64 = 400
4498 var spd: i64 = 3
4499 if bold <= 1 { twd = 0 - 1; stopd = 5*256; spd = 5 }
4500 if bold >= 2 { if bold <= 3 {
4501 twd = 0
4502 if pd + pdz2 < 2*256 { twd = 0 - 1; stopd = 2*256; spd = 4 }
4503 } }
4504 if bold >= 4 { if bold <= 5 { twd = 1; stopd = 400; spd = 3 } }
4505 if bold >= 6 { twd = 1; stopd = 200; spd = 5 }
4506 if bst == 3 { if twd != 0 { spd = 2 } } // gathering: the drift is subtle
4507 if twd == 1 { if pd + pdz2 > stopd {
4508 // S1 F-FORMATION SLOT (the social-interaction brief, rung S1): her approach
4509 // target is HER slot on the ring around the player, never the player center
4510 // -- N approaching girls used to converge on one point and bunch into a
4511 // queue ("they just follow me around"). Slot angle = a stable hash of her
4512 // handle; radius = her OWN personality stop distance, so bold girls ring
4513 // close and warm girls ring at arm's length (proxemics stays personal).
4514 let sa9: i64 = (h*1741) % (2*IT_PI)
4515 let tx9: i64 = s[S_CX] + it_cos4096(sa9)*stopd/WATER_MAGIC_4096
4516 let tz9: i64 = s[S_CZ] + it_sin4096(sa9)*stopd/WATER_MAGIC_4096
4517 let sdx9: i64 = tx9 - x
4518 let sdz9: i64 = tz9 - z
4519 dx = 0
4520 if sdx9 > 128 { dx = spd }
4521 if sdx9 < 0 - 128 { dx = 0 - spd }
4522 dz = 0
4523 if sdz9 > 128 { dz = spd }
4524 if sdz9 < 0 - 128 { dz = 0 - spd }
4525 } }
4526 if twd == 1 { if pd + pdz2 <= stopd { if bst == 0 { dx = 0; dz = 0; ph = ph - 4 } } }
4527 if twd == 0 - 1 { if pd + pdz2 < stopd {
4528 dx = spd
4529 if pdx > 0 { dx = 0 - spd }
4530 dz = spd
4531 if pdz > 0 { dz = 0 - spd }
4532 } }
4533 if twd == 0 { if bst == 0 { dx = 0; dz = 0; ph = ph - 4 } } // the watcher stands
4534 } } } }
4535 // RELATIONSHIP (the FLIAW arc): shared time within arm's reach becomes FRIENDSHIP,
4536 // stored in MC_DISG bits 6-13 (cap 250) -- persisted with the mob arena, so a bond
4537 // survives every save/load. Meeting her (E) adds a step-change on top.
4538 if pd + pdz2 < 3*256 { if (s[S_T] + h) % 64 == 0 {
4539 let dgf: i64 = en_get(m, h, MC_DISG)
4540 if ((dgf >> 6) & 255) < 250 { en_set(m, h, MC_DISG, dgf + 64) }
4541 } }
4542 // Intent still turns a blocked actor away from a wall. The forward clip now
4543 // travels along that bounded facing; existing clearance admits the projected step.
4544 // Behavioral targets and turn rate remain authored by the existing owners.
4545 // Declared forward locomotion admits chase intent through the same heading,
4546 // clearance, gravity and cadence update as other behavior; no second root move.
4547 if wc_forward_policy(base,k)==1 {
4548 if en_get(m,h,MC_KIND)==2&&(en_get(m,h,MC_DISG)&1)==0 {
4549 if na_abs(pdx)+na_abs(pdz)>2*256 {
4550 let hunt:i64=wc_warden_target(base,h,night)
4551 if hunt>=0 {
4552 let target:i64=en_nth(m,hunt)
4553 let tx:i64=en_get(m,target,MC_X)
4554 let tz:i64=en_get(m,target,MC_Z)
4555 if na_abs(tx-x)+na_abs(tz-z)>=220 {
4556 var speed:i64=4
4557 if night==1 {speed=7}
4558 dx=0;dz=0
4559 if tx>x+32 {dx=speed}
4560 if tx<x-32 {dx=0-speed}
4561 if tz>z+32 {dz=speed}
4562 if tz<z-32 {dz=0-speed}
4563 }
4564 }
4565 }
4566 }
4567 }
4568 let hdgp: *i64 = (base + O_MOBHDG) as *i64
4569 hdgp[k] = wc_facing_step(hdgp[k],dx,dz,pdx,pdz,s[S_FACE_RADIUS],s[S_FACE_GAIN])
4570 let forward_step:*i64=wc_motion_step(base,k,hdgp[k],dx,dz)
4571 dx=forward_step[2];dz=forward_step[3]
4572 if wc_mob_resident(base,h)==1 {
4573 let nx: i64 = x + dx
4574 let nz: i64 = z + dz
4575 let cy0: i64 = en_get(m, h, MC_Y)
4576 let g: i64 = mob_ground(base, nx >> 8, (cy0 >> 8) + 1, nz >> 8)
4577 var ok: i64 = 0
4578 if g >= 1 { if g < WY - 4 {
4579 let ng: i64 = wc_contact_floor_q(base,nx,nz,(cy0 >> 8)+1)
4580 var dyy: i64 = ng - cy0
4581 if dyy < 0 { dyy = 0 - dyy }
4582 if dyy <= 384 { if mob_fits(base, nx >> 8, g, nz >> 8) == 1 {
4583 ok = 1
4584 // GE12b: clamp her body out of any neighbouring solid, then RE-DERIVE the
4585 // applied step from the clamped result -- the motion doors and the gait
4586 // accumulator must report what actually happened, so a girl pinned against
4587 // a wall accumulates no stride (she is not walking; she is leaning).
4588 let cxq: i64 = mob_clear_x(base, nx, nz, g)
4589 let czq: i64 = mob_clear_z(base, nx, nz, g)
4590 dx = cxq - x
4591 dz = czq - z
4592 en_set(m, h, MC_X, cxq)
4593 en_set(m, h, MC_Z, czq)
4594 // STEP, don't teleport -- and DROP, don't glide (GE12): the vertical is
4595 // integrated by mob_step_gait (stair-ascent rate up, the player's own
4596 // gravity down), so a rise reads as a step and a drop accelerates.
4597 en_set(m, h, MC_Y, mob_step_gait(vyp, k, cy0, wc_contact_floor_q(base,cxq,czq,(cy0 >> 8)+1)))
4598 } }
4599 } }
4600 let aux: *i64 = (base + O_MOBAUX) as *i64
4601 if ok == 1 { aux[k*3] = dx; aux[k*3 + 1] = dz; aux[k*3 + 2] = en_get(m, h, MC_Y) - cy0 }
4602 if ok == 0 {
4603 // wall/cliff: turn away -- but GE12 never leaves her hanging: if her own column's
4604 // stand height is below her (a block broke under her feet), she keeps falling.
4605 let g0: i64 = mob_ground(base, x >> 8, (cy0 >> 8) + 1, z >> 8)
4606 var dy0: i64 = 0
4607 let ownfloor: i64 = wc_contact_floor_q(base,x,z,(cy0 >> 8)+1)
4608 if g0 >= 1 { if ownfloor >= 0 { if ownfloor < cy0 {
4609 let ny0: i64 = mob_step_gait(vyp, k, cy0, ownfloor)
4610 en_set(m, h, MC_Y, ny0)
4611 dy0 = ny0 - cy0
4612 } } }
4613 // Rejected intent is not travelled distance; gait and footstrikes consume the applied step below.
4614 dx = 0; dz = 0
4615 aux[k*3] = 0; aux[k*3 + 1] = 0; aux[k*3 + 2] = dy0; ph = ph + 977
4616 }
4617 } else {
4618 // Keep needs, social reasoning and identity alive while collision terrain is absent.
4619 dx=0;dz=0
4620 let pending_aux: *i64=(base+O_MOBAUX) as *i64
4621 pending_aux[k*3]=0;pending_aux[k*3+1]=0;pending_aux[k*3+2]=0
4622 }
4623 // PATH LENGTH for the gait drive -- Euclidean, and deliberately NOT the Manhattan proxy
4624 // mob_spd_q8i uses: that one is a calibrated blend THRESHOLD, this is a metric integral.
4625 // Conflating them would make a diagonal walk bob up to 41 percent fast, and would also
4626 // move a threshold that was measured against the proxy. Two named quantities, one job each.
4627 let ds9: i64 = sd_isqrt(dx*dx + dz*dz)
4628 let ga9: i64 = nd[nb + WC_NEED_GAIT]
4629 nd[nb + WC_NEED_GAIT] = ga9 + ds9
4630 // FOOTSTRIKE IMPULSE (sb_impulse_mob's FIRST world wiring -- gate-proven in
4631 // nx_softbind_gate and called by NO engine until now, so heel strike put NOTHING
4632 // into the tissue: the sponsored footfall row). Two ground-contact events kick
4633 // every spring point of this girl, both magnitudes DERIVED, converted into
4634 // sd_impulse's own units by WC_IMP_SCALE:
4635 // (1) HEEL STRIKE -- the stride-phase machinery places foot plant at the
4636 // leg-swing extremes sph = IT_PI/2 and 3*IT_PI/2 (wc_gp_leg is sin(sph);
4637 // its extremes are where the leading leg lands and double support begins).
4638 // In accumulated-path units those are ga == WC_STRIDE_Q8/4 modulo
4639 // WC_STRIDE_Q8/2, so (ga + WC_STRIDE_Q8/4)/(WC_STRIDE_Q8/2) counts foot
4640 // plants and an increment across this tick's travel IS one. Magnitude: each
4641 // step the body falls the gait bob's own declared peak-to-peak
4642 // (2*WC_GAIT_BOB_Q8) onto the leading leg and contact arrests it --
4643 // ballistic contact speed isqrt(2*GRAV * 2*WC_GAIT_BOB_Q8) = 15
4644 // world-q8/tick: the engine's own gravity through the engine's own declared
4645 // vertical excursion, no free parameter. (GRAV is the engine's stylized
4646 // gravity, so this inherits the same stylization every drop landing already
4647 // has -- consistent BY CONSTRUCTION, not calibrated to real-world g.)
4648 // (2) DROP LANDING -- GE12's integrator zeroed a real fall this tick; the
4649 // arrested |vy_pre| IS the ground-reaction delta-v, bounded by VTERM.
4650 // Sign: +y, the recoil-inheritance convention every softbind gate kick uses.
4651 // A standing girl accumulates no path and keeps vy == 0, so she is kicked by
4652 // nothing -- rest stays rest (the sd_deadsnap law undisturbed).
4653 if (ga9 + ds9 + WC_STRIDE_Q8/4)/(WC_STRIDE_Q8/2) != (ga9 + WC_STRIDE_Q8/4)/(WC_STRIDE_Q8/2) {
4654 sb_impulse_mob((base + O_SOFT) as *i64, k, 0, sd_isqrt(2*GRAV*2*WC_GAIT_BOB_Q8)*WC_IMP_SCALE, 0)
4655 }
4656 if vy_pre < 0 { if vyp[k] == 0 {
4657 sb_impulse_mob((base + O_SOFT) as *i64, k, 0, (0 - vy_pre)*WC_IMP_SCALE, 0)
4658 } }
4659 en_set(m, h, MC_PH, ph)
4660 // A4: the soft-body springs tick from ROOT TRUTH + HER GAIT: the anchor carries a
4661 // cadence-coupled thorax bob (published gait biomechanics -- vertical thorax
4662 // oscillation is what drives chest motion; a flat anchor was why nothing moved),
4663 // and the chest band is BRED (genome bits 16-17: firm rides tight, soft swings).
4664 // ★THE SOLVER AND THE INSTRUMENT MUST SHARE ONE REFERENCE (fixed 2026-08-03).
4665 // This tick used to drive the spring toward a RAW gait sinusoid while sbyb and every
4666 // probe subtracted mob_anchor_q8 -- the gait<->breath crossfade. Their difference rode
4667 // the rendered offset as a spurious term, and because ph advances +5 EVERY TICK FOREVER
4668 // the raw bob never quiets, so a STANDING girl still bounced: the 2026-08-02 breath fix
4669 // landed in the INSTRUMENT path and never reached the PHYSICS path. (The comment that
4670 // stood here claimed the bob 'quiets when she stands'. It does not. A comment asserting
4671 // a behaviour the code does not have is a landmine, so it is deleted with the defect.)
4672 // Driving the solver from the SAME anchor the instrument reads makes rest-bouncing
4673 // impossible BY CONSTRUCTION rather than by tuning. At walking speed the crossfade
4674 // weight saturates and the anchor EQUALS the old raw bob exactly => no-op in motion,
4675 // fix at rest: the safest shape a drive change can have.
4676 // ⚠STILL OPEN (debt 1785770635): ph is a FREE-RUNNING clock, so the drive is a fixed
4677 // 2.50Hz regardless of her real cadence while the render clip is DISTANCE-synced. Until
4678 // ph advances from travel, foot plant and tissue drive remain uncorrelated -- which is
4679 // the operator's actual report and is NOT closed by this change.
4680 sb_tick_mob_gene((base + O_SOFT) as *i64, k,
4681 en_get(m, h, MC_X)/4, (en_get(m, h, MC_Y) + mob_anchor_q8i(base, k))/4, en_get(m, h, MC_Z)/4,
4682 en_get(m, h, MC_GENE))
4683 }
4684 k = k + 1
4685 }
4686 // THE STAKES (losability doctrine: a find that cannot be lost is hollow): WARDENS -- real
4687 // demonkin -- hunt FOUND humans. Reached = TAKEN (the certified store's destroy, live in
4688 // production). The player's BODY blocks a warden within 2 blocks. One take per tick, max.
4689 var kw: i64 = 0
4690 var took: i64 = 0
4691 while kw < en_count(m) {
4692 if took == 0 {
4693 let hw: i64 = en_nth(m, kw)
4694 if en_get(m, hw, MC_KIND) == 2 { if (en_get(m, hw, MC_DISG) & 1) == 0 {
4695 let bestj:i64=wc_warden_target(base,hw,night)
4696 var bestd:i64=0
4697 if bestj>=0 {
4698 let target:i64=en_nth(m,bestj)
4699 bestd=na_abs(en_get(m,target,MC_X)-en_get(m,hw,MC_X))+na_abs(en_get(m,target,MC_Z)-en_get(m,hw,MC_Z))
4700 }
4701 if bestj >= 0 {
4702 var pdx3: i64 = s[S_CX] - en_get(m, hw, MC_X)
4703 if pdx3 < 0 { pdx3 = 0 - pdx3 }
4704 var pdz3: i64 = s[S_CZ] - en_get(m, hw, MC_Z)
4705 if pdz3 < 0 { pdz3 = 0 - pdz3 }
4706 if pdx3 + pdz3 > 2*256 { // your presence stays her hand
4707 let hj2: i64 = en_nth(m, bestj)
4708 if bestd < 220 {
4709 // C3/C5: a TAKING is WITNESSED -- every girl within 16 blocks
4710 // carries the grief as a thought and the fact as gossip (source =
4711 // her own eyes, conf 100). The village REMEMBERS its losses.
4712 let tkx: i64 = en_get(m, hj2, MC_X)
4713 let tkz: i64 = en_get(m, hj2, MC_Z)
4714 var tkk: i64 = 0 - 1
4715 var kwi: i64 = 0
4716 while kwi < en_count(m) {
4717 if en_nth(m, kwi) == hj2 { tkk = kwi }
4718 kwi = kwi + 1
4719 }
4720 en_destroy(m, hj2)
4721 s[S_LOST] = s[S_LOST] + 1
4722 var kwj: i64 = 0
4723 while kwj < en_count(m) {
4724 let hwj: i64 = en_nth(m, kwj)
4725 if hwj > 0 {
4726 var wdx: i64 = en_get(m, hwj, MC_X) - tkx
4727 if wdx < 0 { wdx = 0 - wdx }
4728 var wdz: i64 = en_get(m, hwj, MC_Z) - tkz
4729 if wdz < 0 { wdz = 0 - wdz }
4730 if wdx + wdz < 16*256 {
4731 th_add(base, kwj, TH_SAWTAKEN, 0 - 25, 14400)
4732 if tkk >= 0 { go_add(base, kwj, tkk, GO_TAKEN, 100) }
4733 }
4734 }
4735 kwj = kwj + 1
4736 }
4737 took = 1
4738 }
4739 if bestd >= 220&&wc_forward_policy(base,kw)==0 { if took == 0 {
4740 let txq: i64 = en_get(m, hj2, MC_X)
4741 let tzq: i64 = en_get(m, hj2, MC_Z)
4742 var wv: i64 = 4
4743 if night == 1 { wv = 7 } // she closes faster in the dark
4744 var mx3: i64 = 0
4745 if txq > en_get(m, hw, MC_X) + 32 { mx3 = wv }
4746 if txq < en_get(m, hw, MC_X) - 32 { mx3 = 0 - wv }
4747 var mz3: i64 = 0
4748 if tzq > en_get(m, hw, MC_Z) + 32 { mz3 = wv }
4749 if tzq < en_get(m, hw, MC_Z) - 32 { mz3 = 0 - wv }
4750 en_set(m, hw, MC_X, en_get(m, hw, MC_X) + mx3)
4751 en_set(m, hw, MC_Z, en_get(m, hw, MC_Z) + mz3)
4752 } }
4753 }
4754 }
4755 } }
4756 }
4757 kw = kw + 1
4758 }
4759 // INHERITANCE (the past-DAZ lever, live): two MET girls keeping close company bear a
4760 // DAUGHTER -- her genome FIELD-CROSSED from both parents (the breeders gcross pattern),
4761 // spawned through the certified store (capacity-bounded), cooldown-paced. Display cap 12.
4762 if s[S_BCD] > 0 { s[S_BCD] = s[S_BCD] - 1 }
4763 if s[S_BCD] == 0 { if en_count(m) < MOB_CAP {
4764 var ka: i64 = 0
4765 var born: i64 = 0
4766 while ka < en_count(m) {
4767 if born == 0 {
4768 let ha: i64 = en_nth(m, ka)
4769 if (en_get(m, ha, MC_DISG) & 2) == 2 {
4770 var kb2: i64 = ka + 1
4771 while kb2 < en_count(m) {
4772 if born == 0 {
4773 let hb2: i64 = en_nth(m, kb2)
4774 if (en_get(m, hb2, MC_DISG) & 2) == 2 {
4775 var bdx4: i64 = en_get(m, ha, MC_X) - en_get(m, hb2, MC_X)
4776 if bdx4 < 0 { bdx4 = 0 - bdx4 }
4777 var bdz4: i64 = en_get(m, ha, MC_Z) - en_get(m, hb2, MC_Z)
4778 if bdz4 < 0 { bdz4 = 0 - bdz4 }
4779 if bdx4 + bdz4 < 400 {
4780 let hn: i64 = en_spawn(m)
4781 if hn > 0 {
4782 let ga: i64 = en_get(m, ha, MC_GENE)
4783 let gb: i64 = en_get(m, hb2, MC_GENE)
4784 let pick: i64 = whash(ga, gb, s[S_T])
4785 var gn: i64 = 0
4786 var fm: i64 = 0
4787 while fm < 8 {
4788 var msk: i64 = 7
4789 if fm == 1 { msk = 24 }
4790 if fm == 2 { msk = 96 }
4791 if fm == 3 { msk = 384 }
4792 if fm == 4 { msk = WATER_MAGIC_1536 }
4793 if fm == 5 { msk = WATER_MAGIC_6144 }
4794 if fm == 6 { msk = GENE_MASK_TEMPERAMENT } // bits 13-15: TEMPERAMENT (bold<->shy) is bred too
4795 if fm == 7 { msk = 983040 } // bits 16-19: BODY NATURE (firmness 16-17 +
4796