From af48cff80dc0c4a088d8949d4ef59e9ca3e5c8e3 Mon Sep 17 00:00:00 2001 From: Forrest Date: Tue, 6 Oct 2026 16:17:31 -0400 Subject: [PATCH] Task #25: arrivals wait for a free character A planned ground arrival whose type has no free character used to be skipped (spawn_skipped) and lost. Now it waits and comes in as soon as one frees, without holding back other types; any still waiting at sunrise are dropped and counted. - EnemySystem.can_spawn(type): room and, on foot, a free character (EnemyCharacters.has_free; a corpse counts), no side effects. - WaveDirector.step: due arrivals that can't spawn go to a per-type FIFO; waiting ones are admitted first each tick. waited / dropped counters next to spawned, reset at nightfall; waiting() for tests. - GameConfig.character_pool {"thief": 48, "breaker": 20} replaces EnemyCharacters.POOL; the pools are built from it at setup. - RULES.md, Night plans: the real limits and the waiting rule. - test_roster: full thief pool -> the thief waits, crows stay on time, a freed character (corpse) lets it in, sunrise drops and counts. Co-Authored-By: Claude Opus 5.5 --- game/RULES.md | 8 ++- game/scripts/enemy_characters.gd | 33 ++++++++++-- game/scripts/enemy_system.gd | 11 +++- game/scripts/game_config.gd | 6 +++ game/scripts/wave_director.gd | 64 ++++++++++++++++++++-- game/tests/test_roster.gd | 92 +++++++++++++++++++++++++++++++- 6 files changed, 201 insertions(+), 13 deletions(-) diff --git a/game/RULES.md b/game/RULES.md index 484f41e..3009832 100644 --- a/game/RULES.md +++ b/game/RULES.md @@ -65,8 +65,12 @@ is a few crows at a time to learn the stab on. The counts below are as played: | 4 | From both sides | Dividing attention | 3 moments when two groups arrive at once: 77 crows, 42 thieves, 14 breakers | | 5 | The last night | The whole farm layout | 3 announced waves over a 180 s night: 84 crows, 56 thieves, 21 breakers | -Up to 160 enemies can be on the farm at once (`enemy_capacity`): 48 thieves and 20 fence -breakers on foot (the character pool), and the rest in the air. +**How many at once.** Up to 800 enemies can be on the farm at once, all types together +(`enemy_capacity`). Enemies on foot are also limited by their characters (`character_pool`): at +most 48 thieves and 20 fence breakers at a time. Crows and UFOs only count toward the 800. An +enemy that's due while its type is at its limit **waits**. It arrives as soon as one of its kind +leaves or is killed (the body left lying there doesn't keep it waiting). Other enemies due +meanwhile still arrive on time. Any still waiting at sunrise never come. Every kind of enemy in tonight's plan shows up in the dusk warning in the phase HUD, so nothing unannounced arrives (the last night's hint spells out its three waves). Nothing marks where they'll diff --git a/game/scripts/enemy_characters.gd b/game/scripts/enemy_characters.gd index c43f83b..bfaff95 100644 --- a/game/scripts/enemy_characters.gd +++ b/game/scripts/enemy_characters.gd @@ -1,8 +1,9 @@ class_name EnemyCharacters extends Node3D ## The KayKit characters that draw ground enemies (EnemySystem keeps the simulation; flyers stay -## in its MultiMesh). A fixed pool per type is created at load and never grows: a ground enemy -## only spawns while its type has a free character. +## in its MultiMesh). A fixed pool per type (GameConfig.character_pool) is created at setup and +## never grows: a ground enemy only spawns while its type has a free character (has_free; a due +## arrival waits for one, WaveDirector). ## thief Mannequin_Medium, tinted dark, Rig_Medium clips (player_animations.res) ## breaker Mannequin_Large, rust red, scaled down, a club in its right hand, Rig_Large clips ## (breaker_animations.res) @@ -12,7 +13,6 @@ extends Node3D ## for CORPSE_TIME; a corpse is only for show, so a new enemy can take it over at once when no ## other character is free. -const POOL := {EnemySystem.TYPE_THIEF: 48, EnemySystem.TYPE_BREAKER: 20} const CORPSE_TIME := 1.6 const HIT_TIME := 0.35 const KIT := "res://assets/kaykit_characters/characters/" @@ -36,6 +36,8 @@ const CLIPS := { var enemies: EnemySystem var config: GameConfig +## Characters per ground type (EnemySystem type id -> count), from GameConfig.character_pool. +var pool := {} ## Per character (index into the arrays below): its type, the enemy id it draws (-1 when free), ## and timers. var type := PackedByteArray() @@ -55,10 +57,17 @@ func setup(p_enemies: EnemySystem, p_config: GameConfig) -> void: enemies = p_enemies config = p_config var shader := load("res://game/resources/enemy_overlay.gdshader") as Shader - for t: int in POOL: + pool.clear() + for type_name in p_config.character_pool: + var t := EnemySystem.TYPE_NAMES.find(String(type_name)) + if not SCENES.has(t): + push_warning("GameConfig.character_pool: '%s' isn't an enemy type on foot" % type_name) + continue + pool[t] = maxi(int(p_config.character_pool[type_name]), 0) + for t: int in pool: var scene := load(SCENES[t]) as PackedScene var library := load(LIBRARIES[t]) as AnimationLibrary - for k in POOL[t]: + for k in pool[t]: var root := Node3D.new() root.name = "%s_%d" % [EnemySystem.TYPE_NAMES[t], k] root.visible = false @@ -93,6 +102,20 @@ func setup(p_enemies: EnemySystem, p_config: GameConfig) -> void: hit_left.append(0.0) +## The type's characters in all (0 for a type without any). +func pool_size(t: int) -> int: + return int(pool.get(t, 0)) + + +## Whether acquire(t, ...) would succeed now: a character of the type not drawing a live enemy (a +## corpse counts: it can be taken over). No side effects. +func has_free(t: int) -> bool: + for c in type.size(): + if type[c] == t and enemy_id[c] < 0: + return true + return false + + ## Characters of the type not drawing a live enemy (corpses included: they can be taken over). func free_count(t: int) -> int: var n := 0 diff --git a/game/scripts/enemy_system.gd b/game/scripts/enemy_system.gd index dcd432d..208195a 100644 --- a/game/scripts/enemy_system.gd +++ b/game/scripts/enemy_system.gd @@ -236,9 +236,18 @@ func clear() -> void: characters.clear() +## Whether spawn(enemy_type, ...) would succeed now: a free slot and, for a ground type, a free +## character to draw it (EnemyCharacters.has_free: a corpse counts, it can be taken over). No side +## effects; WaveDirector holds a due arrival back until it's true. +func can_spawn(enemy_type: int) -> bool: + if active_count >= capacity or _free_top <= 0: + return false + return characters == null or t_flying[enemy_type] == 1 or characters.has_free(enemy_type) + + ## Acquires a slot and initializes every state field. Returns a handle, or -1 if the pool is ## exhausted, or for a ground enemy there's no free character to draw it (the spawn is skipped -## and counted; nothing grows). +## and counted in spawn_skipped; nothing grows. WaveDirector checks can_spawn first instead). func spawn(enemy_type: int, x: float, z: float, heading: Vector2, p_want: int = LootSystem.ANY) -> int: if active_count >= capacity or _free_top <= 0: spawn_skipped += 1 diff --git a/game/scripts/game_config.gd b/game/scripts/game_config.gd index 3014499..7a73071 100644 --- a/game/scripts/game_config.gd +++ b/game/scripts/game_config.gd @@ -323,7 +323,13 @@ extends Resource @export var radar_refresh: float = 0.25 @export_group("Enemies") +## Enemies on the farm at once, every type together. @export_range(1, 1024) var enemy_capacity: int = 800 +## Enemies on foot are drawn by KayKit characters (EnemyCharacters), a fixed number per type +## (EnemySystem.TYPE_NAMES; ground types only), all created at load: at most this many of each are +## on the farm at once. One that's due while every character of its type is in use waits for one +## to come free; any still waiting at sunrise never arrive (WaveDirector). +@export var character_pool: Dictionary = {"thief": 48, "breaker": 20} ## The enemy roster (EnemySystem.TYPE_NAMES), one row per type: ## speed, acceleration m/s, m/s^2 ## hp hits it takes diff --git a/game/scripts/wave_director.gd b/game/scripts/wave_director.gd index 2aa30b9..77780e7 100644 --- a/game/scripts/wave_director.gd +++ b/game/scripts/wave_director.gd @@ -5,7 +5,10 @@ extends Node3D ## 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 -## GameConfig.target_weights); nothing arrives by day or after sunrise. From dusk until +## GameConfig.target_weights); nothing arrives by day or after sunrise. An arrival that can't come +## in when it's due (EnemySystem.can_spawn: on foot, every character of its type is in use) waits +## and comes in as soon as one is free, while the other types' arrivals keep coming on time; any +## still waiting at sunrise are dropped (waited, dropped). From dusk until ## WARN_AFTER_NIGHTFALL into the night the phase HUD says what tonight brings (not where: they come ## from every side; nothing is marked on the farm itself). debug_plan (F2 in debug builds) ## replaces tonight's plan. Trickle mode (GameConfig.night_thieves > 0, for tests and soaks) @@ -41,9 +44,18 @@ var rng := RandomNumberGenerator.new() ## Seconds since nightfall, and arrivals so far tonight. var night_time: float = 0.0 var spawned: int = 0 +## Tonight's plan arrivals that had to wait when due (they came in later, or not at all), and those +## still waiting at sunrise, which never came. Both reset at nightfall. +var waited: int = 0 +var dropped: int = 0 var _queue: Array = [] # [time, want, type], sorted var _next: int = 0 +## Due arrivals waiting for their type to be able to spawn, per type id: [time, want, type] entries +## oldest first, and the index of the first one still waiting (those before it have come in). +var _waiting: Array[Array] = [] +var _wait_head := PackedInt32Array() +var _waiting_count: int = 0 var _trickle_wait: float = FIRST_TRICKLE_DELAY @@ -54,6 +66,7 @@ func setup(p_campaign: CampaignController, p_enemies: EnemySystem, p_zones: Farm zones = p_zones plans = p_plans rng.randomize() + _reset_waiting() campaign.phase_changed.connect(_on_phase_changed) @@ -185,20 +198,65 @@ func step(dt: float) -> void: spawn_next(int(TARGETS.get(config.trickle_target, LootSystem.ANY))) _trickle_wait = config.thief_spawn_interval return + _admit_waiting() + # Due arrivals: one comes in now unless its type can't spawn or has others waiting ahead of it + # (then it waits behind them); either way the next due arrival, of any type, is looked at too. while _next < _queue.size() and float(_queue[_next][0]) <= night_time: - spawn(int(_queue[_next][2]), int(_queue[_next][1])) + var e: Array = _queue[_next] _next += 1 + var t := int(e[2]) + if _wait_head[t] < _waiting[t].size() or not enemies.can_spawn(t) or spawn(t, int(e[1])) < 0: + _waiting[t].append(e) + _waiting_count += 1 + waited += 1 + + +## Plan arrivals waiting right now for their type to be able to spawn. +func waiting() -> int: + return _waiting_count + + +## Waiting arrivals come in, oldest first within each type, while their type can spawn. Costs +## nothing while none wait, else one EnemySystem.can_spawn per waiting type that stays full. +func _admit_waiting() -> void: + if _waiting_count == 0: + return + for t in _waiting.size(): + var list := _waiting[t] + while _wait_head[t] < list.size() and enemies.can_spawn(t) and spawn(t, int(list[_wait_head[t]][1])) >= 0: + _wait_head[t] += 1 + _waiting_count -= 1 + if _wait_head[t] > 0 and _wait_head[t] == list.size(): + list.clear() + _wait_head[t] = 0 + + +func _reset_waiting() -> void: + _waiting.clear() + for t in EnemySystem.TYPE_NAMES.size(): + _waiting.append([]) + _wait_head.resize(EnemySystem.TYPE_NAMES.size()) + _wait_head.fill(0) + _waiting_count = 0 ## Nightfall: tonight's arrivals are queued and its enemy scales (NightPlan.enemy_speed_scale and ## enemy_hp_scale; the debug plan's when set) go to EnemySystem, until the morning (then 1 again). -func _on_phase_changed(phase: CampaignController.Phase, _previous: CampaignController.Phase) -> void: +## When the night ends (sunrise, or cut short by a retry or a game over), arrivals still waiting +## are dropped and counted. +func _on_phase_changed(phase: CampaignController.Phase, previous: CampaignController.Phase) -> void: + if previous == CampaignController.Phase.NIGHT and phase != CampaignController.Phase.NIGHT: + dropped += _waiting_count + _reset_waiting() if phase == CampaignController.Phase.MORNING: _set_enemy_scales(null) if phase != CampaignController.Phase.NIGHT: return night_time = 0.0 spawned = 0 + waited = 0 + dropped = 0 + _reset_waiting() _trickle_wait = FIRST_TRICKLE_DELAY _queue.clear() _next = 0 diff --git a/game/tests/test_roster.gd b/game/tests/test_roster.gd index 27ebeba..823a38e 100644 --- a/game/tests/test_roster.gd +++ b/game/tests/test_roster.gd @@ -4,7 +4,7 @@ extends RefCounted ## 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; and the debug scenarios. +## escalation, and cap; the debug scenarios; and planned arrivals waiting for a free character. const P := CampaignController.Phase const C := LootSystem.Category @@ -39,6 +39,7 @@ func run(r: GameTestRunner) -> void: _characters(r, sim) _alerts(r, farm, sim, player) _scenarios(r, farm, sim) + _waiting(r, sim) farm.queue_free() await r.process_frame @@ -335,7 +336,8 @@ 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 == EnemyCharacters.POOL[EnemySystem.TYPE_THIEF] and ch.in_use() == 0, "%d thief characters waiting in the pool (%d of them corpses still fading)" % [thieves, ch.corpses()]) + 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: @@ -433,6 +435,92 @@ func _scenarios(r: GameTestRunner, farm: Node3D, sim: FarmSim) -> void: 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)