Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def generate(self):
self.map_canvas.clear(CaveWall)
# First create a bunch of hallways and rooms.
# For now, just carve a big area, run a hallway through the middle, and
# divide either side into rooms.
area = Room.randomize(self.region, minimum_size=self.region.size // 2)
area.draw_to_canvas(self.map_canvas)
center = area.rect.center()
y0 = center.y - 2
y1 = center.y + 2
hallway = Rectangle(origin=Point(area.rect.left, center.y - 2), size=Size(area.rect.width, 5))
Room(hallway).draw_to_canvas(self.map_canvas)
top_space = area.rect.replace(bottom=hallway.top)
bottom_space = area.rect.replace(top=hallway.bottom)
room = Room(room_rect)
cave_area = (
Blob.from_rectangle(self.region)
- Blob.from_rectangle(room_rect)
)
self.cave_region = cave_area
walls = [point for (point, _) in self.region.iter_border()]
floors = []
for point, edge in room_rect.iter_border():
if edge is side or edge.adjacent_to(side):
floors.append(point)
floors.append(point + side)
generate_caves(
self.map_canvas, cave_area, CaveWall,
force_walls=walls, force_floors=floors,
)
room.draw_to_canvas(self.map_canvas)
# OK, now draw a gate in the middle of the side wall
if side is Direction.left:
x = room_rect.left
else:
x = room_rect.right
mid_y = room_rect.top + room_rect.height // 2
if room_rect.height % 2 == 1:
min_y = mid_y - 1
max_y = mid_y + 1
else:
min_y = mid_y - 2
for blob in (left_bank, right_bank):
paths = self.flood_valleys(blob, local_minima, noise)
for path_point in paths:
self.map_canvas.set_architecture(path_point, e.Dirt)
# Whoops time for another step: generating a surrounding cave wall.
for edge in Direction.orthogonal:
width = self.region.edge_length(edge)
wall_noise = discrete_perlin_noise_factory(width, resolution=6)
for n in self.region.edge_span(edge):
offset = int(wall_noise(n) * 4 + 1)
for m in range(offset):
point = self.region.edge_point(edge, n, m)
self.map_canvas.set_architecture(point, e.CaveWall)
# "Feature", where i can check whether it has already claimed a
# tile, or draw it later, or whatever.
if self.map_canvas._arch_grid[point] is not CaveWall:
distance = abs(dx) + abs(dy)
ruination = random_normal_range(0, 0.2) + distance * 0.2
self.map_canvas.set_architecture(
point, e.Rubble(Breakable(ruination)))
# And apply some light ruination to the inside of the room
border = list(room_rect.iter_border())
# TODO don't do this infinitely; give up after x tries
while True:
point, edge = random.choice(border)
if self.map_canvas._arch_grid[point + edge] is CaveWall:
break
self.map_canvas.set_architecture(point, CaveWall)
self.map_canvas.set_architecture(point - edge, CaveWall)
# TODO this would be neater if it were a slightly more random pattern
for direction in (
Direction.up, Direction.down, Direction.left, Direction.right):
self.map_canvas.set_architecture(
point - edge + direction, CaveWall)
max_y = mid_y + 1
for y in range(min_y, max_y + 1):
self.map_canvas.set_architecture(Point(x, y), KadathGate)
# Beat up the border of the room near the gate
y = random.choice(
tuple(range(room_rect.top, min_y))
+ tuple(range(max_y + 1, room_rect.bottom))
)
for dx in range(-2, 3):
for dy in range(-2, 3):
point = Point(x + dx, y + dy)
# TODO i think what i may want is to have the cave be a
# "Feature", where i can check whether it has already claimed a
# tile, or draw it later, or whatever.
if self.map_canvas._arch_grid[point] is not CaveWall:
distance = abs(dx) + abs(dy)
ruination = random_normal_range(0, 0.2) + distance * 0.2
self.map_canvas.set_architecture(
point, e.Rubble(Breakable(ruination)))
# And apply some light ruination to the inside of the room
border = list(room_rect.iter_border())
# TODO don't do this infinitely; give up after x tries
while True:
point, edge = random.choice(border)
if self.map_canvas._arch_grid[point + edge] is CaveWall:
break
self.map_canvas.set_architecture(point, CaveWall)
self.map_canvas.set_architecture(point - edge, CaveWall)
# TODO this would be neater if it were a slightly more random pattern
for direction in (
def __init__(self, size):
self.rect = size.to_rect(Point.origin())
# TODO i think using types instead of entities /most of the time/ is
# more trouble than it's worth
self._arch_grid = {
point: CaveWall for point in self.rect.iter_points()}
self._item_grid = {point: [] for point in self.rect.iter_points()}
self._creature_grid = {
point: None for point in self.rect.iter_points()}
self.floor_spaces = set()
point, e.Rubble(Breakable(ruination)))
# And apply some light ruination to the inside of the room
border = list(room_rect.iter_border())
# TODO don't do this infinitely; give up after x tries
while True:
point, edge = random.choice(border)
if self.map_canvas._arch_grid[point + edge] is CaveWall:
break
self.map_canvas.set_architecture(point, CaveWall)
self.map_canvas.set_architecture(point - edge, CaveWall)
# TODO this would be neater if it were a slightly more random pattern
for direction in (
Direction.up, Direction.down, Direction.left, Direction.right):
self.map_canvas.set_architecture(
point - edge + direction, CaveWall)