local alert_range = {} local state = {} local watch_range = {} local turn_speed = {} local destx = {} local destz = {} local safe_range = {} local attack_range = {} local health = {} function gangmember_old_init(e) watch_range[e] = 600 alert_range[e] = 300 turn_speed[e] = 4 safe_range[e] = 1000 attack_range[e] = 1250 destx[e] = 0 destz[e] = 0 CharacterControlUnarmed(e) state[e] = "normal" health[e] = -1 ai_soldier_pathindex[e] = -1 end function gangmember_old_main(e) EntObjNo = g_Entity[e]['obj'] if g_Entity[e]['health'] < health[e] then state[e] = "aggro" CharacterControlStand(e) SetCharacterToRun(e) health[e] = g_Entity[e]['health'] else health[e] = g_Entity[e]['health'] end if state[e] == "normal" then CharacterControlUnarmed(e) CharacterControlStand(e) SetCharacterToWalk(e) if ai_soldier_pathindex[e] == -1 then ai_soldier_pathindex[e] = -2 -- 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 if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex; ai_path_point_index[e] = 2 ModulateSpeed(e,1.0) SetCharacterToWalk(e) ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) end end -- If we have a path then lets patrol it if 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]) AIEntityGoToPosition(EntObjNo,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 < 300 then 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 if GetPlayerDistance(e) <= alert_range[e] or AIGetEntityHeardSound(EntObjNo) == 1 or (g_MouseClick > 0 and g_PlayerGunID > 0 and GetPlayerDistance(e) < safe_range[e]) then state[e] = "alerted" --ai_soldier_pathindex[e] = -1 AIEntityGoToPosition(EntObjNo,g_Entity[e]['x'],g_Entity[e]['z']) end elseif state[e] == "alerted" then if GetPlayerDistance(e) <= watch_range[e] then RotateToPlayerSlowly(e,turn_speed[e]) if GetPlayerDistance(e) <= alert_range[e] then RotateToPlayer(e) end else state[e] = "normal" CharacterControlStand(e) end if AIGetEntityHeardSound(EntObjNo) == 1 or g_MouseClick == 1 or (g_MouseClick == 2 and EntityLookingAtPlayer(e,attack_range[e],45) == 1) then --CharacterControlDucked(e) state[e] = "aggro" end elseif state[e] == "aggro" then CharacterControlArmed(e) RotateToPlayer(e) FireWeapon(e) if GetPlayerDistance(e) >= attack_range[e] or g_Entity[e]['plrvisible'] == 0 then AIEntityGoToPosition(EntObjNo,g_PlayerPosX,g_PlayerPosZ) else AIEntityGoToPosition(EntObjNo,g_Entity[e]['x'],g_Entity[e]['z']) end end if g_PlayerHealth < 1 then state[e] = "normal" end end function EntityLookingAtPlayer(e,dis,v) if g_Entity[e] ~= nil then if dis == nil then dis = 3000 end if v == nil then v = 0.5 end local destx = g_PlayerPosX - g_Entity[e]['x'] local destz = g_PlayerPosZ - g_Entity[e]['z'] if math.sqrt((destx*destx)+(destz*destz)) <= dis then 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 while g_Entity[e]['angley'] < 0 or g_Entity[e]['angley'] > 360 do if g_Entity[e]['angley'] <= 0 then g_Entity[e]['angley'] = 360 + g_Entity[e]['angley'] elseif g_Entity[e]['angley'] > 360 then g_Entity[e]['angley'] = g_Entity[e]['angley'] - 360 end end local L = angle - v local R = angle + 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 (L < R and math.abs(g_Entity[e]['angley']) > L and math.abs(g_Entity[e]['angley']) < R) then return 1 elseif (L > R and (math.abs(g_Entity[e]['angley']) > L or math.abs(g_Entity[e]['angley']) < R)) then return 1 else return 0 end else return 0 end end end