forked from ermiyaeskandary/Slither.io-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.behaviors.js
630 lines (538 loc) · 21.8 KB
/
bot.behaviors.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
// ==UserScript==
// @name Slither.io-bot A*
// @namespace http://slither.io/
// @version 0.9.3
// @description Slither.io bot A*
// @author Ermiya Eskandary & Théophile Cailliau
// @match http://slither.io/
// @grant none
// ==/UserScript==
var behaviors = (function() {
return {
init: function() {
behaviors.newTask('actionSuggestFood', behaviors.actionSuggestFood);
behaviors.newTask('actionSprint', behaviors.actionSprint);
behaviors.newTask('actionWalk', behaviors.actionWalk);
behaviors.newTask('actionToCenterOfMap', behaviors.actionToCenterOfMap);
behaviors.newTask('actionStopFollow', behaviors.actionStopFollow);
behaviors.newTask('actionNone', behaviors.actionNone);
behaviors.newTask('actionAvoidSurround', behaviors.actionAvoidSurround);
behaviors.newTask('action180FromSnake', behaviors.action180FromSnake);
behaviors.newTask('actionTrollSurrounderer', behaviors.actionTrollSurrounderer);
behaviors.newTask('actionMoveToGoal', behaviors.actionMoveToGoal);
behaviors.newTask('actionFollowPath', behaviors.actionFollowPath);
behaviors.newTask('actionExitSequence', behaviors.actionExitSequence);
behaviors.newTask('isPathAvailable', behaviors.isPathAvailable);
behaviors.newTask('isNearEnemySnakeBody', behaviors.isNearEnemySnakeBody);
behaviors.newTask('isAlmostSurrounded', behaviors.isAlmostSurrounded);
behaviors.newTask('isSurrounded', behaviors.isSurrounded);
behaviors.newTask('isTouchingSnake', behaviors.isTouchingSnake);
behaviors.newTask('isTargetBlockedBySnake', behaviors.isTargetBlockedBySnake);
behaviors.newTask('isFoodExists', behaviors.isFoodExists);
behaviors.newTask('isGoodFood', behaviors.isGoodFood);
behaviors.newTask('isFollowingFood', behaviors.isFollowingFood);
behaviors.newTask('isReachedTarget', behaviors.isReachedTarget);
behaviors.newTask('isFacingTarget', behaviors.isFacingTarget);
behaviors.newTask('isNearEnemySnakeHead', behaviors.isNearEnemySnakeHead);
behaviors.newTask('isSprintAllowed', behaviors.isSprintAllowed);
behaviors.newTask('isHighQualityFoodAvailable', behaviors.isHighQualityFoodAvailable);
//MAIN Sequence, all sequences/tasks/conditions go in here
behaviors.newSequence('sequenceFoodTest',
[
//Avoid Collisions Sequence, this is highest priority
behaviors.sequence([
behaviors.condition({
test: 'isNearEnemySnakeBody',
pass:
behaviors.sequence([
'action180FromSnake',
'actionMoveToGoal'//,
//'actionExitSequence'
]),
fail: 'actionNone'
}),
/*behaviors.condition({
test: 'isNearEnemySnakeHead',
pass: '',
fail: ''
}),*/
behaviors.condition({
test: 'isAlmostSurrounded',
pass:
behaviors.sequence([
'actionAvoidSurround',
//'actionExitSequence'
]),
fail: 'actionNone'
})
]),
//Follow food, always when possible
behaviors.condition({
test: 'isFollowingFood',
pass:
behaviors.condition({
test: 'isPathAvailable',
pass: 'actionFollowPath',
fail:
behaviors.sequence([
'actionStopFollow',
'actionExitSequence'
])
}),
fail:
behaviors.condition({
test: 'isFoodExists',
pass: 'actionSuggestFood',
fail: 'actionToCenterOfMap'
})
}),
//Can we sprint to the food?
behaviors.condition({
test: 'isSprintAllowed',
pass: 'actionSprint',
fail: 'actionWalk'
}),
//Did we reach target?
behaviors.condition({
test: 'isReachedTarget',
pass: 'actionStopFollow',
fail:
//Look for High quality food
behaviors.condition({
test: 'isHighQualityFoodAvailable',
pass:
behaviors.sequence([
'actionStopFollow',
'actionSuggestFood'
]),
fail: 'actionNone'
})
})
]);
//This is the tree that is called from the "bot" module
behaviors.newTree('snakebot', ['sequenceFoodTest']);
},
isSprintAllowed: function(obj) {
var isAlmostSurrouneded = (bot.radarResults && bot.radarResults.open.length > 0 && bot.radarResults.pct < 0.30);
if( obj.insidesnake ) {
this.fail();
return;
}
if( obj.nearsnake && !isAlmostSurrouneded ) {
this.fail();
return;
}
if( isAlmostSurrouneded ) {
this.success();
return;
}
if( obj.foodTarget && obj.foodTarget.score > 50 ) {
if( bot.radarResults && bot.radarResults.open.length > 0 && bot.radarResults.pct < 0.30 ) {
console.log("almost surrounded");
this.success();
return;
}
var v = canvas.getRelativeAngle(window.getPos(), obj.goalCoordinates);
if( v.dot >= 0.5 ) {
this.success();
return;
}
}
this.fail();
},
//Exit a sequence immediately, the fail will propopage to the root node
actionExitSequence: function(obj) {
this.fail();
},
actionSuggestFood: function(obj) {
//window.setAcceleration(0);
var foodCnt = 0;
var bestFood = 0;
var foodPos;
var destpos;
var curpos = window.getPos();
for(var i=0; i<collisionGrid.foodGroups.length; i++) {
bestFood = collisionGrid.foodGroups[i];
var foodcnt = bestFood.nodes.length;
foodPos = {x: bestFood.sumx / foodcnt, y:bestFood.sumy / foodcnt};
destpos = collisionGrid.getCellByXY(foodPos.x, foodPos.y);
var line = collisionGrid.lineTest(curpos.x, curpos.y, foodPos.x, foodPos.y,TYPE_SNAKE);
var linePos = collisionGrid.getCellByColRow(line.col,line.row);
if( line.node )
continue;
if( destpos.node.type==TYPE_FOOD)
break;
}
obj.foodTarget = bestFood;
obj.followTime = (new Date()).getTime();
obj.followCoordinates = {x: foodPos.x, y:foodPos.y};
obj.goalCoordinates = {x: foodPos.x, y:foodPos.y};
this.success();
},
isHighQualityFoodAvailable: function(obj) {
if( collisionGrid.foodGroups.length ) {
if( collisionGrid.foodGroups[0].score > 50 ) {
this.success();
return;
}
}
this.fail();
},
isSprinting: function(obj) {
if( window.snake.sp > 7 ) {
this.success();
return;
}
this.fail();
},
isWalking: function(obj) {
if( window.snake.sp <= 7 ) {
this.success();
return;
}
this.fail();
},
actionMoveToGoal: function(obj) {
if (window.visualDebugging && bot.behaviorData.goalCoordinates) {
var headCoord = {
x: window.snake.xx,
y: window.snake.yy
};
canvas.drawLine(
canvas.mapToCanvas(headCoord),
canvas.mapToCanvas(bot.behaviorData.goalCoordinates),
'green');
canvas.drawCircle(bot.behaviorData.goalCoordinates, 'red', true);
}
canvas.setMouseCoordinates(canvas.mapToMouse(obj.goalCoordinates));
this.success();
},
actionFollowPath: function(obj) {
canvas.setMouseCoordinates(canvas.mapToMouse(obj.followCoordinates));
this.success();
},
action180FromSnake: function(obj) {
//console.log("action180 begin");
if( !obj.aggressor ) {
this.fail();
console.log("no aggressor");
return;
}
//console.log("action180 success");
var newcoord = {
x: window.snake.xx + (window.snake.xx - obj.aggressor.snk.closest.xx),
y: window.snake.yy + (window.snake.yy - obj.aggressor.snk.closest.yy)
};
obj.followCoordinates = {x: newcoord.x, y:newcoord.x};
obj.goalCoordinates = {x: newcoord.x, y:newcoord.y};
this.success();
},
/**
* Avoid surround by choosing a random open path from our radar
*/
actionAvoidSurround: function(obj) {
if( !bot.radarResults.open.length ) {
this.fail();
return false;
}
var curpos = window.getPos();
var openList = bot.radarResults.open[0];
var lineid = 0;
if( openList.length > 2 ) {
lineid = Math.floor(openList.length/2);
}
var line = openList[lineid];
obj.surroundExitPos = collisionGrid.getCellByColRow(line.col,line.row);
if( window.visualDebugging ) {
var canvasPosA = canvas.mapToCanvas({
x: curpos.x,
y: curpos.y,
radius: 1
});
var canvasPosB = canvas.mapToCanvas({
x: obj.surroundExitPos.x,
y: obj.surroundExitPos.y,
radius: 1
});
canvas.drawLine2(canvasPosA.x, canvasPosA.y, canvasPosB.x, canvasPosB.y, 1, 'blue');
}
obj.followCoordinates = {x: obj.surroundExitPos.x, y:obj.surroundExitPos.y};
obj.goalCoordinates = {x: obj.surroundExitPos.x, y:obj.surroundExitPos.y};
this.success();
},
/**
* Troll the snake surrounding us.
* Keep moving the snake to the furthest point
*/
actionTrollSurrounderer: function(obj) {
var collisions = bot.radarResults.collisions;
var furthest = collisions[collisions.length-1];
var cell = furthest.node;
var curpos = window.getPos();
var newpos = {x:curpos.x,y:curpos.y};
if( cell && cell.length ) {
//console.log("moving away from snake part");
newpos.x += curpos.x - cell[0].part.x;
newpos.y += curpos.y - cell[0].part.y;
obj.followCoordinates = {x: newpos.x, y:newpos.y};
obj.goalCoordinates = {x: newpos.x, y:newpos.y};
//var dist = canvas.getDistance(curpos.x, curpos.y, cell[0].part.x, cell[0].part.y);
//if( dist > 100 ) {
// bot.setNextState('findFood');
//}
}
},
isPathAvailable: function(obj) {
var curpos = window.getPos();
var minpath = 2;
var cell = collisionGrid.getCellByXY(obj.goalCoordinates.x, obj.goalCoordinates.y);
if( cell.node.type == TYPE_SNAKE || (cell.node.type == TYPE_EMPTY && cell.node.items.length) ) {
this.fail();
return;
}
var path = collisionGrid.generatePath(curpos.x, curpos.y, obj.goalCoordinates.x, obj.goalCoordinates.y);
//console.log("isPathAvailable");
if( path.length > minpath ) {
bot.currentPath = path;
if( window.visualDebugging )
for(i=0; i<path.length; i++) {
if( i == minpath )
collisionGrid.drawCell(path[i].x, path[i].y, 'rgba(0,0,255,0.4)');
else
collisionGrid.drawCell(path[i].x, path[i].y, 'rgba(255,0,0,0.4)');
}
var cell = collisionGrid.getCellByColRow(path[minpath].x, path[minpath].y);
obj.followCoordinates = {x: cell.x, y:cell.y};
this.success();
}
else {
obj.followCoordinates = {x: obj.goalCoordinates.x, y:obj.goalCoordinates.y};
this.fail();
}
},
actionWalk: function(obj) {
window.setAcceleration(0);
this.success();
},
actionSprint: function(obj) {
window.setAcceleration(1);
this.success();
},
actionToCenterOfMap: function(obj) {
obj.goalCoordinates = {x: 20000, y:20000};
this.success();
},
actionNone: function(obj) {
this.success();
},
actionStopFollow: function(obj) {
obj.followTime = 0;
this.success();
},
isFoodExists: function(obj) {
if( !collisionGrid.foodGroups.length ) {
this.fail();
return;
}
this.success();
},
isFollowingFood: function(obj) {
var curtime = (new Date()).getTime();
var diff = curtime - obj.followTime;
if( diff <= 5000 ) {
//console.log("Following FOOD!");
this.success();
return;
}
this.fail();
},
isGoodFood: function(obj) {
if( obj.foodTarget && obj.foodTarget.score > 50 ) {
this.success();
return;
}
this.fail();
},
isFacingTarget: function(obj) {
//var relv = {x: curpos.x - obj.followCoordinates.x, y: curpos.y-obj.followCoordinates.y};
var v = canvas.getRelativeAngle(window.getPos(), obj.goalCoordinates);
if( v.dot >= 0.5 ) {
this.success();
return;
}
this.fail();
},
isNearTarget: function(obj) {
var curpos = window.getPos();
var dist = canvas.getDistance2(curpos.x, curpos.y, obj.goalCoordinates.x, obj.goalCoordinates.y);
if( dist < 40000 ) { //200 units
this.success();
return;
}
this.fail();
},
isReachedTarget: function(obj) {
var curpos = window.getPos();
var dist = canvas.getDistance2(curpos.x, curpos.y, obj.goalCoordinates.x, obj.goalCoordinates.y);
if( dist < 10000 ) { //50 units
this.success();
return;
}
this.fail();
},
isNearEnemySnakeHead: function(obj) {
var aggressorCnt = collisionGrid.snakeAggressors.length;
for(var i=0; i<aggressorCnt; i++) {
var aggressor = collisionGrid.snakeAggressors[i];
collisionHelper.checkLineIntersection()
}
},
isNearEnemySnakeBody: function(obj) {
obj.nearsnake = false;
obj.insidesnake = false;
var aggressorCnt = collisionGrid.snakeAggressors.length;
var mindist = 22500;
var curpos = window.getPos();
var cell = collisionGrid.getCellByXY(curpos.x,curpos.y);
if( cell.node.type == TYPE_SNAKE ) {
var snake = cell.node.items[0].snake;
for(var i=0; i<aggressorCnt; i++) {
var aggressor = collisionGrid.snakeAggressors[i];
if( snake.id == aggressor.snk.id ) {
obj.aggressor = aggressor;
break;
}
}
obj.insidesnake = true;
//console.log("inside snake");
this.success();
return;
}
for(var i=0; i<aggressorCnt; i++) {
var aggressor = collisionGrid.snakeAggressors[i];
if( aggressor.snk.sp > 7 )
mindist = 90000;
else
mindist = 22500;
if( aggressor.snk.closest.distance2 < mindist ) {
obj.aggressor = aggressor;
//console.log("near snake");
obj.nearsnake = true;
this.success();
return;
}
}
this.fail();
},
isAlmostSurrounded: function(obj) {
if( bot.radarResults && bot.radarResults.open.length > 0 && bot.radarResults.pct < 0.30 ) {
console.log("almost surrounded");
this.success();
return;
}
this.fail();
},
isSurrounded: function(obj) {
if( bot.radarResults && bot.radarResults.open.length == 0 ) {
this.success();
return;
}
this.fail();
},
isTouchingSnake: function(obj) {
var curpos = window.getPos();
var currentCell = collisionGrid.getCellByXY(curpos.x, curpos.y);
if( currentCell.node && currentCell.node.type == TYPE_SNAKE ) {
bot.setNextState('avoidBody');
this.success();
return;
}
this.fail();
},
isTargetBlockedBySnake: function(obj) {
var line = collisionGrid.lineTest(curpos.x, curpos.y, obj.goalCoordinates.x, obj.goalCoordinates.y,TYPE_SNAKE);
var linePos = collisionGrid.getCellByColRow(line.col,line.row);
var collisionDist = canvas.getDistance2(curpos.x, curpos.y, linePos.x, linePos.y);
if( collisionDist < 2500 ) {
var dir = {x:0,y:0};
dir.x = curpos.x - linePos.x;
dir.y = curpos.y - linePos.y;
//window.goalCoordinates.x = curpos.x + dir.x;
//window.goalCoordinates.y = curpos.y + dir.y;
canvas.setMouseCoordinates(canvas.mapToMouse(obj.goalCoordinates));
bot.stopFollowLine(0);
this.success();
return;
}
this.fail();
},
trees: {},
newTask: function(name, run, start, end) {
start = start || function(obj){}
end = end || function(obj){}
BehaviorTree.register(
name,
new BehaviorTree.Task({
title: name,
run: run
})
);
},
newCondition: function(name, test, pass, fail) {
BehaviorTree.register(
name,
new BehaviorTree.Condition({
title: name,
nodes: [test, pass, fail]
})
);
},
newSequence: function(name, nodes) {
BehaviorTree.register(
name,
new BehaviorTree.Sequence({
title: name,
nodes: nodes
})
);
},
newTree: function(name, nodes) {
behaviors.trees[name] = new BehaviorTree({
title: name,
debug: true,
tree: new BehaviorTree.Sequence({
nodes: nodes
})
});
},
//Set a condition to test against
//data.test = sequence/task/condition that tests with success/fail
//data.pass = seq/task/cond when test passes
//data.fail = seq/task/cond when test fails
//@param data - {test:'', pass:'', fail''}
condition: function(data) {//test, pass, fail) {
return new BehaviorTree.Condition({
nodes: [data.test, data.pass, data.fail]
});
},
sequence: function(nodes) {
return new BehaviorTree.Sequence({
nodes: nodes
});
},
invertTask: function(name) {
return new InvertDecorator({
title: 'inverted_'+name,
node:new BehaviorTree.Sequence({
title:'inverted_seq_'+name,
nodes: [ name ]
})
});
},
run: function(treeName, obj) {
behaviors.trees[treeName].setObject(obj);
behaviors.trees[treeName].step();
}
};
})();
behaviors.init();