forked from JSJitsu/hero-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
385 lines (318 loc) · 9.59 KB
/
helpers.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
var gameData;
var unique = 1;
var Directions = {
Stay: {x:0,y:0},
North: {x:0,y:-1},
South: {x:0,y:1},
West: {x:-1,y:0},
East: {x:1,y:0}
};
var Inverse = {
Stay: "Stay",
North: "South",
South: "North",
West: "East",
East: "West"
};
function addHealth(hero, amount) {
hero.health += amount;
if(hero.health<=0) hero.dead = true; // we don't go as far as the game itself, which would replace the tile with unoccupied/bones
if(hero.health>100) hero.health = 100;
}
function performMove(board, hero, direction) {
var y = hero.distanceFromTop;
var x = hero.distanceFromLeft;
var d = Directions[direction];
if(d && direction!=="Stay") {
var x2=x+d.x, y2=y+d.y;
var dest = board.tiles[y2] && board.tiles[y2][x2];
if(dest) {
if(passable(dest)) {
//move the hero
board.tiles[y][x] = {
type: "Unoccupied",
distanceFromTop: y,
distanceFromLeft: x
}
board.tiles[y2][x2] = hero;
hero.distanceFromTop = y2;
hero.distanceFromLeft = x2;
} else if(isAlly(dest, hero)) {
addHealth(dest, 40);
} else if(isEnemy(dest, hero)) {
addHealth(dest, -10);
} else if(isWell(dest)) {
addHealth(hero, 30);
} else if(isMine(dest)) {
if(!dest.owner || dest.owner.id!==hero.id) {
if(!dest.owner || dest.owner.team!=hero.team) {
//TODO subtract from previous owner, but for now just avoid rewarding stealing from own team
hero.mineCount++;
}
addHealth(hero, -20);
dest.owner = hero;
}
}
} //else out of bounds
}
//attack surroundings
if(!hero.dead) { //could have died taking mine
var neighbors = getAdjacentFilter(hero, board, isEnemy, hero);
for(var i=0; i<neighbors.length; i++) {
addHealth(neighbors[i], -20);
}
}
}
//we only track health, not diamonds or other stats yet
function simulate(gameData, mover) {
var board = gameData.board;
var heroes = gameData.heroes;
var startIndex = gameData.heroTurnIndex;
// make sure there's just one copy
for(var i=0; i<heroes.length; i++) {
var h = heroes[i];
if(!h.dead) {
heroes[i] = board.tiles[h.distanceFromTop][h.distanceFromLeft];
}
}
do {
var hero = heroes[gameData.heroTurnIndex];
if(!hero.dead) {
var move = mover(hero, board);
performMove(board, hero, move)
}
gameData.heroTurnIndex++;
if(gameData.heroTurnIndex>=heroes.length) gameData.heroTurnIndex=0;
} while(gameData.heroTurnIndex!=startIndex);
clearNonsense(gameData);
}
function clearNonsense(game) {
// clear some of the stuff that interferes with cloning, like pathfinder dirt
var wh = gameData.board.lengthOfSide;
var tiles = game.board.tiles;
for(var y=0; y<wh; y++) {
for(var x=0; x<wh; x++) {
var tile = tiles[y][x];
if(tile.v) delete tile.v;
if(tile.p) delete tile.p;
if(tile.distance) delete tile.distance;
}
}
}
function getMoveTile(board, hero, direction) {
var y = hero.distanceFromTop;
var x = hero.distanceFromLeft;
var d = Directions[direction];
if(d) {
var x2=x+d.x, y2=y+d.y;
return board.tiles[y2] && board.tiles[y2][x2];
}
}
function validMove(board, hero, direction) {
if(direction==="Stay") return true; //always valid
var dest = getMoveTile(board, hero, direction);
if(!dest) return false;
//we consider pointless/null moves to be invalid here
if(dest.type==="DiamondMine" && dest.owner && dest.owner.id===hero.id) return false;
if(dest.type==="HealthWell" && hero.health===100) return false;
if(isAlly(dest, hero) && dest.health===100) return false;
if(dest.type==="Impassable") return false;
return true;
}
function clone(c) { return JSON.parse(JSON.stringify(c)); }
function shuffle(a) {
var i = a.length;
while(i>1) {
var j = Math.floor(Math.random()*i--);
var t = a[i];
a[i] = a[j];
a[j] = t;
}
}
function inBounds(x, y) {
var wh = gameData.board.lengthOfSide;
return x>=0 && y>=0 && x<wh && y<wh;
}
function getAdjacent(tile, board) {
var tiles = board.tiles;
var x = tile.distanceFromLeft;
var y = tile.distanceFromTop;
var ret=[];
// being silly
var t;
y>0 && (t = tiles[y-1][x]) && ret.push(t);
x+1<board.lengthOfSide && (t = tiles[y][x+1]) && ret.push(t);
y+1<board.lengthOfSide && (t = tiles[y+1][x]) && ret.push(t);
x>0 && (t = tiles[y][x-1]) && ret.push(t);
return ret;
}
function getAdjacentFilter(tile, board, filter, hero) {
var tiles = board.tiles;
var x = tile.distanceFromLeft;
var y = tile.distanceFromTop;
var ret=[];
var t;
y>0 && (t = tiles[y-1][x]) && filter(t,hero) && ret.push(t);
x+1<board.lengthOfSide && (t = tiles[y][x+1]) && filter(t,hero) && ret.push(t);
y+1<board.lengthOfSide && (t = tiles[y+1][x]) && filter(t,hero) && ret.push(t);
x>0 && (t = tiles[y][x-1]) && filter(t,hero) && ret.push(t);
return ret;
}
function grave(tile) {
return (tile.type==="Hero" && tile.dead) || tile.subType==="Bones" || tile.subType==="RedFainted" || tile.subType==="BlueFainted";
}
function passable(tile) {
return tile && (tile.type==="Unoccupied" || (tile.type==="Hero" && tile.dead));
}
function hittable(tile) {
return tile && !passable(tile) && tile.type!=="Impassable";
}
// I'd prefer to be consistent with findNearestObjectDirectionAndDistance if preferGraves is false, hence the reverse loop in pathTrace() and the order of directions in getAdjacent()
function pathTrace(dest, board, preferGraves, source, alternate) {
var path = [dest];
var now = dest.v;
var tile = dest;
while(tile.distance>0) {
if(preferGraves) {
var adjacent = getAdjacent(tile, board);
if(alternate) adjacent.reverse();
var distance = tile.distance-1;
tile = undefined;
for(var i=adjacent.length-1; i>=0; i--) {
var neighbor = adjacent[i];
if(neighbor.distance>distance || neighbor.v!==now || (neighbor!==source && !passable(neighbor))) continue;
tile = neighbor;
if(!preferGraves || grave(tile)) break;
}
} else {
tile = tile.p;
}
path.push(tile);
}
return path.reverse();
}
function direction(srcTile, destTile) {
var dy = destTile.distanceFromTop - srcTile.distanceFromTop;
var dx = destTile.distanceFromLeft - srcTile.distanceFromLeft;
if(dx==0 && dy==0) return "Stay";
if(Math.abs(dx)>Math.abs(dy)) {
return (dx<0) ? "West" : "East";
} else {
return (dy<0) ? "North" : "South";
}
}
function pathFind(source, board, preferGraves, filter, limit, maxDist, avoid) {
var targets = [];
var now = unique++;
source = board.tiles[source.distanceFromTop][source.distanceFromLeft]; //correct for duplication in gameData
var avoided = [];
var queue=[source], next=[];
source.v = now;
source.distance = 0;
for(;;) {
var index=0;
while(index<queue.length) {
var tile = queue[index++];
if(hittable(tile) && (!filter || filter(tile, source)) && tile!==source) {
var path = pathTrace(tile, board, preferGraves, source);
var target = {
tile: tile,
path: path,
dir: direction(source, path[1]),
distance: tile.distance
};
if(preferGraves) { //todo rename that param
//look for an equal, but alternate path
var altPath = pathTrace(tile, board, preferGraves, source, true);
target.altPath = altPath;
target.altDir = direction(source, altPath[1]);
}
targets.push(target);
if(limit && targets.length>=limit) return targets;
}
if(passable(tile) || tile===source) {
var distance = tile.distance+1;
if(maxDist && distance>maxDist) return targets;
var adjacent = getAdjacent(tile, board);
for(var i=0; i<adjacent.length; i++) {
var neighbor = adjacent[i];
if(neighbor.v===now) continue;
neighbor.v = now;
neighbor.distance = distance;
if(!preferGraves) neighbor.p = tile;
if(avoid && avoid(neighbor)) {
neighbor.distance+=5;
avoided.push(neighbor);
} else if(preferGraves && grave(neighbor)) {
//trick to prefer grave paths
next.unshift(neighbor);
} else {
next.push(neighbor);
}
}
}
}
if(next.length==0) {
if(avoided.length>0) {
//low quality alternative to using a priority queue for poor paths
var d=avoided[0].distance;
do {
next.push(avoided.shift());
} while(avoided.length>0 && avoided[0].distance===d);
} else {
break;
}
}
queue = next;
next = [];
}
return targets;
}
function findFirst(source, board, filter) {
return pathFind(source, board, false, filter, 1)[0];
}
function isAlly(other, hero) {
return other.type==="Hero" && !other.dead && other.team === (hero||gameData.activeHero).team;
}
function isEnemy(other, hero) {
return other.type==="Hero" && !other.dead && other.team !== (hero||gameData.activeHero).team;
}
function isWeakerEnemy(other, hero) {
hero = hero||gameData.activeHero;
return other.type==="Hero" && !other.dead && other.team !== hero.team && other.health<hero.health;
}
function isWell(other) {
return other.type==="HealthWell";
}
function isOtherMine(other, hero) {
return other.type==="DiamondMine" && (!other.owner || other.owner.team!==hero.team || other.owner.dead);
}
function isAllyMine(other, hero) {
return other.type==="DiamondMine" && (other.owner && other.owner.team===hero.team && !other.owner.dead);
}
function isMine(other) {
return other.type==="DiamondMine";
}
module.exports = {
setGameData: function(g) { gameData = g; },
clone: clone,
shuffle: shuffle,
inBounds: inBounds,
getAdjacent: getAdjacent,
pathFind: pathFind,
findFirst: findFirst,
isAlly: isAlly,
isEnemy: isEnemy,
isWeakerEnemy: isWeakerEnemy,
isWell: isWell,
isOtherMine: isOtherMine,
isAllyMine: isAllyMine,
Directions: Directions,
validMove: validMove,
simulate: simulate,
Inverse: Inverse,
getAdjacent: getAdjacent,
getMoveTile: getMoveTile,
passable: passable,
getAdjacentFilter: getAdjacentFilter
};