Pathfinding System
The runtime uses the screeps-pathfinding library for advanced pathfinding with traffic management capabilities.
Key Features
Traffic Management
The pathfinding system enables intelligent traffic coordination:
- Priority-based movement: Higher priority creeps move first and can push lower priority creeps aside
- Creep swapping: Two creeps can swap positions instead of getting stuck
- Move off road: Creeps can step off roads when finished working to avoid blocking traffic
- Spot reservation: Reserve positions to prevent creep queueing at chokepoints
Movement Priority Levels
Movement priorities determine which creeps have precedence in traffic situations:
| Priority | Constant | Roles | Description |
|---|---|---|---|
| 6 | STATIONARY_HARVESTER | Stationary Harvesters | Highest - fixed source positions |
| 5 | HARVESTER | Harvesters | Critical for energy collection |
| 4 | HAULER | Haulers | Logistics backbone |
| 3 | COMBAT | Attackers, Healers, Dismantlers | Defense and offense |
| 2 | BUILDER | Builders, Repairers | Infrastructure support |
| 1 | SUPPORT | Scouts, Claimers | Support roles |
| 0 | UPGRADER | Upgraders | Lowest - can wait |
Architecture
Components
1 | ┌─────────────────────────────────────────────────────┐ |
Traffic Management Flow
- Move Intent Phase: All role controllers call
moveTo()orpriorityMoveTo()during behavior execution - Queue Collection: screeps-pathfinding collects all movement intents with their priorities
- Execution Phase:
runMoves()is called at the end of RoleControllerManager.execute() - Traffic Resolution: Higher priority creeps move first, lower priority creeps are pushed/swapped
Usage
Using Priority-Aware Movement
Role controllers can use the priorityMoveTo() helper function for traffic-managed movement:
1 | import { priorityMoveTo, MOVEMENT_PRIORITY } from "@runtime/behavior/controllers/helpers"; |
Accessing PathfindingManager
Use the ServiceLocator to access the PathfindingManager:
1 | import { serviceRegistry } from "@runtime/behavior/controllers/ServiceLocator"; |
Move Off Road Behavior
Creeps can step off roads when idle to avoid blocking traffic:
1 | const pathfindingManager = serviceRegistry.getPathfindingManager(); |
Spot Reservation
Reserve positions to prevent creep queueing:
1 | const pathfindingManager = serviceRegistry.getPathfindingManager(); |
Configuration
PathfindingManager is initialized in RoleControllerManager with these defaults:
1 | this.pathfindingManager = new PathfindingManager({ |
Cache Management
The path cache can be cleared when room structures change:
1 | const pathfindingManager = serviceRegistry.getPathfindingManager(); |
Graceful Degradation
The system gracefully falls back to native Screeps pathfinding when:
- screeps-pathfinding library fails to load
- PathfindingManager is not available in ServiceLocator
- Running in test environment without Screeps globals
1 | // priorityMoveTo automatically falls back to native moveTo |
Related Documentation
- Role Controllers - State machine behavior for roles
- Defense System - Combat role priorities
- Energy Economy - Harvester and hauler logistics
References
- screeps-pathfinding - Library by NesCafe62
- Issue #1358 - Integration PR
- Issue #1459 - ignoreCreeps for narrow passages