-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathscreeps_astar.js
685 lines (597 loc) · 17.2 KB
/
screeps_astar.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// tedivm 24 February 2016 at 22:19
// screep astar
// based off of javascript-astar 0.4.1
// http://github.com/bgrins/javascript-astar
// Freely distributable under the MIT License.
// Implements the astar search algorithm in javascript using a Binary Heap.
// Includes Binary Heap (with modifications) from Marijn Haverbeke.
// http://eloquentjavascript.net/appendix2.html
// Modified for Screeps by Robert Hafner
// Changes include:
// - Lazy loading gridnodes from Room
// - Additional scoring functions
// - Optimized BinaryHeap (from pull request)
// - Simplified API
// - Optimizations from http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
var astar = function() {};
// If this is "true" then flags will be placed. Yellow for nodes explored, green
// for the final path
astar.display = false;
astar.colors = {
optimal: COLOR_GREEN,
tested: COLOR_YELLOW
};
astar.defaults = {
diagonal: true,
heuristic: "manhattan",
closest: true,
weight: false,
heuristicModifier: 5,
avoid: [],
ignore: [],
maxops: false,
scoring: {
avoid: false,
ignore: false,
creep: 10,
terrain: {
swamp: 10,
plain: 2,
wall: 0
},
structures: {
default: 0,
road: 1,
constructedWall: 0,
hostile_rampart: 0,
rampart: false
},
filter: false,
distancepenalty: 0.0,
directionchange: 0.001
}
};
/**
* Perform an A* Search on a graph given a start and end node.
* @param {GridNode} start
* @param {GridNode} end
* @param {Object} [options]
* @param {bool} [options.closest] Specifies whether to return the
path to the closest node if the target is unreachable.
* @param {Function} [options.heuristic] Heuristic function (see
* astar.heuristics).
*/
(astar.prototype.search = function(room, start, end, user_options) {
var options = _.clone(astar.defaults);
_.merge(options, _.clone(user_options) || {});
var scoring = options.scoring;
var heuristicModifier = options.heuristicModifier;
var diagonal = options.diagonal == true;
var closest = options.closest;
var maxops = options.maxops;
var ops = 0;
if (options.heuristic && this.heuristics[options.heuristic]) {
var heuristic = this.heuristics[options.heuristic];
} else {
var heuristic = this.heuristics.manhattan;
}
if (typeof options.weight == "function") {
var weight = options.weight;
} else {
var weight = this.scoring;
}
var avoid_list = {};
if (options.avoid) {
for (var pos of options.avoid) {
if (!avoid_list[pos.x]) {
avoid_list[pos.x] = {};
}
avoid_list[pos.x][pos.y] = true;
}
}
scoring.avoid_list = avoid_list;
var ignore_list = {};
ignore_list[end.x] = {};
ignore_list[end.x][end.y] = true;
if (options.ignore) {
for (var pos of options.ignore) {
if (!ignore_list[pos.x]) {
ignore_list[pos.x] = {};
}
ignore_list[pos.x][pos.y] = true;
}
}
scoring.ignore_list = ignore_list;
var graph = new Graph(room, weight, scoring, diagonal);
var startNode = graph.getNode(start.x, start.y);
var endNode = graph.getNode(end.x, end.y);
var closestNode = startNode; // set the start node to be the closest if required
var direction = "";
var openHeap = new BinaryHeap(function(node) {
return node.f;
});
if (heuristicModifier <= 0) {
start.h = 0;
} else {
start.h = heuristic(startNode, endNode) * heuristicModifier;
}
openHeap.push(startNode);
while (openHeap.size() > 0) {
// Grab the lowest f(x) to process next. Heap keeps this sorted for us.
var currentNode = openHeap.pop();
// End case -- result has been found, return the traced path.
if (currentNode === endNode) {
return this.pathTo(room, currentNode);
}
// Normal case -- move currentNode from open to closed, process each of its neighbors.
currentNode.closed = true;
if (scoring.directionchange > 0 && currentNode.parent) {
direction = currentNode.getDirectionFrom(currentNode.parent);
}
// Find all neighbors for the current node.
var neighbors = graph.neighbors(currentNode);
for (var i = 0, il = neighbors.length; i < il; ++i) {
if (maxops && ops >= maxops) {
return closest ? this.pathTo(room, closestNode) : [];
}
ops++;
var neighbor = neighbors[i];
if (neighbor.closed || neighbor.isBlocked()) {
continue;
}
// The g score is the shortest distance from start to current node.
// We need to check if the path we have arrived at this neighbor is the shortest one we have seen yet.
var gScore = currentNode.g + neighbor.weight;
// Penalize changing direction to encourage straight lines
if (
scoring.directionchange > 0 &&
neighbor.getDirectionFrom(currentNode) != direction
) {
gScore += scoring.directionchange;
}
var beenVisited = neighbor.visited;
if (!beenVisited || gScore < neighbor.g) {
// reset the hueristic modifier each run
var heuristicModifier = options.heuristicModifier;
// If heuristics is disabled don't bother calculating it.
if (heuristicModifier > 0) {
// Increase the heuristic score based off of the distance from the goal
// This encourages straight lines and reduces the search space.
if (scoring.distancepenalty > 0) {
heuristicModifier +=
Math.abs(currentNode.x - endNode.x) * scoring.distancepenalty;
heuristicModifier +=
Math.abs(currentNode.y - endNode.y) * scoring.distancepenalty;
}
neighbor.h =
neighbor.h || heuristic(neighbor, endNode) * heuristicModifier;
} else {
neighbor.h = 0;
}
// Found an optimal (so far) path to this node. Take score for node to see how good it is.
neighbor.visited = true;
neighbor.parent = currentNode;
neighbor.g = gScore;
neighbor.f = neighbor.g + neighbor.h;
if (closest) {
// If the neighbour is closer than the current closestNode or if it's equally close but has
// a cheaper path than the current closest node then it becomes the closest node
if (
neighbor.h < closestNode.h ||
(neighbor.h === closestNode.h && neighbor.g < closestNode.g)
) {
closestNode = neighbor;
}
}
if (!beenVisited) {
// Pushing to heap will put it in proper place based on the 'f' value.
openHeap.push(neighbor);
} else {
// Already seen the node, but since it has been rescored we need to reorder it in the heap
openHeap.rescoreElement(neighbor);
}
}
}
}
// No result was found - return closest (if allowed) or empty array
return closest ? this.pathTo(room, closestNode) : [];
}),
// See list of heuristics: http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html
(astar.prototype.heuristics = {
manhattan: function(pos0, pos1) {
var d1 = Math.abs(pos1.x - pos0.x);
var d2 = Math.abs(pos1.y - pos0.y);
return d1 + +d2;
},
diagonal_weighted: function(pos0, pos1) {
var D = 1;
var D2 = Math.sqrt(2);
var d1 = Math.abs(pos1.x - pos0.x);
var d2 = Math.abs(pos1.y - pos0.y);
return D * (d1 + d2) + (D2 - 2 * D) * Math.min(d1, d2);
},
diagonal: function(pos0, pos1) {
var d1 = Math.abs(pos1.x - pos0.x);
var d2 = Math.abs(pos1.y - pos0.y);
return Math.max(d1, d2);
}
}),
(astar.prototype.scoring = function(room, x, y, scoring) {
if (!scoring) {
scoring = {};
}
var pos = room.getPositionAt(x, y);
if (typeof scoring.filter == "function") {
if (!scoring.filter(pos)) {
return 0;
}
}
var score = 0;
var terrain = room.lookForAt("terrain", pos)[0];
if (!scoring.terrain[terrain]) {
score = 0;
} else {
score = scoring.terrain[terrain];
}
if (score <= 0) {
return 0;
}
if (scoring.structure !== false) {
var structures = pos.getStructure();
if (structures.length > 0) {
for (var structure of structures) {
var structureType = structure.structureType;
if (typeof scoring.structures[structureType] == "undefined") {
structure = "default";
}
if (scoring.structures[structureType] !== false) {
score = scoring.structures[structureType];
}
if (!structure.my) {
var hostileStructureType = "hostile_" + structureType;
if (scoring.structures[hostileStructureType] !== false) {
score = scoring.structures[hostileStructureType];
}
}
if (score < 1) {
return 0;
}
}
}
}
if (scoring.creep !== false) {
var creeps = room.lookForAt("creep", pos);
if (creeps.length > 0) {
if (scoring.creep >= 1) {
score += scoring.creep;
} else {
return 0;
}
}
}
return score;
}),
(astar.prototype.pathTo = function(room, node) {
var path = [];
var curr = node;
do {
var curr_position = {
x: curr.x,
y: curr.y
};
if (astar.display && astar.colors.optimal) {
room.createFlagMemoryEfficient(
room.getPositionAt(curr.x, curr.y),
astar.colors.optimal
);
}
if (curr.parent) {
curr_position.dx = curr.x - curr.parent.x;
curr_position.dy = curr.y - curr.parent.y;
curr_position.direction = curr.parent.getDirectionFrom(curr);
path.unshift(curr_position);
curr = curr.parent;
} else {
// we're at the starting location, which we do not include in the path
curr = false;
}
} while (curr);
return path;
});
/**
* A graph memory structure that lazy loads it's grid elements as needed.
* @param {Room} [room] = Screeps Room object
* @param {Funtion} [weight] = Weight function for nodes
* @param {bool} [diagonal] = Specifies whether diagonal moves are allowed
*/
function Graph(room, weight, scoring, diagonal) {
this.room = room;
this.weight = weight;
this.scoring = scoring;
this.diagonal = diagonal;
this.grid = [];
}
Graph.prototype.getNode = function(x, y) {
if (!this.grid[x]) {
this.grid[x] = [];
}
if (!this.grid[x][y]) {
if (this.scoring.avoid_list[x] && this.scoring.avoid_list[x][y]) {
var weight = 0;
} else if (this.scoring.ignore_list[x] && this.scoring.ignore_list[x][y]) {
var weight = 1;
} else {
var weight = this.weight(this.room, x, y, this.scoring);
}
this.grid[x][y] = new GridNode(this.room, x, y, weight);
if (astar.display && astar.colors.tested) {
this.room.createFlagMemoryEfficient(
this.room.getPositionAt(x, y),
astar.colors.tested
);
}
}
return this.grid[x][y];
};
Graph.prototype.neighbors = function(node) {
var ret = [];
var x = node.x;
var y = node.y;
var grid = this.grid;
// West
if (x - 1 >= 0) {
var node = this.getNode(x - 1, y);
if (!node.isBlocked()) {
ret.push(node);
}
}
// East
if (x + 1 < 50) {
var node = this.getNode(x + 1, y);
if (!node.isBlocked()) {
ret.push(node);
}
}
// South
if (y - 1 >= 0) {
var node = this.getNode(x, y - 1);
if (!node.isBlocked()) {
ret.push(node);
}
}
// North
if (y + 1 < 50) {
var node = this.getNode(x, y + 1);
if (!node.isBlocked()) {
ret.push(node);
}
}
if (this.diagonal) {
// South
if (y - 1 >= 0) {
// West
if (x - 1 >= 0) {
var node = this.getNode(x - 1, y - 1);
if (!node.isBlocked()) {
ret.push(node);
}
}
// East
if (x + 1 < 50) {
var node = this.getNode(x + 1, y - 1);
if (!node.isBlocked()) {
ret.push(node);
}
}
}
// North
if (y + 1 < 50) {
// West
if (x - 1 >= 0) {
var node = this.getNode(x - 1, y + 1);
if (!node.isBlocked()) {
ret.push(node);
}
}
// East
if (x + 1 < 50) {
var node = this.getNode(x + 1, y + 1);
if (!node.isBlocked()) {
ret.push(node);
}
}
}
}
return ret;
};
function GridNode(room, x, y, weight) {
if (!weight || weight < 1) {
this.weight = 0;
} else {
this.weight = weight;
}
this.room = room;
this.x = x;
this.y = y;
this.f = 0;
this.g = 0;
this.h = 0;
this.visited = false;
this.closed = false;
this.parent = null;
}
GridNode.prototype.toString = function() {
return "[" + this.x + " " + this.y + "]";
};
GridNode.prototype.isBlocked = function() {
return this.weight < 1;
};
GridNode.prototype.getDirectionFrom = function(node) {
var x = this.x;
var y = this.y;
// Node is to the left
if (node.x < x) {
// Node is to the top
if (node.y < y) {
return 8;
}
// Node is on the same level
if (node.y == y) {
return 7;
}
// Node is to the bottom
if (node.y > y) {
return 6;
}
}
if (node.x == x) {
// Node is to the top
if (node.y < y) {
return 1;
}
// Node is to the bottom
if (node.y > y) {
return 5;
}
}
// Node is to the right
if (node.x > x) {
// Node is to the top
if (node.y < y) {
return 2;
}
// Node is on the same level
if (node.y == y) {
return 3;
}
// Node is to the bottom
if (node.y > y) {
return 4;
}
}
};
function BinaryHeap(scoreFunction) {
this.content = [];
this.scoreFunction = scoreFunction;
}
BinaryHeap.prototype = {
push: function(element) {
var content = this.content;
// Add the new element to the end of the array.
content.push(element);
// Allow it to sink down.
this.sinkDown(content.length - 1);
},
pop: function() {
var content = this.content;
// Store the first element so we can return it later.
var result = content[0];
// Get the element at the end of the array.
var end = content.pop();
// If there are any elements left, put the end element at the
// start, and let it bubble up.
if (content.length !== 0) {
content[0] = end;
this.bubbleUp(0);
}
return result;
},
remove: function(node) {
var content = this.content;
var i = content.indexOf(node);
// When it is found, the process seen in 'pop' is repeated
// to fill up the hole.
var end = content.pop();
if (i !== content.length - 1) {
content[i] = end;
if (this.scoreFunction(end) < this.scoreFunction(node)) {
this.sinkDown(i);
} else {
this.bubbleUp(i);
}
}
},
size: function() {
return this.content.length;
},
rescoreElement: function(node) {
this.sinkDown(this.content.indexOf(node));
},
sinkDown: function(n) {
var content = this.content;
var scoreFunction = this.scoreFunction;
// Fetch the element that has to be sunk.
var element = content[n];
//
var elemScore = scoreFunction(element);
var parentN = 0,
parent = 0;
// When at 0, an element can not sink any further.
while (n > 0) {
// Compute the parent element's index, and fetch it.
parentN = ((n + 1) >> 1) - 1;
parent = content[parentN];
// Swap the elements if the parent is greater.
if (elemScore < scoreFunction(parent)) {
content[parentN] = element;
content[n] = parent;
// Update 'n' to continue at the new position.
n = parentN;
}
// Found a parent that is less, no need to sink any further.
else {
break;
}
}
},
bubbleUp: function(n) {
var content = this.content;
var scoreFunction = this.scoreFunction;
// Look up the target element and its score.
var length = content.length;
var element = content[n];
var elemScore = scoreFunction(element);
// early declarations with type hints
var child2N = 0,
child1N = 0;
var child1Score = 0;
var swap = -1; // no type change !! -1 stands for no swap. X2 speed increase !!!
var child1 = null,
child2 = null;
while (true) {
// Compute the indices of the child elements.
child2N = (n + 1) << 1;
child1N = child2N - 1;
// This is used to store the new position of the element, if any.
swap = -1;
// If the first child exists (is inside the array)...
if (child1N < length) {
// Look it up and compute its score.
child1 = content[child1N];
child1Score = scoreFunction(child1);
// If the score is less than our element's, we need to swap.
if (child1Score < elemScore) {
swap = child1N;
}
}
// Do the same checks for the other child.
if (child2N < length) {
child2 = content[child2N];
if (scoreFunction(child2) < (swap < 0 ? elemScore : child1Score)) {
swap = child2N;
}
}
// If the element needs to be moved, swap it, and continue.
if (swap >= 0) {
content[n] = content[swap];
content[swap] = element;
n = swap;
}
// Otherwise, we are done.
else {
break;
}
}
}
};
module.exports = astar;