local alert_range = nil local already_seen = 0 local temp = 0 function security_guard_init(e) ai_soldier_state[e] = "patrol" ai_soldier_pathindex[e] = -1 end function security_guard_main(e) EntObjNo = g_Entity[e]['obj'] if alert_range == nil then alert_range = AIGetEntityViewRange(EntObjNo)/2 end if ai_soldier_state[e] == "patrol" then if ai_soldier_pathindex[e] == -1 then ai_soldier_pathindex[e] = -2 CharacterControlUnarmed(e) PathIndex = -1 PathPointIndex = -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 PathPointIndex = po end end -- po end -- pa if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex ai_path_point_index[e] = PathPointIndex ModulateSpeed(e,1.0) SetCharacterToWalk(e) ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) end 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]) 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 < 50 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 (g_Entity[e]['plrvisible'] == 1 or GetPlayerDistance(e) < alert_range) and temp == 0 then oldx = g_Entity[e]['x'] oldz = g_Entity[e]['z'] FreezePlayer(e) ai_soldier_state[e] = "evict" elseif g_Entity[e]['plrvisible'] == 0 then temp = 0 end elseif ai_soldier_state[e] == "evict" then if GetPlayerDistance(e) > 120 then AIEntityGoToPosition(EntObjNo,g_PlayerPosX,g_PlayerPosZ) else UnFreezePlayer(e) if already_seen == 0 and temp == 0 then PromptDuration("I'm afraid you need an appointment sir",3000) PlaySound(e,0) TransportToIfUsed(e) already_seen = 1 temp = 1 elseif already_seen == 1 and temp == 0 then PromptDuration("You have been ejected from the building. You might want to try sneaking past the guard.",3000) PlaySound(e,0) TransportToIfUsed(e) temp = 1 end AIEntityGoToPosition(EntObjNo,oldx,oldz) SetPosition(e,oldx,g_Entity[e]['y'],oldz) RotateToPoint(e,ai_patrol_x[e],ai_patrol_z[e]) ai_soldier_state[e] = "patrol" g_Entity[e]['plrvisible'] = 0 end 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