+
+
+class DijkstraMap(SourcedMap):
+
+ 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
+ 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
+ if self.source_map_segment[position_i] in self.block_chars:
+ return shrunk_test
+ neighbors = self.geometry.get_neighbors_i(position_i)
+ for direction in [d for d in neighbors if neighbors[d]]:
+ j = neighbors[direction]
+ if self.terrain[j] < self.terrain[position_i] - 1:
+ self.terrain[position_i] = self.terrain[j] + 1
+ shrunk_test = True
+ return shrunk_test
+
+ # TODO: refactor with FovMap.circle_out()
+ shrunk = True
+ while shrunk and len(targets) > 0:
+ shrunk = False
+ yx = self.center
+ distance = 1
+ 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)
+ 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 = []
+ # x = 0
+ # for n in self.terrain:
+ # line_to_print += ['%3s' % n]
+ # x += 1
+ # if x >= self.geometry.size.x:
+ # x = 0
+ # print(' '.join(line_to_print))
+ # line_to_print = []
+
+
+
+class FovMap(SourcedMap):
+ # TODO: player visibility asymmetrical (A can see B when B can't see A):
+ # does this make sense, or not?
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)