Example #1
0
    def move_animal(self,
                    sim_id: int,
                    animal_id: int,
                    new_position: SquareGridCoordinate,
                    occupied: bool = None):
        """

        :param sim_id:
        :param animal_id:
        :param new_position:
        :param occupied: optional (passed if know in advance that position is occupied
         in order to avoid unnecessary call of self.coordinate_is_occupied())
        :return:
        """
        if occupied is None:
            if self.coordinate_is_occupied(sim_id=sim_id,
                                           coordinate=new_position):
                raise NonEmptyCoordinate(
                    'Cannot move, coordinate {} is occupied'.format(
                        new_position))

        if occupied:
            raise NonEmptyCoordinate(
                'Cannot move, coordinate {} is occupied'.format(new_position))
        else:
            with self.session_scope() as s:
                simulation = s.query(Simulation).filter(
                    Simulation.sid == sim_id).one()
                # Check coordinate match with the grid
                square_grid_valid(grid_size=simulation.grid_size,
                                  coordinates=new_position)
                a_ = s.query(Animals).filter(Animals.sim_id == sim_id,
                                             Animals.oid == animal_id).one()
                if a_.alive:
                    out = (a_.coord_x, a_.coord_y)
                    a_.coord_x = new_position.x
                    a_.coord_y = new_position.y
                    return out
                else:
                    raise ImpossibleAction(
                        'Attempting to move a dead animal: {}'.format(a_))
Example #2
0
    def move_animal(self, sim_id: int, animal_id: int, new_position: SquareGridCoordinate):
        """

        :param sim_id:
        :param animal_id:
        :param new_position:
        :return:
        """
        if self.coordinate_is_occupied(sim_id=sim_id, coordinate=new_position):
            raise NonEmptyCoordinate('Cannot move, coordinate {} is occupied'.format(new_position))

        with self.session_scope() as s:
            simulation = s.query(Simulation).filter(Simulation.sid == sim_id).one()
            # Check coordinate match with the grid
            square_grid_valid(grid_size=simulation.grid_size, coordinates=new_position)
            a_ = s.query(Animals).filter(Animals.sim_id == sim_id, Animals.oid == animal_id).one()
            if a_.alive:
                a_.coord_x = new_position.x
                a_.coord_y = new_position.y
            else:
                raise ImpossibleAction('Attempting to move a dead animal: {}'.format(a_))
Example #3
0
    def init_animal(self,
                    sim_id: int,
                    current_turn: int,
                    animal_type: Animal,
                    coordinate: SquareGridCoordinate,
                    last_fed: Optional[int] = 0,
                    last_breed: Optional[int] = 0):
        """
        use for single animal init
        :return:
        """

        with self.session_scope() as s:
            try:
                simulation = s.query(Simulation).filter(
                    Simulation.sid == sim_id).one()
                s.flush()
            except NoResultFound:
                _logger.debug("Simulation {} doesn't exist!".format(sim_id))
                raise ValueError("Simulation {} doesn't exist!".format(sim_id))
            # Check coordinate match with the grid
            square_grid_valid(grid_size=simulation.grid_size,
                              coordinates=coordinate)
            # check if coordinate is free
            if self.coordinate_is_occupied(sim_id=sim_id,
                                           coordinate=coordinate):
                raise NonEmptyCoordinate(
                    'Coordinate {} is occupied'.format(coordinate))
            new_animal = Animals(sim_id=simulation.sid,
                                 animal_type=animal_type,
                                 spawn_turn=current_turn,
                                 breed_count=0,
                                 last_breed=last_breed,
                                 alive=True,
                                 last_fed=last_fed,
                                 coord_x=coordinate.x,
                                 coord_y=coordinate.y)
            s.add(new_animal)
        return new_animal.oid
Example #4
0
 def test_valid_coordinate(self):
     assert square_grid_valid(10, SquareGridCoordinate(1, 2))
     # raise if invalid
     with pytest.raises(TopologyError):
         square_grid_valid(10, SquareGridCoordinate(10, 2))
     # invalid not raised
     assert not square_grid_valid(
         10, SquareGridCoordinate(-1, 2), raise_err=False)
     assert not square_grid_valid(
         10, SquareGridCoordinate(1, -1), raise_err=False)
     assert not square_grid_valid(
         10, SquareGridCoordinate(1, 10), raise_err=False)