forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traveler.js
376 lines (371 loc) · 17.5 KB
/
traveler.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* To start using Traveler, require it in main.js:
*
* There are 6 options available to pass to the module. Options are passed in the form
* of an object with one or more of the following:
*
* exportTraveler: boolean Whether the require() should return the Traveler class. Defaults to true.
* installTraveler: boolean Whether the Traveler class should be stored in `global.Traveler`. Defaults to false.
* installPrototype: boolean Whether Creep.prototype.travelTo() should be created. Defaults to true.
* hostileLocation: string Where in Memory a list of hostile rooms can be found. If it can be found in
* Memory.empire, use 'empire'. Defaults to 'empire'.
* maxOps: integer The maximum number of operations PathFinder should use. Defaults to 20000
* defaultStuckValue: integer The maximum number of ticks the creep is in the same RoomPosition before it
* determines it is stuck and repaths.
* reportThreshold: integer The mimimum CPU used on pathing to console.log() warnings on CPU usage. Defaults to 50
*
* Examples: var Traveler = require('Traveler')();
* require('util.traveler')({exportTraveler: false, installTraveler: false, installPrototype: true, defaultStuckValue: 2});
*/
module.exports = function(globalOpts = {}){
const gOpts = _.defaults(globalOpts, {
exportTraveler: true,
installTraveler: false,
installPrototype: true,
maxOps: 20000,
defaultStuckValue: 3,
reportThreshold: 50,
roomRange: 22,
});
class Traveler {
constructor() {
this.getHostileRoom = (roomName) => _.get(Memory, ['rooms', roomName, 'hostile']);
this.registerHostileRoom = (room) => room.registerIsHostile();
}
findAllowedRooms(origin, destination, options = {}) {
_.defaults(options, { restrictDistance: 16 });
if (Game.map.getRoomLinearDistance(origin, destination) > options.restrictDistance) {
return;
}
let allowedRooms = { [origin]: true, [destination]: true };
let ret = Game.map.findRoute(origin, destination, {
routeCallback: (roomName) => {
if (options.routeCallback) {
let outcome = options.routeCallback(roomName);
if (outcome !== undefined) {
return outcome;
}
}
if (Game.map.getRoomLinearDistance(origin, roomName) > options.restrictDistance)
return false;
let parsed;
if (options.preferHighway) {
parsed = /^[WE]([0-9]+)[NS]([0-9]+)$/.exec(roomName);
let isHighway = (parsed[1] % 10 === 0) || (parsed[2] % 10 === 0);
if (isHighway) {
return 1;
}
}
if (!options.allowSK && !Game.rooms[roomName]) {
if (!parsed) {
parsed = /^[WE]([0-9]+)[NS]([0-9]+)$/.exec(roomName);
}
let isSK = ((parsed[1] % 10 === 4) || (parsed[1] % 10 === 6)) &&
((parsed[2] % 10 === 4) || (parsed[2] % 10 === 6));
if (isSK) {
return 10;
}
}
if (!options.allowHostile && this.getHostileRoom(roomName) &&
roomName !== destination && roomName !== origin) {
return Number.POSITIVE_INFINITY;
}
return 2.5;
}
});
if (!_.isArray(ret)) {
console.log(`couldn't findRoute to ${destination}`);
return;
}
for (let value of ret) {
allowedRooms[value.room] = true;
}
allowedRooms.route = ret;
return allowedRooms;
}
findTravelPath(origin, destination, options = {}) {
_.defaults(options, {
ignoreCreeps: true,
range: 1,
obstacles: [],
maxOps: gOpts.maxOps,
});
let origPos = (origin.pos || origin), destPos = (destination.pos || destination);
let allowedRooms;
if (options.useFindRoute || (options.useFindRoute === undefined &&
Game.map.getRoomLinearDistance(origPos.roomName, destPos.roomName) > 2)) {
allowedRooms = this.findAllowedRooms(origPos.roomName, destPos.roomName, options);
}
let callback = (roomName) => {
if (options.roomCallback) {
let outcome = options.roomCallback(roomName, options.ignoreCreeps);
if (outcome !== undefined) {
return outcome;
}
}
if (allowedRooms) {
if (!allowedRooms[roomName]) {
return false;
}
} else if (this.getHostileRoom(roomName) && !options.allowHostile &&
roomName !== origPos.roomName && roomName !== destPos.roomName) {
return false;
}
let room = Game.rooms[roomName];
if (!room)
return;
let matrix;
if (options.ignoreStructures) {
matrix = new PathFinder.CostMatrix();
if (!options.ignoreCreeps) {
Traveler.addCreepsToMatrix(room, matrix);
}
}
else if (options.ignoreCreeps || roomName !== origin.pos.roomName) {
matrix = this.getStructureMatrix(room, options);
}
else {
matrix = this.getCreepMatrix(room, options);
}
for (let obstacle of options.obstacles) {
matrix.set(obstacle.pos.x, obstacle.pos.y, 0xff);
}
return matrix;
};
const ret = PathFinder.search(origPos, { pos: destPos, range: options.range }, {
swampCost: options.ignoreRoads ? 5 : 10,
plainCost: options.ignoreRoads ? 1 : 2,
maxOps: options.maxOps,
roomCallback: callback
});
ret.route = allowedRooms && allowedRooms.route;
return ret;
}
travelTo(creep, destination, options = {}) {
// register hostile rooms entered
let creepPos = creep.pos, destPos = (destination.pos || destination);
this.registerHostileRoom(creep.room);
// initialize data object
if (!creep.memory._travel) {
creep.memory._travel = { stuck: 0, tick: Game.time, cpu: 0, count: 0 };
}
let travelData = creep.memory._travel;
if (creep.fatigue > 0) {
travelData.tick = Game.time;
return ERR_BUSY;
}
if (!destination) {
return ERR_INVALID_ARGS;
}
// manage case where creep is nearby destination
let rangeToDestination = creep.pos.getRangeTo(destPos);
if (rangeToDestination <= 1) {
let outcome = OK;
if (rangeToDestination === 1) {
outcome = creep.move(creep.pos.getDirectionTo(destPos));
}
if (options.returnPosition && outcome === OK) {
return destPos;
}
else {
return outcome;
}
}
// check if creep is stuck
let hasMoved = true;
if (travelData.prev) {
const isBorder = (pos) => {
return pos.x === 0 || pos.x === 49 || pos.y === 0 || pos.y === 49;
};
const opposingBorders = (p1, p2) => {
return isBorder(p1) && isBorder(p2) && p1.roomName !== p2.roomName && (p1.x === p2.x || p1.y === p2.y);
};
travelData.prev = new RoomPosition(travelData.prev.x, travelData.prev.y, travelData.prev.roomName);
if (creepPos.inRangeTo(travelData.prev, 0) ||
opposingBorders(creep.pos, travelData.prev)) {
hasMoved = false;
travelData.stuck++;
} else {
creep.room.recordMove(creep);
travelData.stuck = 0;
}
}
// handle case where creep is stuck
if (travelData.stuck >= gOpts.defaultStuckValue) {
if (options.ignoreStuck) {
if (options.returnPosition && travelData.path && travelData.path.length > 0) {
let direction = parseInt(travelData.path[0]);
return Traveler.positionAtDirection(creepPos, direction);
}
else {
return OK;
}
}
else {
options.ignoreCreeps = false;
delete travelData.path;
}
}
// FIXME: Do an actual calculation to see if we have moved, this is unneccesary and expensive when the creep hasn't moved for
// a few ticks and the path gets rebuilt.
// // handle case where creep wasn't traveling last tick and may have moved, but destination is still the same
// if (Game.time - travelData.tick > Memory.skippedTicks + 2 && hasMoved) {
// console.log(creep.name, 'maybe moved, rebuilding');
// delete travelData.path;
// }
travelData.tick = Game.time;
// delete path cache if destination is different
if (!travelData.dest || travelData.dest.x !== destPos.x || travelData.dest.y !== destPos.y ||
travelData.dest.roomName !== destPos.roomName) {
delete travelData.path;
}
// pathfinding
if (!travelData.path) {
if (creep.spawning)
return ERR_BUSY;
travelData.dest = destPos;
travelData.prev = undefined;
let cpu = Game.cpu.getUsed();
let ret = this.findTravelPath(creep, destPos, options);
travelData.cpu += (Game.cpu.getUsed() - cpu);
travelData.count++;
travelData.avg = _.round(travelData.cpu / travelData.count, 2);
if (travelData.count > 25 && travelData.avg > TRAVELER_THRESHOLD) {
console.log(`TRAVELER: heavy cpu use: ${creep.name}, avg: ${travelData.cpu / travelData.count}, total: ${_.round(travelData.cpu, 2)}, pos: ${creep.pos}`);
}
if (ret.incomplete) {
const route = ret.route && ret.route.length;
console.log(`TRAVELER: incomplete path for ${creep.name} from ${creep.pos} to ${destPos}. Route length ${route}.`);
if (route > 1) {
ret = this.findTravelPath(creep, new RoomPosition(25, 25, ret.route[1].room),
_.create(options, {
range: gOpts.roomRange,
useFindRoute: false,
}));
console.log(`attempting path through next room using known route was ${ret.incomplete ? "not" : ""} successful`);
}
if (ret.incomplete && ret.ops < 2000 && options.useFindRoute === undefined && travelData.stuck < gOpts.defaultStuckValue) {
options.useFindRoute = false;
ret = this.findTravelPath(creep, destPos, options);
console.log(`attempting path without findRoute was ${ret.incomplete ? "not" : ""} successful`);
}
}
travelData.path = Traveler.serializePath(creep.pos, ret.path);
travelData.stuck = 0;
}
if (!travelData.path || travelData.path.length === 0) {
return ERR_NO_PATH;
}
// consume path and move
if (travelData.prev && travelData.stuck === 0) {
travelData.path = travelData.path.substr(1);
}
travelData.prev = creep.pos;
let nextDirection = parseInt(travelData.path[0]);
let outcome = creep.move(nextDirection);
if (!options.returnPosition || outcome !== OK) {
return outcome;
}
else {
return Traveler.positionAtDirection(creep.pos, nextDirection);
}
}
refreshMatrices() {
if (Game.time !== this.currentTick) {
this.currentTick = Game.time;
this.structureMatrixCache = {};
this.creepMatrixCache = {};
}
}
getStructureMatrix(room, options) {
if (options.getStructureMatrix) return options.getStructureMatrix(room);
this.refreshMatrices();
if (!this.structureMatrixCache[room.name]) {
let matrix = new PathFinder.CostMatrix();
this.structureMatrixCache[room.name] = Traveler.addStructuresToMatrix(room, matrix, 1);
}
return this.structureMatrixCache[room.name];
}
static addStructuresToMatrix(room, matrix, roadCost) {
for (let structure of room.find(FIND_STRUCTURES)) {
if (structure instanceof StructureRampart) {
if (!structure.my) {
matrix.set(structure.pos.x, structure.pos.y, 0xff);
}
}
else if (structure instanceof StructureRoad) {
matrix.set(structure.pos.x, structure.pos.y, roadCost);
}
else if (structure.structureType !== STRUCTURE_CONTAINER) {
// Can't walk through non-walkable buildings
matrix.set(structure.pos.x, structure.pos.y, 0xff);
}
}
for (let site of room.find(FIND_CONSTRUCTION_SITES)) {
if (site.structureType === STRUCTURE_CONTAINER) {
continue;
} else if (site.structureType === STRUCTURE_ROAD) {
continue;
} else if (site.structureType === STRUCTURE_RAMPART) {
continue;
}
matrix.set(site.pos.x, site.pos.y, 0xff);
}
return matrix;
}
getCreepMatrix(room, options) {
if (options.getCreepMatrix) return options.getCreepMatrix(room);
this.refreshMatrices();
if (!this.creepMatrixCache[room.name]) {
this.creepMatrixCache[room.name] = Traveler.addCreepsToMatrix(room, this.getStructureMatrix(room, options).clone());
}
return this.creepMatrixCache[room.name];
}
static addCreepsToMatrix(room, matrix) {
room.find(FIND_CREEPS).forEach((creep) => matrix.set(creep.pos.x, creep.pos.y, 0xff));
return matrix;
}
static serializePath(startPos, path) {
let serializedPath = "";
let lastPosition = startPos;
for (let position of path) {
if (position.roomName === lastPosition.roomName) {
serializedPath += lastPosition.getDirectionTo(position);
}
lastPosition = position;
}
return serializedPath;
}
static positionAtDirection(origin, direction) {
let offsetX = [0, 0, 1, 1, 1, 0, -1, -1, -1];
let offsetY = [0, -1, -1, 0, 1, 1, 1, 0, -1];
return new RoomPosition(origin.x + offsetX[direction], origin.y + offsetY[direction], origin.roomName);
}
}
if(gOpts.installTraveler){
global.Traveler = Traveler;
global.traveler = new Traveler();
global.travelerTick = Game.time;
}
if(gOpts.installPrototype){
// prototype requires an instance of traveler be installed in global
if(!gOpts.installTraveler) {
global.traveler = new Traveler();
global.travelerTick = Game.time;
}
Creep.prototype.travelTo = function (destination, options = {}) {
if(global.traveler && global.travelerTick !== Game.time){
global.traveler = new Traveler();
}
options = this.getStrategyHandler([], 'moveOptions', options);
if (_.isUndefined(options.useFindRoute)) options.useFindRoute = global.ROUTE_PRECALCULATION;
if (_.isUndefined(options.routeCallback)) options.routeCallback = Room.routeCallback(this.pos.roomName, destination.roomName, options);
if (_.isUndefined(options.getCreepMatrix)) options.getCreepMatrix = room => room.creepMatrix;
if (_.isUndefined(options.getStructureMatrix)) options.getStructureMatrix = room => room.structureMatrix;
return traveler.travelTo(this, destination, options);
};
}
if(gOpts.exportTraveler){
return Traveler;
}
};