Find fastest direction to get to a number?

Currently for my AI, I need to find the fastest way to get to a point, forward or backwards.

Example:

How would I achieve this? (Points are just numbers)

2 Likes

Did you look into the PathFinding Modifiers section of the Pathfinding page?
I’d think if the points you are using are fixed you could pre-calculate their distances then use a formula, something like:
find the length of path1 = 1to2 + 2to3
find the length of path2 = 5to1 + 4to5 + 3to4

if path1<path2 then 
    -- make AI use path1
else
    -- make AI usepath2
end
2 Likes

Yes, I’ve thought of using pathfinding but I’d like to use the way that I’m currently doing it because it allows more control over what paths I’d like AI to follow.

This wouldnt exactly work. My path (as of now) goes 1 through 7, and when it reaches 7 it loops back to 1. The AI follows the path and has different things to take care of (food, sleep, stay away from players.) I already have the AI scripted but I’m just having a problem finding which way the AI should go to reach a certain point. Example:

Adjective is point 1, AI is currently at point 5. Instead of going (5,4,3,2,1), just go (5,6,7,1) because it loops around. How would I decide to do this with math?

I’m still in need of this… If needed I can provide my script

Then do the same thing. You’ve got the physical distances of all 7 ‘paths’.
Do the same as I said for the 5 point example.
If the AI is at point 5 and needs to get to the goal at 1 then check to see which is larger:
Add the lengths 5to6, 6to7, (if the value of the next step >7 then make the value 1 again) and 7to1.
Add the length of (4to5 + 3to4 + 2to3 + 1to2).
If the length of the first case is less than the length of the second case then travel the first route, else travel the second route.

Here is an algorithmic solution:

local s = 5 -- start number
local e = 1 -- end number

local t = 7 -- total numbers

local i, j, k, p, q, r
if s<e then
	if e-s+1 <= t-e+1+s then
		i,j,k = s,e,1
		p,q,r = t,1,1
	else
		i,j,k = e,t,1
		p,q,r = 1,s,1
	end
else
	if s-e+1 <= t-s+1+e then
		i,j,k = s,e,-1
		p,q,r = 1,t,-1
	else
		i,j,k = s,t,1
		p,q,r = 1,e,1
	end
end

for kk = i,j,k do
	print(kk)
end
for kk = p,q,r do
	print(kk)
end