Contains powerful methods for pathfinding in the game world. This module is written in fast native C++ code and supports custom navigation costs and paths which span multiple rooms.
let creep = Game.creeps.John;
let goals = _.map(creep.room.find(FIND_SOURCES), function(source) {
// We can't actually walk on sources-- set `range` to 1
// so we path next to it.
return { pos: source.pos, range: 1 };
});
let ret = PathFinder.search(
creep.pos, goals,
{
// We need to set the defaults costs higher so that we
// can set the road cost lower in `roomCallback`
plainCost: 2,
swampCost: 10,
roomCallback: function(roomName) {
let room = Game.rooms[roomName];
// In this example `room` will always exist, but since
// PathFinder supports searches which span multiple rooms
// you should be careful!
if (!room) return;
let costs = new PathFinder.CostMatrix;
room.find(FIND_STRUCTURES).forEach(function(struct) {
if (struct.structureType === STRUCTURE_ROAD) {
// Favor roads over plain tiles
costs.set(struct.pos.x, struct.pos.y, 1);
} else if (struct.structureType !== STRUCTURE_CONTAINER &&
(struct.structureType !== STRUCTURE_RAMPART ||
!struct.my)) {
// Can't walk through non-walkable buildings
costs.set(struct.pos.x, struct.pos.y, 0xff);
}
});
// Avoid creeps in the room
room.find(FIND_CREEPS).forEach(function(creep) {
costs.set(creep.pos.x, creep.pos.y, 0xff);
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
Find an optimal path between
origin
and
goal
.
parameter | type | description |
---|---|---|
origin
|
RoomPosition |
The start position. |
goal
|
object |
A goal or an array of goals. If more than one goal is supplied then the cheapest path found out of all the goals will be returned. A goal is either a RoomPosition or an object as defined below.
Important:
Please note that if your goal is not walkable (for instance, a source) then you should set
|
opts
optional |
object |
An object containing additional pathfinding flags.
|
An object containing the following properties:
property | description |
---|---|
path
|
An array of RoomPosition objects. |
ops
|
Total number of operations performed before this path was calculated. |
cost
|
The total cost of the path as derived from
plainCost
,
swampCost
and any given CostMatrix instances.
|
incomplete
|
If the pathfinder fails to find a complete path, this will be true. Note that
path
will still be populated with a partial path which represents the closest path it could find given the search parameters.
|
This method is deprecated and will be removed soon.
PathFinder.use(true);
Game.creeps.John.moveTo(Game.spawns['Spawn1']);
Specify whether to use this new experimental pathfinder in game objects methods. This method should be invoked every tick. It affects the following methods behavior:
Room.findPath
,
RoomPosition.findPathTo
,
RoomPosition.findClosestByPath
,
Creep.moveTo
.
parameter | type | description |
---|---|---|
isEnabled
|
boolean |
Whether to activate the new pathfinder or deactivate. The default is
|