An object representing the specified position in the room. Every
RoomObject
in the room
contains
RoomPosition
as the
pos
property. The position object of a custom location
can be obtained using the
Room.getPositionAt
method or using the constructor.
const pos = new RoomPosition(10, 25, 'sim');
You can create new
RoomPosition
object using its constructor.
parameter | type | description |
---|---|---|
x
|
number |
X position in the room. |
y
|
number |
Y position in the room. |
roomName
|
string |
The room name. |
The name of the room.
X position in the room.
Y position in the room.
Game.flags['Flag1'].pos.createConstructionSite(STRUCTURE_ROAD);
Game.flags['Flag1'].pos.createConstructionSite(STRUCTURE_SPAWN, 'MySpawn2');
Create new ConstructionSite at the specified location.
parameter | type | description |
---|---|---|
structureType
|
string |
One of the
|
name
optional |
string |
The name of the structure, for structures that support it (currently only spawns). |
One of the following codes:
constant | value | description |
---|---|---|
OK
|
0 |
The operation has been scheduled successfully. |
ERR_INVALID_TARGET
|
-7 |
The structure cannot be placed at the specified location. |
ERR_FULL
|
-8 |
You have too many construction sites. The maximum number of construction sites per player is 100. |
ERR_INVALID_ARGS
|
-10 |
The location is incorrect. |
ERR_RCL_NOT_ENOUGH
|
-14 |
Room Controller Level insufficient. Learn more |
creep.pos.createFlag('Flag1');
Create new Flag at the specified location.
parameter | type | description |
---|---|---|
name
optional |
string |
The name of a new flag. It should be unique, i.e. the
|
color
optional |
string |
The color of a new flag. Should be one of the
|
secondaryColor
optional |
string |
The secondary color of a new flag. Should be one of the
|
The name of a new flag, or one of the following error codes:
constant | value | description |
---|---|---|
ERR_NAME_EXISTS
|
-3 |
There is a flag with the same name already. |
ERR_INVALID_ARGS
|
-10 |
The location or the color constant is incorrect. |
const target = creep.pos.findClosestByPath(FIND_MY_SPAWNS);
creep.moveTo(target);
const target = creep.pos.findClosestByPath(FIND_MY_SPAWNS, {maxOps: 500});
creep.moveTo(target);
const target = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS, {
filter: function(object) {
return object.getActiveBodyparts(ATTACK) == 0;
}
});
const target = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS, {
filter: { owner: { username: 'Invader' } }
});
const targets = [
Game.creeps.John,
Game.creeps.Mike,
room.getPositionAt(10,10)
];
const closest = creep.pos.findClosestByPath(targets);
Find an object with the shortest path from the given position. Uses Jump Point Search algorithm and Dijkstra's algorithm .
parameter | type | description |
---|---|---|
type
|
number |
See Room.find . |
objects
|
array |
An array of room's objects or RoomPosition objects that the search should be executed against. |
opts
optional |
object |
An object containing pathfinding options (see Room.findPath ), or one of the following:
|
The closest object if found, null otherwise.
const target = creep.pos.findClosestByRange(FIND_MY_SPAWNS);
creep.moveTo(target);
const target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS, {
filter: function(object) {
return object.getActiveBodyparts(ATTACK) == 0;
}
});
const target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS, {
filter: { owner: { username: 'Invader' } }
});
const targets = [
Game.creeps.John,
Game.creeps.Mike,
room.getPositionAt(10,10)
];
const closest = creep.pos.findClosestByRange(targets);
Find an object with the shortest linear distance from the given position.
parameter | type | description |
---|---|---|
type
|
number |
See Room.find . |
objects
|
array |
An array of room's objects or RoomPosition objects that the search should be executed against. |
opts
optional |
object |
An object containing one of the following options:
|
The closest object if found, null otherwise.
const targets = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 3);
if(targets.length > 0) {
creep.rangedAttack(targets[0]);
}
const targets = [
Game.creeps.John,
Game.creeps.Mike,
room.getPositionAt(10,10)
];
const inRangeTargets = creep.pos.findInRange(targets, 3);
Find all objects in the specified linear range.
parameter | type | description |
---|---|---|
type
|
number |
See Room.find . |
objects
|
array |
An array of room's objects or RoomPosition objects that the search should be executed against. |
range
|
number |
The range distance. |
opts
optional |
object |
See Room.find . |
An array with the objects found.
const path = creep.pos.findPathTo(target);
creep.move(path[0].direction);
let path = creep.pos.findPathTo(target, {maxOps: 200});
if( !path.length || !target.equalsTo(path[path.length - 1]) ) {
path = creep.pos.findPathTo(target,
{maxOps: 1000, ignoreDestructibleStructures: true});
}
if( path.length ) {
creep.move(path[0].direction);
}
Find an optimal path to the specified position using Jump Point Search algorithm . This method is a shorthand for Room.findPath . If the target is in another room, then the corresponding exit will be used as a target.
parameter | type | description |
---|---|---|
x
|
number |
X position in the room. |
y
|
number |
Y position in the room. |
target
|
object |
Can be a RoomPosition object or any object containing RoomPosition . |
opts
optional |
object |
An object containing pathfinding options flags (see Room.findPath for more details). |
An array with path steps in the following format:
[
{ x: 10, y: 5, dx: 1, dy: 0, direction: RIGHT },
{ x: 10, y: 6, dx: 0, dy: 1, direction: BOTTOM },
{ x: 9, y: 7, dx: -1, dy: 1, direction: BOTTOM_LEFT },
...
]
const direction = creep.pos.getDirectionTo(target);
creep.move(direction);
Get linear direction to the specified position.
parameter | type | description |
---|---|---|
x
|
number |
X position in the room. |
y
|
number |
Y position in the room. |
target
|
object |
Can be a RoomPosition object or any object containing RoomPosition . |
A number representing one of the direction constants.
const range = creep.pos.getRangeTo(target);
if(range <= 3) {
creep.rangedAttack(target);
}
Get linear range to the specified position.
parameter | type | description |
---|---|---|
x
|
number |
X position in the room. |
y
|
number |
Y position in the room. |
target
|
object |
Can be a RoomPosition object or any object containing RoomPosition . |
A number of squares to the given position.
if(creep.pos.inRangeTo(target, 3)) {
creep.rangedAttack(target);
}
Check whether this position is in the given range of another position.
parameter | type | description |
---|---|---|
x
|
number |
X position in the same room. |
y
|
number |
Y position in the same room. |
target
|
RoomPosition |
The target position. |
range
|
number |
The range distance. |
A boolean value.
if(creep.pos.isEqualTo(10,25)) {
creep.move(RIGHT);
}
if(creep.pos.isEqualTo(Game.flags.Flag1)) {
creep.move(RIGHT);
}
Check whether this position is the same as the specified position.
parameter | type | description |
---|---|---|
x
|
number |
X position in the room. |
y
|
number |
Y position in the room. |
target
|
object |
Can be a RoomPosition object or any object containing RoomPosition . |
A boolean value.
if(creep.pos.isNearTo(target)) {
creep.transfer(target, RESOURCE_ENERGY);
}
Check whether this position is on the adjacent square to the specified position. The same as
inRangeTo(target, 1)
.
parameter | type | description |
---|---|---|
x
|
number |
X position in the room. |
y
|
number |
Y position in the room. |
target
|
object |
Can be a RoomPosition object or any object containing RoomPosition . |
A boolean value.
const look = Game.flags.Flag1.pos.look();
look.forEach(function(lookObject) {
if(lookObject.type == LOOK_CREEPS &&
lookObject[LOOK_CREEPS].getActiveBodyparts(ATTACK) == 0) {
creep.moveTo(lookObject.creep);
}
});
Get the list of objects at the specified room position.
An array with objects at the specified position in the following format:
[
{ type: 'creep', creep: {...} },
{ type: 'structure', structure: {...} },
...
{ type: 'terrain', terrain: 'swamp' }
]
const found = Game.flags.Flag1.pos.lookFor(LOOK_CREEPS);
if(found.length && found[0].getActiveBodyparts(ATTACK) == 0) {
creep.moveTo(found[0]);
}
Get an object with the given type at the specified room position.
parameter | type | description |
---|---|---|
type
|
string |
One of the
|
An array of objects of the given type at the specified position if found.