Start f new pathfinding, not working
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user