VikkaLorel

Map exploration

Oct 29th, 2020 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.02 KB | None | 0 0
  1. """
  2. An attemp to fix this: https://pastebin.com/S3GsZ4Wt
  3. """
  4. from functools import partial
  5. from math import floor
  6. from typing import Tuple, Iterable, Callable, Any, Optional
  7.  
  8. # These are keys set with the Attribute sector_type on the room.
  9. # The keys None and "you" must always exist.
  10. SYMBOLS = {
  11.     None: ' . ',  # for rooms without a sector_type attr
  12.     'you': '|c@|n',
  13.     'Indoors': '|x[ |x]|n',
  14.     'Outdoors': '|G[ |G]|n',
  15.     'Forest': '|G[|L^|G]|n',
  16.     'City': '|w[ ]|n',
  17. }
  18. COLORS = {
  19.     None: '|n',
  20.     'Indoors': '|x',
  21.     'Outdoors': '|G',
  22.     'Forest': '|G',
  23.     'City': '|w',
  24. }
  25.  
  26.  
  27. def first(array: Iterable, predicate: Callable) -> Any:
  28.     """Generic tool, not bind to the logic"""
  29.     for elem in array:
  30.         if predicate(elem):
  31.             return elem
  32.  
  33.  
  34. class Map(object):
  35.     max_width: int
  36.     max_length: int
  37.     worm_has_mapped: dict
  38.     curX: Optional[int]
  39.     curY: Optional[int]
  40.  
  41.     def __init__(self, caller, max_width=21, max_length=17):
  42.         self.caller = caller
  43.         self.max_width = max_width
  44.         self.max_length = max_length
  45.         self.worm_has_mapped = {}
  46.         self.curX = None
  47.         self.curY = None
  48.  
  49.         if not (self.max_length % 2 or self.max_width % 2):
  50.             return
  51.  
  52.         # Create grid
  53.         self.grid = [['   ' for _ in range(max_length)] for _ in range(max_width)]
  54.         self.draw_room_on_map(caller.location, ((min(max_width, max_length) - 1) / 2))
  55.  
  56.     def update_pos(self, room, exit_name):
  57.         # this ensures the pointer variables always
  58.         # stays up to date to where the worm is currently at.
  59.         self.curX, self.curY = self.worm_has_mapped[room][0][0], self.worm_has_mapped[room][0][1]
  60.  
  61.         def _update_pos(x: int = 0, y: int = 0) -> Tuple[int, int]:
  62.             return self.curX + x, self.curY + y
  63.  
  64.         # define the different cases
  65.         switch = {
  66.             'east': partial(_update_pos, y=1),
  67.             'west': partial(_update_pos, y=-1),
  68.             'north': partial(_update_pos, x=-1),
  69.             'south': partial(_update_pos, x=1),
  70.             'northwest': partial(_update_pos, x=-1, y=-1),
  71.             'northeast': partial(_update_pos, x=-1, y=1),
  72.             'southwest': partial(_update_pos, x=1, y=-1),
  73.             'southeast': partial(_update_pos, x=1, y=1),
  74.         }
  75.  
  76.         update_pos_func = switch.get(exit_name)  # get the right case
  77.         self.curX, self.curY = update_pos_func() if update_pos_func else (self.curX, self.curY)  # update position
  78.  
  79.     def draw_room_on_map(self, room, max_distance):
  80.         self.draw(room, max_distance)
  81.  
  82.         if max_distance == 0:
  83.             return
  84.  
  85.         for exit_ in room.exits:
  86.             if exit_.name not in ("north", "east", "west", "south", "northwest", "northeast", "southwest", "southeast") \
  87.                     or self.worm_has_mapped.get(exit_.destination, (0, 0))[1] > max_distance:
  88.                 continue
  89.  
  90.             self.update_pos(room, exit_.name.lower())
  91.             self.draw_room_on_map(exit_.destination, max_distance - 1)
  92.  
  93.     def draw(self, room, max_distance):
  94.         # draw initial caller location on map first!
  95.         if room == self.caller.location:
  96.             self.start_loc_on_grid()
  97.             self.worm_has_mapped[room] = ([self.curX, self.curY], max_distance)
  98.             return
  99.  
  100.         # map all other rooms
  101.         self.worm_has_mapped[room] = ([self.curX, self.curY], max_distance)
  102.         # this will use the sector_type Attribute or None if not set.
  103.         values = (
  104.             (room.db.hasgate, SYMBOLS[room.db.location_type].replace(" ", "|xΩ" + COLORS[room.db.location_type])),
  105.             (room.db.hasshop, SYMBOLS[room.db.location_type].replace(" ", "|y$" + COLORS[room.db.location_type])),
  106.             (room.db.isbakery, SYMBOLS[room.db.location_type].replace(" ", "|503H" + COLORS[room.db.location_type])),
  107.             (room.db.entry, SYMBOLS[room.db.location_type].replace(" ", "#" + COLORS[room.db.location_type])),
  108.             (True, SYMBOLS[room.db.location_type]),
  109.         )
  110.  
  111.         self.grid[self.curX][self.curY] = first(values, lambda value: value[0])[1]
  112.  
  113.     def median(self, num):
  114.         lst = list(range(num))
  115.         return (lst[num // 2] + lst[(num - 1) // 2]) / 2.0
  116.  
  117.     def start_loc_on_grid(self):
  118.         x = floor(self.median(self.max_width))
  119.         y = floor(self.median(self.max_length))
  120.  
  121.         self.grid[x][y] = COLORS[self.caller.location.db.location_type] + "[" + SYMBOLS['you'] + COLORS[
  122.             self.caller.location.db.location_type] + "]|n"
  123.         self.curX, self.curY = x, y  # updating worms current location
  124.  
  125.     def compact_grid(self):
  126.         return [row for row in self.grid if row != self.grid[0]]
  127.  
  128.     def show_map(self):
  129.         self.grid = self.compact_grid()
  130.  
  131.         chunks = ["\n\n"]
  132.         for row in self.grid:
  133.             chunks.append(" ".join(row))
  134.             chunks.append("\n")
  135.         return "".join(chunks)
  136.  
  137.     def debug_grid(self, adventurer):
  138.         self.compact_grid()
  139.         adventurer.ndb.GRID = self.grid
  140.  
Advertisement
Add Comment
Please, Sign In to add comment