extends RefCounted ## One complete day and night on the real farm (FarmSim stepped by hand): the night plan's ## arrivals (times, targets, random points all along the farm's edge, nothing by day or after ## sunrise), the dusk warnings, the dawn ## retreat and its record, carrying everything into the next morning (structures and their damage, ## materials, losses in every category, the scarecrow's health), the morning review panel, ## retrying from the morning snapshot, the lighting of each phase, and the phase and asset HUDs. const P := CampaignController.Phase const DT := 1.0 / 60.0 const AWAY := Vector3(-11.0, 0.0, 13.0) var _broken: Array[int] = [] var _start_structures: int = 0 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 sim := farm.get_node("FarmSim") as FarmSim sim.set_physics_process(false) sim.waves.rng.seed = 1 # tonight's 11 arrival points, drawn the same every run var player := farm.get_node("Player") as PlayerCharacter player.global_position = AWAY sim.structures.grant_workbench() # #9: granted up front so it doesn't shift the structure # counts this test tracks across night 1 (it isn't about the Workbench itself) var review := farm.get_node("Hud/ReviewPanel") as ReviewPanel var hud := farm.get_node("Hud/PhaseHud") as PhaseHud var assets := " · ".join(PauseMenu.asset_counts(sim.campaign.state)) r.check(assets == "Crops 30/30 · Animals 8/8 · Supplies 10", "the pause menu's assets read '%s'" % assets) sim.structures.destroyed.connect(func(_id: int, _t: String, _at: Vector3) -> void: _broken.append(1)) _morning(r, sim, review, hud) _day(r, sim, player, hud) var before_night := sim.campaign.state.to_dict() _dusk(r, sim, hud) _night(r, sim) _dawn_and_morning(r, sim, review, before_night) _retry(r, sim) farm.queue_free() await r.process_frame func _morning(r: GameTestRunner, sim: FarmSim, review: ReviewPanel, hud: PhaseHud) -> void: r.check(sim.campaign.state.phase == P.MORNING and review.visible and review._title.text == "The farmers are away", "the first morning shows the review panel with the introduction") hud._process(0.0) r.check(hud.describe() == "Day 1 of %d · Morning" % sim.config.campaign_nights, "the phase HUD reads '%s'" % hud.describe()) sim.day_night.snap() var day_energy := sim.sun.light_energy r.check(sim.day_night.lantern.light_energy < 0.6 and day_energy > 0.8, "morning light: bright sun, lantern nearly off") review._start.pressed.emit() r.check(sim.campaign.state.phase == P.DAY and not review.visible, "Start the day ends the review") func _day(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter, hud: PhaseHud) -> void: var c := sim.campaign var s := c.state hud._process(0.0) r.check(hud.describe() == "Day 1 of %d · 3:00 until Dusk" % sim.config.campaign_nights, "the phase HUD reads '%s'" % hud.describe()) sim.day_night.snap() r.check(is_equal_approx(sim.day_night.lantern.light_energy, 0.0) and sim.sun.light_energy > 1.0, "daylight: full sun, no lantern") # A day's work: gather into the pouch, build, take some damage, patch up. var tree: GatherNode for n in sim.interactions.nodes: if n.name == "DeadTree": tree = n player.global_position = tree.global_position + Vector3(tree.reach * 0.7, 0.0, 0.0) Input.action_press("interact") _step(sim, roundi(c.config.gather_time * 60.0) + 2) Input.action_release("interact") _step(sim, 1) r.check(s.resources.pouch["wood"] == 2, "gathered 2 wood into the pouch") s.resources.shed = {"wood": 20, "scrap": 20, "straw": 20, "alien_goop": 0} _start_structures = sim.structures.count() var fence := sim.structures.place("fence", 30.0, -18.5, 0) sim.structures.place("automated_defense", 38.0, -17.0, 0) sim.structures.damage(fence, 6) s.scarecrow_hp = 7 player.global_position = AWAY r.check(sim.structures.count() == _start_structures + 2, "built a fence and an automated defense") var arrived: Array[int] = [] sim.waves.arrived.connect(func(h: int, _at: Vector2) -> void: arrived.append(h)) _step_until(sim, P.DUSK) r.check(arrived.is_empty() and sim.enemies.active_count == 0, "no thieves come by day") func _dusk(r: GameTestRunner, sim: FarmSim, hud: PhaseHud) -> void: var w := sim.waves var plan := w.tonight() r.check(plan != null and plan.total() == 11 and plan.type_counts() == {EnemySystem.TYPE_CROW: 11} and plan.total(plan.count_scale_in(sim.config)) == 11, "tonight's plan: %d crows, played as designed (%d)" % [plan.total() if plan else -1, plan.total(plan.count_scale_in(sim.config)) if plan else -1]) r.check(w.warning_active() and w.tonight_entries() == [[EnemySystem.TYPE_CROW, LootSystem.Category.CROP]], "dusk warns of what tonight brings: %s" % [w.tonight_entries()]) hud._process(0.0) var warning := "Tonight, from every side: crows" r.check(hud.hint == warning, "the HUD warns, by target: '%s'" % hud.hint.replace("\n", " / ")) var marked := w.get_children().filter(func(n: Node) -> bool: return n is Node3D and (n as Node3D).visible) r.check(marked.is_empty() and sim.alerts.collect().is_empty(), "nothing marks tonight's arrivals on the farm or at the screen edge") r.check(hud._dusk_hint.visible and hud._dusk_hint.text == plan.dusk_hint and hud._banner.text == "Dusk: %s" % plan.title, "the dusk banner names tonight, and its hint shows: '%s'" % hud._dusk_hint.text) r.check(sim.waves.enemies.active_count == 0, "still no thieves at dusk") func _night(r: GameTestRunner, sim: FarmSim) -> void: var c := sim.campaign var w := sim.waves var log: Array = [] var cb := func(h: int, at: Vector2) -> void: var d := sim.enemies.resolve_handle(h) log.append([w.night_time, at, int(sim.enemies.want[d]), int(sim.enemies.type[d])]) w.arrived.connect(cb) _step_until(sim, P.NIGHT) sim.day_night.snap() r.check(sim.sun.light_energy < float(DayNight.DAY["energy"]) and sim.day_night.lantern.light_energy > 0.0, "nightfall: the light drops and the lantern comes on") _step(sim, roundi(DayNight.NIGHTFALL * 60.0) + 2) sim.day_night.snap() r.check(sim.sun.light_energy < 0.5 and sim.day_night.lantern.light_energy > 1.5 and is_equal_approx(float(sim.day_night.current["glow"]), 1.0), "full night: moonlight, the lantern lit, enemies glowing") _step_until(sim, P.DAWN) w.arrived.disconnect(cb) var plan := w.tonight() var expect: Array = [] var cfg := sim.config for g in plan.groups: for k in NightPlan.scaled_count(g, plan.count_scale_in(cfg)): expect.append([float(g["time"]) + k * NightPlan.scaled_every(g, plan.spacing_scale_in(cfg)), WaveDirector.want_of(g), WaveDirector.type_of(g)]) expect.sort_custom(func(a: Array, b: Array) -> bool: return a[0] < b[0]) var match_all := log.size() == expect.size() var edge := sim.zones.edge(cfg.flyer_spawn_margin) # tonight is all crows var sides := {} var spots := {} for i in mini(log.size(), expect.size()): var at: Vector2 = log[i][1] match_all = match_all and absf(float(log[i][0]) - float(expect[i][0])) < DT * 1.5 and log[i][2] == expect[i][1] \ and log[i][3] == expect[i][2] and FarmZones.nearest_on_outline(edge, at).distance_to(at) < 0.01 sides[sim.zones.side_of(at)] = true spots[Vector2i(at.round())] = true r.check(match_all, "the night plays the plan: %d arrivals of their types at their times, after their targets, on the farm's edge (got %d)" % [expect.size(), log.size()]) # Tonight's few arrivals land on a handful of sides; many more draws cover all eight. var drawn := {} for type in [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF]: for k in 200: drawn[sim.zones.side_of(w.spawn_point(type))] = true r.check(sides.size() >= 3 and spots.size() > log.size() * 0.8 and drawn.size() == 8, "each arrives at its own random point, all around the farm (%d sides, %d spots for %d arrivals; %d sides in 400 draws)" % [ sides.size(), spots.size(), log.size(), drawn.size()]) var ev := c.state.night_events r.info("night 1 with the scarecrow away: %d crops lost (%d stolen), %d animals lost (%d grabbed, %d wandered out), %d supplies taken; fence at %s HP, %d structures broken" % [ c.state.crops_total - c.state.crops, int(ev.get("crops_stolen", 0)), c.state.animals_total - c.state.animals, int(ev.get("animals_stolen", 0)), int(ev.get("animals_strayed", 0)), int(ev.get("supplies_stolen", 0)), _fence_hp(sim), _broken.size()]) func _dawn_and_morning(r: GameTestRunner, sim: FarmSim, review: ReviewPanel, before: Dictionary) -> void: var c := sim.campaign r.check(c.state.phase == P.DAWN, "sunrise") var still := 0 for d in sim.enemies.active_count: var st := sim.enemies.state[d] if sim.enemies.alive[d] == 1 and st != ThiefBehavior.State.RETREAT and st != ThiefBehavior.State.CARRY: still += 1 r.check(still == 0, "at sunrise every thief retreats or runs with its loot") var lost_before_morning := c.state.crops_total - c.state.crops var stolen := int(c.state.night_events.get("crops_stolen", 0)) _step_until(sim, P.MORNING) var s := c.state r.check(sim.enemies.active_count == 0 and s.night == 2, "the next morning: no thieves left, night 2 ahead") var e: Dictionary = s.night_log[0] r.check(e["crops_lost"] == s.crops_total - s.crops and e["crops_lost"] >= lost_before_morning and e["crops_stolen"] == stolen, "the night's record: %s" % [e]) var built: int = before["structures"].size() r.check(s.to_dict()["structures"].size() == built - _broken.size() and _fence_hp(sim) <= sim.config.fence_hp - 6, "structures stand, damage and all (%d of %d; %d broken)" % [s.structures.size(), built, _broken.size()]) var shed_before := 0 for m in ResourceStore.MATERIALS: shed_before += int(before["resources"]["shed"][m]) r.check(s.resources.pouch == before["resources"]["pouch"] and s.resources.shed_total() == shed_before - int(e["supplies_lost"]), "materials carry over, less what thieves carried off (pouch %s, %d lost from the shed)" % [s.resources.pouch, e["supplies_lost"]]) r.check(s.animals == s.animals_total - s.lost_animals.size() and e["animals_lost"] == s.lost_animals.size() and s.supplies_in_transit == 0, "animal losses are recorded and no sack is left in transit") r.check(s.scarecrow_hp == 7, "the scarecrow's health carries over") r.check(review.visible and review._title.text == "Morning of day 2 of %d" % sim.config.campaign_nights and review._last_head.visible and review._last_head.text == "Night 1: Crows at the crops", "the morning screen shows last night's results") var rows := ReviewPanel.category_counts(e) r.check(review.grid_text() == "Crops %d %d %d\nAnimals %d %d %d\nSupplies %d %d %d" % (rows["Crops"] + rows["Animals"] + rows["Supplies"]), "with every category's taken, recovered, and lost") var killed := int(e["killed_crow"]) + int(e["killed_thief"]) + int(e["killed_breaker"]) + int(e["killed_ufo"]) r.check((killed == 0) == (review.enemies_text() == "None") and (int(e["killed_crow"]) == 0 or review.enemies_text().contains("Crows %d" % int(e["killed_crow"]))) and review.materials_text().begins_with("Wood 2") and int(e["gathered_wood"]) == 2 and int(e["structures_built"]) == 2 and int(e["structures_damaged"]) >= 6, "the enemies defeated and the day's gathering ('%s'; '%s')" % [ review.enemies_text(), review.materials_text()]) r.info("morning screen:\n" + review.grid_text() + "\nEnemies: " + review.enemies_text() + "\nMaterials: " + review.materials_text()) func _retry(r: GameTestRunner, sim: FarmSim) -> void: var c := sim.campaign var morning := c.state.to_dict() c.begin_day() _step(sim, 60) c.state.resources.shed["wood"] = 0 sim.structures.place("slowing_trap", 10.0, -10.0, 0) c.state.scarecrow_hp = 0 _step(sim, 1) r.check(c.state.phase == P.GAME_OVER, "the scarecrow falls on day 2") c.retry_night() _step(sim, 1) var now := c.state.to_dict() r.check(now == morning, "retry restores the morning snapshot exactly") var n: int = morning["structures"].size() r.check(sim.structures.count() == n and sim.structures._views.size() == n, "and the farm shows it (%d structures)" % n) func _fence_hp(sim: FarmSim) -> int: for e in sim.campaign.state.structures: if e["type"] == "fence": return int(e["hp"]) return -1 func _step_until(sim: FarmSim, phase: CampaignController.Phase) -> void: var guard := 0 while sim.campaign.state.phase != phase and not sim.campaign.is_over() and guard < 30000: sim.step(DT) guard += 1 func _step(sim: FarmSim, n: int) -> void: for i in n: sim.step(DT)