-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinding.js
More file actions
250 lines (222 loc) · 6.72 KB
/
Copy pathpathfinding.js
File metadata and controls
250 lines (222 loc) · 6.72 KB
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
var utilities = require('utilities');
/**
* Serializes an array of RoomPosition objects for storing in memory.
*/
Room.serializePositionPath = function (path) {
var result = [];
for (var i in path) {
result.push(utilities.encodePosition(path[i]));
}
return result;
};
/**
* Deserializes a serialized path into an array of RoomPosition objects.
*/
Room.deserializePositionPath = function (path) {
var result = [];
for (var i in path) {
result.push(utilities.decodePosition(path[i]));
}
return result;
};
/**
* Saves a cached path in a creeps memory for use.
*/
Creep.prototype.setCachedPath = function (path, reverse, distance) {
path = _.clone(path);
if (reverse) {
path.reverse();
}
if (distance) {
for (let i = 0; i < distance; i++) {
path.pop();
}
}
this.memory.cachedPath = {
path: path,
position: null,
arrived: false,
lastPositions: {},
};
};
/**
* Checks if a creep has a path stored.
*/
Creep.prototype.hasCachedPath = function () {
return this.memory.cachedPath;
};
/**
* Clears a creep's stored path.
*/
Creep.prototype.clearCachedPath = function () {
delete this.memory.cachedPath;
};
/**
* Checks if a creep has finished traversing it's stored path.
*/
Creep.prototype.hasArrived = function () {
return this.memory.cachedPath && this.memory.cachedPath.arrived;
};
/**
* Makes a creep follow it's cached path until the end.
*/
Creep.prototype.followCachedPath = function () {
var path = Room.deserializePositionPath(this.memory.cachedPath.path);
if (!this.memory.cachedPath.position) {
let target = this.pos.findClosestByRange(path, {
filter: (pos) => {
if (pos.roomName == this.room.name) {
// Only try to get to paths where no creep is positioned.
var found = pos.lookFor(LOOK_CREEPS);
var found2 = pos.lookFor(LOOK_STRUCTURES);
var found3 = pos.lookFor(LOOK_CONSTRUCTION_SITES);
var blocked = found.length > 0 && found[0].name != this.name;
for (let i in found2) {
if (found2[i].structureType != STRUCTURE_ROAD && found2[i].structureType != STRUCTURE_CONTAINER) {
blocked = true;
}
}
for (let i in found3) {
if (found3[i].structureType != STRUCTURE_ROAD && found3[i].structureType != STRUCTURE_CONTAINER) {
blocked = true;
}
}
return !blocked;
}
return false;
}
});
if (!target) {
// We're not in the correct room to move on this path. Kind of sucks, but try to get there using the default pathfinder anyway.
this.moveTo(path[0]);
return;
}
else {
// Try to get to the closest part of the path.
if (this.pos.x == target.x && this.pos.y == target.y) {
// We've arrived on the path, time to get moving along it!
for (let i in path) {
if (this.pos.x == path[i].x && this.pos.y == path[i].y && this.pos.roomName == path[i].roomName) {
this.memory.cachedPath.position = i;
break;
}
}
if (!this.memory.cachedPath.position) {
return;
}
}
else {
// Get closer to the path.
this.moveTo(target);
return;
}
}
}
// Make sure we don't have a string on our hands...
this.memory.cachedPath.position = this.memory.cachedPath.position * 1;
// Check if we've already moved onto the next position.
let next = path[this.memory.cachedPath.position + 1];
if (!next) {
// Out of range, so we're probably at the end of the path.
this.memory.cachedPath.arrived = true;
return;
}
if (next.x == this.pos.x && next.y == this.pos.y) {
this.memory.cachedPath.position++;
}
else if (next.roomName != this.pos.roomName) {
// We just changed rooms.
let afterNext = path[this.memory.cachedPath.position + 2];
if (afterNext.roomName == this.pos.roomName && afterNext.getRangeTo(this.pos) <= 1) {
this.memory.cachedPath.position += 2;
//console.log('path room switch', this.name, this.memory.cachedPath.position);
}
}
//this.say('Pos: ' + this.memory.cachedPath.position);
// @todo Check if we've been blocked for a while and try to move around the blockade.
// Check if we've moved at all during the previous ticks.
if (!this.memory.cachedPath.lastPositions) {
this.memory.cachedPath.lastPositions = {};
}
this.memory.cachedPath.lastPositions[Game.time % 5] = utilities.encodePosition(this.pos);
let stuck = false;
if (_.size(this.memory.cachedPath.lastPositions) > 5 / 2) {
let last = null;
stuck = true;
for (let i in this.memory.cachedPath.lastPositions) {
if (!last) {
last = this.memory.cachedPath.lastPositions[i];
}
if (last != this.memory.cachedPath.lastPositions[i]) {
stuck = false;
break;
}
}
}
if (stuck) {
//console.log(this.name, 'has been stuck for the last', _.size(this.memory.cachedPath.lastPositions), 'ticks. Trying to go around blockade.');
let i = this.memory.cachedPath.position + 1;
while (i < path.length) {
// Only try to get to paths where no creep is positioned.
var found = path[i].lookFor(LOOK_CREEPS);
var found2 = path[i].lookFor(LOOK_STRUCTURES);
var found3 = path[i].lookFor(LOOK_CONSTRUCTION_SITES);
var blocked = found.length > 0 && found[0].name != this.name;
for (let i in found2) {
if (found2[i].structureType != STRUCTURE_ROAD && found2[i].structureType != STRUCTURE_CONTAINER) {
blocked = true;
}
}
for (let i in found3) {
if (found3[i].structureType != STRUCTURE_ROAD && found3[i].structureType != STRUCTURE_CONTAINER) {
blocked = true;
}
}
if (!blocked) break;
i++;
}
if (i >= path.length) {
// No free spots until end of path. Let normal pathfinder take ofer.
this.memory.cachedPath.arrived = true;
return;
}
else {
//console.log(this.name, 'going to pos', i);
this.memory.cachedPath.forceGoTo = i;
delete this.memory.cachedPath.lastPositions;
}
}
// Check if we've arrived at the end of our path.
if (this.memory.cachedPath.position >= path.length - 1) {
this.memory.cachedPath.arrived = true;
return;
}
// Go around obstacles if necessary.
if (this.memory.cachedPath.forceGoTo) {
let pos = path[this.memory.cachedPath.forceGoTo];
if (this.pos.getRangeTo(pos) > 0) {
//this.say('Skip:' + this.memory.cachedPath.forceGoTo);
this.moveTo(pos);
return;
}
else {
this.memory.cachedPath.position = this.memory.cachedPath.forceGoTo;
delete this.memory.cachedPath.forceGoTo;
}
}
// Move towards next position.
next = path[this.memory.cachedPath.position + 1];
if (!next) {
// Out of range, so we're probably at the end of the path.
this.memory.cachedPath.arrived = true;
return;
}
if (next.roomName != this.pos.roomName) {
// Something went wrong, we must have gone off the path.
delete this.memory.cachedPath.position;
//console.log('path reeinitialize', this.name);
return;
}
let direction = this.pos.getDirectionTo(next);
this.move(direction);
};