Skip to content

Persist reserved_resources in save data #231

Description

@itsmiso-ai

Problem

reserve_resource() and release_resource() in scripts/main.gd (lines 2502, 2508) mutate state.reserved_resources, but persist() (line 2439) does not include it in the save payload:

func persist() -> void:
    # ... saves resources, builds, workers, goals, etc.
    # reserved_resources is NEVER written to the save
    GameState.save_game(state)

Wait — actually persist() calls GameState.save_game(state) which serializes the entire state dict. But reserved_resources is initialized lazily in reserve_resource():

func reserve_resource(resource: String, amount: int = 1) -> void:
    if not state.has("reserved_resources"):
        state["reserved_resources"] = {}
    # ...

The issue is that _clean_stale_reservations() (line 1518) runs every tick and can reset reserved_resources. If the game saves mid-tick (after cleanup but before reservation is re-established), the saved state may have stale or empty reservations.

More critically: on load, if reserved_resources is missing from the save (old saves), it defaults to {}. Any in-flight reservations from the previous session are lost, which means two workers could be assigned to the same resource tile because the reservation count is zero.

Fix

  1. Ensure reserved_resources is explicitly included in the persist payload
  2. On load (bootstrap_state or similar), validate that reserved_resources exists and re-sync it by scanning active worker tasks
  3. Add a test that saves with active reservations, loads, and verifies the reservations persist

Acceptance criteria

  • reserved_resources survives save/load cycle
  • On load, reservations are re-synced from active worker tasks
  • Test added to tests/test_reservations.gd covering save/load
  • No double-booking after reload

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions