-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_bot.py
More file actions
86 lines (66 loc) · 2.58 KB
/
example_bot.py
File metadata and controls
86 lines (66 loc) · 2.58 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
import json
import sys
import traceback
import numpy as np
from library import Bot
RESOURCE_TYPES = [None, None, None, "wood", "metal"]
class AIBot(Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.gather_assignments = {}
def on_attack_start(self):
self.log("attack start!")
for unit in self.myunits:
if unit.attack_range > 0:
nearby = sorted(unit.units_within(unit.attack_range, lambda u: u.owner != self.player_id),
key=unit.dist_to)
for enemy in nearby:
unit.attack_unit(enemy)
break
self.end_attack()
def on_move_start(self):
self.log("move start!")
for unit in self.myunits:
if unit.collect_amount > 0:
# print(unit, self.gather_assignments, file=sys.stderr)
if unit not in self.gather_assignments:
self.gather_assignments[unit] = min(self.balance, key=self.balance.get)
for position in unit.nearest_nodes():
# print(self.map[tuple(position)], file=sys.stderr)
if self.map[tuple(position)] == RESOURCE_TYPES.index(self.gather_assignments[unit]):
unit.move(position)
break
elif unit.attack > 0:
for enemy in sorted(self.enemyunits,
key=lambda e: np.hypot(*(np.array(unit.position) - np.array(e.position)))):
unit.move(enemy.position)
break
self.end_move()
def on_collect_start(self):
self.log("collect start!", len(self.units))
for unit in self.myunits:
if unit.collect_amount > 0:
if self.map[tuple(unit.position)] in (3, 4):
unit.collect()
self.end_collect()
def on_spawn_start(self):
# self.log("spawn start!")
# self.log(self.costs)
print(self.balance, file=sys.stderr)
for utype, cost in self.costs.items():
if all(self.balance[x] - y >= 0 for x, y in cost.items()):
print(f"Spawning unit {utype}", file=sys.stderr)
self.create_unit(utype)
self.end_spawn()
# try:
# aib = AIBot(logging_events=('in', 'out'))
# aib.run()
# except Exception as e:
# open(f'asdasd-{aib.player_id}.out', 'w').write(str(e))
try:
bot = AIBot(logging_events=('in', 'out'))
bot.run()
except Exception:
traceback.print_exc()
finally:
bot.sock.close()