More Conquest Updates (#115)
* Start of map maker, ambiguous file format (probably just jpg and png) * More map maker WIP * This isn't dad * Split mapmaker * Don't load current map if it doesn't exist * WIP mapmakerpull/117/head
parent
29bb51c6cd
commit
c02244ceb5
@ -1,5 +1,7 @@
|
|||||||
{
|
{
|
||||||
"maps": [
|
"maps": [
|
||||||
"simple_blank_map"
|
"simple_blank_map",
|
||||||
|
"test",
|
||||||
|
"test2"
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"region_max": 70
|
"region_max": 70,
|
||||||
|
"extension": "jpg"
|
||||||
}
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
import discord
|
||||||
|
from redbot.core import Config, commands
|
||||||
|
from redbot.core.bot import Red
|
||||||
|
|
||||||
|
|
||||||
|
class MapMaker(commands.Cog):
|
||||||
|
"""
|
||||||
|
Create Maps to be used with Conquest
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, bot: Red):
|
||||||
|
super().__init__()
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
|
self.config = Config.get_conf(
|
||||||
|
self, identifier=77971127797107101114, force_registration=True
|
||||||
|
)
|
||||||
|
|
||||||
|
default_guild = {}
|
||||||
|
default_global = {}
|
||||||
|
self.config.register_guild(**default_guild)
|
||||||
|
self.config.register_global(**default_global)
|
||||||
|
|
||||||
|
async def red_delete_data_for_user(self, **kwargs):
|
||||||
|
"""Nothing to delete"""
|
||||||
|
return
|
||||||
|
|
||||||
|
@commands.group()
|
||||||
|
async def mapmaker(self, ctx: commands.context):
|
||||||
|
"""
|
||||||
|
Base command for managing current maps or creating new ones
|
||||||
|
"""
|
||||||
|
if ctx.invoked_subcommand is None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@mapmaker.command(name="upload")
|
||||||
|
async def _mapmaker_upload(self, ctx: commands.Context, map_path=""):
|
||||||
|
"""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:
|
||||||
|
await ctx.maybe_send_embed(
|
||||||
|
"Either upload an image with this command or provide a path to the image"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
await ctx.maybe_send_embed("WIP")
|
||||||
|
|
||||||
|
@mapmaker.command(name="load")
|
||||||
|
async def _mapmaker_load(self, ctx: commands.Context, map_name=""):
|
||||||
|
"""Load an existing map to be modified."""
|
||||||
|
await ctx.maybe_send_embed("WIP")
|
@ -0,0 +1,132 @@
|
|||||||
|
import os
|
||||||
|
import pathlib
|
||||||
|
from PIL import Image, ImageColor, ImageFont, ImageOps, ImageDraw
|
||||||
|
from PIL.ImageDraw import _color_diff
|
||||||
|
|
||||||
|
|
||||||
|
def get_center(points):
|
||||||
|
"""
|
||||||
|
Taken from https://stackoverflow.com/questions/4355894/how-to-get-center-of-set-of-points-using-python
|
||||||
|
"""
|
||||||
|
x = [p[0] for p in points]
|
||||||
|
y = [p[1] for p in points]
|
||||||
|
return sum(x) / len(points), sum(y) / len(points)
|
||||||
|
|
||||||
|
|
||||||
|
def floodfill(image, xy, value, border=None, thresh=0) -> set:
|
||||||
|
"""
|
||||||
|
Taken and modified from PIL.ImageDraw.floodfill
|
||||||
|
|
||||||
|
(experimental) Fills a bounded region with a given color.
|
||||||
|
|
||||||
|
:param image: Target image.
|
||||||
|
:param xy: Seed position (a 2-item coordinate tuple). See
|
||||||
|
:ref:`coordinate-system`.
|
||||||
|
:param value: Fill color.
|
||||||
|
:param border: Optional border value. If given, the region consists of
|
||||||
|
pixels with a color different from the border color. If not given,
|
||||||
|
the region consists of pixels having the same color as the seed
|
||||||
|
pixel.
|
||||||
|
:param thresh: Optional threshold value which specifies a maximum
|
||||||
|
tolerable difference of a pixel value from the 'background' in
|
||||||
|
order for it to be replaced. Useful for filling regions of
|
||||||
|
non-homogeneous, but similar, colors.
|
||||||
|
"""
|
||||||
|
# based on an implementation by Eric S. Raymond
|
||||||
|
# amended by yo1995 @20180806
|
||||||
|
pixel = image.load()
|
||||||
|
x, y = xy
|
||||||
|
try:
|
||||||
|
background = pixel[x, y]
|
||||||
|
if _color_diff(value, background) <= thresh:
|
||||||
|
return set() # seed point already has fill color
|
||||||
|
pixel[x, y] = value
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
return set() # seed point outside image
|
||||||
|
edge = {(x, y)}
|
||||||
|
# use a set to keep record of current and previous edge pixels
|
||||||
|
# to reduce memory consumption
|
||||||
|
filled_pixels = set()
|
||||||
|
full_edge = set()
|
||||||
|
while edge:
|
||||||
|
filled_pixels.update(edge)
|
||||||
|
new_edge = set()
|
||||||
|
for (x, y) in edge: # 4 adjacent method
|
||||||
|
for (s, t) in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
|
||||||
|
# If already processed, or if a coordinate is negative, skip
|
||||||
|
if (s, t) in full_edge or s < 0 or t < 0:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
p = pixel[s, t]
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
full_edge.add((s, t))
|
||||||
|
if border is None:
|
||||||
|
fill = _color_diff(p, background) <= thresh
|
||||||
|
else:
|
||||||
|
fill = p != value and p != border
|
||||||
|
if fill:
|
||||||
|
pixel[s, t] = value
|
||||||
|
new_edge.add((s, t))
|
||||||
|
full_edge = edge # discard pixels processed
|
||||||
|
edge = new_edge
|
||||||
|
return filled_pixels
|
||||||
|
|
||||||
|
|
||||||
|
class Regioner:
|
||||||
|
def __init__(
|
||||||
|
self, filepath: pathlib.Path, filename: str, wall_color="black", region_color="white"
|
||||||
|
):
|
||||||
|
self.filepath = filepath
|
||||||
|
self.filename = filename
|
||||||
|
self.wall_color = ImageColor.getcolor(wall_color, "L")
|
||||||
|
self.region_color = ImageColor.getcolor(region_color, "L")
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
base_img_path = self.filepath / self.filename
|
||||||
|
if not base_img_path.exists():
|
||||||
|
return None
|
||||||
|
|
||||||
|
masks_path = self.filepath / "masks"
|
||||||
|
|
||||||
|
if not masks_path.exists():
|
||||||
|
os.makedirs(masks_path)
|
||||||
|
|
||||||
|
black = ImageColor.getcolor("black", "L")
|
||||||
|
white = ImageColor.getcolor("white", "L")
|
||||||
|
|
||||||
|
base_img: Image.Image = Image.open(base_img_path).convert("L")
|
||||||
|
already_processed = set()
|
||||||
|
|
||||||
|
mask_count = 0
|
||||||
|
mask_centers = {}
|
||||||
|
|
||||||
|
for y1 in range(base_img.height):
|
||||||
|
for x1 in range(base_img.width):
|
||||||
|
if (x1, y1) in already_processed:
|
||||||
|
continue
|
||||||
|
if base_img.getpixel((x1, y1)) == self.region_color:
|
||||||
|
filled = floodfill(base_img, (x1, y1), black, self.wall_color)
|
||||||
|
if filled: # Pixels were updated, make them into a mask
|
||||||
|
mask = Image.new("L", base_img.size, 255)
|
||||||
|
for x2, y2 in filled:
|
||||||
|
mask.putpixel((x2, y2), 0)
|
||||||
|
|
||||||
|
mask_count += 1
|
||||||
|
mask = mask.convert("L")
|
||||||
|
mask.save(masks_path / f"{mask_count}.png", "PNG")
|
||||||
|
|
||||||
|
mask_centers[mask_count] = get_center(filled)
|
||||||
|
|
||||||
|
already_processed.update(filled)
|
||||||
|
|
||||||
|
number_img = Image.new("L", base_img.size, 255)
|
||||||
|
fnt = ImageFont.load_default()
|
||||||
|
d = ImageDraw.Draw(number_img)
|
||||||
|
for mask_num, center in mask_centers.items():
|
||||||
|
d.text(center, str(mask_num), font=fnt, fill=0)
|
||||||
|
|
||||||
|
number_img.save(self.filepath / f"numbers.png", "PNG")
|
||||||
|
|
||||||
|
return mask_centers
|
Loading…
Reference in new issue