Implement method add_loops along with tests

This commit is contained in:
Charles Peyrat 2021-01-10 21:30:18 +01:00
parent 949555ffff
commit 26e66a5796
2 changed files with 108 additions and 23 deletions

View File

@ -6,7 +6,6 @@ from typing import List, Tuple
from ..interfaces import Map, Tile
DEFAULT_PARAMS = {
"width": 120,
"height": 35,
@ -21,8 +20,28 @@ DEFAULT_PARAMS = {
"max_h_corr": 12,
"large_circular_room": .10,
"circular_holes": .5,
"loop_tries" : 40,
"loop_max" : 5,
"loop_threshold" : 15,
}
def dist(level, y1, x1, y2, x2):
copy = [[t for t in l] for l in level]
dist = -1
queue, next_queue = [[y1, x1]], [0]
while next_queue:
next_queue = []
dist += 1
while queue:
y, x = queue.pop()
copy[y][x] = Tile.EMPTY
if y == y2 and x == x2:
return dist
for y, x in Map.neighbourhood(copy, y, x):
if copy[y][x].can_walk():
next_queue.append([y, x])
queue = next_queue
return -1
class Generator:
def __init__(self, params: dict = None):
@ -66,6 +85,39 @@ class Generator:
if room[ry][rx] == Tile.FLOOR:
level[y - door_y + ry][x - door_x + rx] = Tile.FLOOR
@staticmethod
def add_loop(level: List[List[Tile]], y: int, x: int) -> None:
h, w = len(level), len(level[0])
if level[y][x] != Tile.EMPTY:
return False
# loop over both axis
for dx, dy in [[0, 1], [1, 0]]:
# then we find two floor tiles, exiting if we ever move oob
y1, x1, y2, x2 = y, x, y, x
while x1 >= 0 and y1 >= 0 and level[y1][x1] == Tile.EMPTY:
y1, x1 = y1 - dy, x1 - dx
while x2 < w and y2 < h and level[y2][x2] == Tile.EMPTY:
y2, x2 = y2 + dy, x2 + dx
if not(0 <= x1 <= x2 < w and 0 <= y1 <= y2 < h):
continue
# if adding the path would make the two tiles significantly closer
# and its sides don't touch already placed terrain, build it
def verify_sides():
for Dx, Dy in [[dy, dx], [-dy, -dx]]:
for i in range(1, y2-y1+x2-x1):
if not(0<= y1+Dy+i*dy < h and 0 <= x1+Dx+i*dx < w) or \
level[y1+Dy+i*dy][x1+Dx+i*dx].can_walk():
return False
return True
if dist(level, y1, x1, y2, x2) < 20 and verify_sides():
y, x = y1+dy, x1+dx
while level[y][x] == Tile.EMPTY:
level[y][x] = Tile.FLOOR
y, x = y+dy, x+dx
return True
return False
@staticmethod
def place_walls(level: List[List[Tile]]) -> None:
h, w = len(level), len(level[0])
@ -87,9 +139,29 @@ class Generator:
return h_sup, w_sup, h_off, w_off
return 0, 0, 0, 0
def attach_door(self, room: List[List[Tile]], h_sup: int, w_sup: int,
h_off: int, w_off: int) \
-> Tuple[int, int, int, int]:
@staticmethod
def build_door(room, y, x, dy, dx, length):
rh, rw = len(room), len(room[0])
# verify we are pointing away from a floor tile
if not(0 <= y - dy < rh and 0 <= x - dx < rw) \
or room[y - dy][x - dx] != Tile.FLOOR:
return False
# verify there's no other floor tile around us
for ny, nx in [[y + dy, x + dx], [y - dx, x - dy],
[y + dx, x + dy]]:
if 0 <= ny < rh and 0 <= nx < rw \
and room[ny][nx] != Tile.EMPTY:
return False
for i in range(length):
if room[y + i * dy][x + i * dx] != Tile.EMPTY:
return False
for i in range(length):
room[y + i * dy][x + i * dx] = Tile.FLOOR
return True
@staticmethod
def attach_door(room: List[List[Tile]], h_sup: int, w_sup: int,
h_off: int, w_off: int) -> Tuple[int, int, int, int]:
length = h_sup + w_sup
dy, dx = 0, 0
if length > 0:
@ -108,25 +180,10 @@ class Generator:
shuffle(yxs)
for pos in yxs:
y, x = pos // rw, pos % rw
if room[y][x] == Tile.EMPTY:
# verify we are pointing away from a floor tile
if not(0 <= y - dy < rh and 0 <= x - dx < rw) \
or room[y - dy][x - dx] != Tile.FLOOR:
continue
# verify there's no other floor tile around us
for ny, nx in [[y + dy, x + dx], [y - dx, x - dy],
[y + dx, x + dy]]:
if 0 <= ny < rh and 0 <= nx < rw \
and room[ny][nx] != Tile.EMPTY:
break
else:
for i in range(length):
if room[y + i * dy][x + i * dx] != Tile.EMPTY:
break
else:
for i in range(length):
room[y + i * dy][x + i * dx] = Tile.FLOOR
break
if room[y][x] == Tile.EMPTY and \
Generator.build_door(room, y, x, dy, dx, length):
break
return y + length * dy, x + length * dx, dy, dx
def create_circular_room(self) -> Tuple[List[List[Tile]], int, int,
@ -204,6 +261,12 @@ class Generator:
# post-processing
self.place_walls(level)
tries, loops = 0, 0
while tries < self.params["loop_tries"] and \
loops < self.params["loop_max"]:
tries += 1
y, x = randint(0, height-1), randint(0, width-1)
loops += self.add_loop(level, y, x)
# place an exit ladder
y, x = randint(0, height - 1), randint(0, width - 1)

View File

@ -7,11 +7,19 @@ from typing import List
from squirrelbattle.interfaces import Map, Tile
from squirrelbattle.mapgeneration import broguelike
from squirrelbattle.display.texturepack import TexturePack
class TestBroguelike(unittest.TestCase):
def setUp(self) -> None:
self.generator = broguelike.Generator()
self.stom = lambda x : Map.load_from_string("0 0\n" + x)
self.mtos = lambda x : x.draw_string(TexturePack.ASCII_PACK)
def test_dist(self):
m = self.stom(".. ..\n ... ")
distance = broguelike.dist(m.tiles, 0, 0, 0, 4)
self.assertEqual(distance, 6)
def is_connex(self, grid: List[List[Tile]]) -> bool:
h, w = len(grid), len(grid[0])
@ -29,3 +37,17 @@ class TestBroguelike(unittest.TestCase):
def test_connexity(self) -> None:
m = self.generator.run()
self.assertTrue(self.is_connex(m.tiles))
def test_doors(self) -> None:
# corridors shouldn't loop back into the room
pass
def test_loops(self) -> None:
m = self.stom(3*".. ..\n")
self.generator.add_loop(m.tiles, 1, 3)
s = self.mtos(m)
self.assertEqual(s, ".. ..\n.......\n.. ..")
self.assertFalse(self.generator.add_loop(m.tiles, 0, 0))
m = self.stom("...\n. .\n...")
self.assertFalse(self.generator.add_loop(m.tiles, 1, 1))