Phase 4 — Trees, Rocks, Items, Stockpiles, Hauling

Three gdscript-refactor agents in parallel + Opus integration.

Entities (scenes/entities/, Agent A — 3 scripts + 3 .tscn, ~460 lines):
- item.gd: 16-type StringName registry (matches design.md filter chips);
  Node2D + _draw() colored square + stack-count badge; to_dict/from_dict
- tree.gd: class_name HarvestableTree (Godot 4 ships a built-in 'Tree'
  Control class — renamed to avoid the shadow); CHOP_TICKS=80; on_chop_tick
  advances progress, fells when complete, drops 3 wood items at tile +
  walkable neighbours
- rock.gd: MINE_TICKS=120; angular polygon _draw; mined() drops 1 stone

Toil + provider extensions (scenes/ai/, Agent B — 4 files modified/added,
~250 lines):
- Toil: new KIND_INTERACT (timed entity action), KIND_PICKUP, KIND_DEPOSIT
- JobRunner: _tick_interact resolves NodePath, calls target.<method>()
  each tick, marks done when is_choppable/is_mineable returns false;
  _tick_pickup finds Item at pawn.tile, transfers to pawn.carried_item;
  _tick_deposit places carried_item at pawn.tile + clears the
  items_needing_haul dirty flag
- ChopProvider (priority=5): nearest choppable tree; Job=[walk_to + interact]
- MineProvider (priority=4): same for rocks

Hauling system (scenes/world/ + scenes/ai/, Agent C — 4 files, ~330 lines):
- StorageDestination: abstract Node2D base; Priority enum CRITICAL=0..OFF=4;
  accepted_types (empty=wildcard); _filter_accepts() helper
- StockpileZone: concrete rect-region zone; _draw paints priority-tinted
  overlay (z_index=-1); find_drop_position scans for free cells respecting
  one-stack-per-tile rule
- HaulingProvider (priority=3): nearest dirty item × best destination →
  4-toil job [walk → pickup → walk → deposit]; sweep_for_better_destinations
  enables the priority cascade (items in lower-priority zones re-mark dirty
  when a higher-priority destination opens up)

Opus integration (~200 lines):
- World autoload: trees/rocks/items/items_needing_haul/stockpiles registries
  + register/unregister methods; pathfinder reference exposed for entity
  code (tree.fell needs is_walkable for neighbour drops)
- Pawn: carried_item slot + carry-indicator (small colored rect upper-right
  of body) via queue_redraw in _on_sim_tick
- World scene: registers chop/mine/haul/rest providers; spawns 6 trees
  (cluster east-north), 4 rocks (south-east), 2 stockpile zones (Zone A
  wood-only NORMAL, Zone B wildcard HIGH); periodic
  hauling_provider.sweep_for_better_destinations every 100 sim ticks

Acceptance — MCP-verified end-to-end (the full Phase 4 loop):
- 3 pawns boot, Decision picks chop (highest priority work), all walk to
  nearest tree, chop in parallel (3× speed because all 3 call on_chop_tick
  per tick). Trees fell, drop wood (18 items). Pawns move to rocks, mine,
  drop stone (4 items). Total 22 items spawn.
