Functioning map maker

conquest_develop
bobloy 3 years ago
parent 978d6f8627
commit 9cf466c78e

@ -161,7 +161,7 @@ class Conquest(commands.Cog):
@mapmaker.command(name="save") @mapmaker.command(name="save")
async def _mapmaker_save(self, ctx: Context, *, map_name: str): 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: if not self.mm:
await ctx.maybe_send_embed("No map currently being worked on") await ctx.maybe_send_embed("No map currently being worked on")
return return
@ -179,10 +179,10 @@ class Conquest(commands.Cog):
await ctx.maybe_send_embed(f"Map successfully saved to {target_save}") await ctx.maybe_send_embed(f"Map successfully saved to {target_save}")
@mapmaker.command(name="upload") @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""" """Load a map image to be modified. Upload one with this command or provide a path"""
message: discord.Message = ctx.message 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( await ctx.maybe_send_embed(
"Either upload an image with this command or provide a path to the image" "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: if not pred.result:
return return
if not self.mm: if self.mm: # Only one map can be worked on at a time
self.mm = MapMaker(self.custom_map_folder) 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 self.mm.custom = True
if map_path: if path_to_image:
map_path = pathlib.Path(map_path) path_to_image = pathlib.Path(path_to_image)
if not map_path.exists(): if not path_to_image.exists():
await ctx.maybe_send_embed("Map not found at that path") await ctx.maybe_send_embed("Map not found at that path")
return return
mm_img = Image.open(map_path) mm_img = Image.open(path_to_image)
elif message.attachments: elif message.attachments:
attch: discord.Attachment = message.attachments[0] attch: discord.Attachment = message.attachments[0]
# attch_file = await attch.to_file() # attch_file = await attch.to_file()
buffer = BytesIO() buffer = BytesIO()
await attch.save(buffer) await attch.save(buffer)
mm_img: Image.Image = Image.open(buffer) mm_img: Image.Image = Image.open(buffer)
else: else:
# Wait what? # Wait what?
return return
if mm_img.mode == "P": if mm_img.mode == "P":
# Maybe convert to L to prevent RGB? # Maybe convert to L to prevent RGB?
mm_img = mm_img.convert() # No P mode, convert it 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: if not result:
self.mm = None self.mm = None
await ctx.maybe_send_embed("Failed to upload to that name") await ctx.maybe_send_embed("Failed to upload to that name")
return return
maps_json_path = self.custom_map_folder / "maps.json" maps_json_path = self.custom_map_folder / "maps.json"
with maps_json_path.open("r+") as maps: with maps_json_path.open("r+") as maps:
map_data = json.load(maps) map_data = json.load(maps)
map_data["maps"].append(map_name) map_data["maps"].append(map_name)
maps.seek(0) maps.seek(0)
json.dump(map_data, maps, sort_keys=True, indent=4) 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") @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""" """Print the currently being modified map as a sample"""
if not self.mm: if not self.mm:
await ctx.maybe_send_embed("No map currently being worked on") await ctx.maybe_send_embed("No map currently being worked on")
return 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(): async with ctx.typing():
files = await self.mm.get_sample() files = await self.mm.get_sample(region)
for f in files: for f in files:
await ctx.send(file=discord.File(f, filename="map.png")) await ctx.send(file=discord.File(f, filename="map.png"))
@ -277,7 +287,7 @@ class Conquest(commands.Cog):
return return
self.mm = MapMaker(map_path) self.mm = MapMaker(map_path)
# await self.mm.load_data() self.mm.load_data()
await ctx.tick() await ctx.tick()
@ -338,11 +348,11 @@ class Conquest(commands.Cog):
else: else:
await ctx.maybe_send_embed(f"Failed to delete masks") 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( async def _mapmaker_masks_combine(
self, ctx: Context, mask_list: Greedy[int], recommended=False 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: if not mask_list and not recommended:
await ctx.send_help() await ctx.send_help()
return return

@ -23,6 +23,7 @@ class ConquestGame:
def __init__(self, map_path: pathlib.Path, game_name: str, custom_map_path: pathlib.Path): def __init__(self, map_path: pathlib.Path, game_name: str, custom_map_path: pathlib.Path):
self.source_map = ConquestMap(map_path) self.source_map = ConquestMap(map_path)
self.source_map.load_data()
self.game_name = game_name self.game_name = game_name
self.current_map_folder = custom_map_path self.current_map_folder = custom_map_path

@ -107,7 +107,7 @@ def floodfill(image, xy, value, border=None, thresh=0) -> set:
new_edge.add((s, t)) new_edge.add((s, t))
full_edge = edge # discard pixels processed full_edge = edge # discard pixels processed
edge = new_edge edge = new_edge
return filled_pixels return filled_pixels # Modified to returned the filled pixels
def create_number_mask(regions, filepath, filename): def create_number_mask(regions, filepath, filename):
@ -140,8 +140,6 @@ class ConquestMap:
self.region_max = None self.region_max = None
self.regions = {} self.regions = {}
self.load_data()
def masks_path(self): def masks_path(self):
return self.path / "masks" return self.path / "masks"
@ -202,7 +200,7 @@ class ConquestMap:
return lowest_num, eliminated_masks, mask return lowest_num, eliminated_masks, mask
async def get_sample(self): async def get_sample(self, region=None):
files = [self.blank_path()] files = [self.blank_path()]
masks_dir = self.masks_path() masks_dir = self.masks_path()
@ -210,27 +208,34 @@ class ConquestMap:
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
current_map = Image.open(self.blank_path()) current_map = Image.open(self.blank_path())
regions = list(self.regions.keys()) if region is not None:
fourth = len(regions) // 4 if region in self.regions:
current_map = await composite_regions(
current_map = await composite_regions( current_map, [region], ImageColor.getrgb("red"), self.masks_path()
current_map, regions[:fourth], ImageColor.getrgb("red"), self.masks_path() )
) else:
current_map = await composite_regions( regions = list(self.regions.keys())
current_map,
regions[fourth : fourth * 2], fourth = len(regions) // 4
ImageColor.getrgb("green"),
self.masks_path(), current_map = await composite_regions(
) current_map, regions[:fourth], ImageColor.getrgb("red"), self.masks_path()
current_map = await composite_regions( )
current_map, current_map = await composite_regions(
regions[fourth * 2 : fourth * 3], current_map,
ImageColor.getrgb("blue"), regions[fourth : fourth * 2],
self.masks_path(), ImageColor.getrgb("green"),
) self.masks_path(),
current_map = await composite_regions( )
current_map, regions[fourth * 3 :], ImageColor.getrgb("yellow"), 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) current_numbered_img = await self.get_numbered(current_map)

Loading…
Cancel
Save