-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTP_Bot.js
More file actions
612 lines (569 loc) · 25.8 KB
/
TP_Bot.js
File metadata and controls
612 lines (569 loc) · 25.8 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
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
// ==UserScript==
// @name Labyrinth Bot
// @version 0.1
// @include *.newcompte.fr:*
// ==/UserScript==
var obstacleData; // data to be pulled in from Obstacles.json
function waitForId(fn) {
// Don't execute the function until tagpro.playerId has been assigned.
if (!tagpro || !tagpro.playerId) {
return setTimeout(function() {
waitForId(fn);
}, 100);
} else {
// Only run the script if we are not spectating.
if (!tagpro.spectator) {
fn();
}
}
}
function script() {
let ctrl_period = 50;
let counter = 0;
let debugr = create_debugger(); // draws point of where bot is headed to
let buttonDist = 25;
let moveDist = 20;
let standDist = 3;
let obstacles = preprocess();
// console.log(obstacles)
function create_decision_maker(me){
var decision_maker = {};
decision_maker.me = me;
decision_maker.role = {state: 'follow', step: 0, role: false};
decision_maker.human = null; // will be set for caching
decision_maker.lastPlayerPos = {x: me.x, y: me.y, vx: -0, vy: -0};
decision_maker.obsKey = '0'; // key of current obstacle
decision_maker.obsData = {} // data of current obstacle
decision_maker.isAlongPath = false; // keep track of if the bot is currently moving along a path
decision_maker.currTargetIndex = 0; // current target on path
decision_maker.path = []; // [[x1, y1], [x2, y2], ...] of path
decision_maker.startedPath = false;
decision_maker.decide = function(){
this.decide_state();
return this.create_target();
}
// Based off current state and step of bot, decide where it should go
decision_maker.create_target = function(){
var curPlayerPos = {};
var target = this.lastPlayerPos;
if (!this.isAlongPath) {
if (this.role.state === 'follow') { // bot should follow around human player
if (!this.human) {
let humanPlayers = Object.keys(tagpro.players).filter(x => {
// return tagpro.players[x].name === 'Player 1'; // Hardcoding that human player has to be named 'Player 1'
return x !== tagpro.playerId;
})
if (humanPlayers.length > 0) {
this.human = tagpro.players[humanPlayers[0]];
this.lastPlayerPos = {
x: this.human.x, y: this.human.y, vx: -0, vy: -0
}; // initialize last and current player position once human player has been found
}
}
if (this.human) {
//get player's current position and compare it to the last recorded position
//if the difference is >= the size of a ball, move the last recorded position and update it as the player's current position
curPlayerPos = {
x: this.human.x, y: this.human.y, vx: -0, vy: -0
};
var dist = Math.sqrt(Math.pow(curPlayerPos.x-this.lastPlayerPos.x, 2) + Math.pow(curPlayerPos.y-this.lastPlayerPos.y, 2));
if (Math.abs(dist) >= 75){
target = this.lastPlayerPos;
this.lastPlayerPos = curPlayerPos;
}
}
} else if (this.role.state === 'Basic Button') {
var x=this.me.x, y=this.me.y;
switch (this.role.step) {
case 0: // bot should go to button1
x = this.obsData.positions.button1[0] * 40;
y = this.obsData.positions.button1[1] * 40;
break;
case 1: // bot should go to button2
x = this.obsData.positions.button2[0] * 40;
y = this.obsData.positions.button2[1] * 40;
break;
case 2: // bot should wait
break;
case 3: // bot should go to goal
x = this.obsData.positions.goal[0] * 40;
y = this.obsData.positions.goal[1] * 40;
break;
default:
break;
}
target = {x: x, y: y, vx: -0, vy: -0};
} else if (this.role.state === 'Trust') {
switch (this.role.step) {
case 0: // bot should go to waiting area
let path = [];
path.push(this.obsData.positions.topPos);
path.push(this.obsData.positions.interPos1);
path.push(this.obsData.positions.waitPos1);
this.startedPath = true;
this.startPath(path);
break;
case 1: // waiting on boost
target = {x: this.me.x, y: this.me.y, vx: -this.human.vx, xy: 0};
break;
case 2: // go to goal
target = {x: this.obsData.positions.goal[0]*40, y: this.obsData.positions.goal[1]*40, vx: -0, vy: -0};
break;
default:
break;
}
} else if (this.role.state === 'Mars') {
var x=this.me.x, y=this.me.y;
switch (this.role.step) {
case 0: // bot should go to waiting area
let path = [];
path.push(this.obsData.positions.waitPos1);
path.push(this.obsData.positions.portal1);
path.push(this.obsData.positions.waitPos2);
this.startedPath = true;
this.startPath(path);
break;
case 1: // stay in place and wait for mars ball
break;
case 2: // bomb!
x = this.obsData.positions.button[0] * 40;
y = this.obsData.positions.button[1] * 40;
break;
case 3:
x = this.obsData.positions.goal[0] * 40 + 160;
y = this.obsData.positions.goal[1] * 40 + 160;
break;
default:
break;
}
target = {x: x, y: y, vx: -0, vy: -0};
} else if (this.role.state === 'Sacrifice') {
switch (this.role.step) {
case 0: // bot should go to waiting area
let path = [];
path.push(this.obsData.positions.interPos1);
path.push(this.obsData.positions.interPos2);
path.push(this.obsData.positions.waitPos1);
this.startedPath = true;
this.startPath(path);
break;
case 1:
let x = this.obsData.positions.bomb[0] * 40;
let y = this.obsData.positions.bomb[1] * 40;
target = {x: x, y: y, vx: -0, vy: -0};
break;
default:
break;
}
}
} else { // move along a path
let isDone = this.moveAlongPath();
if (!isDone) {
var [x, y] = this.path[this.currTargetIndex];
target = {x: x*40, y: y*40, vx: -0, vy: -0};
}
}
// highlights
//debugr.draw_point(target.x, target.y);
return target;
}
// Check if we've entered an obstacle
decision_maker.decide_state = function(){
if (this.role.state === 'follow') {
for (let o in obstacles) {
let obstacle = obstacles[o];
let {x1, y1, x2, y2} = obstacle.dim;
if (this.me.x >= x1 && this.me.x <= x2 && this.me.y >= y1 && this.me.y <= y2) { // We are inside an obstacle
this.obsKey = o;
this.obsData = obstacle;
this.set_state(obstacle.name, 0, false); // set state to be name of the obstacle
return;
}
}
} else {
this.decide_step();
}
}
// Were inside an obstacle. Decide if we've exited, otherwise decide step
decision_maker.decide_step = function(){
let obstacle = obstacles[this.obsKey];
let {x1, y1, x2, y2} = obstacle.dim;
if (this.me.x < x1 || this.me.x > x2 || this.me.y < y1 || this.me.y > y2) { // we're outside obstacle
this.set_state('follow', 0, false);
}
if (this.role.state === 'Basic Button') {
let isHumanPastGate = this.human.y + 50 < this.obsData.positions.gatePos[1] * 40 ? true : false;
let isBotPastGate = this.me.y + 50 < this.obsData.positions.gatePos[1] * 40 ? true : false;
if (isHumanPastGate && isBotPastGate) { // check if both players above gate
this.set_state(this.role.state, 3, false);
} else {
let [i, y] = this.obsData.positions.gatePos;
let isGateOpen = tagpro.map[i][y] === 9.1 ? false : true;
if (!isGateOpen) { // gate is not open
if (!isHumanPastGate && !isBotPastGate) { // human and bot both behind gate
this.set_state(this.role.state, 0, false);
} else if (isBotPastGate && !isHumanPastGate) { // bot ahead, human behind
this.set_state(this.role.state, 1, false);
} else {
this.set_state(this.role.state, 2, false); // bot should wait for human to open gate
}
} else {
if (this.isOnTile(false, this.obsData.positions.button2, 25)) { // human is on button 2
this.set_state(this.role.state, 1, false);
} else if (this.isOnTile(true, this.obsData.positions.button1, 25)) { // bot is on button1, dont move
this.set_state(this.role.state, 2, false);
} else if (this.isOnTile(true, this.obsData.positions.button2, 25)) { // bot is on button2
this.set_state(this.role.state, 2, false);
} else {
this.set_state(this.role.state, 1, false);
}
}
}
} else if (this.role.state === 'Trust') {
if (this.human.flag || this.me.flag && !this.role.step === 2) { // if either player has flag, go to goal
this.set_state(this.role.state, 2, false);
} else if (this.role.step !== 2) {
if (this.role.step === 0) { // start of obstacle, go to waiting spot
if (this.startedPath && !this.isAlongPath) { // finished path, moment of truth
this.startedPath = false;
this.set_state(obstacle.name, 1, false);
}
}
}
} else if (this.role.state === 'Mars') {
if (this.role.step === 0) {
if (this.startedPath && !this.isAlongPath) { // finished path
this.startedPath = false;
this.set_state(obstacle.name, 1, false);
}
} else if (this.role.step === 1) {
let marsBall = tagpro.objects[0];
let x1 = this.obsData.positions.marsRange1[0] * 40;
if (marsBall && marsBall.x > (x1 + 40)) { // check if mars ball is in correct position
this.set_state(obstacle.name, 2, false);
}
} else if (this.role.step === 2) { // wait for human to cross gate
if (this.human.x > this.obsData.positions.marsRange1[0] * 40) {
this.set_state(obstacle.name, 3, false);
}
}
} else if (this.role.state === 'Sacrifice') {
if (this.role.step === 0) {
if (this.startedPath && !this.isAlongPath) { // finished path
this.startedPath = false;
this.set_state(obstacle.name, 1, false);
}
}
}
}
// Helper function to set step, state the bot is in
decision_maker.set_state = function(state, step, role){
this.role.state = state;
this.role.step = step;
this.role.role = role;
}
// decide if a player is sitting on a tile
// params:
// bot: false = check if bot is on tile, true = check if human on tile
// tile: [x, y] of tile
// dist = accuracy of check
decision_maker.isOnTile = function(isBot, tile, dist=3) {
let {x, y} = isBot ? this.me : this.human; // x and y coords of player
let tx = tile[0] * 40;
let ty = tile[1] * 40;
let d = Math.sqrt(Math.pow(tx-x, 2) + Math.pow(ty-y, 2));
return d <= dist;
}
// start a path
decision_maker.startPath = function(path) {
this.path = path;
this.currTargetIndex = 0;
this.isAlongPath = true;
}
// move bot along a path
decision_maker.moveAlongPath = function() {
var dist = 30;
if (this.currTargetIndex == this.path.length - 1) {
dist = 3;
}
let currTarget = this.path[this.currTargetIndex];
if (this.isOnTile(true, currTarget, dist)) {
this.currTargetIndex++;
if (this.currTargetIndex >= this.path.length) { // finished the path!
this.path = [];
this.isAlongPath = false;
this.currTargetIndex = 0;
return true;
}
}
return false;
}
return decision_maker;
}
function run_bot() {
var me = tagpro.players[tagpro.playerId];
//no_draw();
PID_constants = {};
PID_constants.KP = 0.036;
PID_constants.KI = 0.011;
PID_constants.KD = 3.5;
PID_constants.KS = 11;
var controller = create_controller(PID_constants);
var decision_maker = create_decision_maker(me);
setInterval(function() {
// determine a target state
var target = decision_maker.decide();
// execute the control to get to the target
controller.execute(me,target);
// increment step counter
counter++;
}, ctrl_period);
}
function main(fn) {
fn();
}
main(run_bot);
}
// Find and preprocess all obstacles in maze
function preprocess() {
let obstacles = {};
let obstacleStartingPoints = [];
for (let i=0; i<tagpro.map.length; i++) {
for (let y=0; y<tagpro.map[i].length; y++) {
if (tagpro.map[i][y] === 18) {
obstacleStartingPoints.push([i, y]);
}
}
}
for (let point of obstacleStartingPoints) {
let [i, y] = point;
let config = [
tagpro.map[i-1].slice(y-1, y+2).map(x => {return x === 9 ? 1 : 0}),
tagpro.map[i].slice(y-1, y+2).map(x => {return x === 9 ? 1 : 0}),
tagpro.map[i+1].slice(y-1, y+2).map(x => {return x === 9 ? 1 : 0})
]
for (let key in obstacleData) {
let obstcl = obstacleData[key];
var isSame = true;
for (let a=0; a<3; a++) {
for (let b=0; b<3; b++) {
if (config[a][b] !== obstcl.config[a][b]) isSame = false;
}
}
if (isSame) { // found the obstacle type
let obstclData = {};
obstclData.startLoc = point; // start of maze
obstclData.positions = {};
obstclData.dim = {
x1: (i + obstcl.topLeftOffset[0]) * 40, // top left of obstacle
y1: (y + obstcl.topLeftOffset[1]) * 40,
x2: (i + obstcl.botRightOffset[0]) * 40, // bot right of obstacle
y2: (y + obstcl.botRightOffset[1]) * 40
}
obstclData.positions.goal = [point[0] + obstcl.goalOffset[0], point[1] + obstcl.goalOffset[1]];
obstclData.name = obstcl.name;
if (obstclData.name === 'Basic Button') {
obstclData.positions.gatePos = [point[0], point[1] - 8];
obstclData.positions.button1 = [point[0] - 2, point[1] - 5];
obstclData.positions.button2 = [point[0] + 2, point[1] - 11];
} else if (obstclData.name === 'Trust') {
obstclData.positions.waitPos2 = [point[0] + 8, point[1] + 7]; // left square next to boost
obstclData.positions.waitPos1 = [point[0] - 8, point[1] + 7]; // right square next to boost
obstclData.positions.topPos = [point[0], point[1] + 3]; // top of obstacle
obstclData.positions.interPos2 = [point[0] + 7, point[1] + 4]; // top right of obstacle
obstclData.positions.interPos1 = [point[0] - 7, point[1] + 4]; // top left of obstacle
} else if (obstclData.name === 'Mars') {
obstclData.positions.portal1 = [point[0] + 4.2, point[1] - 2.2]; // top portal
obstclData.positions.portal2 = [point[0] + 4.2, point[1] + 2]; // bottom portal
obstclData.positions.waitPos1 = [point[0] + 3, point[1]]; // before portals
obstclData.positions.waitPos2 = [point[0] + 27, point[1]]; // underneath button
obstclData.positions.button = [point[0] + 27, point[1] - 2]; // button
obstclData.positions.marsRange1 = [point[0] + 25, point[1]]; // left most of where mars ball should end up
} else if (obstclData.name === 'Sacrifice') {
obstclData.positions.interPos1 = [point[0] - 3, point[1] - 6]; // path point 1
obstclData.positions.interPos2 = [point[0] - 3, point[1] - 10]; // path point 2
obstclData.positions.waitPos1 = [point[0], point[1] - 11]; // beneath bomb
obstclData.positions.bomb = [point[0], point[1] - 13]; // bomb
}
obstacles[obstcl.name] = obstclData;
break;
}
}
}
return obstacles;
}
tagpro.ready(function() {
let url = 'https://raw.githubusercontent.com/MarksCode/CM146-Final-Project/master/Obstacles.json';
fetch(url)
.then(response =>
response.json().then(data => ({
data: data,
status: response.status
})
).then(res => {
obstacleData = res.data;
waitForId(script);
}));
});
/*********************************/
/* */
/* Stuff we probably won't touch */
/* */
/*********************************/
/**
* All functionality related to steering
*/
function create_controller(PIDconstants){
var controller = {};
controller.output = {};
controller.output.threshold = 0.18;
controller.output.set = function(u,me){
this.horz(u.x,me);
this.vert(u.y,me);
};
controller.output.horz = function(x,me){
// left/right control
if (x > this.threshold) { // go right
tagpro.sendKeyPress('left', true);
tagpro.sendKeyPress('right', false);
me.right = true;
me.left = false;
} else if (x < -this.threshold) { // go left
tagpro.sendKeyPress('right', true);
tagpro.sendKeyPress('left', false);
me.right = false;
me.left = true;
} else { // release both
tagpro.sendKeyPress('right', true);
tagpro.sendKeyPress('left', true);
me.right = false;
me.left = false;
}
}
controller.output.vert = function(y,me){
// up/down control
if (y < -this.threshold) { // go down
tagpro.sendKeyPress('up', true);
tagpro.sendKeyPress('down', false);
me.up = false;
me.down = true;
} else if (y > this.threshold) { // go up
tagpro.sendKeyPress('up', false);
tagpro.sendKeyPress('down', true);
me.up = true;
me.down = false;
} else { // release both
tagpro.sendKeyPress('up', true);
tagpro.sendKeyPress('down', true);
me.up = false;
me.down = false;
}
}
controller.execute = function(me,target){
ctrl_vec = this.PID.get_ctrl_vec(me,target);
this.output.set(ctrl_vec,me);
}
function create_PID(constants){
var PID = {};
PID.dims = {};
PID.dims.list = ['x','y'];
PID.dims.num = PID.dims.list.length;
PID.constants = {};
PID.constants.set_constants = function(constants){
for (var key in constants){
if (constants.hasOwnProperty(key)) {
this[key] = constants[key];
}
}
}
PID.constants.set_constants(constants);
PID.accum_error = {};
PID.reset_accum_error = function(){
PID.accum_error.pos = {'x':0,'y':0};
PID.accum_error.vel = {'x':0,'y':0};
};
PID.reset_accum_error();
PID.errors = {}; // gets set before its read
PID.get_ctrl_vec = function(state_current, state_goal){
this.errors = this.calc_error(state_current,state_goal);
this.accumulate_error(this.errors);
var u = {};
for (var i = 0; i < this.dims.num; i++){
var dim = this.dims.list[i];
u[dim] = this.calc_P(dim) + this.calc_I(dim) + this.calc_D(dim);
}
return u;
};
PID.calc_P = function(axis){
return this.constants.KP*this.errors.pos[axis];
}
PID.calc_I = function(axis){
return this.constants.KI*this.accum_error.pos[axis];
};
PID.calc_D = function(axis){
return this.constants.KD*this.errors.vel[axis];
};
PID.calc_error = function(state_current, state_goal){
var errors = {};
errors.pos = {};
errors.vel = {};
errors.pos.x = state_goal.x - state_current.x;
errors.pos.y = state_current.y - state_goal.y;
errors.vel.x = state_goal.vx - state_current.vx;
errors.vel.y = state_current.vy - state_goal.vy;
return errors;
};
PID.accumulate_error = function(errors){
this.accum_error.pos.x += errors.pos.x;
this.accum_error.pos.y += errors.pos.y;
if (this.accum_error.pos.x > this.constants.KS) this.accum_error.pos.x = this.constants.KS;
if (-this.accum_error.pos.x > this.constants.KS) this.accum_error.pos.x = -this.constants.KS;
if (this.accum_error.pos.y > this.constants.KS) this.accum_error.pos.y = this.constants.KS;
if (-this.accum_error.pos.y > this.constants.KS) this.accum_error.pos.y = -this.constants.KS;
};
return PID;
}
controller.PID = create_PID(PIDconstants);
return controller;
}
// draw a rectange on map at specified location
function create_debugger() {
var debug = {};
var layer = tagpro.renderer.layers.foreground;
var point = new PIXI.Graphics();
layer.addChild(point);
debug.draw_point = function(x, y, size=5) {
point.clear();
point.lineStyle(2, 0xFF0000);
point.drawCircle(x, y, size);
}
return debug;
}
// removes the all tagpro rendering and suppresses errors to
// preserve computational power for actual bot shenanigans
function no_draw() {
window.onerror = function(message, url, lineNumber) {
return true; // prevents browser error messages
};
// reserve bot resources: don't render anything
delete tagpro.renderer;
tagpro.renderer = {};
tagpro.renderer.destroyPlayer = function(e) {
delete tagpro.players[e.id];
return true;
};
tagpro.renderer.drawName = function(e,t) {
return true;
};
tagpro.sound = false;
delete tagpro.sounds;
delete tagpro.soundTiles;
$('html').remove();
}
// Overriding this function to get a more accurate velocity of players.
// Velocity is saved in player.vx and vy.
Box2D.Dynamics.b2Body.prototype.GetLinearVelocity = function() {
tagpro.players[this.player.id].vx = this.m_linearVelocity.x;
tagpro.players[this.player.id].vy = this.m_linearVelocity.y;
return this.m_linearVelocity;
};