rimlike/autoload/world.gd
megaproxy 9cf9b7dbfd Phase 13: Rooms + Auto-roof + Beauty + Dirtiness + Cleaning
Three-agent fan-out — Opus pre-wrote Room class, World.rooms/room_at_tile/is_indoor,
4 EventBus signals before dispatch so the slices ran fully parallel.

DECISION: Big-room UX = bump auto-roof cap to 16, banner above. Cabin
(24 tiles) intentionally exceeds cap to exercise the warning path; a
5×5 test shed (9 interior tiles) was added to exercise the roof path.

Room detection (Agent A):
- scenes/world/room.gd — class_name Room, tiles/bounds/is_under_roof,
  contains_tile() bounds-then-list-checked, recompute_bounds()
- scenes/world/room_detector.gd — class_name RoomDetector, BFS 4-dir
  from floor/door tiles, walls/terrain as boundary, doors counted as
  room interior. Detects up to 4× cap; auto-roofs only ≤16.
- World.mark_wall_tile/mark_floor_tile/mark_door_tile hook BFS recompute
- Door._complete() now erases wall-layer stamp + registers door tile
- Designation.TOOL_NO_ROOF paint mode wired (UI button deferred Phase 17)
- EventBus.room_changed / room_too_large signals

Indoor/Shelter (Agent B):
- Pawn._is_sheltered() rerouted: World.is_indoor() first, floor-proxy fallback
- IndoorTintOverlay Node2D — _draw fills roofed-room tiles at α=0.10 warm
- Crop._on_sim_tick skips stage advance when World.is_indoor(tile)

Beauty + Dirtiness + Cleaning + Room thoughts (Agent C):
- BeautySystem sparse map, linear falloff radius=3, Quality multiplier
  (SHODDY 0.5 → LEGENDARY 2.5). Base: Bed +2, Workbench +1, Torch +3, Hearth +4
- DirtinessSystem 0-100, tier crossings (clean<25/dirty<60/filthy≥60)
  emit tile_dirtiness_changed. bump/bump_clean/bump_pawn_traffic API
- CleaningProvider priority=2, KIND_CLEAN toil, 2.5 dirt/tick for ~40 ticks
- Bed/Torch/Workbench _complete() now register with BeautySystem
- 7 room mood thoughts: clean_room (+2), dirty_room (-3), filthy_room (-6),
  beautiful_room (+4), ugly_room (-3), slept_in_room (+3 EVENT, wires Ph 17),
  ate_without_table (-3 EVENT, wires Ph 17)
- Pawn._sync_room_thoughts called from _process_thoughts after cold block,
  defensive against null rooms/systems

Integration recovery (Opus):
- Agent C's BeautySystem/DirtinessSystem/CleaningProvider/IndoorTintOverlay
  instantiation in world.gd never landed (only field declarations + entity
  hooks survived). Added preloads + runtime add_child + autoload bindings +
  CleaningProvider registration + furniture pre-seed in _ready
- Added _prestamp_test_shed_for_room_detector with _spawn_complete_wall/floor
  helpers so a 5×5 visible shed exercises the auto-roof path at boot

MCP runtime verified:
- Rooms: cabin Room#2 size=24 roofed=false (room_too_large fires),
  shed Room#3 size=9 roofed=true (auto-roof active)
- beauty_map size=50 around prebuilt furniture; bed at (47,24) beauty=4.0
- Bram teleported to (36, 25) in shed → indoor=true, sheltered=true,
  thoughts=[clean_room +2], mood=52.0
- Screenshot: shed walls + brown floor visible; cabin warmly torch-lit;
  Spring 1/12 indicator; Day 1 07:52

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

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

360 lines
12 KiB
GDScript

