code wiki / _hdl_build / nx_wasm_craft_journal_index_t21_before_t44.nx

nx_wasm_craft_journal_index_t21_before_t44.nx source

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