Table of Contents

Class SpawnerRuntime

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

One running spawner: a baked SpawnerDefinition placed in one loaded instance of its scene. Spawns and respawns networked objects, with pooling, conditions and timers.

public sealed class SpawnerRuntime : ISpawnOwner
Inheritance
SpawnerRuntime
Implements
Inherited Members

Remarks

Created by SpawnerHost when a world scene finishes loading on the server and dropped when it unloads. Stacked instances of one scene each get their own set. The spawned objects hold this as their ISpawnOwner; nothing on a client ever sees it.

Constructors

SpawnerRuntime(SpawnerDefinition, Scene, NetworkManager, SpawnerScheduler, IReadOnlyList<SpawnerRuntime>)

public SpawnerRuntime(SpawnerDefinition definition, Scene scene, NetworkManager networkManager, SpawnerScheduler scheduler, IReadOnlyList<SpawnerRuntime> siblings = null)

Parameters

definition SpawnerDefinition
scene Scene
networkManager NetworkManager
scheduler SpawnerScheduler
siblings IReadOnlyList<SpawnerRuntime>

Fields

HabitatTries

Random points tried for ground the species lives on before the spawn waits for the next check.

public const int HabitatTries = 8

Field Value

int

HerdRadius

Metres within which spawned members of one species count as one herd.

public const float HerdRadius = 40

Field Value

float

HerdSpread

How far from its herd-mate a new member appears, metres.

public const float HerdSpread = 10

Field Value

float

SchedulerIndex

This spawner's position in Scheduler's active list, or NotActive when it has no work outstanding.

public int SchedulerIndex

Field Value

int

Remarks

Stored here so the scheduler can drop a spawner without searching for it. Owned by the scheduler; nothing else should write it.

Properties

Definition

What this spawner produces, and how.

public SpawnerDefinition Definition { get; }

Property Value

SpawnerDefinition

NetworkManager

The network manager that spawns and pools the objects.

public NetworkManager NetworkManager { get; }

Property Value

NetworkManager

Pack

This spawner's current pack, or null when its pack is disabled or no member of it stands.

public NPCGroup Pack { get; }

Property Value

NPCGroup

Remarks

One pack per spawner at a time. Every NPC the spawner spawns joins it with its entry's role (JoinPack(AIController, SpawnableSettings)); members leave on death, despawn and pool reset; and when the last one leaves the pack is released, so the next spawn founds another. A respawn while any member stands therefore rejoins the pack it left.

PendingRespawnCount

Number of respawns owed.

public int PendingRespawnCount { get; }

Property Value

int

Running

True once Start() has run and Stop() has not.

public bool Running { get; }

Property Value

bool

Scene

The scene instance objects are spawned into.

public Scene Scene { get; }

Property Value

Scene

Scheduler

The scheduler this spawner reports its outstanding work to.

public SpawnerScheduler Scheduler { get; }

Property Value

SpawnerScheduler

Spawned

The objects currently alive (or lying as corpses) that this spawner produced.

public IReadOnlyCollection<ISpawnable> Spawned { get; }

Property Value

IReadOnlyCollection<ISpawnable>

SpawnedCount

Number of objects currently spawned.

public int SpawnedCount { get; }

Property Value

int

WildlifePackId

The pack every wild animal this spawner produces belongs to (PackId): predators of one spawner never harm each other, while two spawners' packs fight it out. Independent of the combat Pack, which a spawner may leave disabled.

public int WildlifePackId { get; }

Property Value

int

Methods

ConsumesTimer(SpawnOutcome)

Whether a spawn attempt used up the respawn timer that asked for it.

public static bool ConsumesTimer(SpawnOutcome outcome)

Parameters

outcome SpawnOutcome

What the attempt came to.

Returns

bool

True when the timer is spent.

Remarks

The rule is whether the object is still owed. It is not when one entered the world — tracked or, for a prefab with no ISpawnable, not — or when there is no slot for it (the cap, or every unique entry alive; a death queues a fresh timer). It is when nothing entered the world for a reason that can pass: a misconfigured entry (a random or weighted spawner picks again next time), an unloaded scene, or a spawn that failed and was rolled back.

The timer used to be removed before the attempt whatever came of it, so every failure left the spawner one short for the life of the scene — and a spawner with one bad entry among good ones bled a slot each time it picked the bad one, until it was empty.

Despawn(ISpawnable)

Returns spawnable to the pool and schedules its respawn.

public void Despawn(ISpawnable spawnable)

Parameters

spawnable ISpawnable

An entity this spawner created.

Remarks

Schedules a respawn for the object and returns it to the pool. Reached from the object itself (a corpse decaying, a pickup taken) through Despawn().

GetSibling(int)

The spawner at index in the same scene instance's table, or null.

public SpawnerRuntime GetSibling(int index)

Parameters

index int

Returns

SpawnerRuntime

GetSpawnIndex()

Gets the index of the next spawnable to use, based on the configured spawn type.

public int GetSpawnIndex()

Returns

int

The index of the selected spawnable.

HasRespawnWork()

Whether this spawner has anything left to respawn.

public bool HasRespawnWork()

Returns

bool

True when a respawn is outstanding.

Remarks

Decides membership of Scheduler's active list, and so decides whether this spawner costs anything at all. A spawner at its cap or with no pending timers is not walked, which is the whole point: the cost of respawning scales with how much of the world is in motion rather than with how much of it exists.

SpawnObject()

Spawns a new object in the world using the selected spawnable settings and position logic.

public SpawnOutcome SpawnObject()

Returns

SpawnOutcome

What the attempt came to; see ConsumesTimer(SpawnOutcome) for what each means for the respawn that asked.

Remarks

Tracked or rolled back, never half-made. Once an instance has come out of the pool, everything after it — the settings' OnSpawned, the move into the scene, the brain's preparation, the network spawn and the tracking — either completes or is undone in a finally: the owner reference is cleared and the instance goes back to the pool, despawned first if the network spawn had got that far. An exception used to leave the instance in the world untracked, so it was never despawned or pooled, and the spawner never counted it.

A problem that stops the attempt before that point — a misconfigured entry, an unloaded scene — is logged once per spawner and entry, not at every check that meets it again.

Start()

Pre-warms the pool, spawns the initial population and schedules the rest.

public void Start()

Stop()

Stops the spawner: leaves the schedule and forgets its bookkeeping.

public void Stop()

Remarks

Called when the scene instance unloads. The objects themselves went with the scene: their owner reference is cleared so a pooled instance cannot hand itself back to a spawner that no longer runs, and SpawnerPool is told those instances no longer exist, so the next scene to reserve the prefab makes them again at its load rather than one at a time during play.

TryRespawn()

Attempts to respawn objects if their timers have elapsed and respawn conditions are met.

public void TryRespawn()

Remarks

Public so anything holding a spawner can force an immediate attempt rather than waiting for its scheduled wake. Not budgeted: the caller asked for it now.

WildlifePackFor(int)

The pack id of one species from this spawner: never 0, distinct per spawner and per species.

public int WildlifePackFor(int prefabId)

Parameters

prefabId int

Returns

int