extends Node
## Runtime entity registry + tile-related sim state.
##
## All gameplay entities (pawns, items, furniture, animals, corpses) live here.
## TileMap data is owned by the world-view scene; World holds the *indirect*
## state (designation queue, dirty-haul set, zone records, etc.) that doesn't
## belong on the TileMap itself.
##
## See docs/architecture.md.
# Phase 2 — pawn registry. items/furniture/animals/corpses arrive in later phases.
var pawns: Array[Pawn] = []
# Phase 3 — work providers (e.g. RestProvider, ChopProvider, HaulingProvider).
# World scene registers them on _ready. Decision.pick_next_job() iterates by .priority desc.
var work_providers: Array = []
# Phase 4 — harvestables + items + stockpiles. Entities call register_*/unregister_*
# from their _ready/_exit_tree. Phase 16 will add stable IDs and persistence wiring.
var trees: Array = [] # Array of Tree
var rocks: Array = [] # Array of Rock
var items: Array = [] # Array of Item (on-floor stacks)
var stockpiles: Array = [] # Array of StorageDestination (StockpileZone for now; containers Phase 5)
# Phase 4 — pathfinder reference exposed for entity code that needs walkability
# checks (e.g. Tree.fell() picking neighbour tiles for wood drops). The actual
# Pathfinder node lives on the World scene as a child; the scene sets this in
# its _ready(). Don't access before the world scene is mounted.
var pathfinder = null
# Phase 5 — build queue. Holds Wall/Floor/Door/Crate ghost entities (not yet
# completed). ConstructionProvider iterates this for the nearest buildable site.
# Entities call register_build_site() in _ready and unregister_build_site() when
# they finish or are cancelled.
var build_queue: Array = []
# Phase 5 — completed Door entities, keyed for future open/close logic.
# Door._complete() calls register_door(); Phase 7+ uses this for toggling.
var doors: Array = []
# Phase 6 — workbench entities. Workbench._ready() calls register_workbench();
# _exit_tree() calls unregister_workbench(). CraftingProvider iterates this
# to find bench+bill pairs for eligible pawns.
var workbenches: Array = []
# Phase 7 — crop entities. Crop._ready() calls register_crop();
# _exit_tree() calls unregister_crop(). PlantProvider iterates this to find
# harvestable (READY) and sowable (TILLED) crops for eligible pawns.
var crops: Array = []
# Phase 8 — bed entities. Bed._ready() calls register_bed();
# _exit_tree() calls unregister_bed(). SleepProvider iterates this to find
# available (completed, unoccupied) beds for tired pawns.
# Storyteller also reads beds.size() for the "First Beds" state predicate.
var beds: Array = []
# Phase 11 — light-source entities (Torch + Hearth workbench). Entities call
# register_light_source() in _ready and unregister_light_source() in _exit_tree.
# is_tile_lit() is queried by the "in darkness" thought and any future
# darkness-rendering shader bridge. All entries expose the duck-type interface:
# is_on() → bool | get_light_tile() → Vector2i | get_light_radius() → int
var light_sources: Array = []
# Phase 10 — wolf entities. Wolf._ready() calls register_wolf();
# Wolf._exit_tree() calls unregister_wolf(). WolfSpawner reads/writes
# nothing from this array directly — it only add_child()s new wolves.
# CombatSystem (Phase 10) will iterate this for threat detection.
# Untyped array — avoids class_name ordering window (Phase 2 gotcha).
var wolves: Array = []
# Phase 4 — hauling dirty set. Keys are Items, value is unused (we just use .keys()).
# An Item is added when it spawns (Tree.fell, Rock.mined, workbench drop, ...)
# and removed when it lands at its highest-priority valid destination.
# HaulingProvider.sweep_for_better_destinations() re-marks items when a higher
# priority stockpile opens up (the priority cascade per design.md).
var items_needing_haul: Dictionary = {}
func register_work_provider(wp) -> void:
assert(wp != null, "World.register_work_provider: provider is null")
if not work_providers.has(wp):
work_providers.append(wp)
func clear_work_providers() -> void:
work_providers.clear()
func register_pawn(p: Pawn) -> void:
assert(p != null, "World.register_pawn: pawn is null")
if pawns.has(p):
return
pawns.append(p)
func unregister_pawn(p: Pawn) -> void:
pawns.erase(p)
func pawn_at_tile(tile: Vector2i) -> Pawn:
for p in pawns:
if p.tile == tile:
return p
return null
func clear_pawns() -> void:
# For save-load / new-game flow in Phase 16.
pawns.clear()
# ── Phase 4: harvestables + items + stockpiles ──────────────────────────────
func register_tree(t) -> void:
if not trees.has(t):
trees.append(t)
func unregister_tree(t) -> void:
trees.erase(t)
func register_rock(r) -> void:
if not rocks.has(r):
rocks.append(r)
func unregister_rock(r) -> void:
rocks.erase(r)
func register_item(it) -> void:
if items.has(it):
return
items.append(it)
# Newly-spawned items always start as "needs haul" — HaulingProvider will
# clear the flag once the item lands in its highest-priority destination.
items_needing_haul[it] = true
func unregister_item(it) -> void:
items.erase(it)
items_needing_haul.erase(it)
func register_stockpile(s) -> void:
if not stockpiles.has(s):
stockpiles.append(s)
func unregister_stockpile(s) -> void:
stockpiles.erase(s)
func mark_item_needs_haul(it) -> void:
items_needing_haul[it] = true
func clear_item_haul_flag(it) -> void:
items_needing_haul.erase(it)
# ── Phase 5: build queue + tile-data stamping for walls / floors ────────────
func register_build_site(entity) -> void:
if not build_queue.has(entity):
build_queue.append(entity)
func unregister_build_site(entity) -> void:
build_queue.erase(entity)
func register_door(d) -> void:
if not doors.has(d):
doors.append(d)
func unregister_door(d) -> void:
doors.erase(d)
func register_workbench(wb) -> void:
if not workbenches.has(wb):
workbenches.append(wb)
func unregister_workbench(wb) -> void:
workbenches.erase(wb)
func register_crop(c) -> void:
if not crops.has(c):
crops.append(c)
func unregister_crop(c) -> void:
crops.erase(c)
func register_bed(b) -> void:
if not beds.has(b):
beds.append(b)
func unregister_bed(b) -> void:
beds.erase(b)
# ── Phase 11: light-source registry ────────────────────────────────────────
func register_light_source(ls) -> void:
if not light_sources.has(ls):
light_sources.append(ls)
func unregister_light_source(ls) -> void:
light_sources.erase(ls)
# ── Phase 10: wolf registry ────────────────────────────────────────────────
func register_wolf(w) -> void:
if not wolves.has(w):
wolves.append(w)
func unregister_wolf(w) -> void:
wolves.erase(w)
## Returns true if `tile` is within get_light_radius() of any is_on() light
## source. Uses Manhattan distance (no wall-occlusion in Phase 11; Phase 13
## may add BFS-based occlusion through the room/roof system).
##
## Called by the "in darkness" Thought trigger on each pawn sim tick.
## O(light_sources) per call; trivial at our scale (< 50 sources in MVP).
func is_tile_lit(p_tile: Vector2i) -> bool:
for ls in light_sources:
if not ls.is_on():
continue
var d: int = abs(ls.get_light_tile().x - p_tile.x) + abs(ls.get_light_tile().y - p_tile.y)
if d <= ls.get_light_radius():
return true
return false
# Called by Wall.on_build_tick() when construction completes.
# Stamps the data-only Wall TileMap layer so room/roof/save logic sees the
# wall. World scene exposes wall_layer via a getter set during _ready.
var wall_layer = null
var floor_layer = null
var designation_layer = null
func mark_wall_tile(tile: Vector2i, material: StringName) -> void:
if wall_layer == null:
Audit.log("world", "mark_wall_tile: layer not yet wired — skipping")
return
# Atlas coord encodes material — for Phase 5 placeholder atlas:
# stone → (2, 0), dark stone → (3, 0)
# Real material→atlas mapping lands when assets are imported.
var atlas := Vector2i(2, 0) if material == &"stone" else Vector2i(3, 0)
wall_layer.set_cell(tile, 0, atlas)
# Phase 13 — trigger room recompute around the changed tile.
if room_detector != null:
room_detector.recompute_around(tile)
func mark_floor_tile(tile: Vector2i, material: StringName) -> void:
if floor_layer == null:
return
var atlas := Vector2i(1, 0) if material == &"dirt" else Vector2i(2, 0)
floor_layer.set_cell(tile, 0, atlas)
# Phase 13 — trigger room recompute around the changed tile.
if room_detector != null:
room_detector.recompute_around(tile)
## Phase 13 — Called by Door._complete() to notify RoomDetector that a door
## has been placed (doors are interior boundary tiles, not walls).
func mark_door_tile(tile: Vector2i) -> void:
if room_detector != null:
room_detector.recompute_around(tile)
## Phase 13 — Toggle a tile's no-roof designation. Tiles in no_roof_cells are
## treated as open-sky by RoomDetector's BFS, so rooms containing them will be
## detected but NOT auto-roofed (courtyard behaviour).
## This is also the test-helper entry point the spec asks for:
## World.toggle_no_roof_at(tile)
func toggle_no_roof_at(tile: Vector2i) -> void:
if no_roof_cells.has(tile):
no_roof_cells.erase(tile)
Audit.log("room", "no-roof cleared at %s" % tile)
else:
no_roof_cells[tile] = true
Audit.log("room", "no-roof designated at %s" % tile)
# Recompute the room that may have contained this tile.
if room_detector != null:
room_detector.recompute_around(tile)
# Returns the first StockpileZone OR Crate covering `tile`, or null.
# Used by JobRunner._tick_deposit (Phase 5 refactor) to route deposits into
# Crate contents when applicable.
func stockpile_at_tile(tile: Vector2i):
for sp in stockpiles:
if sp.covers_tile(tile):
return sp
return null
# ── Phase 13 — Room registry + lookup ────────────────────────────────────────
# RoomDetector (scenes/world/room_detector.gd) populates this dict.
# Keys = stable room ids (int), values = Room class instances.
# Callers should treat this as read-only.
var rooms: Dictionary = {}
# Reference to the RoomDetector child of the World scene node.
# Set by World._ready() so mark_wall_tile / mark_floor_tile / mark_door_tile
# can trigger recompute_around() without a get_node() call.
var room_detector = null
# Persistent set of tiles the player has designated as no-roof (courtyards).
# Keys are Vector2i, value is unused boolean. Written by toggle_no_roof_at().
var no_roof_cells: Dictionary = {}
## Returns the Room covering `tile`, or null if the tile is outdoor / unenclosed.
## O(rooms) bounds-checked sweep — cheap at MVP scale (< 20 rooms).
func room_at_tile(tile: Vector2i):
for id in rooms:
var r = rooms[id]
if r.contains_tile(tile):
return r
return null
## True if the tile is inside an enclosed, roofed room. Replaces Phase 12's
## "has floor below" shelter proxy — Pawn._is_sheltered() will reroute here once
## RoomDetector is live.
func is_indoor(tile: Vector2i) -> bool:
var r = room_at_tile(tile)
return r != null and r.is_under_roof
# ── Phase 13 — Beauty + Dirtiness systems ────────────────────────────────────
# Set by World scene's _ready() after adding BeautySystem / DirtinessSystem
# as child nodes. All callers use World.get("beauty_system") defensively.
## BeautySystem child of the World scene.
## Exposes: beauty_at(tile), register_furniture(entity), recompute_around(tile).
var beauty_system = null
## DirtinessSystem child of the World scene.
## Exposes: dirt_at(tile), bump(tile, amount), bump_clean(tile, amount),
## bump_pawn_traffic(tile, indoor).
var dirtiness_system = null