extends RefCounted ## The enemy roster (Step 9) on the real farm, with FarmSim stepped by hand at 60 Hz: the type ## table; crows flying over fences and traps but shot down by automated defenses, and never ## fighting; ground thieves going only for animals and supplies; fence breakers smashing the ## barriers that guard valuables, opening gaps other thieves walk through, and turning on the ## scarecrow; the character pool for ground enemies; the alert manager's grouping, priorities, ## escalation, and cap; the debug scenarios; and planned arrivals waiting for a free character. const P := CampaignController.Phase const C := LootSystem.Category const T := ThiefBehavior.State const DT := 1.0 / 60.0 const AWAY := Vector3(-11.0, 0.0, 13.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.enabled = false var player := farm.get_node("Player") as PlayerCharacter player.global_position = AWAY sim.campaign.begin_day() sim.campaign.state.resources.shed = {"wood": 60, "scrap": 60, "straw": 60} # _breakers expects a single gate guarding the pen (not the full ring: with every ring piece # a real structure, _breaker_goals() would treat each as "guarding" and the fence-breaker # narrative -- exactly two guarding barriers, then the scarecrow -- no longer holds). PenRing.build_gate_only(sim) _settle(sim) _table(r, sim) _crows(r, sim, player) _thieves(r, sim, player) _breakers(r, sim, player) _ufos(r, sim, player) _batches(r, sim) _characters(r, sim) _alerts(r, farm, sim, player) _scenarios(r, farm, sim) _waiting(r, sim) farm.queue_free() await r.process_frame func _table(r: GameTestRunner, sim: FarmSim) -> void: var es := sim.enemies var ok := es.t_flying[EnemySystem.TYPE_CROW] == 1 and es.t_flying[EnemySystem.TYPE_THIEF] == 0 and es.t_flying[EnemySystem.TYPE_BREAKER] == 0 ok = ok and es.t_targets[EnemySystem.TYPE_CROW] == 1 << C.CROP ok = ok and es.t_targets[EnemySystem.TYPE_THIEF] == (1 << C.ANIMAL) | (1 << C.SUPPLY) ok = ok and es.t_targets[EnemySystem.TYPE_BREAKER] == 0 and es.t_breaks[EnemySystem.TYPE_BREAKER] == 1 r.check(ok, "the type table: crows fly after crops, thieves walk after animals and supplies, fence breakers after structures") r.check(es.t_hp[EnemySystem.TYPE_CROW] == 1 and es.t_hp[EnemySystem.TYPE_THIEF] == 3 and es.t_hp[EnemySystem.TYPE_BREAKER] == 6 and es.t_speed[EnemySystem.TYPE_CROW] > es.t_speed[EnemySystem.TYPE_THIEF] and es.t_speed[EnemySystem.TYPE_THIEF] > es.t_speed[EnemySystem.TYPE_BREAKER] and es.t_structure_damage[EnemySystem.TYPE_BREAKER] > es.t_structure_damage[EnemySystem.TYPE_THIEF], "crows are fast and fragile, breakers slow, tough, and heavy-handed") ## Crows ignore fences and traps, but not automated defenses, and never fight. func _crows(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var es := sim.enemies var st := sim.structures var loot := sim.loot # A wall of fences across its way, and a trap under it. var fences: Array[int] = [] for k in 6: fences.append(st.place("fence", 47.0 - 0.6, -9.0 + 2.0 * k, 2)) var trap := st.place("slowing_trap", 44.5, -3.0, 0) _settle(sim) var h := es.spawn(EnemySystem.TYPE_CROW, 52.0, -3.0, Vector2.LEFT) var d := es.resolve_handle(h) _step(sim, 1) var unit := es.target[d] r.check(unit > 0 and loot.category[unit] == C.CROP, "a crow goes for a crop") var crossed := false var slowed := false var t := 0.0 while t < 20.0 and es.resolve_handle(h) >= 0 and es.state[es.resolve_handle(h)] == T.SEEK: sim.step(DT) t += DT d = es.resolve_handle(h) if d >= 0: crossed = crossed or (es.pos_x[d] < 46.0 and absf(es.pos_z[d] + 3.0) < 2.0) slowed = slowed or es.slow_left[d] > 0.0 d = es.resolve_handle(h) r.check(crossed and d >= 0 and es.state[d] == T.STEAL, "it flies straight over a fence line to its crop (%.1f s)" % t) r.check(not slowed and int(sim.campaign.state.structure(trap)["hp"]) == sim.config.trap_triggers, "a slowing trap does nothing to it") r.check(fences.all(func(id: int) -> bool: return int(sim.campaign.state.structure(id)["hp"]) == sim.config.fence_hp), "and it breaks nothing") # Standing right next to the scarecrow, it doesn't fight. player.global_position = Vector3(es.pos_x[d] + 0.5, 0.0, es.pos_z[d]) var attacks := es.attacks_attempted _step(sim, 90) r.check(es.attacks_attempted == attacks, "a crow never attacks the scarecrow") player.global_position = AWAY es.apply_hit_handle(h, 99) _step(sim, 1) for id in fences: st.dismantle(id) st.dismantle(trap) # An automated defense in its way shoots it down. var turret := st.place("automated_defense", 42.0, -3.0, 0) _settle(sim) h = es.spawn(EnemySystem.TYPE_CROW, 52.0, -3.0, Vector2.LEFT) var gone := _until(sim, func() -> bool: return es.resolve_handle(h) < 0, 15.0) r.check(gone < 15.0 and es.kills >= 2, "an automated defense shoots a crow down (%.1f s)" % gone) st.dismantle(turret) _settle(sim) ## Ground thieves go only for animals and supplies. func _thieves(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var es := sim.enemies var loot := sim.loot var s := sim.campaign.state var h := sim.spawn_enemy(EnemySystem.TYPE_THIEF, C.CROP, GameTestRunner.EDGE["east"]) _step(sim, 1) var item := es.target[es.resolve_handle(h)] r.check(item > 0 and loot.category[item] != C.CROP, "a thief sent for crops goes for %s instead" % LootSystem.CATEGORY_KEYS[loot.category[item]]) es.apply_hit_handle(h, 99) _step(sim, 1) # A hit while it fills a sack restarts it. h = sim.spawn_enemy(EnemySystem.TYPE_THIEF, C.SUPPLY, GameTestRunner.EDGE["north"]) _until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.STEAL, 40.0) _step(sim, 60) var d := es.resolve_handle(h) var sack := es.target[d] r.check(loot.progress[sack] > 0.2, "it fills a sack (%.0f %%)" % (loot.progress[sack] * 100.0)) es.apply_hit(d) r.check(es.timer[d] == 0.0 and loot.progress[sack] == 0.0 and es.health[d] == es.t_hp[EnemySystem.TYPE_THIEF] - 1, "a hit while stealing restarts the theft") es.apply_hit(d, 99) _step(sim, 1) # Nothing of its own left: it leaves, though the crops are still there. var keep := s.lost_animals.duplicate() var shed: Dictionary = s.resources.shed.duplicate() s.lose_all_animals() loot.sync_from_state() for m in ResourceStore.MATERIALS: s.resources.shed[m] = 0 h = sim.spawn_enemy(EnemySystem.TYPE_THIEF, LootSystem.ANY, GameTestRunner.EDGE["east"]) var events: Array[StringName] = [] var listen := func(kind: StringName, _id: int, _p: Vector3) -> void: events.append(kind) loot.loot_event.connect(listen) _step(sim, 2) loot.loot_event.disconnect(listen) d = es.resolve_handle(h) r.check((d < 0 or es.state[d] == T.RETREAT) and not events.has(&"targeted") and s.crops > 0, "with no animals or supplies left it claims nothing and leaves, crops or not") if d >= 0: es.apply_hit(d, 99) _step(sim, 1) s.lost_animals = keep s.resources.shed = shed loot.sync_from_state() sim.animals.sync_from_state() player.global_position = AWAY ## Fence breakers smash what guards the valuables, open gaps thieves walk through, and fight. func _breakers(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var es := sim.enemies var st := sim.structures var s := sim.campaign.state var gate := sim.nav.structure_at[sim.nav.cell_of(4.0, -6.0)] var far_fence := st.place("fence", 25.0, -30.0, 0) # guards nothing var shed_fence := st.place("fence", 1.0, -12.0, 0) # by the shed _settle(sim) var broken: Array[int] = [] st.destroyed.connect(func(id: int, _t: String, _at: Vector3) -> void: broken.append(id)) var h := sim.spawn_enemy(EnemySystem.TYPE_BREAKER, LootSystem.ANY, GameTestRunner.EDGE["south-west"]) var at := _until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.BREAK, 40.0) var d := es.resolve_handle(h) var first := es.barrier[d] var first_type := _type_of(sim, first) r.check(at < 40.0 and (first == gate or first == shed_fence), "a fence breaker walks to a barrier guarding valuables (%.1f s; the %s)" % [at, first_type]) r.check(es.target[d] < 0 and es.carry[d] < 0, "it claims nothing to steal") var took := _until(sim, func() -> bool: return broken.has(first), 20.0) var hits := ceili(float(st.max_hp(first_type)) / es.t_structure_damage[EnemySystem.TYPE_BREAKER]) r.check(took < (hits - 1) * es.t_hit_interval[EnemySystem.TYPE_BREAKER] + 1.0, "it smashes it in %d heavy hits (%.1f s)" % [hits, took]) var second := _until(sim, func() -> bool: var dd := es.resolve_handle(h) return dd >= 0 and es.state[dd] == T.BREAK and es.barrier[dd] != first, 40.0) d = es.resolve_handle(h) r.check(second < 40.0 and (es.barrier[d] == gate or es.barrier[d] == shed_fence), "then it goes for the next guarding barrier (%.1f s)" % second) _until(sim, func() -> bool: return broken.has(gate) and broken.has(shed_fence), 30.0) r.check(not broken.has(far_fence) and not s.structure(far_fence).is_empty(), "guarding barriers come first: a fence guarding nothing still stands") # Thieves walk through the breach. var thief := sim.spawn_enemy(EnemySystem.TYPE_THIEF, C.ANIMAL, GameTestRunner.EDGE["west"]) var breaks := 0 var t := 0.0 while t < 40.0 and es.resolve_handle(thief) >= 0 and es.state[es.resolve_handle(thief)] != T.CARRY: sim.step(DT) t += DT if es.resolve_handle(thief) >= 0 and es.state[es.resolve_handle(thief)] == T.BREAK: breaks += 1 r.check(t < 40.0 and breaks == 0, "a thief walks in through the gap it opened and takes an animal (%.1f s, no breaking)" % t) es.apply_hit_handle(thief, 99) _step(sim, 1) # With nothing guarding valuables left, it hunts the scarecrow. st.dismantle(far_fence) _settle(sim) player.global_position = Vector3(-6.0, 0.0, 10.0) var hunting := _until(sim, func() -> bool: return es.state[es.resolve_handle(h)] == T.FIGHT, 10.0) var hp := s.scarecrow_hp var reached := _until(sim, func() -> bool: return s.scarecrow_hp < hp, 40.0) r.check(hunting < 10.0 and reached < 40.0 and hp - s.scarecrow_hp == es.t_damage[EnemySystem.TYPE_BREAKER], "with nothing left to break it hunts the scarecrow down and hits it hard (-%d HP, %.1f s)" % [hp - s.scarecrow_hp, reached]) player.global_position = AWAY es.apply_hit_handle(h, 99) _step(sim, 1) # Shot from range, a breaker turns on the scarecrow; it takes 6 hits. st.place("gate", 4.0, -6.0, 2) _settle(sim) h = sim.spawn_enemy(EnemySystem.TYPE_BREAKER, LootSystem.ANY, GameTestRunner.EDGE["south-west"]) _step(sim, 30) d = es.resolve_handle(h) es.apply_hit(d) r.check(es.state[d] == T.FIGHT, "shot, it turns on the scarecrow") for k in es.t_hp[EnemySystem.TYPE_BREAKER] - 2: es.apply_hit(d) _step(sim, 1) r.check(es.resolve_handle(h) >= 0, "five hits don't stop it") es.apply_hit_handle(h) _step(sim, 1) r.check(es.resolve_handle(h) < 0, "the sixth does") s.scarecrow_hp = s.scarecrow_max_hp _settle(sim) ## UFOs ignore every barrier like a crow, but fight: they go straight for the scarecrow and damage ## it per their FIGHT aggression, and their stats come from GameConfig. func _ufos(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void: var es := sim.enemies var st := sim.structures var s := sim.campaign.state r.check(es.t_flying[EnemySystem.TYPE_UFO] == 1 and es.t_aggression[EnemySystem.TYPE_UFO] == EnemySystem.Aggression.FIGHT, "a UFO is a flying, fight-aggression type") # A wall of fences across its way, like the crow test: it flies straight through, breaking nothing. var fences: Array[int] = [] for k in 6: fences.append(st.place("fence", 47.0 - 0.6, -9.0 + 2.0 * k, 2)) _settle(sim) var h := es.spawn(EnemySystem.TYPE_UFO, 52.0, -3.0, Vector2.LEFT) var d := es.resolve_handle(h) _step(sim, 1) r.check(d >= 0 and es.state[d] == T.FIGHT, "it goes straight for the scarecrow, never seeking loot") var crossed := false var t := 0.0 while t < 10.0 and not crossed and es.resolve_handle(h) >= 0: sim.step(DT) t += DT d = es.resolve_handle(h) if d >= 0: crossed = crossed or (es.pos_x[d] < 46.0 and absf(es.pos_z[d] + 3.0) < 2.0) r.check(crossed, "it flies straight through a fence line, ignoring it (%.1f s)" % t) r.check(fences.all(func(id: int) -> bool: return int(s.structure(id)["hp"]) == st.max_hp("fence")), "and breaks nothing") es.apply_hit_handle(h, 99) _step(sim, 1) for id in fences: st.dismantle(id) _settle(sim) # It engages the scarecrow and damages it, per its FIGHT aggression. player.global_position = Vector3(10.0, 0.0, -3.0) h = es.spawn(EnemySystem.TYPE_UFO, 10.0, -20.0, Vector2.UP) var hp := s.scarecrow_hp var reached := _until(sim, func() -> bool: return s.scarecrow_hp < hp, 20.0) r.check(reached < 20.0 and hp - s.scarecrow_hp == es.t_damage[EnemySystem.TYPE_UFO], "it dives in and hits the scarecrow for its configured damage (-%d HP, %.1f s)" % [hp - s.scarecrow_hp, reached]) s.scarecrow_hp = s.scarecrow_max_hp es.apply_hit_handle(h, 99) _step(sim, 1) player.global_position = AWAY # Its stats come straight from GameConfig: a duplicate with a different speed changes them. var cfg := (load("res://game/resources/game_config.tres") as GameConfig).duplicate() as GameConfig cfg.enemy_types = (cfg.enemy_types as Dictionary).duplicate(true) (cfg.enemy_types["ufo"] as Dictionary)["speed"] = 1.0 var probe := EnemySystem.new() probe._load_types(cfg) r.check(probe.t_speed[EnemySystem.TYPE_UFO] == 1.0 and es.t_speed[EnemySystem.TYPE_UFO] != 1.0, "UFO stats are config-tunable: overriding speed to 1.0 on a duplicate config changes it (the live one is %.1f)" % es.t_speed[EnemySystem.TYPE_UFO]) probe.free() ## Flyers are drawn by two MultiMesh batches: crows by the bird one, UFOs by their own saucer ## (built from primitives). Each batch draws only its type, also after a swap-removal moves a UFO ## into a crow's slot; ground enemies are in neither. func _batches(r: GameTestRunner, sim: FarmSim) -> void: var es := sim.enemies var birds := es.renderer var saucers := es.ufo_renderer _step(sim, 1) var base_birds := _drawn_count(birds, es.active_count) var base_saucers := _drawn_count(saucers, es.active_count) var crow := es.spawn(EnemySystem.TYPE_CROW, 20.0, -30.0, Vector2.UP) var thief := es.spawn(EnemySystem.TYPE_THIEF, 22.0, -30.0, Vector2.UP) var ufo := es.spawn(EnemySystem.TYPE_UFO, 24.0, -30.0, Vector2.UP) _step(sim, 1) var d_crow := es.resolve_handle(crow) var d_thief := es.resolve_handle(thief) var d_ufo := es.resolve_handle(ufo) r.check(d_ufo >= 0 and saucers.is_drawn(d_ufo) and not birds.is_drawn(d_ufo), "a UFO is drawn by the UFO batch, not the bird batch") r.check(d_crow >= 0 and birds.is_drawn(d_crow) and not saucers.is_drawn(d_crow), "a crow is still drawn by the bird batch, and only there") r.check(d_thief >= 0 and not birds.is_drawn(d_thief) and not saucers.is_drawn(d_thief), "a thief is in neither batch (its character draws it)") es.apply_hit_handle(crow, 99) _step(sim, 1) d_ufo = es.resolve_handle(ufo) r.check(d_ufo == d_crow and saucers.is_drawn(d_ufo) and not birds.is_drawn(d_ufo) and _drawn_count(birds, es.active_count) == base_birds and _drawn_count(saucers, es.active_count) == base_saucers + 1, "the UFO swapped into the dead crow's slot is still drawn as a saucer only") var mesh := EnemySystem.build_ufo_mesh() var box := mesh.get_aabb() var center := box.get_center() r.check(box.size.x > 1.4 and box.size.x < 1.8 and absf(box.size.x - box.size.z) < 0.03 and absf(center.x) < 0.01 and absf(center.z) < 0.01 and box.position.y > -1.5 and box.end.y < 1.0 and (mesh.surface_get_format(0) & Mesh.ARRAY_FORMAT_COLOR) != 0, "the saucer is round, %.2f m across, centered, from %.2f to %.2f m about its origin, and colored per part" % [box.size.x, box.position.y, box.end.y]) for hh in [thief, ufo]: es.apply_hit_handle(hh, 99) _step(sim, 1) sim.characters._process(EnemyCharacters.CORPSE_TIME + 0.1) func _drawn_count(rend: HordeRenderer, n: int) -> int: var drawn := 0 for slot in n: if rend.is_drawn(slot): drawn += 1 return drawn ## Ground enemies are drawn by pooled characters; flyers aren't. func _characters(r: GameTestRunner, sim: FarmSim) -> void: var es := sim.enemies var ch := sim.characters var thieves := ch.free_count(EnemySystem.TYPE_THIEF) r.check(thieves == int(sim.config.character_pool["thief"]) and thieves == ch.pool_size(EnemySystem.TYPE_THIEF) and ch.in_use() == 0, "%d thief characters (GameConfig.character_pool) waiting in the pool (%d of them corpses still fading)" % [thieves, ch.corpses()]) var crow := es.spawn(EnemySystem.TYPE_CROW, 20.0, -30.0, Vector2.UP) var handles: Array[int] = [] for k in thieves + 2: handles.append(es.spawn(EnemySystem.TYPE_THIEF, 12.0 + k * 0.7, -30.0, Vector2.UP)) # all inside the farm's edge var spawned := handles.filter(func(hh: int) -> bool: return hh >= 0).size() r.check(crow >= 0 and spawned == thieves and ch.in_use() == thieves and ch.free_count(EnemySystem.TYPE_THIEF) == 0, "one character per ground enemy, none for a crow; past the pool, spawns are skipped (%d of %d)" % [spawned, handles.size()]) var corpses := ch.corpses() es.apply_hit_handle(handles[0], 99) _step(sim, 1) r.check(ch.in_use() == thieves - 1 and ch.corpses() == corpses + 1 and ch.free_count(EnemySystem.TYPE_THIEF) == 1, "a killed thief's character lies there a moment, free to be taken over") var again := es.spawn(EnemySystem.TYPE_THIEF, 25.0, -30.0, Vector2.UP) r.check(again >= 0 and ch.in_use() == thieves and ch.corpses() == corpses, "a new thief takes it over") handles.append(again) for hh in handles + [crow]: if hh >= 0: es.apply_hit_handle(hh, 99) _step(sim, 1) ch._process(EnemyCharacters.CORPSE_TIME + 0.1) ## The alert manager groups alerts by kind, type, and zone, orders them, escalates escapes, and ## caps how many show. func _alerts(r: GameTestRunner, farm: Node3D, sim: FarmSim, player: PlayerCharacter) -> void: var am := farm.get_node("Hud/Alerts") as AlertManager var es := sim.enemies var loot := sim.loot var keep := es.behavior es.behavior = null # everyone holds still where they're put var crows: Array[int] = [] var units: Array[int] = [] for k in 3: var u := _home_crop(loot, units) units.append(u) var h := es.spawn(EnemySystem.TYPE_CROW, loot.home[u].x, loot.home[u].y, Vector2.UP) loot.holder[u] = h crows.append(h) var list := am.collect() var theft := list.filter(func(a: Dictionary) -> bool: return a["kind"] == AlertManager.Kind.THEFT) r.check(theft.size() == 1 and int(theft[0]["count"]) == 3 and theft[0]["label"] == "3 crows — Crop Field", "three crows at the crops make one alert: '%s'" % (theft[0]["label"] if not theft.is_empty() else "-")) # A carrier near the fence outranks one in the middle of the field, and both outrank thefts. var near := _carrier(sim, Vector2(45.5, 5.0), _home_crop(loot, units)) var mid := _carrier(sim, Vector2(30.0, -3.0), _home_crop(loot, units)) list = am.collect() var escapes := list.filter(func(a: Dictionary) -> bool: return a["kind"] == AlertManager.Kind.ESCAPE) r.check(escapes.size() == 1 and int(escapes[0]["count"]) == 2, "two crows escaping from the field are grouped too") r.check(list[0]["kind"] == AlertManager.Kind.ESCAPE and float(list[0]["escalation"]) > 0.7 and String(list[0]["label"]).contains("!"), "escapes come first, escalated by the one nearly out: '%s'" % list[0]["label"]) var raw: Array[Dictionary] = [am._alert(AlertManager.Kind.ESCAPE, EnemySystem.TYPE_CROW, C.CROP, Vector2(45.5, 5.0), am._escalation(es.resolve_handle(near), Vector2(45.5, 5.0))), am._alert(AlertManager.Kind.ESCAPE, EnemySystem.TYPE_CROW, C.CROP, Vector2(30.0, -3.0), am._escalation(es.resolve_handle(mid), Vector2(30.0, -3.0)))] r.check(float(raw[0]["escalation"]) > float(raw[1]["escalation"]), "escape alerts escalate as the carrier nears its exit (%.2f > %.2f)" % [raw[0]["escalation"], raw[1]["escalation"]]) # A breach outranks a theft; at most MAX_SHOWN show. var many: Array[Dictionary] = [] for k in 8: many.append(am._alert(AlertManager.Kind.LOOSE if k % 2 == 0 else AlertManager.Kind.THEFT, k % 3, k % 3, Vector2(-10.0 + k * 7.0, -40.0 + k * 6.0), 0.0)) many.append(am._alert(AlertManager.Kind.BREACH, -1, -1, Vector2(3.5, -6.0), 1.0)) var sorted := AlertManager.sort_alerts(AlertManager.group_alerts(many)) var shown := AlertManager.cap(sorted, AlertManager.MAX_SHOWN) r.check(sorted[0]["kind"] == AlertManager.Kind.BREACH and sorted[0]["label"] == "Breach! — Animal Pen", "a breach comes before thefts: '%s'" % sorted[0]["label"]) r.check(sorted.size() > AlertManager.MAX_SHOWN and shown.size() == AlertManager.MAX_SHOWN and shown[0] == sorted[0], "of %d alerts, the top %d show" % [sorted.size(), AlertManager.MAX_SHOWN]) es.behavior = keep for hh in crows + [near, mid]: es.apply_hit_handle(hh, 99) _step(sim, 1) loot.return_dropped() player.global_position = AWAY ## Debug scenarios (F2): one enemy type at a time, then pairs, then all three. func _scenarios(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void: var dbg := farm.get_node("CampaignDebug") as CampaignDebug var c := sim.campaign var titles: Array[String] = [] for k in CampaignDebug.SCENARIOS.size(): dbg.cycle_scenario() titles.append(sim.waves.debug_plan.title if sim.waves.debug_plan != null else "") r.check(not titles.has("") and titles.size() == 6, "F2 cycles through %d scenarios: %s" % [titles.size(), ", ".join(titles)]) r.check(sim.waves.tonight() == sim.waves.debug_plan and sim.waves.debug_plan.title == "All three", "the scenario is tonight's plan") var types := {} sim.waves.arrived.connect(func(h: int, _at: Vector2) -> void: types[sim.enemies.type[sim.enemies.resolve_handle(h)]] = true) var skipped_before := sim.enemies.spawn_skipped sim.waves.enabled = true while c.state.phase != P.NIGHT: if c.state.phase == P.MORNING: c.begin_day() else: c.advance_phase() _step(sim, 60 * 45) r.check(types.size() == 3, "'All three' sends in crows, thieves, and a fence breaker (%s; %d spawns skipped)" % [types.keys(), sim.enemies.spawn_skipped - skipped_before]) dbg.cycle_scenario() r.check(sim.waves.debug_plan == null and sim.waves.tonight() == sim.waves.plan_for(c.state.night), "and back to the night's own plan") sim.waves.enabled = false ## A planned arrival whose type has no free character waits for one instead of vanishing, and ## comes in as soon as one is free; arrivals of other types behind it still come on time; any still ## waiting at sunrise are dropped and counted (WaveDirector.waited / dropped). func _waiting(r: GameTestRunner, sim: FarmSim) -> void: var c := sim.campaign var es := sim.enemies var ch := sim.characters var w := sim.waves # A fresh night (back to this night's morning, which clears the farm: 'All three' can end the # vacation), with a plan of our own: a thief at 1 s, crows at 1.5, 2, and 2.5 s, and two more # thieves at 4 and 4.5 s. c.retry_night() c.begin_day() c.advance_phase() var plan := NightPlan.new() plan.title = "Waiting" plan.count_scale = 1.0 plan.spacing_scale = 1.0 plan.groups = [ {"time": 1.0, "count": 1, "every": 1.0, "type": "thief", "target": "any"}, {"time": 1.5, "count": 3, "every": 0.5, "type": "crow", "target": "crops"}, {"time": 4.0, "count": 2, "every": 0.5, "type": "thief", "target": "any"}, ] w.debug_plan = plan w.enabled = true c.advance_phase() var night := c.state.phase == P.NIGHT and w.tonight() == plan and es.alive_count() == 0 r.check(night, "a fresh night with a plan of a thief, three crows, and two thieves") if not night: w.debug_plan = null w.enabled = false return var keep := es.behavior es.behavior = null # everyone holds still where they arrive: nobody leaves unless killed var arrivals: Array = [] # [type, night_time] var listen := func(h: int, _at: Vector2) -> void: arrivals.append([es.type[es.resolve_handle(h)], w.night_time]) w.arrived.connect(listen) # Every thief character in use. var pool := ch.pool_size(EnemySystem.TYPE_THIEF) var thieves: Array[int] = [] while es.can_spawn(EnemySystem.TYPE_THIEF) and thieves.size() < pool + 5: var h := es.spawn(EnemySystem.TYPE_THIEF, 12.0 + thieves.size() * 0.7, -30.0, Vector2.UP) if h < 0: break thieves.append(h) var skipped := es.spawn_skipped r.check(thieves.size() == pool and pool == int(sim.config.character_pool["thief"]) and not es.can_spawn(EnemySystem.TYPE_THIEF) and es.can_spawn(EnemySystem.TYPE_CROW) and es.can_spawn(EnemySystem.TYPE_BREAKER) and es.spawn(EnemySystem.TYPE_THIEF, 40.0, -28.0, Vector2.UP) == -1 and es.spawn_skipped == skipped + 1, "with all %d thief characters in use, can_spawn says no to a thief (and spawn agrees), yes to a crow or a breaker" % thieves.size()) skipped = es.spawn_skipped _until(sim, func() -> bool: return w.night_time >= 3.0, 10.0) var thief_times := arrivals.filter(func(a: Array) -> bool: return a[0] == EnemySystem.TYPE_THIEF) var crow_times := arrivals.filter(func(a: Array) -> bool: return a[0] == EnemySystem.TYPE_CROW).map(func(a: Array) -> float: return a[1]) r.check(thief_times.is_empty() and w.waiting() == 1 and w.waited == 1 and es.spawn_skipped == skipped and ch.in_use() == pool, "the thief due at 1 s waits for a character instead of vanishing (%d waiting, %d skipped)" % [w.waiting(), es.spawn_skipped - skipped]) var on_time := crow_times.size() == 3 for k in crow_times.size(): on_time = on_time and absf(float(crow_times[k]) - (1.5 + 0.5 * k)) <= 1.5 * DT r.check(on_time, "the crows behind it still arrive on time (at %s s)" % [crow_times.map(func(t: float) -> String: return "%.2f" % t)]) # One thief is killed: its body is free to be taken over, and the waiting thief comes in. es.apply_hit_handle(thieves[0], 99) es.commit_removals() r.check(ch.corpses() >= 1 and ch.free_count(EnemySystem.TYPE_THIEF) == 1 and es.can_spawn(EnemySystem.TYPE_THIEF), "a killed thief's character, still lying there, can be taken over: can_spawn says yes") var killed_at := w.night_time _step(sim, 1) thief_times = arrivals.filter(func(a: Array) -> bool: return a[0] == EnemySystem.TYPE_THIEF) r.check(thief_times.size() == 1 and float(thief_times[0][1]) - killed_at <= 1.5 * DT and w.waiting() == 0 and ch.in_use() == pool and es.spawn_skipped == skipped, "it arrives at once when one leaves (%.2f s, the kill at %.2f s)" % [float(thief_times[0][1]) if not thief_times.is_empty() else -1.0, killed_at]) # The two thieves due at 4 and 4.5 s find the pool full again and wait until sunrise. _until(sim, func() -> bool: return w.night_time >= 5.0, 10.0) r.check(w.waiting() == 2 and w.waited == 3 and w.dropped == 0 and w.spawned == 4, "two more thieves wait (%d waiting; %d waited tonight)" % [w.waiting(), w.waited]) var before := arrivals.size() c.advance_phase() _step(sim, 60) r.check(c.state.phase == P.DAWN and w.dropped == 2 and w.waiting() == 0 and w.waited == 3 and w.spawned == 4 and arrivals.size() == before, "at sunrise the ones still waiting are dropped and counted (%d dropped, %d waited, %d arrived)" % [w.dropped, w.waited, w.spawned]) w.arrived.disconnect(listen) es.behavior = keep es.remove_all() w.debug_plan = null w.enabled = false func _carrier(sim: FarmSim, p: Vector2, unit: int) -> int: var es := sim.enemies var h := es.spawn(EnemySystem.TYPE_CROW, p.x, p.y, Vector2.RIGHT) sim.loot.holder[unit] = h sim.loot.pick_up(unit, h) es.carry[es.resolve_handle(h)] = unit es.state[es.resolve_handle(h)] = T.CARRY return h func _home_crop(loot: LootSystem, not_these: Array[int]) -> int: for id in range(1, loot.unit_count + 1): if loot.status[id] == LootSystem.Status.HOME and loot.holder[id] == -1 and not not_these.has(id): return id return -1 func _type_of(sim: FarmSim, id: int) -> String: var e := sim.campaign.state.structure(id) return String(e.get("type", "gate")) 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) func _until(sim: FarmSim, cond: Callable, max_seconds: float) -> float: var t := 0.0 while t < max_seconds and not cond.call(): sim.step(DT) t += DT return t