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

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