From b88f390df3bc48059149c8d8ac8172527ccd54b6 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Wed, 19 Aug 2015 00:15:10 +0200 Subject: automatically recalculate path if it becomes blocked (slow!) --- pathfinding.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pathfinding.py b/pathfinding.py index b250523..ce3ab8e 100644 --- a/pathfinding.py +++ b/pathfinding.py @@ -87,7 +87,7 @@ class PathfindingTesterStrategy: grid = [] - interesting_cells = list(filter(lambda c : not c.is_food, self.c.player.world.cells.values())) + interesting_cells = list(filter(lambda c : not (c.is_food or c in self.c.player.own_cells), self.c.player.world.cells.values())) for x in range(-grid_radius,grid_radius+1,grid_density): gridline = [] @@ -106,6 +106,17 @@ class PathfindingTesterStrategy: path = aStar(grid[int(grid_radius/grid_density)][int(grid_radius/grid_density)], grid[goalx][goaly], grid) return path + def path_is_valid(self, path): + interesting_cells = list(filter(lambda c : not (c.is_food or c in self.c.player.own_cells), self.c.player.world.cells.values())) + for node in path: + for cell in interesting_cells: + relpos = (cell.pos.x - node.point[0], cell.pos.y - node.point[1]) + dist_sq = relpos[0]**2 + relpos[1]**2 + if dist_sq < cell.size**2 *2: + return False + + return True + def process_frame(self): if marker_updated[0]: marker_updated[0]=False @@ -124,6 +135,10 @@ class PathfindingTesterStrategy: if relx*relx + rely*rely < (2*grid_density)**2: self.path=self.path[1:] + if self.path and not self.path_is_valid(self.path): + print("recalculating!") + self.path = self.plan_path() + if self.path: return self.path[0].point return marker[0] -- cgit v1.2.3