home · contact · privacy
Add Hex map capabilities.
[plomrogue2-experiments] / new2 / plomrogue / mapping.py
index 1847f691a79f7cef829a3a6bc1b4323397e38488..e0a59d8e225f56af79b0b516bb7f6e366de164a4 100644 (file)
@@ -1,4 +1,5 @@
 import collections
+from plomrogue.errors import ArgError
 
 
 
@@ -12,3 +13,124 @@ class YX(collections.namedtuple('YX', ('y', 'x'))):
 
     def __str__(self):
         return 'Y:%s,X:%s' % (self.y, self.x)
+
+
+
+class MapGeometry():
+
+    def __init__(self, size):
+        self.size = size
+
+    def get_directions(self):
+        directions = []
+        for name in dir(self):
+            if name[:5] == 'move_':
+                directions += [name[5:]]
+        return directions
+
+    def get_neighbors(self, pos):
+        neighbors = {}
+        for direction in self.get_directions():
+            neighbors[direction] = self.move(pos, direction)
+        return neighbors
+
+    def move(self, start_pos, direction):
+        mover = getattr(self, 'move_' + direction)
+        target = mover(start_pos)
+        if target.y < 0 or target.x < 0 or \
+                target.y >= self.size.y or target.x >= self.size.x:
+            return None
+        return target
+
+
+
+class MapGeometryWithLeftRightMoves(MapGeometry):
+
+    def move_LEFT(self, start_pos):
+        return YX(start_pos.y, start_pos.x - 1)
+
+    def move_RIGHT(self, start_pos):
+        return YX(start_pos.y, start_pos.x + 1)
+
+
+
+class MapGeometrySquare(MapGeometryWithLeftRightMoves):
+
+    def move_UP(self, start_pos):
+        return YX(start_pos.y - 1, start_pos.x)
+
+    def move_DOWN(self, start_pos):
+        return YX(start_pos.y + 1, start_pos.x)
+
+
+
+class MapGeometryHex(MapGeometryWithLeftRightMoves):
+
+    def move_UPLEFT(self, start_pos):
+        start_indented = start_pos.y % 2
+        if start_indented:
+            return YX(start_pos.y - 1, start_pos.x)
+        else:
+            return YX(start_pos.y - 1, start_pos.x - 1)
+
+    def move_UPRIGHT(self, start_pos):
+        start_indented = start_pos.y % 2
+        if start_indented:
+            return YX(start_pos.y - 1, start_pos.x + 1)
+        else:
+            return YX(start_pos.y - 1, start_pos.x)
+
+    def move_DOWNLEFT(self, start_pos):
+        start_indented = start_pos.y % 2
+        if start_indented:
+            return YX(start_pos.y + 1, start_pos.x)
+        else:
+            return YX(start_pos.y + 1, start_pos.x - 1)
+
+    def move_DOWNRIGHT(self, start_pos):
+        start_indented = start_pos.y % 2
+        if start_indented:
+            return YX(start_pos.y + 1, start_pos.x + 1)
+        else:
+            return YX(start_pos.y + 1, start_pos.x)
+
+
+
+class Map():
+
+    def __init__(self, map_size):
+        self.size = map_size
+        self.terrain = '.' * self.size_i
+
+    def __getitem__(self, yx):
+        return self.terrain[self.get_position_index(yx)]
+
+    def __setitem__(self, yx, c):
+        pos_i = self.get_position_index(yx)
+        if type(c) == str:
+            self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
+        else:
+            self.terrain[pos_i] = c
+
+    @property
+    def size_i(self):
+        return self.size.y * self.size.x
+
+    def set_line(self, y, line):
+        height_map = self.size.y
+        width_map = self.size.x
+        if y >= height_map:
+            raise ArgError('too large row number %s' % y)
+        width_line = len(line)
+        if width_line != width_map:
+            raise ArgError('map line width %s unequal map width %s' % (width_line, width_map))
+        self.terrain = self.terrain[:y * width_map] + line +\
+                       self.terrain[(y + 1) * width_map:]
+
+    def get_position_index(self, yx):
+        return yx.y * self.size.x + yx.x
+
+    def lines(self):
+        width = self.size.x
+        for y in range(self.size.y):
+            yield (y, self.terrain[y * width:(y + 1) * width])