forked from kimbring2/AlphaStar_Implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrajectory.py
267 lines (225 loc) · 9.36 KB
/
trajectory.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
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
#!/usr/bin/env python
from pysc2.lib import features, point, actions, units
from pysc2.env.environment import TimeStep, StepType
from pysc2.env import sc2_env, available_actions_printer
from pysc2 import run_configs
from s2clientprotocol import sc2api_pb2 as sc_pb
import units_new
import importlib
import random
import sys
import glob
from absl import app, flags
FLAGS = flags.FLAGS
FLAGS(sys.argv)
class Trajectory(object):
def __init__(self, source, home_race_name, away_race_name, replay_filter, filter_repeated_camera_moves=True):
self.source = source
self.home_race_name = home_race_name
self.away_race_name = away_race_name
self.replay_filter = replay_filter
self.filter_repeated_camera_moves = filter_repeated_camera_moves
self.home_BO = None
self.home_trajectory = []
self.away_BU = None
self.away_trajectory = []
self.home_score_cumulative = None
self.away_score_cumulative = None
def get_BO(self, player):
if player == 0:
return self.home_BO
else:
return self.away_BU
def get_random_trajectory(self):
function_dict = {}
for _FUNCTION in actions._FUNCTIONS:
#print(_FUNCTION)
function_dict[_FUNCTION.ability_id] = _FUNCTION.name
race_list = ['Terran', 'Zerg', 'Protoss']
"""How many agent steps the agent has been trained for."""
run_config = run_configs.get()
sc2_proc = run_config.start()
controller = sc2_proc.controller
#print ("source: {}".format(source))
#root_path = '/media/kimbring2/Steam/StarCraftII/Replays/4.8.2.71663-20190123_035823-1'
root_path = self.source
file_list = glob.glob(root_path + '*.*')
#print ("file_list: {}".format(file_list))
for i in range(0, 500):
#print("i: " + str(i))
replay_file_path = random.choice(file_list)
#print ("replay_file_path: {}".format(replay_file_path))
#replay_file_path = root_path + '0a0f62052fe4311368910ad38c662bf979e292b86ad02b49b41a87013e58c432.SC2Replay'
#replay_file_path = root_path + '/0a1b09abc9e98f4e0c3921ae0a427c27e97c2bbdcf34f50df18dc41cea3f3249.SC2Replay'
#replay_file_path_2 = root_path + '/0a01d32e9a98e1596b88bc2cdec7752249b22aca774e3305dae2e93efef34be3.SC2Replay'
#replay_file_path_0 = human_data
#print ("replay_file_path: {}".format(replay_file_path))
try:
replay_data = run_config.replay_data(replay_file_path)
ping = controller.ping()
info = controller.replay_info(replay_data)
print("ping: " + str(ping))
print("replay_info: " + str(info))
player0_race = info.player_info[0].player_info.race_actual
player0_mmr = info.player_info[0].player_mmr
player0_apm = info.player_info[0].player_apm
player0_result = info.player_info[0].player_result.result
print("player0_race: " + str(player0_race))
print("player0_mmr: " + str(player0_mmr))
print("player0_apm: " + str(player0_apm))
print("player0_result: " + str(player0_result))
home_race = race_list.index(self.home_race_name) + 1
if (home_race == player0_race):
print("player0_race pass")
else:
print("player0_race fail")
continue
if (player0_mmr >= self.replay_filter):
print("player0_mmr pass ")
else:
print("player0_mmr fail")
continue
player1_race = info.player_info[0].player_info.race_actual
player1_mmr = info.player_info[0].player_mmr
player1_apm = info.player_info[0].player_apm
player1_result = info.player_info[0].player_result.result
print("player1_race: " + str(player1_race))
print("player1_mmr: " + str(player1_mmr))
print("player1_apm: " + str(player1_apm))
print("player1_result: " + str(player1_result))
away_race = race_list.index(self.away_race_name) + 1
if (away_race == player1_race):
print("player1_race pass ")
else:
print("player1_race fail ")
continue
if (player1_mmr >= self.replay_filter):
print("player1_mmr pass ")
else:
print("player1_mmr fail")
continue
screen_size_px = (128, 128)
minimap_size_px = (64, 64)
player_id = 1
discount = 1.
step_mul = 8
screen_size_px = point.Point(*screen_size_px)
minimap_size_px = point.Point(*minimap_size_px)
interface = sc_pb.InterfaceOptions(raw=True, score=True,
feature_layer=sc_pb.SpatialCameraSetup(width=24))
screen_size_px.assign_to(interface.feature_layer.resolution)
minimap_size_px.assign_to(interface.feature_layer.minimap_resolution)
map_data = None
if info.local_map_path:
map_data = run_config.map_data(info.local_map_path)
_episode_length = info.game_duration_loops
_episode_steps = 0
controller.start_replay(sc_pb.RequestStartReplay(replay_data=replay_data,
map_data=map_data, options=interface,
observed_player_id=player_id))
_state = StepType.FIRST
if (info.HasField("error") or
info.base_build != ping.base_build or # different game version
info.game_duration_loops < 1000 or
len(info.player_info) != 2):
# Probably corrupt, or just not interesting.
print("error")
continue
feature_screen_size = 128
feature_minimap_size = 64
rgb_screen_size = None
rgb_minimap_size = None
action_space = None
use_feature_units = True
aif = sc2_env.parse_agent_interface_format(
feature_screen=feature_screen_size,
feature_minimap=feature_minimap_size,
rgb_screen=rgb_screen_size,
rgb_minimap=rgb_minimap_size,
action_space=action_space,
use_feature_units=use_feature_units)
_features = features.features_from_game_info(controller.game_info(), agent_interface_format=aif)
build_info = []
build_name = []
replay_step = 0
#print("True loop")
while True:
replay_step += 1
#print("#####################################################")
print("replay_step: " + str(replay_step))
controller.step(step_mul)
obs = controller.observe()
#print("obs.actions: " + str(obs.actions))
#print("len(obs.actions): " + str(len(obs.actions)))
#print("")
#self.home_trajectory.append(obs)
agent_act = None
if (len(obs.actions) != 0):
action = (obs.actions)[0]
action_feature_layer = action.action_feature_layer
unit_command = action_feature_layer.unit_command
ability_id = unit_command.ability_id
function_name = function_dict[ability_id]
#print("action: " + str(action))
#print("action_feature_layer: " + str(action_feature_layer))
#print("unit_command: " + str(unit_command))
#print("ability_id: " + str(ability_id))
#print("function_name: " + str(function_name))
#print("#####################################################")
#print("")
if (function_name != 'build_queue'):
function_name_parse = function_name.split('_')
function_name_first = function_name_parse[0]
#print("function_name_first: " + str(function_name_first))
if (function_name_first == 'Build' or function_name_first == 'Train'):
unit_name = function_name_parse[1]
unit_info = int(units_new.get_unit_type(self.home_race_name, unit_name)[0])
#print("unit_name: " + str(unit_name))
#print("unit_info: " + str(unit_info))
#print("function_name_parse[1]: " + str(function_name_parse[1]))
build_name.append(unit_name)
build_info.append(unit_info)
else:
#print("#####################################################")
print("")
if obs.player_result: # Episide over.
_state = StepType.LAST
discount = 0
else:
discount = discount
_episode_steps += step_mul
agent_obs = _features.transform_obs(obs)
exec_actions = []
for ac in obs.actions:
try:
exec_act = _features.reverse_action(ac)
except ValueError:
exec_act = _features.actions.FunctionCall(0, []) # no_op
exec_actions.append(exec_act)
self.home_trajectory.append([agent_obs, exec_actions])
step = TimeStep(step_type=_state, reward=0,
discount=discount, observation=agent_obs)
score_cumulative = agent_obs['score_cumulative']
score_cumulative_dict = {}
score_cumulative_dict['score'] = score_cumulative.score
score_cumulative_dict['idle_production_time'] = score_cumulative.idle_production_time
score_cumulative_dict['idle_worker_time'] = score_cumulative.idle_worker_time
score_cumulative_dict['total_value_units'] = score_cumulative.total_value_units
score_cumulative_dict['total_value_structures'] = score_cumulative.total_value_structures
score_cumulative_dict['killed_value_units'] = score_cumulative.killed_value_units
score_cumulative_dict['killed_value_structures'] = score_cumulative.killed_value_structures
score_cumulative_dict['collected_minerals'] = score_cumulative.collected_minerals
score_cumulative_dict['collected_vespene'] = score_cumulative.collected_vespene
score_cumulative_dict['collection_rate_minerals'] = score_cumulative.collection_rate_minerals
score_cumulative_dict['collection_rate_vespene'] = score_cumulative.collection_rate_vespene
score_cumulative_dict['spent_minerals'] = score_cumulative.spent_minerals
score_cumulative_dict['spent_vespene'] = score_cumulative.spent_vespene
#print("obs.player_result: " + str(obs.player_result))
if obs.player_result:
break
_state = StepType.MID
self.home_BO = build_info
self.away_BU = score_cumulative_dict
break
except:
continue