asobi_lua_world (asobi v0.72.5)
View SourceAn asobi_world implementation that delegates all callbacks to Lua scripts
via Luerl.
The Lua script must define these functions:
function init(config) -- return initial game state
function join(player_id, state, ctx) -- ctx is the client join context
function leave(player_id, state) -- return updated state
function spawn_position(player_id, state) -- return {x, y}
function zone_tick(entities, zone_state) -- return entities, zone_state
function handle_input(player_id, input, entities) -- return entities
function post_tick(tick, state) -- return state (or state + vote/finished)
-- Optional:
function generate_world(seed, config) -- return zone_states table
function get_state(player_id, state) -- return state visible to player
function phases(config) -- return list of phase definitions
function on_phase_started(phase_name, state) -- return updated state
function on_phase_ended(phase_name, state) -- return updated state
function spawn_templates(config) -- return template registry table
function on_world_recovered(snapshots, state) -- return updated state
function terrain_provider(config) -- return {module, args} or nil
function on_zone_loaded(cx, cy, state) -- return zone_state, state
function on_zone_unloaded(cx, cy, state) -- return statevote_resolved is deliberately absent. asobi_world_server reaches it through
erlang:function_exported/3 and this module does not export it, so a Lua world
script defining vote_resolved is never called. Match scripts are unaffected;
see guides/voting.md.
After a hot reload (ASOBI_LUA_RELOAD/reload_mode), spawn_templates_hint/1
tells the running zone to re-fetch spawn_templates(config) so an edited
script's new templates become spawnable without restarting the zone. The
game.zone.spawn guard reads that live set from a per-zone ETS table rather
than a value snapshotted once into Ctx, so this stays correct across any number
of hot reloads.
Summary
Functions
Join carrying the client-supplied join context (asobi's optional join/3).
Passed to the Lua join as a third argument: function join(player_id, state) keeps working (Lua discards extra arguments) and
function join(player_id, state, ctx) receives it.
-spec spawn_templates(map()) -> #{binary() => asobi_zone_spawner:spawn_template()}.
-spec spawn_templates_hint(map()) -> unchanged | {changed, #{binary() => asobi_zone_spawner:spawn_template()}}.