Commits

Anonymous committed 9486f20

refactoring to try to track down the error which makes it impossible to hit all reachable hexes.

  • Participants
  • Parent commits 2182f3f

Comments (0)

Files changed (2)

hexbattle_logic.py

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

hexbattle_units.py

 
         TODO: Move method into model, as it deals with relationship between units.
         
-        FIXME: Some 
+        FIXME: Some fields are left out for some reason. 
         """
         targets = set()
         # basic optimization: only check a hex-„square“ around the blob.
                 if pos in targets:
                     continue
                 z = - (x+y)
-                dist = 0.5*(abs(x)+abs(y)+abs(z))
+                dist = self.distance(self.hex_x, self.hex_y, self.hex_x + x, self.hex_y + y)
                 if dist <= self.max_move:
                     # don’t list those places which are already taken.
                     hexunit = self.hexmap.get(pos, None)
                         targets.add(pos)
                     if hexunit is None: 
                         path = self.path_to(pos)
-                        if not path: # no path to target
+                        if not path: # target not reachable in any way.
                             continue
                         for pos in path[:self.max_move]:
                             targets.add(pos)
         
         return list(targets)
 
+    def twopos2fourhex(self, pos1, pos2):
+            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 dist_heuristic(self, pos1, pos2):
+            """The estimated distance between two nodes."""
+            return self.distance(*self.twopos2fourhex(pos1, pos2))
+
     def path_to(self, goal):
         """Find the cheapest path towards the goal hex.
 
         
         :return: the list of hexes to traverse or None (if no path could be found).
         """
-        def twopos2fourhex(pos1, pos2):
-            return pos1[0], pos1[1], pos2[0], pos2[1]
-        def free_neighbor_nodes(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 dist_heuristic(pos1, pos2):
-            """The estimated distance between two nodes."""
-            return hexgrid_distance(*twopos2fourhex(pos1, pos2))
         #: TODO: replace by weighted distances which allow different terrains to have different real costs between neighboring nodes.
-        dist = dist_heuristic
+        dist = self.dist_heuristic
         def reconstruct(previous, current):
             if current in previous:
                 p = reconstruct(previous, previous[current])
                 return reconstruct(previous, previous[goal])
             openset.remove(current)
             closed.add(current)
-            for neighbor in free_neighbor_nodes(current):
+            for neighbor in self.free_neighbor_nodes(current):
                 if neighbor in closed: continue
                 g_tentative = g[current] + dist(current, neighbor)
                 if not neighbor in openset:
                     openset.add(neighbor)
-                    h[neighbor] = dist_heuristic(neighbor, goal)
+                    h[neighbor] = self.dist_heuristic(neighbor, goal)
                     tentative_is_better = True
                 elif g_tentative < g[neighbor]:
                     tentative_is_better = True
             # if we have no enemies, just don’t move
             if not enemies:
                 return self.hex_x, self.hex_y
+            # otherwise find the possible target which is closest to an enemy
             nearest = possible_targets[0]
             dist_nearest = self.distance(nearest[0], nearest[1], enemies[0].hex_x, enemies[0].hex_y)
             for field in possible_targets: