Ejemplo n.º 1
0
def noop_board_sequence(obs, length):

    """
    Simulate the sequence of boards, assuming agents stay unmoved
    """

    model = ForwardModel()

    # Dummy objects
    actions = [constants.Action.Stop.value] * 4  # agents stay unmoved
    curr_agents = list()  # empty list of Bombers
    curr_items = dict()  # we never know hidden items

    # Prepare initial state
    curr_board = obs["board"]
    
    curr_bombs = list()
    rows, cols = np.where(obs["bomb_life"] > 0)
    for row, col in zip(rows, cols):
        bomber = characters.Bomber()  # dummy owner of the bomb
        position = (row, col)
        life = int(obs["bomb_life"][row][col])
        blast_strength = int(obs["bomb_blast_strength"][row][col])
        moving_direction = None  # TODO: this may be known
        bomb = characters.Bomb(bomber,
                               position,
                               life,
                               blast_strength,
                               moving_direction)
        curr_bombs.append(bomb)
        # overwrite bomb over agent if they overlap
        curr_board[position] = constants.Item.Bomb.value
    
    curr_flames = list()
    rows, cols = np.where(obs["board"] == constants.Item.Flames.value)
    for row, col in zip(rows, cols):
        position = (row, col)
        life = None  # TODO: this may be known
        if life is not None:
            flame = characters.Flame(position, life)
        else:
            flame = characters.Flame(position)
        curr_flames.append(flame)

    # Simulate
    list_boards = [curr_board.copy()]
    for _ in range(length):
        curr_board, _, curr_bombs, _, curr_flames = model.step(actions,
                                                               curr_board,
                                                               curr_agents,
                                                               curr_bombs,
                                                               curr_items,
                                                               curr_flames)
        list_boards.append(curr_board.copy())

    return list_boards
Ejemplo n.º 2
0
 def convert_flames(self, board):
     #Assuming that for each flame object, its life span is 2 ticks
     ret = []
     locations = np.where(board == 4)
     for r, c in zip(locations[0], locations[1]):
         ret.append(characters.Flame((r, c)))
     return ret
Ejemplo n.º 3
0
    def set_json_info(self):
        """Sets the game state as the init_game_state."""
        board_size = int(self._init_game_state['board_size'])
        self._board_size = board_size
        self._step_count = int(self._init_game_state['step_count'])

        board_array = json.loads(self._init_game_state['board'])
        self._board = np.ones((board_size, board_size)).astype(np.uint8)
        self._board *= constants.Item.Passage.value
        for x in range(self._board_size):
            for y in range(self._board_size):
                self._board[x, y] = board_array[x][y]

        self._items = {}
        item_array = json.loads(self._init_game_state['items'])
        for i in item_array:
            self._items[tuple(i[0])] = i[1]

        agent_array = json.loads(self._init_game_state['agents'])
        for a in agent_array:
            agent = next(x for x in self._agents \
                         if x.agent_id == a['agent_id'])
            agent.set_start_position((a['position'][0], a['position'][1]))
            agent.reset(int(a['ammo']), bool(a['is_alive']),
                        int(a['blast_strength']), bool(a['can_kick']))

        self._bombs = []
        bomb_array = json.loads(self._init_game_state['bombs'])
        for b in bomb_array:
            bomber = next(x for x in self._agents \
                          if x.agent_id == b['bomber_id'])
            moving_direction = b['moving_direction']
            if moving_direction is not None:
                moving_direction = constants.Action(moving_direction)
            self._bombs.append(
                characters.Bomb(bomber, tuple(b['position']), int(b['life']),
                                int(b['blast_strength']), moving_direction))

        self._flames = []
        flame_array = json.loads(self._init_game_state['flames'])
        for f in flame_array:
            self._flames.append(
                characters.Flame(tuple(f['position']), f['life']))
Ejemplo n.º 4
0
def get_gamedata(gamestate, game_type):
    game_data = env_simulator.GameData()
    game_data.board_size = gamestate['board_size']
    game_data.step_count = gamestate['step_count'] - 1
    game_data.max_steps = 800
    game_data.game_type = game_type
    game_data.simulation_bomb_life = None

    # board
    game_data.board = gamestate['board']
    # items
    game_data.items = {}
    # agents
    game_data.agents = []
    for a in gamestate['agents']:
        id = a['agent_id']
        board_id = id + 10
        agent = characters.Bomber(id, game_data.game_type)
        agent.set_start_position(get_position(game_data.board, board_id, True))
        agent.reset(a['ammo'], a['is_alive'], a['blast_strength'],
                    a['can_kick'])
        game_data.agents.append(agent)

    # bombs
    game_data.bombs = []
    for b in gamestate['bombs']:
        bomb = characters.Bomb(**b)
        game_data.bombs.append(bomb)

    # flames
    game_data.flames = []
    for f in gamestate['flames']:
        flame = characters.Flame(**f)
        game_data.flames.append(flame)

    # done
    game_data.done = forward_model.ForwardModel.get_done(
        game_data.agents, game_data.step_count, game_data.max_steps,
        game_data.game_type, None)

    return game_data
