Table of Contents

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

cellSize float

Cell 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

float

Properties

CellSize

Cell edge, in metres.

public float CellSize { get; }

Property Value

float

Count

Bodies added since the last Clear().

public int Count { get; }

Property Value

int

Methods

Add(Vector3, uint)

Adds one body.

public void Add(Vector3 position, uint key)

Parameters

position Vector3

Where it stands.

key uint

Its 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

center Vector3

The asking body's position.

radius float

Horizontal reach, in metres.

verticalReach float

Vertical reach, in metres.

excludeKey uint

The asker's own key.

positionsOut List<Vector3>

Receives each neighbour's position.

keysOut List<uint>

Receives each neighbour's key, in the same order.

Returns

int

How many neighbours were appended.