local found_next = {} local closest_point = {} local max_health = {} local PathPoint = {} local prev_point = {} local player_max_health = nil DistFromPath = 0 pDist = 0 pDis2 = 0 pClosest = 0 function ai_control_waypoint_init(e) ai_soldier_state[e] = "find path" ai_soldier_pathindex[e] = -1 found_next[e] = 0 closest_point[e] = 0 end function ai_control_waypoint_main(e) if max_health[e] == nil then max_health[e] = g_Entity[e]['health'] end if player_max_health == nil then player_max_health = g_PlayerHealth end if ai_soldier_state[e] == "find path" then -- Try and find a close path to patrol, just check once for it if ai_soldier_pathindex[e] == -1 then found_next[e] = 0 ai_soldier_pathindex[e] = -2 CharacterControlArmed(e) -- find initial waypoint path to follow PathIndex = -1; pClosest = 99999; for pa = 1, AIGetTotalPaths(), 1 do for po = 1 , AIGetPathCountPoints(pa), 1 do pDX1 = g_Entity[e]['x'] - AIPathGetPointX(pa,po); pDZ1 = g_Entity[e]['z'] - AIPathGetPointZ(pa,po); pDist = math.sqrt((pDX1*pDX1)+(pDZ1*pDZ1)); if pDist < pClosest and pDist < 100 then pClosest = pDist; PathIndex = pa; PathPoint[e] = po prev_point[e] = po end end -- po end -- pa -- follow found path if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex ai_path_point_index[e] = PathPoint[e] ModulateSpeed(e,1.2) SetCharacterToRun(e) ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) ai_soldier_state[e] = "patrol" end end elseif ai_soldier_state[e] == "patrol" then -- If we have a path then lets patrol it if (g_Entity[e]['health'] > max_health[e] / 3) or (g_PlayerHealth < player_max_health / 3) then LookAtPlayer(e) if g_Entity[e]['plrvisible'] == 1 then ai_path_point_index[e] = prev_point[e] AIEntityGoToPosition(g_Entity[e]['obj'],g_Entity[e]['x'],g_Entity[e]['z']) AIEntityStop(e) CharacterControlArmed(e) RotateToPlayer(e) FireWeapon(e) else CharacterControlArmed(e) ai_patrol_x[e] = AIPathGetPointX(ai_soldier_pathindex[e],ai_path_point_index[e]) ai_patrol_z[e] = AIPathGetPointZ(ai_soldier_pathindex[e],ai_path_point_index[e]) AIEntityGoToPosition(g_Entity[e]['obj'],ai_patrol_x[e],ai_patrol_z[e]) tDistX = g_Entity[e]['x'] - ai_patrol_x[e] tDistZ = g_Entity[e]['z'] - ai_patrol_z[e] DistFromPath = math.sqrt((tDistX*tDistX)+(tDistZ*tDistZ)) if DistFromPath < 60 then if found_next[e] == 0 then PathIndex = -1; pClosest = 99999; for pa = 1, AIGetTotalPaths(), 1 do for po = 1 , AIGetPathCountPoints(pa), 1 do pDX = g_PlayerPosX - AIPathGetPointX(pa,po); pDZ = g_PlayerPosZ - AIPathGetPointZ(pa,po); pDist = math.sqrt((pDX*pDX)+(pDZ*pDZ)); if pDist < pClosest then pClosest = pDist; closest_point[e] = po; end end -- po end -- pa -- follow found path if closest_point[e] ~= ai_path_point_index[e] then if closest_point[e] > ai_path_point_max[e]/2 then if ai_path_point_index[e] > closest_point[e] - (ai_path_point_max[e]/2) and closest_point[e] < ai_path_point_index[e] then ai_path_point_index[e] = ai_path_point_index[e] - 1 else ai_path_point_index[e] = ai_path_point_index[e] + 1 end else --closest point below 1/2 if ai_path_point_index[e] < closest_point[e] + (ai_path_point_max[e]/2) and ai_path_point_index[e] > closest_point[e] then ai_path_point_index[e] = ai_path_point_index[e] - 1 else ai_path_point_index[e] = ai_path_point_index[e] + 1 end end if ai_path_point_index[e] > ai_path_point_max[e] then ai_path_point_index[e] = 2 elseif ai_path_point_index[e] < 1 then ai_path_point_index[e] = ai_path_point_max[e]-1 end found_next[e] = 1 else ai_path_point_index[e] = ai_path_point_index[e] + math.random(0,1) if ai_path_point_index[e] == closest_point[e] then ai_path_point_index[e] = ai_path_point_index[e] - 1 end if ai_path_point_index[e] < 1 then ai_path_point_index[e] = ai_path_point_max[e] end if ai_path_point_index[e] > ai_path_point_max[e] then ai_path_point_index[e] = 1 end found_next[e] = 1 end end else prev_point[e] = ai_path_point_index[e] found_next[e] = 0 end end --we're hurt so run away else ai_patrol_x[e] = AIPathGetPointX(ai_soldier_pathindex[e],ai_path_point_index[e]) ai_patrol_z[e] = AIPathGetPointZ(ai_soldier_pathindex[e],ai_path_point_index[e]) AIEntityGoToPosition(g_Entity[e]['obj'],ai_patrol_x[e],ai_patrol_z[e]) tDistX = g_Entity[e]['x'] - ai_patrol_x[e] tDistZ = g_Entity[e]['z'] - ai_patrol_z[e] DistFromPath = math.sqrt((tDistX*tDistX)+(tDistZ*tDistZ)) if DistFromPath < 60 then if found_next[e] == 0 then PathIndex = -1; pClosest = 0; for pa = 1, AIGetTotalPaths(), 1 do for po = 1 , AIGetPathCountPoints(pa), 1 do pDX = g_PlayerPosX - AIPathGetPointX(pa,po); pDZ = g_PlayerPosZ - AIPathGetPointZ(pa,po); pDist = math.sqrt((pDX*pDX)+(pDZ*pDZ)); if pDist > pClosest then pDx1 = g_Entity[e]['x'] - AIPathGetPointX(pa,po); pDz1 = g_Entity[e]['z'] - AIPathGetPointZ(pa,po); pDis2 = math.sqrt((pDX1*pDX1)+(pDZ1*pDZ1)); if pDis2 < pDist then pClosest = pDist; closest_point[e] = po; end end end -- po end -- pa -- follow found path if closest_point[e] ~= ai_path_point_index[e] then if closest_point[e] > ai_path_point_max[e]/2 then if ai_path_point_index[e] + (ai_path_point_max[e]/2) < closest_point[e] then ai_path_point_index[e] = ai_path_point_index[e] - 1 else ai_path_point_index[e] = ai_path_point_index[e] + 1 end else if ai_path_point_index[e] - (ai_path_point_max[e]/2) > closest_point[e] then ai_path_point_index[e] = ai_path_point_index[e] + 1 else ai_path_point_index[e] = ai_path_point_index[e] - 1 end end if ai_path_point_index[e] > ai_path_point_max[e] then ai_path_point_index[e] = 2 elseif ai_path_point_index[e] < 1 then ai_path_point_index[e] = ai_path_point_max[e] - 1 end found_next[e] = 1 end end else prev_point[e] = ai_path_point_index[e] found_next[e] = 0 end end end --ai state --Prompt(closest_point[e].." "..ai_path_point_index[e]) end --main function ai_control_waypoint_exit(e) end function GetPlayerFlatDistance(e) tPlayerDX = (g_Entity[e]['x'] - g_PlayerPosX) tPlayerDZ = (g_Entity[e]['z'] - g_PlayerPosZ) return math.sqrt(math.abs(tPlayerDX*tPlayerDX)+math.abs(tPlayerDZ*tPlayerDZ)); end