-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoffensive.js
385 lines (343 loc) · 6.61 KB
/
offensive.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
'use strict';
var Client = require('battle/commander.js').Client;
var ROLE = require("battle/terms.js").ROLE;
var client = new Client();
var _ = {
askMyInfo: client.askMyInfo(),
logs: [],
pathHistory: [],
}
function whenIdle() {
if (isReady()) {
if (me('level') >= 2) {
timeout(1).then(whenReady) // to avoid the latest idle case
} else {
whenReady()
}
} else {
client.whenIdle().then(whenIdle)
}
}
function isReady() {
if (client.askCurTime() < 10) return // avoid init only 1 client then ready
/* not use === 'idle' is because lastest one just idle, but other bot alreay moved
* then idle bot will wait unitl other bot attacking, state will change to others
* so this idle bot can go next step
*/
return team()
.every( item => item.state.action !== 'move')
}
function printLogs() {
if (_.logs.length) {
console.log.apply(null, _.logs.splice(0))
}
}
function timeout(sec) {
if (me('level') < 2) {
var pos = me('coordinates')
var x = pos[0]
var y = pos[1]
var r = sec * me('speed') / 1.5
if (x < 30 && y > 20) {
y -= r
} else if (x > 30 && y > 20) {
x -= r
} else if (x > 30 && y < 20) {
x -= r
} else if (x < 30 && y < 20) {
y += r
}
client.doMoves([ [x,y], pos ]) // don't use go(), this is fake move
return client.whenIdle()
} else {
return client.whenTime(client.askCurTime() + sec)
}
}
function back() {
var rocket = team('rocketBot')
if (rocket) {
client.doMove(rocket.coordinates)
_.logs.push('back to rockets')
} else {
var last2me = p2p(_.pathHistory.pop(), me('coordinates'))
_.logs.push('back to last pos')
client.doMove(last2me.fireCoordinates)
}
printLogs()
}
function go(path) {
_.pathHistory = _.pathHistory.concat(path)
client.doMoves(path)
}
function p2p(from, to, r) {
if (!from) {
from = me('coordinates')
}
if (!to) {
to = client.askNearestEnemy(_.enemies).coordinates
}
if (!r) {
r = me('firing_range')
}
var x = from[0] - to[0]
var y = from[1] - to[1]
var z = pythagorean(x, y)
var fireDistance = z - r
var scale = z / fireDistance
return {
fireCoordinates: [
from[0] - x / scale,
from[1] - y / scale,
],
distance: z,
fireDistance: fireDistance,
}
}
function pythagorean(x, y) {
return Math.pow(
x*x + y*y,
0.5
)
}
function whenReady() {
_.logs.push('I am ' + me('type'))
var cmds = _[me('type')]
if (!cmds.length) return
var cmd = cmds.shift()
_.logs.push('CMD: ' + cmd)
if (Array.isArray(cmd)) {
go(cmd)
client.whenIdle().then(whenReady)
} else {
if (cmd === 'auto') {
cmds.push(cmd)
}
var target = findTarget()
switch (me('type')) {
case 'heavyBot':
attack(target)
break
case 'infantryBot':
case 'rocketBot':
waitThenAttack(target)
break
}
client.whenItemDestroyed(target.id).then(whenReady)
}
}
function waitThenAttack(target) {
if (unSafe(me('coordinates')).length) {
_.logs.push('unsafe')
return attack(target)
}
if (target.type === 'commandCenter') {
return attack(target)
}
var slow = team('heavyBot')
if (!slow && me('type') === 'rocketBot') {
slow = team('infantryBot')
}
if (!slow) {
_.logs.push('nobody slower than me')
return attack(target)
}
var slow2target = p2p(slow.coordinates,target.coordinates)
var hurtLen = slow2target.distance - target.firing_range / 2
var myLen = p2p(null, target.coordinates).fireDistance
var wait = hurtLen / slow.speed - myLen / me('speed')
_.logs.push('wait ' + wait + 's')
printLogs()
if (wait < 0) {
attack(target)
} else {
timeout(wait).then(
() => {
attack(target)
}
)
}
}
function me(attr) {
if (attr === 'coordinates') {
_.askMyInfo = client.askMyInfo()
}
return _.askMyInfo[attr]
}
function team(type) {
if (type) {
var output
client.askMyItems().some(
item => {
if (item.type === type) {
output = item
return true
}
}
)
return output
} else {
return client.askMyItems()
}
}
function info(id) {
return client.askItemInfo(id)
}
function position(path) {
var x = {
min: 20,
max: 40,
}
var y = {
min: 0,
max: 40,
}
if (path === 'LR') {
path = me('coordinates')[1] > y.max / 2 ? 'LT' : 'RT'
}
switch (path) {
case 'C':
path = [
[1, 0.5],
]
break
case 'CC':
path = [
[1, 0.5],
[0.5, 0.5],
]
break
case 'CT':
path = [
[1, 0.5],
[0, 0.5],
]
break
case 'L':
path = [
[1, 1],
]
break
case 'LC':
path = [
[1, 1],
[0.5, 1],
]
break
case 'LT':
var pos = client.askTowers().reduce(
(pos, tower) => {
var range = tower.coordinates[1] + tower.firing_range + 1
if (range > pos[1]) {
return [tower.coordinates[0], range]
} else {
return pos
}
},
[x.min, y.min]
)
path = [
[x.max, pos[1]],
pos,
]
return path
case 'R':
path = [
[1, 0],
]
break
case 'RC':
path = [
[1, 0],
[0.5, 0],
]
break
case 'RT':
var pos = client.askTowers().reduce(
(pos, tower) => {
var range = tower.coordinates[1] - tower.firing_range - 1
if (range < pos[1]) {
return [tower.coordinates[0], range]
} else {
return pos
}
},
[x.min, y.max]
)
path = [
[x.max, pos[1]],
pos,
]
return path
}
if (Array.isArray(path)) {
path = path.map(
pos => {
return [
x.min + (x.max - x.min) * pos[0],
y.min + (y.max - y.min) * pos[1],
]
}
)
}
if (!path) {
path = [me('coordinates')]
}
return path
}
function unSafe(coordinates) {
return client.askTowers().filter(
tower => {
var pos2tower = p2p(coordinates, tower.coordinates)
return pos2tower.distance <= tower.firing_range
}
).sort(
(a, b) => {
var priority = ['rocketGun', 'machineGun', 'sentryGun']
return priority.indexOf(a.type) - priority.indexOf(b.type)
}
)
}
function findTarget() {
var target = client.askNearestEnemy( _.enemies)
var rocketRange = 8
var me2target = p2p(null, null, rocketRange)
var towers = unSafe(me2target.fireCoordinates)
if (
towers.length &&
towers[0].type !== 'sentryGun'
) {
target = towers[0]
console.log('!!!!', target.type)
}
return target
}
function attack(target) {
if (info(target.id).is_dead) {
_.logs.push(target.type + target.id + ' is dead')
printLogs()
client.whenIdle().then(whenReady)
} else {
_.logs.push('attack ' + target.type + target.id)
printLogs()
client.doAttack(target.id)
}
}
_.enemies = [
ROLE.CENTER,
ROLE.TOWER,
//ROLE.UNIT,
//ROLE.BUILDING,
//ROLE.OBSTACLE,
//ROLE.ALL,
]
_.heavyBot = [
'auto',
]
_.infantryBot = [
'auto',
]
_.rocketBot = [
'auto',
]
go(position('C')) // this will effect isReady(), don't put in cmds
client.whenIdle().then(whenIdle)