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 <noreply@anthropic.com>
This commit is contained in:
Forrest 2026-10-06 16:17:31 -04:00
parent 3bb835f7c3
commit af48cff80d
6 changed files with 201 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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