Task #9: Workbench auto-grants after Night 1, destructible/repairable

Gate the Workbench to appear once Night 1 is survived instead of being
buildable: StructureSystem.grant_workbench() (an unvalidated, unpurchased
place() variant, grant()) adds a "workbench" structure record at the scene
node's position, reusing the existing damage/repair/targeting/save pipeline
end to end. The grant happens on CampaignController.night_ending (so it
lands in state before the morning snapshot, like #13's crop growth) and is
re-derived from FarmState.nights_survived on load/retry (sync_from_state).

The Workbench's view is the scene's own node (hidden until granted, shown
by StructureSystem._add_view) instead of a second built mesh, so it gets
the standard HP bar, damage tint, and wobble for free. It's repair-only:
damage() never removes its record at 0 HP ("broken" instead of destroyed),
and it can't be dismantled. Interactions refuses to open its shop before
it exists or while broken (and no longer treats it as focusable), and
coop_session.gd's "buy" request handler refuses a workbench-shop item
(Upgrades.shop_of) the same way for a co-op partner. review_panel.gd's
pre-grant summary line no longer promises the work area from night one.

No FarmState.VERSION bump: a "workbench" record is a new type string, not
a new field, and from_dict() already validates types generically.

Tests: a new test_structures.gd check covers absent/no-prompt/shop-refused
before Night 1 (including a co-op "buy" request), granted after, damaged
by an enemy (thief_behavior.gd's generic BREAK state), broken-not-destroyed
at 0 HP with its shop refused, repaired at the normal proportional cost,
and persistence through a save/retry round trip. test_cycle.gd and
test_nights.gd pre-grant the workbench in setup (grant_workbench()) so
their per-night structure-count invariants aren't thrown off by the
night-1 grant itself.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-05 16:27:55 -04:00
parent ff2ace9d3c
commit df10a1967c
10 changed files with 272 additions and 21 deletions

View file

@ -8,7 +8,11 @@ extends RefCounted
## persistence and retry, an A/B night: the same attack on an open and on a fenced crop field, and
## per-instance leveling (#10): a turret's fire rate and L3 volley, a tar patch's footprint, paid
## independently per placed structure, the rules around it (repair first, room, afford), and that
## the level survives a save/retry round trip.
## the level survives a save/retry round trip; and the Workbench (#9): absent and inaccessible
## (no prompt, no shop, including over a co-op request) before Night 1, granted into state before
## the morning snapshot once it's survived, then damaged by an enemy like any barrier, "broken"
## (not destroyed) at 0 HP with its shop refused, repaired at the normal proportional cost, and
## its presence and HP surviving a save and a retry.
const P := CampaignController.Phase
const T := ThiefBehavior.State
@ -40,6 +44,7 @@ func run(r: GameTestRunner) -> void:
_repair_and_dismantle(r, sim, player)
_persistence(r, sim)
_ab_night(r, sim, player)
_workbench(r, sim, player)
farm.queue_free()
await r.process_frame
@ -554,6 +559,117 @@ func _ab_night(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void
r.check(defended[0] < open[0], "defenses change the outcome of the same attack (%d < %d units lost)" % [defended[0], open[0]])
## Task #9: the Workbench is granted (not built) once Night 1 is survived. A fresh new_campaign()
## (the A/B nights above leave the campaign well past night 1) so "before Night 1" means something.
func _workbench(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
var c := sim.campaign
var st := sim.structures
var it := sim.interactions
var bench := it.workbench
var es := sim.enemies
c.new_campaign()
_step(sim, 1)
r.check(not st.has_workbench() and st.workbench_id() == -1 and not bench.visible,
"a fresh vacation, morning of night 1: the Workbench isn't a structure yet, and its scene node is hidden")
_rich(c)
c.begin_day()
player.global_position = bench.global_position + Vector3(0.0, 0.0, 1.2)
var msgs: Array[String] = []
it.message.connect(func(t: String, _col: Color) -> void: msgs.append(t))
Input.action_release("interact")
_tap(sim, "interact")
r.check(not sim.upgrade_panel.is_open, "standing where it will be, before night 1, interact opens no shop")
r.check(msgs.is_empty(), "and no workbench prompt or message fires either (it isn't focused: it doesn't exist) (%s)" % [msgs])
var coop := CoopSession.new()
coop.sim = sim
coop._do_request(["buy", "straw_stuffing"])
r.check(not Upgrades.owned(c.state, "straw_stuffing"), "a co-op 'buy' request for a workbench-shop item is refused before the workbench exists")
# Survive night 1: DAY -> DUSK -> NIGHT -> DAWN -> MORNING. night_ending grants it (before the
# morning snapshot/save, like #13's crop growth).
c.advance_phase()
c.advance_phase()
c.advance_phase()
c.advance_phase()
r.check(c.state.phase == P.MORNING and c.state.nights_survived == 1, "night 1 is behind us")
var id := st.workbench_id()
r.check(id >= 1 and st.has_workbench() and bench.visible and not StructureSystem.ORDER.has("workbench"),
"the Workbench now exists as a real (but not buildable) structure, and its scene node shows")
r.check(int(c.state.structure(id)["hp"]) == st.max_hp("workbench"), "granted at full HP")
var morning_structs: Array = c.state.to_dict()["structures"]
r.check(morning_structs.any(func(e: Dictionary) -> bool: return String(e["type"]) == "workbench"),
"it was already in state before the morning snapshot was taken")
c.begin_day()
player.global_position = bench.global_position + Vector3(0.0, 0.0, 1.2)
msgs.clear()
_tap(sim, "interact")
r.check(sim.upgrade_panel.is_open, "now its shop opens like any other workbench day")
_tap(sim, "interact")
# Damaged by an enemy: thief_behavior.gd's generic BREAK state (any barrier in a route, not
# just a "fence breaker"'s guarded ones) -- it's a nav barrier like a fence (StructureSystem
# .blocks() excludes only slowing_trap).
var h := es.spawn(EnemySystem.TYPE_THIEF, bench.global_position.x + 0.6, bench.global_position.z, Vector2.LEFT)
var d := es.resolve_handle(h)
es.state[d] = T.BREAK
es.barrier[d] = id
var hp0 := int(c.state.structure(id)["hp"])
_step(sim, roundi(es.t_hit_interval[EnemySystem.TYPE_THIEF] * 60.0) + 4)
r.check(int(c.state.structure(id)["hp"]) < hp0, "an enemy breaking against it damages it like any barrier")
es.apply_hit_handle(h, 99)
_step(sim, 1)
# Broken at 0 HP: the record stays (repair-only, not destroyed/removed); its shop is refused.
var destroyed: Array[String] = []
st.destroyed.connect(func(_i: int, t: String, _at: Vector3) -> void: destroyed.append(t))
r.check(not st.damage(id, 9999) and not c.state.structure(id).is_empty() and int(c.state.structure(id)["hp"]) == 0,
"damaging it to 0 HP leaves it 'broken', not destroyed: the record stays")
r.check(destroyed.is_empty(), "no breach/destroyed signal for it")
# Just outside REPAIR_REACH (1.4 m to the footprint's edge, 0.5 m out from center) but inside
# the Workbench's own reach (2.2 m, to its center): repair takes priority within REPAIR_REACH
# (Interactions), so this is where its shop's own refusal shows.
player.global_position = bench.global_position + Vector3(0.0, 0.0, 2.0)
msgs.clear()
_tap(sim, "interact")
r.check(not sim.upgrade_panel.is_open and msgs.has("Broken -- repair it"), "its shop refuses to open while broken (%s)" % [msgs])
coop._do_request(["buy", "straw_stuffing"])
r.check(not Upgrades.owned(c.state, "straw_stuffing"), "a co-op 'buy' request is refused while it's broken too")
# Repaired at the normal proportional cost (StructureSystem.repair_cost/repair), from within
# REPAIR_REACH.
var cost := st.repair_cost(id)
r.check(not cost.is_empty() and cost == sim.config.costs["workbench"], "broken (0 of its HP), repairing costs the full amount (%s)" % [cost])
c.state.resources.shed = cost.duplicate()
player.global_position = bench.global_position + Vector3(0.0, 0.0, 1.2)
Input.action_press("interact")
_step(sim, roundi(sim.config.repair_time * 60.0) + 2)
Input.action_release("interact")
_step(sim, 1)
r.check(int(c.state.structure(id)["hp"]) == st.max_hp("workbench"), "holding interact repairs it fully, for the proportional cost")
msgs.clear()
_tap(sim, "interact")
r.check(sim.upgrade_panel.is_open, "repaired, its shop opens again")
_tap(sim, "interact")
# Persistence: presence and HP survive a save (JSON) round trip and a retry.
var d2 := c.state.to_dict()
var back := FarmState.from_dict(JSON.parse_string(JSON.stringify(d2, "", true, true)))
r.check(back != null and back.structure(id)["type"] == "workbench" and int(back.structure(id)["hp"]) == st.max_hp("workbench"),
"its presence and HP survive a save (JSON) round trip")
st.damage(id, 5)
c.advance_phase() # DAY -> DUSK
c.advance_phase() # DUSK -> NIGHT
c.state.scarecrow_hp = 0
_step(sim, 1)
c.retry_night()
_step(sim, 1)
r.check(st.has_workbench() and int(c.state.structure(st.workbench_id())["hp"]) == st.max_hp("workbench"),
"a retry restores the morning's Workbench, undamaged by what happened after")
player.global_position = AWAY
_settle(sim)
func _field_fences() -> Array:
var parts: Array = []
for k in 15: