Game.ResourceAvailabilitySystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase, IDefaultSerializable, ISerializable
Summary:
ResourceAvailabilitySystem is a DOTS/ECS system responsible for discovering and computing availability of various "resources" (workplaces, services, different supply types, public transport availability such as taxis/buses/tram/subway, attractiveness, outside connections, etc.) and writing those results into ResourceAvailability buffers on edge entities. It schedules many Burst-compiled IJobChunk / IJobParallelFor jobs and uses a PathfindTargetSeeker to produce path targets for providers. It runs on a fixed update interval (64 frames) and contains special handling for taxi availability (district filtering, per-sub-lane parking updates and enqueuing TimeActions). The system serializes which resource index was last written and exposes an appliedResource property indicating which resource is currently applied.
Fields
-
private const uint UPDATE_INTERVAL = 64u
Description: Frame interval (64) used by the system — the system's GetUpdateInterval returns this value. Controls how frequently the system iterates through resource types. -
private EntityQuery m_EdgeGroup
Description: Query matching edge entities that hold ResourceAvailability buffers. Used when clearing and writing availability values to edges. -
private EntityQuery m_WorkplaceGroup
Description: Query for entities that provide work (WorkProvider). Used to find workplace providers. -
private EntityQuery m_ServiceGroup
Description: Query for entities with ServiceAvailable to compute service availability. -
private EntityQuery m_RenterGroup
Description: Query for residential properties with renters (Renter buffers) to compute consumer availability (educated/uneducated citizens). -
private EntityQuery m_ConvenienceFoodStoreGroup
Description: Query for convenience-food-capable stores (ServiceAvailable + ResourceSeller). -
private EntityQuery m_OutsideConnectionGroup
Description: Query for outside connection providers (excluding specific outside connection types like electricity/water). -
private EntityQuery m_AttractionGroup
Description: Query for attractiveness providers (AttractivenessProvider). -
private EntityQuery m_ResourceSellerGroup
Description: Query used to locate resource sellers and storage companies for various supply types. -
private EntityQuery m_TaxiQuery
Description: Query matching taxi dispatch entities (transport depots or taxi vehicles with dispatch) used when computing taxi availability. -
private EntityQuery m_BusStopQuery
Description: Query for bus stops when computing bus availability. -
private EntityQuery m_TramSubwayQuery
Description: Query for tram or subway stops when computing tram/subway availability. -
private EntityQuery m_ParkingLaneQuery
Description: Query for parking lanes to update taxi availability flags and enqueue TimeActions for lane enabling/disabling. -
private SimulationSystem m_SimulationSystem
Description: Cached reference to the SimulationSystem for frame index and coordination. -
private PathfindQueueSystem m_PathfindQueueSystem
Description: Reference to the PathfindQueueSystem used to enqueue pathfinding work produced by this system. -
private AirwaySystem m_AirwaySystem
Description: Cached reference used to supply airway data to PathfindTargetSeekerData. -
private ResourceSystem m_ResourceSystem
Description: Cached reference to ResourceSystem to fetch resource prefabs/data when computing availability parameters for supply resources. -
private PathfindTargetSeekerData m_TargetSeekerData
Description: Helper data used to build PathfindTargetSeeker instances across jobs. -
private Entity m_AvailabilityContainer
Description: A single entity created to hold AvailabilityElement buffers produced by the PathfindTargetSeeker; used to pass results between jobs. -
private AvailableResource m_LastQueriedResource
Description: Tracks which resource index was last queried/fetched by the system (rotates through available resource types). -
private AvailableResource m_LastWrittenResource
Description: Tracks which resource index was last written (applied) to edges; serialized to persist which availability was last stored. -
private TypeHandle __TypeHandle
Description: Internal struct used to cache ComponentTypeHandle/BufferTypeHandle/ComponentLookup/BufferLookup used by jobs. Populated on creation.
(Internal job structs such as FindWorkplaceLocationsJob, FindServiceLocationsJob, FindConsumerLocationsJob, FindSellerLocationsJob, FindTaxiLocationsJob, FindBusStopLocationsJob, FindTramSubwayLocationsJob, ClearAvailabilityJob, ApplyAvailabilityJob, FindTaxiDistrictsJob, ApplyTaxiAvailabilityJob, RefreshTaxiAvailabilityJob and other nested types are defined inside the class; they are Burst-compiled jobs used to sample entities, gather providers, and write availability results.)
Properties
public AvailableResource appliedResource { get; private set; }
Description: Indicates which AvailableResource index was applied (written) during the last ApplyAvailability step. When the system cycles resources each update this is set to the last written resource or AvailableResource.Count when none has been written or when rotating. Can be used by other code to discover what availability is currently effective on edges.
Constructors
public ResourceAvailabilitySystem()
Description: Default constructor. The real initialization happens in OnCreate. The constructor is preserved for DOTS/serialization patterns.
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Description: Returns the system update interval in frames. This system uses 64 frames to rotate resource processing. -
[Preserve] protected override void OnCreate()
Description: Initializes queries, caches system references (SimulationSystem, PathfindQueueSystem, AirwaySystem, ResourceSystem), creates the m_AvailabilityContainer entity, and prepares PathfindTargetSeekerData and other internal state. This is where the various EntityQuery definitions are created. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Description: Serializes minimal state (m_LastWrittenResource) so the last-applied resource can be restored across saves. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Description: Deserializes state and clamps values into the valid AvailableResource range. Resets appliedResource/last-queried resource state appropriately. -
public void SetDefaults(Context context)
Description: Sets default values used for new contexts; sets m_LastWrittenResource to a sensible default (FishSupply in the implementation) and resets tracking fields. -
[Preserve] protected override void OnUpdate()
Description: Main update routine called by the ECS scheduler. It rotates m_LastQueriedResource and m_LastWrittenResource each frame, creates an AvailabilityAction (which holds the producer and consumer queues and parameters), schedules the FindLocations jobs for the current resource, schedules ApplyAvailability for the previously selected resource (if any), and enqueues the pathfinding action into the PathfindQueueSystem. This method coordinates job dependencies between pathfinding and availability application. Special handling for availability rotation and persistence is implemented here. -
private AvailabilityParameters GetAvailabilityParameters(AvailableResource resource, ResourcePrefabs prefabs, ComponentLookup<ResourceData> datas)
Description: Returns resource-specific weighting factors (density weight, cost factor, result factor) used by pathfinding/provider weighting logic. The system handles many supply resource cases and common resource categories (workplaces, citizens, services, taxis, etc.) and returns tuned parameters per resource type. -
private JobHandle FindLocations(AvailableResource resource, UnsafeQueue<PathTarget> pathTargets, UnsafeQueue<AvailabilityProvider> providers, JobHandle inputDeps)
Description: Schedules the appropriate Burst IJobChunk (or returns input deps) for the given available resource type. The job populates providers and path targets using PathfindTargetSeeker. Handles many resource types: workplaces, services, consumers (educated/uneducated), convenience food stores, outside connections, a broad set of supply resources, attractiveness providers, taxi/bus/tram-subway providers. The method prepares PathfindParameters and SetupQueueTarget used by PathfindTargetSeeker. -
private JobHandle ApplyAvailability(AvailableResource resource, JobHandle inputDeps, JobHandle pathDeps)
Description: Takes the availability results produced into the AvailabilityElement buffer (m_AvailabilityContainer) and writes them into ResourceAvailability buffers on edges. Clears the resource slot first (ClearAvailabilityJob) then either runs standard ApplyAvailabilityJob or special taxi flow (FindTaxiDistricts, ApplyTaxiAvailability, RefreshTaxiAvailabilityJob plus enqueue TimeAction). Returns a JobHandle representing application completion. -
private static void AddProvider(Entity provider, float capacity, UnsafeQueue<AvailabilityProvider>.ParallelWriter providers, ref PathfindTargetSeeker<PathfindTargetBuffer> targetSeeker, float cost)
Description: Helper used by jobs to add a provider with explicit cost if PathfindTargetSeeker finds any path targets for the provider. Enqueues AvailabilityProvider when targets exist. -
private static void AddProvider(Entity provider, float capacity, UnsafeQueue<AvailabilityProvider>.ParallelWriter providers, ref PathfindTargetSeeker<PathfindTargetBuffer> targetSeeker)
Description: Overload that calls AddProvider with cost 0. Used for providers that don't have a provider-specific cost. -
private void __AssignQueries(ref SystemState state)
Description: Compiler/IL2CPP helper used by generated code paths to assign queries. Internal; part of the OnCreateForCompiler pattern. -
protected override void OnCreateForCompiler()
Description: Internal compiler helper: calls __AssignQueries and populates __TypeHandle handles. Used by DOTS code generation / AOT code paths.
Usage Example
// Example: read which resource was last applied by the system in another system
protected override void OnUpdate()
{
var resSystem = World.GetExistingSystemManaged<Game.Simulation.ResourceAvailabilitySystem>();
if (resSystem != null)
{
AvailableResource applied = resSystem.appliedResource;
// Use 'applied' to show a debug overlay or to trigger logic depending on last-applied availability
}
}
Notes and modding tips: - The system uses DOTS jobs and Burst. When modifying or extending, ensure thread-safety for Lookups/BufferLookups and use InternalCompilerInterface handles as in the original code. - Taxi availability has additional logic: district filtering, updating parking lane flags and enqueuing TimeActions for lanes. Extending taxi logic requires updating FindTaxiDistrictsJob, ApplyTaxiAvailabilityJob, and RefreshTaxiAvailabilityJob accordingly. - To add a new AvailableResource type, update GetAvailabilityParameters, FindLocations switch, and ensure serialization/clamping logic for m_LastWrittenResource is updated.