Below is my code for findclosest. Basically, what i meants to do is to find the closest route to a diamonds inside of my grid. However there are portal features where the player can went from one tiles to another without going 1 step by 1 step. I plan to check between the distance between portal and diamond and portal with player and compare it with just finding closest diamond to see which one is closer. However, i came to realize that whenever i went in a portal and get transported into other portal, it tend to stay there (It always return the same aim when it just got transported to the end of a portal).
def findclosest(current: GameObject, goal: 'list[GameObject]', teleport_pairs: 'list[tuple[Position, Position]]'):
closest_distance = 100
aim = None
for target in goal:
direct_distance = distance(current.position, target.position)
for pair in teleport_pairs:
# Check both directions of the teleporter
teleport_distance_1 = distance(current.position, pair[0]) + distance(pair[1], target.position)
teleport_distance_2 = distance(current.position, pair[1]) + distance(pair[0], target.position)
# Find the shorter distance using the teleporter
teleport_distance = min(teleport_distance_1, teleport_distance_2)
if teleport_distance < direct_distance:
direct_distance = teleport_distance
# Set aim to the closer teleporter entrance
aim = pair[0] if teleport_distance_1 < teleport_distance_2 else pair[1]
if direct_distance < closest_distance:
closest_distance = direct_distance
if not aim:
aim = target.position
return aim
I was wondering why it keep on staying as the code should run this function everytime the player makes a move and when it just got out of portal, there should be a diamond close by.