home · contact · privacy
Only calculate DijkstraMap until reachable targets.
[plomrogue2] / plomrogue / mapping.py
index 29078d87c0b9d71ef9ee2d8616e862888b73f705..707111263d884b21508ba536fc02b95e2af66677 100644 (file)
@@ -256,15 +256,21 @@ class SourcedMap(Map):
 
 class DijkstraMap(SourcedMap):
 
-    def __init__(self, *args, **kwargs):
+    def __init__(self, potential_targets, *args, **kwargs):
         # TODO: check potential optimizations:
         # - somehow ignore tiles that have the lowest possible value (we can
         #   compare with a precalculated map for given starting position)
         # - check if Python offers more efficient data structures to use here
-        # - shorten radius to nearest possible target
         super().__init__(*args, **kwargs)
         self.terrain = [255] * self.size_i
         self[self.center] = 0
+        targets = []
+        for target_yxyx in potential_targets:
+            target = self.target_yx(*target_yxyx)
+            if target == self.center:
+                continue
+            if self.inside(target):
+                targets += [target]
 
         def work_tile(position_i):
             shrunk_test = False
@@ -280,18 +286,21 @@ class DijkstraMap(SourcedMap):
 
         # TODO: refactor with FovMap.circle_out()
         shrunk = True
-        while shrunk:
+        while shrunk and len(targets) > 0:
             shrunk = False
             yx = self.center
             distance = 1
-            while distance <= self.radius:
+            while distance <= self.radius and len(targets) > 0:
                 yx = self.geometry.basic_circle_out_move(yx, 'RIGHT')
                 for dir_i in range(len(self.geometry.circle_out_directions)):
                     for dir_progress in range(distance):
                         direction = self.geometry.circle_out_directions[dir_i]
                         yx = self.geometry.circle_out_move(yx, direction)
                         position_i = self.get_position_index(yx)
-                        shrunk = True if work_tile(position_i) else shrunk
+                        cur_shrunk = work_tile(position_i)
+                        if cur_shrunk and yx in targets:
+                            targets.remove(yx)
+                        shrunk = shrunk or cur_shrunk
                 distance += 1
         # print('DEBUG Dijkstra')
         # line_to_print = []