Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.18 KB | None | 0 0
  1. #pragma once
  2.  
  3. class AimAssist
  4. {
  5. public:
  6.     AimAssist()
  7.     {
  8.         m_pSignatureHelper = Container::Instance().Resolve<SignatureHelper>();
  9.         m_bIsAttacking = false;
  10.         m_pAimLockedTarget = nullptr;
  11.     }
  12.  
  13.     void CreateMove_Post(C_CSPlayer* pLocal, CUserCmd* pCmd)
  14.     {
  15.         m_bIsAttacking = pCmd->buttons & IN_ATTACK;
  16.     }
  17.  
  18.     void OverrideMouseInput_Post(float* x, float* y)
  19.     {
  20.         if (Options::g_iAimAssistType == 0)
  21.             return;
  22.  
  23.         auto pLocal = C_CSPlayer::GetLocalPlayer();
  24.         auto pWeapon = pLocal->GetActiveWeapon();
  25.         if (!IsValidData(pLocal, pWeapon))
  26.         {
  27.             m_pAimLockedTarget = nullptr;
  28.             return;
  29.         }
  30.  
  31.         if (m_pAimLockedTarget != nullptr && !m_pAimLockedTarget->IsAlive())
  32.             m_pAimLockedTarget = nullptr;
  33.  
  34.         Vector qDelta;
  35.         if (!UpdateTarget(pLocal, pWeapon, qDelta))
  36.             return;
  37.  
  38.         // Adjust cursor position (0.022f = no smoothing)
  39.         const auto xSmooth = pWeapon->IsPistol() ? Options::g_fAimAssistSmoothPistol : Options::g_fAimAssistSmooth;
  40.         const auto ySmooth = xSmooth / Options::g_fAimAssistVerticalSmoothMultiplier;
  41.         qDelta.x /= xSmooth >= 0.022f ? xSmooth : 0.022f;
  42.         qDelta.y /= ySmooth >= 0.022f ? ySmooth : 0.022f;
  43.  
  44.         auto vMouse = Options::g_bAimAssistLockMouse && Options::g_iAimAssistType != 2
  45.             ? Vector(0, 0, 0)
  46.             : Vector(*static_cast<float*>(x), *static_cast<float*>(y), 0.0f);
  47.  
  48.         auto vDelta = Vector(qDelta.y, -qDelta.x, 0.0f);
  49.  
  50.         // Slow down if going wrong, accelerate if going right
  51.         // 'Hacky' acceleration should be greatly reduced
  52.         if (Options::g_iAimAssistType == 2)
  53.         {
  54.             const auto boost = 1 + Options::g_fAimAssistType2DirectionBoost / 100.f;
  55.  
  56.             if (vMouse.x > 0 && vDelta.x < 0 || vMouse.x < 0 && vDelta.x > 0)
  57.                 vMouse.x *= 1 - Options::g_fAimAssistType2HorizontalPenalty / 100.f;
  58.             else
  59.                 vMouse.x *= boost;
  60.  
  61.             if (vMouse.y > 0 && vDelta.y < 0 || vMouse.y < 0 && vDelta.y > 0)
  62.                 vMouse.y *= 1 - Options::g_fAimAssistType2VerticalPenalty / 100.f;
  63.             else
  64.                 vMouse.y *= boost;
  65.  
  66.             vDelta *= Options::g_fAimAssistType2AcceleratePercentage / 100.f;
  67.         }
  68.  
  69.         vDelta.x = (vDelta.x + vMouse.x) / 2.0f;
  70.         vDelta.y = (vDelta.y + vMouse.y) / 2.0f;
  71.         *static_cast<float*>(x) = vDelta.x;
  72.         *static_cast<float*>(y) = vDelta.y;
  73.     }
  74. private:
  75.     std::shared_ptr<SignatureHelper> m_pSignatureHelper;
  76.     bool m_bIsAttacking;
  77.     C_CSPlayer* m_pAimLockedTarget;
  78.  
  79.     bool IsValidData(C_CSPlayer* pLocal, C_BaseCombatWeapon* pWeapon) const
  80.     {
  81.         if (!pLocal || !pLocal->IsAlive())
  82.             return false;
  83.  
  84.         if (!pWeapon || pWeapon->IsKnife() || pWeapon->IsGrenade() || pWeapon->IsC4())
  85.             return false;
  86.  
  87.         if (!m_bIsAttacking && !IsTriggerEnabled(pLocal, pWeapon))
  88.         {
  89.             if (Options::g_iAimAssistType != 2 && !Options::g_bAimAssistAutoShoot)
  90.                 return false;
  91.  
  92.             if (Options::g_iAimAssistType == 2 && (!Options::g_bAimAssistType2SniperAlwaysActive || !pLocal->IsScoped() || !pWeapon->IsSniper()))
  93.                 return false;
  94.         }
  95.  
  96.         static auto emptyClip = false;
  97.         const auto nextAttackTime = pWeapon->NextPrimaryAttack() - Interfaces::GlobalVars()->curtime;
  98.         if (nextAttackTime >= 1.f || pWeapon->GetClip() == 0) emptyClip = true;
  99.         else if (nextAttackTime <= 0.1f) emptyClip = false;
  100.  
  101.         const auto shouldBeScoped = Options::g_bAimAssistSniperScopedOnly && pWeapon->IsSniper();
  102.         if (emptyClip || shouldBeScoped && !pLocal->IsScoped())
  103.             return false;
  104.  
  105.         return true;
  106.     }
  107.  
  108.     bool UpdateTarget(C_CSPlayer* pLocal, C_BaseCombatWeapon* pWeapon, Vector& qDelta)
  109.     {
  110.         auto bone = HEAD_0;
  111.         auto fov = pWeapon->IsPistol()
  112.             ? Options::g_fAimAssistFovPistol
  113.             : Options::g_fAimAssistFov;
  114.  
  115.         // Decelerate aim requires much higher FOV to be legit
  116.         if (Options::g_iAimAssistType == 2)
  117.             fov *= 1 + Options::g_fAimAssistType2FovBoost / 100.f;
  118.  
  119.         while (true)
  120.         {
  121.             const auto pTarget = m_pAimLockedTarget == nullptr
  122.                 ? GetClosestPlayer(pLocal, fov, bone)
  123.                 : Options::g_iAimAssistLockTarget == 1
  124.                 ? GetClosestPlayer(pLocal, fov * 1.25f, bone)
  125.                 : Options::g_iAimAssistLockTarget == 2
  126.                 ? GetClosestPlayer(pLocal, fov * 1.5f, bone)
  127.                 : Options::g_iAimAssistLockTarget == 3
  128.                 ? GetClosestPlayer(pLocal, fov * 2.f, bone)
  129.                 : m_pAimLockedTarget;
  130.  
  131.             if (pTarget && TraceBone(pLocal, pTarget, bone))
  132.             {
  133.                 qDelta = GetDelta(pLocal, pTarget, bone);
  134.                 if (Options::g_iAimAssistLockTarget > 0)
  135.                     m_pAimLockedTarget = pTarget;
  136.  
  137.                 break;
  138.             }
  139.  
  140.             if (bone == HEAD_0) bone = NECK_0;
  141.             else if (bone == NECK_0) bone = SPINE_3;
  142.             else if (bone == SPINE_3) bone = SPINE_2;
  143.             else if (bone == SPINE_2) bone = SPINE_1;
  144.             else if (bone == SPINE_1) bone = SPINE_0;
  145.             else
  146.             {
  147.                 m_pAimLockedTarget = nullptr;
  148.                 return false;
  149.             }
  150.         }
  151.         return true;
  152.     }
  153.  
  154.     // Returns true if trigger is enabled and all requirements are met
  155.     bool IsTriggerEnabled(C_CSPlayer* pLocal, C_BaseCombatWeapon* pWeapon) const
  156.     {
  157.         const auto shouldBeScoped = Options::g_bTriggerSniperScopedOnly && pWeapon->IsSniper();
  158.         if (shouldBeScoped && !pLocal->IsScoped())
  159.             return false;
  160.  
  161.         return Options::g_bTriggerEnabled && Options::g_bTriggerAimSynergy && (GetAsyncKeyState(Options::KeysID[Options::g_iTriggerKey]) || Options::g_bTriggerAlwaysActive);
  162.     }
  163.  
  164.     bool TraceBone(C_CSPlayer* pLocal, C_CSPlayer* pTarget, const ECSPlayerBones bone) const
  165.     {
  166.         if (Options::g_bAimAssistIgnoreObstacles)
  167.             return true;
  168.  
  169.         trace_t trace;
  170.         Ray_t ray;
  171.         CTraceFilter filter;
  172.         auto vTraceForward = Vector(0, 0, 0);
  173.         const auto vEyePos = pLocal->GetEyePos();
  174.         const auto vAimBone = Utils::GetEntityBone(pTarget, bone);
  175.         const auto qAimAngles = Utils::CalcAngle(vEyePos, vAimBone);
  176.         Utils::AngleVectors(qAimAngles, &vTraceForward);
  177.         ray.Init(vEyePos, vEyePos + vTraceForward * 8192.0f);
  178.         filter.pSkip = pLocal;
  179.         Interfaces::EngineTrace()->TraceRay(ray, MASK_SHOT_HULL | CONTENTS_HITBOX, &filter, &trace);
  180.         if (trace.fraction == 1.f || !trace.m_pEnt)
  181.             return false;
  182.  
  183.         const auto vTraceEndPos = trace.endpos;
  184.         if (!Options::g_bAimAssistIgnoreSmoke && m_pSignatureHelper->LineThroughSmoke(vEyePos, vTraceEndPos))
  185.             return false;
  186.  
  187.         auto target = static_cast<C_CSPlayer*>(trace.m_pEnt);
  188.         if (!target || !target->IsAlive() || target->GetHealth() < 1 || target->IsImmune() || target->IsDormant())
  189.             return false;
  190.  
  191.         if (target->GetTeamNum() == pLocal->GetTeamNum() && !Options::g_bDeathmatch)
  192.             return false;
  193.  
  194.         return true;
  195.     }
  196.  
  197.     C_CSPlayer* GetClosestPlayer(C_CSPlayer* pLocal, float fov, ECSPlayerBones bone) const
  198.     {
  199.         C_CSPlayer* pTarget = nullptr;
  200.         auto maxFov = fov;
  201.  
  202.         QAngle aimAngles;
  203.         Interfaces::Engine()->GetViewAngles(aimAngles);
  204.         aimAngles += GetAimPunchCorrection(pLocal);
  205.  
  206.         const auto eyePos = pLocal->GetEyePos();
  207.  
  208.         for (auto i = 1; i <= Interfaces::Engine()->GetMaxClients(); i++)
  209.         {
  210.             const auto pPotentialTarget = static_cast<C_CSPlayer*>(Interfaces::EntityList()->GetClientEntity(i));
  211.            
  212.             if (!IsTargetValid(pLocal, pPotentialTarget))
  213.                 continue;
  214.  
  215.             if (!IsTargetBetter(pPotentialTarget, aimAngles, eyePos, maxFov, bone))
  216.                 continue;
  217.  
  218.             pTarget = pPotentialTarget;
  219.         }
  220.         return pTarget;
  221.     }
  222.  
  223.     bool IsTargetValid(C_CSPlayer* pLocal, C_CSPlayer* pTarget) const
  224.     {
  225.         if (!pTarget || pTarget == pLocal)
  226.             return false;
  227.  
  228.         if (!pTarget->IsAlive() || pTarget->IsDormant() || pTarget->IsImmune())
  229.             return false;
  230.  
  231.         if (pTarget->GetTeamNum() == pLocal->GetTeamNum() && !Options::g_bDeathmatch)
  232.             return false;
  233.  
  234.         return true;
  235.     }
  236.  
  237.     bool IsTargetBetter(C_CSPlayer* pPotentialTarget, Vector aimAngles, Vector eyePos, float& maxFov, ECSPlayerBones bone) const
  238.     {
  239.         const auto vTargetBone = Utils::GetEntityBone(pPotentialTarget, bone);
  240.         const auto fTargetFov = Options::g_bAimAssistDistanceBasedFov
  241.             ? GetFovFraction(aimAngles, Utils::CalcAngle(eyePos, vTargetBone))
  242.             : GetFov(aimAngles, Utils::CalcAngle(eyePos, vTargetBone));
  243.  
  244.         if (fTargetFov > maxFov)
  245.             return false;
  246.  
  247.         maxFov = fTargetFov;
  248.         return true;
  249.     }
  250.  
  251.     Vector GetAimPunchCorrection(C_CSPlayer* pLocal) const
  252.     {
  253.         if (Options::g_bRCSEnabled)
  254.             return Vector(0, 0, 0);
  255.  
  256.         auto aimPunchCorrection = *pLocal->AimPunch() * 2.f;
  257.         if (Options::g_fAimAssistRCSFailureChance < rand() % 100)
  258.             return aimPunchCorrection;
  259.  
  260.         const auto failureAmount = rand() % 2 == 1
  261.             ? 1 + Options::g_fAimAssistRCSFailureAmount / 100.f
  262.             : 1 - Options::g_fAimAssistRCSFailureAmount / 100.f;
  263.  
  264.         return aimPunchCorrection * failureAmount;
  265.     }
  266.  
  267.     Vector GetDelta(C_CSPlayer* pLocal, C_CSPlayer* pTarget, ECSPlayerBones bone) const
  268.     {
  269.         QAngle qViewAngles;
  270.         Interfaces::Engine()->GetViewAngles(qViewAngles);
  271.         if (!Options::g_bRCSEnabled)
  272.             qViewAngles += *pLocal->AimPunch() * 2.0f;
  273.         Utils::Clamp(qViewAngles);
  274.         const auto vEyePos = pLocal->GetEyePos();
  275.         const auto vAimBone = Utils::GetEntityBone(pTarget, bone);
  276.         const auto qAimAngles = Utils::CalcAngle(vEyePos, vAimBone);
  277.         auto qDelta = qViewAngles - qAimAngles;
  278.         Utils::Clamp(qDelta);
  279.         return qDelta;
  280.     }
  281.  
  282.     float GetFov(const QAngle& viewAngle, const QAngle& aimAngle) const
  283.     {
  284.         auto delta = aimAngle - viewAngle;
  285.         Utils::Clamp(delta);
  286.         return sqrtf(powf(delta.x, 2.f) + powf(delta.y, 2.f));
  287.     }
  288.  
  289.     float GetFovFraction(const QAngle& viewAngle, const QAngle& aimAngle) const
  290.     {
  291.         auto delta = aimAngle - viewAngle;
  292.         Utils::Clamp(delta);
  293.         return sin(DEG2RAD(delta.Length()) / 2.f) * 180.f;
  294.     }
  295. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement