Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pathfinding/core/grid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from typing import List
from .diagonal_movement import DiagonalMovement
from .node import Node
try:
Expand All @@ -8,7 +9,7 @@
USE_NUMPY = False


def build_nodes(width, height, matrix=None, inverse=False, grid_id=None):
def build_nodes(width, height, matrix=None, inverse=False, grid_id=None) -> List[List[Node]]:
"""
create nodes according to grid size. If a matrix is given it
will be used to determine what nodes are walkable.
Expand Down Expand Up @@ -62,7 +63,7 @@ def set_passable_left_right_border(self):
def set_passable_up_down_border(self):
self.passable_up_down_border = True

def node(self, x, y):
def node(self, x, y) -> Node:
"""
get node at position
:param x: x pos
Expand All @@ -71,7 +72,7 @@ def node(self, x, y):
"""
return self.nodes[y][x]

def inside(self, x, y):
def inside(self, x, y) -> bool:
"""
check, if field position is inside map
:param x: x pos
Expand All @@ -80,13 +81,13 @@ def inside(self, x, y):
"""
return 0 <= x < self.width and 0 <= y < self.height

def walkable(self, x, y):
def walkable(self, x, y) -> bool:
"""
check, if the tile is inside grid and if it is set as walkable
"""
return self.inside(x, y) and self.nodes[y][x].walkable

def neighbors(self, node, diagonal_movement=DiagonalMovement.never):
def neighbors(self, node: Node, diagonal_movement: DiagonalMovement = DiagonalMovement.never) -> List[Node]:
"""
get all neighbors of one node
:param node: node
Expand Down Expand Up @@ -179,7 +180,7 @@ def cleanup(self):
def grid_str(self, path=None, start=None, end=None,
border=True, start_chr='s', end_chr='e',
path_chr='x', empty_chr=' ', block_chr='#',
show_weight=False):
show_weight=False) -> str:
"""
create a printable string from the grid using ASCII characters

Expand Down
10 changes: 5 additions & 5 deletions pathfinding/core/heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .util import SQRT2


def null(dx, dy):
def null(dx, dy) -> float:
"""
special heuristic for Dijkstra
return 0, so node.h will always be calculated as 0,
Expand All @@ -13,22 +13,22 @@ def null(dx, dy):
return 0


def manhattan(dx, dy):
def manhattan(dx, dy) -> float:
"""manhattan heuristics"""
return dx + dy


def euclidean(dx, dy):
def euclidean(dx, dy) -> float:
"""euclidean distance heuristics"""
return math.sqrt(dx * dx + dy * dy)


def chebyshev(dx, dy):
def chebyshev(dx, dy) -> float:
""" Chebyshev distance. """
return max(dx, dy)


def octile(dx, dy):
def octile(dx, dy) -> float:
f = SQRT2 - 1
if dx < dy:
return f * dx + dy
Expand Down
16 changes: 10 additions & 6 deletions pathfinding/core/util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# -*- coding: utf-8 -*-
import copy
import math
from typing import List, Tuple

from .node import Node
from .grid import Grid

# square root of 2 for diagonal distance
SQRT2 = math.sqrt(2)

Coords = Tuple[float, float]

def backtrace(node):
def backtrace(node: Node) -> List[Node]:
"""
Backtrace according to the parent records and return the path.
(including both start and end nodes)
Expand All @@ -20,7 +24,7 @@ def backtrace(node):
return path


def bi_backtrace(node_a, node_b):
def bi_backtrace(node_a: Node, node_b: Node) -> List[Node]:
"""
Backtrace from start and end node, returns the path for bi-directional A*
(including both start and end nodes)
Expand All @@ -31,7 +35,7 @@ def bi_backtrace(node_a, node_b):
return path_a + path_b


def raytrace(coords_a, coords_b):
def raytrace(coords_a: Coords, coords_b: Coords) -> List[Coords]:
line = []
x0, y0 = coords_a
x1, y1 = coords_b
Expand Down Expand Up @@ -64,7 +68,7 @@ def raytrace(coords_a, coords_b):
return line


def bresenham(coords_a, coords_b):
def bresenham(coords_a: Coords, coords_b: Coords) -> List[Coords]:
'''
Given the start and end coordinates, return all the coordinates lying
on the line formed by these coordinates, based on Bresenham's algorithm.
Expand Down Expand Up @@ -94,7 +98,7 @@ def bresenham(coords_a, coords_b):
return line


def expand_path(path):
def expand_path(path: List[Coords]) -> List[Coords]:
'''
Given a compressed path, return a new path that has all the segments
in it interpolated.
Expand All @@ -108,7 +112,7 @@ def expand_path(path):
return expanded


def smoothen_path(grid, path, use_raytrace=False):
def smoothen_path(grid: Grid, path: List[Coords], use_raytrace=False) -> List[Coords]:
x0, y0 = path[0]

sx, sy = path[0]
Expand Down
10 changes: 8 additions & 2 deletions pathfinding/core/world.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from typing import Dict

from .grid import Grid
from .node import Node
from .diagonal_movement import DiagonalMovement

# a world connects grids but can have multiple grids.
class World:
def __init__(self, grids: dict):
def __init__(self, grids: Dict[int, Grid]):
self.grids = grids

def neighbors(self, node, diagonal_movement):
def neighbors(self, node: Node, diagonal_movement: DiagonalMovement):
return self.grids[node.grid_id].neighbors(
node, diagonal_movement=diagonal_movement)