Exemple #1
0
class GameEnvironment:
    def __init__(self, ip, port, api_token,
                 need_human_ob=False,
                 max_containable_step=10,
                 logfile_path='./',
                 debug=False):
        self.grpc_client = GrpcClient(ip, port, api_token, logfile_path, debug)
        self.need_human_ob = need_human_ob
        self.max_containable_step = max_containable_step
        self._frame_period = self.grpc_client.get_system_info().frame_period
        self._last_action_frame = 0
        self._refresh_obs = RefreshObservation(self.grpc_client, need_human_ob)
        self._action_space = None
        self._reincarnation_flag = True

    def create_room(self, password):
        rsp = self.grpc_client.create_room(password)
        if rsp.err_code == 0:
            self._refresh_obs.start()
            return rsp.room_id, rsp.state
        else:
            return None

    def join_room(self, room_id, password):
        rsp = self.grpc_client.join_room(room_id, password)
        if rsp.err_code == 0:
            self._refresh_obs.start()
            return rsp.state
        else:
            return None

    def get_action_space(self):
        if self._action_space is None:
            self._action_space = self.grpc_client.get_action_space()
        return self._action_space.move, self._action_space.swing, self._action_space.fire, self._action_space.apply

    def get_inform(self):
        inform = self.grpc_client.get_inform()
        return inform.score, inform.kills, inform.health, inform.state, inform.frame_index

    def get_frame_period(self):
        return self._frame_period

    def submit_reincarnation(self):
        rsp = self.grpc_client.submit_reincarnation()
        if rsp.err_code == 0:
            return rsp.state
        else:
            return None

    def submit_action(self, frame, move, swing, fire, apply):
        """read"""
        frame_lock.acquire_read()
        current_frame = frame_index
        frame_lock.release()
        if self._last_action_frame < frame and frame > current_frame - self.max_containable_step:
            rsp = self.grpc_client.submit_action(move, swing, fire, apply)
            if rsp.err_code == 0:
                self._last_action_frame = frame
                return rsp.state
            else:
                return None
        else:
            return None

    def get_observation_with_info(self):
        """read"""
        observation_lock.acquire_read()
        state = observation_state
        location = location_observation
        immutable_element = immutable_element_observation
        mutable_element = mutable_element_observation
        bodies = bodies_observation
        asset_ownership = asset_ownership_observation
        self_asset = self_asset_observation
        self_status = self_status_observation
        pointer = pointer_observation
        human = human_observation
        observation_lock.release()

        frame_lock.acquire_read()
        frame = frame_index
        frame_lock.release()

        inform_lock.acquire_read()
        score = score_inform
        kill = kill_inform
        heath = heath_inform
        inform_lock.release()

        if self.need_human_ob:
            return frame, \
                   state, \
                   location, \
                   immutable_element, \
                   mutable_element, \
                   bodies, \
                   asset_ownership, \
                   self_asset, \
                   self_status, \
                   pointer, \
                   human, \
                   score, \
                   kill, \
                   heath

        else:
            return frame, \
                   state, \
                   location, \
                   immutable_element, \
                   mutable_element, \
                   bodies, \
                   asset_ownership, \
                   self_asset, \
                   self_status, \
                   pointer, \
                   score, \
                   kill, \
                   heath

    def get_move_meanings(self):
        return [MOVE_MEANING[i] for i in self._action_space.move]

    def show_image(self, image, height=100, width=200):
        plt.close('all')
        image_array = np.array(image).reshape(height, width)
        plt.imshow(image_array, cmap='gray')
        plt.show()

    @staticmethod
    def get_state_meanings():
        return STATE_MEANING
Exemple #2
0
class GameEnvironment:
    def __init__(self,
                 ip,
                 port,
                 api_token,
                 need_human_ob=False,
                 max_containable_step=10,
                 logfile_path='./',
                 debug=False):
        self.grpc_client = GrpcClient(ip, port, api_token, logfile_path, debug)
        self._check_frame = CheckFrame(self.grpc_client, need_human_ob)
        self.need_human_ob = need_human_ob
        self.last_action_frame = 0
        self.max_containable_step = max_containable_step
        self._action_space = None
        self._reincarnation_flag = True

    def create_room(self, password):
        rsp = self.grpc_client.create_room(password)
        if rsp.err_code == 0:
            self._check_frame.start()
            return rsp.room_id, rsp.state
        else:
            return None

    def join_room(self, room_id, password):
        rsp = self.grpc_client.join_room(room_id, password)
        if rsp.err_code == 0:
            self._check_frame.start()
            return rsp.state
        else:
            return None

    def get_action_space(self):
        if self._action_space is None:
            self._action_space = self.grpc_client.get_action_space()
        return self._action_space.move, self._action_space.swing, self._action_space.fire, self._action_space.apply

    def get_inform(self):
        inform = self.grpc_client.get_inform()
        return inform.score, inform.kills, inform.health, inform.state, inform.frame_index

    def submit_reincarnation(self):
        if self._reincarnation_flag:
            rsp = self.grpc_client.submit_reincarnation()
            if rsp.err_code == 0:
                self._reincarnation_flag = False
                return rsp.state
            else:
                return None
        else:
            return None

    def submit_action(self, frame, move, swing, fire, apply):
        """read"""
        frame_lock.acquire_read()
        current_frame = frame
        frame_lock.release()
        if self.last_action_frame < frame and frame > current_frame - self.max_containable_step:
            rsp = self.grpc_client.submit_action(move, swing, fire, apply)
            if rsp.err_code == 0:
                self.last_action_frame = frame
                return rsp.state
            else:
                return None
        else:
            return None

    def get_observation_with_info(self):
        """read"""
        observation_lock.acquire_read()
        state = observation_state
        location = location_observation
        immutable_element = immutable_element_observation
        mutable_element = mutable_element_observation
        bodies = bodies_observation
        asset_ownership = asset_ownership_observation
        self_asset = self_asset_observation
        asset_status = asset_status_observation
        pointer = pointer_observation
        human = human_observation
        observation_lock.release()

        frame_lock.acquire_read()
        frame = frame_index
        frame_lock.release()

        inform_lock.acquire_read()
        score = score_inform
        kill = kill_inform
        heath = heath_inform
        inform_lock.release()

        if state == api.in_game and self._reincarnation_flag is False:
            self._reincarnation_flag = True

        if self.need_human_ob:
            return frame, \
                   state, \
                   location, \
                   immutable_element, \
                   mutable_element, \
                   bodies, \
                   asset_ownership, \
                   self_asset, \
                   asset_status, \
                   pointer, \
                   human, \
                   score, \
                   kill, \
                   heath

        else:
            return frame, \
                   state, \
                   location, \
                   immutable_element, \
                   mutable_element, \
                   bodies, \
                   asset_ownership, \
                   self_asset, \
                   asset_status, \
                   pointer, \
                   score, \
                   kill, \
                   heath

    def get_move_meanings(self):
        return [MOVE_MEANING[i] for i in self._action_space.move]

    @staticmethod
    def get_state_meanings():
        return STATE_MEANING