Skip to content

Game.Tools.BulldozeToolSystem

Assembly: Game
Namespace: Game.Tools

Type: class

Base: ToolBaseSystem

Summary:
BulldozeToolSystem implements the Bulldoze tool logic for Cities: Skylines 2. It handles raycasting, control-point selection, snapping, path creation for multi-edge bulldozes, and the creation of deletion "definitions" (CreationDefinition) that are later consumed to actually remove entities (edges, nodes, objects, areas, attachments, upgrades, etc.). The system supports editor vs game modes, three tool modes (MainElements, SubElements, Everything), underground toggling, multi-selection, confirmation flow when buildings would be destroyed, and schedules two main jobs (SnapJob and CreateDefinitionsJob) to prepare definitions on worker threads while using an EntityCommandBuffer to push creation requests to the simulation.


Fields

  • public const string kToolID
    Constant identifier for the tool: "Bulldoze Tool".

  • public Action EventConfirmationRequested
    Event fired when bulldoze requires player confirmation (used by UI). Raised before final apply when user may destroy buildings.

  • private ToolOutputBarrier m_ToolOutputBarrier
    Barrier used to produce creation/deletion definition entities (CreationDefinition) safely from jobs.

  • private AudioManager m_AudioManager
    Reference to the audio manager used to play bulldoze sounds.

  • private AchievementTriggerSystem m_AchievementTriggerSystem
    Reference used to report progress for achievements (e.g., squasher progression).

  • private EntityQuery m_DefinitionQuery
    EntityQuery used to find existing creation definitions to destroy/update when tool state changes.

  • private EntityQuery m_BuildingQuery
    EntityQuery to detect temporary building definition entities (used to decide if confirmation is needed).

  • private EntityQuery m_RoadQuery
    EntityQuery for temporary road (edge) definitions.

  • private EntityQuery m_PlantQuery
    EntityQuery for plant/object definitions.

  • private EntityQuery m_SoundQuery
    Query to access tool UX sound settings (used to play different bulldoze sounds).

  • private ControlPoint m_LastRaycastPoint
    Stores the previous raycast control point to detect changes and avoid redundant updates.

  • private State m_State
    Internal finite-state for the tool: Default, Applying, Waiting (confirmation), Confirmed, Cancelled.

  • private NativeList<ControlPoint> m_ControlPoints
    Persistent NativeList holding the current selection/control points used to produce definitions.

  • private IProxyAction m_Bulldoze
    Input action proxy for the bulldoze/apply action.

  • private IProxyAction m_BulldozeDiscard
    Input action proxy for the discard/cancel action during multi-selection.

  • private bool m_ApplyBlocked
    Temporary flag to block apply when a discard/cancel has just occurred.

  • private TypeHandle __TypeHandle
    Holds ComponentLookup/BufferLookup handles used by burst jobs (assigned in OnCreateForCompiler).

(These are the main fields — the class contains additional nested types and helper data used by the jobs and query/lookup assignment.)

Properties

  • public override string toolID { get }
    Returns the tool ID string ("Bulldoze Tool"). Used by UI/tool management.

  • public override int uiModeIndex { get }
    Returns the current UI mode index. It maps from actualMode to int (used for UI mode selection).

  • public override bool allowUnderground { get }
    Tool indicates it allows underground selection (true).

  • public Mode mode { get; set; }
    User-selected mode (MainElements, SubElements, Everything). In editor mode more options are available.

  • public Mode actualMode { get; }
    The effective mode used by the tool. If not in editor actionMode, this returns MainElements regardless of the user setting.

  • public bool underground { get; set; }
    Whether the tool is currently targeting underground objects.

  • public bool allowManipulation { get; set; }
    If set, raycast includes moving objects for manipulation.

  • public bool debugBypassBulldozeConfirmation { get; set; }
    Debug flag to skip confirmation checks (useful for dev/testing).

  • public BulldozePrefab prefab { get; set; }
    Associated prefab object for the tool (BulldozePrefab). GetPrefab/TrySetPrefab work with it.

  • private protected override IEnumerable<IProxyAction> toolActions { get; }
    Sequence of actions the tool exposes (Bulldoze and BulldozeDiscard).

