aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


docs/world-state.md (13258B)


World storage

The server stores mutable game state in data/world.sqlite. Static catalogs, NPC definitions, terrain, maps and client assets remain embedded resources. The database contains:

All mutable state uses native SQL scalar columns and related rows. There are no YAML/JSON state blobs or earlier-format readers. Item identity and placement use relational columns: foreign keys reject missing owners or instances, the item ID allows only one location, and unique indexes reject two items in an occupied slot. Backpacks cannot contain backpacks.

The connection uses WAL, full synchronous commits and foreign keys. Inventory changes, corpse loot transfers and cash transfers commit atomically before success is published. Profiles and admin clock changes save immediately. Player movement and routine vitals save at staggered 20-second intervals. The one-second world checkpoint does not rewrite player resources, inventory metadata, learned programs or skill locks. Inventory actions still save their significant changes immediately. Inventory metadata belongs to the acting player, so another player's expired-cooldown notification cannot be folded into the transaction. Inventory transactions update credits only when they change. Skill locks save with the action that starts them, independently of inventory metadata; expiry notifications only update memory. Learned programs retain existing members of the set. Removed programs are deleted only for their owning player. These transactions do not checkpoint unrelated players' routine vitals. An equipment change that limits current health or nano commits that clamp with ownership; explicit character edits and full world flushes retain their resource saves. Consumable resource effects include a recipient vital update in that same inventory transaction. The charge, source/patient cooldown and restored health or nano commit before success reaches the client, including when the last charge destroys its item instance. A missing or ignored recipient write fails the transaction. Only the recipient's vital columns are written; positions keep their normal checkpoint cadence. The vital cache changes only after commit. Completed Winter's Bite casts commit the caster's nano/recharge and the NPC's health, contributors and death/respawn state together before releasing the native result. Caster updates touch only nano and nano recharge; routine health and position remain dirty until their checkpoint. Failed or ignored caster writes and failed commits publish no damage. Available nano and the internal nano_reserved amount commit together at admission; checkpoints preserve both. Cancellation clears the reservation with its capped refund before success, and completion clears it with the damage outcome. Startup refunds abandoned reservations after equipment is loaded. Failed refunds remain blocked and retry without damage or a false cancellation acknowledgement. Explicit actor/resource replacements settle the reservation in their own transaction. See nano outcome persistence. The world header is updated only when its counters, clock or population-initialization state change. NPC state is checked on the one-second world tick, but routine writes occur 20-30 seconds apart, with fresh jitter after each checkpoint, only for NPCs within 200 metres horizontally of a ready, non-loading player in the same playfield, or within a fifteen-minute grace period after the last nearby player leaves. Roaming, pursuit, returns, recovery and periodic effects use the same activity gate. Grace expiry triggers one final snapshot; an occupied zone does not activate its entire population. The activity deadline lives in memory; after a restart, NPCs wait for a nearby player before resuming. Changed character and NPC metadata rows are batched in groups of up to 100 per statement in one transaction. Unchanged tables and contribution records are skipped; snapshots and deadlines publish only after commit. Corpse ownership and cash changes save immediately with their loot transaction. A new database takes one initial population snapshot. SIGINT/SIGTERM flush pending state before closing the database. A crash can lose simulation changes since the relevant checkpoint. Network sessions and in-progress client requests are recreated on connection.

Shutdown holds the simulation lock during the final flush, then retains the state lock through database close and process exit. Periodic checkpoints and other state writers cannot enter the closed database. Two SIGTERM/restart checks with the normal HTTPS preview flushed movement that was still unsaved before each signal, preserved every item placement and reconnected the WASM player at the saved position without late database-close errors. The full Go suite passed; the checks are retained in local/shutdown-smoke/verification.json.

Restarts restore NPC instances rather than adding a new copy of the static population. Only a new database seeds NPCs from the static definitions. The world clock continues advancing across downtime. Reconnecting chat clients receive their latest 500 history entries, in chronological order.

The character control page presents one YAML editing document for profile, position, vitals, inventory and top-level equipment. Appearance has separate race, fatness, breed, sex, side and head fields, with comments explaining native codes. Every skill and empty slot is visible. Equipment uses names such as clothes_left_arm and implants_left_arm; ordinary inventory uses inventory_slot_1 through inventory_slot_30. Backpack item pages use slot_1 through slot_21. Below the editor, inventory and equipment tables show every slot, including empty positions, with item names and links to exact instances. Reclaim items have their own table. Owned backpacks have contents tables, even in reclaim. Backpack item pages retain their editable YAML and show the same readable contents. Contained-item pages link to the parent backpack and owning character. Saving the character document commits all sections and item placements in one transaction. YAML is only an editing format; the server does not read or write mutable YAML files.

