Game.Simulation.WorkWatercraftAISystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
WorkWatercraftAISystem is an ECS system that controls "work" watercraft (work vehicles that operate on water) in the simulation. It iterates over work watercraft entities, handles pathfinding setup, navigation lane handling, arrival/return behavior, and queues work actions (harvest/collect) that modify world state. The system uses two Burst-compatible jobs:
- WorkWatercraftTickJob (IJobChunk) — per-chunk tick logic: reading/writing vehicle state, lane/path handling, issuing pathfind requests, enqueuing work actions, and emitting EntityCommandBuffer commands.
- WorkWatercraftWorkJob (IJob) — applies queued work actions (e.g., turn trees into stumps, update extractor statistics).
The system depends on an EndFrameBarrier (for command buffer), and PathfindSetupSystem (for enqueuing pathfind setups). It runs periodically (GetUpdateInterval/Offset) and schedules the two jobs; the work job runs after the tick job and consumes a NativeQueue of WorkAction items produced by the tick job.
Fields
-
private EndFrameBarrier m_EndFrameBarrier
Used to create an EntityCommandBuffer (parallel writer). The command buffer is used by the tick job to add or modify components (Deleted, BatchesUpdated, etc.) safely from jobs and applied at the end of the frame. -
private PathfindSetupSystem m_PathfindSetupSystem
Reference to the pathfind setup system; used to get a pathfind queue writer so the job can enqueue pathfind setup requests for vehicles that need new paths. -
private EntityQuery m_VehicleQuery
EntityQuery used to select work watercraft entities (components include WorkVehicle, WatercraftCurrentLane, Owner, PrefabRef, PathOwner, Target, and excludes Deleted/Temp/TripSource/OutOfControl). The query is required for system updates. -
private TypeHandle __TypeHandle
Internal struct storing component/lookup handles used to build the job data (EntityTypeHandle, ComponentTypeHandles, BufferTypeHandles and ComponentLookup/BufferLookup instances). It is initialized in OnCreateForCompiler.
Properties
- (none publicly exposed on the system itself)
Constructors
public WorkWatercraftAISystem()
Default constructor. System is created by the world (OnCreate does the real initialization).
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval for the system. The compiled system returns 16 (frames) — this system updates less frequently to spread workload. -
public override int GetUpdateOffset(SystemUpdatePhase phase)
Returns the update offset for the system. The compiled system returns 8. Interval+offset control when across multiple frames the system runs. -
[Preserve] protected override void OnCreate()
Initializes the system: retrieves/creates EndFrameBarrier and PathfindSetupSystem from the world, sets up the EntityQuery (m_VehicleQuery) to select work watercraft and calls RequireForUpdate to avoid running when there are none. -
[Preserve] protected override void OnUpdate()
Main scheduling routine: - Allocates a NativeQueue
(temporary job allocator). - Constructs WorkWatercraftTickJob (IJobChunk) with all ComponentTypeHandles, ComponentLookup/BufferLookup, command buffer, and queue writers.
- Constructs WorkWatercraftWorkJob (IJob) that consumes the work queue and applies world changes (trees/extractors).
- Schedules the tick job in parallel (ScheduleParallel) and then schedules the work job after the tick job.
-
Disposes the NativeQueue after jobs complete and wires job handles into PathfindSetupSystem and EndFrameBarrier. This method is responsible for wiring job dependencies and ensuring command buffers and pathfind requests are produced safely.
-
protected override void OnCreateForCompiler()
Internal helper used by generated code paths to assign queries and initialize the __TypeHandle (component handles) for job construction. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Internal stub used by the compiler-generated plumbing; in this decompiled form it creates/cleans an EntityQueryBuilder. -
(Nested/Job-level notable methods — documented here because they define the system behavior)
-
WorkWatercraftTickJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Per-chunk execution: iterates entities in the chunk and calls Tick for each vehicle. It handles checking/unspawned state, writes back modified components and enqueues pathfind/work actions via provided writers. -
WorkWatercraftTickJob.Tick(int jobIndex, Entity vehicleEntity, Owner owner, PathInformation pathInformation, CurrentRoute currentRoute, PrefabRef prefabRef, DynamicBuffer
navigationLanes, DynamicBuffer path, ref Game.Vehicles.WorkVehicle workVehicle, ref Watercraft watercraft, ref WatercraftCurrentLane currentLane, ref PathOwner pathOwner, ref Target target)
Core per-vehicle logic:- Resets or continues paths, returns to depot if needed.
- Handles path end events (work stops vs route waypoints).
- Marks watercraft as Working and handles Arriving state.
- Checks and manipulates navigation lanes (CheckNavigationLanes).
- Requests a new path when required (FindPathIfNeeded).
- Enqueues work actions for extraction/collection and updates vehicle quantities.
-
WorkWatercraftTickJob.IsWorkStop(CurrentRoute currentRoute, ref Target target, out bool workLocation) : bool
Determines whether the current target is a work stop (like a work stop prefab) and returns whether it is a work location (for special route behavior). -
WorkWatercraftTickJob.CheckNavigationLanes(Owner owner, CurrentRoute currentRoute, DynamicBuffer
navigationLanes, ref Game.Vehicles.WorkVehicle workVehicle, ref WatercraftCurrentLane currentLane, ref PathOwner pathOwner, ref Target target, out Entity skipWaypoint)
Handles end-of-path conditions for navigation lanes including waypoints and boarding vehicle checks, possibly setting skipWaypoint or adding/removing navigation lane entries and adjusting arriving flags. -
WorkWatercraftTickJob.FindPathIfNeeded(Entity vehicleEntity, Owner owner, CurrentRoute currentRoute, PrefabRef prefabRef, Entity skipWaypoint, ref Game.Vehicles.WorkVehicle workVehicle, ref Watercraft watercraft, ref WatercraftCurrentLane currentLane, ref PathOwner pathOwner, ref Target target)
Prepares PathfindParameters and SetupQueueTargets for origin/destination, considering work vehicle flags (Returning, RouteSource, WorkLocation, ExtractorVehicle, StorageVehicle), and enqueues a pathfind setup via PathfindSetupSystem queue writer. Uses the watercraft prefab data (max speed, path methods), work vehicle prefab data (work type, map feature), and owner information. -
WorkWatercraftTickJob.TrySetWaypointTarget(Owner owner, CurrentRoute currentRoute, ref Game.Vehicles.WorkVehicle workVehicle, ref PathOwner pathOwner, ref Target target) : bool
Tries to set the next waypoint in the active route as the vehicle target (restricting to same owner). Returns true if a waypoint target was set. -
WorkWatercraftTickJob.SetNextWaypointTarget(Owner owner, CurrentRoute currentRoute, ref Game.Vehicles.WorkVehicle workVehicle, ref PathOwner pathOwner, ref Target target)
Selects the next waypoint (wraps around the route) and decides whether to target the owner's area (work location) or just the waypoint, setting WorkVehicleFlags accordingly. -
WorkWatercraftTickJob.HasNavigation(Entity entity, RoadTypes roadTypes) : bool
Checks whether an entity or any of its sub-areas has navigation for specified road types (used to decide if route work locations are reachable by watercraft). -
WorkWatercraftTickJob.ResetPath(...) : bool
Resets path data and navigation flags for a vehicle; updates WatercraftFlags.StayOnWaterway depending on whether vehicle is returning or continuing to pathInformation.m_Destination. -
WorkWatercraftTickJob.ReturnToDepot(int jobIndex, Entity vehicleEntity, Owner ownerData, ref Game.Vehicles.WorkVehicle workVehicle, ref Watercraft watercraft, ref PathOwner pathOwner, ref Target target)
Marks the vehicle as Returning, clears WorkLocation flag, and sets the owner's depot (or attached owner) as the new target. -
WorkWatercraftTickJob.ShouldStartWork(ref Game.Vehicles.WorkVehicle workVehicle) : bool
Adjusts the vehicle's work/done amounts and returns whether it should start doing more work (i.e., done amount is less than work amount). -
WorkWatercraftTickJob.PerformWork(int jobIndex, Entity vehicleEntity, Owner owner, PrefabRef prefabRef, ref Game.Vehicles.WorkVehicle workVehicle, ref Target target, ref PathOwner pathOwner) : bool
Performs a unit of work appropriate for the vehicle type (Extractor, Storage, etc.):- For ExtractorVehicle.Harvest: calculates wood amount (object utils and prefab tree data), enqueues a WorkAction with m_WorkAmount, and marks tree as updated (BatchesUpdated).
- For Collect: enqueues WorkAction to mark tree as collected. Updates vehicle quantities (QuantityUpdated) and returns true if the job's done amount has completed the work amount (so vehicle can move on).
-
WorkWatercraftTickJob.QuantityUpdated(int jobIndex, Entity vehicleEntity)
Notifies sub-objects (visual payloads) that quantities changed by adding BatchesUpdated components to sub-objects. -
WorkWatercraftWorkJob.Execute()
Consumes the queued WorkAction items and applies them to world data:- Harvest: convert Tree to stump, reset growth, update Extractor stats (m_ExtractedAmount, m_HarvestedAmount).
- Collect: mark Tree as Collected. This job runs after the tick job and writes to component lookups (Tree, Extractor) directly (not via command buffer), which is why it runs after the parallel tick job finished producing actions.
-
(Job plumbing) WorkWatercraftTickJob implements IJobChunk.Execute and the file also explicitly maps IJobChunk.Execute to the method; WorkWatercraftWorkJob implements IJob.Execute.
Usage Example
// Example: obtain the system and (for inspection/debug) read its query or force a one-time update.
// Note: systems are normally managed by the ECS world and run automatically — you usually don't call OnUpdate manually.
using Unity.Entities;
using Game.Simulation;
var world = World.DefaultGameObjectInjectionWorld;
var workWatercraftSystem = world.GetExistingSystemManaged<WorkWatercraftAISystem>();
if (workWatercraftSystem != null)
{
// The system itself does not expose public API to trigger specific behavior,
// but you can inspect state or access other systems it uses (EndFrameBarrier, PathfindSetupSystem).
// For example, you can add components that match its EntityQuery to cause it to run on its schedule.
}
Notes and tips for modders: - If you need to alter how watercraft pathfinding or work behavior behaves, consider: - Modifying WorkWatercraftTickJob logic (Tick / FindPathIfNeeded / PerformWork / ReturnToDepot) via Harmony or a replacement system. - Intercepting or augmenting the PathfindSetupSystem queue to change path parameters. - Hooking the WorkAction queue (or WorkWatercraftWorkJob) to alter how harvest/collect actions affect world state (e.g., custom resource types). - The system uses Burst and ComponentLookup/BufferLookup patterns. When changing component access patterns, ensure proper read/write attributes and job dependency handling (command buffers for structural changes). - The system runs infrequently (interval 16, offset 8) — changes to vehicle behavior may be batched across frames.