Class AIBrainHost
- Namespace
- FishMMO.Server.Implementation.World.SceneServer.AI
- Assembly
- FishMMO.Server.dll
Attaches, drives and detaches the server-side brain of every NPC spawned under one FishNet.Managing.NetworkManager.
public sealed class AIBrainHost
- Inheritance
-
AIBrainHost
- Inherited Members
Remarks
Why a host. NPC prefabs are shared with clients and carry no AI. The server adds an AIController the first time it spawns an NPC, keeps it for the life of the pooled instance, and gives it the archetype the AIBrainCatalogue (or the spawner) names on every spawn. One network-tick subscription drives every brain rather than one per NPC.
Two ways in. A spawn path that places the NPC — a spawner, the pet system — calls
Prepare(NPC, Vector3, AIArchetypeTemplate, Vector3[]) before ServerManager.Spawn, so the agent is warped onto the mesh
while the object is active and in its final scene. Anything else that spawns an NPC (a boss
calling in adds, a harness) is caught by OnServerSpawned and prepared where
it stands.
Packs tick here too. An NPCGroup is handed to the host of its first
member and ticked after the brains on every network tick, gating itself onto the same AI
tick and its members' LOD tiers; it has no Update of its own.
A plain class rather than a server behaviour so a simulation can host brains on a bare FishNet server; AISystem is the production wrapper.
Constructors
AIBrainHost(NetworkManager, AIBrainCatalogue)
Creates a host. Nothing happens until Start().
public AIBrainHost(NetworkManager networkManager, AIBrainCatalogue catalogue)
Parameters
networkManagerNetworkManagerThe network manager whose NPCs this host drives.
catalogueAIBrainCatalogueWhich brain each NPC prefab runs.
Fields
MaxConsecutiveTickFaults
Failures in a row, with no completed brain run between them, after which a brain is taken off the tick.
public const int MaxConsecutiveTickFaults = 30
Field Value
Remarks
Thirty: one second of body steps at the project's 30 Hz network tick, or four seconds of Active-tier brain runs. Every transient cause seen so far — a target despawned mid-tick, a scene unloading under the agent — clears on the very next run, and any run that completes resets the count, so thirty straight failures is a brain that is broken, not unlucky: an archetype with a null slot, a state that throws on entry. Carrying on would burn the tick on it for the rest of the NPC's life and leave it half-updated each time.
Counted in failures rather than seconds so the threshold means the same thing at every LOD tier; a Far-tier brain that runs once a second takes proportionally longer to trip.
Properties
Catalogue
Which brain each NPC prefab runs. Null leaves every NPC without an archetype unless its spawner names one.
public AIBrainCatalogue Catalogue { get; set; }
Property Value
Count
Number of brains currently ticking. Diagnostics and tests.
public int Count { get; }
Property Value
GroupCount
Number of packs currently ticking. Diagnostics and tests.
public int GroupCount { get; }
Property Value
NetworkManager
The network manager whose NPCs this host drives.
public NetworkManager NetworkManager { get; }
Property Value
- NetworkManager
Running
public bool Running { get; }
Property Value
Methods
Prepare(NPC, Vector3, AIArchetypeTemplate, Vector3[])
Gives npc its brain for this spawn and starts it ticking.
public AIController Prepare(NPC npc, Vector3 home, AIArchetypeTemplate archetypeOverride = null, Vector3[] waypoints = null)
Parameters
npcNPCThe NPC being spawned.
homeVector3Where it leashes and wanders around.
archetypeOverrideAIArchetypeTemplateA spawner's archetype for this placement, or null for the prefab's.
waypointsVector3[]Optional patrol route.
Returns
- AIController
The brain, or null when
npcis null.
Remarks
Call after the NPC is active and in its final scene, and before
ServerManager.Spawn: initialising warps the agent onto the NavMesh at
home and enters the archetype's first state, both of which need a live
agent. Safe to call again for the same spawn; the latest call wins.
RetireBrainOf(NetworkManager, NetworkObject)
Retires the brain of an instance going back into the pool unspawned, through the host
running for networkManager, or directly when none is.
public static void RetireBrainOf(NetworkManager networkManager, NetworkObject instance)
Parameters
networkManagerNetworkManagerThe network manager whose host prepared the brain.
instanceNetworkObjectThe pooled instance; one with no brain is left alone.
ShouldQuarantine(int)
Whether a brain with this many failures in a row is taken off the tick. Pure, so the threshold can be pinned.
public static bool ShouldQuarantine(int consecutiveFailures)
Parameters
consecutiveFailuresintFailures since the brain last completed a run.
Returns
- bool
True to quarantine.
Start()
Starts hosting: follows NPC spawns and the network tick.
public void Start()
Stop()
Stops hosting. Brains stay on their NPCs but no longer tick.
public void Stop()
TryGet(NetworkManager, out AIBrainHost)
The running host for networkManager, if there is one.
public static bool TryGet(NetworkManager networkManager, out AIBrainHost host)
Parameters
networkManagerNetworkManagerhostAIBrainHost