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>
276 lines
10 KiB
GDScript
276 lines
10 KiB
GDScript
class_name WaveDirector
|
|
extends Node3D
|
|
## Night arrivals. By default it runs the night's NightPlan: each group's enemies (thieves, crows,
|
|
## 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
|
|
## 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)
|
|
## ignores the plan and keeps that many enemies of GameConfig.trickle_type on the farm at night,
|
|
## the next arriving thief_spawn_interval after one leaves. enabled = false stops all arrivals.
|
|
## rng picks the points: seed it for a repeatable night.
|
|
|
|
signal arrived(handle: int, at: Vector2)
|
|
|
|
const WARN_AFTER_NIGHTFALL := 12.0
|
|
const FIRST_TRICKLE_DELAY := 3.0
|
|
## spawn()'s default spot: a random point along the edge.
|
|
const ANYWHERE := Vector2(INF, INF)
|
|
## NightPlan group target -> LootSystem.Category (or ANY).
|
|
const TARGETS := {
|
|
"crops": LootSystem.Category.CROP,
|
|
"animals": LootSystem.Category.ANIMAL,
|
|
"supplies": LootSystem.Category.SUPPLY,
|
|
"any": LootSystem.ANY,
|
|
}
|
|
|
|
var enabled := true
|
|
var plans: Array[NightPlan] = []
|
|
## Replaces tonight's plan when set (debug scenarios).
|
|
var debug_plan: NightPlan
|
|
var config: GameConfig
|
|
var campaign: CampaignController
|
|
var enemies: EnemySystem
|
|
var zones: FarmZones
|
|
## Set by FarmSim: where ground enemies can arrive (cell centers along the edge with a route in).
|
|
var ground_spawns := PackedVector2Array()
|
|
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
|
|
|
|
|
|
func setup(p_campaign: CampaignController, p_enemies: EnemySystem, p_zones: FarmZones, p_plans: Array[NightPlan]) -> void:
|
|
campaign = p_campaign
|
|
config = p_campaign.config
|
|
enemies = p_enemies
|
|
zones = p_zones
|
|
plans = p_plans
|
|
rng.randomize()
|
|
_reset_waiting()
|
|
campaign.phase_changed.connect(_on_phase_changed)
|
|
|
|
|
|
func plan_for(night: int) -> NightPlan:
|
|
if plans.is_empty():
|
|
return null
|
|
return plans[clampi(night - 1, 0, plans.size() - 1)]
|
|
|
|
|
|
func tonight() -> NightPlan:
|
|
return debug_plan if debug_plan != null else plan_for(campaign.state.night)
|
|
|
|
|
|
## A night's length from its plan (0 or less: GameConfig.night_length). The debug plan counts
|
|
## for tonight.
|
|
func night_length(night: int) -> float:
|
|
var plan := debug_plan if debug_plan != null and night == campaign.state.night else plan_for(night)
|
|
return plan.night_length if plan != null else 0.0
|
|
|
|
|
|
func trickle() -> bool:
|
|
return config.night_thieves > 0
|
|
|
|
|
|
static func want_of(group: Dictionary) -> int:
|
|
return int(TARGETS.get(String(group.get("target", "any")), LootSystem.ANY))
|
|
|
|
|
|
## A NightPlan group's enemy type ("thief", "crow", "breaker") as an EnemySystem type id.
|
|
static func type_of(group: Dictionary) -> int:
|
|
return maxi(EnemySystem.TYPE_NAMES.find(String(group.get("type", "thief"))), 0)
|
|
|
|
|
|
## What tonight brings: its groups' [type, want] pairs (in the plan's order, no repeats).
|
|
func tonight_entries() -> Array:
|
|
var out := []
|
|
var plan := tonight()
|
|
if plan == null or trickle():
|
|
return out
|
|
for g in plan.groups:
|
|
var entry := [type_of(g), want_of(g)]
|
|
if not out.has(entry):
|
|
out.append(entry)
|
|
return out
|
|
|
|
|
|
## What a [type, want] pair is called: "Crows", "Alien robots", "Aliens after the crops",
|
|
## "Aliens after the animals", "Aliens after the supplies", or "Aliens" (any target).
|
|
static func entry_label(entry: Array) -> String:
|
|
match int(entry[0]):
|
|
EnemySystem.TYPE_CROW:
|
|
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"
|
|
LootSystem.Category.ANIMAL:
|
|
return "Aliens after the animals"
|
|
LootSystem.Category.SUPPLY:
|
|
return "Aliens after the supplies"
|
|
return "Aliens"
|
|
|
|
|
|
## Tonight's warning as lines for the HUD: "Tonight, from every side: crows, aliens after the
|
|
## supplies and alien robots".
|
|
func tonight_warning() -> PackedStringArray:
|
|
var labels := PackedStringArray()
|
|
for e in tonight_entries():
|
|
var label := entry_label(e).to_lower()
|
|
if not labels.has(label):
|
|
labels.append(label)
|
|
if labels.is_empty():
|
|
return PackedStringArray()
|
|
return PackedStringArray(["Tonight, from every side: %s" % PhaseHud.join_words(labels)])
|
|
|
|
|
|
func warning_active() -> bool:
|
|
if not enabled or trickle():
|
|
return false
|
|
var phase := campaign.state.phase
|
|
return phase == CampaignController.Phase.DUSK or (phase == CampaignController.Phase.NIGHT and night_time < WARN_AFTER_NIGHTFALL)
|
|
|
|
|
|
## A random point along the farm's edge for an enemy of type: a flyer anywhere on its ring, a
|
|
## ground enemy at one of ground_spawns.
|
|
func spawn_point(type: int) -> Vector2:
|
|
if enemies.t_flying[type] == 1 or ground_spawns.is_empty():
|
|
var margin := config.flyer_spawn_margin if enemies.t_flying[type] == 1 else config.spawn_margin
|
|
return zones.edge_point(rng.randf(), margin)
|
|
return ground_spawns[rng.randi_range(0, ground_spawns.size() - 1)]
|
|
|
|
|
|
## Sends one enemy of type in, going for want: at a random point along the edge, or at the given
|
|
## point. Returns its handle (-1 if the pool is full).
|
|
func spawn(type: int, want: int = LootSystem.ANY, at: Vector2 = ANYWHERE) -> int:
|
|
var p := at if at.is_finite() else spawn_point(type)
|
|
var h := enemies.spawn(type, p.x, p.y, zones.playable.get_center() - p, want)
|
|
if h >= 0:
|
|
spawned += 1
|
|
arrived.emit(h, p)
|
|
return h
|
|
|
|
|
|
## Sends an enemy of the trickle type in.
|
|
func spawn_next(want: int = LootSystem.ANY) -> int:
|
|
return spawn(trickle_type(), want)
|
|
|
|
|
|
func trickle_type() -> int:
|
|
return maxi(EnemySystem.TYPE_NAMES.find(config.trickle_type), 0)
|
|
|
|
|
|
## Physics tick (FarmSim): arrivals while the night lasts.
|
|
func step(dt: float) -> void:
|
|
if campaign.state.phase != CampaignController.Phase.NIGHT:
|
|
return
|
|
night_time += dt
|
|
if not enabled:
|
|
return
|
|
if trickle():
|
|
if enemies.alive_count() >= config.night_thieves:
|
|
_trickle_wait = config.thief_spawn_interval
|
|
return
|
|
_trickle_wait -= dt
|
|
if _trickle_wait <= 0.0:
|
|
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:
|
|
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).
|
|
## 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
|
|
var plan := tonight()
|
|
_set_enemy_scales(plan)
|
|
if plan != null:
|
|
for g in plan.groups:
|
|
var every := NightPlan.scaled_every(g, plan.spacing_scale_in(config))
|
|
for k in NightPlan.scaled_count(g, plan.count_scale_in(config)):
|
|
_queue.append([float(g.get("time", 0.0)) + k * every, want_of(g), type_of(g)])
|
|
_queue.sort_custom(func(a: Array, b: Array) -> bool: return float(a[0]) < float(b[0]))
|
|
|
|
|
|
## A plan's enemy scales on EnemySystem (1 and 1 without a plan, or for a scale that isn't positive).
|
|
func _set_enemy_scales(plan: NightPlan) -> void:
|
|
enemies.night_speed_scale = plan.enemy_speed_scale if plan != null and plan.enemy_speed_scale > 0.0 else 1.0
|
|
enemies.night_hp_scale = plan.enemy_hp_scale if plan != null and plan.enemy_hp_scale > 0.0 else 1.0
|