Changed avoid logic to allow single pixels

This commit is contained in:
Casey 2024-07-04 17:02:05 +03:00
parent 40d7bfba33
commit 7889d58170
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
4 changed files with 18 additions and 8 deletions

View File

@ -19,7 +19,7 @@ class AsyncBotManager:
self.difference: dict[int, bool] = {} self.difference: dict[int, bool] = {}
self._last_update = 0 self._last_update = 0
self._shutdown: bool = False self._shutdown: bool = False
self.avoid: set[range] = set() self.avoid: set[int] = set()
def put_text(self, x: int, y: int, text: str): def put_text(self, x: int, y: int, text: str):
with Image.new("LA", (int(self.font.getlength(text) + 12), 16)) as im: with Image.new("LA", (int(self.font.getlength(text) + 12), 16)) as im:
@ -40,7 +40,7 @@ class AsyncBotManager:
self.put_index(index, l > 0) self.put_index(index, l > 0)
def put_index(self, index: int, value: bool): def put_index(self, index: int, value: bool):
if not any([index in avoid for avoid in self.avoid]): if not index in self.avoid:
self.difference[index] = value self.difference[index] = value
def add_avoid_rect(self, sx: int, sy: int, w: int, h: int): def add_avoid_rect(self, sx: int, sy: int, w: int, h: int):
@ -49,7 +49,10 @@ class AsyncBotManager:
self.add_avoid_range(sx + ox, sx + w + ox) self.add_avoid_range(sx + ox, sx + w + ox)
def add_avoid_range(self, start: int, stop: int, step: int = 1): def add_avoid_range(self, start: int, stop: int, step: int = 1):
self.avoid.add(range(start, stop, step)) self.avoid |= set(range(start, stop, step))
def add_avoid_index(self, *indices: int):
self.avoid |= set(indices)
def get_difference_image(self) -> Image.Image: def get_difference_image(self) -> Image.Image:
with Image.new("LA", (1000, 1000), 0) as im: with Image.new("LA", (1000, 1000), 0) as im:
@ -60,10 +63,9 @@ class AsyncBotManager:
def get_avoid_image(self) -> Image.Image: def get_avoid_image(self) -> Image.Image:
with Image.new("RGB", (1000, 1000), (0, 255, 0)) as im: with Image.new("RGB", (1000, 1000), (0, 255, 0)) as im:
for rng in self.avoid: for index in self.avoid:
for index in rng: y, x = divmod(index, 1000)
y, x = divmod(index, 1000) im.putpixel((x, y), (255, 0, 0))
im.putpixel((x, y), (255, 0, 0))
return im.copy() return im.copy()
async def listener(self): async def listener(self):
@ -140,6 +142,14 @@ async def amain():
mgr.add_avoid_range( mgr.add_avoid_range(
avoid["start"], avoid["stop"], avoid.get("step", 1) avoid["start"], avoid["stop"], avoid.get("step", 1)
) )
elif avoid["type"] == "image":
with Image.open(avoid["path"]).convert("LA") as im:
assert im.width == 1000 and im.height == 1000
for y in range(im.height):
for x in range(im.width):
l, a = im.getpixel((x, y)) # type: ignore
if a > 128:
mgr.add_avoid_index(x + y * 1000)
for elem in settings["elements"]: for elem in settings["elements"]:
if elem["type"] == "text": if elem["type"] == "text":
mgr.put_text(elem["x"], elem["y"], elem["text"]) mgr.put_text(elem["x"], elem["y"], elem["text"])

BIN
avoid_masks/noita.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

View File

@ -45,7 +45,7 @@
}, },
{ {
"type": "image", "type": "image",
"path": "./avoid_masks/noita_black_outline_179x111.png", "path": "./avoid_masks/noita.png",
"description": "Noita logo by Cr4xy" "description": "Noita logo by Cr4xy"
} }
], ],