Advertisement
Wolfrost

Vitruvia First legitbot code

Jun 27th, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include "Legit.h"
  3.  
  4. VITRUVIA_INIT
  5.  
  6. F_Legit* Legit;
  7.  
  8. using sorter_function = std::function<bool(const Target_t& a, const Target_t& b)>;
  9. std::array<sorter_function, 4> sorters
  10. {
  11.     // Health
  12.     [](const Target_t& a, const Target_t& b) -> bool   
  13.     {
  14.         return a.GetHealth() > b.GetHealth();
  15.     },
  16.     // Distance
  17.     [](const Target_t& a, const Target_t& b) -> bool
  18.     {
  19.         return a.GetDistance() > b.GetDistance();
  20.     },
  21.     // FOV
  22.     [](const Target_t& a, const Target_t& b) -> bool
  23.     {
  24.         return a.GetCrosshairDistance() > b.GetCrosshairDistance();
  25.     },
  26.     // Threat
  27.     [](const Target_t& a, const Target_t& b) -> bool
  28.     {
  29.         return a.GetThreat() > b.GetThreat();
  30.     }
  31. };
  32.  
  33. using shooter_function = std::function<bool(const Source::CUserCmd*, int)>;
  34. std::array<shooter_function, 3> shooters
  35. {
  36.     // On Fire
  37.     [](const Source::CUserCmd* cmd, int) -> bool
  38.     {
  39.         return cmd->buttons & IN_ATTACK;
  40.     },
  41.     // On Key
  42.     [](const Source::CUserCmd*, int button) -> bool
  43.     {
  44.         return Source::InputSystem->IsButtonDown((Source::ButtonCode_t)button);
  45.     },
  46.     // On Sight
  47.     [](const Source::CUserCmd*, int) -> bool
  48.     {
  49.         return true;
  50.     }
  51. };
  52.  
  53. bool F_Legit::FindTarget()
  54. {
  55.     using namespace Source;
  56.  
  57.     // The objective of this function is to fill m_TargetList, sort it, and grab the best target to aim at.
  58.     auto local = C_CSPlayer::GetLocal();
  59.     // If the local player has got an invalid weapon in hands, return prematurely
  60.     if (local->GotInvalidWeapon())
  61.         return false;
  62.  
  63.     // Used to track down entities I'm pushing in the targets array
  64.     int target_index = 0;
  65.  
  66.     // Retrieve the config table to use based on the local active weapon if the user wants weapon groups
  67.     Config::cLegit::cAimbot cfg = Config::Aimbot;
  68.  
  69.     // If the user doesn't want to use weapon groups, the cfg will stay the same
  70.     if (Config::Aimbot.UseGroups.m_Value)
  71.     {
  72.         // If he wants, I edit the cfg using the weapon cfg
  73.         auto cfg_weap = Config::WeaponGroups[local->GetActiveWeapon()->GetWeaponInfo()->iWeaponType];
  74.         cfg.Active = cfg_weap->Active;
  75.         cfg.Hitbox = cfg_weap->Hitbox;
  76.         cfg.FOV = cfg_weap->FOV;
  77.         cfg.Smooth = cfg_weap->Smooth;
  78.         cfg.Time = cfg_weap->Time;
  79.     }
  80.  
  81.     if (!cfg.Active.m_Value)
  82.         return false;
  83.        
  84.     // Loop through the clients
  85.     FOR_EACH_CLIENT
  86.     {
  87.         // Grab the entity and perform several validity checks on it
  88.         auto entity = GET_ENTITY(ClientIndex);
  89.  
  90.         // If the entity is not found skip
  91.         if (entity == nullptr)
  92.             continue;
  93.         // If the entity is the local player, skip
  94.         if (entity == local)
  95.             continue;
  96.         // If the entity is dormant, skip
  97.         if (entity->GetNetworkable()->IsDormant())
  98.             continue;
  99.         // If the entity is dead, skip
  100.         if (entity->m_iHealth() <= 0)
  101.             continue;
  102.         // If the entity has got spawn immunity, skip
  103.         if (entity->m_bGunGameImmunity())
  104.             continue;
  105.  
  106.         // If config needs to skip team mates
  107.         if (!cfg.TeamMates.m_Value)
  108.         {
  109.             // If the entity is a team mate, skip
  110.             if (entity->m_iTeamNum() == local->m_iTeamNum())
  111.                 continue;
  112.         }
  113.  
  114.         // If config needs to skip targets behind a smoke
  115.         if (cfg.SmokeCheck.m_Value)
  116.         {
  117.             // If the entity is behind a smoke, skip
  118.             if (entity->IsBehindSmoke(cfg.Hitbox.m_Value))
  119.                 continue;
  120.         }
  121.  
  122.         // If config needs to skip targets that aren't visible
  123.         if (cfg.VisibleCheck.m_Value)
  124.         {
  125.             // If entity isn't visible, skip
  126.             if (!entity->IsVisible(cfg.Hitbox.m_Value))
  127.                 continue;
  128.         }
  129.  
  130.         // Get the hitbox position
  131.         Vector hitbox_position = entity->GetHitboxPosition(cfg.Hitbox.m_Value);
  132.         if (hitbox_position == Vector(0, 0, 0)) continue;
  133.  
  134.         // Check for FOV
  135.         if (cfg.FOV.m_Value > 0.f)
  136.         {
  137.             if (Maths::GetFov(m_pCmd->viewangles, local->GetEyePosition(), hitbox_position) > cfg.FOV.m_Value)
  138.                 continue;
  139.         }
  140.  
  141.         // Calculate the angle I need to aim at if this is the best entity and normalize it
  142.         Vector aim_angle = Maths::GetAngle(local->GetEyePosition(), hitbox_position);
  143.         aim_angle = Maths::NormalizeAngle(aim_angle);
  144.  
  145.         // Correct the recoil based on RCS amount
  146.         aim_angle -= local->m_aimPunchAngle() * cfg.RCSFactor.m_Value;
  147.         aim_angle = Maths::NormalizeAngle(aim_angle);
  148.  
  149.         // Add a new target to the list
  150.         m_TargetList[target_index] = Target_t(local, entity, aim_angle, hitbox_position);
  151.         target_index++;
  152.     }
  153.  
  154.     // If I've actually found entities good enough to aim at, I can start the sorting
  155.     if (target_index > 0)
  156.     {
  157.         // Sort in C++11 style based on the SortBy cfg value
  158.         std::sort(m_TargetList.begin(), m_TargetList.end(), sorters[cfg.SortBy.m_Value]);
  159.         // Now the value at the beginning of the list contains the actual best entity
  160.         m_BestTarget = m_TargetList[0];
  161.         return true;
  162.     }
  163.  
  164.     return false;
  165. }
  166.  
  167. void F_Legit::AimTarget()
  168. {
  169.     using namespace Source;
  170.  
  171.     // If I'm calling this function it means that FindTarget() returned true, which means that m_pBestTarget is not null
  172.     // Now all I need to do is to aim at him, always based on the config
  173.     auto local = C_CSPlayer::GetLocal();
  174.  
  175.     // Retrieve the config table to use based on the local active weapon if the user wants weapon groups
  176.     Config::cLegit::cAimbot cfg = Config::Aimbot;
  177.  
  178.     // If the user doesn't want to use weapon groups, the cfg will stay the same
  179.     if (Config::Aimbot.UseGroups.m_Value)
  180.     {
  181.         // If he wants, I edit the cfg using the weapon cfg
  182.         auto cfg_weap = Config::WeaponGroups[local->GetActiveWeapon()->GetWeaponInfo()->iWeaponType];
  183.         cfg.Active = cfg_weap->Active;
  184.         cfg.Hitbox = cfg_weap->Hitbox;
  185.         cfg.FOV = cfg_weap->FOV;
  186.         cfg.Smooth = cfg_weap->Smooth;
  187.         cfg.Time = cfg_weap->Time;
  188.     }
  189.  
  190.     // Different behaviour based on the value of cfg.Mode
  191.     if (shooters[cfg.Mode.m_Value](m_pCmd, cfg.AimKey.m_Value))
  192.     {
  193.         Vector best_angle = m_BestTarget.GetAimbotAngle();
  194.  
  195.         // If the user wants to apply smooth
  196.         if (cfg.Smooth.m_Value > 0.f)
  197.         {
  198.             // I smooth
  199.             Maths::SmoothAngles(m_pCmd->viewangles, best_angle, cfg.Smooth.m_Value);
  200.         }
  201.  
  202.         // Normalize the angle
  203.         best_angle = Maths::NormalizeAngle(best_angle);
  204.        
  205.         // Apply the angles
  206.         (cfg.Silent.m_Value) ?
  207.             m_pCmd->viewangles = best_angle :
  208.             Source::Engine->SetViewAngles(best_angle);
  209.     }
  210. }
  211.  
  212. void F_Legit::OnCreateMove(Source::CUserCmd* pCmd)
  213. {
  214.     // Set the cmd pointer to work on
  215.     this->m_pCmd = pCmd;
  216.     // Reset best target
  217.     this->m_BestTarget.Reset();
  218.     // Reset target list
  219.     std::fill(m_TargetList.begin(), m_TargetList.end(), Target_t());
  220.  
  221.     // Let's find the target
  222.     if (this->FindTarget())
  223.     {
  224.         // If target is found, aim at it
  225.         this->AimTarget();
  226.     }
  227. }
  228.  
  229. VITRUVIA_END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement