예제 #1
0
    def __init__(self,
                 width: int,
                 height: int,
                 tiles: List[List[Union['TerrainType', str]]] = None):
        self.__size = (width, height)
        if width % 2 == 0 or height % 2 == 0:
            raise IllegalArgumentException('Map size must be odd numbers')

        if width < 3:
            raise IllegalArgumentException('Map width must be greater than 2')

        if height < 3:
            raise IllegalArgumentException('Map height must be greater than 2')

        self.__width = width
        self.__height = height

        self.__vertical_radius = (height - 1) // 2
        self.__horizontal_radius = (width - 1) // 2

        self.__visible_tiles_cache = {}

        self.__tiles = []  # type: List[List[Union[Terrain,None]]]
        for i in range(height):
            self.__tiles.append([])
            for j in range(width):
                self.__tiles[i].append(None)

        if tiles:
            for y, terrain_list in enumerate(tiles):
                for x, terrain_type in enumerate(terrain_list):
                    position = OffsetPosition(x - self.__horizontal_radius,
                                              y - self.__vertical_radius)
                    if type(terrain_type) is TerrainType:
                        self.set_tile(position, terrain_type.value)
                    elif type(terrain_type) is str:
                        _terrain_type = TerrainType.from_char(
                            terrain_type).value
                        if _terrain_type is None:
                            raise IllegalArgumentException(
                                'Invalid tile character {}'.format(
                                    terrain_type))
                        self.set_tile(
                            position,
                            TerrainType.from_char(terrain_type).value)
                    else:
                        raise IllegalArgumentException(
                            'Map tiles could be TerrainType enum or string representation!'
                        )
예제 #2
0
    def first_free_tile(self, terrain_type: str):
        """ Find random tile with given terrain type """
        tiles = self.map_proxy.get_inner_tiles()
        border_tiles = self.map_proxy.get_border_tiles()

        for position in tiles:
            terrain = self.map_proxy.get_terrain_type(
                position) == TerrainType.from_string(terrain_type)
            if terrain and position not in border_tiles:
                return position
        return None
예제 #3
0
    def visible_free_tile(self, terrain_type: str):
        """ Find random free tile with given terrain type """
        tiles = self.map_proxy.get_player_visible_tiles()
        border_tiles = self.map_proxy.get_border_tiles()

        for position in tiles:
            terrain = self.map_proxy.get_terrain_type(
                position) == TerrainType.from_string(terrain_type)
            occupied = self.map_proxy.is_position_occupied(position)
            if terrain and not occupied and position not in border_tiles:
                return position
        return None