RimWorld Under the Hood: ThinkTree AI, Tick Scheduling & Region Pathfinding

RimWorld Under the Hood: ThinkTree AI, Tick Scheduling & Region Pathfinding

Engine & Tech Stack

  • Engine / language: Unity with C# on the Mono scripting backend; most 2D rendering is custom, batching meshes per map layer.
  • Map representation: a flat 2D grid: every cell is an IntVec3 flattened to a 1D array index (CellIndices), with parallel layer arrays for terrain, things (ThingGrid), pathability (PathGrid), fog, snow/filth and per-room temperature.
  • Content & modding: everything is data-defined in XML Defs loaded at startup; runtime behavior is patched via Harmony.

Core AI & Decision Making

  • Architecture: pawn behavior runs on a hierarchical ThinkTree: ordered ThinkNodes forming a behavior-tree / utility hybrid, not a flat state machine.
  • Dispatch: each decision walks the tree top-down: high-priority nodes (fire, threats, urgent needs, mental states) short-circuit first; otherwise it descends into work via JobGivers filtered and sorted by the player's per-pawn work-priority matrix. The winning JobGiver emits a Job (with TargetA/B/C) that a JobDriver executes as a sequence of Toils.

State Management & Data Architecture

  • Model: object-oriented component trees, not strict ECS. The world is a hierarchy of managers: Game → World / Map → per-map managers (ThingGrid, RegionGrid, PathGrid, temperature cache). Entities are Things extended by attachable ThingComps and Hediffs.
  • Persistence: a reflection-based serializer, Scribe, writes XML and resolves cross-references on load.

Tick Scheduling & Performance Tricks

  • Staggered ticks: a central TickManager spreads work across three lists: Normal (~60/s at 1×), Rare (every 250 ticks ≈ 4.2 s) and Long (every 2000 ticks ≈ 33 s).
  • Why it scales: costly simulation (temperature diffusion, plant growth, filth/deterioration, need decay) is pushed onto TickRare/TickLong so hundreds of pawns and things never update in a single frame. Game speed multiplies ticks-per-frame, not the interval.

Pathfinding & Spatial Systems

  • Algorithm: a custom grid A* (PathFinder) over per-cell move costs.
  • Spatial partitioning: a Region + Room system groups contiguous cells joined by portals/doors, giving near-O(1) reachability checks and fast "closest X" queries that prune A* to relevant regions.
  • Cost matrix: terrain move cost, doors, buildings and blueprints, plus an avoid grid weighting danger/enemy proximity; rooms cache temperature and gas.

Follow us on social media for updates