def _reset(self): self.nloop = 0 self.battles_won = 0 self.battles_game = 0 self.actions = [] logging.info('game start') # create a client and connect to the TorchCraft server self.client = torchcraft.Client(self.args.ip, self.args.port) init_info = self.client.connect() logging.debug('received init: %s', init_info) # setup the game setup_list = [ proto.concat_cmd(proto.commands['set_speed'], self.args.speed), proto.concat_cmd(proto.commands['set_gui'], 1), proto.concat_cmd(proto.commands['set_frameskip'], 1), proto.concat_cmd(proto.commands['set_cmd_optim'], 1) ] logging.debug('setting up the game: %s', ':'.join(setup_list)) self.client.send(setup_list) utils.progress(self.nloop, self.battles_won, self.battles_game, self.episodes) self.episodes += 1 return 0
def _reset(self): print "3" utils.print_progress(self.episodes, self.episode_wins) if not self.self_play and self.episode_steps == self.max_episode_steps: self.client.send([proto.concat_cmd(proto.commands['restart'])]) self.client.receive() while not bool(self.client.state.d['game_ended']): self.client.send([]) self.client.receive() self.episodes += 1 self.episode_steps = 0 self.client.close() self.client.connect() setup = [ proto.concat_cmd(proto.commands['set_speed'], self.speed), proto.concat_cmd(proto.commands['set_gui'], 1), proto.concat_cmd(proto.commands['set_frameskip'], self.frame_skip), proto.concat_cmd(proto.commands['set_cmd_optim'], 1) ] self.client.send(setup) self.client.receive() self.state = self.client.state.d self.obs = self._make_observation() self.obs_pre = self.obs return self.obs
def _reset(self): utils.print_progress(self.episodes, self.episode_wins) if not self.self_play and self.episode_steps == self.max_episode_steps: self.client.send([proto.concat_cmd(proto.commands['restart'])]) self.client.receive() while not bool(self.client.state.d['game_ended']): self.client.send([]) self.client.receive() self.episodes += 1 self.episode_steps = 0 self.client.close() self.client.connect() setup = [proto.concat_cmd(proto.commands['set_speed'], self.speed), proto.concat_cmd(proto.commands['set_gui'], 1), proto.concat_cmd(proto.commands['set_frameskip'], self.frame_skip), proto.concat_cmd(proto.commands['set_cmd_optim'], 1)] self.client.send(setup) self.client.receive() self.state = self.client.state.d self.obs = self._make_observation() self.obs_pre = self.obs return self.obs
def getCommand(self, uid): if self.act_type == 1: return proto.concat_cmd(proto.commands['command_unit_protected'], uid, proto.unit_command_types['Attack_Unit'], self.targetID) elif self.act_type == 2: return proto.concat_cmd(proto.commands['command_unit_protected'], uid, proto.unit_command_types['Move'], -1, self.target_pos_x, self.target_pos_y)
def run(self): while self.episodes < self.EPISODES_NUM: if 0 != self._reset(): logging.error('reset failed!') continue while True: update_info = self.client.receive() logging.debug('received state: %s', update_info) self.nloop += 1 self.actions = [] if bool(self.client.state.d['game_ended']): logging.info('game over') break elif self.client.state.d['battle_just_ended']: utils.progress(self.nloop, self.battles_won, self.battles_game, self.episodes) self.battles_game += 1 if bool(self.client.state.d['battle_won']): self.battles_won += 1 if self.battles_game >= self.BATTLES_NUM: self.actions = [ proto.concat_cmd(proto.commands['restart']) ] elif self.client.state.d['waiting_for_restart']: logging.info('waiting for restart') elif 0 != self._execute_strategy(): logging.error('execute strategy failed!') logging.debug('sending actions: %s', str(self.actions)) self.client.send(self.actions) self.client.close() return 0
def _make_commands(self, action): cmds = [] #if self.state is None or not action: # return cmds myself_id = None myself = None enemy_id = None enemy = None for uid, ut in self.state['units_myself'].iteritems(): myself_id = uid myself = ut for uid, ut in self.state['units_enemy'].iteritems(): enemy_id = uid enemy = ut if action[0] > 0: # Attack action if myself is None or enemy is None: return cmds # TODO: compute the enemy id based on its position cmds.append(proto.concat_cmd( proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Attack_Unit'], enemy_id)) print myself_id
def __init__(self): ip = 'localhost' port = '11111' self.action = ['left', 'right', 'up', 'down'] self.client = tc.Client(ip, port) self.ore = 0 self.client.connect() setup = [proto.concat_cmd(proto.commands['set_speed'], 10), proto.concat_cmd(proto.commands['set_gui'], 1), proto.concat_cmd(proto.commands['set_frameskip'], 9), proto.concat_cmd(proto.commands['set_cmd_optim'], 1)] self.client.send(setup) self._wait_unit()
def _execute_strategy(self): for (uid, ut) in self.client.state.d['units_myself'].iteritems(): target_uid = utils.get_weakest_uid( self.client.state.d['units_enemy']) if -1 == target_uid: #logging.warning('can not get the weakest unit!') continue self.actions.append( proto.concat_cmd(proto.commands['command_unit_protected'], uid, proto.unit_command_types['Attack_Unit'], target_uid)) return 0
def _make_commands(self, action): cmds = [] if self.state is None or not action.any(): return cmds myself_id = None myself = None enemy_id = None enemy = None for uid, ut in self.state['units_myself'].iteritems(): myself_id = uid myself = ut for uid, ut in self.state['units_enemy'].iteritems(): enemy_id = uid enemy = ut if action[0] > 0: # Attack action if myself is None or enemy is None: return cmds # TODO: compute the enemy id based on its position cmds.append( proto.concat_cmd(proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Attack_Unit'], enemy_id)) else: # Move action if myself is None or enemy is None: return cmds degree = action[1] * 180 distance = (action[2] + 1) * DISTANCE_FACTOR x2, y2 = utils.get_position(degree, distance, myself.x, -myself.y) cmds.append( proto.concat_cmd(proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Move'], -1, x2, -y2)) return cmds
def _make_commands(self, action): cmds = [] if self.state is None or action is None: return cmds myself_id = None myself = None enemy_id = None enemy = None for uid, ut in self.state['units_myself'].iteritems(): myself_id = uid myself = ut for uid, ut in self.state['units_enemy'].iteritems(): enemy_id = uid enemy = ut if action[0] > 0: # Attack action if myself is None or enemy is None: return cmds # TODO: compute the enemy id based on its position cmds.append(proto.concat_cmd( proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Attack_Unit'], enemy_id)) else: # Move action if myself is None or enemy is None: return cmds degree = action[1] * 180 distance = (action[2] + 1) * DISTANCE_FACTOR x2, y2 = utils.get_position(degree, distance, myself.x, -myself.y) cmds.append(proto.concat_cmd( proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Move'], -1, x2, -y2)) return cmds
def _make_command(self, action): STEP_SIZE = 8 units = self.client.state.d['units_myself'] if not units: return "" uid, ut = units.items()[0] mov_pos = [ut.x, ut.y] if action == 'left': mov_pos[0] -= STEP_SIZE elif action == 'right': mov_pos[0] += STEP_SIZE elif action == 'up': mov_pos[1] -= STEP_SIZE elif action == 'down': mov_pos[1] += STEP_SIZE cmd = [proto.concat_cmd(proto.commands['command_unit'], uid, proto.unit_command_types['Move'], -1, mov_pos[0], mov_pos[1], -1), ] return cmd, mov_pos, uid
def _make_commands(self, action): cmds = [] if self.state is None or action is None: return cmds ''' myself_id = None myself = None enemy_id = None enemy = None for uid, ut in self.state['units_myself'].iteritems(): myself_id = uid myself = ut for uid, ut in self.state['units_enemy'].iteritems(): enemy_id = uid enemy = ut if action[0] > 0: # Attack action if myself is None or enemy is None: return cmds # TODO: compute the enemy id based on its position cmds.append(proto.concat_cmd( proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Attack_Unit'], enemy_id)) else: # Move action if myself is None or enemy is None: return cmds degree = action[1] * 180 distance = (action[2] + 1) * DISTANCE_FACTOR x2, y2 = utils.get_position(degree, distance, myself.x, -myself.y) cmds.append(proto.concat_cmd( proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Move'], -1, x2, -y2)) ''' for i in range(len(action)): uid = int(action[i][0]) attacking = self.state['units_myself'][uid] if action[i][1] > 0: # Attack action if action[i][4] < 0: continue attacked_uid = int(action[i][4]) attacked = self.state['units_enemy'][attacked_uid] if attacking is None or attacked is None: print('attacking or attacked is emety! Please check!') continue cmds.append(proto.concat_cmd(proto.commands['command_unit_protected'], uid, proto.unit_command_types['Attack_Unit'], attacked_uid)) else: # Move action if attacking is None: print('The unit to move is empty, please chaeck!') continue degree = action[i][2] * 180 distance = (action[i][3] + 1) * DISTANCE_FACTOR x2, y2 = utils.get_position(degree, distance, attacking.x, -attacking.y) cmds.append(proto.concat_cmd(proto.commands['command_unit_protected'], uid, proto.unit_command_types['Move'], -1, x2, -y2)) return cmds
nloop = 1 battles_won = 0 battles_game = 0 print "" print "GAME STARTED" # Create a client and connect to the TorchCraft server client = tc.Client(args.ip, args.port) init = client.connect() if DEBUG > 1: print "Received init: " + init # Setup the game setup = [ proto.concat_cmd(proto.commands['set_speed'], 10), proto.concat_cmd(proto.commands['set_gui'], 1), proto.concat_cmd(proto.commands['set_frameskip'], 9), proto.concat_cmd(proto.commands['set_cmd_optim'], 1) ] if DEBUG > 0: print "Setting up the game: " + ':'.join(setup) client.send(setup) utils.progress(nloop, battles_won, battles_game, total_battles) while True: # Print the progress if np.mod(nloop, 50) == 0: utils.progress(nloop, battles_won, battles_game, total_battles) update = client.receive()
while True: nloop = 1 battles_game = 0 print "" print "GAME STARTED" # 连接游戏客户端 client = tc.Client(args.ip, args.port) init = client.connect() if DEBUG > 0: print "Received init: " + init # 设置游戏参数 setup = [ proto.concat_cmd(proto.commands['set_speed'], 0.001), proto.concat_cmd(proto.commands['set_gui'], 1), proto.concat_cmd(proto.commands['set_frameskip'], 9), proto.concat_cmd(proto.commands['set_cmd_optim'], 1) ] if DEBUG > 0: print "Setting up the game: " + ':'.join(setup) client.send(setup) utils.progress(nloop, battles_won, battles_game, total_battles) # 进行初始化游戏 if (raw_input() == "r"): actions = [proto.concat_cmd(proto.commands['restart'])] client.send(actions)
return cmds # TODO: compute the enemy id based on its position cmds.append(proto.concat_cmd( proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Attack_Unit'], enemy_id)) print myself_id else: # Move action if myself is None or enemy is None: return cmds degree = action[1] * 180 distance = (action[2] + 1) * DISTANCE_FACTOR x2, y2 = utils.get_position(degree, distance, myself.x, -myself.y) print myself_id cmds.append(proto.concat_cmd( proto.commands['command_unit_protected'], myself_id, proto.unit_command_types['Move'], -1, x2, -y2)) return cmds def _make_observation(self): myself = None enemy = None for uid, ut in self.state['units_myself'].iteritems(): myself = ut for uid, ut in self.state['units_enemy'].iteritems(): enemy = ut obs = np.zeros(self.observation_space.shape) if myself is not None and enemy is not None:
nloop = 1 battles_won = 0 battles_game = 0 print("") print("GAME STARTED") # Create a client and connect to the TorchCraft server client = tc.Client(args.ip, args.port) init = client.connect() if DEBUG > 0: print("Received init: ", init) # Setup the game setup = [ proto.concat_cmd(proto.commands['set_speed'], 60), proto.concat_cmd(proto.commands['set_gui'], 1), proto.concat_cmd(proto.commands['set_frameskip'], 9), proto.concat_cmd(proto.commands['set_cmd_optim'], 1) ] if DEBUG > 0: print("Setting up the game: " + ':'.join(setup)) client.send(setup) utils.progress(nloop, battles_won, battles_game, total_battles) while True: # Print the progress if np.mod(nloop, 50) == 0: utils.progress(nloop, battles_won, battles_game, total_battles) update = client.receive()
def _make_commands(self, action): cmds = [] #if self.state is None or not action: # return cmds myself_id = [] myself = [] enemy_id = [] enemy = [] i = 0 j = 0 for uid, ut in self.state['units_myself'].iteritems(): myself_id.append(uid) myself.append(ut) #print "uid:" + str(uid) + ' ut: ' + str(ut) i += 1 for uid, ut in self.state['units_enemy'].iteritems(): enemy_id.append(uid) enemy.append(ut) j += 1 if action[0] > 0: # Attack action if myself is None or enemy is None: return cmds # TODO: compute the enemy id based on its position for a in range(0, i, 2): for b in range(j): cmds.append( proto.concat_cmd( proto.commands['command_unit_protected'], myself_id[a], proto.unit_command_types['Attack_Unit'], enemy_id[b])) if a < i - 1: cmds.append( proto.concat_cmd( proto.commands['command_unit_protected'], myself_id[a + 1], proto.unit_command_types['Attack_Unit'], enemy_id[b])) j -= 1 else: # Move action if myself is None or enemy is None: return cmds for a in range(0, i, 2): degree = action[1] * 180 distance = (action[2] + 1) * DISTANCE_FACTOR x2, y2 = utils.get_position(degree, distance, myself[a].x, -myself[a].y) cmds.append( proto.concat_cmd(proto.commands['command_unit_protected'], myself_id[a], proto.unit_command_types['Move'], -1, x2, -y2)) if a < i - 1: degree = action[1] * 180 distance = (action[2] + 1) * DISTANCE_FACTOR x2, y2 = utils.get_position(degree, distance, myself[a + 1].x, -myself[a + 1].y) cmds.append( proto.concat_cmd( proto.commands['command_unit_protected'], myself_id[a + 1], proto.unit_command_types['Move'], -1, x2, -y2)) j -= 1 print cmds return cmds