Startup upgrades the world database in place before accepting connections. SQLite's PRAGMA user_version records the last applied migration. Version zero covers new and unversioned worlds. Add each schema change as the next numbered SQL file in server/migrations/ and keep applied scripts and the unversioned server/world.sql baseline intact. Schema changes and their version commit in the same startup transaction. A failed migration rolls back and a newer database version stops startup. To reset the world, stop the server and delete data/world.sqlite and any matching -wal/-shm files before restarting. The server uses the pure-Go SQLite driver and builds with CGO_ENABLED=0.

Editor checks cover separate inventory/equipment slot validation, missing and duplicate instances, incompatible equipment, ownership, explicit unassignment, reclaim contents and transaction rollback across all document sections. The full Go suite and focused control/item/database/position/backpack race checks pass. Browser form and link checks exercised a 28-instance inventory, including a backpack containing twelve items; a separate full armor suit showed all seven worn pieces. Saving a helmet to equipment appeared in the WASM client, and restoring the document while connected reloaded it into ordinary inventory. All 28 records and locations were unchanged after that round-trip, and all sixteen native top-level entries matched SQLite before and after reconnect. No page or adapter errors occurred. The checks and screenshots are retained in local/control-inventory-smoke/verification.json and its sibling files.

Checkpoint write audits use SQL triggers across characters, players, items, locations, learned programs, treatment locks, NPCs/contributors and the world header. server/checkpoint_test.go verifies zero writes over an unchanged simulated minute, player position/health/nano saved only at their deadline, rollback and retry after a failed save, one initial population snapshot, and immediate charge/cooldown persistence after a routine checkpoint. Existing corpse/container tests verify that periodic expiry cleanup still retains or removes the appropriate contents.

On the normal HTTPS preview with 23 stored players, a five-second idle sample fell from 235 row writes to zero. A separate five-second idle observation left the WAL file's size and modification time unchanged. In a hardware-rendered WASM session, movement remained in memory for about 15 seconds until its scheduled checkpoint; two player snapshots were 20.016 seconds apart. The pistol's exact instance moved from right-hand equipment to inventory and back through native actions, keeping its 18 loaded rounds and the separate 100-round stack through reconnect. The fixture profile and all 192 item records and placements were restored. No page errors occurred. The full server suite and focused race checks pass. Evidence is retained in local/checkpoint-churn-research/.

server/inventory_write_test.go audits writes made by real item moves, editor transactions and treatment-kit use. It verifies that unrelated player HP/nano and expired cooldown notifications remain outside another player's item save; unrelated insert/update failures cannot block that save; retained programs and locks do not churn; additions, deadline changes and removals persist; and a late failure rolls back metadata with ownership before a successful retry. The resource-removal tests also retain atomic equipment clamps and failed-save resource/bonus conservation.

The published WASM client exercised the final implementation with temporary insert/update guards on another fixture visitor. A forced pistol-placement failure committed no rows and retained its native equipped identity. After removing that failure, the same pistol moved to inventory and back to the right hand, retaining 18 loaded rounds and the separate 100-round ammunition stack through reconnect. SQL recorded only its four location deltas for those actions and one separate periodic checkpoint for the actor. There were no unrelated player/metadata/item writes or page errors. Both fixture profiles and all 192 item records and placements were restored; the guards and audit table were removed. The final full Go suite and focused race checks pass. Evidence is in local/inventory-write-research/, with the final native run in final/.

server/consumable_outcome_test.go invokes public item use and reopens SQLite before a checkpoint. It covers self/other-player resource restoration, a DB read during the first outgoing success packet, last-charge deletion, failed charge/lock/resource writes, ignored recipient writes, failed commit, retry, resource caps, unsaved movement, and output failure followed by restart/replay. The cache, inventory, cooldown and recipient resources change together.

Two published WASM clients exercised both kits on a native teammate. The first received success already had recipient nano 11, a decremented kit and the source cooldown in SQLite. The treatment success already had recipient health 54, its charge debit and patient lock. Both clients showed those resource values; ordinary regeneration explains the subsequent increases. After an ungraceful server stop, SQLite and both reconnected clients agreed on 215 HP, 111 nano and two charges remaining in each fixture kit. Unteamed use was also rejected by the native client without a server request or debit. No page or adapter errors occurred. Both profiles, their original item records/placements and receipts were restored, and the fixture items/team were removed. The full server suite and focused race checks pass. Evidence is retained in local/consumable-outcome-research/. The apparent saved/native posture mismatch on entry was a pending checkpoint: entry already resets live posture to standing. Connection regressions now verify standing treatment rejection before that checkpoint, and sitting is included in late observers' actor snapshots. See posture and recovery.