Caliber_

Gmod leak dump part 13: rabidtoaster's aimbot

Dec 31st, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 32.17 KB | None | 0 0
  1. if SERVER then return end
  2.  
  3. local AA = {}
  4.  
  5. local concommand = concommand
  6. local cvars = cvars
  7. local debug = debug
  8. local ents = ents
  9. local file = file
  10. local hook = hook
  11. local math = math
  12. local spawnmenu = spawnmenu
  13. local string = string
  14. local surface = surface
  15. local table = table
  16. local timer = timer
  17. local util = util
  18. local vgui = vgui
  19.  
  20. local Angle = Angle
  21. local CreateClientConVar = CreateClientConVar
  22. local CurTime = CurTime
  23. local ErrorNoHalt = ErrorNoHalt
  24. local FrameTime = FrameTime
  25. local GetConVarString = GetConVarString
  26. local GetViewEntity = GetViewEntity
  27. local include = include
  28. local ipairs = ipairs
  29. local LocalPlayer = LocalPlayer
  30. local pairs = pairs
  31. local pcall = pcall
  32. local print = print
  33. local RunConsoleCommand = RunConsoleCommand
  34. local ScrH = ScrH
  35. local ScrW = ScrW
  36. local tonumber = tonumber
  37. local type = type
  38. local unpack = unpack
  39. local ValidEntity = IsValid
  40. local Vector = Vector
  41.  
  42. do
  43.     local hooks = {}
  44.     local created = {}
  45.     local function CallHook(self, name, args)
  46.         if !hooks[name] then return end
  47.         for funcName, _ in pairs(hooks[name]) do
  48.             local func = self[funcName]
  49.             if func then
  50.                 local ok, err = pcall(func, self, unpack(args or {}))
  51.                 if !ok then
  52.                     ErrorNoHalt(err .. "\n")
  53.                 elseif err then
  54.                     return err
  55.                 end
  56.             end
  57.         end
  58.     end
  59.     local function RandomName()
  60.         local random = ""
  61.         for i = 1, math.random(4, 10) do
  62.             local c = math.random(65, 116)
  63.             if c >= 91 && c <= 96 then c = c + 6 end
  64.             random = random .. string.char(c)
  65.         end
  66.         return random
  67.     end
  68.     local function AddHook(self, name, funcName)
  69.         // If we haven't got a hook for this yet, make one with a random name and store it.
  70.        // This is so anti-cheats can't detect by hook name, and so we can remove them later.
  71.         if !created[name] then
  72.             local random = RandomName()
  73.             hook.Add(name, random, function(...) return CallHook(self, name, {...}) end)
  74.             created[name] = random
  75.         end
  76.        
  77.         hooks[name] = hooks[name] or {}
  78.         hooks[name][funcName] = true
  79.     end
  80.    
  81.     local cvarhooks = {}
  82.     local function GetCallbackTable(convar)
  83.         local callbacks = cvars.GetConVarCallbacks(convar)
  84.         if !callbacks then
  85.             cvars.AddChangeCallback(convar, function() end)
  86.             callbacks = cvars.GetConVarCallbacks(convar)
  87.         end
  88.         return callbacks
  89.     end
  90.            
  91.     local function AddCVarHook(self, convar, funcName, ...)
  92.         local hookName = "CVar_" .. convar
  93.         if !cvarhooks[convar] then
  94.             local random = RandomName()
  95.            
  96.             local callbacks = GetCallbackTable(convar)
  97.             callbacks[random] = function(...)
  98.                 CallHook(self, hookName, {...})
  99.             end
  100.            
  101.             cvarhooks[convar] = random
  102.         end
  103.         AddHook(self, hookName, funcName)
  104.     end
  105.    
  106.     // Don't let other scripts remove our hooks.
  107.    local oldRemove = hook.Remove
  108.    function hook.Remove(name, unique)
  109.        if created[name] == unique then return end
  110.        oldRemove(name, unique)
  111.    end
  112.    
  113.    // Removes all hooks, useful if reloading the script.
  114.    local function RemoveHooks()
  115.        for hookName, unique in pairs(created) do
  116.            oldRemove(hookName, unique)
  117.        end
  118.        for convar, unique in pairs(cvarhooks) do
  119.            local callbacks = GetCallbackTable(convar)
  120.            callbacks[unique] = nil
  121.        end
  122.    end
  123.    
  124.    // Add copies the script can access.
  125.    AA.AddHook = AddHook
  126.    AA.AddCVarHook = AddCVarHook
  127.    AA.CallHook = CallHook
  128.    AA.RemoveHooks = RemoveHooks
  129. end
  130.  
  131. concommand.Add("aa_reload", function()
  132.    AA:CallHook("Shutdown")
  133.    print("Removing hooks...")
  134.    AA:RemoveHooks()
  135.    
  136.    AA = nil
  137.    local info = debug.getinfo(1, "S")
  138.    if info && info.short_src then
  139.        if string.Left(info.short_src, 3) == "lua" then
  140.            info.short_src = string.sub(info.short_src, 5)
  141.        end
  142.        print("Reloading (" .. info.short_src .. ")...")
  143.        include(info.short_src)
  144.    else
  145.        print("Cannot find AutoAim file, reload manually.")
  146.    end
  147. end)
  148. print("AutoAim loaded.")
  149.  
  150. // ##################################################
  151. // MetaTables
  152. // ##################################################
  153.  
  154. local function GetMeta(name)
  155.    return table.Copy(FindMetaTable(name) or {})
  156. end
  157.  
  158. local AngM = GetMeta("Angle")
  159. local CmdM = GetMeta("CUserCmd")
  160. local EntM = GetMeta("Entity")
  161. local PlyM = GetMeta("Player")
  162. local VecM = GetMeta("Vector")
  163.  
  164. // ##################################################
  165. // Settings
  166. // ##################################################
  167.  
  168. do
  169.    local settings = {}
  170.    local function SettingVar(self, name)
  171.        return (self.SettingPrefix or "") .. string.lower(name)
  172.    end
  173.    
  174.    local function RandomName()
  175.        local random = ""
  176.        for i = 1, math.random(4, 10) do
  177.            local c = math.random(65, 116)
  178.            if c >= 91 && c <= 96 then c = c + 6 end
  179.            random = random .. string.char(c)
  180.        end
  181.        return random
  182.    end
  183.    
  184.    local function SetSetting(name, _, new)
  185.        if !settings[name] then return end
  186.        local info = settings[name]
  187.        
  188.        if info.Type == "number" then
  189.            new = tonumber(new)
  190.        elseif info.Type == "boolean" then
  191.            new = (tonumber(new) or 0) > 0
  192.        end
  193.        
  194.        info.Value = new
  195.    end
  196.    
  197.    local function CreateSetting(self, name, desc, default, misc)
  198.        local cvar = SettingVar(self, name)
  199.        local info = {Name = name, Desc = desc, CVar = cvar, Type = type(default), Value = default}
  200.        
  201.        for k, v in pairs(misc or {}) do
  202.            if !info[k] then info[k] = v end
  203.        end
  204.        
  205.        // Convert default from boolean to number.
  206.        if type(default) == "boolean" then
  207.            default = default and 1 or 0
  208.        end
  209.        
  210.        if !settings[cvar] then
  211.            local tab = cvars.GetConVarCallbacks(cvar)
  212.            if !tab then
  213.                cvars.AddChangeCallback(cvar, function() end)
  214.                tab = cvars.GetConVarCallbacks(cvar)
  215.            end
  216.            
  217.            while true do
  218.                local name = RandomName()
  219.                if !tab[name] then
  220.                    tab[name] = SetSetting
  221.                    info.Callback = name
  222.                    break
  223.                end
  224.            end
  225.        end
  226.        
  227.        settings[cvar] = info
  228.        settings[#settings + 1] = info
  229.        
  230.        // Create the convar.
  231.        CreateClientConVar(cvar, default, (info.Save != false), false)
  232.        SetSetting(cvar, _, GetConVarString(cvar))
  233.    end
  234.    local function GetSetting(self, name)
  235.        local cvar = SettingVar(self, name)
  236.        if !settings[cvar] then return end
  237.        return settings[cvar].Value
  238.    end
  239.    local function Shutdown()
  240.        print("Removing settings callbacks...")
  241.        for _, info in ipairs(settings) do
  242.            if info.CVar && info.Callback then
  243.                local tab = cvars.GetConVarCallbacks(info.CVar)
  244.                if tab then
  245.                    tab[info.Callback] = nil
  246.                end
  247.            end
  248.        end
  249.    end
  250.    local function SettingsList()
  251.        return table.Copy(settings)
  252.    end
  253.    local function BuildMenu(self, panel)
  254.        for _, info in ipairs(settings) do
  255.            if info.Show != false then
  256.                if info.MultiChoice then
  257.                    local m = panel:MultiChoice(info.Desc or info.CVar, info.CVar)
  258.                    for k, v in pairs(info.MultiChoice) do
  259.                        m:AddChoice(k, v)
  260.                    end
  261.                elseif info.Type == "number" then
  262.                    panel:NumSlider(info.Desc or info.CVar, info.CVar, info.Min or -1, info.Max or -1, info.Places or 0)
  263.                elseif info.Type == "boolean" then
  264.                    panel:CheckBox(info.Desc or info.CVar, info.CVar)
  265.                elseif info.Type == "string" then
  266.                    panel:TextEntry(info.Desc or info.CVar, info.CVar)
  267.                end
  268.            end
  269.        end
  270.    end
  271.    
  272.    AA.SettingPrefix = "aa_"
  273.    AA.CreateSetting = CreateSetting
  274.    AA.Setting = GetSetting
  275.    AA.SettingsList = SettingsList
  276.    AA.BuildMenu = BuildMenu
  277.    
  278.    AA.SettingsShutdown = Shutdown
  279.    AA:AddHook("Shutdown", "SettingsShutdown")
  280. end
  281.  
  282.  
  283. // ##################################################
  284. // Targetting - Positions
  285. // ##################################################
  286.  
  287. AA.ModelTarget = {}
  288. function AA:SetModelTarget(model, targ)
  289.    self.ModelTarget[model] = targ
  290. end
  291. function AA:BaseTargetPosition(ent)
  292.    // The eye attachment is a lot more stable than bones for players.
  293.    if type(ent) == "Player" then
  294.        local head = EntM["LookupAttachment"](ent, "eyes")
  295.        if head then
  296.            local pos = EntM["GetAttachment"](ent, head)
  297.            if pos then
  298.                return pos.Pos - (AngM["Forward"](pos.Ang) * 2)
  299.            end
  300.        end
  301.    end
  302.    
  303.    // Check if the model has a special target assigned to it.
  304.    local special = self.ModelTarget[string.lower(EntM["GetModel"](ent) or "")]
  305.    if special then
  306.        // It's a string - look for a bone.
  307.         if type(special) == "string" then
  308.             local bone = EntM["LookupBone"](ent, special)
  309.             if bone then
  310.                 local pos = EntM["GetBonePosition"](ent, bone)
  311.                 if pos then
  312.                     return pos
  313.                 end
  314.             end
  315.         // It's a Vector - return a relative position.
  316.        elseif type(special) == "Vector" then
  317.            return EntM["LocalToWorld"](ent, special)
  318.        // It's a function - do something fancy!
  319.         elseif type(special) == "function" then
  320.             local pos = pcall(special, ent)
  321.             if pos then return pos end
  322.         end
  323.     end
  324.  
  325.     // Try and use the head bone, found on all of the player + human models.
  326.     local bone = "ValveBiped.Bip01_Head1"
  327.     local head = EntM["LookupBone"](ent, bone)
  328.     if head then
  329.         local pos = EntM["GetBonePosition"](ent, head)
  330.         if pos then
  331.             return pos
  332.         end
  333.     end
  334.  
  335.     // Give up and return the center of the entity.
  336.     return EntM["LocalToWorld"](ent, EntM["OBBCenter"](ent))
  337. end
  338. function AA:TargetPosition(ent)
  339.     local targetPos = self:BaseTargetPosition(ent)
  340.    
  341.     local ply = LocalPlayer()
  342.     if ValidEntity(ply) then
  343.         targetPos = self:CallHook("TargetPrediction", {ply, ent, targetPos}) or targetPos
  344.     end
  345.    
  346.     return targetPos
  347. end
  348.  
  349. AA:SetModelTarget("models/crow.mdl", Vector(0, 0, 5))                        // Crow.
  350. AA:SetModelTarget("models/pigeon.mdl", Vector(0, 0, 5))                     // Pigeon.
  351. AA:SetModelTarget("models/seagull.mdl", Vector(0, 0, 6))                     // Seagull.
  352. AA:SetModelTarget("models/combine_scanner.mdl", "Scanner.Body")                 // Scanner.
  353. AA:SetModelTarget("models/hunter.mdl", "MiniStrider.body_joint")             // Hunter.
  354. AA:SetModelTarget("models/combine_turrets/floor_turret.mdl", "Barrel")         // Turret.
  355. AA:SetModelTarget("models/dog.mdl", "Dog_Model.Eye")                         // Dog.
  356. AA:SetModelTarget("models/vortigaunt.mdl", "ValveBiped.Head")                 // Vortigaunt.
  357. AA:SetModelTarget("models/antlion.mdl", "Antlion.Body_Bone")                     // Antlion.
  358. AA:SetModelTarget("models/antlion_guard.mdl", "Antlion_Guard.Body")             // Antlion guard.
  359. AA:SetModelTarget("models/antlion_worker.mdl", "Antlion.Head_Bone")             // Antlion worker.
  360. AA:SetModelTarget("models/zombie/fast_torso.mdl", "ValveBiped.HC_BodyCube")     // Fast zombie torso.
  361. AA:SetModelTarget("models/zombie/fast.mdl", "ValveBiped.HC_BodyCube")         // Fast zombie.
  362. AA:SetModelTarget("models/headcrabclassic.mdl", "HeadcrabClassic.SpineControl") // Normal headcrab.
  363. AA:SetModelTarget("models/headcrabblack.mdl", "HCBlack.body")                 // Poison headcrab.
  364. AA:SetModelTarget("models/headcrab.mdl", "HCFast.body")                         // Fast headcrab.
  365. AA:SetModelTarget("models/zombie/poison.mdl", "ValveBiped.Headcrab_Cube1")     // Poison zombie.
  366. AA:SetModelTarget("models/zombie/classic.mdl", "ValveBiped.HC_Body_Bone")     // Zombie.
  367. AA:SetModelTarget("models/zombie/classic_torso.mdl", "ValveBiped.HC_Body_Bone") // Zombie torso.
  368. AA:SetModelTarget("models/zombie/zombie_soldier.mdl", "ValveBiped.HC_Body_Bone") // Zombine.
  369. AA:SetModelTarget("models/combine_strider.mdl", "Combine_Strider.Body_Bone") // Strider.
  370. AA:SetModelTarget("models/combine_dropship.mdl", "D_ship.Spine1")             // Combine dropship.
  371. AA:SetModelTarget("models/combine_helicopter.mdl", "Chopper.Body")             // Combine helicopter.
  372. AA:SetModelTarget("models/gunship.mdl", "Gunship.Body")                        // Combine gunship.
  373. AA:SetModelTarget("models/lamarr.mdl", "HeadcrabClassic.SpineControl")        // Lamarr!
  374. AA:SetModelTarget("models/mortarsynth.mdl", "Root Bone")                        // Mortar synth.
  375. AA:SetModelTarget("models/synth.mdl", "Bip02 Spine1")                        // Synth.
  376. AA:SetModelTarget("models/vortigaunt_slave.mdl", "ValveBiped.Head")            // Vortigaunt slave.
  377.  
  378.  
  379. // ##################################################
  380. // Targetting - General
  381. // ##################################################
  382.  
  383. AA.NPCDeathSequences = {}
  384. function AA:AddNPCDeathSequence(model, sequence)
  385.     self.NPCDeathSequences = self.NPCDeathSequences or {}
  386.     self.NPCDeathSequences[model] = self.NPCDeathSequences[model] or {}
  387.     if !table.HasValue(self.NPCDeathSequences[model]) then
  388.         table.insert(self.NPCDeathSequences[model], sequence)
  389.     end
  390. end
  391.  
  392. AA:AddNPCDeathSequence("models/barnacle.mdl", 4)
  393. AA:AddNPCDeathSequence("models/barnacle.mdl", 15)
  394. AA:AddNPCDeathSequence("models/antlion_guard.mdl", 44)
  395. AA:AddNPCDeathSequence("models/hunter.mdl", 124)
  396. AA:AddNPCDeathSequence("models/hunter.mdl", 125)
  397. AA:AddNPCDeathSequence("models/hunter.mdl", 126)
  398. AA:AddNPCDeathSequence("models/hunter.mdl", 127)
  399. AA:AddNPCDeathSequence("models/hunter.mdl", 128)
  400.  
  401. AA:CreateSetting("friendlyfire", "Target teammates", false)
  402. function AA:IsValidTarget(ent)
  403.     // We only want players/NPCs.
  404.     local typename = type(ent)
  405.     if typename != "NPC" && typename != "Player" then return false end
  406.  
  407.     // No invalid entities.
  408.     if !ValidEntity(ent) then return false end
  409.  
  410.     // Go shoot yourself, emo kid.
  411.     local ply = LocalPlayer()
  412.     if ent == ply then return false end
  413.  
  414.     if typename == "Player" then
  415.         if !PlyM["Alive"](ent) then return false end // Dead players FTL.
  416.         if !self:Setting("friendlyfire") && PlyM["Team"](ent) == PlyM["Team"](ply) then return false end
  417.         if EntM["GetMoveType"](ent) == MOVETYPE_OBSERVER then return false end // No spectators.
  418.         if EntM["GetMoveType"](ent) == MOVETYPE_NONE then return false end
  419.         //if pl["Team"](ent) == 1001 then return false end
  420.     end
  421.  
  422.     if typename == "NPC" then
  423.         if EntM["GetMoveType"](ent) == MOVETYPE_NONE then return false end // No dead NPCs.
  424.  
  425.         // No dying NPCs.
  426.         local model = string.lower(EntM["GetModel"](ent) or "")
  427.         if table.HasValue(self.NPCDeathSequences[model] or {}, EntM["GetSequence"](ent)) then return false end
  428.     end
  429. end
  430.  
  431. AA:CreateSetting("predictblocked", "Predict blocked (time)", 0.4, {Min = 0, Max = 1})
  432. function AA:BaseBlocked(target, offset)
  433.     local ply = LocalPlayer()
  434.     if !ValidEntity(ply) then return end
  435.    
  436.     // Trace from the players shootpos to the position.
  437.     local shootPos = PlyM["GetShootPos"](ply)
  438.     local targetPos = self:TargetPosition(target)
  439.    
  440.     if offset then targetPos = targetPos + offset end
  441.  
  442.     local trace = util.TraceLine({start = shootPos, endpos = targetPos, filter = {ply, target}, mask = MASK_SHOT})
  443.     local wrongAim = self:AngleBetween(PlyM["GetAimVector"](ply), VecM["GetNormal"](targetPos - shootPos)) > 2
  444.  
  445.     // If we hit something, we're "blocked".
  446.    if trace.Hit && trace.Entity != target then
  447.        return true, wrongAim
  448.    end
  449.  
  450.    // It is not blocked.
  451.    return false, wrongAim
  452. end
  453. function AA:TargetBlocked(target)
  454.    if !target then target = self:GetTarget() end
  455.    if !target then return end
  456.    
  457.    local blocked, wrongAim = self:BaseBlocked(target)
  458.    if self:Setting("predictblocked") > 0 && blocked then
  459.        blocked = self:BaseBlocked(target, EntM["GetVelocity"](target) * self:Setting("predictblocked"))
  460.    end
  461.    return blocked, wrongAim
  462. end
  463.    
  464.  
  465. function AA:SetTarget(ent)
  466.    if self.Target && !ent then
  467.        self:CallHook("TargetLost")
  468.    elseif !self.Target && ent then
  469.        self:CallHook("TargetGained")
  470.    elseif self.Target && ent && self.Target != ent then
  471.        self:CallHook("TargetChanged")
  472.    end
  473.  
  474.    self.Target = ent
  475. end
  476. function AA:GetTarget()
  477.    if ValidEntity(self.Target) != false then
  478.        return self.Target
  479.    else
  480.        return false
  481.    end
  482. end
  483.  
  484. AA:CreateSetting("maxangle", "Max angle", 30, {Min = 5, Max = 90})
  485. AA:CreateSetting("targetblocked", "Don't check LOS", false)
  486. AA:CreateSetting("holdtarget", "Hold targets", false)
  487. function AA:FindTarget()
  488.    if !self:Enabled() then return end
  489.  
  490.    local ply = LocalPlayer()
  491.    if !ValidEntity(ply) then return end
  492.  
  493.    local maxAng = self:Setting("maxangle")
  494.    local aimVec, shootPos = PlyM["GetAimVector"](ply), PlyM["GetShootPos"](ply)
  495.    local targetBlocked = self:Setting("targetblocked")
  496.  
  497.    if self:Setting("holdtarget") then
  498.        local target = self:GetTarget()
  499.        if target then
  500.            local targetPos = self:TargetPosition(target)
  501.            local angle = self:AngleBetween(AngM["Forward"](self:GetView()), VecM["GetNormal"](targetPos - shootPos))
  502.            local blocked = self:TargetBlocked(target)
  503.            if angle <= maxAng && (!blocked || targetBlocked) then return end
  504.        end
  505.    end
  506.  
  507.    // Filter out targets.
  508.    local targets = ents.GetAll()
  509.    for i, ent in pairs(targets) do
  510.        if self:IsValidTarget(ent) == false then
  511.            targets[i] = nil
  512.        end
  513.    end
  514.  
  515.    local closestTarget, lowestAngle = _, maxAng
  516.    for _, target in pairs(targets) do
  517.        if targetBlocked || !self:TargetBlocked(target) then
  518.            local targetPos = self:TargetPosition(target)
  519.            local angle = self:AngleBetween(AngM["Forward"](self:GetView()), VecM["GetNormal"](targetPos - shootPos))
  520.  
  521.            if angle < lowestAngle then
  522.                lowestAngle = angle
  523.                closestTarget = target
  524.            end
  525.        end
  526.    end
  527.  
  528.    self:SetTarget(closestTarget)
  529. end
  530. AA:AddHook("Think", "FindTarget")
  531.  
  532.  
  533. // ##################################################
  534. // Fake view
  535. // ##################################################
  536.  
  537. AA.View = Angle(0, 0, 0)
  538. function AA:GetView()
  539.    return self.View * 1
  540. end
  541. function AA:KeepView()
  542.    if !self:Enabled() then return end
  543.  
  544.    local ply = LocalPlayer()
  545.    if !ValidEntity(ply) then return end
  546.  
  547.    self.View = EntM["EyeAngles"](ply)
  548. end
  549. AA:AddHook("OnToggled", "KeepView")
  550.  
  551. local sensitivity = 0.022
  552. function AA:RotateView(cmd)
  553.    self.View.p = math.Clamp(self.View.p + (CmdM["GetMouseY"](cmd) * sensitivity), -89, 89)
  554.    self.View.y = math.NormalizeAngle(self.View.y + (CmdM["GetMouseX"](cmd) * sensitivity * -1))
  555. end
  556.  
  557. AA:CreateSetting("debug", "Debug", false, {Show = false})
  558. function AA:FakeView(ply, origin, angles, FOV)
  559.    if !self:Enabled() && !self.SetAngleTo then return end
  560.    if GetViewEntity() != LocalPlayer() then return end
  561.    if self:Setting("debug") then return end
  562.    
  563.    local base = GAMEMODE:CalcView(ply, origin, self.SetAngleTo or self.View, FOV) or {}
  564.            base.angles = base.angles or (self.AngleTo or self.View)
  565.            base.angles.r = 0 // No crappy screen tilting in ZS.
  566.    return base
  567. end
  568. AA:AddHook("CalcView", "FakeView")
  569.  
  570.  
  571. function AA:TargetPrediction(ply, target, targetPos)
  572.    local weap = PlyM["GetActiveWeapon"](ply)
  573.    if ValidEntity(weap) then
  574.        local class = EntM["GetClass"](weap)
  575.        if class == "weapon_crossbow" then
  576.            local dist = VecM["Length"](targetPos - PlyM["GetShootPos"](ply))
  577.            local time = (dist / 3500) + 0.05 // About crossbow bolt speed.
  578.            targetPos = targetPos + (EntM["GetVelocity"](target) * time)
  579.        end
  580.        
  581.        local mul = 0.0075
  582.        //targetPos = targetPos - (e["GetVelocity"](ply) * mul)
  583.    end
  584.    
  585.    return targetPos
  586. end
  587. AA:AddHook("TargetPrediction", "TargetPrediction")
  588.  
  589. // ##################################################
  590. // Aim
  591. // ##################################################
  592.  
  593. function AA:SetAngle(ang)
  594.    self.SetAngleTo = ang
  595. end
  596.  
  597. AA:CreateSetting("smoothspeed", "Smooth aim speed (0 to disable)", 120, {Min = 0, Max = 360})
  598. AA:CreateSetting("snaponfire", "Snap on fire", true)
  599. AA:CreateSetting("snapgrace", "Snap on fire grace", 0.5, {Min = 0, Max = 3, Places = 1})
  600. AA.LastAttack = 0
  601. function AA:SetAimAngles(cmd)
  602.    self:RotateView(cmd)
  603.  
  604.    if !self:Enabled() && !self.SetAngleTo then return end
  605.  
  606.    local ply = LocalPlayer()
  607.    if !ValidEntity(ply) then return end
  608.  
  609.    // We're aiming with the view, normally.
  610.    local targetAim = self:GetView()
  611.  
  612.    // If we have a target, aim at them!
  613.    local target = self:GetTarget()
  614.    if target then
  615.        local targetPos = self:TargetPosition(target)
  616.        targetAim = VecM["Angle"](targetPos - ply:GetShootPos())
  617.    end
  618.  
  619.    // We're following the view, until we fire.
  620.    if self:Setting("snaponfire") then
  621.        local time = CurTime()
  622.        if PlyM["KeyDown"](ply, IN_ATTACK) || PlyM["KeyDown"](ply, IN_ATTACK2) || self:Setting("autoshoot") != 0 then
  623.            self.LastAttack = time
  624.        end
  625.        if CurTime() - self.LastAttack > self:Setting("snapgrace") then
  626.            targetAim = self:GetView()
  627.        end
  628.    end
  629.  
  630.    // We want to change to whatever was SetAngle'd.
  631.    if self.SetAngleTo then
  632.        targetAim = self.SetAngleTo
  633.    end
  634.  
  635.    // Smooth aiming.
  636.    local smooth = self:Setting("smoothspeed")
  637.    if smooth > 0 then
  638.        local current = CmdM["GetViewAngles"](cmd)
  639.  
  640.        // Approach the target angle.
  641.        current = self:ApproachAngle(current, targetAim, smooth * FrameTime())
  642.        current.r = 0
  643.  
  644.        // If we're just following the view, we don't need to smooth it.
  645.        if self.RevertingAim then
  646.            local diff = self:NormalizeAngle(current - self:GetView())
  647.            if math.abs(diff.p) < 1 && math.abs(diff.y) < 1 then self.RevertingAim = false end
  648.        elseif targetAim == self:GetView() then
  649.            current = targetAim
  650.        end
  651.  
  652.        // Check if the angles are the same...
  653.        if self.SetAngleTo then
  654.            local diff = self:NormalizeAngle(current - self.SetAngleTo)
  655.            if math.abs(diff.p) < 1 && math.abs(diff.y) < 1 then self.SetAngleTo = nil end
  656.        end
  657.  
  658.        aim = current
  659.    else
  660.        aim = targetAim
  661.        self.SetAngleTo = nil
  662.    end
  663.  
  664.    // Set the angles.
  665.    CmdM["SetViewAngles"](cmd, aim)
  666.    local sensitivity = 0.22
  667.    local diff = aim - CmdM["GetViewAngles"](cmd)
  668.    CmdM["SetMouseX"](cmd, diff.y / sensitivity)
  669.    CmdM["SetMouseY"](cmd, diff.p / sensitivity)
  670.    
  671.  
  672.    // Change the players movement to be relative to their view instead of their aim.
  673.    local move = Vector(CmdM["GetForwardMove"](cmd), CmdM["GetSideMove"](cmd), 0)
  674.    local norm = VecM["GetNormal"](move)
  675.    local set = AngM["Forward"](VecM["Angle"](norm) + (aim - self:GetView())) * VecM["Length"](move)
  676.        CmdM["SetForwardMove"](cmd, set.x)
  677.        CmdM["SetSideMove"](cmd, set.y)
  678. end
  679. AA:AddHook("CreateMove", "SetAimAngles")
  680.  
  681. function AA:RevertAim()
  682.    self.RevertingAim = true
  683. end
  684. AA:AddHook("TargetLost", "RevertAim")
  685. function AA:StopRevertAim()
  686.    self.RevertingAim = false
  687. end
  688. AA:AddHook("TargetGained", "RevertAim")
  689.  
  690. // When we turn off the bot, we want our aim to go back to our view.
  691. function AA:ViewToAim()
  692.    if self:Enabled() then return end
  693.    self:SetAngle(self:GetView())
  694. end
  695. AA:AddHook("OnToggled", "ViewToAim")
  696.  
  697.  
  698. // ##################################################
  699. // HUD
  700. // ##################################################
  701.  
  702. AA:CreateSetting("crosshair", "Crosshair size (0 to disable)", 18, {Min = 0, Max = 20})
  703. function AA:DrawTarget()
  704.    if !self:Enabled() then return end
  705.  
  706.    local target = self:GetTarget()
  707.    if !target then return end
  708.  
  709.    local size = self:Setting("crosshair")
  710.    if size <= 0 then return end
  711.  
  712.    // Change colour on the block status.
  713.    local blocked, aimOff = self:TargetBlocked()
  714.    if blocked then
  715.        surface.SetDrawColor(255, 0, 0, 255) // Red.
  716.    elseif aimOff then
  717.        surface.SetDrawColor(255, 255, 0, 255) // Yellow.
  718.    else
  719.        surface.SetDrawColor(0, 255, 0, 255) // Green.
  720.    end
  721.  
  722.    // Get the onscreen coordinates for the target.
  723.    local pos = self:TargetPosition(target)
  724.  
  725.    local screen = VecM["ToScreen"](pos)
  726.    local x, y = screen.x, screen.y
  727.  
  728.    // Work out sizes.
  729.    local a, b = size / 2, size / 6
  730.  
  731.    // Top left.
  732.    surface.DrawLine(x - a, y - a, x - b, y - a)
  733.    surface.DrawLine(x - a, y - a, x - a, y - b)
  734.  
  735.    // Bottom right.
  736.    surface.DrawLine(x + a, y + a, x + b, y + a)
  737.    surface.DrawLine(x + a, y + a, x + a, y + b)
  738.  
  739.    // Top right.
  740.    surface.DrawLine(x + a, y - a, x + b, y - a)
  741.    surface.DrawLine(x + a, y - a, x + a, y - b)
  742.  
  743.    // Bottom left.
  744.    surface.DrawLine(x - a, y + a, x - b, y + a)
  745.    surface.DrawLine(x - a, y + a, x - a, y + b)
  746. end
  747. AA:AddHook("HUDPaint", "DrawTarget")
  748.  
  749.  
  750. AA.ScreenMaxAngle = {
  751.    Length = 0,
  752.    FOV = 0,
  753.    MaxAngle = 0
  754. }
  755. AA:CreateSetting("draw_maxangle", "Draw Max Angle", true)
  756. function AA:DrawMaxAngle()
  757.    if !self:Enabled() then return end
  758.  
  759.    // Check that we want to be drawing this...
  760.    local show = AA:Setting("draw_maxangle")
  761.    if !show then return end
  762.  
  763.    // We need a player for this to work...
  764.    local ply = LocalPlayer()
  765.    if !ValidEntity(ply) then return end
  766.  
  767.    local info = self.ScreenMaxAngle
  768.    local maxang = AA:Setting("maxangle")
  769.    
  770.    local fov = PlyM["GetFOV"](ply)
  771.    if GetViewEntity() == ply && (maxang != info.MaxAngle || fov != info.FOV) then
  772.        local view = self:GetView()
  773.            view.p = view.p + maxang
  774.  
  775.        local screen = (PlyM["GetShootPos"](ply) + (AngM["Forward"](view) * 100))
  776.        screen = VecM["ToScreen"](screen)
  777.  
  778.        info.Length = math.abs((ScrH() / 2) - screen.y)
  779.  
  780.        info.MaxAngle = maxang
  781.        info.FOV = fov
  782.    end
  783.  
  784.    local length = info.Length
  785.  
  786.    local cx, cy = ScrW() / 2, ScrH() / 2
  787.    for x = -1, 1 do
  788.        for y = -1, 1 do
  789.            if x != 0 || y != 0 then
  790.                local add = VecM["GetNormal"](Vector(x, y, 0)) * length
  791.                surface.SetDrawColor(0, 0, 0, 255)
  792.                surface.DrawRect((cx + add.x) - 2, (cy + add.y) - 2, 5, 5)
  793.                surface.SetDrawColor(255, 255, 255, 255)
  794.                surface.DrawRect((cx + add.x) - 1, (cy + add.y) - 1, 3, 3)
  795.            end
  796.        end
  797.    end
  798.  
  799. end
  800. AA:AddHook("HUDPaint", "DrawMaxAngle")
  801.  
  802. // ##################################################
  803. // Auto-shoot
  804. // ##################################################
  805.  
  806. AA.AttackDown = false
  807. function AA:SetShooting(bool)
  808.    if self.AttackDown == bool then return end
  809.    self.AttackDown = bool
  810.  
  811.    local pre = {[true] = "+", [false] = "-"}
  812.    RunConsoleCommand(pre[bool] .. "attack")
  813. end
  814.  
  815. AA.NextShot = 0
  816. AA:CreateSetting("autoshoot", "Max auto-shoot distance (0 to disable)", 0, {Min = 0, Max = 16384})
  817. function AA:Shoot()
  818.    if !self:Enabled() then
  819.        self:SetShooting(false)
  820.        return
  821.    end
  822.  
  823.    // Get the maximum distance.
  824.    local maxDist = self:Setting("autoshoot")
  825.    if maxDist == 0 then return end
  826.  
  827.    // Check we've got something to shoot at...
  828.    local target = self:GetTarget()
  829.    if !target then return end
  830.    
  831.    // Don't shoot until we can hit, you idiot!
  832.    local blocked, wrongAim = self:TargetBlocked(target)
  833.    if blocked || wrongAim then return end
  834.  
  835.    // We're gonna need the player object in a second.
  836.    local ply = LocalPlayer()
  837.    if !ValidEntity(ply) then return end
  838.    
  839.    // Check we're within our maximum distance.
  840.    local targetPos = self:TargetPosition(target)
  841.    local distance = VecM["Length"](targetPos - ply:GetShootPos())
  842.    if distance > maxDist && maxDist != -1 then return end
  843.  
  844.    // Check if it's time to shoot yet.
  845.    if CurTime() < self.NextShot then return end
  846.  
  847.    // Check we got our weapon.
  848.    local weap = PlyM["GetActiveWeapon"](ply)
  849.    if !ValidEntity(weap) then return end
  850.  
  851.    // Shoot!
  852.    self:SetShooting(true)
  853.    // If we're semi-auto, we want to stop holding down fire.
  854.    if self:IsSemiAuto(weap) then
  855.        timer.Simple(0.05, function() self:SetShooting(false) end)
  856.    end
  857.  
  858.    // Set the next time to shoot.
  859.    self.NextShot = CurTime() + 0.1
  860. end
  861. AA:AddHook("Think", "Shoot")
  862.  
  863. // When we lose our target we stop shooting.
  864. function AA:StopShooting()
  865.    self:SetShooting(false)
  866. end
  867. AA:AddHook("TargetLost", "StopShooting")
  868.  
  869. // ##################################################
  870. // Toggle
  871. // ##################################################
  872.  
  873. AA.IsEnabled = false
  874. function AA:Enabled() return self.IsEnabled end
  875.  
  876. function AA:SetEnabled(bool)
  877.    if self.IsEnabled == bool then return end
  878.    self.IsEnabled = bool
  879.  
  880.    local message = {[true] = "ON", [false] = "OFF"}
  881.    print("AutoAim " .. message[self.IsEnabled])
  882.  
  883.    local e = {[true] = "1", [false] = "0"}
  884.    RunConsoleCommand("aa_enabled", e[self.IsEnabled])
  885.  
  886.    self:CallHook("OnToggled")
  887. end
  888.  
  889. function AA:Toggle()
  890.    self:SetEnabled(!self:Enabled())
  891. end
  892. concommand.Add("aa_toggle", function() AA:Toggle() end)
  893.  
  894. AA:CreateSetting("enabled", "Enabled", false, {Save = false})
  895. function AA:ConVarEnabled(_, old, val)
  896.    if old == val then return end
  897.    val = tonumber(val) or 0
  898.    self:SetEnabled(val > 0)
  899. end
  900. AA:AddCVarHook("aa_enabled", "ConVarEnabled")
  901.  
  902. concommand.Add("+aa", function() AA:SetEnabled(true) end)
  903. concommand.Add("-aa", function() AA:SetEnabled(false) end)
  904.  
  905. // ##################################################
  906. // Menu
  907. // ##################################################
  908.  
  909. function AA:OpenMenu()
  910.    local w, h = ScrW() / 3, ScrH() / 2
  911.  
  912.    local menu = vgui.Create("DFrame")
  913.    menu:SetTitle("AutoAim")
  914.    menu:SetSize(w, h)
  915.    menu:Center()
  916.    menu:MakePopup()
  917.  
  918.    local scroll = vgui.Create("DPanelList", menu)
  919.    scroll:SetPos(5, 25)
  920.    scroll:SetSize(w - 10, h - 30)
  921.    scroll:EnableVerticalScrollbar()
  922.  
  923.    local form = vgui.Create("DForm", menu)
  924.    form:SetName("")
  925.    form.Paint = function() end
  926.    scroll:AddItem(form)
  927.  
  928.    self:BuildMenu(form)
  929.  
  930.    if AA.Menu then AA.Menu:Remove() end
  931.    AA.Menu = menu
  932. end
  933. concommand.Add("aa_menu", function() AA:OpenMenu() end)
  934.  
  935. function AA:RegisterMenu()
  936.    spawnmenu.AddToolMenuOption("Options", "Hacks", "AutoAim", "AutoAim", "", "", function(p) self:BuildMenu(p) end)
  937. end
  938. AA:AddHook("PopulateToolMenu", "RegisterMenu")
  939.  
  940. // ##################################################
  941. // Useful functions
  942. // ##################################################
  943.  
  944. function AA:AngleBetween(a, b)
  945.    return math.deg(math.acos(VecM["Dot"](a, b)))
  946. end
  947.  
  948. function AA:NormalizeAngle(ang)
  949.    return Angle(math.NormalizeAngle(ang.p), math.NormalizeAngle(ang.y), math.NormalizeAngle(ang.r))
  950. end
  951.  
  952. function AA:ApproachAngle(start, target, add)
  953.    local diff = self:NormalizeAngle(target - start)
  954.  
  955.    local vec = Vector(diff.p, diff.y, diff.r)
  956.    local len = VecM["Length"](vec)
  957.    vec = VecM["GetNormal"](vec) * math.min(add, len)
  958.  
  959.    return start + Angle(vec.x, vec.y, vec.z)
  960. end
  961.  
  962. local notAuto = {"weapon_pistol", "weapon_rpg", "weapon_357", "weapon_crossbow"}
  963. function AA:IsSemiAuto(weap)
  964.    if !ValidEntity(weap) then return end
  965.    return (weap.Primary && !weap.Primary.Automatic) || table.HasValue(notAuto, EntM["GetClass"](weap))
  966. end
Add Comment
Please, Sign In to add comment