-
Notifications
You must be signed in to change notification settings - Fork 1
/
codecraft.py
169 lines (144 loc) · 5.77 KB
/
codecraft.py
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
import requests
import logging
import time
import numpy as np
RETRIES = 100
def create_game(game_length: int = None, action_delay: int = 0, self_play: bool = False, custom_map=None) -> int:
if custom_map is None:
custom_map = ''
try:
scripted_opponent = 'false' if self_play else 'true'
if game_length:
response = requests.post(f'http://localhost:9000/start-game'
f'?maxTicks={game_length}'
f'&actionDelay={action_delay}'
f'&scriptedOpponent={scripted_opponent}',
json=custom_map).json()
else:
response = requests.post(f'http://localhost:9000/start-game?actionDelay={action_delay}').json()
return int(response['id'])
except requests.exceptions.ConnectionError:
logging.info(f"Connection error on create_game, retrying")
time.sleep(1)
return create_game(game_length, action_delay, self_play)
def act(game_id: int, action):
retries = 100
while retries > 0:
try:
requests.post(f'http://localhost:9000/act?gameID={game_id}&playerID=0', json=action)
return
except requests.exceptions.ConnectionError:
# For some reason, a small percentage of requests fails with
# "connection error (errno 98, address already in use)"
# Just retry
retries -= 1
logging.info(f"Connection error on act({game_id}), retrying")
time.sleep(1)
def act_batch(actions, disable_harvest: bool = False):
payload = {}
for (game_id, player_id, move, turn, buildSpec, harvest) in actions:
action = {
"buildDrone": buildSpec,
"move": move,
"harvest": not disable_harvest,#harvest,
"transfer": False,
"turn": turn,
}
payload[f'{game_id}.{player_id}'] = action
retries = 100
while retries > 0:
try:
requests.post(f'http://localhost:9000/batch-act', json=payload)
return
except requests.exceptions.ConnectionError:
# For some reason, a small percentage of requests fails with
# "connection error (errno 98, address already in use)"
# Just retry
retries -= 1
logging.info(f"Connection error on act_batch(), retrying")
time.sleep(1)
def observe(game_id: int, player_id: int = 0):
try:
return requests.get(f'http://localhost:9000/observation?gameID={game_id}&playerID={player_id}').json()
except requests.exceptions.ConnectionError:
logging.info(f"Connection error on observe({game_id}.{player_id}), retrying")
time.sleep(1)
return observe(game_id, player_id)
def observe_batch(game_ids):
retries = RETRIES
while retries > 0:
try:
return requests.get(f'http://localhost:9000/batch-observation', json=game_ids).json()
except requests.exceptions.ConnectionError:
retries -= 1
logging.info(f"Connection error on observe_batch(), retrying")
time.sleep(10)
def observe_batch_raw(game_ids):
retries = RETRIES
while retries > 0:
try:
response = requests.get(f'http://localhost:9000/batch-observation?json=false',
json=game_ids,
stream=True)
response_bytes = response.content
return np.frombuffer(response_bytes, dtype=np.float32)
except requests.exceptions.ConnectionError:
retries -= 1
logging.info(f"Connection error on observe_batch(), retrying")
time.sleep(10)
def one_hot_to_action(action):
# 0-5: turn/movement (4 is no turn, no movement)
# 6: build [0,1,0,0,0] drone (if minerals > 5)
# 7: harvest
move = False
harvest = False
turn = 0
build = []
if action == 0 or action == 1 or action == 2:
move = True
if action == 0 or action == 3:
turn = -1
if action == 2 or action == 5:
turn = 1
if action == 6:
build = [[0, 1, 0, 0, 0]]
if action == 7:
harvest = True
return {
"buildDrone": build,
"move": move,
"harvest": True,
"transfer": False,
"turn": turn,
}
def observation_to_np(observation):
o = []
x = float(observation['alliedDrones'][0]['xPos'])
y = float(observation['alliedDrones'][0]['yPos'])
o.append(x / 1000.0)
o.append(y / 1000.0)
o.append(np.sin(float(observation['alliedDrones'][0]['orientation'])))
o.append(np.cos(float(observation['alliedDrones'][0]['orientation'])))
o.append(float(observation['alliedDrones'][0]['storedResources']) / 50.0)
o.append(1.0 if observation['alliedDrones'][0]['isConstructing'] else -1.0)
o.append(1.0 if observation['alliedDrones'][0]['isHarvesting'] else -1.0)
minerals = sorted(observation['minerals'], key=lambda m: dist2(m['xPos'], m['yPos'], x, y))
for m in range(0, 10):
if m < len(minerals):
mx = float(minerals[m]['xPos'])
my = float(minerals[m]['yPos'])
o.append((mx - x) / 1000.0)
o.append((my - y) / 1000.0)
o.append(dist(mx, my, x, y) / 1000.0)
o.append(float(minerals[m]['size'] / 100.0))
else:
o.extend([0.0, 0.0, 0.0, 0.0])
return np.array(o, dtype=np.float32)
def dist(x1, y1, x2, y2):
dx = x1 - x2
dy = y1 - y2
return np.sqrt(dx * dx + dy * dy)
def dist2(x1, y1, x2, y2):
dx = x1 - x2
dy = y1 - y2
return dx * dx + dy * dy