- HaulingProvider routes wood + stone toward Zone B (wildcard HIGH > Zone
  A's wood-only NORMAL). Pawns carry items one at a time, visual indicator
  shows during transit. Items deposit, items_needing_haul dirty flag
  clears.
- **Priority cascade test:** Zone A promoted from NORMAL to CRITICAL.
  Manually-triggered sweep marks 3 wood items in Zone B for re-haul.
  Within a few thousand ticks: Zone A has 5 wood (cascaded from Zone B),
  Zone B has 4 stone only (wood left, stone stayed because Zone A rejects
  stone). Filter + priority cascade working exactly per design.md spec.

Phase 4 gotchas (logged in implementation.md):
- 'Tree' shadows Godot 4's built-in Tree Control class — class_name had to
  be renamed to HarvestableTree. Scene/file names stayed as 'tree' since
  the game concept is still 'tree'; the rename only affects code-side
  type references.
- draw_colored_polygon(points, color) takes a SINGLE Color, not a
  PackedColorArray. Agent C had to be reminded; draw_polygon(points, colors)
  is the variant that takes per-vertex colors.
- Godot's class-name cache lags behind file changes — a full editor scan
  ('godot --headless --editor --quit') is needed to flush. Even after
  reload_project, type-annotation assignments can fail; duck-typed
  variables ('var x = scene.instantiate()') sidestep the issue.
- JobRunner's _tick_deposit had to explicitly call
  World.clear_item_haul_flag — the dirty set persisted otherwise and
  items appeared 'needing haul' even after deposit.

Delegation report this phase:
- Agent A (Sonnet, gdscript-refactor): Tree + Rock + Item entities + i18n
  keys. ~460 lines.
- Agent B (Sonnet, gdscript-refactor): Toil extensions + JobRunner handlers
  + ChopProvider + MineProvider. ~250 lines.
- Agent C (Sonnet, gdscript-refactor): StorageDestination + StockpileZone
  + HaulingProvider with cascade sweep. ~330 lines.
- Opus: World autoload extensions (entity registries + pathfinder ref),
  Pawn carry slot + visual, world.tscn/gd wiring, the Tree rename, the
  draw_colored_polygon fix, the dirty-set-clear fix, MCP-driven runtime
  verification including the full chop-mine-haul loop and the priority
  cascade demo.

~75% of Phase 4's GDScript was subagent-authored.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
megaproxy 2026-05-10 21:32:39 +01:00
parent 5bf0f51efb
commit 91bceeebe8
28 changed files with 1252 additions and 23 deletions

151
scenes/entities/item.gd Normal file
View file

@ -0,0 +1,151 @@
## Dropped item entity — a single stack of one item type lying on the world floor.
##
## Visuals are drawn procedurally via _draw() (Phase 4 placeholder). Real
## ElvGames item icons land in Phase 5+.
##
## Item type constants mirror the 16 filter chips in docs/design.md. They are
## used by StockpileZone filter bitmasks and pawn-carry typing.
##
## World registration (World.register_item / World.unregister_item) is called
## here but the methods land in World during Opus integration. The script will
## parse cleanly; the call will fail at runtime until then.
class_name Item extends Node2D
const TILE_SIZE_PX: int = 16
# ── canonical type registry — matches docs/design.md "16 filter chips" ───────
const TYPE_WOOD: StringName = &"wood" # Wd
const TYPE_STONE: StringName = &"stone" # St
const TYPE_IRON_ORE: StringName = &"iron_ore" # Ir
const TYPE_COPPER_ORE: StringName = &"copper_ore" # Cu
const TYPE_SILVER: StringName = &"silver" # Ag
const TYPE_GOLD: StringName = &"gold" # Au
const TYPE_CLOTH: StringName = &"cloth" # Cl
const TYPE_VEGETABLE: StringName = &"vegetable" # Veg
const TYPE_MEAT: StringName = &"meat" # Mt
const TYPE_GRAIN: StringName = &"grain" # Gr
const TYPE_MEAL: StringName = &"meal" # Ck (cooked)
const TYPE_MEDICINE: StringName = &"medicine" # Md
const TYPE_TOOL: StringName = &"tool" # Tl
const TYPE_WEAPON: StringName = &"weapon" # Wp
const TYPE_ARMOR: StringName = &"armor" # Ar
const TYPE_CORPSE: StringName = &"corpse" # Co
const ALL_TYPES: Array[StringName] = [
TYPE_WOOD, TYPE_STONE, TYPE_IRON_ORE, TYPE_COPPER_ORE,
TYPE_SILVER, TYPE_GOLD, TYPE_CLOTH, TYPE_VEGETABLE,
TYPE_MEAT, TYPE_GRAIN, TYPE_MEAL, TYPE_MEDICINE,
TYPE_TOOL, TYPE_WEAPON, TYPE_ARMOR, TYPE_CORPSE,
]
# ── state ────────────────────────────────────────────────────────────────────
@export var item_type: StringName = TYPE_WOOD
@export var stack_size: int = 1
var tile: Vector2i = Vector2i.ZERO
## When true the on-floor visual is suppressed; the carrying pawn renders the
## carry indicator instead.
var being_carried: bool = false
# ── lifecycle ─────────────────────────────────────────────────────────────────
func _ready() -> void:
position = _tile_to_world(tile)
visible = not being_carried
func _exit_tree() -> void:
World.unregister_item(self)
# ── public API ────────────────────────────────────────────────────────────────
## One-shot initialiser called by the spawning code (Tree.fell, Rock.mined, etc.)
## Sets all fields, syncs position, and registers with World.
func setup(p_type: StringName, p_stack: int, p_tile: Vector2i) -> void:
item_type = p_type
stack_size = p_stack
tile = p_tile
position = _tile_to_world(tile)
visible = not being_carried
queue_redraw()
World.register_item(self)
Audit.log("item", "spawned %s×%d at %s" % [item_type, stack_size, tile])
## Hide/show the on-floor sprite when the pawn picks up or drops this item.
func set_being_carried(value: bool) -> void:
being_carried = value
visible = not being_carried
# ── save / load ───────────────────────────────────────────────────────────────
func to_dict() -> Dictionary:
return {
"type": String(item_type),
"stack_size": stack_size,
"tile_x": tile.x,
"tile_y": tile.y,
}
## Returns a plain Dictionary spec for World.load_items() to instantiate from.
## Items cannot reconstruct themselves standalone — they need a parent in the
## scene tree. World adds the node, then calls setup() from the returned dict.
static func from_dict(d: Dictionary) -> Dictionary:
return {
"type": StringName(d.get("type", "wood")),
"stack_size": int(d.get("stack_size", 1)),
"tile_x": int(d.get("tile_x", 0)),
"tile_y": int(d.get("tile_y", 0)),
}
# ── render ────────────────────────────────────────────────────────────────────
func _draw() -> void:
# 12×12 coloured square centered on the tile; colour hashed from item_type.
var hue := float(item_type.hash() % 360) / 360.0
var fill := Color.from_hsv(hue, 0.6, 0.85)
var half: int = 6
var square := Rect2(Vector2(-half, -half), Vector2(half * 2, half * 2))
draw_rect(square, fill)
draw_rect(square, Color(0.0, 0.0, 0.0, 0.75), false, 1.0)
# Stack count badge — bottom-right corner of the square, font_size 7.
if stack_size > 1:
var label := Strings.t(&"item.stack_count").format({"n": stack_size})
draw_string(
ThemeDB.fallback_font,
Vector2(half - 1, half - 1),
label,
HORIZONTAL_ALIGNMENT_RIGHT,
-1,
7,
Color(0.0, 0.0, 0.0, 0.6) # drop-shadow offset below
)
draw_string(
ThemeDB.fallback_font,
Vector2(half - 2, half - 2),
label,
HORIZONTAL_ALIGNMENT_RIGHT,
-1,
7,
Color(1.0, 1.0, 1.0, 1.0)
)
# ── helpers ───────────────────────────────────────────────────────────────────
func _tile_to_world(t: Vector2i) -> Vector2:
return Vector2(
t.x * TILE_SIZE_PX + TILE_SIZE_PX / 2.0,
t.y * TILE_SIZE_PX + TILE_SIZE_PX / 2.0
)