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 _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
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() if DEBUG > 0: print("Received state: ", update) nloop += 1 actions = [] if bool(client.state.d['game_ended']): if DEBUG > 0: print("GAME ENDED")