-- Original script By SMALLGREY // -- Some little features added by Wertes Group Studio -- Details about this AI : Ally AI doesnt follow you if you press E // It will Run with Weapons Handled /armed/ to a Waypoint and if he cross a NonAlly he will try to kill him and continue to reach the waypoint. team = {} local nearest_enemy_dist = {} local target_enemy = {} local attack_range = 600 local attack_delay = 1200 local attack_damage = 25 local shout_range = 900 shouting = 0 function team_ai_run_init_name(e,name) team[e] = name ai_soldier_state[e] = "patrol" ai_soldier_pathindex[e] = -1 SetCharacterSoundSet(e) max_enemies = e if first_enemy == nil then first_enemy = e team["player"] = name end end function team_ai_run_main(e) if ai_soldier_state[e] == "patrol" then -- Try and find a close path to patrol, just check once for it if ai_soldier_pathindex[e] == -1 then ai_soldier_pathindex[e] = -2 CharacterControlStand(e) PlayCharacterSound(e,"OnIdle") -- find initial waypoint path to follow PathIndex = -1; pClosest = 99999; for pa = 1, AIGetTotalPaths(), 1 do for po = 1 , AIGetPathCountPoints(pa), 1 do pDX = g_Entity[e]['x'] - AIPathGetPointX(pa,po); pDZ = g_Entity[e]['z'] - AIPathGetPointZ(pa,po); pDist = math.sqrt(math.abs(pDX*pDX)+math.abs(pDZ*pDZ)); if pDist < pClosest and pDist < 200 then pClosest = pDist; PathIndex = pa; end end -- po end -- pa -- follow found path IT'S SEEM DOESNT RETURN TO FIRST POINT IN RUN MOD /:Wertes if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex; ai_path_point_index[e] = 2 SetCharacterToRun(e) ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) end -- If we have a path then lets patrol it elseif ai_soldier_pathindex[e] > -1 then 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]) CharacterControlArmed(e) --The style when he want to reach a waypoint running 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(math.abs(tDistX*tDistX)+math.abs(tDistZ*tDistZ)) if DistFromPath < 50 then PlayCharacterSound(e,"onAlert") CharacterControlArmed(e) if ai_path_point_direction[e] == 1 then ai_path_point_index[e] = ai_path_point_index[e] + 1 if ( ai_path_point_index[e] > ai_path_point_max[e] ) then ai_path_point_index[e] = ai_path_point_max[e] -1 ai_path_point_direction[e] = 0 end else ai_path_point_index[e] = ai_path_point_index[e] - 1 if ( ai_path_point_index[e] < 1 ) then ai_path_point_index[e] = 2 ai_path_point_direction[e] = 1 end end end else CharacterControlFidget(e) end nearest_enemy_dist[e] = attack_range target_enemy[e] = nil for a = first_enemy, max_enemies do if g_Entity[a] ~= nil then if team[a] ~= nil then if team[a] ~= team[e] then if GetDistance(e,a) < nearest_enemy_dist[e] then if g_Entity[a]['health'] > 0 then if AICouldSee(g_Entity[e]['obj'],g_Entity[a]['x'],g_Entity[a]['y'],g_Entity[a]['z']) == 1 then PlayCharacterSound(e,"onAlert") CharacterControlArmed(e) nearest_enemy_dist[e] = GetDistance(e,a) target_enemy[e] = a end end end end end end end if team[e] ~= team["player"] then if GetPlayerDistance(e) < nearest_enemy_dist[e] then if AICouldSee(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosY,g_PlayerPosZ) == 1 then nearest_enemy_dist[e] = GetPlayerDistance(e) target_enemy[e] = "player" end end end if target_enemy[e] ~= nil then StartTimer(e) ai_soldier_state[e] = "move" return end elseif ai_soldier_state[e] == "move" then if target_enemy[e] ~= "player" then if GetDistance(e,target_enemy[e]) < attack_range then ai_soldier_state[e] = "attack" CharacterControlArmed(e) PlayCharacterSound(e,"onAggro") else SetCharacterToRun(e) CharacterControlArmed(e) PlayCharacterSound(e,"onAlert") AIEntityGoToPosition(g_Entity[e]['obj'],g_Entity[target_enemy[e]]['x'],g_Entity[target_enemy[e]]['z']) end else if GetPlayerDistance(e) < attack_range then CharacterControlArmed(e) PlayCharacterSound(e,"onAggro") ai_soldier_state[e] = "attack" else SetCharacterToRun(e) AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ) end end elseif ai_soldier_state[e] == "attack" then PlayCharacterSound(e,"onAggro") if target_enemy[e] ~= "player" then if GetDistance(e,target_enemy[e]) < attack_range then AIEntityStop(g_Entity[e]['obj']) RotateToPoint(e,g_Entity[target_enemy[e]]['x'],g_Entity[target_enemy[e]]['z']) if GetTimer(e) > attack_delay then StartTimer(e) PlaySound(e,1) SetEntityHealth(target_enemy[e],g_Entity[target_enemy[e]]['health']-attack_damage) end else ai_soldier_state[e] = "move" end else if GetPlayerDistance(e) < attack_range then AIEntityStop(g_Entity[e]['obj']) RotateToPoint(e,g_PlayerPosX,g_PlayerPosZ) if GetTimer(e) > attack_delay then StartTimer(e) PlaySound(e,1) HurtPlayer(e,attack_damage) end else ai_soldier_state[e] = "move" end end elseif ai_soldier_state[e] == "toplayer" then if GetPlayerDistance(e) > 100 then SetCharacterToRun(e) AIEntityGoToPosition(g_Entity[e]['obj'],g_PlayerPosX,g_PlayerPosZ) else ai_soldier_state[e] = "patrol" CharacterControlStand(e) end end --state ----- if target_enemy[e] ~= nil then if target_enemy[e] ~= "player" then PlayCharacterSound(e,"onAggro") if g_Entity[target_enemy[e]] == nil or g_Entity[target_enemy[e]]['health'] < 1 or GetTimer(e) > attack_delay * 6 then StartTimer(e) target_enemy[e] = nil ai_soldier_state[e] = "patrol" CharacterControlStand(e) end else if g_PlayerHealth < 1 or GetTimer(e) > attack_delay * 6 then StartTimer(e) target_enemy[e] = nil ai_soldier_state[e] = "patrol" CharacterControlStand(e) end end end end --main(e) function team_exit(e) end function GetDistance(e,v) if g_Entity[e] ~= nil and g_Entity[e] ~= 0 and g_Entity[v] ~= nil and g_Entity[v] ~= 0 then local disx = g_Entity[e]['x'] - g_Entity[v]['x'] local disz = g_Entity[e]['z'] - g_Entity[v]['z'] local disy = g_Entity[e]['y'] - g_Entity[v]['y'] return math.sqrt(disx^2 + disz^2 + disy^2) end end function RotateToPoint(e,x,z) if g_Entity[e] ~= nil and x > 0 and z > 0 then local destx = x - g_Entity[e]['x'] local destz = z - g_Entity[e]['z'] local angle = math.atan2(destx,destz) angle = angle * (180.0 / math.pi) if angle < 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end SetRotation(e,0,angle,0) return angle end end