Code: Select all
Function p_PathFinding(location, distance)
Local t_location = locationcards[location]
If t_location.destroyed = True Then Return
Local found = False
ForEach(t_havedistancealready, Function(index, t_place)
If t_place.name = location And t_place.distance =< distance Then found=True
EndFunction)
If found = True Then Return
Local t_temp = {name=location, distance=distance}
InsertItem(t_havedistancealready, t_temp)
If distance = currentshortestdistance And t_location.type = "mountain"
InsertItem(t_closestmountains, location)
ElseIf distance < currentshortestdistance And t_location.type = "mountain"
currentshortestdistance = distance
t_closestmountains = {}
InsertItem(t_closestmountains, location)
EndIf
Local amount = ListItems(t_location.neighbors)
For n=0 To amount-1
p_PathFinding(t_location.neighbors[n], distance+1)
Next
EndFunction
And when i make the call, i call it with p_PathFinding(location, 1)
problem with this code is, that it doesnt go through every location, and after having spent last 3 hours trying to hunt that problem down, I decided it was better to ask if one of you can see the problem with my code.
Each location has neighbors, and hence every location is connected to every location through some path, and therefore every time this pathfinding is executed, it should give distance to each location. But at worst it might check only 4 locations in total and i cant figure out the logic of why, even i have the names of those locations making it possible for me to follow the execution using debugprints.
oh, and just to make it more clear. The real purpose of this function is to find closest mountain locations, but it is just in practice done in such way that it shold go through every location.
Can anyone spot my error?