social_dilemmas/envs/map_env.py
def update_map_fire(
        self,
        firing_pos,
        firing_orientation,
        fire_len,
        fire_char,
        cell_types=[],
        update_char=[],
        blocking_cells=b"P",
        beam_width=3,
    ):
        """From a firing position, fire a beam that may clean or hit agents

        Notes:
            (1) Beams are blocked by agents
            (2) A beam travels along until it hits a blocking cell at which beam the beam
                covers that cell and stops
            (3) If a beam hits a cell whose character is in cell_types, it replaces it with
                the corresponding index in update_char
            (4) As per the rules, the beams fire from in front of the agent and on its
                sides so the beam that starts in front of the agent travels out one
                cell further than it does along the sides.
            (5) This method updates the beam_pos, an internal representation of how
                which cells need to be rendered with fire_char in the agent view

        Parameters
        ----------
        firing_pos: (list)
            the row, col from which the beam is fired
        firing_orientation: (string)
            the direction the beam is to be fired in
        fire_len: (int)
            the number of cells forward to fire
        fire_char: (bytes)
            the cell that should be placed where the beam goes
        cell_types: (list of bytes)
            the cells that are affected by the beam
        update_char: (list of bytes)
            the character that should replace the affected cells.
        blocking_cells: (list of bytes)
            cells that block the firing beam
        Returns
        -------
        updates: (tuple (row, col, char))
            the cells that have been hit by the beam and what char will be placed there
        """
        agent_by_pos = {tuple(agent.pos): agent_id for agent_id, agent in self.agents.items()}
        start_pos = np.asarray(firing_pos)
        firing_direction = ORIENTATIONS[firing_orientation]
        # compute the other two starting positions
        right_shift = self.rotate_right(firing_direction)
        if beam_width == 1:
            firing_pos = [start_pos]
        elif beam_width == 3:
            firing_pos = [
                start_pos,
                start_pos + right_shift - firing_direction,
                start_pos - right_shift - firing_direction,
            ]
        else:
            raise NotImplementedError()
        firing_points = []
        updates = []
        for pos in firing_pos:
            next_cell = pos + firing_direction
            for i in range(fire_len):
                if (
                    self.test_if_in_bounds(next_cell)
                    and self.world_map[next_cell[0], next_cell[1]] != b"@"
                ):
                    # Update the cell if needed
                    firing_points.append((next_cell[0], next_cell[1], fire_char))
                    for c in range(len(cell_types)):
                        if self.world_map[next_cell[0], next_cell[1]] == cell_types[c]:
                            updates.append((next_cell[0], next_cell[1], update_char[c]))
                            break

                    # agents absorb beams
                    # activate the agents hit function if needed
                    if [next_cell[0], next_cell[1]] in self.agent_pos:
                        agent_id = agent_by_pos[(next_cell[0], next_cell[1])]
                        self.agents[agent_id].hit(fire_char)
                        break

                    # check if the cell blocks beams. For example, waste blocks beams.
                    if self.world_map[next_cell[0], next_cell[1]] in blocking_cells:
                        break

                    # increment the beam position
                    next_cell += firing_direction

                else:
                    break

        self.beam_pos += firing_points
        return updates
From a firing position, fire a beam that may clean or hit agents

        Notes:
            (1) Beams are blocked by agents
            (2) A beam travels along until it hits a blocking cell at which beam the beam
                covers that cell and stops
            (3) If a beam hits a cell whose character is in cell_types, it replaces it with
                the corresponding index in update_char
            (4) As per the rules, the beams fire from in front of the agent and on its
                sides so the beam that starts in front of the agent travels out one
                cell further than it does along the sides.
            (5) This method updates the beam_pos, an internal representation of how
                which cells need to be rendered with fire_char in the agent view

        Parameters
        ----------
        firing_pos: (list)
            the row, col from which the beam is fired
        firing_orientation: (string)
            the direction the beam is to be fired in
        fire_len: (int)
            the number of cells forward to fire
        fire_char: (bytes)
            the cell that should be placed where the beam goes
        cell_types: (list of bytes)
            the cells that are affected by the beam
        update_char: (list of bytes)
            the character that should replace the affected cells.
        blocking_cells: (list of bytes)
            cells that block the firing beam
        Returns
        -------
        updates: (tuple (row, col, char))
            the cells that have been hit by the beam and what char will be placed there
        
From a firing position, fire a beam that may clean or hit agents
Notes:
(1) Beams are blocked by agents
(2) A beam travels along until it hits a blocking cell at which beam the beam
covers that cell and stops
(3) If a beam hits a cell whose character is in cell_types, it replaces it with
the corresponding index in update_char
(4) As per the rules, the beams fire from in front of the agent and on its
sides so the beam that starts in front of the agent travels out one
cell further than it does along the sides.
(5) This method updates the beam_pos, an internal representation of how
which cells need to be rendered with fire_char in the agent view

Parameters



