-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerAIv2.js
557 lines (452 loc) · 17.2 KB
/
PlayerAIv2.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
const STEP = {
MOVE: 1,
SHOOT: 2
};
class PlayerAIv2 extends EventListener {
constructor(game, options = {}) {
super();
const {
delay = 200,
computeOptions = {}
} = options;
//Apply default values to computeOption
{
const {
delay = 0,
renderHelper = false,
queueLoopLimit = 1000,
debug = false
} = computeOptions;
this.computeOptions = {
delay,
renderHelper,
queueLoopLimit,
debug
};
}
this.game = game;
this.delay = delay;
//Wait for the game to initialize
this.game.on("init", async e => {
if(this._isClone) return;
this.dispatchEvent("computestart");
const steps = await this.generateSteps();
this.dispatchEvent("computefinish", {steps});
this.log({finalSteps: steps});
for(const step of steps) {
if(step.type === STEP.MOVE) this.game.controller.moveTo(step.direction);
if(step.type === STEP.SHOOT) this.game.controller.shoot(step.direction);
await timeout(this.delay);
}
});
}
log(...message) {
if(this.computeOptions.debug) console.log(...message);
}
cloneGame(gameToClone = this.game) {
//Create a clone of the game and new AI agent
const game = gameToClone.clone();
const AIPlayer = new PlayerAIv2(game, {
delay: this.computeOptions.delay,
computeOptions: this.computeOptions
});
//Mark a new AI agent as a clone
AIPlayer._isClone = true;
//Setup the rendering options
if(this.computeOptions.renderHelper) {
const canvas = document.createElement("pre");
canvas.classList = "canvas helper";
game.renderOptions.canvas = canvas;
JL(".helper-games").appendChild(canvas);
}
game.renderOptions.enabled = this.computeOptions.renderHelper;
game.renderOptions.renderDistance = 8;
//Init the game
game.init();
//Return the cloned game and AI agent
return {
game,
AIPlayer
};
}
generateStepsFromVectorPath(path) {
return path.map(e => ({
type: STEP.MOVE,
direction: e.copy()
}));
}
async followThePath(path, game) {
if(this.computeOptions.delay) {
for(const position of path) {
game.controller.moveTo(position);
await timeout(this.delay);
}
} else if(path.length > 0) {
game.controller.moveTo(path[path.length - 1]);
}
}
async generateSteps() {
//Clone the game, so we can play it without affecting the original game
const {game: _game, AIPlayer: _AIPlayer} = this.cloneGame(this.game);
//LIFO stack for "recursive" game processing
const queue = [{game: _game, AIPlayer: _AIPlayer, steps: []}];
let _i = 0;
while(queue.length > 0) {
//Protection against infinite loop
if(_i++ > this.computeOptions.queueLoopLimit) {
this.log("Too many iterations");
this.dispatchEvent("fail", {reason: "Maximum number of iterations exceeded"});
break;
}
//Pop an item from the stack
const iteration = queue.shift();
const steps = iteration.steps.slice();
//Check if the Exit is reachable witout any other actions
{
//Generate path to the Exit
const result = iteration.game.map.find(iteration.game.map.getPlayer().position, object => {
if(object instanceof Exit) return 1;
else if(object instanceof Monster) return -1;
});
//The exit is reachable
if(result.found) {
await iteration.AIPlayer.followThePath(result.path, iteration.game);
steps.push(...this.generateStepsFromVectorPath(result.path));
this.log({exitSteps: steps});
return steps;
}
}
//No reachable Exit found
if(iteration.game.map.getPlayer().inventory.some(e => e instanceof Arrow)) {
//Get all reachable monsters from the current position of player
const monsters = iteration.game.map.getReachable(iteration.game.map.getPlayer().position, Monster);
this.log("reached monsters", monsters);
//Loop all the monsters
for(const monster of monsters) {
//Generate the path to the monster
this.log("locating monster", monster);
const result = iteration.game.map.find(iteration.game.map.getPlayer().position, object => {
if(object == monster) return 1;
if(object instanceof Player) return 0;
if(object instanceof Solid) return -1;
return 0;
});
this.log("locating monster", {playerPosition: iteration.game.map.getPlayer().position}, {result});
//Path to the monster was found
if(result.found) {
//Clone the current state of the game
const {game: game1, AIPlayer: AIPlayer1} = this.cloneGame(iteration.game);
//Navigate to the monster in the cloned game
//game1.map.moveObjectTo(game1.map.getPlayer(), result.path[firstMonsterIndex - 1]);
const _steps = steps.slice();
const safePath = result.path.slice(0, -1); this.log({safePath});
await iteration.AIPlayer.followThePath(safePath, game1);
_steps.push(...this.generateStepsFromVectorPath(safePath));
//Shoot the monster
const direction = Utils.direction(game1.map.getPlayer().position, monster.position).invert();
_steps.push({
type: STEP.SHOOT,
direction
});
game1.map.destroyObject(game1.map.getObject(monster.position));
game1.map.getPlayer().inventory.splice(game1.map.getPlayer().inventory.findIndex(e => e instanceof Arrow), 1);
//Push the cloned game to the stack for further processing
queue.push({game: game1, AIPlayer: AIPlayer1, steps: _steps});
} else {
this.log("Failed to found any monster");
}
}
} else {
//find some arrow to to kill the monster
{
//Generate path to the closest arrow
const result = iteration.game.map.find(iteration.game.map.getPlayer().position, object => {
if(object instanceof Arrow) return 1;
else if(object instanceof Monster) return -1;
});
//The arrow was found
if(result.found) {
await iteration.AIPlayer.followThePath(result.path, iteration.game);
steps.push(...this.generateStepsFromVectorPath(result.path));
queue.push({game: iteration.game, AIPlayer: iteration.AIPlayer, steps: steps});
this.log({arrowPickupSteps: steps});
}
}
}
}
this.log("No steps found");
this.dispatchEvent("fail", {reason: "No available actions can be made"});
return [];
}
async __generateSteps(steps = [], __game = this.game) {
const {game: _game, AIPlayer: _AIPlayer} = this.cloneGame(__game);
//console.log(_game.map.getPlayer().inventory);
let madeAction = false;
//Check if the Exit is reachable witout any other actions
{
const result = _game.map.find(_game.map.getPlayer().position, object => {
if(object instanceof Exit) return 1;
else if(object instanceof Monster) return -1;
});
if(result.found) {
await this.followThePath(result.path, _game);
steps.push(...this.generateStepsFromVectorPath(result.path));
madeAction = true;
steps._hasFinished = true;
return steps;
}
}
//No reachable Exit found
if(_game.map.getPlayer().inventory.some(e => e instanceof Arrow)) {
//Has some arrows, so go to kill the monster
{
//Find a way to the nearest exit
const result = _game.map.find(_game.map.getPlayer().position, object => {
if(object instanceof Exit) return 1;
});
//This is destructive task, so we need to calculate the right action
if(result.found) {
let monsterToAttack = null;
//Convert vector path to list of map objects
const objects = result.path.map(p => _game.map.getObject(p));
//Get all the monsters in the way
const monsters = objects.filter(e => e instanceof Monster);
//We know there's at least one monster in between player and the exit
const firstMonsterIndex = result.path.findIndex(v => v.isEqual(monsters[0].position));
//Now, let's imagine we kill this monster. Will we be able to made any new actions then?
{
//Clone the current state of the game, so we don't mess up the original one
const {game: game, AIPlayer: AIPlayer} = this.cloneGame(_game);
//throw new Error("Not implemented");
//console.log(game, game.map.getPlayer(), game.map.objects.filter(e => e.position.isEqual(game.map.getPlayer().position)));
//Kill the monster and remove one arrow from inventory
game.map.destroyObject(game.map.getObject(monsters[0].position));
game.map.getPlayer().inventory.splice(game.map.getPlayer().inventory.findIndex(e => e instanceof Arrow), 1);
//Try another action
await timeout(100);
var _steps = [];
const result1 = await AIPlayer.__generateSteps(_steps, game);
console.log({result1});
if(result1._hasFinished) {
steps.push(..._steps);
return steps;
}
//If we kill this monster, there will be some new actions to do
if(result1.length) {
monsterToAttack = monsters[0];
steps.push(..._steps);
} else {
//Let's check the next monster
//AIPlayer.player.inventory.push(new Arrow());
//If there are no more monsters, the exit is unreachable
if(monsters.length === 1) {
console.warn("No more monsters to kill");
return [];
}
else {
console.log("blocking");
const {game: game2, AIPlayer: AIPlayer2} = this.cloneGame(game);
//Kill the monster
game2.map.destroyObject(game2.map.getObject(monsters[0].position));
game2.map.addObject(new Wall({position: monsters[0].position}));
AIPlayer2.game.map.getPlayer().inventory.push(new Arrow());
//console.log(AIPlayer2.player.inventory);
//Try another action
var _steps2 = [];
const result2 = await AIPlayer2.__generateSteps(_steps2, game2);
console.log({result2});
if(result2._hasFinished) {
steps.push(..._steps2);
return steps;
}
//return result2;
//madeAction = true;
if(result2.length) {
monsterToAttack = monsters[1];
steps.push(..._steps2);
madeAction = true;
}
}
}
}
//Navigate to one block away from the Monster
const path = result.path.slice(0, result.path.findIndex(v => v.isEqual(monsterToAttack.position)));
await this.followThePath(path, _game);
//Attack the Monster
const direction = Utils.direction(_game.map.getPlayer().position, monsterToAttack.position).invert();
_game.controller.shoot(direction);
steps.push({
type: STEP.SHOOT,
direction
});
madeAction = true;
// //Navigate to one block away from the Monster
// const path = result.path.slice(0, firstMonsterIndex);
// await this.followThePath(path, _game);
// //Attack the Monster
// const direction = Utils.direction(_game.map.getPlayer().position, result.path[firstMonsterIndex]).invert();
// _game.controller.shoot(direction);
// madeAction = true;
}
}
} else {
//find some arrow to to kill the monster
{
const result = _game.map.find(_game.map.getPlayer().position, object => {
if(object instanceof Arrow) return 1;
else if(object instanceof Monster) return -1;
});
if(result.found) {
await this.followThePath(result.path, _game);
steps.push(...this.generateStepsFromVectorPath(result.path));
madeAction = true;
}
}
}
await timeout(100);
if(steps._hasFinished) {
console.log("finished");
return steps;
}
if(!madeAction) {
console.warn(new Error("No action can be made in current state of the game!"));
return steps;
//throw new Error("No action can be made in current state of the game!");
}
if(_game.isRunning) {
console.log("Game is still running!");
let _steps = [];
//console.log(_game.map.getPlayer().inventory);
await this.__generateSteps(_steps, _game);
if(_steps.length) steps.push(..._steps);
return steps;
}
}
async __calculateNextPosition(steps = []) {
let madeAction = false;
//Check if the Exit is reachable witout any other actions
{
const result = this.game.map.find(this.player.position, object => {
if(object instanceof Exit) return 1;
else if(object instanceof Monster) return -1;
});
if(result.found) {
await this.followThePath(result.path);
madeAction = true;
return true;
}
}
//No reachable Exit found
if(this.player.inventory.some(e => e instanceof Arrow)) {
//Has some arrows, so go to kill the monster
{
//Find a way to the nearest exit
const result = this.game.map.find(this.player.position, object => {
if(object instanceof Exit) return 1;
});
//This is destructive task, so we need to calculate the right action
if(result.found) {
let monsterToAttack = null;
//Convert vector path to list of map objects
const objects = result.path.map(p => this.game.map.getObject(p));
//Get all the monsters in the way
const monsters = objects.filter(e => e instanceof Monster);
//We know there's at least one monster in between player and the exit
const firstMonsterIndex = result.path.findIndex(v => v.isEqual(monsters[0].position));
//Now, let's imagine we kill this monster. Will we be able to made any new actions then?
{
//Clone the current state of the game, so we don't mess up the original one
var elm = document.createElement("pre");
elm.classList = "canvas helper";
document.body.appendChild(elm);
const game = this.game.clone();
console.log(this.game.map.spawn, game.map.spawn);
const AIPlayer = new PlayerAIv2(game, {delay: 300});
game.renderOptions.canvas = elm;
//game.renderOptions.enabled = false;
game.init();
//game.map.getPlayer().position = result.path[firstMonsterIndex - 1].copy();
//game.map.getPlayer().position = result.path[1].copy();
game.renderer.renderFrame();
//throw new Error("Not implemented");
console.log(game, game.map.getPlayer(), game.map.objects.filter(e => e.position.isEqual(game.map.getPlayer().position)));
//Kill the monster
game.map.destroyObject(game.map.getObject(monsters[0].position));
//Try another action
await timeout(100);
const result1 = await AIPlayer.__calculateNextPosition();
console.log({result1});
//If we kill this monster, there will be some new actions to do
if(result1) monsterToAttack = monsters[0];
else {
//Let's check the next monster
AIPlayer.player.inventory.push(new Arrow());
//If there are no more monsters, the exit is unreachable
if(monsters.length === 1) return false;
else {
console.log("blocking");
var elm2 = document.createElement("pre");
elm2.classList = "canvas helper";
document.body.appendChild(elm2);
//Block the way by replacing moster with wall
const game2 = game.clone();
const AIPlayer2 = new PlayerAIv2(game2, {delay: 300});
game2.renderOptions.canvas = elm2;
//game2.renderOptions.enabled = false;
game2.init();
AIPlayer2.player.inventory.push(new Arrow());
//Kill the monster
game2.map.destroyObject(game2.map.getObject(monsters[0].position));
game2.map.addObject(new Wall({position: monsters[0].position}));
console.log(AIPlayer2.player.inventory);
//Try another action
const result2 = await AIPlayer2.__calculateNextPosition();
console.log({result2});
//return result2;
//madeAction = true;
if(result2) {
monsterToAttack = monsters[1];
madeAction = true;
}
}
}
}
//Navigate to one block away from the Monster
const path = result.path.slice(0, result.path.findIndex(v => v.isEqual(monsterToAttack.position)));
await this.followThePath(path);
//Attack the Monster
const direction = Utils.direction(this.player.position, monsterToAttack.position).invert();
this.game.controller.shoot(direction);
madeAction = true;
// //Navigate to one block away from the Monster
// const path = result.path.slice(0, firstMonsterIndex);
// await this.followThePath(path);
// //Attack the Monster
// const direction = Utils.direction(this.player.position, result.path[firstMonsterIndex]).invert();
// this.game.controller.shoot(direction);
// madeAction = true;
}
}
} else {
//find some arrow to to kill the monster
{
const result = this.game.map.find(this.player.position, object => {
if(object instanceof Arrow) return 1;
else if(object instanceof Monster) return -1;
});
if(result.found) {
await this.followThePath(result.path);
madeAction = true;
}
}
}
if(!madeAction) {
console.warn(new Error("No action can be made in current state of the game!"));
return false;
//throw new Error("No action can be made in current state of the game!");
}
if(this.game.isRunning) {
return this.__calculateNextPosition();
}
}
}