Start f new pathfinding, not working
This commit is contained in:
		@@ -3,6 +3,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from squirrelbattle.interfaces import Map
 | 
					from squirrelbattle.interfaces import Map
 | 
				
			||||||
from .display import Display
 | 
					from .display import Display
 | 
				
			||||||
 | 
					from squirrelbattle.entities.player import Player
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MapDisplay(Display):
 | 
					class MapDisplay(Display):
 | 
				
			||||||
@@ -22,6 +23,24 @@ class MapDisplay(Display):
 | 
				
			|||||||
        for e in self.map.entities:
 | 
					        for e in self.map.entities:
 | 
				
			||||||
            self.addstr(self.pad, e.y, self.pack.tile_width * e.x,
 | 
					            self.addstr(self.pad, e.y, self.pack.tile_width * e.x,
 | 
				
			||||||
                        self.pack[e.name.upper()], self.color_pair(2))
 | 
					                        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:
 | 
					    def display(self) -> None:
 | 
				
			||||||
        y, x = self.map.currenty, self.pack.tile_width * self.map.currentx
 | 
					        y, x = self.map.currenty, self.pack.tile_width * self.map.currentx
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from random import randint
 | 
					from random import randint
 | 
				
			||||||
from typing import Dict, Tuple
 | 
					from typing import Dict, Tuple
 | 
				
			||||||
 | 
					from queue import PriorityQueue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from ..interfaces import FightingEntity
 | 
					from ..interfaces import FightingEntity
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -95,26 +96,28 @@ class Player(FightingEntity):
 | 
				
			|||||||
        Use Dijkstra algorithm to calculate best paths
 | 
					        Use Dijkstra algorithm to calculate best paths
 | 
				
			||||||
        for monsters to go to the player.
 | 
					        for monsters to go to the player.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        queue = [(self.y, self.x)]
 | 
					        queue = PriorityQueue()
 | 
				
			||||||
 | 
					        queue.put((0, (self.y, self.x)))
 | 
				
			||||||
        visited = []
 | 
					        visited = []
 | 
				
			||||||
        distances = {(self.y, self.x): 0}
 | 
					        distances = {(self.y, self.x): 0}
 | 
				
			||||||
        predecessors = {}
 | 
					        predecessors = {}
 | 
				
			||||||
        while queue:
 | 
					        while not queue.empty:
 | 
				
			||||||
            y, x = queue.pop(0)
 | 
					            dist, (y, x) = queue.get()
 | 
				
			||||||
            visited.append((y, x))
 | 
					            if dist >= max_distance or (y,x) in visited:
 | 
				
			||||||
            if distances[(y, x)] >= max_distance:
 | 
					 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
 | 
					            visited.append((y, x))
 | 
				
			||||||
            for diff_y, diff_x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
 | 
					            for diff_y, diff_x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
 | 
				
			||||||
                new_y, new_x = y + diff_y, x + diff_x
 | 
					                new_y, new_x = y + diff_y, x + diff_x
 | 
				
			||||||
                if not 0 <= new_y < self.map.height or \
 | 
					                if not 0 <= new_y < self.map.height or \
 | 
				
			||||||
                        not 0 <= new_x < self.map.width or \
 | 
					                        not 0 <= new_x < self.map.width or \
 | 
				
			||||||
                        not self.map.tiles[y][x].can_walk() or \
 | 
					                        not self.map.tiles[y][x].can_walk():
 | 
				
			||||||
                        (new_y, new_x) in visited or \
 | 
					 | 
				
			||||||
                        (new_y, new_x) in queue:
 | 
					 | 
				
			||||||
                    continue
 | 
					                    continue
 | 
				
			||||||
                predecessors[(new_y, new_x)] = (y, x)
 | 
					                new_distance = dist + 1
 | 
				
			||||||
                distances[(new_y, new_x)] = distances[(y, x)] + 1
 | 
					                if not (new_y, new_x) in distances or \
 | 
				
			||||||
                queue.append((new_y, new_x))
 | 
					                        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
 | 
					        self.paths = predecessors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def save_state(self) -> dict:
 | 
					    def save_state(self) -> dict:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user