(tuple (row, col, char))
the cells that have been hit by the beam and what char will be placed there
From a firing position fire a beam that may clean or hit agents Notes : ( 1 ) Beams are blocked by agents ( 2 ) A beam travels along until it hits a blocking cell at which beam the beam covers that cell and stops ( 3 ) If a beam hits a cell whose character is in cell_types it replaces it with the corresponding index in update_char ( 4 ) As per the rules the beams fire from in front of the agent and on its sides so the beam that starts in front of the agent travels out one cell further than it does along the sides . ( 5 ) This method updates the beam_pos an internal representation of how which cells need to be rendered with fire_char in the agent view Parameters ( tuple ( row col char )) the cells that have been hit by the beam and what char will be placed there
def update_map_fire(
        self,
        firing_pos,
        firing_orientation,
        fire_len,
        fire_char,
        cell_types=[],
        update_char=[],
        blocking_cells=b"P",
        beam_width=3,
    ):
        agent_by_pos = {tuple(agent.pos): agent_id for agent_id, agent in self.agents.items()}
        start_pos = np.asarray(firing_pos)
        firing_direction = ORIENTATIONS[firing_orientation]
        right_shift = self.rotate_right(firing_direction)
        if beam_width == 1:
            firing_pos = [start_pos]
        elif beam_width == 3:
            firing_pos = [
                start_pos,
                start_pos + right_shift - firing_direction,
                start_pos - right_shift - firing_direction,
            ]
        else:
            raise NotImplementedError()
        firing_points = []
        updates = []
        for pos in firing_pos:
            next_cell = pos + firing_direction
            for i in range(fire_len):
                if (
                    self.test_if_in_bounds(next_cell)
                    and self.world_map[next_cell[0], next_cell[1]] != b"@"
                ):
                    firing_points.append((next_cell[0], next_cell[1], fire_char))
                    for c in range(len(cell_types)):
                        if self.world_map[next_cell[0], next_cell[1]] == cell_types[c]:
                            updates.append((next_cell[0], next_cell[1], update_char[c]))
                            break
                    if [next_cell[0], next_cell[1]] in self.agent_pos:
                        agent_id = agent_by_pos[(next_cell[0], next_cell[1])]
                        self.agents[agent_id].hit(fire_char)
                        break
                    if self.world_map[next_cell[0], next_cell[1]] in blocking_cells:
                        break
                    next_cell += firing_direction
                else:
                    break
        self.beam_pos += firing_points
        return updates
def update_map_fire ( self , firing_pos , firing_orientation , fire_len , fire_char , cell_types = [ ] , update_char = [ ] , blocking_cells = b"P" , beam_width = 3 , ) : agent_by_pos = { tuple ( agent . pos ) : agent_id for agent_id , agent in self . agents . items ( ) } start_pos = np . asarray ( firing_pos ) firing_direction = ORIENTATIONS [ firing_orientation ] right_shift = self . rotate_right ( firing_direction ) if beam_width == 1 : firing_pos = [ start_pos ] elif beam_width == 3 : firing_pos = [ start_pos , start_pos + right_shift - firing_direction , start_pos - right_shift - firing_direction , ] else : raise NotImplementedError ( ) firing_points = [ ] updates = [ ] for pos in firing_pos : next_cell = pos + firing_direction for i in range ( fire_len ) : if ( self . test_if_in_bounds ( next_cell ) and self . world_map [ next_cell [ 0 ] , next_cell [ 1 ] ] != b"@" ) : firing_points . append ( ( next_cell [ 0 ] , next_cell [ 1 ] , fire_char ) ) for c in range ( len ( cell_types ) ) : if self . world_map [ next_cell [ 0 ] , next_cell [ 1 ] ] == cell_types [ c ] : updates . append ( ( next_cell [ 0 ] , next_cell [ 1 ] , update_char [ c ] ) ) break if [ next_cell [ 0 ] , next_cell [ 1 ] ] in self . agent_pos : agent_id = agent_by_pos [ ( next_cell [ 0 ] , next_cell [ 1 ] ) ] self . agents [ agent_id ] . hit ( fire_char ) break if self . world_map [ next_cell [ 0 ] , next_cell [ 1 ] ] in blocking_cells : break next_cell += firing_direction else : break self . beam_pos += firing_points return updates
From a firing position, fire a beam that may clean or hit agents
Notes:
(1) Beams are blocked by agents
(2) A beam travels along until it hits a blocking cell at which beam the beam
covers that cell and stops
(3) If a beam hits a cell whose character is in cell_types, it replaces it with
the corresponding index in update_char
(4) As per the rules, the beams fire from in front of the agent and on its
sides so the beam that starts in front of the agent travels out one
cell further than it does along the sides.
From a firing position fire a beam that may clean or hit agents Notes : ( 1 ) Beams are blocked by agents ( 2 ) A beam travels along until it hits a blocking cell at which beam the beam covers that cell and stops ( 3 ) If a beam hits a cell whose character is in cell_types it replaces it with the corresponding index in update_char ( 4 ) As per the rules the beams fire from in front of the agent and on its sides so the beam that starts in front of the agent travels out one cell further than it does along the sides .
"""From a firing position, fire a beam that may clean or hit agents

        Notes:
            (1) Beams are blocked by agents
            (2) A beam travels along until it hits a blocking cell at which beam the beam
                covers that cell and stops
            (3) If a beam hits a cell whose character is in cell_types, it replaces it with
                the corresponding index in update_char
            (4) As per the rules, the beams fire from in front of the agent and on its
                sides so the beam that starts in front of the agent travels out one
                cell further than it does along the sides.
            (5) This method updates the beam_pos, an internal representation of how
                which cells need to be rendered with fire_char in the agent view

        Parameters
        ----------
        firing_pos: (list)
            the row, col from which the beam is fired
        firing_orientation: (string)
            the direction the beam is to be fired in
        fire_len: (int)
            the number of cells forward to fire
        fire_char: (bytes)
            the cell that should be placed where the beam goes
        cell_types: (list of bytes)
            the cells that are affected by the beam
        update_char: (list of bytes)
            the character that should replace the affected cells.
        blocking_cells: (list of bytes)
            cells that block the firing beam
        Returns
        -------
        updates: (tuple (row, col, char))
            the cells that have been hit by the beam and what char will be placed there
        """ # compute the other two starting positions # Update the cell if needed # agents absorb beams # activate the agents hit function if needed # check if the cell blocks beams. For example, waste blocks beams. # increment the beam position
self
firing_pos
firing_orientation
fire_len
fire_char
cell_types
update_char
blocking_cells
beam_width