home · contact · privacy
Fix MAP command geometries returning invalid classes.
[plomrogue2-experiments] / server_ / map_.py
index 96021be8501951c48ab5402d170d4f0e9a3c67f6..b59eceeee6174761ed8e6ffef87c84f53614f189 100644 (file)
@@ -12,7 +12,10 @@ class Map(game_common.Map):
 
     def __setitem__(self, yx, c):
         pos_i = self.get_position_index(yx)
-        self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
+        if type(c) == str:
+            self.terrain = self.terrain[:pos_i] + c + self.terrain[pos_i + 1:]
+        else:
+            self.terrain[pos_i] = c
 
     def __iter__(self):
         """Iterate over YX position coordinates."""
@@ -30,9 +33,9 @@ class Map(game_common.Map):
             yield (y, self.terrain[y * width:(y + 1) * width])
 
     def get_fov_map(self, yx):
-        # TODO: Currently only have MapFovHex. Provide MapFovSquare.
-        fov_map_class = map_manager.get_map_class('Fov' + self.geometry)
-        return fov_map_class(self, yx)
+        fov_class_name = 'Fov' + self.__class__.__name__
+        fov_class = globals()[fov_class_name]
+        return fov_class(self, yx)
 
     # The following is used nowhere, so not implemented.
     #def items(self):
@@ -97,9 +100,9 @@ class MapHex(Map):
 
     def move_DOWNLEFT(self, start_pos):
         if start_pos[0] % 2 == 1:
-            return [start_pos[0] + 1, start_pos[1] - 1]
+             return [start_pos[0] + 1, start_pos[1] - 1]
         else:
-            return [start_pos[0] + 1, start_pos[1]]
+               return [start_pos[0] + 1, start_pos[1]]
 
     def move_DOWNRIGHT(self, start_pos):
         if start_pos[0] % 2 == 1:
@@ -107,8 +110,36 @@ class MapHex(Map):
         else:
             return [start_pos[0] + 1, start_pos[1] + 1]
 
+    def get_neighbors(self, pos):
+        # DOWNLEFT, DOWNRIGHT, LEFT, RIGHT, UPLEFT, UPRIGHT (alphabetically)
+        neighbors = [None, None, None, None, None, None]  # e, d, c, x, s, w
+        if pos[1] > 0:
+            neighbors[2] = [pos[0], pos[1] - 1]
+        if pos[1] < self.size[1] - 1:
+            neighbors[3] = [pos[0], pos[1] + 1]
+        # x, c, s, d, w, e  # 3->0, 2->1, 5->4, 0->5
+        if pos[0] % 2 == 1:
+            if pos[0] > 0 and pos[1] > 0:
+                neighbors[4] = [pos[0] - 1, pos[1] - 1]
+            if pos[0] < self.size[0] - 1 and pos[1] > 0:
+                neighbors[0] = [pos[0] + 1, pos[1] - 1]
+            if pos[0] > 0:
+                neighbors[5] = [pos[0] - 1, pos[1]]
+            if pos[0] < self.size[0] - 1:
+                neighbors[1] = [pos[0] + 1, pos[1]]
+        else:
+            if pos[0] > 0 and pos[1] < self.size[1] - 1:
+                neighbors[5] = [pos[0] - 1, pos[1] + 1]
+            if pos[0] < self.size[0] - 1 and pos[1] < self.size[1] - 1:
+                neighbors[1] = [pos[0] + 1, pos[1] + 1]
+            if pos[0] > 0:
+                neighbors[4] = [pos[0] - 1, pos[1]]
+            if pos[0] < self.size[0] - 1:
+                neighbors[0] = [pos[0] + 1, pos[1]]
+        return neighbors
 
-class MapFovHex(MapHex):
+
+class FovMapHex(MapHex):
 
     def __init__(self, source_map, yx):
         self.source_map = source_map
@@ -231,8 +262,21 @@ class MapSquare(Map):
     def move_DOWN(self, start_pos):
         return [start_pos[0] + 1, start_pos[1]]
 
-
-class MapFovSquare(MapSquare):
+    def get_neighbors(self, pos):
+        # DOWN, LEFT, RIGHT, UP  (alphabetically)
+        neighbors = [None, None, None, None]
+        if pos[0] > 0:
+            neighbors[3] = [pos[0] - 1, pos[1]]
+        if pos[1] > 0:
+            neighbors[1] = [pos[0], pos[1] - 1]
+        if pos[0] < self.size[0] - 1:
+            neighbors[0] = [pos[0] + 1, pos[1]]
+        if pos[1] < self.size[1] - 1:
+            neighbors[2] = [pos[0], pos[1] + 1]
+        return neighbors
+
+
+class FovMapSquare(MapSquare):
     """Just a marginally and unsatisfyingly adapted variant of MapFovHex."""
 
     def __init__(self, source_map, yx):
@@ -332,4 +376,4 @@ class MapFovSquare(MapSquare):
             distance += 1
 
 
-map_manager = game_common.MapManager(globals())
+map_manager = game_common.MapManager((MapHex, MapSquare))