Ejemplo n.º 5
0
    def _get_flames(self, board, prev_flame_life, bomb_position_strength):

        """
        Summarize information about flames

        Parameters
        ----------
        board : array
            pommerman board
        prev_flame_life : array
            remaining life of flames in the previous step
        exploted_position_strength : list
           list of pairs of position and strength of bombs just exploded

        Return
        ------
        curr_flames : list
            list of Flames
        flame_life : array
            remaining life of flames
        """

        flame_life = prev_flame_life - (prev_flame_life > 0)  # decrement by 1

        for (x, y), strength in bomb_position_strength:
            if not utility.position_is_flames(board, (x, y)):
                # not exploded yet
                continue
            # To up and stop
            for dx in range(0, strength):
                position = (x + dx, y)
                if not self._on_board(position):
                    break
                elif utility.position_is_flames(board, position):
                    flame_life[position] = 3
            # To down
            for dx in range(1, strength):
                position = (x - dx, y)
                if not self._on_board(position):
                    break
                elif utility.position_is_flames(board, position):
                    flame_life[position] = 3
            # To right
            for dy in range(1, strength):
                position = (x, y + dy)
                if not self._on_board(position):
                    break
                elif utility.position_is_flames(board, position):
                    flame_life[position] = 3
            # To left
            for dy in range(1, strength):
                position = (x, y - dy)
                if not self._on_board(position):
                    break
                elif utility.position_is_flames(board, position):
                    flame_life[position] = 3

        curr_flames = list()
        rows, cols = np.where(flame_life > 0)
        for position in zip(rows, cols):
            flame = characters.Flame(position, flame_life[position] - 1)
            curr_flames.append(flame)

        return curr_flames, flame_life
Ejemplo n.º 6
0
    def _get_flames(self, board, prev_board, prev_flame_life,
                    bomb_position_strength, moving_direction):
        """
        Summarize information about flames

        Parameters
        ----------
        board : array
            pommerman board
        prev_flame_life : array
            remaining life of flames in the previous step
        bomb_position_strength : list
           list of pairs of position and strength of bombs just exploded
        moving_direction : array
            direction of moving bombs

        Return
        ------
        curr_flames : list
            list of Flames
        flame_life : array
            remaining life of flames
        """

        # decrement the life of existing flames by 1
        flame_life = prev_flame_life - (prev_flame_life > 0)

        # set the life of new flames
        locations = np.where((prev_board != constants.Item.Flames.value) *
                             (board == constants.Item.Flames.value))
        flame_life[locations] = 3

        # set the life of overestimated flames at 0
        locations = np.where(board != constants.Item.Flames.value)
        flame_life[locations] = 0

        for (x, y), strength in bomb_position_strength:

            # for moving bombs, we cannot exactly tell whether it has stopped or not
            # so, consider both possibility

            dx = 0
            dy = 0
            if moving_direction[(x, y)] == constants.Action.Right:
                dy = 1
            elif moving_direction[(x, y)] == constants.Action.Left:
                dy = -1
            elif moving_direction[(x, y)] == constants.Action.Down:
                dx = 1
            elif moving_direction[(x, y)] == constants.Action.Up:
                dx = -1
            possible_positions = [(x, y)]
            if moving_direction[(x, y)] is not None:
                next_position = (x + dx, y + dy)
                if self._on_board(next_position):
                    possible_positions.append(next_position)
            """
            # there is also a possibility that a bomb just started to move, or the direction is changed by kicking
            for (dx, dy) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                agent_position = (x + dx, y + dy)
                if not self._on_board(agent_position):
                    continue
                if not utility.position_is_agent(prev_board, agent_position):
                    continue               
                # the agent might have kicked
                next_position = (x - dx, y - dy)
                if self._on_board(next_position):
                    possible_positions.append(next_position)
            """

            for (xx, yy) in possible_positions:
                if not utility.position_is_flames(board, (xx, yy)):
                    # not exploded yet
                    continue
                # To up and stop
                for dx in range(0, strength):
                    position = (xx + dx, yy)
                    if not self._on_board(position):
                        break
                    elif utility.position_is_flames(board, position):
                        flame_life[position] = 3
                # To down
                for dx in range(1, strength):
                    position = (xx - dx, yy)
                    if not self._on_board(position):
                        break
                    elif utility.position_is_flames(board, position):
                        flame_life[position] = 3
                # To right
                for dy in range(1, strength):
                    position = (xx, yy + dy)
                    if not self._on_board(position):
                        break
                    elif utility.position_is_flames(board, position):
                        flame_life[position] = 3
                # To left
                for dy in range(1, strength):
                    position = (xx, yy - dy)
                    if not self._on_board(position):
                        break
                    elif utility.position_is_flames(board, position):
                        flame_life[position] = 3

        curr_flames = list()
        rows, cols = np.where(flame_life > 0)
        for position in zip(rows, cols):
            flame = characters.Flame(position, flame_life[position] - 1)
            curr_flames.append(flame)

        return curr_flames, flame_life