diff --git a/conquest/conquest.py b/conquest/conquest.py index 36a0c2e..7b52ce3 100644 --- a/conquest/conquest.py +++ b/conquest/conquest.py @@ -161,7 +161,7 @@ class Conquest(commands.Cog): @mapmaker.command(name="save") async def _mapmaker_save(self, ctx: Context, *, map_name: str): - """Save the current map to the specified map name""" + """Save the current map to a different map name""" if not self.mm: await ctx.maybe_send_embed("No map currently being worked on") return @@ -179,10 +179,10 @@ class Conquest(commands.Cog): await ctx.maybe_send_embed(f"Map successfully saved to {target_save}") @mapmaker.command(name="upload") - async def _mapmaker_upload(self, ctx: Context, map_name: str, map_path=""): + async def _mapmaker_upload(self, ctx: Context, map_name: str, path_to_image=""): """Load a map image to be modified. Upload one with this command or provide a path""" message: discord.Message = ctx.message - if not message.attachments and not map_path: + if not message.attachments and not path_to_image: await ctx.maybe_send_embed( "Either upload an image with this command or provide a path to the image" ) @@ -202,61 +202,71 @@ class Conquest(commands.Cog): if not pred.result: return - if not self.mm: - self.mm = MapMaker(self.custom_map_folder) + if self.mm: # Only one map can be worked on at a time + await ctx.maybe_send_embed( + "An existing map is in progress, close it before opening a new one. (`[p]mapmaker close`)" + ) + return + + async with ctx.typing(): + self.mm = MapMaker(target_save) self.mm.custom = True - if map_path: - map_path = pathlib.Path(map_path) + if path_to_image: + path_to_image = pathlib.Path(path_to_image) - if not map_path.exists(): - await ctx.maybe_send_embed("Map not found at that path") - return + if not path_to_image.exists(): + await ctx.maybe_send_embed("Map not found at that path") + return - mm_img = Image.open(map_path) + mm_img = Image.open(path_to_image) - elif message.attachments: - attch: discord.Attachment = message.attachments[0] - # attch_file = await attch.to_file() + elif message.attachments: + attch: discord.Attachment = message.attachments[0] + # attch_file = await attch.to_file() - buffer = BytesIO() - await attch.save(buffer) + buffer = BytesIO() + await attch.save(buffer) - mm_img: Image.Image = Image.open(buffer) - else: - # Wait what? - return + mm_img: Image.Image = Image.open(buffer) + else: + # Wait what? + return - if mm_img.mode == "P": - # Maybe convert to L to prevent RGB? - mm_img = mm_img.convert() # No P mode, convert it + if mm_img.mode == "P": + # Maybe convert to L to prevent RGB? + mm_img = mm_img.convert() # No P mode, convert it - result = await self.mm.init_directory(map_name, target_save, mm_img) + result = await self.mm.init_directory(map_name, target_save, mm_img) - if not result: - self.mm = None - await ctx.maybe_send_embed("Failed to upload to that name") - return + if not result: + self.mm = None + await ctx.maybe_send_embed("Failed to upload to that name") + return - maps_json_path = self.custom_map_folder / "maps.json" - with maps_json_path.open("r+") as maps: - map_data = json.load(maps) - map_data["maps"].append(map_name) - maps.seek(0) - json.dump(map_data, maps, sort_keys=True, indent=4) + maps_json_path = self.custom_map_folder / "maps.json" + with maps_json_path.open("r+") as maps: + map_data = json.load(maps) + map_data["maps"].append(map_name) + maps.seek(0) + json.dump(map_data, maps, sort_keys=True, indent=4) - await ctx.maybe_send_embed(f"Map successfully uploaded to {target_save}") + await ctx.maybe_send_embed(f"Map successfully uploaded to {target_save}") @mapmaker.command(name="sample") - async def _mapmaker_sample(self, ctx: Context): + async def _mapmaker_sample(self, ctx: Context, region: int = None): """Print the currently being modified map as a sample""" if not self.mm: await ctx.maybe_send_embed("No map currently being worked on") return + if region is not None and region not in self.mm.regions: + await ctx.send("This region doesn't exist or was deleted") + return + async with ctx.typing(): - files = await self.mm.get_sample() + files = await self.mm.get_sample(region) for f in files: await ctx.send(file=discord.File(f, filename="map.png")) @@ -277,7 +287,7 @@ class Conquest(commands.Cog): return self.mm = MapMaker(map_path) - # await self.mm.load_data() + self.mm.load_data() await ctx.tick() @@ -338,11 +348,11 @@ class Conquest(commands.Cog): else: await ctx.maybe_send_embed(f"Failed to delete masks") - @_mapmaker_masks.command(name="combine") + @_mapmaker_masks.command(name="merge", aliases=["combine"]) async def _mapmaker_masks_combine( self, ctx: Context, mask_list: Greedy[int], recommended=False ): - """Generate masks for the map""" + """Merge masks into a single mask""" if not mask_list and not recommended: await ctx.send_help() return diff --git a/conquest/conquestgame.py b/conquest/conquestgame.py index ab14201..53e8bec 100644 --- a/conquest/conquestgame.py +++ b/conquest/conquestgame.py @@ -23,6 +23,7 @@ class ConquestGame: def __init__(self, map_path: pathlib.Path, game_name: str, custom_map_path: pathlib.Path): self.source_map = ConquestMap(map_path) + self.source_map.load_data() self.game_name = game_name self.current_map_folder = custom_map_path diff --git a/conquest/regioner.py b/conquest/regioner.py index b6eb892..c7cf506 100644 --- a/conquest/regioner.py +++ b/conquest/regioner.py @@ -107,7 +107,7 @@ def floodfill(image, xy, value, border=None, thresh=0) -> set: new_edge.add((s, t)) full_edge = edge # discard pixels processed edge = new_edge - return filled_pixels + return filled_pixels # Modified to returned the filled pixels def create_number_mask(regions, filepath, filename): @@ -140,8 +140,6 @@ class ConquestMap: self.region_max = None self.regions = {} - self.load_data() - def masks_path(self): return self.path / "masks" @@ -202,7 +200,7 @@ class ConquestMap: return lowest_num, eliminated_masks, mask - async def get_sample(self): + async def get_sample(self, region=None): files = [self.blank_path()] masks_dir = self.masks_path() @@ -210,27 +208,34 @@ class ConquestMap: loop = asyncio.get_running_loop() current_map = Image.open(self.blank_path()) - regions = list(self.regions.keys()) - fourth = len(regions) // 4 - - current_map = await composite_regions( - current_map, regions[:fourth], ImageColor.getrgb("red"), self.masks_path() - ) - current_map = await composite_regions( - current_map, - regions[fourth : fourth * 2], - ImageColor.getrgb("green"), - self.masks_path(), - ) - current_map = await composite_regions( - current_map, - regions[fourth * 2 : fourth * 3], - ImageColor.getrgb("blue"), - self.masks_path(), - ) - current_map = await composite_regions( - current_map, regions[fourth * 3 :], ImageColor.getrgb("yellow"), self.masks_path() - ) + if region is not None: + if region in self.regions: + current_map = await composite_regions( + current_map, [region], ImageColor.getrgb("red"), self.masks_path() + ) + else: + regions = list(self.regions.keys()) + + fourth = len(regions) // 4 + + current_map = await composite_regions( + current_map, regions[:fourth], ImageColor.getrgb("red"), self.masks_path() + ) + current_map = await composite_regions( + current_map, + regions[fourth : fourth * 2], + ImageColor.getrgb("green"), + self.masks_path(), + ) + current_map = await composite_regions( + current_map, + regions[fourth * 2 : fourth * 3], + ImageColor.getrgb("blue"), + self.masks_path(), + ) + current_map = await composite_regions( + current_map, regions[fourth * 3 :], ImageColor.getrgb("yellow"), self.masks_path() + ) current_numbered_img = await self.get_numbered(current_map)