From 641f5c7872134f585883f6b76eaa1be16ff9c4f3 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 8 Jan 2021 07:38:47 +0100 Subject: [PATCH] Make generation more sparse by asking for extra space around rooms; also add out of bounds option to Map.neighbourhood --- squirrelbattle/interfaces.py | 6 +++--- squirrelbattle/mapgeneration/broguelike.py | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 4e5f9ff..ad0e3b3 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -194,7 +194,7 @@ class Map: self.add_entity(dictclasses[entisave["type"]](**entisave)) @staticmethod - def neighbourhood(grid, y, x, large=False): + def neighbourhood(grid, y, x, large=False, oob=False): """ Returns up to 8 nearby coordinates, in a 3x3 square around the input coordinate if large is set to True, or in a 5-square cross by default. Does not return coordinates if they are out @@ -203,12 +203,12 @@ class Map: height, width = len(grid), len(grid[0]) neighbours = [] if large: - dyxs = product([-1, 0, 1], [-1, 0, 1]) + dyxs = [[dy, dx] for dy, dx in product([-1, 0, 1], [-1, 0, 1])] dyxs = dyxs[:5] + dyxs[6:] else: dyxs = [[0, -1], [0, 1], [-1, 0], [1, 0]] for dy, dx in dyxs: - if 0 <= y+dy < height and 0 <= x+dx < width: + if oob or (0 <= y+dy < height and 0 <= x+dx < width): neighbours.append([y+dy, x+dx]) return neighbours diff --git a/squirrelbattle/mapgeneration/broguelike.py b/squirrelbattle/mapgeneration/broguelike.py index b986b8f..96c7153 100644 --- a/squirrelbattle/mapgeneration/broguelike.py +++ b/squirrelbattle/mapgeneration/broguelike.py @@ -39,9 +39,16 @@ class Generator: for rx in range(rw): if room[ry][rx] == Tile.FLOOR: ly, lx = y + ry - door_y, x + rx - door_x + # tile must be in bounds and empty if not(0 <= ly < lh and 0 <= lx < lw) or \ level[ly][lx] == Tile.FLOOR: return False + # so do all neighbouring tiles bc we may + # need to place walls there eventually + for ny, nx in Map.neighbourhood(level, ly, lx, large=True, oob=True): + if not(0 <= ny < lh and 0 <= nx < lw) or \ + level[ny][nx] != Tile.EMPTY: + return False return True @staticmethod