Merge task #11: Animal Pen boundary is player-built

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-05 14:35:10 -04:00
commit 43300a15de
9 changed files with 163 additions and 96 deletions

77
game/tests/pen_ring.gd Normal file
View file

@ -0,0 +1,77 @@
class_name PenRing
extends RefCounted
## Positions for the Animal Pen's player-built fence/gate boundary (task #11): the same 24-piece
## layout the deleted fence_024 props and the removed GameConfig.start_structures starting gate
## used to occupy, around FarmZone.Kind.ANIMAL_PEN's rect (center (-3, -6), 14 x 10 m, world x
## -10..4, z -11..-1). Built through StructureSystem's normal validate/place path, at normal
## cost. Shared by test setups that need the pen closed the way it used to be out of the box
## (test_targets.gd, test_roster.gd); test_structures.gd's dedicated player-built-pen-boundary
## test builds the same layout by hand (with checks) instead of calling build_full().
## North and south runs: 7 fences each, 2 m apart, covering the full 14 m side.
const RUN_XS: Array[float] = [-9.0, -7.0, -5.0, -3.0, -1.0, 1.0, 3.0]
const NORTH_Z := -11.0
const SOUTH_Z := -1.0
## West and east runs: 5 slots each, 2 m apart, covering the full 10 m side. The east run's
## middle slot is the gate (where the old starting gate stood), so animals can be led in.
const SIDE_ZS: Array[float] = [-10.0, -8.0, -6.0, -4.0, -2.0]
const WEST_X := -10.0
const EAST_X := 4.0
const GATE_Z := -6.0
const GATE_ROT := 2
## Pen center: within StructureSystem.build_range (9 m) of every slot below, so one point works
## as the "scarecrow" distance check in validate() for the whole ring.
const NEAR := Vector2(-3.0, -6.0)
## The 23 fence slots (the east run's middle slot skipped: that one's the gate's), each a
## [x: float, z: float, rot: int] triple.
static func fence_slots() -> Array:
var out: Array = []
for x in RUN_XS:
out.append([x, NORTH_Z, 0])
out.append([x, SOUTH_Z, 0])
for z in SIDE_ZS:
out.append([WEST_X, z, GATE_ROT])
if z != GATE_Z:
out.append([EAST_X, z, GATE_ROT])
return out
## Builds the full ring (gate first, so it gets structure id 1, the way the old starting gate
## did) at normal cost, through the normal validate/place path. Grants and restores shed
## resources around it so callers don't need to pre-fund it. Returns the gate's id (-1 if the
## farm's layout ever drifts enough that validate() refuses a slot -- logged as a warning rather
## than failing outright, since this is setup, not the check itself).
static func build_full(sim: FarmSim) -> int:
var s := sim.campaign.state
var st := sim.structures
var keep: Dictionary = s.resources.shed.duplicate()
s.resources.shed = {"wood": 999, "scrap": 999, "straw": 999}
var gate_id := _place(st, "gate", EAST_X, GATE_Z, GATE_ROT)
for slot in fence_slots():
_place(st, "fence", slot[0], slot[1], slot[2])
s.resources.shed = keep
return gate_id
## Just the gate, at normal cost (granting and restoring shed resources around it): for tests that
## only need a single structure near the pen (matching nav.structure_at lookups at its old spot),
## not a closed pen -- e.g. test_roster.gd's fence-breaker test, where a full ring of equally-
## "guarding" structures (FarmSim._breaker_goals) would change its two-barriers-then-the-
## scarecrow narrative.
static func build_gate_only(sim: FarmSim) -> int:
var s := sim.campaign.state
var keep: Dictionary = s.resources.shed.duplicate()
s.resources.shed = {"wood": 999, "scrap": 999, "straw": 999}
var gate_id := _place(sim.structures, "gate", EAST_X, GATE_Z, GATE_ROT)
s.resources.shed = keep
return gate_id
static func _place(st: StructureSystem, type: String, x: float, z: float, rot: int) -> int:
var why := st.validate(type, x, z, rot, NEAR)
if why != "":
push_warning("PenRing: %s at (%s, %s) refused: %s" % [type, x, z, why])
return -1
return st.place(type, x, z, rot)

View file

@ -0,0 +1 @@
uid://cxr4cikm6ieph

View file

@ -78,7 +78,7 @@ func _day(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter, hud: PhaseHu
sim.structures.damage(fence, 6)
s.scarecrow_hp = 7
player.global_position = AWAY
r.check(sim.structures.count() == _start_structures + 2, "built a fence and an automated defense (next to the pen's gate)")
r.check(sim.structures.count() == _start_structures + 2, "built a fence and an automated defense")
var arrived: Array[int] = []
sim.waves.arrived.connect(func(h: int, _at: Vector2) -> void: arrived.append(h))
_step_until(sim, P.DUSK)

View file

@ -1,7 +1,7 @@
extends RefCounted
## FarmState: creation from config (all three asset categories and the pen's gate), exact
## to_dict/from_dict round trips (also through JSON), rejection of invalid data, snapshot
## independence, and night loss records for every category.
## FarmState: creation from config (all three asset categories, and no starting structures --
## the pen is fully player-built), exact to_dict/from_dict round trips (also through JSON),
## rejection of invalid data, snapshot independence, and night loss records for every category.
func run(r: GameTestRunner) -> void:
@ -14,8 +14,8 @@ func run(r: GameTestRunner) -> void:
r.check(s.animals == cfg.start_animals and s.animals_total == cfg.start_animals, "animals start at the configured count")
var stock := cfg.start_wood + cfg.start_scrap + cfg.start_straw
r.check(s.supplies == stock and s.supplies_total == stock and not s.shed_raided, "supplies are the shed's starting stock (%d)" % stock)
r.check(s.structures.size() == cfg.start_structures.size() and s.structures[0]["type"] == "gate"
and s.structures[0]["hp"] == cfg.gate_hp and s.next_structure_id == 2, "the pen's gate stands from the start")
r.check(s.structures.size() == cfg.start_structures.size() and s.structures.is_empty() and s.next_structure_id == 1,
"no structures stand at the start: the pen is fully player-built")
r.check(s.is_valid(), "a new farm is valid")
# A state with every field away from its default.

View file

@ -25,6 +25,11 @@ func run(r: GameTestRunner) -> void:
player.global_position = AWAY
sim.campaign.begin_day()
sim.campaign.state.resources.shed = {"wood": 60, "scrap": 60, "straw": 60}
# _breakers expects a single gate guarding the pen (not the full ring: with every ring piece
# a real structure, _breaker_goals() would treat each as "guarding" and the fence-breaker
# narrative -- exactly two guarding barriers, then the scarecrow -- no longer holds).
PenRing.build_gate_only(sim)
_settle(sim)
_table(r, sim)
_crows(r, sim, player)
_thieves(r, sim, player)

View file

@ -1,7 +1,8 @@
extends RefCounted
## Construction on the real farm, with FarmSim stepped by hand: placement rules (each reason),
## build mode driven by gamepad input (build, dismantle, leave), paying and recording, the sliced
## flow-field rebuild, gates (the scarecrow passes, enemies can't), a walled-in thief breaking
## the player-built Animal Pen boundary (open with nothing built, open with a gap, shut once the
## ring is complete), build mode driven by gamepad input (build, dismantle, leave), paying and
## recording, the sliced flow-field rebuild, gates (the scarecrow passes, enemies can't), a walled-in thief breaking
## out through a fence (barrier routing), slowing traps (speed, triggers), the automated
## defense (range), damage and breaking (breach), repair (cost, any phase), dismantling (refund),
## persistence and retry, and an A/B night: the same attack on an open and on a fenced crop field.
@ -25,10 +26,11 @@ func run(r: GameTestRunner) -> void:
c.begin_day()
_rich(c)
_validation(r, sim)
_pen_boundary(r, sim)
_place_and_rebuild(r, sim)
_build_mode(r, sim, player)
await _gate(r, sim, player)
_breakout(r, sim, player)
await _breakout(r, sim, player)
_trap(r, sim)
_turret(r, sim)
_repair_and_dismantle(r, sim, player)
@ -52,7 +54,7 @@ func _validation(r: GameTestRunner, sim: FarmSim) -> void:
["at the barn doors", "fence", Vector2(1.0, -19.5), 0, "Keep the barn doors clear"],
["on a crop row", "fence", sim.loot.home[1], 2, "Not on the crops"],
["in the house", "automated_defense", Vector2(20.5, -26.0), 0, "Blocked"],
["on the pen fence", "fence", Vector2(-3.0, -11.0), 0, "Blocked"],
["on the pen's edge (no more fence_024 colliders)", "fence", Vector2(-3.0, -11.0), 0, ""],
]
for case in cases:
var why := st.validate(case[1], case[2].x, case[2].y, case[3], case[2] + Vector2(2.0, 2.0))
@ -76,6 +78,40 @@ func _validation(r: GameTestRunner, sim: FarmSim) -> void:
_to_day(sim)
## Task #11: the Animal Pen's boundary is no longer a scene prop (fence_024) or a free starting
## gate -- it's built the same way as any other fence/gate, at normal cost, through the normal
## validate/place path. With nothing built the pen is open; closing all but its gate slot still
## leaves that one gap open; filling it with a gate shuts the pen. This also leaves the ring
## standing (gate included) for the rest of the suite, the way the deleted props and the removed
## starting gate used to.
func _pen_boundary(r: GameTestRunner, sim: FarmSim) -> void:
var st := sim.structures
var s := sim.campaign.state
r.check(sim.animals.pen_open, "with no fence_024 props and no starting gate, the pen starts open")
var slots := PenRing.fence_slots()
var wood0 := int(s.resources.shed["wood"])
var refused := []
for slot in slots:
var why := st.validate("fence", slot[0], slot[1], slot[2], PenRing.NEAR)
if why == "":
st.place("fence", slot[0], slot[1], slot[2])
else:
refused.append("(%s, %s): %s" % [slot[0], slot[1], why])
r.check(refused.is_empty(), "every fence around the pen rect validates and places (%s)" % [refused])
r.check(int(s.resources.shed["wood"]) == wood0 - slots.size() * 2, "%d fences cost 2 wood each" % slots.size())
_settle(sim)
r.check(sim.animals.pen_open, "the ring minus its gate slot still leaves a gap: the pen is open")
var why := st.validate("gate", PenRing.EAST_X, PenRing.GATE_Z, PenRing.GATE_ROT, PenRing.NEAR)
r.check(why == "", "a gate validates in the last gap (got '%s')" % why)
var wood1 := int(s.resources.shed["wood"])
var scrap1 := int(s.resources.shed["scrap"])
var gate_id := st.place("gate", PenRing.EAST_X, PenRing.GATE_Z, PenRing.GATE_ROT)
r.check(gate_id >= 1 and int(s.resources.shed["wood"]) == wood1 - 4 and int(s.resources.shed["scrap"]) == scrap1 - 1,
"the gate costs 4 wood and 1 scrap")
_settle(sim)
r.check(not sim.animals.pen_open, "a player-built gate in the last gap shuts the pen")
func _place_and_rebuild(r: GameTestRunner, sim: FarmSim) -> void:
var st := sim.structures
var s := sim.campaign.state
@ -155,15 +191,19 @@ func _gate(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
player.global_position = AWAY
## A thief inside the pen, whose only opening is shut by the pen's gate, breaks the gate to get
## out; a new gate can go back in the opening.
## A thief inside the pen, walled in by its player-built ring, breaks a way out; a replacement
## piece can go back in the breach.
## Task #11: escape_field is a global distance-to-the-farm's-outer-edge field, not a "nearest pen
## exit" one, and every ring piece costs the same to cross -- so (unlike the old fence_024 wall
## with its one gap) the thief isn't guaranteed to break the gate specifically; it breaks whatever
## ring piece sits nearest its route to the edge. This checks the general mechanic (a walled-in
## thief breaks out, the breach opens the pen, patching it shuts the pen again) against whichever
## piece that turns out to be, rather than assuming it's the gate.
func _breakout(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
var st := sim.structures
var es := sim.enemies
player.global_position = AWAY
var id := sim.nav.structure_at[sim.nav.cell_of(4.0, -6.0)]
r.check(id >= 1 and sim.campaign.state.structure(id)["type"] == "gate" and not sim.animals.pen_open,
"the vacation starts with a gate shutting the pen")
r.check(not sim.animals.pen_open, "the player-built ring (_pen_boundary) is shutting the pen")
var inside := sim.nav.cell_of(-4.0, -6.0)
r.check(sim.escape_field[inside] < FarmNavigation.UNREACHABLE, "the walled-in pen still has a route out (through the barrier)")
var broken: Array[int] = []
@ -171,18 +211,25 @@ func _breakout(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void
var h := es.spawn(EnemySystem.TYPE_THIEF, -2.0, -6.0, Vector2.RIGHT)
es.state[es.resolve_handle(h)] = T.RETREAT
var reached := _until(sim, func() -> bool: return es.resolve_handle(h) >= 0 and es.state[es.resolve_handle(h)] == T.BREAK, 20.0)
r.check(reached < 20.0 and es.barrier[es.resolve_handle(h)] == id, "the thief walks to the fence and starts breaking it (%.1f s)" % reached)
var id := es.barrier[es.resolve_handle(h)] if reached < 20.0 else -1
r.check(reached < 20.0 and id >= 1, "the thief walks to the pen's boundary and starts breaking a way out (%.1f s)" % reached)
var piece: Dictionary = sim.campaign.state.structure(id)
var type: String = piece.get("type", "fence")
var at := Vector2(float(piece.get("x", 0.0)), float(piece.get("z", 0.0)))
var rot: int = int(piece.get("rot", 0))
var took := _until(sim, func() -> bool: return broken.has(id), 30.0)
var expect := ceilf(float(sim.config.gate_hp) / es.t_structure_damage[EnemySystem.TYPE_THIEF]) * es.t_hit_interval[EnemySystem.TYPE_THIEF]
var expect := ceilf(float(st.max_hp(type)) / es.t_structure_damage[EnemySystem.TYPE_THIEF]) * es.t_hit_interval[EnemySystem.TYPE_THIEF]
r.check(took < 30.0 and absf(took - expect) < es.t_hit_interval[EnemySystem.TYPE_THIEF] + 0.5,
"it breaks the gate in about %.0f s (took %.1f s)" % [expect, took])
r.check(sim.nav.structure_at[sim.nav.cell_of(4.0, -6.0)] < 0 and sim.animals.pen_open, "the broken gate is gone from navigation: the pen is open")
"it breaks the %s in about %.0f s (took %.1f s)" % [type, expect, took])
r.check(sim.nav.structure_at[sim.nav.cell_of(at.x, at.y)] < 0 and sim.animals.pen_open, "the broken piece is gone from navigation: the pen is open")
var out := _until(sim, func() -> bool: return es.resolve_handle(h) == -1, 40.0)
r.check(out < 40.0, "then it leaves the farm through the breach (%.1f s)" % out)
r.check(st.validate("gate", 4.0, -6.0, 2, Vector2(6.0, -6.0)) == "", "a new gate can go in the pen opening")
st.place("gate", 4.0, -6.0, 2)
await r.process_frame # the broken piece's view (fences keep real collision) is queue_free()'d, not freed outright
var replace_why := st.validate(type, at.x, at.y, rot, PenRing.NEAR)
r.check(replace_why == "", "a replacement %s can go back in the breach (got '%s')" % [type, replace_why])
st.place(type, at.x, at.y, rot)
_settle(sim)
r.check(not sim.animals.pen_open, "and shuts the pen again")
r.check(not sim.animals.pen_open, "patching the breach shuts the pen again")
func _trap(r: GameTestRunner, sim: FarmSim) -> void:
@ -269,7 +316,7 @@ func _repair_and_dismantle(r: GameTestRunner, sim: FarmSim, player: PlayerCharac
func _persistence(r: GameTestRunner, sim: FarmSim) -> void:
var c := sim.campaign
var st := sim.structures
var n0 := st.count() # the pen's gate
var n0 := st.count() # the player-built pen ring (fences and its gate)
st.place("fence", 8.0, -8.0, 0)
st.place("slowing_trap", 10.0, -12.0, 0)
var d := c.state.to_dict()

View file

@ -29,6 +29,8 @@ func run(r: GameTestRunner) -> void:
player.global_position = AWAY
sim.loot.loot_event.connect(func(kind: StringName, id: int, _p: Vector3) -> void: _events.append([kind, id]))
sim.campaign.begin_day()
PenRing.build_full(sim) # the pen is player-built now: close it the way the old starting gate did
_settle(sim)
_layout(r, sim)
_pen_by_day(r, sim)
_animal_theft(r, sim, player)
@ -93,7 +95,11 @@ func _animal_theft(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) ->
var loot := sim.loot
var s := sim.campaign.state
var gate := 1
var h := sim.spawn_thief(C.ANIMAL, GameTestRunner.EDGE["south-west"])
# Task #11: the pen's boundary is a full ring of equally-breakable player-built fences now, not
# a single gap in an otherwise solid wall, so a thief's nearest crossing depends on where it
# comes from. Spawn just outside the gate (not a shared GameTestRunner.EDGE point) so it's
# still the nearest barrier, keeping this test's "breaks the gate" narrative.
var h := sim.spawn_thief(C.ANIMAL, Vector2(7.0, -6.0))
_step(sim, 1)
var item := es.target[es.resolve_handle(h)]
r.check(item > 0 and loot.category[item] == C.ANIMAL, "an animal thief claims an animal")