This commit is contained in:
Forrest 2026-10-03 12:14:28 -04:00
commit 276d43613d
1687 changed files with 109059 additions and 0 deletions

85
game/tests/run_tests.gd Normal file
View file

@ -0,0 +1,85 @@
class_name GameTestRunner
extends SceneTree
## Headless test runner for the game (same pattern as prototype/tests/run_tests.gd). Run:
## godot --headless --path . --script res://game/tests/run_tests.gd
## Optional: -- --suite=test_campaign (run one suite)
## Exit code = number of failures.
const SUITES := [
"res://game/tests/test_farm_state.gd",
"res://game/tests/test_outcome.gd",
"res://game/tests/test_campaign.gd",
"res://game/tests/test_farm_layout.gd",
]
const TICK := 1.0 / 60.0
var passes: int = 0
var failures: int = 0
var current: String = ""
func _initialize() -> void:
_main.call_deferred()
func _main() -> void:
await process_frame
var only := ""
for a in OS.get_cmdline_user_args():
if String(a).begins_with("--suite="):
only = String(a).trim_prefix("--suite=")
var t0 := Time.get_ticks_msec()
for path: String in SUITES:
if only != "" and path.get_file().get_basename() != only:
continue
current = path.get_file().get_basename()
print("== ", current)
var suite: Object = (load(path) as GDScript).new()
await suite.call("run", self)
print("TESTS: %d checks passed, %d failed (%.1f s)" % [passes, failures, (Time.get_ticks_msec() - t0) / 1000.0])
quit(mini(failures, 125))
func check(cond: bool, message: String) -> void:
if cond:
passes += 1
else:
failures += 1
printerr(" FAIL [%s] %s" % [current, message])
func info(message: String) -> void:
print(" ", message)
## A campaign in the tree (so it is freed at exit) driven only by explicit tick() calls.
func make_campaign(cfg: GameConfig = null) -> CampaignController:
var c := CampaignController.new()
c.config = cfg
root.add_child(c)
c.set_physics_process(false)
return c
## Ticks at 60 Hz until the phase changes; returns the game seconds that took (INF if it
## never changed within max_seconds).
static func run_phase(c: CampaignController, max_seconds: float = 1000.0) -> float:
var start := c.state.phase
var ticks := 0
while c.state.phase == start:
if ticks * TICK > max_seconds:
return INF
c.tick(TICK)
ticks += 1
return ticks * TICK
## Ticks at 60 Hz until the given phase is reached (or the campaign ends).
static func run_until(c: CampaignController, phase: CampaignController.Phase) -> void:
var guard := 0
while c.state.phase != phase and not c.is_over() and guard < 1000000:
if c.state.phase == CampaignController.Phase.MORNING:
c.begin_day()
else:
c.tick(TICK)
guard += 1

View file

@ -0,0 +1 @@
uid://bke1ak3n6vb4x

130
game/tests/test_campaign.gd Normal file
View file

@ -0,0 +1,130 @@
extends RefCounted
## Phase state machine: the untimed morning, phase order and lengths, five-night completion,
## 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)
_five_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 _five_nights(r: GameTestRunner) -> void:
var c := r.make_campaign()
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 == 5 and c.state.night == 5, "the vacation is exactly five nights (night %d, survived %d)" % [c.state.night, c.state.nights_survived])
r.check(mornings == [2, 3, 4, 5], "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() == 5, "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)
c.state.crops -= 3
GameTestRunner.run_until(c, P.DAWN)
c.state.crops -= 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")
c.state.crops -= 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)
c.state.crops -= 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)
c.state.crops -= 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")

View file

@ -0,0 +1 @@
uid://c2vk5glcqxcxr

View file

