home · contact · privacy
Refactor.
[plomrogue2-experiments] / server_ / map_.py
index 836e540b75b89a4b99f3aa0b668d5f00e2a4951e..35d29c6b56da95fd2f25c4e35f4e85a3ff419da8 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,62 @@ 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 MapSquare(Map):
+
+    # The following is used nowhere, so not implemented.
+    #def are_neighbors(self, pos_1, pos_2):
+    #    return abs(pos_1[0] - pos_2[0]) <= 1 and abs(pos_1[1] - pos_2[1] <= 1)
 
-class MapFovHex(MapHex):
+    def move_UP(self, start_pos):
+        return [start_pos[0] - 1, start_pos[1]]
+
+    def move_DOWN(self, start_pos):
+        return [start_pos[0] + 1, start_pos[1]]
+
+    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 FovMap:
 
     def __init__(self, source_map, yx):
         self.source_map = source_map
@@ -172,7 +229,7 @@ class MapFovHex(MapHex):
                     self.shadow_cones += [cone]
 
         #print('DEBUG', yx)
-        step_size = (CIRCLE/6) / distance_to_center
+        step_size = (CIRCLE/len(self.circle_out_directions)) / distance_to_center
         number_steps = dir_i * distance_to_center + dir_progress
         left_arm = correct_arm(-(step_size/2) - step_size*number_steps)
         right_arm = correct_arm(left_arm - step_size)
@@ -184,6 +241,15 @@ class MapFovHex(MapHex):
         else:
             eval_cone([left_arm, right_arm])
 
+    def basic_circle_out_move(self, pos, direction):
+        """Move position pos into direction. Return whether still in map."""
+        mover = getattr(self, 'move_' + direction)
+        pos[:] = mover(pos)
+        if pos[0] < 0 or pos[1] < 0 or \
+            pos[0] >= self.size[0] or pos[1] >= self.size[1]:
+            return False
+        return True
+
     def circle_out(self, yx, f):
         # Optimization potential: Precalculate movement positions. (How to check
         # circle_in_map then?)
@@ -192,142 +258,38 @@ class MapFovHex(MapHex):
         # shading implies they completely lie in existing shades; otherwise we
         # would lose shade growth through hexes at shade borders.)
 
-        def move(pos, direction):
-            """Move position pos into direction. Return whether still in map."""
-            mover = getattr(self, 'move_' + direction)
-            pos[:] = mover(pos)
-            if pos[0] < 0 or pos[1] < 0 or \
-               pos[0] >= self.size[0] or pos[1] >= self.size[1]:
-                return False
-            return True
-
         # TODO: Start circling only in earliest obstacle distance.
