summaryrefslogtreecommitdiff
path: root/pathfinding.py
diff options
context:
space:
mode:
authorFlorian Jung <flo@windfisch.org>2015-08-19 00:15:10 +0200
committerFlorian Jung <flo@windfisch.org>2015-08-19 00:15:10 +0200
commitb88f390df3bc48059149c8d8ac8172527ccd54b6 (patch)
treebdfa1d01fc46fc7e4a73a64151e7a570c821f2ed /pathfinding.py
parent9cc1886cca05124d84b5c02c44b5ffaf5ee4c6c0 (diff)
automatically recalculate path if it becomes blocked (slow!)
Diffstat (limited to 'pathfinding.py')
-rw-r--r--pathfinding.py17
1 files changed, 16 insertions, 1 deletions
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]