Commits

Arne Babenhauserheide  committed bdbd1e2

Avoid an infinite loop when trying to find a path to an unreachable hex.

  • Participants
  • Parent commits 9486f20

Comments (0)

Files changed (2)

File hexbattle_logic.py

         
         # settings
         self.player_team = "trees"
-        self.num_rats = 20
-        self.num_goblins = 0
+        self.num_rats = 10
+        self.num_goblins = 7
         
     def are_enemies(self, unit, other):
         """Check if the given units are enemies."""

File hexbattle_units.py

         FIXME: Some fields are left out for some reason. 
         """
         targets = set()
+        def add(position):
+            if not position in self.hexmap:
+                targets.add(position)
         # basic optimization: only check a hex-„square“ around the blob.
         for x in range(-self.max_move, self.max_move+1):
             for y in range(-self.max_move, self.max_move+1):
                         if not path: # target not reachable in any way.
                             continue
                         for pos in path[:self.max_move]:
-                            targets.add(pos)
+                            add(pos)
                         # if we can reach the target, add it
                         pos_is_reachable = len(path) < self.max_move
                         if pos_is_reachable:
-                            targets.add(pos)
+                            add(pos)
         
         return list(targets)
 
             return pos1[0], pos1[1], pos2[0], pos2[1]
     def free_neighbor_nodes(self, pos):
             return [(h[0] + pos[0], h[1] + pos[1]) for h in hex_vectors[1:] if self.hexmap.get((h[0] + pos[0], h[1] + pos[1]), None) is None]
+    def friendly_neighbor_nodes(self, pos):
+        """All nodes over which we can move."""
+        targets = []
+        for h in hex_vectors[1:]:
+            x, y = h[0] + pos[0], h[1] + pos[1]
+            unit = self.hexmap.get((x, y), None)
+            if unit is None or not self.model.are_enemies(self, unit):
+                targets.append((x, y))
+        return targets
+
     def dist_heuristic(self, pos1, pos2):
             """The estimated distance between two nodes."""
             return self.distance(*self.twopos2fourhex(pos1, pos2))
                 return reconstruct(previous, previous[goal])
             openset.remove(current)
             closed.add(current)
-            for neighbor in self.free_neighbor_nodes(current):
+            for neighbor in self.friendly_neighbor_nodes(current):
                 if neighbor in closed: continue
+                # Avoid an infinite loop when trying to find a path to an unreachable hex.
+                if self.dist_heuristic(start, neighbor) > self.max_move: 
+                    continue # don’t ever check further than we can go
                 g_tentative = g[current] + dist(current, neighbor)
                 if not neighbor in openset:
                     openset.add(neighbor)