Skip to content

Game.Simulation.AvailabilityInfoToGridSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: CellMapSystem, IJobSerializable

Summary:
AvailabilityInfoToGridSystem collects ResourceAvailability data from road/edge entities and rasterizes that information into a fixed-size cell grid (texture) used by other systems to query local availability values (attractiveness, consumers, services, workplaces). It builds a weighted average per cell by sampling availability buffers on nearby edge geometry (start/end curb segments), using a NetSearchTree for fast spatial queries and a Burst-compiled IJobParallelFor to fill the grid in parallel. The final data for each cell is stored in an AvailabilityInfoCell (m_AvailabilityInfo float4) in the inherited map (m_Map). The system updates periodically (kUpdatesPerDay) and exposes helper functions to sample/interpolate the map at arbitrary world positions.


Fields

  • public static readonly int kTextureSize
    Defines the resolution of the square availability grid/texture. Value: 128. The grid is kTextureSize × kTextureSize cells and is used when computing cell centers, indexing and interpolation.

  • public static readonly int kUpdatesPerDay
    Defines how many times per in-game day the grid should be updated. Value: 32. Used by GetUpdateInterval to determine scheduling frequency.

  • private SearchSystem m_NetSearchSystem
    Reference to the game's SearchSystem used to obtain a NativeQuadTree of network entities (edges) for spatial queries. The system requests a read-only net search tree and registers itself as a reader when scheduling jobs.

  • private TypeHandle __TypeHandle
    Container of component/buffer lookup handles (BufferLookup and ComponentLookup) assigned in OnCreateForCompiler. Internal use for safely obtaining buffer/component access inside jobs via InternalCompilerInterface.GetBufferLookup / GetComponentLookup.

Properties

  • public int2 TextureSize { get; }
    Returns the grid resolution as an int2: (kTextureSize, kTextureSize). Useful when creating or sampling textures or for code that needs to know the size of the AvailabilityInfo map.

Constructors

  • public AvailabilityInfoToGridSystem()
    Default constructor. The system uses [Preserve] attributed lifecycle methods (OnCreate/OnUpdate) to set up textures and get references to SearchSystem. No custom constructor initialization beyond the default.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns scheduling interval for the system in simulation ticks. Implementation returns 262144 / kUpdatesPerDay. This controls how often OnUpdate runs relative to the engine update tick rate.

  • public static float3 GetCellCenter(int index)
    Helper to compute the world-space center of the cell at a flattened index. Uses the base CellMapSystem.GetCellCenter helper with kTextureSize.

  • public static AvailabilityInfoCell GetAvailabilityInfo(float3 position, NativeArray<AvailabilityInfoCell> AvailabilityInfoMap)
    Samples the availability grid at an arbitrary world position and returns an interpolated AvailabilityInfoCell. The method:

  • Converts world position to a cell (int2) and fractional cell coordinates.
  • Performs bilinear interpolation between up to 4 neighboring cells' m_AvailabilityInfo (float4).
  • Returns default(AvailabilityInfoCell) if the position is outside the grid. This is the recommended way for other systems/mods to query local availability values from the raw NativeArray grid.

  • [Preserve] protected override void OnCreate()
    Initializes the textures (CreateTextures(kTextureSize)) and acquires the SearchSystem reference via World.GetOrCreateSystemManaged(). Also called during domain reloads and system creation. Ensures data structures needed in OnUpdate are available.

  • [Preserve] protected override void OnUpdate()
    Main scheduling point. It:

  • Gets a read-only NativeQuadTree from m_NetSearchSystem (also obtains native dependency JobHandle).
  • Prepares an AvailabilityInfoToGridJob with:
    • m_AvailabilityInfoMap (m_Map from the base CellMapSystem),
    • m_NetSearchTree,
    • buffer/component lookups (ResourceAvailability buffer and EdgeGeometry component) via the compiled TypeHandle helpers,
    • computed cell size (kMapSize / kTextureSize).
  • Schedules a Burst-compiled IJobParallelFor across all grid cells (kTextureSize*kTextureSize) with suitable batching.
  • Registers the job dependency with writers/readers and the net search system (AddNetSearchTreeReader). The job computes per-cell weighted averages by iterating nearby edges through the net search tree.

  • protected override void OnCreateForCompiler()
    Internal/compiler helper invoked during system creation to assign queries and component/buffer lookups. It calls __AssignQueries and __TypeHandle.__AssignHandles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal query assignment stub. Leaves an empty EntityQueryBuilder as implemented here (likely a placeholder required by codegen).

Nested types (important for modders to know):

  • NetIterator (private struct)
    Implements INativeQuadTreeIterator and IUnsafeQuadTreeIterator used by NativeQuadTree.Iterate. For each visited edge entity, NetIterator:
  • Checks intersection between cell bounds and the iterator bounds,
  • Reads ResourceAvailability dynamic buffer on the entity and EdgeGeometry component,
  • Samples a number of points along the start and end curb curves (count proportional to middleLength * 0.05),
  • For each sample computes a weight based on distance to the cell center and m_CellSize,
  • Adds weighted contributions for attractiveness, consumers (combining uneducated + educated halves), services and workplaces into a running AvailabilityInfoCell result and a total weight cell. NetIterator fields include m_TotalWeight, m_Result, m_CellSize, m_Bounds, BufferLookup and ComponentLookup.

  • AvailabilityInfoToGridJob (private struct, [BurstCompile])
    IJobParallelFor that runs per cell index:

  • Creates a NetIterator with bounds centered at the cell center (radius 1.5 * m_CellSize in XZ and a very large Y extent),
  • Iterates the net search tree to accumulate availability into the iterator,
  • Writes the normalized result into m_AvailabilityInfo for the cell (division guarded by zero-weight check). This job uses Burst and runs in parallel; it reads buffers/components via BufferLookup/ComponentLookup.

  • TypeHandle (private struct)
    Holds readonly handles for BufferLookup and ComponentLookup. Has a method __AssignHandles(ref SystemState) which obtains the lookups from the SystemState. This is necessary to get access inside scheduled jobs in a safe way.

Usage Example

// Example: sample availability at a world position from within another system/job.
// `availabilityMap` is the NativeArray<AvailabilityInfoCell> belonging to this system (m_Map).
float3 worldPos = new float3(1000f, 0f, 2000f);
AvailabilityInfoCell info = AvailabilityInfoToGridSystem.GetAvailabilityInfo(worldPos, availabilityMap);
float4 availabilityValues = info.m_AvailabilityInfo; // x=attractiveness, y=consumers, z=services, w=workplaces (mapping depends on AvailabilityInfoCell)

Additional notes for modders: - The system relies on ResourceAvailability buffers being present on net entities and EdgeGeometry components being available. If your custom network/entities do not populate these, they will not contribute to the grid. - The grid resolution is fixed by kTextureSize (128). Lowering/higher resolutions would require changing kTextureSize and ensuring consumers of the grid respect the new size. - The job is Burst-compiled and scheduled in parallel (IJobParallelFor). Ensure any code interacting with internal NativeArrays observes thread-safety (read/write dependencies) and uses job dependencies correctly. - Use GetAvailabilityInfo for interpolation, do not directly index m_Map unless you know the intended cell indexing and bounds. - The system adds itself as a reader to the NetSearchSystem; other systems depending on net search data should coordinate via job dependencies (AddNetSearchTreeReader / AddNetSearchTreeWriter) to avoid race conditions.