Constructors

  • public BulldozeToolSystem()
    Standard public constructor. Most initialization happens in OnCreate/OnStartRunning rather than the ctor.

Methods

  • [Preserve] protected override void OnCreate()
    Initializes references and state: caches ToolOutputBarrier, AudioManager, AchievementTriggerSystem, creates the persistent m_ControlPoints list, sets up EntityQueries, and retrieves input actions for bulldoze/discard. This is the main startup logic for the tool system.

  • [Preserve] protected override void OnDestroy()
    Disposes native containers (m_ControlPoints) and calls base.OnDestroy.

  • [Preserve] protected override void OnStartRunning()
    Called when the system starts running: clears control points, resets last raycast point, sets initial state to Default, resets apply-blocked flag and base required flags (areas/net/stop icons).

  • public override void GetUIModes(List<ToolMode> modes)
    Populates the UI mode list. Always adds MainElements; in editor action mode also adds SubElements and Everything.

  • private protected override void UpdateActions()
    Updates action proxies and their enabled states depending on current tool state and selection (e.g., apply and cancel enabling).

  • private bool IsMultiSelection()
    Helper that returns whether the current control points constitute a multi-selection. Roads/nodes require more points for multi selection; general objects require >1.

  • public override PrefabBase GetPrefab()
    Returns the assigned BulldozePrefab.

  • public override bool TrySetPrefab(PrefabBase prefab)
    Attempts to set the tool's prefab; accepts BulldozePrefab instances.

  • public override void SetUnderground(bool underground)
    Set underground flag.

  • public override void ElevationUp() / ElevationDown() / ElevationScroll()
    Convenience methods to change underground toggle.

  • public override void InitializeRaycast()
    Configures the tool raycast system based on current mode, underground flag, actionMode (editor vs game) and allowManipulation. Sets typeMask, netLayerMask, collisionMask, raycastFlags and areaTypeMask appropriately.

  • [Preserve] protected override JobHandle OnUpdate(JobHandle inputDeps)
    Main update method of the system: manages required data flags, updates actions, and runs a small state machine to switch between Default, Applying, Waiting, Confirmed, Cancelled states. Handles transition to Apply() and Update() and triggers confirmation event when needed.

  • private bool ConfirmationNeeded()
    Checks whether any temporary building definitions scheduled for deletion require player confirmation (non-spawnable or signature buildings). Iterates building query results to decide.

  • public void ConfirmAction(bool confirm)
    Callback used by UI to provide player confirmation (true accept, false cancel) when in Waiting state.

  • private JobHandle Apply(JobHandle inputDeps)
    Performs the apply action if allowed (GetAllowApply()). Plays sound(s) depending on what will be deleted, reports achievement progress, sets applyMode to Apply, clears control points and destroys/consumes created definitions via DestroyDefinitions. If apply is not allowed it trims multi-selection and returns to update.

  • protected override bool GetRaycastResult(out ControlPoint controlPoint)
    Raycast wrapper that converts Entity + RaycastHit to a ControlPoint. Handles service-upgrade owner redirection and selecting edge when node+edge hit combo occurs.

  • protected override bool GetRaycastResult(out ControlPoint controlPoint, out bool forceUpdate)
    Overload with forceUpdate return. Similar behavior.

  • private JobHandle Update(JobHandle inputDeps, bool fullUpdate)
    Handles per-frame raycast results, managing and updating m_ControlPoints, calling SnapControlPoints and UpdateDefinitions as needed, or clearing definitions when nothing hit.

  • private JobHandle SnapControlPoints(JobHandle inputDeps)
    Schedules SnapJob (Burst compiled) to snap control points, resolve owners, pathfinding for edges/nodes and sanitize control point list. Uses the TypeHandle component/buffer lookups for read-only access from the job.

  • private JobHandle UpdateDefinitions(JobHandle inputDeps)
    First destroys existing definitions, then schedules CreateDefinitionsJob (Burst compiled) which computes which entities should be bulldozed and writes CreationDefinition entities into the tool output barrier's command buffer. Adds job handle to the barrier and combines dependencies.

  • private JobHandle DestroyDefinitions(EntityQuery defQuery, ToolOutputBarrier barrier, JobHandle deps)
    (Provided by base/utility; used to destruct previously created CreationDefinition entities. Not shown in class body but used.)

  • private JobHandle SnapJob.Execute() (nested Burst job)
    SnapJob logic: resolves outside connections, owner chains for static/upgrade service cases, handles special case path creation when start/end points are edges/nodes, and otherwise enforces same-owner constraints or removes invalid last control point. Also contains CreatePath and AddControlPoints helpers used for path construction between network nodes/edges.

  • private JobHandle CreateDefinitionsJob.Execute() (nested Burst job)
    CreateDefinitionsJob logic: given snapped control points it builds the set of entities to bulldoze (edges, nodes, objects, area subareas, attached/installed upgrades), follows owner chains when needed, collects attached parents/fixed edges/subnets, and writes CreationDefinition entities using the provided EntityCommandBuffer. It also marks CreationFlags (Delete, Upgrade, Parent, Align, Hidden, Attach) to convey additional behavior to the consumer.

  • protected override void OnCreateForCompiler()
    Assigns query and typehandle lookups used by job scheduling (compiler helper).

