def _generate_level_data(self, game_name, num_levels, doc_path, yaml_string): # load a simple griddly env with each tile printed loader = GriddlyLoader() renderer = RenderToFile() level_images = defaultdict(dict) level_sizes = {} supported_observer_types = [] for observer_type in self._observer_types: grid = loader.load_string(yaml_string) try: game = grid.create_game(observer_type) except ValueError as e: self._logger.debug(f'Skipping observer type {observer_type} for environment {game_name}') continue supported_observer_types.append(observer_type) players = [] for p in range(grid.get_player_count()): players.append(game.register_player(f'P{p}', observer_type)) game.init() for level in range(num_levels): observer_type_string = self._get_observer_yaml_key(observer_type) grid.load_level(level) game.reset() rendered_level = np.array(game.observe(), copy=False) relative_image_path = os.path.join('img', f'{game_name.replace(" ", "_")}-level-{observer_type_string}-{level}.png') doc_image_path = os.path.join(doc_path, relative_image_path) renderer.render(rendered_level, doc_image_path) level_images[observer_type_string][level] = relative_image_path level_sizes[level] = {'width': grid.get_width(), 'height': grid.get_height()} # We are creating loads of game instances. this forces the release of vulkan resources before the python GC game.release() return level_images, level_sizes, supported_observer_types
def _generate_object_tile_images(self, game_description, yaml_string, doc_path, game_name): # img/sokoban-wall-x.png tile_images = defaultdict(dict) # load a simple griddly env with each tile printed loader = GriddlyLoader() renderer = RenderToFile() debug_renderer = RenderWindow(320,480) objects = game_description['Objects'] level_string = '' for i, object in enumerate(objects): if 'MapCharacter' in object: name = object['Name'] level_string += f'{object["MapCharacter"]} ' supported_observer_types = [] for observer_type in self._observer_types: grid = loader.load_string(yaml_string) player_count = grid.get_player_count() try: game = grid.create_game(observer_type) except ValueError as e: self._logger.debug(f'Skipping observer type {observer_type} for environment {game_name}') continue supported_observer_types.append(observer_type) players = [] for p in range(player_count): players.append(game.register_player(f'P{p}', observer_type)) observer_type_string = self._get_observer_yaml_key(observer_type) environment = game_description['Environment'] if 'Observers' in environment and observer_type_string in environment['Observers']: observer_config = environment['Observers'][observer_type_string] if 'TileSize' in observer_config: tileSize = observer_config['TileSize'] if isinstance(tileSize, int): tileSize = [tileSize, tileSize] else: tileSize = [24, 24] if 'TileOffsetY' in observer_config: tileOffsetY = observer_config['TileOffsetY'] else: tileOffsetY = 0 else: tileSize = [24, 24] game.init() if observer_type == gd.ObserverType.ISOMETRIC: level_string = level_string.replace(' ', '.') grid.load_level_string(f'{level_string}\n') game.reset() rendered_sprite_map = np.array(game.observe(), copy=False) i = 0 for object in objects: if 'MapCharacter' in object: name = object['Name'] relative_image_path = os.path.join('img', f'{game_name.replace(" ", "_")}-object-{observer_type_string}-{name}.png') doc_image_path = os.path.join(doc_path, relative_image_path) if observer_type == gd.ObserverType.ISOMETRIC: tilePosX = i * int(tileSize[0]) tilePosY = i * int(tileSize[1]-32) single_sprite = rendered_sprite_map[ :, tilePosX:tilePosX + tileSize[0], tilePosY:tilePosY + tileSize[1] ] #debug_renderer.render(single_sprite) else: single_sprite = rendered_sprite_map[:, i * tileSize[0]:i * tileSize[0] + tileSize[0], ] tile_images[observer_type_string][name] = relative_image_path renderer.render(single_sprite, doc_image_path) i += 1 # We are creating loads of game instances. this forces the release of vulkan resources before the python GC game.release() return tile_images, supported_observer_types