Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# - none of the decay applied to the room may block off any of its
# interesting features
# TODO it would be nice if i could really write all this without ever
# having to hardcode a specific direction, so the logic could always be
# rotated freely
side = random.choice([Direction.left, Direction.right])
# TODO assert region is big enough
room_size = Size(
random_normal_range(9, int(self.region.width * 0.4)),
random_normal_range(9, int(self.region.height * 0.4)),
)
room_position = self.region.center() - room_size // 2
room_position += Point(
random_normal_int(0, self.region.width * 0.1),
random_normal_int(0, self.region.height * 0.1),
)
room_rect = Rectangle(room_position, room_size)
self.room_region = room_rect
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 = []
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)
rooms = []
for orig_space in (top_space, bottom_space):
space = orig_space
# This includes walls!
minimum_width = 7
# Note that the rooms overlap where they touch, so we subtract one
# from both the total width and the minimum width, in effect
# ignoring all the walls on one side
maximum_rooms = (space.width - 1) // (minimum_width - 1)
# The maximum number of rooms that will fit also affects how much
# wiggle room we're willing to have. For example, if at most 3 rooms
random_normal_range(9, int(self.region.height * 0.4)),
)
room_position = self.region.center() - room_size // 2
room_position += Point(
random_normal_int(0, self.region.width * 0.1),
random_normal_int(0, self.region.height * 0.1),
)
room_rect = Rectangle(room_position, room_size)
self.room_region = room_rect
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)
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
)
room_position = self.region.center() - room_size // 2
room_position += Point(
random_normal_int(0, self.region.width * 0.1),
random_normal_int(0, self.region.height * 0.1),
)
room_rect = Rectangle(room_position, room_size)
self.room_region = room_rect
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
# rotated freely
side = random.choice([Direction.left, Direction.right])
# TODO assert region is big enough
room_size = Size(
random_normal_range(9, int(self.region.width * 0.4)),
random_normal_range(9, int(self.region.height * 0.4)),
)
room_position = self.region.center() - room_size // 2
room_position += Point(
random_normal_int(0, self.region.width * 0.1),
random_normal_int(0, self.region.height * 0.1),
)
room_rect = Rectangle(room_position, room_size)
self.room_region = room_rect
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(
def __init__(self, player):
walker = urwid.SimpleListWalker([])
self.listbox = urwid.ListBox(walker)
from flax.component import IContainer
for item in IContainer(player).inventory:
item_w = InventoryItem(item)
urwid.connect_signal(item_w, 'fire', lambda *a: self._emit('close', *a))
self.listbox.body.append(item_w)
super().__init__(urwid.LineBox(self.listbox))
Render(sprite=Sprite.ladder, color='wood'),
name='ladder',
)
CaveWall = Architecture(
Solid,
Render(sprite=Sprite.halftone, color='rock'),
name='wall',
)
Wall = Architecture(
Solid,
Render(sprite=Sprite.fill, color='wall'),
name='wall',
)
Pillar = Architecture(
Solid,
Render(sprite=Sprite.pillar, color='rock'),
name='pillar',
)
KadathGate = Architecture(
Solid,
Render(sprite=Sprite.gate, color='gate'),
name='wall',
)
Water = Architecture(
Solid,
Render(sprite=Sprite.water, color='water'),
name='water',
)
Bridge = Architecture(
Empty,
Render(sprite=Sprite.bridge, color='bridge'),
name='bridge',
Breakable(health=10),
HealthRender(
(2, Sprite.rubble4, 'decay2'),
(2, Sprite.rubble3, 'decay2'),
(2, Sprite.rubble2, 'decay2'),
(2, Sprite.rubble1, 'decay3'),
),
name='rubble',
)
Ruin = Architecture(
Solid,
Breakable(health=10),
HealthRender(
(1, Sprite.ruin4a, 'decay3'),
(1, Sprite.ruin4b, 'decay3'),
(1, Sprite.ruin4c, 'decay3'),
(1, Sprite.ruin4d, 'decay3'),
(5, Sprite.ruin3a, 'decay2'),
(5, Sprite.ruin3b, 'decay2'),
(3, Sprite.ruin2, 'decay1'),
(10, Sprite.ruin1e, 'decay0'),
),
name='ruin',
)
# -----------------------------------------------------------------------------
# Creatures
Creature = partial(EntityType, Solid, Container, Bodied, layer=Layer.creature)
Player = Creature(