Revamp door placing algorithm so that it generates cleaner doors; also remove lone starting room door from level

This commit is contained in:
Charles Peyrat 2021-01-08 07:36:31 +01:00
parent 5ba07afc9f
commit 605696dddd
1 changed files with 13 additions and 6 deletions

View File

@ -89,18 +89,24 @@ class Generator:
else: else:
dx = -1 if random() < .5 else 1 dx = -1 if random() < .5 else 1
yxs = [i for i in range(len(room) * len(room[0]))] rh, rw = len(room), len(room[0])
yxs = [i for i in range(rh * rw)]
shuffle(yxs) shuffle(yxs)
for pos in yxs: for pos in yxs:
y, x = pos // len(room[0]), pos % len(room[0]) y, x = pos // rw, pos % rw
if room[y][x] == Tile.EMPTY: if room[y][x] == Tile.EMPTY:
if room[y-dy][x-dx] == Tile.FLOOR: # verify we are pointing away from a floor tile
build_here = True 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(l): for i in range(l):
if room[y+i*dy][x+i*dx] != Tile.EMPTY: if room[y+i*dy][x+i*dx] != Tile.EMPTY:
build_here = False
break break
if build_here: else:
for i in range(l): for i in range(l):
room[y+i*dy][x+i*dx] == Tile.FLOOR room[y+i*dy][x+i*dx] == Tile.FLOOR
break break
@ -147,6 +153,7 @@ class Generator:
mem, self.params["corridor_chance"] = self.params["corridor_chance"], 0 mem, self.params["corridor_chance"] = self.params["corridor_chance"], 0
starting_room, _, _, _, _ = self.create_random_room() starting_room, _, _, _, _ = self.create_random_room()
self.place_room(level, height//2, width//2, starting_room, 0, 0) self.place_room(level, height//2, width//2, starting_room, 0, 0)
level[0][0] = Tile.EMPTY
self.params["corridor_chance"] = mem self.params["corridor_chance"] = mem
# find a starting position # find a starting position