ScriptUO
Scripting Resources & Utilities => OpenEUO Scripts => OEUO => OEUO Snippets => Topic started by: TrailMyx on August 31, 2010, 07:40:47 PM
-
Here's a port of the pathfinding sub used in my rail engine. It's always been pretty basic, but very functional for getting out of blocked conditions in Fel.
dofile("tm_subs_collection5.lua")
function TM_Pathfind(xdest, ydest, zdest)
local max_retry = 3
local retry = 0
if UO.CursKind == 0 then
max_retry = 8
end
local complete = false
while not complete do
UO.Pathfind(xdest,ydest,zdest)
local timer = TM_SecondTimer:New{duration = 8}
local move_again = false
while not move_again and not complete do
repeat
local tempx = math.abs(xdest - UO.CharPosX)
local tempy = math.abs(ydest - UO.CharPosY)
local tempz = math.abs(zdest - UO.CharPosZ)
if (tempx > 1 or tempy > 1 or tempz > 1) and (timer:Check() == false) then
break
end
if (tempx == 1 or tempy == 1) and UO.CursKind == 0 and UO.Stamina ~= UO.MaxStam then
UO.Move(xdest, ydest, 2, 1000)
break
end
if timer:Check() == true then
retry = retry + 1
if retry <= max_retry then
move_again = true
break
end
return true
else
complete = true
break
end
until false
end
end
return false
end
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
TM_Pathfind(1006,430,-63)
TM_Pathfind(1006,436,-63)
TM_Pathfind(1013,436,-63)
TM_Pathfind(1013,430,-63)
pause()
Note: requires tm_subs_collection5.lua and you need to save this file to a lua file before you run it. I've also attached the file.
-
Reserved
-
You might wonder why I'm using a nested while and repeat? One of the annoying things about Lua is there is no "continue" command. In other languages, you have a "break" that breaks you out of the present loop, and also a "continue" that will start the loop over again without running the rest of the code. In this example, I've nested a "repeat/until" which I can "break" out of so from the point of view of the enclosing "while" statement the loop starts over again...
Broken down:
while not move_again and not complete do
repeat
...
if something then
break -- breaks out of the repeat/until, while continues
end
if something_else then
break -- breaks out of the repeat/until, while continues
end
if timer:Check() == true then
return true
else
complete = true
break -- breaks out of the repeat/until, while continues (stops because complete is true)
end
until false
end