local U = require "scriptbank\\utillib" local Q = require "scriptbank\\quatlib" local rad = math.rad local deg = math.deg local floor = math.floor local atan = math.atan2 local abs = math.abs local sqrt = math.sqrt local reset_time = 1500 --time until the pointer gets reset automatically (low time stops it taking a long time to return for long paths) isObjective = {} current_objective = nil local state = {} epress = 0 --[[ set 'current_objective = #' in your scripts to update the path i.e. current_objective = e or current_objective = 12 (where 12 = the entity you want to lead the player to) --]] function quest_path_init(e) state[e] = "reset path" CollisionOff(e) CharacterControlManual(e) Hide(e) end function quest_path_main(e) --[[ if g_KeyPressE == 1 then if epress == 0 then epress = 1 current_objective = AddQuestObjective() end else epress = 0 end if g_KeyPressQ == 1 then start = 1 end if start == nil then return end --]] local co = current_objective local x,y,z = GetEntityPosAng(e) if co ~= nil then local ox,oy,oz = GetEntityPosAng(co) end local obj = g_Entity[e]['obj'] local px,py,pz = g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ if state[e] == "reset path" then local tx,ty,tz = GetEntityPosAng(e) if U.CloserThan(px,py,pz,tx,ty,tz,70) then --SetRotation(e,0,AIGetEntityAngleY(obj),0) if current_objective ~= nil then Show(e) ModulateSpeed(e,1) StartTimer(e) state[e] = "find path" end else Hide(e) ModulateSpeed(e,20) AIEntityGoToPosition(obj,px,pz) SetRotation(e,0,AIGetEntityAngleY(obj),0) MoveForward(e,AIGetEntitySpeed(obj)*1) AISetEntityPosition(obj,GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) end elseif state[e] == "find path" then if U.CloserThan(x,y,z,ox,oy,oz,70) or GetTimer(e) > reset_time then state[e] = "reset path" else AIEntityGoToPosition(obj,ox,oz) local tang = AIGetEntityAngleY(obj) --Text(50,70,3,tang) SetRotation(e,0,tang,0) MoveForward(e,AIGetEntitySpeed(obj)*1) AISetEntityPosition(obj,GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) end end if current_objective ~= nil then --Prompt("current objective = "..current_objective.." , AI char state = "..state[e]) else --TextCenterOnX(50,80,3,"no current objective") end end function AddQuestObjective() local dist = 99999999 local px,py,pz = g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ local closest_obj = nil for a,b in pairs (isObjective) do local ox,oy,oz = GetEntityPosAng(a) local tdist = TCloserThan(px,py,pz,ox,oy,oz) if tdist < dist then dist = tdist closest_obj = a end end return closest_obj end function TCloserThan( x1, y1, z1, x2, y2, z2) local dx, dy, dz = x1 - x2, y1 - y2, z1 - z2 return (dx*dx+dy*dy+dz*dz) end