From 0351acb75d51b18c98a8f8902651568758a1e09b Mon Sep 17 00:00:00 2001 From: Axy Date: Fri, 6 Mar 2026 01:30:48 +0100 Subject: [PATCH] maze_make_empty and layout runspeed improvements --- __main__.py | 8 ++++-- amazeing/maze_display/TTYdisplay.py | 40 +++++++++++++++++++---------- amazeing/maze_display/layout.py | 21 ++++++++++++++- amazeing/maze_make_empty.py | 16 ++++++++++++ example.conf | 14 +++++----- 5 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 amazeing/maze_make_empty.py diff --git a/__main__.py b/__main__.py index 21eed84..851ac1a 100644 --- a/__main__.py +++ b/__main__.py @@ -14,6 +14,7 @@ from amazeing.config.config_parser import Config from amazeing.maze_class.maze_walls import Cardinal, CellCoord from amazeing.maze_display.TTYdisplay import Tile, TileMaps, extract_pairs from amazeing.maze_display.backend import BackendEvent, CloseRequested, IVec2 +from amazeing.maze_make_empty import maze_make_empty config = Config.parse(open("./example.conf").read()) @@ -72,7 +73,7 @@ def display_maze(maze: Maze) -> None: backend.draw_tile(pixel) maze.clear_dirty() backend.present() - poll_events(0) + poll_events(2) def poll_events(timeout_ms: int = -1) -> None: @@ -95,6 +96,9 @@ maze_make_perfect(maze, callback=display_maze) maze_make_pacman(maze, walls_const, callback=display_maze) while True: maze_make_perfect(maze, callback=display_maze) - maze_make_pacman(maze, walls_const, callback=display_maze) + poll_events(200) + # maze_make_pacman(maze, walls_const, callback=display_maze) + maze_make_empty(maze, walls_const, callback=display_maze) + poll_events(200) maze._rebuild() poll_events() diff --git a/amazeing/maze_display/TTYdisplay.py b/amazeing/maze_display/TTYdisplay.py index d51607b..a9fa9e1 100644 --- a/amazeing/maze_display/TTYdisplay.py +++ b/amazeing/maze_display/TTYdisplay.py @@ -4,6 +4,7 @@ from ..config.config_parser import Color, Config, ColoredLine, ColorPair from amazeing.maze_display.layout import ( BInt, Box, + DBox, FBox, HBox, VBox, @@ -61,7 +62,6 @@ class Tile: ) -> None: if size.x <= 0 or size.y <= 0: return - print(src, dst, size, window.getmaxyx(), file=stderr) self.__pad.overwrite( window, *src.yx(), *dst.yx(), *(dst + size - IVec2.splat(1)).yx() ) @@ -322,16 +322,26 @@ class TTYBackend(Backend[int]): IVec2(BInt(dims.x), BInt(dims.y)), lambda at, into: self.__pad.present(at, into, self.__scratch), ) - filler_box = FBox( - IVec2(BInt(0, True), BInt(0, True)), - lambda at, into: ( - None - if self.__filler is None - else self.__tilemap.draw_at_wrapping( - at, at, into, self.__filler, self.__scratch + + self.__filler_boxes: list[DBox] = [] + + def filler_box() -> Box: + self.__filler_boxes.append( + res := DBox( + FBox( + IVec2(BInt(0, True), BInt(0, True)), + lambda at, into: ( + None + if self.__filler is None + else self.__tilemap.draw_at_wrapping( + at, at, into, self.__filler, self.__scratch + ) + ), + ) ) - ), - ) + ) + return res + f: Callable[[int], int] = lambda e: e layout = layout_split( layout_fair, layout_sort_chunked(layout_fair, layout_priority, f) @@ -339,15 +349,15 @@ class TTYBackend(Backend[int]): self.__layout: Box = VBox( layout, [ - (filler_box, 0), + (filler_box(), 0), ( HBox( layout, - [(filler_box, 0), (maze_box, 1), (filler_box, 0)], + [(filler_box(), 0), (maze_box, 1), (filler_box(), 0)], ), 1, ), - (filler_box, 0), + (filler_box(), 0), ], ) @@ -364,6 +374,8 @@ class TTYBackend(Backend[int]): def set_filler(self, style: int) -> None: self.__filler = style + for box in self.__filler_boxes: + box.mark_dirty() def add_style(self, style: Tile) -> int: return self.__tilemap.add_tile(style) @@ -381,6 +393,8 @@ class TTYBackend(Backend[int]): if self.__resize: self.__resize = False self.__screen.erase() + for box in self.__filler_boxes: + box.mark_dirty() self.__screen.refresh() y, x = self.__screen.getmaxyx() self.__scratch.resize(y, x) diff --git a/amazeing/maze_display/layout.py b/amazeing/maze_display/layout.py index 16fc870..45118ce 100644 --- a/amazeing/maze_display/layout.py +++ b/amazeing/maze_display/layout.py @@ -226,7 +226,8 @@ class HBox[T](Box): heights = [(get_height(dim.y), assoc) for dim, assoc in dims] for (height, _), width, (box, _) in zip(heights, widths, self.boxes): - box.laid_out(at, IVec2(width, height)) + # ditto error that i forgot to fix :> + box.laid_out(at.copy(), IVec2(width, height)) at.x += width @@ -247,6 +248,24 @@ class FBox(Box): self.__cb(at, into) +class DBox(Box): + def __init__(self, inner: Box) -> None: + self.__inner: Box = inner + self.__prev: tuple[IVec2, IVec2] | None = None + + def mark_dirty(self) -> None: + self.__prev = None + + def dims(self) -> BVec2: + return self.__inner.dims() + + def laid_out(self, at: IVec2, into: IVec2) -> None: + if self.__prev == (at, into): + return + self.__prev = (at, into) + self.__inner.laid_out(at, into) + + def vpad_box(min_pad: int = 0, cb=lambda _at, _into: None) -> FBox: return FBox(IVec2(BInt(0), BInt(min_pad, True)), cb) diff --git a/amazeing/maze_make_empty.py b/amazeing/maze_make_empty.py new file mode 100644 index 0000000..7acb5ef --- /dev/null +++ b/amazeing/maze_make_empty.py @@ -0,0 +1,16 @@ +from collections.abc import Callable +from amazeing.maze_class.maze import Maze +from amazeing.maze_class.maze_walls import WallCoord +import random + + +def maze_make_empty( + maze: Maze, + walls_const: set[WallCoord], + callback: Callable[[Maze], None] = lambda _: None, +) -> None: + walls = [wall for wall in maze.walls_full() if wall not in walls_const] + random.shuffle(walls) + for wall in walls: + maze._remove_wall(wall) + callback(maze) diff --git a/example.conf b/example.conf index 8a41563..dc3427d 100644 --- a/example.conf +++ b/example.conf @@ -1,5 +1,5 @@ -WIDTH=50 -HEIGHT=50 +WIDTH=25 +HEIGHT=25 ENTRY=2,5 #EXIT=100,100 OUTPUT_FILE=test @@ -13,11 +13,11 @@ TILEMAP_FULL="{100,100,1000:1000,1000,1000}###{1000,1000,1000:1000,1000,1000}### TILEMAP_EMPTY="{0,0,0:0,0,0} " TILEMAP_EMPTY="{0,0,0:0,0,0} " TILEMAP_EMPTY="{0,0,0:0,0,0} " -TILEMAP_BACKGROUND_SIZE=4,4 -TILEMAP_BACKGROUND="{100,100,100:0,0,0}# " -TILEMAP_BACKGROUND="{100,100,100:0,0,0}### " -TILEMAP_BACKGROUND="{100,100,100:0,0,0} # " -TILEMAP_BACKGROUND="{100,100,100:0,0,0}# # " +TILEMAP_BACKGROUND_SIZE=8,4 +TILEMAP_BACKGROUND="{1000,1000,1000:0,0,0}## " +TILEMAP_BACKGROUND="{1000,1000,1000:0,0,0}###### " +TILEMAP_BACKGROUND="{1000,1000,1000:0,0,0} ## " +TILEMAP_BACKGROUND="{1000,1000,1000:0,0,0}## ## " MAZE_PATTERN=" # # " MAZE_PATTERN=" # # " MAZE_PATTERN=" # " -- 2.53.0