From 34358d0641daf7cc5e35ccdf271278bb27f9b65a Mon Sep 17 00:00:00 2001 From: Forrest Date: Mon, 5 Oct 2026 14:43:46 -0400 Subject: [PATCH 1/2] Task #15: UFO enemy, a fast flying fighter Adds a 4th enemy type (TYPE_UFO=3) that reuses the generic flying-nav infrastructure (ignores fences, gates, and traps) and gets a new FIGHT-capable behavior in ThiefBehavior: it skips SEEK/loot entirely and goes straight to FIGHT, strafing the scarecrow in dive-and-pull-up passes (diving to land_altitude while its attack is off cooldown, climbing to cruise_altitude right after landing a hit) until it's hit for every pass. All stats (speed 7.0, hp 2, damage 1, hit_interval 1.5s, aggro_range 10.0, etc.) live in GameConfig.enemy_types["ufo"]. It renders through the same shared flyer MultiMesh crows use (no new art) and is wired into NightPlan/WaveDirector composition, the HUD/assessment roster lines, kill log keys, and the arrival sound/toast, with a generic audio fallback since it has no cue of its own. Per a scope addition mid-task, night 5 now also sends in a wave of 4 UFOs late in the night (game/resources/nights/night_5.tres), which shifts test_nights' known-flaky "arrived" baseline line by one new {3: N} entry (the UFO counts agree between plan and arrival) and required updating its hardcoded "all three types" check to four. A debug-only "UFOs only" scenario is added (game/resources/scenarios/ufos.tres) but deliberately left out of CampaignDebug.SCENARIOS, since wiring it in would change test_roster's hardcoded scenario count and that file's existing checks were required to stay untouched. Co-Authored-By: Claude Sonnet 5 --- game/resources/nights/night_5.tres | 6 ++++ game/resources/scenarios/ufos.tres | 20 +++++++++++ game/scripts/assessment.gd | 2 +- game/scripts/enemy_system.gd | 9 ++--- game/scripts/farm_audio.gd | 4 ++- game/scripts/farm_sim.gd | 2 ++ game/scripts/farm_state.gd | 4 +-- game/scripts/game_config.gd | 5 +++ game/scripts/night_plan.gd | 4 +-- game/scripts/phase_hud.gd | 2 +- game/scripts/thief_behavior.gd | 43 +++++++++++++++++++++-- game/scripts/wave_director.gd | 4 ++- game/tests/test_nights.gd | 4 +-- game/tests/test_roster.gd | 55 ++++++++++++++++++++++++++++++ 14 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 game/resources/scenarios/ufos.tres diff --git a/game/resources/nights/night_5.tres b/game/resources/nights/night_5.tres index da8ff9e..dcdea3e 100644 --- a/game/resources/nights/night_5.tres +++ b/game/resources/nights/night_5.tres @@ -73,4 +73,10 @@ groups = Array[Dictionary]([{ "target": "any", "time": 125.0, "type": "breaker" +}, { +"count": 4, +"every": 3.0, +"target": "any", +"time": 140.0, +"type": "ufo" }]) diff --git a/game/resources/scenarios/ufos.tres b/game/resources/scenarios/ufos.tres new file mode 100644 index 0000000..89bbc6e --- /dev/null +++ b/game/resources/scenarios/ufos.tres @@ -0,0 +1,20 @@ +[gd_resource type="Resource" script_class="NightPlan" format=3] + +[ext_resource type="Script" path="res://game/scripts/night_plan.gd" id="1_plan"] + +[resource] +script = ExtResource("1_plan") +title = "UFOs only" +groups = Array[Dictionary]([{ +"count": 3, +"every": 2.0, +"target": "any", +"time": 3.0, +"type": "ufo" +}, { +"count": 3, +"every": 1.5, +"target": "any", +"time": 30.0, +"type": "ufo" +}]) diff --git a/game/scripts/assessment.gd b/game/scripts/assessment.gd index 96e5dc1..d9dccf6 100644 --- a/game/scripts/assessment.gd +++ b/game/scripts/assessment.gd @@ -40,7 +40,7 @@ static func of(s: FarmState, cfg: GameConfig) -> Dictionary: ## "50 crows, 20 thieves and 6 fence breakers" (or "none"). static func enemies_line(t: Dictionary) -> String: var parts := PackedStringArray() - for type in [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER]: + for type in [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER, EnemySystem.TYPE_UFO]: var n := int(t.get("killed_" + EnemySystem.TYPE_NAMES[type], 0)) if n == 1: parts.append("1 " + EnemySystem.NAMES[type]) diff --git a/game/scripts/enemy_system.gd b/game/scripts/enemy_system.gd index b6b9e80..168fa3c 100644 --- a/game/scripts/enemy_system.gd +++ b/game/scripts/enemy_system.gd @@ -24,13 +24,14 @@ const HURT_FADE := 4.0 const TYPE_THIEF := 0 const TYPE_CROW := 1 const TYPE_BREAKER := 2 +const TYPE_UFO := 3 ## GameConfig.enemy_types keys, by type id. -const TYPE_NAMES: Array[String] = ["thief", "crow", "breaker"] +const TYPE_NAMES: Array[String] = ["thief", "crow", "breaker", "ufo"] ## Display names, singular and plural. -const NAMES: Array[String] = ["alien", "crow", "alien robot"] -const PLURALS: Array[String] = ["aliens", "crows", "alien robots"] +const NAMES: Array[String] = ["alien", "crow", "alien robot", "UFO"] +const PLURALS: Array[String] = ["aliens", "crows", "alien robots", "UFOs"] ## "a" or "an" for NAMES[t], by type id. -const ARTICLES: Array[String] = ["an", "a", "an"] +const ARTICLES: Array[String] = ["an", "a", "an", "a"] const NB_DX: Array[int] = [0, -1, 0, 1, -1, 1, -1, 0, 1] const NB_DZ: Array[int] = [0, -1, -1, -1, 0, 0, 1, 1, 1] diff --git a/game/scripts/farm_audio.gd b/game/scripts/farm_audio.gd index ee02616..df85d36 100644 --- a/game/scripts/farm_audio.gd +++ b/game/scripts/farm_audio.gd @@ -218,7 +218,9 @@ func setup(p_sim: FarmSim) -> void: sim.waves.arrived.connect(func(h: int, at: Vector2) -> void: var d := sim.enemies.resolve_handle(h) var type := sim.enemies.type[d] if d >= 0 else EnemySystem.TYPE_THIEF - play(["thief_arrive", "crow_arrive", "breaker_arrive"][type], Vector3(at.x, 0.0, at.y))) + # thief, crow, breaker, and a UFO (no cue of its own yet: it flies in like a crow). + var arrive_cues := ["thief_arrive", "crow_arrive", "breaker_arrive", "crow_arrive"] + play(arrive_cues[type], Vector3(at.x, 0.0, at.y))) var kit := sim.scarecrow kit.damaged.connect(func(hp: int) -> void: play("fall" if hp <= 0 else "hurt")) kit.fear_used.connect(func(_p: Vector3, _n: int) -> void: play("fear")) diff --git a/game/scripts/farm_sim.gd b/game/scripts/farm_sim.gd index bfe216f..311b3a6 100644 --- a/game/scripts/farm_sim.gd +++ b/game/scripts/farm_sim.gd @@ -698,6 +698,8 @@ static func arrival_message(type: int, side: String) -> String: return "A crow flew in" + from EnemySystem.TYPE_BREAKER: return "An alien robot came in" + from + EnemySystem.TYPE_UFO: + return "A UFO swept in" + from return "An alien slipped in" + from diff --git a/game/scripts/farm_state.gd b/game/scripts/farm_state.gd index 79f0668..0b3022b 100644 --- a/game/scripts/farm_state.gd +++ b/game/scripts/farm_state.gd @@ -24,7 +24,7 @@ const STRUCTURE_KEYS: Array[String] = ["id", "type", "x", "z", "rot", "hp"] const EVENTS: Array[String] = [ "crops_stolen", "crops_recovered", "animals_stolen", "animals_strayed", "animals_recovered", "supplies_stolen", "supplies_recovered", "supplies_lost", - "killed_crow", "killed_thief", "killed_breaker", "escaped", + "killed_crow", "killed_thief", "killed_breaker", "killed_ufo", "escaped", "scarecrow_damage", "patches", "structures_built", "structures_damaged", "structures_destroyed", "repairs", "upgrades_bought", "gathered_wood", "gathered_scrap", "gathered_straw", "gathered_alien_goop", @@ -35,7 +35,7 @@ const LOG_KEYS: Array[String] = [ "night", "crops_lost", "animals_lost", "crops_stolen", "crops_recovered", "animals_stolen", "animals_strayed", "animals_recovered", "supplies_stolen", "supplies_recovered", "supplies_lost", - "killed_crow", "killed_thief", "killed_breaker", "escaped", + "killed_crow", "killed_thief", "killed_breaker", "killed_ufo", "escaped", "scarecrow_damage", "patches", "structures_built", "structures_damaged", "structures_destroyed", "repairs", "upgrades_bought", "gathered_wood", "gathered_scrap", "gathered_straw", "gathered_alien_goop", diff --git a/game/scripts/game_config.gd b/game/scripts/game_config.gd index b6b2896..e102c0c 100644 --- a/game/scripts/game_config.gd +++ b/game/scripts/game_config.gd @@ -243,6 +243,11 @@ extends Resource "breaker": {"speed": 1.9, "acceleration": 8.0, "hp": 6, "flying": false, "targets": ["structures"], "steal_time": 0.0, "carry_speed": 0.0, "structure_damage": 8, "damage": 2, "hit_interval": 1.5, "aggression": "fight", "hit_radius": 0.55, "aggro_range": 4.0}, + ## Fast, fragile, and always spoiling for a fight: a UFO heads straight for the scarecrow (no + ## loot, so no targets) and dives in for a strafing pass every hit_interval (ThiefBehavior). + "ufo": {"speed": 7.0, "acceleration": 20.0, "hp": 2, "flying": true, "targets": [], + "steal_time": 0.0, "carry_speed": 0.0, "structure_damage": 0, "damage": 1, "hit_interval": 1.5, + "aggression": "fight", "hit_radius": 0.5, "aggro_range": 10.0}, } ## Flyers cruise at this visual altitude and come down to land_altitude to take a crop. @export var cruise_altitude: float = 3.2 diff --git a/game/scripts/night_plan.gd b/game/scripts/night_plan.gd index 2522504..7428991 100644 --- a/game/scripts/night_plan.gd +++ b/game/scripts/night_plan.gd @@ -8,7 +8,7 @@ extends Resource ## time seconds after nightfall when the group's first enemy arrives ## count how many arrive ## every seconds between them -## type enemy type: "thief", "crow", or "breaker" +## type enemy type: "thief", "crow", "breaker", or "ufo" ## target what they're after: "crops", "animals", "supplies", or "any" (GameConfig.target_weights) ## The plans are the nights' designs; GameConfig.wave_count_scale and wave_spacing_scale scale ## every group when the night is played (count_scale x the enemies, spacing_scale x the time @@ -66,7 +66,7 @@ func problems(length: float, count_scale: float = 1.0, spacing_scale: float = 1. out.append("%s: unknown enemy type '%s'" % [where, type]) continue var target := String(g.get("target", "any")) - var allowed := {"crow": ["crops", "any"], "thief": ["animals", "supplies", "any"], "breaker": ["any"]} + var allowed := {"crow": ["crops", "any"], "thief": ["animals", "supplies", "any"], "breaker": ["any"], "ufo": ["any"]} if not (allowed[type] as Array).has(target): out.append("%s: a %s doesn't go for '%s'" % [where, type, target]) if int(g.get("count", 0)) < 1 or float(g.get("every", 0.0)) <= 0.0: diff --git a/game/scripts/phase_hud.gd b/game/scripts/phase_hud.gd index e8ff043..72ab85d 100644 --- a/game/scripts/phase_hud.gd +++ b/game/scripts/phase_hud.gd @@ -167,7 +167,7 @@ static func on_the_farm(es: EnemySystem) -> String: if es.alive[d] == 1: counts[es.type[d]] += 1 var parts := PackedStringArray() - for t in [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER]: + for t in [EnemySystem.TYPE_CROW, EnemySystem.TYPE_THIEF, EnemySystem.TYPE_BREAKER, EnemySystem.TYPE_UFO]: if counts[t] == 1: parts.append(EnemySystem.ARTICLES[t] + " " + EnemySystem.NAMES[t]) elif counts[t] > 1: diff --git a/game/scripts/thief_behavior.gd b/game/scripts/thief_behavior.gd index d567ec7..a812e78 100644 --- a/game/scripts/thief_behavior.gd +++ b/game/scripts/thief_behavior.gd @@ -1,6 +1,6 @@ class_name ThiefBehavior extends RefCounted -## What each enemy wants, decided every tick (EnemySystem turns it into motion). Three types +## What each enemy wants, decided every tick (EnemySystem turns it into motion). Four types ## (stats in GameConfig.enemy_types): ## thief on foot, after its type's targets (animals and supplies): the theft lifecycle of ## game/RULES.md below. Routes around barriers when it can, breaks through when not. @@ -11,6 +11,10 @@ extends RefCounted ## barrier guarding the crops, the pen, or the shed and smashes it, then goes for the ## next. It turns on the scarecrow when it comes close or shoots it, and hunts the ## scarecrow when nothing is left to break. +## ufo flies, steals nothing, and goes straight to FIGHT the moment it arrives (fences, +## gates, and traps mean nothing to it, same as a crow). Strafes the scarecrow: dives +## toward it while its attack is off cooldown, then peels away and swings around for +## another pass once it lands a hit (altitude dips for the dive, climbs on the pull-up). ## States: ## SEEK claim something to take (LootSystem.claim, limited to the type's categories: dropped ## loot in view first, then its wanted category, EnemySystem.want, then the others), @@ -97,6 +101,8 @@ func update(es: EnemySystem, dt: float) -> void: State.SEEK: if t == EnemySystem.TYPE_CROW: _crow_seek(es, i) + elif t == EnemySystem.TYPE_UFO: + _start_fight(es, i) elif es.t_breaks[t] == 1: _breaker_seek(es, i) else: @@ -113,7 +119,10 @@ func update(es: EnemySystem, dt: float) -> void: State.BREAK: _break(es, i) State.FIGHT: - _fight(es, i) + if t == EnemySystem.TYPE_UFO: + _ufo_fight(es, i) + else: + _fight(es, i) _: if es.t_flying[t] == 1: _fly_off(es, i, EnemySystem.Removal.LEFT) @@ -184,7 +193,7 @@ func retreat_all(es: EnemySystem) -> void: ## A flyer's visual altitude: cruising, coming down as it nears its crop, sitting on it while it -## takes it. +## takes it. A UFO dives low for its attack run and climbs again once it has landed a hit. func altitude_for(es: EnemySystem, i: int) -> float: match es.state[i]: State.STEAL: @@ -194,9 +203,17 @@ func altitude_for(es: EnemySystem, i: int) -> float: if unit > 0: var d := loot.position_of(unit).distance_to(Vector2(es.pos_x[i], es.pos_z[i])) return lerpf(cfg.land_altitude, cfg.cruise_altitude, clampf((d - 0.5) / 4.0, 0.0, 1.0)) + State.FIGHT: + if es.type[i] == EnemySystem.TYPE_UFO: + return cfg.cruise_altitude if es.attack_cd[i] > 0.0 else cfg.land_altitude return cfg.cruise_altitude +## Body contact range for an attack: the enemy's hit proxy, the scarecrow's, and the melee reach. +func _attack_reach() -> float: + return cfg.enemy_radius + FarmSim.SCARECROW_RADIUS + cfg.enemy_attack_reach + + func _seek(es: EnemySystem, i: int, dt: float) -> void: var h := es.handle_of(i) var p := Vector2(es.pos_x[i], es.pos_z[i]) @@ -390,6 +407,26 @@ func _fight(es: EnemySystem, i: int) -> void: es.desire_z[i] = to.y / d +## A UFO's strafing run: straight at the scarecrow while its attack is off cooldown (diving in), +## straight away from it right after landing a hit (pulling up and swinging around for the next +## pass). No scarecrow left standing: leave like anything else at the end of a fight. +func _ufo_fight(es: EnemySystem, i: int) -> void: + var p := Vector2(es.pos_x[i], es.pos_z[i]) + var sc := nearest_scarecrow(p) + if not sc.is_finite(): + es.state[i] = State.RETREAT + return + var to := sc - p + var d := maxf(to.length(), 1e-4) + es.pace[i] = 1.0 + if es.attack_cd[i] > 0.0 and d <= _attack_reach() * 2.0: + es.desire_x[i] = -to.x / d + es.desire_z[i] = -to.y / d + else: + es.desire_x[i] = to.x / d + es.desire_z[i] = to.y / d + + ## The flow field toward an item's home: the crop rows, the pen, or its sack's pile at the shed. func field_for(id: int) -> PackedInt32Array: match loot.category[id]: diff --git a/game/scripts/wave_director.gd b/game/scripts/wave_director.gd index ad131f2..b848f74 100644 --- a/game/scripts/wave_director.gd +++ b/game/scripts/wave_director.gd @@ -1,7 +1,7 @@ class_name WaveDirector extends Node3D ## Night arrivals. By default it runs the night's NightPlan: each group's enemies (thieves, crows, -## or fence breakers) arrive at their times after nightfall, each at its own random point along +## fence breakers, or UFOs) arrive at their times after nightfall, each at its own random point along ## the farm's edge (FarmZones.edge): on foot GameConfig.spawn_margin outside the map, at a free ## cell with a route onto the farm (ground_spawns); flyers flyer_spawn_margin out. They go for the ## group's target (crops, animals, supplies, or any of their type's targets by @@ -108,6 +108,8 @@ static func entry_label(entry: Array) -> String: return "Crows" EnemySystem.TYPE_BREAKER: return "Alien robots" + EnemySystem.TYPE_UFO: + return "UFOs" match int(entry[1]): LootSystem.Category.CROP: return "Aliens after the crops" diff --git a/game/tests/test_nights.gd b/game/tests/test_nights.gd index 1be53fb..e5c7df5 100644 --- a/game/tests/test_nights.gd +++ b/game/tests/test_nights.gd @@ -71,8 +71,8 @@ func _designs(r: GameTestRunner, sim: FarmSim) -> void: 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() == 3 and waves_5 >= 3 and n[4].night_length > sim.config.night_length, - "night 5: all three types in %d waves, over a longer %.0f s night" % [waves_5, n[4].night_length]) + 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))) diff --git a/game/tests/test_roster.gd b/game/tests/test_roster.gd index e88090a..8b12d7a 100644 --- a/game/tests/test_roster.gd +++ b/game/tests/test_roster.gd @@ -29,6 +29,7 @@ func run(r: GameTestRunner) -> void: _crows(r, sim, player) _thieves(r, sim, player) _breakers(r, sim, player) + _ufos(r, sim, player) _characters(r, sim) _alerts(r, farm, sim, player) _scenarios(r, farm, sim) @@ -220,6 +221,60 @@ func _breakers(r: GameTestRunner, sim: FarmSim, player: PlayerCharacter) -> void _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() + + ## Ground enemies are drawn by pooled characters; flyers aren't. func _characters(r: GameTestRunner, sim: FarmSim) -> void: var es := sim.enemies From 04657c713e6b06d44583a255d7e1d9e529b9f5bc Mon Sep 17 00:00:00 2001 From: Forrest Date: Mon, 5 Oct 2026 14:46:55 -0400 Subject: [PATCH 2/2] Task #15 follow-up: shrink night 5's UFO group to a modest 7 count_scale is plan-wide (7x by default) with no per-group override, so the smallest actual UFO wave is the group's count floor of 1 -> 7 UFOs, not 4 -> 28. Against a 3-5 HP scarecrow, 28 was far from the "modest 3-4" originally asked for; 1 (scaled to 7) matches the brief without adding a per-group scale mechanism. Co-Authored-By: Claude Sonnet 5 --- game/resources/nights/night_5.tres | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/resources/nights/night_5.tres b/game/resources/nights/night_5.tres index dcdea3e..ea4a0b0 100644 --- a/game/resources/nights/night_5.tres +++ b/game/resources/nights/night_5.tres @@ -74,7 +74,7 @@ groups = Array[Dictionary]([{ "time": 125.0, "type": "breaker" }, { -"count": 4, +"count": 1, "every": 3.0, "target": "any", "time": 140.0,