@ -0,0 +1,187 @@
extends RefCounted
## The playable farm: every zone exists once inside the 60 x 60 m map without overlapping,
## lookups by position, the crop units, the perimeter markers, a perimeter fence without holes
## except its gaps, ground navigation from every gap to every zone, scarecrow-only bounds,
## zone-to-zone travel times (printed as the table kept in game/LAYOUT.md), and the zone banner.
const RUN_SPEED := 5.0 # PlayerCharacter.run_speed
const MAX_TRAVEL := 12.0
const ENEMY_RADIUS := 0.35
const SCARECROW_RADIUS := 0.4
const CELL := 0.5
func run(r: GameTestRunner) -> void:
var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D
r.root.add_child(farm)
await r.process_frame
await r.physics_frame
var zones := farm.get_node("Zones") as FarmZones
r.check(zones != null, "farm.tscn has a FarmZones node")
if zones == null:
return
var map := zones.playable
r.check(is_equal_approx(map.size.x, 60.0) and is_equal_approx(map.size.y, 60.0), "the playable map is 60 x 60 m")
_zones(r, zones)
_crops(r, zones)
_markers(r, zones)
var nav := FarmNavigation.new()
nav.build(farm, map.grow(5.0), CELL, ENEMY_RADIUS)
r.info("enemy nav: %d cells, %d shapes rasterized, built in %.1f ms" % [nav.cell_count(), nav.shapes_rasterized, nav.build_usec / 1000.0])
_fence(r, zones, nav)
_enemy_routes(r, zones, nav)
_scarecrow(r, farm, zones)
_travel_times(r, farm, zones)
await _banner(r, farm, zones)
farm.queue_free()
await r.process_frame
func _zones(r: GameTestRunner, zones: FarmZones) -> void:
var all := zones.zones()
r.check(all.size() == FarmZone.Kind.size(), "one zone per kind (%d zones)" % all.size())
for kind: FarmZone.Kind in FarmZone.Kind.values():
var z := zones.zone(kind)
r.check(z != null and z.display_name != "", "zone %s exists and has a name" % FarmZone.Kind.keys()[kind])
for i in all.size():
var a := all[i]
r.check(zones.playable.encloses(a.rect()), "%s lies inside the playable map" % a.display_name)
r.check(zones.zone_at(a.global_position) == a, "zone_at(center of %s) finds it" % a.display_name)
for j in range(i + 1, all.size()):
r.check(not a.rect().intersects(all[j].rect()), "%s and %s don't overlap" % [a.display_name, all[j].display_name])
r.check(zones.zone_at(Vector3(8.0, 0.0, -10.0)) == null, "open ground between zones belongs to no zone")
func _crops(r: GameTestRunner, zones: FarmZones) -> void:
var cfg := load("res://game/resources/game_config.tres") as GameConfig
var units := zones.crop_units()
var field := zones.zone(FarmZone.Kind.CROP_FIELD)
r.check(units.size() == cfg.start_crops, "%d crop units, matching GameConfig.start_crops (%d)" % [units.size(), cfg.start_crops])
var ids := {}
var per_plot := {}
var fewest := 1 << 30
for u in units:
ids[u.unit_id] = true
per_plot[u.plot] = int(per_plot.get(u.plot, 0)) + 1
fewest = mini(fewest, u.get_child_count())
r.check(field.contains(u.global_position), "crop unit %d lies in the crop field" % u.unit_id)
r.check(ids.size() == units.size() and ids.has(1) and ids.has(units.size()), "crop unit ids are unique, 1..%d" % units.size())
r.check(per_plot.size() == 6 and per_plot.values().all(func(n: int) -> bool: return n == 5), "6 plots of 5 units (%s)" % [per_plot])
r.check(fewest >= 8, "every unit is a visible cluster (fewest plants: %d)" % fewest)
func _markers(r: GameTestRunner, zones: FarmZones) -> void:
var map := zones.playable
var gaps := zones.markers(FarmMarker.Kind.GAP)
r.check(gaps.size() == 6, "6 perimeter gaps (got %d)" % gaps.size())
for m in gaps:
var p := Vector2(m.global_position.x, m.global_position.z)
r.check(not map.has_point(p) and map.grow(4.0).has_point(p), "%s sits just outside the perimeter" % m.name)
var crows := zones.markers(FarmMarker.Kind.CROW)
r.check(crows.size() == 4, "4 crow approach directions (got %d)" % crows.size())
for m in crows:
r.check(not map.grow(4.0).has_point(Vector2(m.global_position.x, m.global_position.z)), "%s is well outside the fence" % m.name)
## Every free cell on the fence line is part of a gap (near a gap marker), and every gap is open.
func _fence(r: GameTestRunner, zones: FarmZones, nav: FarmNavigation) -> void:
var map := zones.playable
var gaps := zones.markers(FarmMarker.Kind.GAP)
var corners := [map.position, Vector2(map.end.x, map.position.y), map.end, Vector2(map.position.x, map.end.y)]
var holes: Array[Vector2] = []
for side in 4:
var a: Vector2 = corners[side]
var b: Vector2 = corners[(side + 1) % 4]
var steps := ceili(a.distance_to(b) / (CELL * 0.5))
for k in steps + 1:
var p := a.lerp(b, k / float(steps))
if nav.is_free(nav.cell_of(p.x, p.y)) and not gaps.any(func(m: FarmMarker) -> bool:
return Vector2(m.global_position.x, m.global_position.z).distance_to(p) < 4.5):
holes.append(p)
r.check(holes.is_empty(), "the perimeter fence has no holes outside its gaps %s" % [holes.slice(0, 5)])
for m in gaps:
var g := Vector2(m.global_position.x, m.global_position.z)
var edge := Vector2(clampf(g.x, map.position.x, map.end.x), clampf(g.y, map.position.y, map.end.y))
r.check(nav.is_free(nav.cell_of(edge.x, edge.y)), "%s is open for ground enemies" % m.name)
func _enemy_routes(r: GameTestRunner, zones: FarmZones, nav: FarmNavigation) -> void:
var gaps := zones.markers(FarmMarker.Kind.GAP)
for zone in zones.zones():
var goal := nav.free_cells_in(zone.rect())
r.check(goal.size() > 0, "%s has walkable ground (%d cells)" % [zone.display_name, goal.size()])
var field := nav.distance_field(goal)
var lengths: Array[String] = []
for m in gaps:
var c := nav.nearest_free(nav.cell_of(m.global_position.x, m.global_position.z))
var reachable := c >= 0 and field[c] < FarmNavigation.UNREACHABLE
r.check(reachable, "ground enemies can walk from %s to the %s and back" % [m.name, zone.display_name])
lengths.append("%.0f" % nav.meters(field[c]) if reachable else "-")
r.info("%-12s from gaps (m): %s" % [zone.display_name, ", ".join(lengths)])
func _scarecrow(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void:
var player := farm.get_node("Player") as CharacterBody3D
var bounds := farm.get_node("Zones/Perimeter/PlayerBounds") as StaticBody3D
r.check(zones.playable.has_point(Vector2(player.global_position.x, player.global_position.z)), "the scarecrow starts inside the map")
r.check(bounds.collision_layer == 4 and (player.collision_mask & 4) != 0 and (player.collision_mask & 1) != 0,
"PlayerBounds is on the player_bounds layer, which only the scarecrow collides with")
# Stand in each gap and try to walk out: the invisible wall stops the scarecrow.
var start := player.global_transform
for m in zones.markers(FarmMarker.Kind.GAP):
var g := Vector2(m.global_position.x, m.global_position.z)
var map := zones.playable
var edge := Vector2(clampf(g.x, map.position.x, map.end.x), clampf(g.y, map.position.y, map.end.y))
var out := (g - edge).normalized()
player.global_position = Vector3(edge.x - out.x * 1.0, 0.3, edge.y - out.y * 1.0)
var hit := player.move_and_collide(Vector3(out.x, 0.0, out.y) * 3.0, true)
r.check(hit != null, "the scarecrow can't walk out through %s" % m.name)
player.global_transform = start
func _travel_times(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void:
var nav := FarmNavigation.new()
nav.build(farm, zones.playable, CELL, SCARECROW_RADIUS, 1 | 4)
var all := zones.zones()
var anchors := PackedInt32Array()
for z in all:
anchors.append(nav.nearest_free(nav.cell_of(z.global_position.x, z.global_position.z)))
var header := "| From \\ to |"
var rule := "|---|"
for z in all:
header += " %s |" % z.display_name
rule += "---|"
r.info(header)
r.info(rule)
var worst := 0.0
var worst_pair := ""
for i in all.size():
var field := nav.distance_field(PackedInt32Array([anchors[i]]))
var row := "| **%s** |" % all[i].display_name
for j in all.size():
var cost := field[anchors[j]]
r.check(cost < FarmNavigation.UNREACHABLE, "the scarecrow can walk from %s to %s" % [all[i].display_name, all[j].display_name])
var seconds := nav.meters(cost) / RUN_SPEED
row += " — |" if i == j else " %.1f s |" % seconds
if seconds > worst:
worst = seconds
worst_pair = "%s -> %s" % [all[i].display_name, all[j].display_name]
r.info(row)
r.check(worst <= MAX_TRAVEL, "no key area is more than %.0f s from another (worst %.1f s: %s)" % [MAX_TRAVEL, worst, worst_pair])
func _banner(r: GameTestRunner, farm: Node3D, zones: FarmZones) -> void:
var banner := farm.get_node("ZoneBanner") as ZoneBanner
var player := farm.get_node("Player") as Node3D
var start := player.global_transform
for zone in zones.zones():
player.global_position = zone.global_position + Vector3(0.0, 0.5, 0.0)
await r.process_frame # emitted before _process, so wait for the next one too
await r.process_frame
r.check(banner.current == zone, "walking into the %s shows its name" % zone.display_name)
player.global_position = Vector3(8.0, 0.5, -10.0)
await r.process_frame
await r.process_frame
r.check(banner.current == null, "walking out of every zone clears the banner")
player.global_transform = start

View file

@ -0,0 +1 @@
uid://bak4w33e4nqeq

View file

@ -0,0 +1,71 @@
extends RefCounted
## FarmState: creation from config, exact to_dict/from_dict round trips (also through JSON),
## rejection of invalid data, snapshot independence, and night loss records.
func run(r: GameTestRunner) -> void:
var cfg := (load("res://game/resources/game_config.tres") as GameConfig).duplicate() as GameConfig
var s := FarmState.create(cfg)
r.check(s.night == 1 and s.nights_survived == 0, "a new farm starts before night 1")
r.check(s.phase == CampaignController.Phase.MORNING, "a new farm starts in the morning")
r.check(s.scarecrow_hp == cfg.scarecrow_max_hp and s.scarecrow_max_hp == cfg.scarecrow_max_hp, "scarecrow starts at full HP")
r.check(s.crops == cfg.start_crops and s.crops_total == cfg.start_crops, "crops start at the configured count")
r.check(s.animals_total == 0 and s.supplies_total == 0, "animals and supplies are not in play until Step 8")
r.check(s.is_valid(), "a new farm is valid")
# A state with every field away from its default.
s.night = 3
s.nights_survived = 2
s.phase = CampaignController.Phase.DUSK
s.phase_time_left = 12.5
s.scarecrow_hp = 6
s.crops_total = 40
s.crops = 31
s.animals_total = 8
s.animals = 5
s.supplies_total = 120
s.supplies = 77
s.night_log.append({"night": 1, "crops_lost": 4, "animals_lost": 0, "supplies_lost": 0})
s.night_log.append({"night": 2, "crops_lost": 5, "animals_lost": 3, "supplies_lost": 43})
var d := s.to_dict()
var copy := FarmState.from_dict(d)
r.check(copy != null and copy.to_dict() == d, "to_dict -> from_dict round trip is exact")
var parsed: Variant = JSON.parse_string(JSON.stringify(d))
var from_json := FarmState.from_dict(parsed as Dictionary)
r.check(from_json != null and from_json.to_dict() == d, "round trip through JSON is exact (numbers coerced back to ints)")
r.check(from_json != null and typeof(from_json.night_log[1]["supplies_lost"]) == TYPE_INT, "night log values are ints after JSON")
d["night_log"][0]["crops_lost"] = 99
r.check(s.night_log[0]["crops_lost"] == 4, "a snapshot is independent of the live state")
_rejects(r, s.to_dict())
_night_records(r, cfg)
func _rejects(r: GameTestRunner, good: Dictionary) -> void:
var cases := {
"another version": func(d: Dictionary) -> void: d["version"] = FarmState.VERSION + 1,
"a missing key": func(d: Dictionary) -> void: d.erase("crops"),
"an unknown phase": func(d: Dictionary) -> void: d["phase"] = 42,
"more crops than the total": func(d: Dictionary) -> void: d["crops"] = d["crops_total"] + 1,
"negative animals": func(d: Dictionary) -> void: d["animals"] = -1,
"HP above max": func(d: Dictionary) -> void: d["scarecrow_hp"] = d["scarecrow_max_hp"] + 1,
"night 0": func(d: Dictionary) -> void: d["night"] = 0,
"more nights survived than played": func(d: Dictionary) -> void: d["nights_survived"] = d["night"] + 1,
"a malformed night log": func(d: Dictionary) -> void: d["night_log"] = [{"night": 1}],
}
for name: String in cases:
var d := good.duplicate(true)
(cases[name] as Callable).call(d)
r.check(FarmState.from_dict(d) == null, "from_dict rejects %s" % name)
func _night_records(r: GameTestRunner, cfg: GameConfig) -> void:
var s := FarmState.create(cfg)
var start := s.asset_counts()
s.crops -= 7
s.log_night(start)
var e: Dictionary = s.night_log[0]
r.check(s.night_log.size() == 1 and e["night"] == 1 and e["crops_lost"] == 7 and e["animals_lost"] == 0,
"log_night records the losses since the night started")

View file

@ -0,0 +1 @@
uid://dll6k5miikrur

111
game/tests/test_outcome.gd Normal file
View file

@ -0,0 +1,111 @@
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")

View file

@ -0,0 +1 @@
uid://ylvc3hivyh53