From 0f924e175d0f321e703e3f00511b547c4a027dbc Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 16 Dec 2020 19:28:45 +0100 Subject: [PATCH] Only calculate DijkstraMap until reachable targets. --- plomrogue/mapping.py | 19 ++++++++++++++----- plomrogue/things.py | 7 ++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/plomrogue/mapping.py b/plomrogue/mapping.py index 29078d8..7071112 100644 --- a/plomrogue/mapping.py +++ b/plomrogue/mapping.py @@ -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 = [] diff --git a/plomrogue/things.py b/plomrogue/things.py index 950a777..dd14db5 100644 --- a/plomrogue/things.py +++ b/plomrogue/things.py @@ -70,10 +70,11 @@ class Thing(ThingBase): largest_audible_distance = 20 obstacles = [t.position for t in self.game.things if t.blocks_sound] + targets = [t.position for t in self.game.things if t.type_ == 'Player'] sound_blockers = self.game.get_sound_blockers() - dijkstra_map = DijkstraMap(sound_blockers, obstacles, self.game.maps, - self.position, largest_audible_distance, - self.game.get_map) + dijkstra_map = DijkstraMap(targets, sound_blockers, obstacles, + self.game.maps, self.position, + largest_audible_distance, self.game.get_map) url_limits = [] for m in re.finditer('https?://[^\s]+', msg): url_limits += [m.start(), m.end()] -- 2.30.2