Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- An attemp to fix this: https://pastebin.com/S3GsZ4Wt
- """
- from functools import partial
- from math import floor
- from typing import Tuple, Iterable, Callable, Any, Optional
- # These are keys set with the Attribute sector_type on the room.
- # The keys None and "you" must always exist.
- SYMBOLS = {
- None: ' . ', # for rooms without a sector_type attr
- 'you': '|c@|n',
- 'Indoors': '|x[ |x]|n',
- 'Outdoors': '|G[ |G]|n',
- 'Forest': '|G[|L^|G]|n',
- 'City': '|w[ ]|n',
- }
- COLORS = {
- None: '|n',
- 'Indoors': '|x',
- 'Outdoors': '|G',
- 'Forest': '|G',
- 'City': '|w',
- }
- def first(array: Iterable, predicate: Callable) -> Any:
- """Generic tool, not bind to the logic"""
- for elem in array:
- if predicate(elem):
- return elem
- class Map(object):
- max_width: int
- max_length: int
- worm_has_mapped: dict
- curX: Optional[int]
- curY: Optional[int]
- def __init__(self, caller, max_width=21, max_length=17):
- self.caller = caller
- self.max_width = max_width
- self.max_length = max_length
- self.worm_has_mapped = {}
- self.curX = None
- self.curY = None
- if not (self.max_length % 2 or self.max_width % 2):
- return
- # Create grid
- self.grid = [[' ' for _ in range(max_length)] for _ in range(max_width)]
- self.draw_room_on_map(caller.location, ((min(max_width, max_length) - 1) / 2))
- def update_pos(self, room, exit_name):
- # this ensures the pointer variables always
- # stays up to date to where the worm is currently at.
- self.curX, self.curY = self.worm_has_mapped[room][0][0], self.worm_has_mapped[room][0][1]
- def _update_pos(x: int = 0, y: int = 0) -> Tuple[int, int]:
- return self.curX + x, self.curY + y
- # define the different cases
- switch = {
- 'east': partial(_update_pos, y=1),
- 'west': partial(_update_pos, y=-1),
- 'north': partial(_update_pos, x=-1),
- 'south': partial(_update_pos, x=1),
- 'northwest': partial(_update_pos, x=-1, y=-1),
- 'northeast': partial(_update_pos, x=-1, y=1),
- 'southwest': partial(_update_pos, x=1, y=-1),
- 'southeast': partial(_update_pos, x=1, y=1),
- }
- update_pos_func = switch.get(exit_name) # get the right case
- self.curX, self.curY = update_pos_func() if update_pos_func else (self.curX, self.curY) # update position
- def draw_room_on_map(self, room, max_distance):
- self.draw(room, max_distance)
- if max_distance == 0:
- return
- for exit_ in room.exits:
- if exit_.name not in ("north", "east", "west", "south", "northwest", "northeast", "southwest", "southeast") \
- or self.worm_has_mapped.get(exit_.destination, (0, 0))[1] > max_distance:
- continue
- self.update_pos(room, exit_.name.lower())
- self.draw_room_on_map(exit_.destination, max_distance - 1)
- def draw(self, room, max_distance):
- # draw initial caller location on map first!
- if room == self.caller.location:
- self.start_loc_on_grid()
- self.worm_has_mapped[room] = ([self.curX, self.curY], max_distance)
- return
- # map all other rooms
- self.worm_has_mapped[room] = ([self.curX, self.curY], max_distance)
- # this will use the sector_type Attribute or None if not set.
- values = (
- (room.db.hasgate, SYMBOLS[room.db.location_type].replace(" ", "|xΩ" + COLORS[room.db.location_type])),
- (room.db.hasshop, SYMBOLS[room.db.location_type].replace(" ", "|y$" + COLORS[room.db.location_type])),
- (room.db.isbakery, SYMBOLS[room.db.location_type].replace(" ", "|503H" + COLORS[room.db.location_type])),
- (room.db.entry, SYMBOLS[room.db.location_type].replace(" ", "#" + COLORS[room.db.location_type])),
- (True, SYMBOLS[room.db.location_type]),
- )
- self.grid[self.curX][self.curY] = first(values, lambda value: value[0])[1]
- def median(self, num):
- lst = list(range(num))
- return (lst[num // 2] + lst[(num - 1) // 2]) / 2.0
- def start_loc_on_grid(self):
- x = floor(self.median(self.max_width))
- y = floor(self.median(self.max_length))
- self.grid[x][y] = COLORS[self.caller.location.db.location_type] + "[" + SYMBOLS['you'] + COLORS[
- self.caller.location.db.location_type] + "]|n"
- self.curX, self.curY = x, y # updating worms current location
- def compact_grid(self):
- return [row for row in self.grid if row != self.grid[0]]
- def show_map(self):
- self.grid = self.compact_grid()
- chunks = ["\n\n"]
- for row in self.grid:
- chunks.append(" ".join(row))
- chunks.append("\n")
- return "".join(chunks)
- def debug_grid(self, adventurer):
- self.compact_grid()
- adventurer.ndb.GRID = self.grid
Advertisement
Add Comment
Please, Sign In to add comment