Table of Contents

Class SpawnerScheduler

Namespace
FishMMO.Server.Implementation.World.SceneServer.Spawner
Assembly
FishMMO.Server.dll

Runs the respawn checks for every SpawnerRuntime that currently has something to respawn, from one per-frame call instead of one per spawner.

public sealed class SpawnerScheduler
Inheritance
SpawnerScheduler
Inherited Members

Remarks

The interval a spawner polls on is still its own — see RespawnCheckIntervalMinimum. What changes here is which spawners are asked at all. A spawner with nothing pending, which is most of them in a world at rest, is not in the list and costs nothing whatsoever: not a check, not a gate comparison, and not an Update dispatch.

The list holds references, not scheduling state. It is a plain List<T> of spawners with membership maintained by swap-removal, so a spawner occupies one reference while it has work and nothing at all when it does not. There is no ordering structure, no queued entries, and no per-spawner allocation. Measured against 100,000 spawners, whose own timer lists and spawned dictionaries come to roughly 107 MB, a membership list covering a tenth of them adds about 0.25 MB.

One per SpawnerHost, ticked by SpawnerSystem's per-frame update. It used to be static with a hidden driver object of its own; an instance is what lets two servers in one process (a simulation beside the real thing) keep their spawners apart.

The clock. Every deadline and check interval its spawners keep is in seconds on Now, a monotonic clock. Respawn deadlines used to be DateTime.UtcNow, so the host's wall clock being stepped — an NTP correction, a VM resumed — moved every deadline at once: forward, and every camp in the world respawned on one frame; back, and none did for as long as the step.

Constructors

SpawnerScheduler()

Creates a scheduler on the process's monotonic clock, MonotonicClock.

public SpawnerScheduler()

SpawnerScheduler(Func<double>)

Creates a scheduler on a clock of the caller's choosing.

public SpawnerScheduler(Func<double> clock)

Parameters

clock Func<double>

Seconds, never decreasing. Null uses the process's monotonic clock.

Fields

DefaultSpawnsPerFrame

Default for SpawnsPerFrame.

public const int DefaultSpawnsPerFrame = 8

Field Value

int

FramesPerSweep

How many frames one full pass over the active list is spread across.

public const int FramesPerSweep = 60

Field Value

int

Remarks

A spawner's own check interval is seconds long, so being visited within about a second costs nothing anybody can observe and keeps the per-frame walk proportional to a slice of the active list rather than all of it.

NotActive

The index stored on a spawner that is not currently in FishMMO.Server.Implementation.World.SceneServer.Spawner.SpawnerScheduler.active.

public const int NotActive = -1

Field Value

int

Properties

ActiveCount

How many spawners currently have work outstanding. Exposed for tests and diagnostics.

public int ActiveCount { get; }

Property Value

int

Now

Now, in seconds, on the clock every deadline of this scheduler's spawners is measured on.

public double Now { get; }

Property Value

double

SpawnsPerFrame

Most objects all of this scheduler's spawners together spawn in one Tick(double).

public int SpawnsPerFrame { get; set; }

Property Value

int

Remarks

A spawner drains every due timer when it is visited, which is right for its refill rate but puts a wiped camp's whole population — pooled retrieval, a ground cast, a NavMesh warp and a network spawn each — on one frame, and a raid clearing several camps at once on the same one. The budget spreads that over consecutive frames.

It cannot starve anyone. A spawner cut short by the budget keeps the sweep's cursor and is first next frame, with its interval gate still open, so it carries on where it stopped rather than waiting out another interval. The first spawner of every frame therefore always has the whole budget, each such frame spends at least one of that spawner's finite due timers, and the cursor moves on as soon as it has none left. At least 1.

Methods

Clear()

Empties the active list. Intended for tests and for a clean shutdown between sessions.

public void Clear()

Refresh(SpawnerRuntime)

Brings spawner into or out of the active list to match whether it has anything to respawn.

public void Refresh(SpawnerRuntime spawner)

Parameters

spawner SpawnerRuntime

The spawner whose membership should be refreshed.

Remarks

Call after anything that changes that: a despawn adding a respawn timer, a spawn consuming one, or the spawner reaching its cap and clearing them. Idempotent, so callers need not track whether the spawner was already in the list.

Tick(double)

Gives a slice of the spawners with outstanding work the chance to run their respawn check.

public void Tick(double now)

Parameters

now double

The current time on Now's clock.

Remarks

The list is swept rather than walked: each frame advances a cursor through a fraction of it, so every active spawner is visited about once per FramesPerSweep frames instead of every frame. Their own interval gate is measured in seconds (RespawnCheckIntervalMinimum), so visiting more often than that buys nothing — it only moves the cost from the spawners that have work to the ones that are merely waiting, which is most of them.

The clock is read once by the caller rather than by each spawner, so every spawner in a sweep judges its deadlines against the same instant.

Each spawner is isolated. A pass that throws — a spawnable's OnSpawned, a brain that cannot be prepared, a network spawn — is reported to that spawner's own fault log and the sweep carries on. The spawner has already put its timer back and rolled back the half-made object (see SpawnObject()), so it simply tries again at its next check. Before, the exception left the sweep, skipping every spawner after it, and did so again at every check of the broken one.

Spawning stops for the frame once SpawnsPerFrame objects have been spawned; see that property for why that cannot starve a spawner.

Unregister(SpawnerRuntime)

Drops spawner from the active list.

public void Unregister(SpawnerRuntime spawner)

Parameters

spawner SpawnerRuntime

The spawner to drop.

Remarks

Called when a spawner's scene unloads, so it is not walked and its reference is not held.