(There are numerous small helpers inside the nested jobs: CreatePath, AddControlPoints, Execute (per-entity), AddEdge, AddFixedEdges, AddEntity, AddAttachedParent, AddSubNets, HasEdgeStartOrEnd, etc. These implement the detailed logic for gathering dependent entities to bulldoze and for generating NetCourse / ObjectDefinition components on CreationDefinition entities.)

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Typical initialization done by the system:
    m_ToolOutputBarrier = base.World.GetOrCreateSystemManaged<ToolOutputBarrier>();
    m_AudioManager = base.World.GetOrCreateSystemManaged<AudioManager>();
    m_AchievementTriggerSystem = base.World.GetOrCreateSystemManaged<AchievementTriggerSystem>();
    m_ControlPoints = new NativeList<ControlPoint>(4, Allocator.Persistent);
    m_DefinitionQuery = GetDefinitionQuery();
    m_BuildingQuery = GetEntityQuery(ComponentType.ReadOnly<Building>(), ComponentType.ReadOnly<Temp>(), ComponentType.Exclude<Deleted>());
    m_RoadQuery = GetEntityQuery(ComponentType.ReadOnly<Edge>(), ComponentType.ReadOnly<Temp>(), ComponentType.Exclude<Deleted>());
    m_PlantQuery = GetEntityQuery(ComponentType.ReadOnly<Plant>(), ComponentType.ReadOnly<Temp>(), ComponentType.Exclude<Deleted>());
    m_SoundQuery = GetEntityQuery(ComponentType.ReadOnly<ToolUXSoundSettingsData>());
    m_Bulldoze = InputManager.instance.toolActionCollection.GetActionState("Bulldoze", "BulldozeToolSystem");
    m_BulldozeDiscard = InputManager.instance.toolActionCollection.GetActionState("Bulldoze Discard", "BulldozeToolSystem");
}

Notes and tips for modders: - The heavy lifting is performed by two Burst jobs (SnapJob and CreateDefinitionsJob). To influence bulldoze behavior, you can hook or replace CreationDefinition consumer systems that actually perform deletes/recreates, or listen to EventConfirmationRequested to display custom UI. - The tool respects owner chains, fixed edges, attached parents, installed upgrades, and area sub-objects — be cautious when manipulating CreationDefinition consumers to maintain consistency. - Use ConfirmAction(true/false) when integrating with custom confirmation UI to resume or cancel bulldoze apply.