-        directions = ('DOWNLEFT', 'LEFT', 'UPLEFT', 'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
         circle_in_map = True
         distance = 1
         yx = yx[:]
         #print('DEBUG CIRCLE_OUT', yx)
         while circle_in_map:
             circle_in_map = False
-            move(yx, 'RIGHT')
-            for dir_i in range(len(directions)):
+            self.basic_circle_out_move(yx, 'RIGHT')
+            for dir_i in range(len(self.circle_out_directions)):
                 for dir_progress in range(distance):
-                    direction = directions[dir_i]
-                    if move(yx, direction):
+                    direction = self.circle_out_directions[dir_i]
+                    if self.circle_out_move(yx, direction):
                         f(yx, distance, dir_i, dir_progress)
                         circle_in_map = True
             distance += 1
 
 
-class MapSquare(Map):
+class FovMapHex(FovMap, MapHex):
+    circle_out_directions = ('DOWNLEFT', 'LEFT', 'UPLEFT',
+                             'UPRIGHT', 'RIGHT', 'DOWNRIGHT')
 
-    # The following is used nowhere, so not implemented.
-    #def are_neighbors(self, pos_1, pos_2):
-    #    return abs(pos_1[0] - pos_2[0]) <= 1 and abs(pos_1[1] - pos_2[1] <= 1)
+    def circle_out_move(self, yx, direction):
+        return self.basic_circle_out_move(yx, direction)
 
-    def move_UP(self, start_pos):
-        return [start_pos[0] - 1, start_pos[1]]
-
-    def move_DOWN(self, start_pos):
-        return [start_pos[0] + 1, start_pos[1]]
-
-
-class MapFovSquare(MapSquare):
-    """Just a marginally and unsatisfyingly adapted variant of MapFovHex."""
-
-    def __init__(self, source_map, yx):
-        self.source_map = source_map
-        self.size = self.source_map.size
-        self.terrain = '?' * self.size_i
-        self[yx] = '.'
-        self.shadow_cones = []
-        self.circle_out(yx, self.shadow_process_hex)
 
-    def shadow_process_hex(self, yx, distance_to_center, dir_i, dir_progress):
-        CIRCLE = 360  # Since we'll float anyways, number is actually arbitrary.
+class FovMapSquare(FovMap, MapSquare):
+    circle_out_directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
+                             ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
 
-        def correct_arm(arm):
-            if arm < 0:
-                arm += CIRCLE
-            return arm
-
-        def in_shadow_cone(new_cone):
-            for old_cone in self.shadow_cones:
-                if old_cone[0] >= new_cone[0] and \
-                    new_cone[1] >= old_cone[1]:
-                    #print('DEBUG shadowed by:', old_cone)
-                    return True
-            return False
-
-        def merge_cone(new_cone):
-            for old_cone in self.shadow_cones:
-                if new_cone[0] > old_cone[0] and \
-                    new_cone[1] <= old_cone[0]:
-                    #print('DEBUG merging to', old_cone)
-                    old_cone[0] = new_cone[0]
-                    #print('DEBUG merged cone:', old_cone)
-                    return True
-                if new_cone[1] < old_cone[1] and \
-                    new_cone[0] >= old_cone[1]:
-                    #print('DEBUG merging to', old_cone)
-                    old_cone[1] = new_cone[1]
-                    #print('DEBUG merged cone:', old_cone)
-                    return True
-            return False
-
-        def eval_cone(cone):
-            new_cone = [left_arm, right_arm]
-            #print('DEBUG CONE', cone, '(', step_size, distance_to_center, number_steps, ')')
-            if in_shadow_cone(cone):
-                return
-            self[yx] = '.'
-            if self.source_map[yx] != '.':
-                #print('DEBUG throws shadow', cone)
-                unmerged = True
-                while merge_cone(cone):
-                    unmerged = False
-                if unmerged:
-                    self.shadow_cones += [cone]
-
-        #print('DEBUG', yx)
-        step_size = fractions.Fraction(CIRCLE, 4) / distance_to_center
-        number_steps = dir_i * distance_to_center + dir_progress
-        left_arm = correct_arm(-(step_size/2) - step_size*number_steps)
-        right_arm = correct_arm(left_arm - step_size)
-        if right_arm > left_arm:
-            eval_cone([left_arm, 0])
-            eval_cone([CIRCLE, right_arm])
-        else:
-            eval_cone([left_arm, right_arm])
-
-    def circle_out(self, yx, f):
-
-        def move(pos, direction):
-            """Move position pos into direction. Return whether still in map."""
-            mover = getattr(self, 'move_' + direction)
-            pos[:] = mover(pos)
-            if pos[0] < 0 or pos[1] < 0 or \
-               pos[0] >= self.size[0] or pos[1] >= self.size[1]:
-                return False
-            return True
-
-        directions = (('DOWN', 'LEFT'), ('LEFT', 'UP'),
-                      ('UP', 'RIGHT'), ('RIGHT', 'DOWN'))
-        circle_in_map = True
-        distance = 1
-        yx = yx[:]
-        #print('DEBUG CIRCLE_OUT', yx)
-        while circle_in_map:
-            circle_in_map = False
-            move(yx, 'RIGHT')
-            for dir_i in range(len(directions)):
-                for dir_progress in range(distance):
-                    direction = directions[dir_i]
-                    move(yx, direction[0])
-                    if move(yx, direction[1]):
-                        f(yx, distance, dir_i, dir_progress)
-                        circle_in_map = True
-            distance += 1
+    def circle_out_move(self, yx, direction):
+        self.basic_circle_out_move(yx, direction[0])
+        return self.basic_circle_out_move(yx, direction[1])
 
 
-map_manager = game_common.MapManager(globals())
+map_manager = game_common.MapManager((MapHex, MapSquare))