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

@ -8,14 +8,14 @@ extends RefCounted
## pouch live in resources). Gather points below full, bought upgrades, and the built structures
## are kept too.
const VERSION := 5
const VERSION := 6
const KEYS: Array[String] = [
"night", "nights_survived", "phase", "phase_time_left", "scarecrow_max_hp", "scarecrow_hp",
"crops_total", "lost_crops", "animals_total", "lost_animals", "penned_animals", "supplies_total",
"shed_raided", "resources", "gather_charges", "upgrades", "structures", "next_structure_id",
"night_events", "night_log", "coop", "partner_max_hp", "partner_hp",
]
const STRUCTURE_KEYS: Array[String] = ["id", "type", "x", "z", "rot", "hp"]
const STRUCTURE_KEYS: Array[String] = ["id", "type", "x", "z", "rot", "hp", "level"]
## Events counted from the morning to the end of the dawn grace, copied into the night's log
## entry when it closes. Theft: supply events count material units, the others crop units or
## animals. Then enemies killed by type and those that escaped with loot; the scarecrow's damage
@ -106,7 +106,8 @@ var gather_charges: Dictionary = {}
## Bought upgrades (id -> level).
var upgrades: Dictionary = {}
## Built structures: {"id", "type", "x", "z" (world position, on the 0.5 m build grid), "rot"
## (0..7, steps of 45 degrees), "hp" (for a slowing trap, the enemies it can still slow)}.
## (0..7, steps of 45 degrees), "hp" (for a slowing trap, the enemies it can still slow), "level"
## (1-3; account-wide upgrades are tracked separately, above -- this is per placed instance, #10)}.
var structures: Array[Dictionary] = []
var next_structure_id: int = 1
@ -131,7 +132,7 @@ static func create(cfg: GameConfig) -> FarmState:
for e in cfg.start_structures:
var t := String(e["type"])
s.structures.append({"id": s.next_structure_id, "type": t, "x": float(e["x"]), "z": float(e["z"]),
"rot": int(e.get("rot", 0)), "hp": cfg.structure_hp(t)})
"rot": int(e.get("rot", 0)), "hp": cfg.structure_hp(t), "level": 1})
s.next_structure_id += 1
return s
@ -393,8 +394,9 @@ static func from_dict(d: Dictionary) -> FarmState:
e["z"] = float(raw["z"])
e["rot"] = int(raw["rot"])
e["hp"] = int(raw["hp"])
e["level"] = int(raw["level"])
if e["id"] < 1 or e["id"] >= s.next_structure_id or ids.has(e["id"]) or e["type"] == "" \
or e["rot"] < 0 or e["rot"] > 7 or e["hp"] < 0:
or e["rot"] < 0 or e["rot"] > 7 or e["hp"] < 0 or e["level"] < 1 or e["level"] > 3:
return null
ids[e["id"]] = true
s.structures.append(e)