diff --git a/async-bot.py b/async-bot.py index 155d281..b64ba83 100644 --- a/async-bot.py +++ b/async-bot.py @@ -19,7 +19,7 @@ class AsyncBotManager: self.difference: dict[int, bool] = {} self._last_update = 0 self._shutdown: bool = False - self.avoid: set[range] = set() + self.avoid: set[int] = set() def put_text(self, x: int, y: int, text: str): 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) 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 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) 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: with Image.new("LA", (1000, 1000), 0) as im: @@ -60,10 +63,9 @@ class AsyncBotManager: def get_avoid_image(self) -> Image.Image: with Image.new("RGB", (1000, 1000), (0, 255, 0)) as im: - for rng in self.avoid: - for index in rng: - y, x = divmod(index, 1000) - im.putpixel((x, y), (255, 0, 0)) + for index in self.avoid: + y, x = divmod(index, 1000) + im.putpixel((x, y), (255, 0, 0)) return im.copy() async def listener(self): @@ -140,6 +142,14 @@ async def amain(): mgr.add_avoid_range( 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"]: if elem["type"] == "text": mgr.put_text(elem["x"], elem["y"], elem["text"]) diff --git a/avoid_masks/noita.png b/avoid_masks/noita.png new file mode 100644 index 0000000..33fb84f Binary files /dev/null and b/avoid_masks/noita.png differ diff --git a/avoid_masks/noita_black_outline_179x111.png b/avoid_masks/noita_black_outline_179x111.png deleted file mode 100644 index 7358dcc..0000000 Binary files a/avoid_masks/noita_black_outline_179x111.png and /dev/null differ diff --git a/settings.json b/settings.json index c19bd56..063a7b8 100644 --- a/settings.json +++ b/settings.json @@ -45,7 +45,7 @@ }, { "type": "image", - "path": "./avoid_masks/noita_black_outline_179x111.png", + "path": "./avoid_masks/noita.png", "description": "Noita logo by Cr4xy" } ],