harvest/game/tests/test_outcome.gd
2026-10-03 12:14:28 -04:00

111 lines
4.5 KiB
GDScript

extends RefCounted
## Outcome rules: scarecrow destruction, a stripped farm (only categories in play count),
## completing the vacation, the failure-before-completion tie policy, failure in every phase,
## and terminal phases staying terminal.
const O := CampaignController.Outcome
const P := CampaignController.Phase
func run(r: GameTestRunner) -> void:
var cfg := (load("res://game/resources/game_config.tres") as GameConfig).duplicate() as GameConfig
var n := cfg.campaign_nights
var fresh := FarmState.create(cfg)
r.check(CampaignController.check_outcome(fresh, n) == O.CONTINUE, "a fresh farm continues")
var s := FarmState.create(cfg)
s.scarecrow_hp = 0
r.check(CampaignController.check_outcome(s, n) == O.GAME_OVER_SCARECROW, "0 HP is a Game Over")
s = FarmState.create(cfg)
s.crops = 0
r.check(CampaignController.check_outcome(s, n) == O.GAME_OVER_STRIPPED, "with only crops in play, no crops left is a stripped farm")
s = _all_categories(cfg)
s.crops = 0
r.check(CampaignController.check_outcome(s, n) == O.CONTINUE, "losing every crop alone does not end the game")
s.animals = 0
r.check(CampaignController.check_outcome(s, n) == O.CONTINUE, "losing crops and animals with supplies left does not end the game")
s.supplies = 0
r.check(CampaignController.check_outcome(s, n) == O.GAME_OVER_STRIPPED, "losing every category ends the game")
s = FarmState.create(cfg)
s.crops_total = 0
s.crops = 0
r.check(not s.is_stripped(), "a farm with nothing in play is not 'stripped'")
s = FarmState.create(cfg)
s.nights_survived = n - 1
r.check(CampaignController.check_outcome(s, n) == O.CONTINUE, "%d nights survived continues" % (n - 1))
s.nights_survived = n
r.check(CampaignController.check_outcome(s, n) == O.VACATION_COMPLETE, "%d nights survived completes the vacation" % n)
# Tie policy: any failure beats completion; scarecrow destruction is reported first.
s.scarecrow_hp = 0
r.check(CampaignController.check_outcome(s, n) == O.GAME_OVER_SCARECROW, "tie: scarecrow destroyed on the final night loses")
s.scarecrow_hp = 1
s.crops = 0
r.check(CampaignController.check_outcome(s, n) == O.GAME_OVER_STRIPPED, "tie: farm stripped on the final night loses")
s.scarecrow_hp = 0
r.check(CampaignController.check_outcome(s, n) == O.GAME_OVER_SCARECROW, "scarecrow destruction is reported before a stripped farm")
_failure_in_every_phase(r)
_final_dawn_tie(r)
_terminal(r)
func _all_categories(cfg: GameConfig) -> FarmState:
var s := FarmState.create(cfg)
s.animals_total = 8
s.animals = 8
s.supplies_total = 50
s.supplies = 50
return s
func _failure_in_every_phase(r: GameTestRunner) -> void:
for phase: CampaignController.Phase in [P.MORNING, P.DAY, P.DUSK, P.NIGHT, P.DAWN]:
var c := r.make_campaign()
var ended: Array[CampaignController.Outcome] = []
c.campaign_ended.connect(func(o: CampaignController.Outcome) -> void: ended.append(o))
GameTestRunner.run_until(c, phase)
c.state.scarecrow_hp = 0
c.tick(GameTestRunner.TICK)
r.check(c.state.phase == P.GAME_OVER and c.outcome == O.GAME_OVER_SCARECROW and ended == [O.GAME_OVER_SCARECROW],
"scarecrow destroyed during %s ends the game once" % P.keys()[phase])
c = r.make_campaign()
GameTestRunner.run_until(c, phase)
c.state.crops = 0
c.tick(GameTestRunner.TICK)
r.check(c.state.phase == P.GAME_OVER and c.outcome == O.GAME_OVER_STRIPPED,
"farm stripped during %s ends the game" % P.keys()[phase])
## The scarecrow is destroyed on the very tick the final dawn ends: a loss, not a victory.
func _final_dawn_tie(r: GameTestRunner) -> void:
var c := r.make_campaign()
for i in c.config.campaign_nights - 1:
GameTestRunner.run_until(c, P.DAWN)
GameTestRunner.run_until(c, P.MORNING)
GameTestRunner.run_until(c, P.DAWN)
r.check(c.state.night == c.config.campaign_nights, "reached the final dawn")
c.state.phase_time_left = GameTestRunner.TICK * 0.5
c.state.scarecrow_hp = 0
c.tick(GameTestRunner.TICK)
r.check(c.state.phase == P.GAME_OVER and c.outcome == O.GAME_OVER_SCARECROW,
"tie on the final dawn tick resolves as a loss (got %s)" % P.keys()[c.state.phase])
func _terminal(r: GameTestRunner) -> void:
var c := r.make_campaign()
GameTestRunner.run_until(c, P.NIGHT)
c.state.scarecrow_hp = 0
c.tick(GameTestRunner.TICK)
var before := c.state.to_dict()
c.tick(1000.0)
c.advance_phase()
r.check(not c.begin_day(), "begin_day does nothing after Game Over")
c.state.scarecrow_hp = 5
c.tick(GameTestRunner.TICK)
before["scarecrow_hp"] = 5
r.check(c.state.to_dict() == before and c.state.phase == P.GAME_OVER, "Game Over is terminal: ticks and phase skips change nothing")