Class AIBodyGrid
- Namespace
- FishMMO.Server.Implementation.World.SceneServer.AI
- Assembly
- FishMMO.Server.dll
Where every NPC body in one physics scene stands, bucketed into a uniform grid so a separation query reads a handful of cells instead of running a physics overlap.
public sealed class AIBodyGrid
- Inheritance
-
AIBodyGrid
- Inherited Members
Remarks
What it replaced. Each NPC's separation used to run its own OverlapSphere on
the character layer every brain tick, standing or moving. NPCs live on that layer too, so
every query returned the NPC's own collider and paid a rigidbody read and an interface
component lookup to reject it, then the same again for each neighbour: four hundred Active
NPCs was three thousand overlaps and at least three thousand interface lookups a second.
AIBrainHost now fills one of these per scene, at most once per network tick
and only for a scene something asked about, and every separation in that scene reads it.
Equivalent to the overlap it replaced. Resolve(Vector3, uint, IReadOnlyList<Vector3>, IReadOnlyList<uint>, float, float) weighs neighbours by horizontal centre distance and ignores anyone at or beyond the radius, so a collider the sphere merely grazed contributed nothing anyway. The vertical reach stands in for the sphere's height test, so an NPC on a bridge does not push the one underneath it.
Plain C#, no physics and no Unity objects, so it can be pinned in an EditMode test. Each cell's bodies are a linked list threaded through flat arrays, and the arrays and the cell table are reused, so a rebuild allocates nothing once warm.
Constructors
AIBodyGrid(float)
Creates an empty grid.
public AIBodyGrid(float cellSize = 2)
Parameters
cellSizefloatCell edge in metres; a non-positive value uses DefaultCellSize.
Fields
DefaultCellSize
Default cell edge, in metres. About twice a humanoid's separation radius, so the common query reads a 3 x 3 block.
public const float DefaultCellSize = 2
Field Value
Properties
CellSize
Cell edge, in metres.
public float CellSize { get; }
Property Value
Count
Bodies added since the last Clear().
public int Count { get; }
Property Value
Methods
Add(Vector3, uint)
Adds one body.
public void Add(Vector3 position, uint key)
Parameters
positionVector3Where it stands.
keyuintIts identity key, used to exclude the asker and to break ties.
Clear()
Empties the grid, keeping its storage.
public void Clear()
Query(Vector3, float, float, uint, List<Vector3>, List<uint>)
Appends every body within radius of center
horizontally and within verticalReach of it vertically, except the
one keyed excludeKey.
public int Query(Vector3 center, float radius, float verticalReach, uint excludeKey, List<Vector3> positionsOut, List<uint> keysOut)
Parameters
centerVector3The asking body's position.
radiusfloatHorizontal reach, in metres.
verticalReachfloatVertical reach, in metres.
excludeKeyuintThe asker's own key.
positionsOutList<Vector3>Receives each neighbour's position.
keysOutList<uint>Receives each neighbour's key, in the same order.
Returns
- int
How many neighbours were appended.