Skip to content

Instantly share code, notes, and snippets.

@andreivasiliu
Created March 15, 2020 12:30
Show Gist options
  • Save andreivasiliu/cc854f8f8316edcc8c2da7f9fda9afd5 to your computer and use it in GitHub Desktop.
Save andreivasiliu/cc854f8f8316edcc8c2da7f9fda9afd5 to your computer and use it in GitHub Desktop.
def show_map() -> Iterable[str]:
yield f"The map has {len(bat_map)} rooms."
if not current_room:
return
yield f"Current room: {current_room.title}"
links = bat_links.get(current_room.room_id)
if links:
inc = len(links.incoming)
out = len(links.outgoing)
yield f"Current room has {inc} incoming and {out} outgoing links."
grid: Dict[Tuple[int, int], Room] = {}
place_rooms(current_room, grid, 0, 0)
for y in range(-4, 5):
rooms = [
"[ ]" if (x, y) in grid else " "
for x in range(-5, 5)
]
yield ''
yield ' '.join(rooms)
def get_xy_in(direction: str, x: int, y: int) -> Tuple[int, int]:
direction_offset = {
'north': (0, 1),
'northeast': (1, 1),
'east': (1, 0),
'southeast': (1, -1),
'south': (0, -1),
'southwest': (-1, -1),
'west': (-1, 0),
'northwest': (-1, 1),
'up': (2, 2),
'down': (-2, -2),
}
offset = direction_offset.get(direction, (2, -2))
return x + offset[0], y + offset[1]
def place_rooms(room: Room, grid: Dict[Tuple[int, int], Room], x: int, y: int):
if x < -5 or x > 5 or y < -5 or y > 5 or (x, y) in grid:
return
grid[(x, y)] = room
links = bat_links.get(room.room_id)
if not links:
return
for direction in set(links.outgoing).union(set(links.incoming)):
outgoing_room = links.outgoing.get(direction)
rooms = [outgoing_room] if outgoing_room else []
rooms += links.incoming.get(direction, [])
rooms = set(rooms)
log("Rooms: {}", rooms)
if len(rooms) == 1:
other_room = bat_map[rooms[0]]
place_rooms(other_room, grid, *get_xy_in(direction, x, y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment