harvest/game/tests/test_campaign.gd
Forrest ab1443325a Task #27: the thirty nights
- campaign_nights 5 -> 30. The old night 5 ("The last night") is now night 30, keeping its
  uid, title, hint, and 180 s. It grew to 73 base / 511 played in its three waves, with UFO
  groups added, at speed x1.25 and HP x1.625.
- New hand-authored night 5, "Lights in the sky": night 4's shape plus the first UFOs (two
  single-UFO groups, 14 as played), 24 base.
- New game/tools/make_nights.gd writes nights 6-29 by the plan's recipe (seed 30, sieges on
  10/15/20/25, mix, waves, scales, draft titles and hints) as .tres text with fresh uids. It
  skips existing files unless --force (which keeps their uids), refuses ranges outside 6..29,
  and checks every plan with NightPlan.problems() at the played scales (non-zero exit on any
  problem). Generated nights 6-29 are committed.
- test_nights: the arc (30 plans, breakers from night 3 and UFOs from night 5, no non-siege drop
  over 15% from night 3 on, sieges, last night longest), the per-night scales, the new night 5
  and night 30 designs. The back-to-back run plays nights 1-5, 10, and 30, skipping the rest
  with CampaignDebug.jump_morning.
- test_campaign, test_cycle, test_flow read campaign_nights instead of a hard-coded 5.
- RULES (the vacation, the Night row, the thirty nights and their table, the vacation's end),
  README, and a dated PLAYTEST section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 16:38:07 -04:00

133 lines
6.4 KiB
GDScript

extends RefCounted
## Phase state machine: the untimed morning, phase order and lengths, completing the vacation
## (GameConfig.campaign_nights nights), the early-night option, night loss records, and retrying a
## failed night from its morning.
const O := CampaignController.Outcome
const P := CampaignController.Phase
func run(r: GameTestRunner) -> void:
_order_and_timers(r)
_all_nights(r)
_early_night(r)
_night_log(r)
_retry(r)
func _order_and_timers(r: GameTestRunner) -> void:
var c := r.make_campaign()
var seen: Array[CampaignController.Phase] = []
c.phase_changed.connect(func(p: CampaignController.Phase, _prev: CampaignController.Phase) -> void: seen.append(p))
r.check(c.state.phase == P.MORNING and c.state.night == 1, "a new vacation starts on the morning of night 1")
c.tick(1000.0)
r.check(c.state.phase == P.MORNING, "the morning review is untimed (waits for the player)")
r.check(c.begin_day(), "begin_day ends the morning review")
r.check(not c.begin_day(), "begin_day only works in the morning")
var cfg := c.config
var expected := {P.DAY: cfg.day_length, P.DUSK: cfg.dusk_length, P.NIGHT: cfg.night_length, P.DAWN: cfg.dawn_grace}
for phase: CampaignController.Phase in [P.DAY, P.DUSK, P.NIGHT, P.DAWN]:
r.check(c.state.phase == phase and is_equal_approx(c.state.phase_time_left, expected[phase]),
"%s starts with %.0f s on the clock" % [P.keys()[phase], expected[phase]])
var took := GameTestRunner.run_phase(c)
r.check(absf(took - expected[phase]) <= GameTestRunner.TICK + 1e-4,
"%s lasts %.0f s of game time (took %.3f s)" % [P.keys()[phase], expected[phase], took])
r.check(seen == [P.DAY, P.DUSK, P.NIGHT, P.DAWN, P.MORNING], "phases run MORNING -> DAY -> DUSK -> NIGHT -> DAWN -> MORNING")
r.check(c.state.night == 2 and c.state.nights_survived == 1, "after the first dawn it is the morning of night 2")
# advance_phase (debug) goes through the same transitions.
c.advance_phase()
r.check(c.state.phase == P.DAY, "advance_phase ends the morning")
c.advance_phase()
c.advance_phase()
c.advance_phase()
c.advance_phase()
r.check(c.state.phase == P.MORNING and c.state.night == 3, "advance_phase walks a whole cycle")
func _all_nights(r: GameTestRunner) -> void:
var c := r.make_campaign()
var nights := c.config.campaign_nights
var ended: Array[CampaignController.Outcome] = []
var mornings: Array[int] = []
c.campaign_ended.connect(func(o: CampaignController.Outcome) -> void: ended.append(o))
c.phase_changed.connect(func(p: CampaignController.Phase, _prev: CampaignController.Phase) -> void:
if p == P.MORNING:
mornings.append(c.state.night))
GameTestRunner.run_until(c, P.FARMERS_RETURN)
r.check(c.state.phase == P.FARMERS_RETURN and c.outcome == O.VACATION_COMPLETE, "surviving every night brings the farmers back")
r.check(c.state.nights_survived == nights and c.state.night == nights,
"the vacation is exactly campaign_nights (%d) nights (night %d, survived %d)" % [nights, c.state.night, c.state.nights_survived])
r.check(mornings == range(2, nights + 1), "a morning follows each night except the last (got %s)" % [mornings])
r.check(ended == [O.VACATION_COMPLETE], "campaign_ended fires once")
r.check(c.state.night_log.size() == nights, "every night is recorded")
c.tick(1000.0)
c.advance_phase()
r.check(c.state.phase == P.FARMERS_RETURN and ended.size() == 1, "the farmers' return is terminal")
c.new_campaign()
r.check(c.state.phase == P.MORNING and c.state.night == 1 and c.outcome == O.CONTINUE and c.state.night_log.is_empty(),
"new_campaign starts a fresh vacation")
func _early_night(r: GameTestRunner) -> void:
var c := r.make_campaign()
r.check(not c.config.allow_early_night, "starting the night early is off by default")
c.begin_day()
r.check(not c.can_start_night_early() and not c.start_night_early() and c.state.phase == P.DAY,
"with the option off, the day can't be cut short")
GameTestRunner.run_until(c, P.DUSK)
r.check(not c.start_night_early() and c.state.phase == P.DUSK, "with the option off, dusk can't be cut short")
var cfg := c.config.duplicate() as GameConfig
cfg.allow_early_night = true
c = r.make_campaign(cfg)
r.check(not c.start_night_early(), "the night can't start early from the morning review")
c.begin_day()
c.tick(10.0)
r.check(c.start_night_early() and c.state.phase == P.NIGHT and is_equal_approx(c.state.phase_time_left, cfg.night_length),
"with the option on, the day can skip straight to a full night")
r.check(not c.start_night_early(), "the night can't be started early again")
c = r.make_campaign(cfg)
GameTestRunner.run_until(c, P.DUSK)
r.check(c.start_night_early() and c.state.phase == P.NIGHT, "with the option on, dusk can skip to the night")
func _night_log(r: GameTestRunner) -> void:
var c := r.make_campaign()
GameTestRunner.run_until(c, P.NIGHT)
GameTestRunner.lose_crops(c.state, 3)
GameTestRunner.run_until(c, P.DAWN)
GameTestRunner.lose_crops(c.state, 1) # loot escaping during the dawn grace still counts for that night
GameTestRunner.run_until(c, P.MORNING)
var e: Dictionary = c.state.night_log[0]
r.check(e["night"] == 1 and e["crops_lost"] == 4, "night 1 records 4 crops lost, dawn included (got %s)" % [e])
r.check(c.state.crops == c.config.start_crops - 4, "losses persist into the next day")
GameTestRunner.lose_crops(c.state, 2) # lost during the day don't count against the next night
c.begin_day()
GameTestRunner.run_until(c, P.MORNING)
r.check(c.state.night_log[1]["crops_lost"] == 0, "night 2 only counts losses after it started")
func _retry(r: GameTestRunner) -> void:
var c := r.make_campaign()
GameTestRunner.run_until(c, P.NIGHT)
GameTestRunner.lose_crops(c.state, 3)
c.state.scarecrow_hp -= 4
GameTestRunner.run_until(c, P.MORNING)
var morning := c.state.to_dict()
c.begin_day()
GameTestRunner.run_until(c, P.NIGHT)
GameTestRunner.lose_crops(c.state, 5)
c.state.scarecrow_hp = 0
c.tick(GameTestRunner.TICK)
r.check(c.state.phase == P.GAME_OVER, "the scarecrow falls on night 2")
r.check(c.retry_night(), "retry_night is available after Game Over")
r.check(c.state.to_dict() == morning, "retry restores the morning of the failed night exactly")
r.check(c.state.night == 2 and c.state.scarecrow_hp == c.config.scarecrow_max_hp - 4 and c.outcome == O.CONTINUE,
"night 1's losses and damage are kept; night 2 is replayed")
c.begin_day()
GameTestRunner.run_until(c, P.MORNING)
r.check(c.state.night == 3 and c.state.night_log.size() == 2, "the retried night plays through normally")