git.fiddlerwoaroof.com
Raw Blame History
from __future__ import print_function
import numpy as np

class CoordPair(object):
   def __init__(self, fst, snd=None):
      if snd == None: fst, snd = fst
      self.fst, self.snd = fst,snd

   @property
   def pair(self):
      return self.fst, self.snd

   def __len__(self): return 2
   def __iter__(self): return iter(self.pair)
   def __getitem__(self, idx):
      if idx == 0: return self.fst
      elif idx == 1: return self.snd
      else: raise KeyError("Only two elements in a coordpair")

   def transform(self, *transforms):
      '''Note: mul _then_ add'''
      f,s = self.pair
      for transform in transforms:
         x = transform.get('mul', (1,1))
         if not hasattr(x, '__iter__'): x=(x,x)
         f1,f2 = x
         f *= f1
         s *= f2
         x = transform.get('add', (0,0))
         if not hasattr(x, '__iter__'): x=(x,x)
         f1,f2 = x
         f += f1
         s += f2
      return f,s

   def __ne__(self, other):
      return not self.__eq__(other)
   def __rne__(self, other):
      return not self.__eq__(other)

   def __eq__(self, other):
      a,b = self
      c,d = other
      return a==c and b==d
   def __req__(self, other):
      return self == other

   def __add__(self, other):
      return self.__class__(self.transform({'add': other}))
   def __radd__(self, other):
      return self.__class__(self.transform({'add': other}))

   def __mul__(self, other):
      return self.__class__(self.transform({'mul': other}))
   def __rmul__(self, other):
      return self.__class__(self.transform({'mul': other}))


   def __sub__(self, other):
      a,b = other
      return self.__class__(self.transform({'add': (-a,-b)}))
   def __rsub__(self, other):
      a,b = other
      return self.__class__(self.transform({'add': (-a,-b)}))

   def __div__(self, other):
      return self.__class__(self.transform({'mul': 1.0/other}))
   def __rdiv__(self, other):
      return self.__class__(self.transform({'mul': 1.0/other}))

   def __repr__(self):
      return '%s(%s,%s)' % (self.__class__.__name__, self.fst, self.snd)

class MazeDigger(object):
   def __init__(self, traveller, size, multiplier=3):
      self.t = traveller
      self.size = CoordPair(*size)*multiplier
      self.mult = multiplier
      self.map = np.ones(self.size+1, int)*2

   def clamp(self, start):
      if start.fst < 0: start.fst = 0
      elif start.fst >= self.size.fst: start.fst = self.size.fst-1
      if start.snd < 0: start.snd = 0
      elif start.snd >= self.size.snd: start.snd = self.size.snd-1
      return start

   def draw_line(self, fro, to):
      (x1,y1),  (x2,y2) = fro, to
      if x2<x1: x1,x2 = x2,x1
      if y2<y1: y1,y2 = y2,y1
      x1*=self.mult;x2*=self.mult
      x2 += 1
      y1*=self.mult;y2*=self.mult
      y2 += 1
      self.map[x1+1:x2+1,y1+1:y2+1] = 3

   def make_map(self):
      for x,y in self.t.visited:
         self.map[x*self.mult:(x+1)*self.mult, y*self.mult:(y+1)*self.mult] = 0
      for fro, to in self.t.connections:
         self.draw_line(fro, to)
      x,y = self.t.pos
      #self.map[x*self.mult:(x+1)*self.mult, y*self.mult:(y+1)*self.mult] = 1
      return self

   def show_map(self):
      nmap = np.rollaxis(self.map, 1)
      for row in nmap:
         for col in row:
            if col == 3: print('.', end='')
            if col == 2: print('#', end='')
            if col == 1: print('o', end='')
            if col == 0: print('x', end='')
         print()

   def tcod_show_map(self):
      nmap = self.map.copy()
      nmap = nmap.max() - nmap
      omin = nmap.min()
      nmap -= omin
      omax = nmap.max()
      nmap *= 255
      nmap /= omax
      nmap = np.rollaxis(nmap, 1)
      tc.console_fill_background(0, nmap,nmap,nmap)


if __name__ == '__main__':
   import sys
   aldus = __import__(sys.argv[1])
   import time
   import libtcodpy as tc

   self = MazeDigger(aldus.t, (aldus.x,aldus.y))

   tc.console_init_root(self.size.fst+1, self.size.snd+1, 'The Title', False, 2)
   tc.sys_set_fps(30)

   key, mouse = tc.Key(), tc.Mouse()

   walk_result = False
   while True:
      tc.sys_check_for_event(tc.EVENT_KEY_PRESS|tc.EVENT_MOUSE|tc.KEY_PRESSED, key, mouse)
      if key.c == ord('q'): break

      if not walk_result:
         walk_result = aldus.t.random_walk(aldus.maze)
         self.make_map()

         tc.sys_save_screenshot('tmp.png')
         self.tcod_show_map()

      tc.console_flush()