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. 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/" 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.enabled = false var player := farm.get_node("Player") as PlayerCharacter player.global_position = AWAY _validation(r, sim) _designs(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) farm.queue_free() await r.process_frame ## Every plan (the five 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) for f in DirAccess.get_files_at(SCENARIOS): if f.ends_with(".tres"): plans.append(load(SCENARIOS + f) as NightPlan) for plan in plans: var length := plan.night_length if plan.night_length > 0.0 else sim.config.night_length var bad := plan.problems(length, plan.count_scale_in(sim.config), plan.spacing_scale_in(sim.config)) r.check(bad.is_empty() and plan.title != "", "'%s' is valid%s" % [plan.title, "" if bad.is_empty() else ": %s" % [bad]]) # The checks catch mistakes. var broken := NightPlan.new() broken.groups = [{"type": "crow", "target": "crops", "time": 5.0, "count": 1, "every": 1.0}, {"type": "thief", "target": "crops", "time": 200.0, "count": 2, "every": 1.0}, {"type": "crow", "target": "animals", "time": 5.0, "count": 0, "every": 1.0}, {"type": "dragon", "time": 5.0, "count": 1, "every": 1.0}] var found := broken.problems(150.0) r.check(found.size() == 5, "a broken plan is caught: %s" % [found]) for k in sim.waves.plans.size(): 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. func _designs(r: GameTestRunner, sim: FarmSim) -> void: var w := sim.waves var n := w.plans var crow := EnemySystem.TYPE_CROW var thief := EnemySystem.TYPE_THIEF var breaker := EnemySystem.TYPE_BREAKER 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: if g["type"] == "thief": targets_2[g["target"]] = true r.check(n[1].type_counts().has(crow) and n[1].type_counts().has(thief) and not n[1].type_counts().has(breaker) and targets_2.has("supplies") and targets_2.has("animals"), "night 2: crows, plus thieves at the shed and the pen") 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 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) 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]) ## Times when two groups start arriving within 3 s of each other. func _crowded_moments(plan: NightPlan) -> int: var moments := {} for i in plan.groups.size(): for j in range(i + 1, plan.groups.size()): var a := float(plan.groups[i]["time"]) var b := float(plan.groups[j]["time"]) if absf(a - b) <= 3.0: moments[roundi(minf(a, b) / 10.0)] = true return moments.size() ## Clusters of arrivals at least 25 s apart. func _waves(plan: NightPlan) -> int: var times: Array[float] = [] for g in plan.groups: times.append(float(g["time"])) times.sort() var waves := 1 for k in range(1, times.size()): if times[k] - times[k - 1] >= 25.0: waves += 1 return waves ## The upgrades change the systems they're about, can't be bought twice or at night, and persist. func _upgrades(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var c := sim.campaign var s := c.state var cfg := sim.config var es := sim.enemies var st := sim.structures c.begin_day() s.resources.shed = {"wood": 200, "scrap": 200, "straw": 200, "alien_goop": 200} s.night_events["gathered_alien_goop"] = cfg.hidden_reveal_goop # so the Alien Ray (#7, hidden) buys below var fence := st.place("fence", 8.0, -8.0, 0) _settle(sim) var before := { "interval": sim.combat.interval_ticks(), "damage": sim.combat.shot_damage(), "radius": sim.scarecrow.fear_radius(), "cooldown": sim.scarecrow.fear_cooldown(), "max_hp": s.scarecrow_max_hp, "fence": st.max_hp("fence"), "gate": st.max_hp("gate"), "range": st.turret_range(), "slow": st.trap_slow_time(), } var morning := s.to_dict() var shed := s.resources.shed_total() var spent := 0 r.check(not player.can_sprint(), "(no sprint before the Sprint upgrade)") for id in Upgrades.ORDER: spent += _sum(Upgrades.cost(cfg, id)) var text := Upgrades.buy(c, id, st) r.check(Upgrades.owned(s, id) and text.contains("done:"), "bought %s: '%s'" % [id, text]) r.check(s.resources.shed_total() == shed - spent, "all %d cost %d materials together" % [Upgrades.ORDER.size(), spent]) r.check(player.can_sprint(), "sprint: the scarecrow can sprint") r.check(Upgrades.buy(c, "rapid_fire", st).contains("already done"), "an upgrade can't be bought twice") r.check(sim.combat.interval_ticks() < int(before["interval"]) and sim.combat.shot_damage() == int(before["damage"]) + 1, "rapid fire and heavy seeds: a shot every %d ticks (was %d), %d damage" % [sim.combat.interval_ticks(), before["interval"], sim.combat.shot_damage()]) r.check(is_equal_approx(sim.scarecrow.fear_radius(), float(before["radius"]) * 1.3) and is_equal_approx(sim.scarecrow.fear_cooldown(), float(before["cooldown"]) * 0.8), "wide and quick fear: %.1f m, %.1f s" % [sim.scarecrow.fear_radius(), sim.scarecrow.fear_cooldown()]) r.check(s.scarecrow_max_hp == int(before["max_hp"]) + 2 and s.scarecrow_hp == s.scarecrow_max_hp, "straw stuffing: +2 max health") r.check(st.max_hp("fence") == roundi(int(before["fence"]) * 1.5) and st.max_hp("gate") == roundi(int(before["gate"]) * 1.5) and int(s.structure(fence)["hp"]) == st.max_hp("fence"), "reinforced fences: fences %d HP, gates %d; a standing fence keeps its full health" % [st.max_hp("fence"), st.max_hp("gate")]) r.check(is_equal_approx(st.turret_range(), float(before["range"]) * 1.3) and is_equal_approx(st.trap_slow_time(), float(before["slow"]) * 1.5), "long-range defenses %.1f m, sticky tar %.1f s" % [st.turret_range(), st.trap_slow_time()]) # The effects are real: a shot does 2 damage, a defense reaches a thief beyond its old range, # a trap holds longer. var keep := es.behavior es.behavior = null var h := es.spawn(EnemySystem.TYPE_THIEF, 10.0, -8.0, Vector2.DOWN) _step(sim, 1) sim.projectiles.fire(Vector3(10.0, cfg.combat_height, -2.0), Vector3(10.0, cfg.combat_height, -3.0), Vector3(0, 0, -1), sim.combat.shot_damage()) for i in 20: sim.projectiles.step(DT) r.check(es.health[es.resolve_handle(h)] == es.t_hp[EnemySystem.TYPE_THIEF] - 2, "a heavy seed takes 2 health off a thief") es.apply_hit_handle(h, 99) st.dismantle(fence) var turret := st.place("automated_defense", 6.0, -13.0, 0) var far := es.spawn(EnemySystem.TYPE_THIEF, 6.0 + float(before["range"]) + 1.0, -13.0, Vector2.UP) _step(sim, 90) r.check(es.resolve_handle(far) < 0 or es.health[es.resolve_handle(far)] < es.t_hp[EnemySystem.TYPE_THIEF], "a defense hits a thief %.0f m away (its old range: %.0f m)" % [float(before["range"]) + 1.0, before["range"]]) for d in es.active_count: es.apply_hit(d, 99) st.dismantle(turret) var trap := st.place("slowing_trap", 8.0, -12.0, 0) _settle(sim) h = es.spawn(EnemySystem.TYPE_THIEF, 8.0, -12.0, Vector2.RIGHT) _step(sim, 1) r.check(absf(es.slow_left[es.resolve_handle(h)] - st.trap_slow_time()) < 0.05, "a trap slows for %.1f s" % es.slow_left[es.resolve_handle(h)]) es.apply_hit_handle(h, 99) st.dismantle(trap) es.behavior = keep _step(sim, 1) # They persist: into the next morning, through JSON, and in a retry from that morning; a retry # from before they were bought takes them back. var bought := s.upgrades.duplicate() while s.phase != P.NIGHT: c.advance_phase() r.check(Upgrades.blocker(c, "rapid_fire") == "Done" and Upgrades.buy(c, "long_range", st).contains("already done"), "(owned ones stay owned at night)") s.upgrades.erase("sticky_tar") r.check(Upgrades.blocker(c, "sticky_tar") == "The work area is for daytime", "nothing can be bought at night") s.upgrades["sticky_tar"] = 1 while s.phase != P.MORNING: c.advance_phase() r.check(s.upgrades == bought, "the upgrades carry into the next morning") var back := FarmState.from_dict(JSON.parse_string(JSON.stringify(s.to_dict(), "", true, true))) r.check(back != null and back.upgrades == bought, "and through a save") c.begin_day() s.scarecrow_hp = 0 _step(sim, 1) c.retry_night() r.check(s != c.state and c.state.upgrades == bought, "a retry from that morning keeps them") var old := FarmState.from_dict(morning) r.check(old.upgrades.is_empty() and old.resources.shed_total() == shed, "(the morning before they were bought had none, and the materials)") c.new_campaign() _step(sim, 1) r.check(c.state.upgrades.is_empty() and sim.combat.shot_damage() == 1 and st.max_hp("fence") == cfg.fence_hp and not player.can_sprint(), "a new vacation starts without them") player.global_position = AWAY ## #24: the plans load by count, night_plan_path(n) for every night of the vacation; a missing ## file plays the last plan that loaded. func _plan_paths(r: GameTestRunner, sim: FarmSim) -> void: var nights := sim.config.campaign_nights var all_load := sim.waves.plans.size() == nights for n in range(1, nights + 1): var path := FarmSim.night_plan_path(n) var plan: NightPlan = load(path) as NightPlan if ResourceLoader.exists(path) else null all_load = all_load and path == "res://game/resources/nights/night_%d.tres" % n and plan != null and sim.waves.plans[n - 1] == plan r.check(all_load, "night_plan_path(n) loads a plan for every night 1..%d, in order" % nights) r.info("(the error just below, about night %d's missing plan, is expected)" % (nights + 1)) var more := FarmSim.load_night_plans(nights + 1) r.check(more.size() == nights + 1 and more[nights] == more[nights - 1] and more[nights] != null, "a night without a plan file plays the last plan that loaded") ## #24: tonight's enemy scales. The vacation's own plans keep every enemy as it was; a plan with ## enemy_speed_scale and enemy_hp_scale makes that night's enemies faster and tougher (HP rounded ## down, never below the type's: a crow stays one hit); the morning puts them back to 1. func _enemy_scales(r: GameTestRunner, sim: FarmSim) -> void: var c := sim.campaign var es := sim.enemies var w := sim.waves c.new_campaign() _step(sim, 1) 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()) # A plan with the default scales: as before. while c.state.phase != P.NIGHT: c.advance_phase() var hp := _spawn_hp(es) var top := _top_speed(sim, EnemySystem.TYPE_CROW) r.check(es.night_speed_scale == 1.0 and es.night_hp_scale == 1.0 and hp == Array(es.t_hp) and absf(top - es.t_speed[EnemySystem.TYPE_CROW]) < 0.05 and is_equal_approx(es.max_speed(), fastest), "a night at the default scales: HP %s, a crow flies at %.2f m/s, as before" % [hp, top]) while c.state.phase != P.MORNING: c.advance_phase() # A night at 1.6 (the debug plan's own scales stand for tonight's). var tough := NightPlan.new() tough.title = "Tough" tough.enemy_speed_scale = 1.6 tough.enemy_hp_scale = 1.6 tough.groups = [{"type": "crow", "target": "crops", "time": 5.0, "count": 1, "every": 1.0}] w.debug_plan = tough while c.state.phase != P.NIGHT: c.advance_phase() hp = _spawn_hp(es) top = _top_speed(sim, EnemySystem.TYPE_CROW) var ids := [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER, EnemySystem.TYPE_UFO] r.check(hp[ids[0]] == 1 and hp[ids[1]] == 4 and hp[ids[2]] == 9 and hp[ids[3]] == 3, "at HP x1.6 a crow stays at 1 HP, a thief goes 3 -> %d, a breaker 6 -> %d, a UFO 2 -> %d" % [hp[ids[1]], hp[ids[2]], hp[ids[3]]]) r.check(absf(top - es.t_speed[EnemySystem.TYPE_CROW] * 1.6) < 0.05 and is_equal_approx(es.max_speed(), fastest * 1.6), "at speed x1.6 a crow flies at %.2f m/s (its own %.1f)" % [top, es.t_speed[EnemySystem.TYPE_CROW]]) while c.state.phase != P.MORNING: c.advance_phase() r.check(es.night_speed_scale == 1.0 and es.night_hp_scale == 1.0 and es.full_hp(EnemySystem.TYPE_THIEF) == es.t_hp[EnemySystem.TYPE_THIEF], "the morning puts the scales back to 1") w.debug_plan = null c.new_campaign() _step(sim, 1) ## The HP each enemy type arrives with right now (by type id); they leave again at once. func _spawn_hp(es: EnemySystem) -> Array: var out := [] for t in EnemySystem.TYPE_NAMES.size(): var h := es.spawn(t, 12.0 + 3.0 * t, -2.0, Vector2.UP) out.append(es.health[es.resolve_handle(h)] if h >= 0 else -1) es.remove_all() return out ## How fast an enemy of type goes flat out (full desire, no behavior) after a second. func _top_speed(sim: FarmSim, type: int) -> float: var es := sim.enemies var keep := es.behavior es.behavior = null var d := es.resolve_handle(es.spawn(type, 0.0, 0.0, Vector2.RIGHT)) var speed := -1.0 if d >= 0: for i in 60: es.desire_x[d] = 1.0 es.desire_z[d] = 0.0 es.pace[d] = 1.0 es.step(DT, Vector2(INF, INF), FarmSim.SCARECROW_RADIUS) speed = Vector2(es.vel_x[d], es.vel_z[d]).length() es.remove_all() es.behavior = keep return speed ## #24: F3 (CampaignDebug.jump_morning) moves the morning review to the next day's morning and ## Shift+F3 to the previous one, with the farm as it is: nights_survived follows, the Workbench ## comes, a field sprouts, the retry snapshot and the review panel are the new morning's, and ## nothing is saved. func _night_jump(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void: var c := sim.campaign var debug := farm.get_node("CampaignDebug") as CampaignDebug var st := sim.structures c.new_campaign() _step(sim, 1) var s := c.state s.resources.shed = {"wood": 99, "scrap": 99, "straw": 99} var fence := st.place("fence", 8.0, -8.0, 0) s.scarecrow_hp = s.scarecrow_max_hp - 1 var shed := s.resources.shed.duplicate() var crops := s.crops_total var dormant := sim.loot.count(LootSystem.Status.DORMANT) var keep_path := c.save_path c.save_path = "user://test_night_jump.json" if FileAccess.file_exists(c.save_path): DirAccess.remove_absolute(c.save_path) r.check(not debug.jump_morning(-1) and s.night == 1, "Shift+F3 on the first morning stays there") var f3 := InputEventKey.new() f3.physical_keycode = KEY_F3 f3.pressed = true debug._unhandled_input(f3) var tonight := sim.waves.plan_for(2).title var body := (sim.review_panel._body as Label).text r.check(c.state == s and s.night == 2 and s.nights_survived == 1 and s.phase == P.MORNING, "F3: the morning of day 2 (%d survived)" % s.nights_survived) r.check(s.structure(fence).size() > 0 and s.resources.shed == shed and s.scarecrow_hp == s.scarecrow_max_hp - 1 and s.crops_total == crops and st.has_workbench(), "the farm is as it was (the fence, the shed, the scarecrow's HP), and the Workbench is granted") r.check(c.morning_snapshot() == s.to_dict() and sim.review_panel.visible and body.contains("Tonight: %s." % tonight), "the retry snapshot and the review panel are day 2's") debug._unhandled_input(f3) r.check(s.night == 3 and s.nights_survived == 2 and s.crops_total == crops + 10 and sim.loot.count(LootSystem.Status.DORMANT) == dormant - 10, "F3 again: day 3, and the field due after night 2 sprouts (%d crops)" % s.crops_total) var back := f3.duplicate() as InputEventKey back.shift_pressed = true debug._unhandled_input(back) r.check(s.night == 2 and s.nights_survived == 1 and c.morning_snapshot()["night"] == 2, "Shift+F3: back to day 2") r.check(not FileAccess.file_exists(c.save_path), "a jump doesn't autosave") c.begin_day() r.check(not debug.jump_morning(1) and s.night == 2, "F3 only works in the morning review") c.save_path = keep_path c.new_campaign() _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: var c := sim.campaign var hud := farm.get_node("Hud/PhaseHud") as PhaseHud c.new_campaign() _step(sim, 1) sim.structures.grant_workbench() # #9: granted once, up front, so the per-night structure # count below (broken aside) doesn't trip over the night-1 grant itself c._morning_snapshot = c.state.to_dict() # ...and this morning's snapshot (the loop checks it) agrees sim.waves.enabled = true player.global_position = AWAY var arrivals := {} sim.waves.arrived.connect(func(h: int, _at: Vector2) -> void: var t: int = sim.enemies.type[sim.enemies.resolve_handle(h)] arrivals[t] = int(arrivals.get(t, 0)) + 1) 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 t0 := Time.get_ticks_msec() for night in range(1, c.config.campaign_nights + 1): 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() if night == 1: _defend(sim) 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) hud._on_phase_changed(P.NIGHT, P.DUSK) var title_ok := hud._banner.text == "Night %d: %s" % [night, sim.waves.plan_for(night).title] arrivals.clear() var structures_at_night := c.state.structures.size() broken.clear() 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 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: 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]]) sim.waves.enabled = false ## Automated defenses by the crops, the pen, and the shed. func _defend(sim: FarmSim) -> void: var st := sim.structures sim.campaign.state.resources.shed = {"wood": 99, "scrap": 99, "straw": 99} for at in [Vector2(13.5, -15.0), Vector2(46.0, -15.0), Vector2(13.5, 9.0), Vector2(46.0, 9.0), Vector2(30.0, -17.5), Vector2(30.0, 11.5), Vector2(7.0, -3.5), Vector2(9.5, -14.0), Vector2(-3.5, -12.0)]: if st.validate("automated_defense", at.x, at.y, 0, at) in ["", "The scarecrow is in the way"]: st.place("automated_defense", at.x, at.y, 0) _settle(sim) static func _sum(cost: Dictionary) -> int: var n := 0 for m in cost: n += int(cost[m]) return n func _settle(sim: FarmSim) -> void: var guard := 0 while sim.rebuilding and guard < 600: sim.step(DT) guard += 1 func _step(sim: FarmSim, n: int) -> void: for i in n: sim.step(DT)