-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
269 lines (239 loc) · 6.4 KB
/
bot.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
Object.getOwnPropertyNames(Math).forEach(k => global[k] = Math[k]);
let map;
module.exports = (function (port, username, token) {
let ids = {};
let url = 'http://localhost:'+port+'?role=player&username='+username+'&token='+token;
let socket = require('socket.io-client')(url);
let playing = false;
socket.on('map', _map => {
map = _map;
if (typeof map[0] === 'string')
map = map.map(row => row.split(''));
height = map.length;
width = map[0].length;
});
socket.on('_scoreboard', players => players.forEach(player => ids[player.id] = player.username))
socket.on('field', (data) => {
let field = parseField(data, map, username);
if (playing && field.me)
tick(field);
});
function toArrayBuffer(buf) {
var ab = new ArrayBuffer(buf.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buf.length; ++i) {
view[i] = buf[i];
}
return ab;
}
function parseField(data, map) {
let d = new DataView(toArrayBuffer(data));
let time = d.getUint16(0, true);
let bonus = d.getUint16(2, true);
let playersCount = d.getUint8(4);
let O = {
width,
height,
players: [],
me: null,
enemies: [],
bonus: v(bonus),
time: time
};
let offset = 5;
for (let i = 0; i < playersCount; ++i) {
let id = d.getUint8(offset++),
dir = d.getUint8(offset++),
len = d.getUint8(offset++);
let snake = new Array(len);
for (let j = 0; j < len; ++j) {
snake[j] = v(d.getUint16(offset, true));
offset += 2;
}
let pO = {
id,
username: ids[id] || '...',
direction: dir,
length: len,
snake
};
pO.x = snake[len-1].x;
pO.y = snake[len-1].y;
if (pO.username == username)
O.me = pO;
else
O.enemies.push(pO);
O.players.push(pO);
}
O.rawMap = map;
O.map = generateMap(map, O);
return O;
}
function generateMap(map, o) {
let newMap = new Array(map.length);
for (let y = 0; y < map.length; ++y) {
newMap[y] = new Array(map[y].length);
for (let x = 0; x < map[y].length; ++x)
newMap[y][x] = map[y][x];
}
newMap[o.bonus.y][o.bonus.x] = 'b';
for (let player of o.players) {
for (let s of player.snake) {
newMap[s.y][s.x] = o.me&&o.me.id==player.id ? 'm' : 'e';
}
}
return newMap;
}
function v(u) {
return {
x: u % width,
y: (u - (u % width)) / width
};
}
socket.on('disconnect', () => {
playing = false;
});
function tick(field) {
// if (typeof onTick !== 'function') {
// if (!compile()) {
// stop();
// return;
// }
// }
let result;
try {
result = onTick(field)
} catch (e) {
// bodjo.showError(e);
// stop();
return;
}
if (!Number.isInteger(result) ||
result < 0 || result > 4) {
// bodjo.showError('function should return an integer in range [0, 3] \n(');
// stop();
return;
}
socket.emit('turn', result);
}
playing = true;
socket.emit('join');
});
// == bot logic ==
const UP = 0;
const RIGHT = 1;
const DOWN = 2;
const LEFT = 3;
function matrix(width, height, def) {
return Array.from({length:height},()=>Array.from({length:width},()=>def))
}
function move(o, d, field) {
let x = o.x, y = o.y;
switch (d) {
case UP:
if (y <= 0) return {x, y: field.height - 1};
return {x, y: y - 1};
case LEFT:
if (x <= 0) return {x: field.width - 1, y};
return {x: x - 1, y};
case DOWN:
if (y >= field.height-1) return {x, y: 0};
return {x, y: y + 1};
case RIGHT:
if (x >= field.width-1) return {x: 0, y};
return {x: x + 1, y};
default:
return {x, y};
}
}
function can(field, x, y) {
return (field.map[y][x] == ' ' || field.map[y][x] == 'b');
}
function canO(field, o) {
return (field.map[o.y][o.x] == ' ' || field.map[o.y][o.x] == 'b');
}
function onTick(field) {
let M = matrix(field.width, field.height, -1);
let cells = [], i = 0, nx, ny;
cells.push(field.me);
while (cells.length > 0) {
let f = false, ncells = [];
if (i > 15) break;
for (let cell of cells) {
M[cell.y][cell.x] = i;
//ctx.fillText(i, cell.x / field.width * canvas.width,
// (cell.y + 0.25) / field.height * canvas.height);
if (cell.x == field.bonus.x &&
cell.y == field.bonus.y) {
f = true;
break;
}
// up
ny = cell.y == 0 ? (field.height-1) : cell.y-1;
if (M[ny][cell.x] == -1 && can(field, cell.x, ny))
ncells.push({x: cell.x, y: ny});
// down
ny = cell.y == (field.height-1) ? 0 : cell.y+1;
if (M[ny][cell.x] == -1 && can(field, cell.x, ny))
ncells.push({x: cell.x, y: ny});
// left
nx = cell.x == 0 ? (field.width-1) : cell.x-1;
if (M[cell.y][nx] == -1 && can(field, nx, cell.y))
ncells.push({x: nx, y: cell.y});
// right
nx = cell.x == (field.width-1) ? 0 : cell.x+1;
if (M[cell.y][nx] == -1 && can(field, nx, cell.y))
ncells.push({x: nx, y: cell.y});
}
if (f)
break;
cells = ncells;
i++;
}
let dist = M[field.bonus.y][field.bonus.x];
if (dist == -1) {
if (canO(field, move(field.me, UP, field)))
return UP;
if (canO(field, move(field.me, DOWN, field)))
return DOWN;
if (canO(field, move(field.me, LEFT, field)))
return LEFT;
if (canO(field, move(field.me, RIGHT, field)))
return RIGHT;
}
let path = [], x = field.bonus.x, y = field.bonus.y, d;
i = dist;
while (!(x == field.me.x && y == field.me.y)) {
path.push({x, y});
// up
ny = y == 0 ? (field.height-1) : y-1;
if (M[ny][x] == i-1) {
y = ny; i--;
d = DOWN;
continue;
}
// down
ny = y == (field.height-1) ? 0 : y+1;
if (M[ny][x] == i-1) {
y = ny; i--;
d = UP;
continue;
}
// left
nx = x == 0 ? (field.width-1) : x-1;
if (M[y][nx] == i-1) {
x = nx; i--;
d = RIGHT;
continue;
}
// right
nx = x == (field.width-1) ? 0 : x+1;
if (M[y][nx] == i-1) {
x = nx; i--;
d = LEFT;
continue;
}
break;
}
return d;
};