Merge task #27: the thirty nights

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-06 16:38:48 -04:00
commit ddb3cb0568
36 changed files with 2956 additions and 136 deletions

View file

@ -1,16 +1,23 @@
extends RefCounted
## The five-night progression (Step 10): every night plan and debug scenario is valid (markers
## exist and suit the enemy type, types and targets are known, arrivals fit the night), each night
## tests what the plan says it should, the upgrades apply to the systems they change and persist
## (next morning, JSON, retry), the plans load by count with each night's enemy speed and HP
## scales, the debug F3 jump moves the morning, and the five nights play back to back to the
## farmers' return, each with its own plan and morning snapshot, everything carrying over.
## The thirty nights (Step 10, #27): every night plan and debug scenario is valid (types and
## targets are known, arrivals fit the night as played), each of the first five nights and the last
## tests what its plan says it should, the arc holds (the robots from night 3, the UFOs from night 5,
## no night much lighter than the one before, sieges every fifth night, the last night the longest),
## the upgrades apply to the systems they change and persist (next morning, JSON, retry), the plans
## load by count with each night's enemy speed and HP scales, the debug F3 jump moves the morning,
## and a sample of the nights (1-5, a siege, the last) plays back to back to the farmers' return,
## the rest skipped by the F3 morning path, each with its own plan and morning snapshot,
## everything carrying over.
const P := CampaignController.Phase
const T := ThiefBehavior.State
const DT := 1.0 / 60.0
const AWAY := Vector3(-11.0, 0.0, 13.0)
const SCENARIOS := "res://game/resources/scenarios/"
## The nights the back-to-back run plays (_back_to_back); the others are skipped on F3's path.
const PLAYED := [1, 2, 3, 4, 5, 10, 30]
## Every fifth night before the last is a siege (game/tools/make_nights.gd).
const SIEGES := [10, 15, 20, 25]
func run(r: GameTestRunner) -> void:
@ -25,16 +32,17 @@ func run(r: GameTestRunner) -> void:
player.global_position = AWAY
_validation(r, sim)
_designs(r, sim)
_arc(r, sim)
_upgrades(r, sim, player)
_plan_paths(r, sim)
_enemy_scales(r, sim)
_night_jump(r, farm, sim)
_five_nights(r, farm, sim, player)
_back_to_back(r, farm, sim, player)
farm.queue_free()
await r.process_frame
## Every plan (the five nights and the debug scenarios) checks out against the farm.
## Every plan (the vacation's nights and the debug scenarios) checks out against the farm.
func _validation(r: GameTestRunner, sim: FarmSim) -> void:
var plans: Array[NightPlan] = sim.waves.plans.duplicate()
r.check(plans.size() == sim.config.campaign_nights, "a plan for each of the %d nights" % sim.config.campaign_nights)
@ -57,13 +65,15 @@ func _validation(r: GameTestRunner, sim: FarmSim) -> void:
r.check(sim.waves.plans[k].dusk_hint.length() > 20, "night %d has a dusk hint" % (k + 1))
## Each night tests what it's meant to.
## Each of the first five nights, and the last, tests what it's meant to.
func _designs(r: GameTestRunner, sim: FarmSim) -> void:
var w := sim.waves
var n := w.plans
var cfg := sim.config
var crow := EnemySystem.TYPE_CROW
var thief := EnemySystem.TYPE_THIEF
var breaker := EnemySystem.TYPE_BREAKER
var ufo := EnemySystem.TYPE_UFO
r.check(n[0].type_counts().keys() == [crow], "night 1: crows only, at the crops (%s)" % [n[0].type_counts()])
var targets_2 := {}
for g in n[1].groups:
@ -74,17 +84,68 @@ func _designs(r: GameTestRunner, sim: FarmSim) -> void:
r.check(int(n[2].type_counts().get(breaker, 0)) >= 2 and int(n[2].type_counts().get(thief, 0)) >= 3, "night 3: fence breakers, and thieves behind them (%s)" % [n[2].type_counts()])
var crowded := _crowded_moments(n[3])
r.check(crowded >= 3, "night 4: %d moments with two groups arriving at once" % crowded)
var waves_5 := _waves(n[4])
r.check(n[4].type_counts().size() == 4 and waves_5 >= 3 and n[4].night_length > sim.config.night_length,
"night 5: all four types (including UFOs) in %d waves, over a longer %.0f s night" % [waves_5, n[4].night_length])
var ufo_groups := n[4].groups.filter(func(g: Dictionary) -> bool: return g["type"] == "ufo")
var ufos_5 := int(n[4].type_counts(n[4].count_scale_in(cfg)).get(ufo, 0))
r.check(n[4].type_counts().size() == 4 and ufo_groups.size() == 2 and ufo_groups.all(func(g: Dictionary) -> bool: return int(g["count"]) == 1)
and ufos_5 == 14 and _crowded_moments(n[4]) >= 3 and n[4].night_length == 0.0,
"night 5: all four types, the first UFOs in two single-UFO groups (%d as played), and night 4's moments at once" % ufos_5)
var last := n[n.size() - 1]
var waves_last := _waves(last)
r.check(last.type_counts().size() == 4 and waves_last >= 3 and last.night_length > cfg.night_length,
"the last night: all four types in %d waves, over a longer %.0f s night" % [waves_last, last.night_length])
var totals := []
for p in n:
totals.append(p.total(p.count_scale_in(sim.config)))
r.check(totals[4] == totals.max() and totals[0] == totals.min(), "the first night is the smallest, the last the biggest, as played (%s)" % [totals])
r.check(is_equal_approx(n[0].count_scale_in(sim.config), 1.0) and is_equal_approx(n[0].spacing_scale_in(sim.config), 1.0)
totals.append(p.total(p.count_scale_in(cfg)))
r.check(totals[n.size() - 1] == totals.max() and totals.count(totals.max()) == 1 and totals[0] == totals.min(),
"the first night is the smallest, the last the biggest, as played (%s)" % [totals])
r.check(is_equal_approx(n[0].count_scale_in(cfg), 1.0) and is_equal_approx(n[0].spacing_scale_in(cfg), 1.0)
and totals[0] <= 12 and n[1].count_scale == 0.0, "night 1 plays as designed, unscaled (%d crows); the later nights at the config's scale" % totals[0])
## #27: the thirty nights' arc (_validation checks each plan as played). Thirty plans; the fence
## breakers come from night 3 and the UFOs from night 5; as played, no night is more than 15% lighter than the
## non-siege night before it (from night 3: night 2 -> 3 trades crows and thieves for the first
## robots); every fifth night is a siege, about 30% heavier at the normal length; and the last
## night is the longest.
func _arc(r: GameTestRunner, sim: FarmSim) -> void:
var cfg := sim.config
var n := sim.waves.plans
r.check(cfg.campaign_nights == 30 and n.size() == 30, "the vacation is thirty nights, with a plan for each (%d)" % n.size())
var first := {}
for k in n.size():
for t in n[k].type_counts():
if not first.has(t):
first[t] = k + 1
var robots := int(first.get(EnemySystem.TYPE_BREAKER, 0))
var ufos := int(first.get(EnemySystem.TYPE_UFO, 0))
r.check(robots >= 3 and ufos >= 5, "no fence breaker before night 3 (the first on night %d), no UFO before night 5 (night %d)" % [robots, ufos])
var drops := PackedStringArray()
var before := 0
var before_night := 0
for k in range(2, n.size()):
var night := k + 1
if SIEGES.has(night):
continue
var played := n[k].total(n[k].count_scale_in(cfg))
if before > 0 and played < before * 0.85:
drops.append("night %d -> %d: %d -> %d" % [before_night, night, before, played])
before = played
before_night = night
r.check(drops.is_empty(), "as played, no night from 3 on is more than 15%% lighter than the non-siege night before it%s"
% ("" if drops.is_empty() else ": %s" % [drops]))
var sieges := true
for night in SIEGES:
var p := n[night - 1]
var prev := n[night - 2]
sieges = sieges and p.title.begins_with("Siege: ") and p.night_length == 0.0 and p.total() >= roundi(prev.total() * 1.2)
r.check(sieges, "nights %s are sieges: 'Siege: ...', about 30%% heavier than the night before, at the normal %.0f s" % [SIEGES, cfg.night_length])
var longest := true
var last_length := n[n.size() - 1].night_length
for k in n.size() - 1:
longest = longest and (n[k].night_length if n[k].night_length > 0.0 else cfg.night_length) < last_length
r.check(longest, "the last night is the longest (%.0f s)" % last_length)
## Times when two groups start arriving within 3 s of each other.
func _crowded_moments(plan: NightPlan) -> int:
var moments := {}
@ -236,10 +297,14 @@ func _enemy_scales(r: GameTestRunner, sim: FarmSim) -> void:
var fastest := 0.0
for v in es.t_speed:
fastest = maxf(fastest, v)
var unscaled := true
for p in w.plans:
unscaled = unscaled and p.enemy_speed_scale == 1.0 and p.enemy_hp_scale == 1.0
r.check(unscaled, "the vacation's %d nights keep the enemies' own speed and HP (scales 1)" % w.plans.size())
var as_planned := true
for k in w.plans.size():
var after_5 := maxi(k + 1 - 5, 0)
as_planned = (as_planned and is_equal_approx(w.plans[k].enemy_speed_scale, 1.0 + 0.01 * after_5)
and is_equal_approx(w.plans[k].enemy_hp_scale, 1.0 + 0.025 * after_5))
var last := w.plans[w.plans.size() - 1]
r.check(as_planned, "nights 1-5 keep the enemies' own speed and HP; from night 6 they rise 1%% and 2.5%% a night, to x%.2f and x%.3f on night %d"
% [last.enemy_speed_scale, last.enemy_hp_scale, w.plans.size()])
# A plan with the default scales: as before.
while c.state.phase != P.NIGHT:
c.advance_phase()
@ -354,11 +419,15 @@ func _night_jump(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void:
_step(sim, 1)
## All five nights back to back, each with its own plan and morning snapshot, to the farmers'
## return. The farm is defended, and the scarecrow kept out of harm.
func _five_nights(r: GameTestRunner, farm: Node3D, sim: FarmSim, player: PlayerCharacter) -> void:
## The vacation back to back to the farmers' return: the PLAYED nights (1-5, a siege, the last),
## each with its own plan and morning snapshot and its arrivals as planned; the nights between are
## skipped on F3's morning path (CampaignDebug.jump_morning: the next morning, the farm as it is).
## The farm is defended, and the scarecrow kept out of harm.
func _back_to_back(r: GameTestRunner, farm: Node3D, sim: FarmSim, player: PlayerCharacter) -> void:
var c := sim.campaign
var hud := farm.get_node("Hud/PhaseHud") as PhaseHud
var debug := farm.get_node("CampaignDebug") as CampaignDebug
var nights := c.config.campaign_nights
c.new_campaign()
_step(sim, 1)
sim.structures.grant_workbench() # #9: granted once, up front, so the per-night structure
@ -373,9 +442,16 @@ func _five_nights(r: GameTestRunner, farm: Node3D, sim: FarmSim, player: PlayerC
var broken: Array[int] = []
sim.structures.destroyed.connect(func(id: int, _t: String, _at: Vector3) -> void: broken.append(id))
var all_good := true
var lengths: Array[float] = []
var skipped_ok := true
var lengths := {}
var t0 := Time.get_ticks_msec()
for night in range(1, c.config.campaign_nights + 1):
for night: int in PLAYED:
while c.state.night < night and c.state.phase == P.MORNING:
var from := c.state.night
if not debug.jump_morning(1):
skipped_ok = false
break
skipped_ok = skipped_ok and c.state.night == from + 1 and c.state.nights_survived == from
var s := c.state
all_good = all_good and s.night == night and s.phase == P.MORNING and c._morning_snapshot == s.to_dict()
c.begin_day()
@ -384,26 +460,32 @@ func _five_nights(r: GameTestRunner, farm: Node3D, sim: FarmSim, player: PlayerC
s.resources.shed = {"wood": 99, "scrap": 99, "straw": 99}
while c.state.phase != P.NIGHT:
c.advance_phase()
lengths.append(c.state.phase_time_left)
lengths[night] = c.state.phase_time_left
hud._on_phase_changed(P.NIGHT, P.DUSK)
var title_ok := hud._banner.text == "Night %d: %s" % [night, sim.waves.plan_for(night).title]
var plan := sim.waves.plan_for(night)
var title_ok := hud._banner.text == "Night %d: %s" % [night, plan.title]
arrivals.clear()
var structures_at_night := c.state.structures.size()
broken.clear()
var t_night := Time.get_ticks_msec()
while c.state.phase != P.MORNING and not c.is_over():
sim.scarecrow.invulnerable_left = 1.0
sim.step(DT)
var expect := sim.waves.plan_for(night).type_counts(sim.waves.plan_for(night).count_scale_in(c.config))
r.check(arrivals == expect and title_ok, "night %d, '%s': the plan's %s arrived %s" % [night, sim.waves.plan_for(night).title, expect, arrivals])
var expect := plan.type_counts(plan.count_scale_in(c.config))
r.check(arrivals == expect and title_ok, "night %d, '%s': the plan's %s arrived %s" % [night, plan.title, expect, arrivals])
var e: Dictionary = c.state.night_log[c.state.night_log.size() - 1]
r.info("night %d: %d crops, %d animals, %d supplies lost; %d structures broken" % [night, e["crops_lost"], e["animals_lost"], e["supplies_lost"], broken.size()])
if night < c.config.campaign_nights:
r.info("night %d: %d crops, %d animals, %d supplies lost; %d structures broken (%.1f s)" % [night, e["crops_lost"],
e["animals_lost"], e["supplies_lost"], broken.size(), (Time.get_ticks_msec() - t_night) / 1000.0])
if night < nights:
all_good = all_good and c.state.structures.size() == structures_at_night - broken.size() and c.state.supplies_in_transit == 0
r.check(all_good, "every morning is its night's snapshot, and the farm carries over night to night")
r.check(c.state.phase == P.FARMERS_RETURN and c.state.nights_survived == 5 and c.state.night_log.size() == 5,
"all five nights played to the farmers' return (%.1f s)" % ((Time.get_ticks_msec() - t0) / 1000.0))
r.check(is_equal_approx(lengths[4], sim.waves.plans[4].night_length) and is_equal_approx(lengths[0], c.config.night_length),
"the last night is longer (%.0f s; the others %.0f s)" % [lengths[4], lengths[0]])
r.check(all_good and skipped_ok, "every morning is its night's snapshot, the farm carries over night to night, and F3 skips the nights between")
r.check(c.state.phase == P.FARMERS_RETURN and c.state.nights_survived == nights and c.state.night_log.size() == PLAYED.size(),
"nights %s played, the rest skipped, to the farmers' return after night %d (%.1f s)" % [PLAYED, nights, (Time.get_ticks_msec() - t0) / 1000.0])
var others := true
for night: int in PLAYED:
others = others and (night == nights or is_equal_approx(lengths[night], c.config.night_length))
r.check(is_equal_approx(lengths[nights], sim.waves.plans[nights - 1].night_length) and others,
"the last night is longer (%.0f s; the others %.0f s)" % [lengths[nights], lengths[1]])
sim.waves.enabled = false