rimlike/scenes/entities/grave_slot.gd
megaproxy 19d28ca9f8 Phase 16: Save/load full coverage + autosave + UI
Three-agent fan-out reusing the contracts-first pattern: Opus pre-wrote
World.clear_all + 4 EventBus signals (save_started/finished, load_started/
finished) before dispatch. Pattern proven across Phases 12/13/14/15/16.

Entity to_dict/from_dict + class_id tagging (Agent A):
- class_id tag added to all 18 entity to_dict methods for loader routing
- Missing pairs filled in: wolf, grave_slot, graveyard_zone, stockpile_zone,
  crate (from_dict). All defensive with d.get(field, default).
- Workbench round-trips label_text so Carpenter/Smelter/Millstone/Hearth/
  Pyre kinds survive reload
- BeautySystem + DirtinessSystem save_dict/apply_dict for sparse maps
- World.save_tilemap_layers / apply_tilemap_layers covering 5 layers
  (Terrain/Floor/Wall/Designation/Roof; Fog runtime-only skipped)

SaveSystem v2 rewrite (Agent B):
- SAVE_VERSION bumped from 1 to 2
- write_save(slot) pauses Sim, emits save_started, collects every entity
  via _collect_entities iterating all World registries, writes payload to
  user://save_<slot>.json
- apply_save full rewrite: pause sim → emit load_started → World.clear_all
  → apply autoloads (GameState/Clock/Weather/Storyteller) → apply tilemap
  layers → iterate payload.entities and dispatch to per-class factories
  → apply beauty/dirt maps → emit load_finished(slot, ok, real_seconds_away)
- Per-class factory registry: 18 class_ids dispatched to setup+add_child+
  from_dict patterns. CremationPyre detected via workbench.label_text == 'Pyre'
- Public slot API: save_to_slot/load_from_slot/has_save/delete_save/
  peek_save_metadata. Slots locked: &manual + &autosave

Autosave + UI + Resume toast (Agent C):
- autoload/autosave.gd — new Autosave autoload. Periodic every
  AUTOSAVE_INTERVAL_TICKS = 6000 (~5 in-game min at 20 Hz) + NOTIFICATION_
  APPLICATION_PAUSED (mobile) + NOTIFICATION_WM_WINDOW_FOCUS_OUT (desktop).
  Gated by _busy flag tied to EventBus.save_started/save_finished.
- TopBar extended with SaveBtn (💾) + LoadBtn buttons, 48×48 min hit area
- scenes/ui/load_menu.gd — CanvasLayer slot picker. Reads peek_save_metadata
  to show 'Manual save (Date Time)' / 'Autosave (Date Time)' rows.
  Version-mismatch warning dialog before continuing on older saves.
- scenes/ui/resume_toast.gd — top-center toast. On load_finished(ok=true):
  'Welcome back — N minutes/hours away' for 5s + 0.8s fade.
  On ok=false: 'Load failed (corrupt or version mismatch)'.
- Strings catalog: 14 new keys (ui.save / ui.load / ui.welcome_back_* /
  ui.load_failed etc.)
- main.gd mounts LoadMenu + ResumeToast as runtime CanvasLayer children

MCP runtime verified:
- Saved at tick 1137 → [save] wrote slot 'manual': 113 entities at tick 1137
- Advanced sim to tick 4600 at ULTRA speed (different state)
- load_from_slot(&manual) → [save] applied slot 'manual': 113 entities,
  0 errors, tick=1137, away=34s
- post-load: Sim.tick=1137 (restored), pawns alive=3, all furniture +
  workbenches + crops + walls + floors back in place
- Resume toast fires: [resume_toast] showing — ok=true seconds_away=34
- Autosave on focus-loss verified: [autosave] focus-loss → wrote autosave
- Screenshot shows TopBar with Save + Load buttons + post-load Lone Wolf
  storyteller modal from fresh dawn roll

Known acceptable gaps (deferred to Phase 20 tuning):
- Pawn JobRunner mid-INTERACT/mid-BUILD restarts from toil 0 on reload
  (walk toil round-trips; multi-step interact does not). Pawns lose a few
  seconds of work.
- Workbench bill mid-craft fetch state isn't fully serialized.
- Wolf.target_pawn re-resolution from name string is Agent A's documented
  pattern; Agent B's apply_save respects pawn-restoration ordering so the
  resolution works after pawns are back.

