Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - speed up pathfinding

This is my pathfinding function:

def get_distance(x1,y1,x2,y2):
    neighbors = [(-1,0),(1,0),(0,-1),(0,1)]
    old_nodes = [(square_pos[x1,y1],0)]
    new_nodes = []
    for i in range(50):
        for node in old_nodes:
            if node[0].x == x2 and node[0].y == y2:
                return node[1]
            for neighbor in neighbors:
                try:
                    square = square_pos[node[0].x+neighbor[0],node[0].y+neighbor[1]]
                    if square.lightcycle == None:
                        new_nodes.append((square,node[1]))
                except KeyError:
                    pass
        old_nodes = []
        old_nodes = list(new_nodes)
        new_nodes = []
        nodes = []
    return 50

The problem is that the AI takes to long to respond( response time <= 100ms) This is just a python way of doing https://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm

like image 611
DrevanTonder Avatar asked Jan 20 '26 10:01

DrevanTonder


1 Answers

You should replace your algorithm with A*-search with the Manhattan distance as a heuristic.

like image 188
Unapiedra Avatar answered Jan 22 '26 01:01

Unapiedra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!