first_point = {} function slow_waypoint_init(e) ai_bot_state[e] = "patrol" end function slow_waypoint_main(e) if ai_bot_state[e] == "patrol" then PathIndex = -1 PointIndex = 2 pClosest = 99999 for pa = 1, AIGetTotalPaths(), 1 do for po = 1 , AIGetPathCountPoints(pa), 1 do pDX = g_Entity[e]['x'] - AIPathGetPointX(pa,po) pDY = g_Entity[e]['y'] - AIPathGetPointY(pa,po) pDZ = g_Entity[e]['z'] - AIPathGetPointZ(pa,po) pDist = math.sqrt(math.abs(pDX*pDX)+math.abs(pDY*pDY)+math.abs(pDZ*pDZ)); if pDist < pClosest then --and pDist < 200 then pClosest = pDist PathIndex = pa PointIndex = po end end -- po end -- pa ai_bot_pathindex[e] = PathIndex if PathIndex > -1 then ai_bot_state[e] = ai_state_startpatrol ai_bot_pointdirection[e] = 1 ai_bot_pointindex[e] = PointIndex first_point[e] = PointIndex ai_bot_pointmax[e] = AIGetPathCountPoints(PathIndex) else ai_bot_state[e] = ai_state_startidle end end if ai_bot_state[e] == ai_state_startpatrol then ai_bot_state[e] = ai_state_patrol ai_bot_pointtime[e] = g_Time + 100 SetAnimation(1) LoopAnimation(e) StartTimer(e) end if ai_bot_state[e] == ai_state_patrol then patrolx = AIPathGetPointX(ai_bot_pathindex[e],ai_bot_pointindex[e]) patroly = AIPathGetPointY(ai_bot_pathindex[e],ai_bot_pointindex[e]) patrolz = AIPathGetPointZ(ai_bot_pathindex[e],ai_bot_pointindex[e]) --module_combatcore.moveandavoid(e,AIObjNo,PlayerDist,MoveType,patrolx,patroly,patrolz,stopstate) tDistX = g_Entity[e]['x'] - patrolx tDistZ = g_Entity[e]['z'] - patrolz VertDist = math.abs(g_Entity[e]['y'] - patroly) DistFromPath = math.sqrt(math.abs(tDistX*tDistX)+math.abs(tDistZ*tDistZ)) if DistFromPath < 185 and VertDist < 195 and g_Time > ai_bot_pointtime[e] then ai_bot_pointtime[e] = g_Time + 100 StartTimer(e) if ai_bot_pointdirection[e] == 1 then ai_bot_pointindex[e] = ai_bot_pointindex[e] + 1 if ai_bot_pointindex[e] > ai_bot_pointmax[e] then ai_bot_pointindex[e] = first_point[e] --ai_bot_pointindex[e] = ai_bot_pointmax[e] -1 --ai_bot_pointdirection[e] = 0 end else ai_bot_pointindex[e] = ai_bot_pointindex[e] - 1 if ai_bot_pointindex[e] < 1 then ai_bot_pointindex[e] = 2 ai_bot_pointdirection[e] = 1 end end end pthx = AIPathGetPointX(ai_bot_pathindex[e],ai_bot_pointindex[e]) pthz = AIPathGetPointZ(ai_bot_pathindex[e],ai_bot_pointindex[e]) turn = 8-(GetDistanceToPoint(e,pthx,pthz)/100) if turn < 0.1 then turn = 0.1 end RotateToPointSlowly(e,pthx,pthz,turn) MoveForward(e,120) end end function RotateToPointSlowly(e,x,z,v) if g_Entity[e] ~= nil and x > 0 and z > 0 then if v == nil then v = 1 end local destx = x - g_Entity[e]['x'] local destz = z - g_Entity[e]['z'] local angleneeded = math.atan2(destx,destz) angleneeded = angleneeded * (180.0 / math.pi) if angleneeded < 0 then angleneeded = 360 + angleneeded elseif angleneeded > 360 then angleneeded = angleneeded - 360 end local current_angy = g_Entity[e]['angley'] while current_angy < 0 or current_angy > 360 do if current_angy <= 0 then current_angy = 360 + current_angy elseif current_angy > 360 then current_angy = current_angy - 360 end end local L = angleneeded - v local R = angleneeded + v if L <= 0 then L = 360 + L elseif L > 360 then L = L - 360 end if R <= 0 then R = 360 + R elseif R > 360 then R = R - 360 end --if not already facing the target direction (+/- the rotation speed) if current_angy < L or current_angy > R then --do it in 2 halfs so we can account for 0/360 wrap if current_angy > 180 then if angleneeded > current_angy - 180 and angleneeded < current_angy then current_angy = current_angy - v else current_angy = current_angy + v end else if angleneeded < current_angy + 180 and angleneeded > current_angy then current_angy = current_angy + v else current_angy = current_angy - v end end SetRotation(e,0,current_angy,0) return current_angy end end end function GetDistanceToPoint(e,x,z) if g_Entity[e] ~= nil and g_Entity[e] ~= 0 then local disx = g_Entity[e]['x'] - x local disz = g_Entity[e]['z'] - z return math.sqrt(disx^2 + disz^2) end end