fix(world): build per-zone Lua VM in the zone process#56
Merged
Conversation
Implement the new asobi_world init_zone_state/2 and dump_zone_state/1 callbacks. Each zone now builds its own Luerl VM from the script in its own process, bound to the zone pid, so game.zone.spawn (and zone-based game.spatial / game.terrain) resolve on lazy and snapshot-recovered zones, not just pre-spawned ones. - init_zone_state/2 constructs the VM with zone_pid => self() and match_pid => world_server_pid, and re-encodes gameplay state restored from a snapshot. The VM is never persisted, only rebuilt here. - dump_zone_state/1 decodes the Luerl game_state ref to a plain map for jsonb (null when never seeded, so a script's `game_state == nil` guard still fires after recovery) and drops the VM. generate_world no longer injects per-zone VMs; it returns plain zone states and each zone builds its own. Requires asobi with init_zone_state/2 + dump_zone_state/1 (widgrensit/asobi#131); the asobi pin bump follows once that merges.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
game.zone.spawn(and zone-basedgame.spatial/game.terrain) silently did nothing from Lua on lazy and snapshot-recovered zones - in fact all per-zone Lua (zone_tick,handle_input) was dead there. The per-zone Luerl VM was only ever stitched in on the pre-spawned path, and a VM (opaque closures) can't survive a jsonb snapshot. Large worlds (grid_size > 100defaults to lazy) and persistent worlds were affected.Fix
Implements the new optional
asobi_worldcallbacks from widgrensit/asobi#131:init_zone_state/2builds the zone's Luerl VM in the zone process (handle_continue), so it bindszone_pid => self()andmatch_pid => world_server_pid. Covers every creation path uniformly - pre-spawned, lazy, recovered. Re-encodes gameplay state restored from a snapshot.dump_zone_state/1decodes the Luerlgame_stateref to a plain, jsonb-safe map (ornullwhen never seeded, so a script'sgame_state == nilinit guard still fires after recovery) and drops the VM.generate_worldno longer injects per-zone VMs; it returns plain zone states and each zone builds its own.Depends on widgrensit/asobi#131 (merged); pinned via
rebar.lock.Tests
New
asobi_lua_zone_spawn_tests:init_zone_statebuilds a VM from an empty (lazy) zone_state.dump_zone_state-> jsonb ->init_zone_state.nil, not an empty table.dump_zone_statewithout a VM stays jsonb-safe (degraded path).game.zone.spawnfrom Lua reaches a liveasobi_zonebuilt purely viahandle_continue.Existing world/resource-limit/property suites migrated to the new
init_zone_state/2contract. Full suite: 223 eunit, 0 failures.Follow-up (separate)
widgrensit/asobi#132 - lazy-reaped zones don't reload their DB snapshot on respawn, so their gameplay state doesn't round-trip yet (eager persistent worlds do).