From 7823a422b9a7f35dcefa85e1ec218b9eddd3a1fe Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Tue, 8 Dec 2020 00:59:19 +0100 Subject: [PATCH] Start f new pathfinding, not working --- squirrelbattle/display/mapdisplay.py | 19 +++++++++++++++++++ squirrelbattle/entities/player.py | 25 ++++++++++++++----------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/squirrelbattle/display/mapdisplay.py b/squirrelbattle/display/mapdisplay.py index 445f736..93be162 100644 --- a/squirrelbattle/display/mapdisplay.py +++ b/squirrelbattle/display/mapdisplay.py @@ -3,6 +3,7 @@ from squirrelbattle.interfaces import Map from .display import Display +from squirrelbattle.entities.player import Player class MapDisplay(Display): @@ -22,6 +23,24 @@ class MapDisplay(Display): for e in self.map.entities: self.addstr(self.pad, e.y, self.pack.tile_width * e.x, self.pack[e.name.upper()], self.color_pair(2)) + players = [ p for p in self.map.entities if isinstance(p,Player) ] + player = players[0] if len(players) > 0 else None + if player: + for x in range(self.map.width): + for y in range(self.map.height): + if (y,x) in player.paths: + deltay, deltax = (y - player.path[(y, x)][0], + x - player.path[(y, x)][1]) + if (deltay, deltax) == (-1, 0): + character = '╹' + elif (deltay, deltax) == (1, 0): + character = '╻' + elif (deltay, deltax) == (0, -1): + character = '╺' + else: + character = '╸' + self.addstr(self.pad, y, self.pack.tile_width * x, + character, self.color_pair(1)) def display(self) -> None: y, x = self.map.currenty, self.pack.tile_width * self.map.currentx diff --git a/squirrelbattle/entities/player.py b/squirrelbattle/entities/player.py index 3f1d941..0875d3a 100644 --- a/squirrelbattle/entities/player.py +++ b/squirrelbattle/entities/player.py @@ -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: