Task #10: per-instance Turret/Tar Patch leveling (L1-L3)

Adds a per-placed-structure "level" (1-3) to automated_defense and
slowing_trap, upgraded independently (not account-wide) by holding
interact at that specific structure, by day, once it's undamaged
(repair still takes priority while it isn't), for a config-tunable
per-level cost.

Turret L2 fires turret_level_fire_mult (3x, matching the live Rapid
Fire multiplier) as often; L3 keeps that rate and fires
turret_l3_projectiles shots at once in a fan. A tar patch's trigger
footprint and visual grow by trap_level_size_mult per level (L2
double, L3 triple its L1 size); a bigger footprint that would overlap
a neighbor or the world refuses the upgrade with a reason.

FarmState.VERSION bumps 5->6 (STRUCTURE_KEYS gains "level"; migrate()
backfills level 1 on older saves, following the v2->v3 precedent for
night_log). Co-op: an upgrade is driven the same way repair already
is -- entirely host-side, off the partner's held interact button, no
new "req" case -- so the only wire change is forwarding the upgrade
channel's target/progress in the avatar snapshot, same as repair's.

New test_structures.gd coverage: fire interval and projectile count
per turret level, tar patch footprint per level, each proven
independently for two separately-placed structures of the same type,
plus the upgrade rules (repair-first, room, afford) and that level
survives a save/retry round trip.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-05 15:48:19 -04:00
parent 0613cca408
commit 62324ff4b1
8 changed files with 468 additions and 33 deletions

View file

@ -5,7 +5,10 @@ extends RefCounted
## 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.
## 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.
const P := CampaignController.Phase
const T := ThiefBehavior.State
@ -33,6 +36,7 @@ func run(r: GameTestRunner) -> void:
await _breakout(r, sim, player)
_trap(r, sim)
_turret(r, sim)
_leveling(r, sim)
_repair_and_dismantle(r, sim, player)
_persistence(r, sim)
_ab_night(r, sim, player)
@ -286,6 +290,190 @@ func _turret(r: GameTestRunner, sim: FarmSim) -> void:
_settle(sim)
## Task #10: per-instance leveling (L1-L3), independent per placed structure -- a turret's fire
## rate and L3 volley, a tar patch's footprint -- and the rules around it (undamaged, affordable,
## room for a bigger tar patch), proven for two separately-placed structures of the same type so a
## level-up only ever changes the one instance.
func _leveling(r: GameTestRunner, sim: FarmSim) -> void:
var st := sim.structures
var es := sim.enemies
_rich(sim.campaign)
var keep := es.behavior
es.behavior = null # drive enemies by hand: stay put in range, no AI movement
_level_turrets(r, sim, st, es)
_level_traps(r, sim, st)
_level_rules(r, sim, st)
es.behavior = keep
_settle(sim)
## Two separately-placed turrets: A is leveled up through L2 and L3 while B is left at L1, so every
## check also proves the other one didn't change.
func _level_turrets(r: GameTestRunner, sim: FarmSim, st: StructureSystem, es: EnemySystem) -> void:
var cfg := sim.config
var base := cfg.turret_fire_interval
var a := st.place("automated_defense", 8.0, -6.0, 0)
var b := st.place("automated_defense", 30.0, -17.5, 0) # far enough apart (> 2x turret_range) that neither's test enemy draws the other's fire
r.check(st.level_of(a) == 1 and st.level_of(b) == 1, "a freshly placed turret starts at L1")
var ha := es.spawn(EnemySystem.TYPE_THIEF, 9.3, -6.0, Vector2.UP)
var ev := _fire_events(sim, 400, 3)
r.check(ev.size() == 3 and ev.all(func(e: Array) -> bool: return int(e[1]) == 1), "L1: one projectile a shot (got %s)" % [ev])
r.check(absf(ev[1][0] - ev[0][0] - base) < 0.05 and absf(ev[2][0] - ev[1][0] - base) < 0.05,
"L1: the base interval between shots (%.3f s apart, expected %.3f)" % [ev[1][0] - ev[0][0], base])
es.apply_hit_handle(ha, 99)
_step(sim, 3)
var cost2: Dictionary = cfg.structure_level_costs["automated_defense"][0]
var shed0: Dictionary = sim.campaign.state.resources.shed.duplicate()
r.check(st.upgrade(a) and st.level_of(a) == 2 and st.level_of(b) == 1, "upgrading A to L2 pays and levels only A, not B")
for m in cost2:
r.check(int(sim.campaign.state.resources.shed[m]) == int(shed0[m]) - int(cost2[m]), "L2 costs %s" % [cost2])
var expect2 := base / cfg.turret_level_fire_mult
var ha2 := es.spawn(EnemySystem.TYPE_THIEF, 9.3, -6.0, Vector2.UP)
ev = _fire_events(sim, 400, 3)
r.check(ev.size() == 3 and ev.all(func(e: Array) -> bool: return int(e[1]) == 1), "L2: still one projectile a shot (got %s)" % [ev])
r.check(absf(ev[1][0] - ev[0][0] - expect2) < 0.05 and absf(ev[2][0] - ev[1][0] - expect2) < 0.05,
"L2: %.1fx the fire rate (%.3f s apart, expected %.3f)" % [cfg.turret_level_fire_mult, ev[1][0] - ev[0][0], expect2])
es.apply_hit_handle(ha2, 99)
_step(sim, 3)
var cost3: Dictionary = cfg.structure_level_costs["automated_defense"][1]
shed0 = sim.campaign.state.resources.shed.duplicate()
r.check(st.upgrade(a) and st.level_of(a) == 3, "upgrading A to L3")
for m in cost3:
r.check(int(sim.campaign.state.resources.shed[m]) == int(shed0[m]) - int(cost3[m]), "L3 costs %s" % [cost3])
r.check(st.upgrade_cost(a).is_empty() and st.upgrade_blocker(a) == "Max level", "L3 is the max: no further cost, 'Max level'")
var ha3 := es.spawn(EnemySystem.TYPE_THIEF, 9.3, -6.0, Vector2.UP)
es.health[es.resolve_handle(ha3)] = 999 # outlast a few L3 volleys (3 shots each) so a 2nd volley is there to measure
ev = _fire_events(sim, 400, 2)
r.check(ev.size() == 2 and int(ev[0][1]) == cfg.turret_l3_projectiles and int(ev[1][1]) == cfg.turret_l3_projectiles,
"L3: %d projectiles at once, in a fan (got %s)" % [cfg.turret_l3_projectiles, ev])
r.check(absf(ev[1][0] - ev[0][0] - expect2) < 0.05, "L3: keeps L2's rate (%.3f s apart, expected %.3f)" % [ev[1][0] - ev[0][0], expect2])
es.health[es.resolve_handle(ha3)] = 1 # undo the extra HP (above) before the usual kill-and-clean-up
es.apply_hit_handle(ha3, 99)
_step(sim, 3)
r.check(st.level_of(b) == 1, "B was never upgraded, through all of A's: still L1")
var hb := es.spawn(EnemySystem.TYPE_THIEF, 31.3, -17.5, Vector2.UP)
ev = _fire_events(sim, 400, 2)
r.check(ev.size() == 2 and ev.all(func(e: Array) -> bool: return int(e[1]) == 1) and absf(ev[1][0] - ev[0][0] - base) < 0.05,
"B still fires at the unleveled base rate, one projectile (got %s)" % [ev])
es.apply_hit_handle(hb, 99)
_step(sim, 3)
for dd in es.active_count:
if es.alive[dd] == 1:
es.apply_hit(dd, 99)
st.dismantle(a)
st.dismantle(b)
## Two separately-placed tar patches: A is leveled up through L2 and L3 (footprint doubling, then
## tripling, its L1 size) while B is left at L1.
func _level_traps(r: GameTestRunner, sim: FarmSim, st: StructureSystem) -> void:
var cfg := sim.config
r.check(is_equal_approx(st.trap_size_mult(1), 1.0) and is_equal_approx(st.trap_size_mult(2), 2.0) and is_equal_approx(st.trap_size_mult(3), 3.0),
"trap_level_size_mult: L1 1x, L2 2x, L3 3x")
var a := st.place("slowing_trap", 8.0, -12.0, 0)
var b := st.place("slowing_trap", 8.0, -8.0, 0)
r.check(st.level_of(a) == 1 and st.level_of(b) == 1, "a freshly placed tar patch starts at L1")
var e: Dictionary = sim.campaign.state.structure(a)
var half1 := 0.75 * st.trap_size_mult(1)
r.check(st.try_trap(8.0 + half1 - 0.02, -12.0) and int(e["hp"]) == cfg.trap_triggers - 1, "L1 (%.2f m half): triggers just inside its edge" % half1)
e["hp"] = cfg.trap_triggers
r.check(not st.try_trap(8.0 + half1 + 0.05, -12.0), "L1: not just past its edge")
var cost2: Dictionary = cfg.structure_level_costs["slowing_trap"][0]
var shed0: Dictionary = sim.campaign.state.resources.shed.duplicate()
r.check(st.upgrade(a) and st.level_of(a) == 2 and st.level_of(b) == 1, "upgrading A to L2 pays and levels only A, not B")
for m in cost2:
r.check(int(sim.campaign.state.resources.shed[m]) == int(shed0[m]) - int(cost2[m]), "L2 costs %s" % [cost2])
var half2 := 0.75 * st.trap_size_mult(2)
r.check(is_equal_approx(half2, half1 * 2.0), "L2 doubles the L1 footprint (%.2f vs %.2f m half)" % [half2, half1])
e["hp"] = cfg.trap_triggers
r.check(st.try_trap(8.0 + half1 + 0.05, -12.0), "L2: now triggers just past the old L1 edge (bigger footprint)")
e["hp"] = cfg.trap_triggers
r.check(not st.try_trap(8.0 + half2 + 0.05, -12.0), "L2: still not past its own (bigger) edge")
var cost3: Dictionary = cfg.structure_level_costs["slowing_trap"][1]
shed0 = sim.campaign.state.resources.shed.duplicate()
r.check(st.upgrade(a) and st.level_of(a) == 3, "upgrading A to L3")
for m in cost3:
r.check(int(sim.campaign.state.resources.shed[m]) == int(shed0[m]) - int(cost3[m]), "L3 costs %s" % [cost3])
var half3 := 0.75 * st.trap_size_mult(3)
r.check(is_equal_approx(half3, half1 * 3.0), "L3 triples the L1 footprint (%.2f vs %.2f m half)" % [half3, half1])
e["hp"] = cfg.trap_triggers
r.check(st.try_trap(8.0 + half2 + 0.05, -12.0), "L3: now triggers past L2's old edge too")
e["hp"] = cfg.trap_triggers
r.check(not st.try_trap(8.0 + half3 + 0.05, -12.0), "L3: still not past its own edge")
r.check(st.upgrade_cost(a).is_empty() and st.upgrade_blocker(a) == "Max level", "L3 is the max: no further cost, 'Max level'")
r.check(st.level_of(b) == 1, "B was never upgraded, through all of A's: still L1")
var eb: Dictionary = sim.campaign.state.structure(b)
r.check(st.try_trap(8.0 + half1 - 0.02, -8.0) and int(eb["hp"]) == cfg.trap_triggers - 1
and not st.try_trap(8.0 + half1 + 0.05, -8.0), "B still triggers at its L1 size, not past it")
st.dismantle(a)
st.dismantle(b)
## The rules around leveling: a damaged structure must be repaired first (repair takes priority),
## a bigger tar patch refuses to overlap a neighbor, an unaffordable upgrade is refused, and the
## level survives a to_dict()/from_dict() round trip (save) and sync_from_state() (retry).
func _level_rules(r: GameTestRunner, sim: FarmSim, st: StructureSystem) -> void:
var s := sim.campaign.state
var dmg := st.place("automated_defense", 8.0, -6.0, 0)
st.damage(dmg, 1)
r.check(st.upgrade_blocker(dmg) == "Repair it first" and not st.upgrade(dmg) and st.level_of(dmg) == 1,
"a damaged turret must be repaired before it can be upgraded")
st.repair(dmg)
st.dismantle(dmg)
var near_a := st.place("slowing_trap", 8.0, -12.0, 0)
var near_b := st.place("slowing_trap", 9.3, -12.0, 0) # 1.3 m away: clear at L1, overlaps once doubled
var why := st.upgrade_blocker(near_a)
r.check(why == "Not enough room to upgrade" and not st.upgrade(near_a) and st.level_of(near_a) == 1,
"a tar patch refuses to upgrade into a neighbor (got '%s')" % why)
st.dismantle(near_a)
st.dismantle(near_b)
var poor := st.place("automated_defense", 8.0, -6.0, 0)
var shed0: Dictionary = s.resources.shed.duplicate()
s.resources.shed = {"wood": 0, "scrap": 0, "straw": 0}
why = st.upgrade_blocker(poor)
r.check(why.begins_with("Not enough:") and not st.upgrade(poor), "an unaffordable upgrade is refused (got '%s')" % why)
s.resources.shed = shed0
st.dismantle(poor)
var lvl := st.place("automated_defense", 8.0, -6.0, 0)
st.upgrade(lvl)
st.upgrade(lvl)
r.check(st.level_of(lvl) == 3, "set up an L3 turret to round-trip")
var d := s.to_dict()
var back := FarmState.from_dict(JSON.parse_string(JSON.stringify(d, "", true, true)))
r.check(back != null and int(back.structure(lvl)["level"]) == 3, "the level survives a save/load round trip")
st.sync_from_state() # as a retry would: rebuild every view from the (unchanged) live state
r.check(st.level_of(lvl) == 3, "and sync_from_state() (retry) keeps it")
st.dismantle(lvl)
## [time_since_the_first_call, projectiles_fired_at_once] for up to `want` shots (sim.projectiles
## may fire more than one projectile in a single tick, at L3).
func _fire_events(sim: FarmSim, max_ticks: int, want: int) -> Array:
var events: Array = []
var last := sim.projectiles.fired
var t := 0.0
var ticks := 0
while events.size() < want and ticks < max_ticks:
sim.step(DT)
t += DT
ticks += 1
var now := sim.projectiles.fired
if now > last:
events.append([t, now - last])
last = now
return events
func _repair_and_dismantle(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void:
var st := sim.structures
var s := sim.campaign.state