Legacy GM Finding which path is shortest

T

tbourget1

Guest
So I am creating code for a game where there are 4 players, and the enemy needs to path its way to one of them. The code is working with one player, but once other players are added all of the enemies will still only follow player 1. I created an array path[] to store the 1-4 path(s). So I have all 4 paths in an array, and I also have all 4 path distances in an array called path_distance[] (calculated using path_get_length). How do I get the enemy started on the shortest path? Is there a basic coding function that I could be using?

I also have the shortest path distance stored in a variable shortest_distance (which was calculated using min( path_distance[1]...path_distance[4] ) ), but I'm not sure how to derive the path from the distance.
 

Tthecreator

Your Creator!
Yes, you could create a for loop, which loops trough all possibilities inside path_distance[]. Then it checks what's the smallest and uses the path from path[]
You will need:
-one for loop;
-one counter variable for inside your for loop;
-one variable holding the smallest path distance;
-one variable holding the id(or array index) of the smallest path distance.

This should get you started.
 
T

tbourget1

Guest
Yes, you could create a for loop, which loops trough all possibilities inside path_distance[]. Then it checks what's the smallest and uses the path from path[]
You will need:
-one for loop;
-one counter variable for inside your for loop;
-one variable holding the smallest path distance;
-one variable holding the id(or array index) of the smallest path distance.

This should get you started.
But how do I get which path[] is the smallest distance? I can figure out which path_distance[] is the smallest, but how do I derive which path[] this corresponds to??
 

Tthecreator

Your Creator!
That's what point 4 here is for. You DON'T want to use the function min() in this case, since it won't help you with this.

ps: why does your array start at 1?, it should start at 0 and I've taken a range from 0 to 3 instead of a range from 1 to 4.
What you do is:
var pathindex,minpathdistance;
//setting it to the values of the first path in the array.
var minpathdistance=path_distance[0]
pathindex=0
//this scrolls trough the numbers 1, 2 and 3. Not trough 0 because we've already handled that above here.
for(var i=1;i<3;i++){
if path_distance<minpathdistance then{
minpathdistance=path_distance; //changes the minpathdistance for the next comparison.
pathindex=i//sets the path index to the smallest seen number so far.
}//end of if
}//end of for
pathtouse=pathindex

I'm not a big fan of copy and past work, so please make sure you understand this code well, else you will not have learned anything and that would be a shame.
Just say it if you don't understand this code.
 
T

tbourget1

Guest
That's what point 4 here is for. You DON'T want to use the function min() in this case, since it won't help you with this.

ps: why does your array start at 1?, it should start at 0 and I've taken a range from 0 to 3 instead of a range from 1 to 4.
What you do is:
var pathindex,minpathdistance;
//setting it to the values of the first path in the array.
var minpathdistance=path_distance[0]
pathindex=0
//this scrolls trough the numbers 1, 2 and 3. Not trough 0 because we've already handled that above here.
for(var i=1;i<3;i++){
if path_distance<minpathdistance then{
minpathdistance=path_distance; //changes the minpathdistance for the next comparison.
pathindex=i//sets the path index to the smallest seen number so far.
}//end of if
}//end of for
pathtouse=pathindex

I'm not a big fan of copy and past work, so please make sure you understand this code well, else you will not have learned anything and that would be a shame.
Just say it if you don't understand this code.
This code makes sense to me, thank you! This is very helpful
 
Top