Start f new pathfinding, not working

This commit is contained in:
Nicolas Margulies
2020-12-08 00:59:19 +01:00
parent f648bcd7fb
commit 7823a422b9
2 changed files with 33 additions and 11 deletions

View File

@ -3,6 +3,7 @@
from random import randint
from typing import Dict, Tuple
from queue import PriorityQueue
from ..interfaces import FightingEntity
@ -95,26 +96,28 @@ class Player(FightingEntity):
Use Dijkstra algorithm to calculate best paths
for monsters to go to the player.
"""
queue = [(self.y, self.x)]
queue = PriorityQueue()
queue.put((0, (self.y, self.x)))
visited = []
distances = {(self.y, self.x): 0}
predecessors = {}
while queue:
y, x = queue.pop(0)
visited.append((y, x))
if distances[(y, x)] >= max_distance:
while not queue.empty:
dist, (y, x) = queue.get()
if dist >= max_distance or (y,x) in visited:
continue
visited.append((y, x))
for diff_y, diff_x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
new_y, new_x = y + diff_y, x + diff_x
if not 0 <= new_y < self.map.height or \
not 0 <= new_x < self.map.width or \
not self.map.tiles[y][x].can_walk() or \
(new_y, new_x) in visited or \
(new_y, new_x) in queue:
not self.map.tiles[y][x].can_walk():
continue
predecessors[(new_y, new_x)] = (y, x)
distances[(new_y, new_x)] = distances[(y, x)] + 1
queue.append((new_y, new_x))
new_distance = dist + 1
if not (new_y, new_x) in distances or \
distances[(new_y, new_x)] > new_distance:
predecessors[(new_y, new_x)] = (y, x)
distances[(new_y, new_x)] = new_distance
queue.put(new_distance, (new_y, new_x))
self.paths = predecessors
def save_state(self) -> dict: