Multi room Navigation.md - Mirroar/hivemind GitHub Wiki

Multi-room Navigation

Hivemind's navigation system is designed to efficiently move creeps across multiple rooms, even in complex or partitioned environments. This is achieved by maintaining a graph of known rooms, tracking how exits and portals connect, and detecting regions within rooms to ensure that travel between exits is actually possible.

How the Nav Mesh Works

  • Room Graph: The bot keeps a graph of all known rooms, including which exits (N/S/E/W) and portals connect to each other. This allows it to plan routes that span many rooms, not just the current one.
  • Exit and Portal Detection: For each room, the nav mesh records the location of all exits and portals, and which other rooms they connect to. This includes handling special cases like portals to distant rooms, or multiple exits in the same direction.
  • Region Detection: Some rooms may be partitioned by walls or terrain, making it impossible to travel directly between certain exits. The nav mesh detects these regions and ensures that only actually reachable exits are considered when planning a route.
  • Memory Storage: All this information is stored in Memory.nav and updated periodically, so pathfinding can be performed quickly without recalculating room structure every tick.

Pathfinding with findPath

The core method for multi-room navigation is findPath(startPos, endPos, options), defined in nav-mesh.ts. This method:

  • Uses the room graph to find a sequence of exits and portals connecting the start and end positions, even across many rooms.
  • Returns a list of RoomPositions representing the path, typically connecting the center of each exit or portal along the way. Note that this is not a full path that you can use with moveByPath, but rather a series of waypoints that the creep can follow.
  • Takes into account regions within rooms, so it will not suggest impossible routes (e.g., between exits that are separated by walls).
  • Can be configured to avoid dangerous rooms or prioritize safer routes.

This method is used internally by movement utilities like interRoomTravel, which allows creeps to travel long distances efficiently and safely.

Example Usage

// Find a path from the current creep's position to a target in another room
const navMesh = new NavMesh();
const result = navMesh.findPath(creep.pos, new RoomPosition(25, 25, 'W8N3'));
if (!result.incomplete && result.path) {
  // result.path is an array of RoomPosition objects connecting the rooms
}

Practical Details

  • The nav mesh is automatically updated as new rooms are explored or structures change.
  • If a room is partitioned (e.g., by walls), only reachable exits are considered for pathfinding.
  • Portals are treated as special exits and are included in the room graph.