Delegation: 3× gdscript-refactor (Sonnet) agents in parallel; integration
+ MCP verify on Opus.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-11 19:24:59 +01:00

227 lines
8.3 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name GraveSlot extends Node2D
## Phase 14 — per-tile grave slot entity. Spawned by the designation system when
## the player paints TOOL_DIG_GRAVE. Progresses through a two-state build model:
##
## ghost (BUILD_TICKS not yet reached)
## ↓ ConstructionProvider builds it via on_build_tick()
## dug (open hole, ready for a corpse)
## ↓ HaulingProvider routes a corpse here; accept_corpse() is called
## (GraveSlot queue_free-s itself; GraveMarker spawned at same tile)
##
## Build contract: same duck-type interface as Wall / Floor / Door so that
## ConstructionProvider's untyped iteration in World.build_queue picks it up
## automatically without any dispatch addition.
## site.tile: Vector2i
## site.is_buildable() -> bool
## site.label() -> String
## site.get_path() -> NodePath
## site.blocks_pathing_when_complete() -> bool (returns false — grave stays walkable)
##
## StorageDestination duck-type interface (registered in World.stockpiles):
## accepts(item) -> bool
## find_drop_position(item) -> Vector2i
## covers_tile(tile) -> bool
## is_grave_slot_dug() -> bool (queried by GraveyardZone._find_open_grave_slot)
##
## See docs/implementation.md Phase 14 "GraveSlot".
const TILE_SIZE_PX: int = 16
## Sim ticks to dig the grave (80 ticks ≈ 4 s at 1×).
const BUILD_TICKS: int = 80
## Visual colours.
const _GHOST_FILL := Color(0.38, 0.28, 0.16, 0.35) # pale dirt, transparent
const _DUG_FILL := Color(0.28, 0.18, 0.08, 0.85) # dark open earth
const _OUTLINE := Color(0.18, 0.12, 0.06, 0.80)
## Tile position.
@export var tile: Vector2i = Vector2i.ZERO
var build_progress: int = 0
var _dug: bool = false # true once BUILD_TICKS reached (ghost→dug transition)
# ── lifecycle ─────────────────────────────────────────────────────────────────
func _ready() -> void:
# Register as a build site so ConstructionProvider picks it up.
World.register_build_site(self)
# Register as a stockpile destination so HaulingProvider can route corpses here.
World.register_stockpile(self)
z_index = 1 # above terrain, below items
func _exit_tree() -> void:
World.unregister_build_site(self)
World.unregister_stockpile(self)
# ── public setup ──────────────────────────────────────────────────────────────
## One-shot initialiser. Call after add_child(). Sets tile + world position.
func setup(p_tile: Vector2i) -> void:
tile = p_tile
# Centre-align within the tile (same convention as Corpse, Item).
global_position = Vector2(
tile.x * TILE_SIZE_PX + TILE_SIZE_PX / 2.0,
tile.y * TILE_SIZE_PX + TILE_SIZE_PX / 2.0
)
queue_redraw()
Audit.log("grave_slot", "ghost placed at %s" % tile)
# ── ConstructionProvider duck-type interface ──────────────────────────────────
## True while the slot still needs digging. ConstructionProvider checks this.
func is_buildable() -> bool:
return not _dug
## GraveSlot stays walkable after being dug — the pawn stands on it to deposit.
func blocks_pathing_when_complete() -> bool:
return false
## Human-readable job label used by ConstructionProvider and Audit logs.
func label() -> String:
return "grave"
## Called by the BUILD toil in JobRunner once per sim tick while the pawn digs.
func on_build_tick() -> void:
if _dug:
return
build_progress += 1
queue_redraw()
if build_progress >= BUILD_TICKS:
_complete_dig()
## True once the slot has finished the ghost-phase (for World._on_designation_cleared).
func is_completed() -> bool:
return _dug
# ── StorageDestination duck-type interface ────────────────────────────────────
## Returns true if this slot is dug (open) and ready to accept a corpse.
## GraveyardZone calls this to verify an open slot exists in the region.
func is_grave_slot_dug() -> bool:
return _dug
## Returns true if `item` is a Corpse entity AND this slot is dug.
func accepts(item) -> bool:
if not _dug:
return false
return item.has_method("get_item_type") and item.get_item_type() == &"corpse"
## Returns the slot's own tile if it is dug and available; Vector2i(-1,-1) otherwise.
func find_drop_position(item) -> Vector2i:
if not accepts(item):
return Vector2i(-1, -1)
return tile
## True when `p_tile` matches this slot's tile.
func covers_tile(p_tile: Vector2i) -> bool:
return p_tile == tile
## Priority mirrors StorageDestination enum so HaulingProvider's priority
## comparison works cleanly. NORMAL for grave slots.
var priority: int = StorageDestination.Priority.NORMAL
# ── burial ─────────────────────────────────────────────────────────────────────
## Called by JobRunner._tick_deposit_corpse when a pawn has carried a corpse
## here and is standing on this tile. Instantiates a GraveMarker at this tile,
## emits EventBus.corpse_buried, then frees both the corpse and this slot.
func accept_corpse(corpse, world_parent: Node) -> void:
if not _dug:
Audit.log("grave_slot", "accept_corpse called before slot is dug at %s — ignoring" % tile)
return
Audit.log("grave_slot", "burying '%s' at %s" % [corpse.deceased_name, tile])
# Spawn permanent GraveMarker.
var marker_script: Script = load("res://scenes/entities/grave_marker.gd")
var marker := Node2D.new()
marker.set_script(marker_script)
marker.name = "GraveMarker_%s_%s" % [tile.x, tile.y]
world_parent.add_child(marker)
marker.setup(tile, corpse.deceased_name, corpse.deceased_portrait_color,
corpse.death_cause, corpse.death_tick)
Audit.log("grave_slot", "marker placed for '%s' (cause=%s death_tick=%d)" % [
corpse.deceased_name, corpse.death_cause, corpse.death_tick
])
EventBus.corpse_buried.emit(corpse, marker)
# Free the corpse and the slot — marker persists.
corpse.queue_free()
queue_free()
# ── visual ────────────────────────────────────────────────────────────────────
func _draw() -> void:
# Drawn centred in tile (origin = tile centre).
if _dug:
_draw_open_grave()
else:
_draw_ghost()
func _draw_ghost() -> void:
# Transparent dashed outline: dug-grave shape as a ghost.
draw_rect(Rect2(-6.0, -4.0, 12.0, 8.0), _GHOST_FILL, true)
draw_rect(Rect2(-6.0, -4.0, 12.0, 8.0), Color(_OUTLINE.r, _OUTLINE.g, _OUTLINE.b, 0.40), false, 1.0)
# Progress bar — thin strip across bottom.
if BUILD_TICKS > 0 and build_progress > 0:
var pct := float(build_progress) / float(BUILD_TICKS)
var bar_w := 12.0 * pct
draw_rect(Rect2(-6.0, 3.0, bar_w, 1.5), Color(0.6, 0.5, 0.3, 0.6), true)
func _draw_open_grave() -> void:
# Dark open pit: recessed rectangle to suggest depth.
draw_rect(Rect2(-6.0, -4.0, 12.0, 8.0), _DUG_FILL, true)
# Lighter near-edge to suggest rim of earth.
draw_rect(Rect2(-6.0, -4.0, 12.0, 2.0), Color(0.45, 0.33, 0.18, 0.70), true)
draw_rect(Rect2(-6.0, -4.0, 12.0, 8.0), _OUTLINE, false, 1.0)
# ── save / load ───────────────────────────────────────────────────────────────
func to_dict() -> Dictionary:
return {
"class_id": &"grave_slot",
"tile_x": tile.x,
"tile_y": tile.y,
"build_progress": build_progress,
"dug": _dug,
}
func from_dict(d: Dictionary) -> void:
tile = Vector2i(int(d.get("tile_x", 0)), int(d.get("tile_y", 0)))
build_progress = int(d.get("build_progress", 0))
_dug = bool(d.get("dug", false))
global_position = Vector2(
tile.x * TILE_SIZE_PX + TILE_SIZE_PX / 2.0,
tile.y * TILE_SIZE_PX + TILE_SIZE_PX / 2.0
)
queue_redraw()
# ── internal ─────────────────────────────────────────────────────────────────
func _complete_dig() -> void:
_dug = true
queue_redraw()
Audit.log("grave_slot", "grave dug at %s (ready for burial)" % tile)