It is best to use the AI commands than the move commands as the AI code will try to avoid obstacles and each other...
Here have a play with this...
-- roam3.lua
--
-- author's
-- idea - Honkeyboy
-- script updates - J_C
--
-- what we are trying to achieve is that the entity
-- just moves forward so far rotates 90 degrees
-- and moves back and loops = patrolling. Ideas anyone?
--
--//////////////////////////////////////////////////////////////////////////////
-- original code roam2.lua
--
-- author - Honkeyboy
--
-- function roam2_init(e)
-- CharacterControlUnarmed(e)
-- end
--
-- function roam2_main(e)
-- LookAtPlayer(e)
-- if GetPlayerDistance(e) > 200 then
-- if GetPlayerDistance(e) < 300 then
-- SetCharacterToWalk(e)
-- end
-- AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ)
-- else
-- AIEntityStop(g_Entity[e]['obj'])
-- end
-- end
--//////////////////////////////////////////////////////////////////////////////
--
-- roam3.lua
-- we will make use of some variables from global.lua script
--
-- ai_start_x = {}
-- ai_start_z = {}
--
-- set up our own variables
local pnt = 0
local destx = 0
local destz = 0
local tDistX = 0
local tDistZ = 0
local DistFromPnt = 0
-- how close to each patrol point
local near_dist = 50
local mystate = {}
-- if we need each character to have diff idle time
-- and patrol distance then use arrays
local my_idle_time = {}
local my_patrol_dist = {}
local my_patrol_pnt = {}
local mydest_x = {}
local mydest_z = {}
local max_patrol_pnts = 4
function roam3_init(e)
-- patrol point reset
my_patrol_pnt[e] = 0
end
function roam3_main(e)
-- we must be just starting up
if my_patrol_pnt[e] == 0 then
roam3_do_setup(e)
return
end
if mystate[e] == "idle" then
roam3_do_idle(e)
elseif mystate[e] == "patrol" then
roam3_do_patrol(e)
end
PromptLocal(e, mystate[e].." dist "..my_patrol_dist[e])
end
function roam3_do_setup(e)
CharacterControlUnarmed(e)
-- we will start in idle state
mystate[e] = "idle"
-- how long we will be idle
my_idle_time[e] = math.random(2,4) * 1000
-- how far we will patrol - each action
my_patrol_dist[e] = math.random(5,9) * 100
-- reset the timer
StartTimer(e)
-- set up our character start position
ai_start_x[e] = g_Entity[e]['x']
ai_start_z[e] = g_Entity[e]['z']
-- our 4 destinations
-- declare extra dimension
mydest_x[e] = {}
mydest_z[e] = {}
mydest_x[e][1] = ai_start_x[e]
mydest_z[e][1] = ai_start_z[e]
mydest_x[e][2] = mydest_x[e][1]
mydest_z[e][2] = mydest_z[e][1] + my_patrol_dist[e]
mydest_x[e][3] = mydest_x[e][2] + my_patrol_dist[e]
mydest_z[e][3] = mydest_z[e][2]
mydest_x[e][4] = mydest_x[e][3]
mydest_z[e][4] = mydest_z[e][3] - my_patrol_dist[e]
-- start patrol at point 1
my_patrol_pnt[e] = 1
end
function roam3_do_idle(e)
CharacterControlFidget(e)
-- check if our wait is over
if GetTimer(e) > my_idle_time[e] then
-- advance to next patrol point
my_patrol_pnt[e] = my_patrol_pnt[e] + 1
if my_patrol_pnt[e] > max_patrol_pnts then
-- go back to start
my_patrol_pnt[e] = 1
end
mystate[e] = "patrol"
SetCharacterToWalk(e)
end
end
function roam3_do_patrol(e)
pnt = my_patrol_pnt[e]
destx = mydest_x[e][pnt]
destz = mydest_z[e][pnt]
AIEntityGoToPosition(g_Entity[e]['obj'], destx, destz)
tDistX = g_Entity[e]['x'] - destx
tDistZ = g_Entity[e]['z'] - destz
DistFromPnt = math.sqrt(math.abs(tDistX*tDistX)+math.abs(tDistZ*tDistZ))
if DistFromPnt < near_dist then
-- we are close so lets go to idle here for few seconds
mystate[e] = "idle"
my_idle_time[e] = math.random(2,4) * 1000
-- reset the timer so our wait check will work
StartTimer(e)
end
end