-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.movement.js
63 lines (60 loc) · 1.95 KB
/
helper.movement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const inverseDirections = {
"1": BOTTOM,
"2": BOTTOM_LEFT,
"3": LEFT,
"4": TOP_LEFT,
"5": TOP,
"6": TOP_RIGHT,
"7": RIGHT,
"8": BOTTOM_RIGHT
};
module.exports = {
moveToRoom: function(creep, roomName) {
creep.goTo({ pos: new RoomPosition(25, 25, roomName) }, { range: 10, avoidHostiles: true });
},
calculateRoute: function(startRoom, endRoom) {
let route = Game.map.findRoute(startRoom, endRoom, { routeCallback: routeCallback });
let walkableRoute = {};
let from = startRoom.name || startRoom;
for(let node of route) {
walkableRoute[from] = node.exit;
from = node.room;
}
return walkableRoute;
},
leaveExit: function(creep) {
if(creep.pos.x == 0) {
creep.move(RIGHT);
} else if(creep.pos.x == 49) {
creep.move(LEFT);
} else if(creep.pos.y == 0) {
creep.move(BOTTOM);
} else if(creep.pos.y == 49) {
creep.move(TOP);
}
},
isOnExit: function(creep) {
return creep.pos.x == 0 || creep.pos.y == 0 || creep.pos.x == 49 || creep.pos.y == 49;
},
getExitRoom: function(creep) {
let direction = this.getExitDirection(creep);
if(!direction) return null;
return Game.map.describeExits(creep.room.name)[direction];
},
getExitDirection: function(creep) {
if(creep.pos.x == 0) return LEFT;
if(creep.pos.x == 49) return RIGHT;
if(creep.pos.y == 0) return TOP;
if(creep.pos.y == 49) return BOTTOM;
return null;
},
inverseDirection: function(direction) {
return inverseDirections[direction];
},
isWithin: function(creep, left, top, right, bottom) {
let pos = creep.pos;
return pos.x >= left && pos.x <= right && pos.y >= top && pos.y <= bottom;
}
};
const profiler = require("screeps-profiler");
profiler.registerObject(module.exports, 'movement');