Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"can't place player with no open spaces"
floor_spaces = self.map_canvas.floor_spaces
room_floors = floor_spaces & frozenset(self.rooms_area.iter_points())
hall_floors = floor_spaces & frozenset(self.hallway_area.iter_points())
lock_floors = floor_spaces & frozenset(self.locked_area.iter_points())
points = random.sample(list(room_floors), 8)
self.map_canvas.set_creature(points[0], Salamango)
self.map_canvas.set_creature(points[1], Salamango)
self.map_canvas.set_creature(points[2], Salamango)
self.map_canvas.add_item(points[3], e.Armor)
self.map_canvas.add_item(points[4], e.Potion)
self.map_canvas.add_item(points[5], e.Potion)
self.map_canvas.add_item(points[6], e.Gem)
self.map_canvas.add_item(points[7], e.Crate)
points = random.sample(list(lock_floors), 1)
self.map_canvas.add_item(points[0], e.Crown)
def place_stuff(self):
# TODO having to override this per room is becoming increasingly
# tedious and awkward and copy-pastey.
assert self.map_canvas.floor_spaces, \
"can't place player with no open spaces"
floor_spaces = self.map_canvas.floor_spaces
room_floors = floor_spaces & frozenset(self.rooms_area.iter_points())
hall_floors = floor_spaces & frozenset(self.hallway_area.iter_points())
lock_floors = floor_spaces & frozenset(self.locked_area.iter_points())
points = random.sample(list(room_floors), 8)
self.map_canvas.set_creature(points[0], Salamango)
self.map_canvas.set_creature(points[1], Salamango)
self.map_canvas.set_creature(points[2], Salamango)
self.map_canvas.add_item(points[3], e.Armor)
self.map_canvas.add_item(points[4], e.Potion)
self.map_canvas.add_item(points[5], e.Potion)
self.map_canvas.add_item(points[6], e.Gem)
self.map_canvas.add_item(points[7], e.Crate)
points = random.sample(list(lock_floors), 1)
self.map_canvas.add_item(points[0], e.Crown)
assert self.map_canvas.floor_spaces, \
"can't place player with no open spaces"
floor_spaces = self.map_canvas.floor_spaces
room_floors = floor_spaces & frozenset(self.rooms_area.iter_points())
hall_floors = floor_spaces & frozenset(self.hallway_area.iter_points())
lock_floors = floor_spaces & frozenset(self.locked_area.iter_points())
points = random.sample(list(room_floors), 8)
self.map_canvas.set_creature(points[0], Salamango)
self.map_canvas.set_creature(points[1], Salamango)
self.map_canvas.set_creature(points[2], Salamango)
self.map_canvas.add_item(points[3], e.Armor)
self.map_canvas.add_item(points[4], e.Potion)
self.map_canvas.add_item(points[5], e.Potion)
self.map_canvas.add_item(points[6], e.Gem)
self.map_canvas.add_item(points[7], e.Crate)
points = random.sample(list(lock_floors), 1)
self.map_canvas.add_item(points[0], e.Crown)
right_side = {}
river = {}
center_factory = discrete_perlin_noise_factory(
self.region.height, resolution=3)
width_factory = discrete_perlin_noise_factory(
self.region.height, resolution=6, octaves=2)
center = random_normal_int(
self.region.center().x, self.region.width / 4 / 3)
for y in self.region.range_height():
center += (center_factory(y) - 0.5) * 3
width = width_factory(y) * 2 + 5
x0 = int(center - width / 2)
x1 = int(x0 + width + 0.5)
for x in range(x0, x1 + 1):
self.map_canvas.set_architecture(Point(x, y), e.Water)
left_side[y] = (Span(self.region.left, x0 - 1),)
right_side[y] = (Span(x1 + 1, self.region.right),)
river[y] = (Span(x0, x1),)
return Blob(left_side), Blob(river), Blob(right_side)
def place_stuff(self):
super().place_stuff()
assert self.map_canvas.floor_spaces, \
"can't place player with no open spaces"
floor = self.map_canvas.floor_spaces
points = random.sample(list(floor), 1)
self.map_canvas.add_item(points[0], e.Key)
for rect in rooms:
Room(rect).draw_to_canvas(self.map_canvas)
from flax.component import Lockable
# Add some doors for funsies.
locked_room = random.choice(rooms)
for rect in rooms:
x = random.randrange(rect.left + 1, rect.right - 1)
if rect.top > hallway.top:
side = Direction.down
else:
side = Direction.up
point = rect.edge_point(side.opposite, x, 0)
door = e.Door(Lockable(locked=rect is locked_room))
self.map_canvas.set_architecture(point, door)
self.hallway_area = Blob.from_rectangle(hallway)
self.locked_area = Blob.from_rectangle(locked_room)
self.rooms_area = reduce(operator.add, (Blob.from_rectangle(rect) for rect in rooms if rect is not locked_room))
def draw_to_canvas(self, canvas):
assert self.rect in canvas.rect
for point in self.rect.iter_points():
canvas.set_architecture(point, e.Floor)
for point, _ in self.rect.iter_border():
canvas.set_architecture(point, e.Wall)
room_floors = floor_spaces & frozenset(self.rooms_area.iter_points())
hall_floors = floor_spaces & frozenset(self.hallway_area.iter_points())
lock_floors = floor_spaces & frozenset(self.locked_area.iter_points())
points = random.sample(list(room_floors), 8)
self.map_canvas.set_creature(points[0], Salamango)
self.map_canvas.set_creature(points[1], Salamango)
self.map_canvas.set_creature(points[2], Salamango)
self.map_canvas.add_item(points[3], e.Armor)
self.map_canvas.add_item(points[4], e.Potion)
self.map_canvas.add_item(points[5], e.Potion)
self.map_canvas.add_item(points[6], e.Gem)
self.map_canvas.add_item(points[7], e.Crate)
points = random.sample(list(lock_floors), 1)
self.map_canvas.add_item(points[0], e.Crown)
def place_portal(self, portal_type, destination):
# TODO and this part is even worse yes
from flax.component import Portal
portal = portal_type(Portal(destination=destination))
# TODO not guaranteed
assert self.map_canvas.floor_spaces, \
"can't place portal with no open spaces"
floor_spaces = self.map_canvas.floor_spaces
room_floors = floor_spaces & frozenset(self.rooms_area.iter_points())
hall_floors = floor_spaces & frozenset(self.hallway_area.iter_points())
lock_floors = floor_spaces & frozenset(self.locked_area.iter_points())
if portal_type is e.StairsDown:
# Down stairs go in an unlocked room
point = random.choice(list(room_floors))
else:
# Up stairs go in the hallway
point = random.choice(list(hall_floors))
self.map_canvas.set_architecture(point, portal)