UnitSpatialIndex

Utilities View source
UnitSpatialIndex

Deprecated. Cell geometry belongs to the shared grid, so SPATIAL_PARTITION_CELL_SIZE is the setting; one grid cannot have a second cell size for units alone.

Kept exported so an existing UnitSpatialIndex_config still compiles, and defaulted to agree. It is checked at init rather than ignored: a map that had tuned this and upgrades gets told its value is no longer in effect, instead of quietly running on a different grid.

Types

Functions and constants

public constant USE_UNIT_SPATIAL_INDEX = true

Master switch; override from UnitSpatialIndex_config to disable the native-less index.

Configurable. Override it in your map’s config package.

public constant SPATIAL_INDEX_UNITS_PER_TICK = 128

Units re-bucketed per tick; raising this shortens the cycle and query padding.

Configurable. Override it in your map’s config package.

public constant SPATIAL_INDEX_SWEEP_PERIOD = ANIMATION_PERIOD

Time between sweep steps. Raising this reduces timer and refresh work but increases query padding.

Configurable. Override it in your map’s config package.

public constant SPATIAL_INDEX_MAX_UNIT_SPEED = 522.

Engine movement-speed cap. Configurable because gameplay constants can raise the default 522.

Configurable. Override it in your map’s config package.

public constant SPATIAL_INDEX_CHECK_HIDDEN = true

Disable when hidden units are never used; saves one native per corpse the query finds.

Configurable. Override it in your map’s config package.

public constant SPATIAL_INDEX_CELL_SIZE = SPATIAL_PARTITION_CELL_SIZE

Declaration shown above.

Configurable. Override it in your map’s config package.

public function unit.updateSpatialIndex()

Re-buckets one unit immediately, and registers an indexed unit the index does not hold yet. Ordinary movement does not need this - the sweep covers it - but an engine-side teleport or a raw SetUnitX call does, because neither is observable. A unit UnitIndexer has not indexed is not in the index; identity is assigned only by the entry lifecycle.

public function onSpatialUnitMoved(unit _u, vec2 _current)

Called on the first periodic Lua sweep sample and whenever a later sample changes position. Queries and explicit index updates do not consume this notification. The index has recorded the new position. Implementations must not mutate unit position or lifetime.

Configurable. Override it in your map’s config package.

public function spatialIndexHealthy() returns boolean

Whether the Lua spatial index is active and can serve indexed queries.

public function spatialIndexWorstStaleness() returns real

Guaranteed upper staleness bound in seconds used to derive query padding.

public function spatialIndexObservedStaleness() returns real

Observed staleness in seconds; diagnostic only, never safe for padding.

public function spatialIndexTrackedUnits() returns int

How many units the sweep currently cycles over.

public function spatialIndexAllocatedSlots() returns int

Highest row a unit has taken, which is what sizes the arrays here, since they are indexed by it. The rows are UnitIndexer indices, so their reuse is UnitIndexer’s: this stays near the peak concurrent population.

public function spatialIndexPadCells() returns int

Current query padding in whole cells, derived from guaranteed staleness.

public function rebuildSpatialIndexGrid(vec2 worldMin, vec2 worldMax)

Rebuilds the extent and re-buckets all units. Unguarded so grid math remains testable on Jass.

The grid is shared, so this re-derives it for every partition consumer, not only for units: there is one extent, and an entry linked under the old cell indices would be invisible to the new grid. That is why the rebuild re-links everything rather than only this layer’s entries.

public function spatialIndexCellOf(vec2 pos) returns int

Internal-grid cell for tests and diagnostics.

public function spatialIndexGridWidth() returns int

Grid dimensions in cells, for tests and diagnostics.

public function spatialIndexGridHeight() returns int
public function unit.isSpatialQueryCandidate() returns boolean

Consumer visibility policy; identity and spatial registration remain independent.

Configurable. Override it in your map’s config package.

public function spatialIndexBeginQuery(vec2 center, real radius, boolean collisionSizeFiltering) returns int

Origin-distance query, or collision-radius distance when collisionSizeFiltering is true. Collision is cached lazily for the tick; call updateSpatialIndex after changing it within a tick. Must be paired with spatialIndexEndQuery().

public function spatialIndexBeginBoxQuery(vec2 boxMin, vec2 boxMax) returns int

Collects units inside the box into the query snapshot.

public function spatialIndexBeginPlayerQuery(player owner) returns int

Collects currently indexed units owned by owner without a native group scan.

Player enumeration intentionally does not apply the range/box visibility filter: Warcraft’s GroupEnumUnitsOfPlayer includes hidden and Locust units, so this query does too.

public function spatialIndexQueryUnit(int i) returns unit

Returns the i-th match of the innermost open query.

public function spatialIndexEndQuery()

Closes the innermost open query and releases its snapshot.