Advertisement
NatPatat

LegitBot.cpp

Nov 21st, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.87 KB | None | 0 0
  1.  
  2. #include "LegitBot.h"
  3. #include "Render.h"
  4. #include "SDK.h"
  5. #include "EnginePrediction.h"
  6. #include "Global.h"
  7. #define NOMINMAX
  8. #include <Windows.h>
  9. #include <stdio.h>
  10. #include <random>
  11. #include <string>
  12. #include <vector>
  13.  
  14. legitbot::legitbot()
  15. {
  16. best_target = -1;
  17.  
  18. view_angle = QAngle(0.0f, 0.0f, 0.0f);
  19. aim_angle = QAngle(0.0f, 0.0f, 0.0f);
  20. delta_angle = QAngle(0.0f, 0.0f, 0.0f);
  21. final_angle = QAngle(0.0f, 0.0f, 0.0f);
  22.  
  23. hitbox_position = Vector(0.0f, 0.0f, 0.0f);
  24.  
  25. aim_key = 0;
  26. aim_smooth = 1;
  27. aim_fov = 0;
  28. randomized_smooth = 0;
  29. recoil_min = 0;
  30. recoil_max = 0;
  31. randomized_angle = 0;
  32. shoot = false;
  33. }
  34. float get_fov(const QAngle &viewAngles, const QAngle &aimAngles)
  35. {
  36. Vector ang, aim;
  37. AngleVectors(viewAngles, &aim);
  38. AngleVectors(aimAngles, &ang);
  39.  
  40. return RAD2DEG(acos(aim.Dot(ang) / aim.LengthSqr()));
  41. }
  42. float random_number_range(float min, float max)
  43. {
  44. std::random_device device;
  45. std::mt19937 engine(device());
  46. std::uniform_real_distribution<> distribution(min, max);
  47. return static_cast< float >(distribution(engine));
  48. }
  49.  
  50.  
  51. bool shoot;
  52. static int custom_delay = 0;
  53.  
  54. void legitbot::OnCreateMove(CInput::CUserCmd *pCmd, C_BaseEntity *local)
  55. {
  56. if (!g_Options.Legitbot.MainSwitch)
  57. return;
  58.  
  59. CBaseCombatWeapon* pWeapon = (CBaseCombatWeapon*)g_EntityList->GetClientEntityFromHandle(local->GetActiveWeaponHandle());
  60.  
  61. if (local && local->IsAlive() && pWeapon)
  62. {
  63. do_aimbot(local, pWeapon, pCmd);
  64.  
  65. if (!G::PressedKeys[g_Options.Legitbot.Triggerbot.Key]) custom_delay = 0;
  66.  
  67. if (g_Options.Legitbot.Triggerbot.Enabled && g_Options.Legitbot.Triggerbot.Key != 0 && G::PressedKeys[g_Options.Legitbot.Triggerbot.Key])
  68. triggerbot(pCmd, local, pWeapon);
  69. }
  70. }
  71.  
  72. void legitbot::triggerbot(CInput::CUserCmd *cmd, C_BaseEntity* local, CBaseCombatWeapon* weapon)
  73. {
  74. if (!local->IsAlive())
  75. return;
  76.  
  77. if (weapon) {
  78. if (weapon->GetAmmoInClip() == 0)
  79. return;
  80. if (MiscFunctions::IsKnife(weapon) || MiscFunctions::IsGrenade(weapon)) return;
  81. if (*weapon->m_AttributeManager()->m_Item()->ItemDefinitionIndex() == 64) return;
  82. }
  83.  
  84.  
  85.  
  86. QAngle ViewAngles = cmd->viewangles + local->localPlayerExclusive()->GetAimPunchAngle() ;
  87.  
  88.  
  89. Vector CrosshairForward;
  90. AngleVectors(ViewAngles, &CrosshairForward);
  91. CrosshairForward *= weapon->GetCSWpnData()->m_fRange;
  92.  
  93.  
  94. Vector TraceSource = local->GetEyePosition() ;
  95. Vector TraceDestination = TraceSource + CrosshairForward;
  96.  
  97. Ray_t Ray;
  98. trace_t Trace;
  99. CTraceFilter Filter;
  100.  
  101. Filter.pSkip = local;
  102.  
  103. Ray.Init(TraceSource, TraceDestination);
  104. g_EngineTrace->TraceRay(Ray, MASK_SHOT, &Filter, &Trace);
  105.  
  106. if (!Trace.m_pEnt)
  107. return;
  108. if (!Trace.m_pEnt->IsAlive())
  109. return;
  110. if (Trace.m_pEnt->HasGunGameImmunity())
  111. return;
  112.  
  113.  
  114. if (local->GetTeamNum() == Trace.m_pEnt->GetTeamNum())
  115. return;
  116.  
  117. if (!hit_chance(local, cmd, weapon, Trace.m_pEnt))
  118. return;
  119.  
  120. bool didHit = false;
  121. if ((g_Options.Legitbot.Triggerbot.Filter.Head && Trace.hitgroup == 1)
  122. || (g_Options.Legitbot.Triggerbot.Filter.Chest && Trace.hitgroup == 2)
  123. || (g_Options.Legitbot.Triggerbot.Filter.Stomach && Trace.hitgroup == 3)
  124. || (g_Options.Legitbot.Triggerbot.Filter.Arms && (Trace.hitgroup == 4 || Trace.hitgroup == 5))
  125. || (g_Options.Legitbot.Triggerbot.Filter.Legs && (Trace.hitgroup == 6 || Trace.hitgroup == 7)))
  126. {
  127. didHit = true;
  128. }
  129.  
  130. if (g_Options.Legitbot.Triggerbot.Delay >= 1)
  131. {
  132. if (custom_delay >= g_Options.Legitbot.Triggerbot.Delay / 30)
  133. {
  134. if (didHit)
  135. {
  136. custom_delay = 0;
  137. shoot = true;
  138. cmd->buttons |= IN_ATTACK;
  139. }
  140. }
  141. else
  142. {
  143. custom_delay++;
  144. }
  145. }
  146.  
  147. }
  148.  
  149. void legitbot::do_aimbot(C_BaseEntity *local, CBaseCombatWeapon *weapon, CInput::CUserCmd *cmd)
  150. {
  151. if (!g_Options.Legitbot.MainSwitch)
  152. return;
  153.  
  154. if (!weapon)
  155. return;
  156.  
  157. if (!local)
  158. return;
  159. if (!cmd)
  160. return;
  161.  
  162. if (!local->IsAlive())
  163. return;
  164.  
  165. if (!weapon->GetAmmoInClip() > 0)
  166. return;
  167.  
  168.  
  169. if (weapon->GetAmmoInClip() == 0)
  170. return;
  171.  
  172. if (MiscFunctions::IsKnife(weapon) || MiscFunctions::IsGrenade(weapon))
  173. return;
  174.  
  175.  
  176. weapon_settings(weapon);
  177.  
  178. if (!aim_key)
  179. return;
  180.  
  181. if (!G::PressedKeys[aim_key])
  182. return;
  183.  
  184.  
  185.  
  186. best_target = get_target(local, weapon, cmd, hitbox_position);
  187.  
  188.  
  189. if (best_target == -1)
  190. return;
  191. C_BaseEntity* entity = (C_BaseEntity*)g_EntityList->GetClientEntity(best_target);
  192. if (!entity)
  193. return;
  194.  
  195.  
  196.  
  197. if (get_distance(local->GetEyePosition(), hitbox_position) > 8192.0f)
  198. return;
  199.  
  200.  
  201. compute_angle(local->GetEyePosition(), hitbox_position, aim_angle);
  202. sanitize_angles(aim_angle);
  203.  
  204. if (hitbox_position == Vector(0, 0, 0))
  205. return;
  206.  
  207. aim_angle -= get_randomized_recoil(local);
  208. aim_angle += get_randomized_angles(local);
  209.  
  210. sanitize_angles(view_angle);
  211.  
  212. delta_angle = view_angle - aim_angle;
  213. sanitize_angles(delta_angle);
  214.  
  215. float randomSmoothing = 1.0f;
  216.  
  217. if (randomized_smooth > 1.0f)
  218. randomSmoothing = random_number_range(randomized_smooth / 10.0f, 1.0f);
  219.  
  220. final_angle = view_angle - delta_angle / aim_smooth * randomSmoothing;
  221. sanitize_angles(final_angle);
  222.  
  223. if (!sanitize_angles(final_angle))
  224. return;
  225.  
  226. cmd->viewangles = final_angle;
  227. g_Engine->SetViewAngles(cmd->viewangles);
  228. }
  229.  
  230. bool legitbot::hit_chance(C_BaseEntity* local, CInput::CUserCmd* cmd, CBaseCombatWeapon* weapon, C_BaseEntity* target)
  231. {
  232. Vector forward, right, up;
  233.  
  234. constexpr auto max_traces = 150;
  235.  
  236. AngleVectors(cmd->viewangles, &forward, &right, &up);
  237.  
  238. int total_hits = 0;
  239. int needed_hits = static_cast<int>(max_traces * (g_Options.Legitbot.Triggerbot.hitchance / 100.f));
  240.  
  241. weapon->UpdateAccuracyPenalty(weapon);
  242.  
  243. auto eyes = local->GetEyePosition();
  244. auto flRange = weapon->GetCSWpnData()->m_fRange;
  245.  
  246. for (int i = 0; i < max_traces; i++) {
  247. RandomSeed(i + 1);
  248.  
  249. float fRand1 = RandomFloat(0.f, 1.f);
  250. float fRandPi1 = RandomFloat(0.f, XM_2PI);
  251. float fRand2 = RandomFloat(0.f, 1.f);
  252. float fRandPi2 = RandomFloat(0.f, XM_2PI);
  253.  
  254. float fRandInaccuracy = fRand1 * weapon->GetInaccuracy();
  255. float fRandSpread = fRand2 * weapon->GetSpread();
  256.  
  257. float fSpreadX = cos(fRandPi1) * fRandInaccuracy + cos(fRandPi2) * fRandSpread;
  258. float fSpreadY = sin(fRandPi1) * fRandInaccuracy + sin(fRandPi2) * fRandSpread;
  259.  
  260. auto viewSpreadForward = (forward + fSpreadX * right + fSpreadY * up).Normalized();
  261.  
  262. QAngle viewAnglesSpread;
  263. VectorAngles(viewSpreadForward, viewAnglesSpread);
  264. sanitize_angles(viewAnglesSpread);
  265.  
  266. Vector viewForward;
  267. AngleVectors(viewAnglesSpread, &viewForward);
  268. viewForward.NormalizeInPlace();
  269.  
  270. viewForward = eyes + (viewForward * flRange);
  271.  
  272. trace_t tr;
  273. Ray_t ray;
  274. ray.Init(eyes, viewForward);
  275.  
  276. g_EngineTrace->ClipRayToEntity(ray, MASK_SHOT | CONTENTS_GRATE, target, &tr);
  277.  
  278.  
  279. if (tr.m_pEnt == target)
  280. total_hits++;
  281.  
  282. if (total_hits >= needed_hits)
  283. return true;
  284.  
  285. if ((max_traces - i + total_hits) < needed_hits)
  286. return false;
  287. }
  288.  
  289. return false;
  290. }
  291.  
  292. void legitbot::weapon_settings(CBaseCombatWeapon* weapon)
  293. {
  294. if (!weapon)
  295. return;
  296.  
  297.  
  298. if (MiscFunctions::IsSniper(weapon))
  299. {
  300. aim_key = g_Options.Legitbot.SniperKey;
  301. aim_smooth = g_Options.Legitbot.SniperSmooth;
  302. aim_fov = g_Options.Legitbot.Sniperfov;
  303. randomized_smooth = 1;
  304. recoil_min = g_Options.Legitbot.sniper_recoil_min;
  305. recoil_max = g_Options.Legitbot.sniper_recoil_max;
  306. randomized_angle = 1;
  307.  
  308. }
  309. else if (MiscFunctions::IsPistol(weapon))
  310. {
  311. aim_key = g_Options.Legitbot.PistolKey;
  312. aim_smooth = g_Options.Legitbot.PistolSmooth;
  313. aim_fov = g_Options.Legitbot.Pistolfov;
  314. randomized_smooth = 1;
  315. recoil_min = g_Options.Legitbot.pistol_recoil_min;
  316. recoil_max = g_Options.Legitbot.pistol_recoil_max;
  317. randomized_angle = 1;
  318. }
  319. else
  320. {
  321. aim_key = g_Options.Legitbot.MainKey;
  322. aim_smooth = g_Options.Legitbot.MainSmooth;
  323. aim_fov = g_Options.Legitbot.Mainfov;
  324. randomized_smooth = 1;
  325. recoil_min = g_Options.Legitbot.main_recoil_min;
  326. recoil_max = g_Options.Legitbot.main_recoil_max;
  327. randomized_angle = 1;
  328. }
  329. }
  330.  
  331. QAngle legitbot::get_randomized_recoil(C_BaseEntity *local)
  332. {
  333. QAngle compensatedAngles = (local->localPlayerExclusive()->GetAimPunchAngle() * 2.0f) * (random_number_range(recoil_min, recoil_max) / 100.0f);
  334. sanitize_angles(compensatedAngles);
  335.  
  336. return (local->m_iShotsFired() > 1 ? compensatedAngles : QAngle(0.0f, 0.0f, 0.0f));
  337. }
  338.  
  339. QAngle legitbot::get_randomized_angles(C_BaseEntity *local)
  340. {
  341. QAngle randomizedValue = QAngle(0.0f, 0.0f, 0.0f);
  342.  
  343. float randomRate = random_number_range(-randomized_angle, randomized_angle);
  344. float randomDeviation = random_number_range(-randomized_angle, randomized_angle);
  345.  
  346. switch (rand() % 2)
  347. {
  348. case 0:
  349. randomizedValue.x = (randomRate * cos(randomDeviation));
  350. randomizedValue.y = (randomRate * cos(randomDeviation));
  351. randomizedValue.z = (randomRate * cos(randomDeviation));
  352. break;
  353. case 1:
  354. randomizedValue.x = (randomRate * sin(randomDeviation));
  355. randomizedValue.y = (randomRate * sin(randomDeviation));
  356. randomizedValue.z = (randomRate * sin(randomDeviation));
  357. break;
  358. }
  359.  
  360. sanitize_angles(randomizedValue);
  361.  
  362. return (local->m_iShotsFired() > 1 ? randomizedValue : QAngle(0.0f, 0.0f, 0.0f));
  363. }
  364. bool get_hitbox_pos(C_BaseEntity* entity, int hitbox, Vector &output)
  365. {
  366. if (hitbox >= 20)
  367. return false;
  368.  
  369. const model_t *model = entity->GetModel();
  370. if (!model)
  371. return false;
  372.  
  373. studiohdr_t *studioHdr = g_ModelInfo->GetStudiomodel(model);
  374. if (!studioHdr)
  375. return false;
  376.  
  377. matrix3x4 matrix[128];
  378. if (!entity->SetupBones(matrix, 128, 0x100, entity->GetSimulationTime()))
  379. return false;
  380.  
  381. mstudiobbox_t *studioBox = studioHdr->GetHitboxSet(0)->GetHitbox(hitbox);
  382. if (!studioBox)
  383. return false;
  384.  
  385. Vector min, max;
  386.  
  387. VectorTransform(studioBox->bbmin, matrix[studioBox->bone], min);
  388. VectorTransform(studioBox->bbmax, matrix[studioBox->bone], max);
  389.  
  390. output = (min + max) * 0.5f;
  391.  
  392. return true;
  393. }
  394. bool legitbot::get_hitbox(C_BaseEntity *local, C_BaseEntity *entity, Vector &destination)
  395. {
  396. int bestHitbox = -1;
  397. float best_fov = aim_fov;
  398.  
  399. static const std::vector<int> hitboxes = { (int)CSGOHitboxID::Head };
  400.  
  401. for (auto hitbox : hitboxes)
  402. {
  403. Vector temp;
  404. if (!get_hitbox_pos(entity, hitbox, temp))
  405. continue;
  406.  
  407. float fov = get_fov(view_angle, compute_angle(local->GetEyePosition(), temp));
  408. if (fov < best_fov)
  409. {
  410. best_fov = fov;
  411. bestHitbox = hitbox;
  412. }
  413. }
  414.  
  415. if (bestHitbox != -1)
  416. {
  417. if (!get_hitbox_pos(entity, bestHitbox, destination))
  418. return true;
  419. }
  420.  
  421. return false;
  422. }
  423.  
  424.  
  425. int legitbot::get_target(C_BaseEntity *local, CBaseCombatWeapon *weapon, CInput::CUserCmd *cmd, Vector &destination)
  426. {
  427. int best_target = -1;
  428. float best_fov = aim_fov;
  429.  
  430. g_Engine->GetViewAngles(view_angle);
  431.  
  432. for (int i = 1; i <= g_Globals->maxClients; i++)
  433. {
  434. C_BaseEntity *entity = (C_BaseEntity*)g_EntityList->GetClientEntity(i);
  435. if (!entity
  436. || entity == local
  437. || entity->IsDormant()
  438. || entity->GetLifeState() != LIFE_ALIVE
  439. || entity->HasGunGameImmunity()
  440. || entity->GetClientClass()->m_ClassID != (int)ClassID::CCSPlayer
  441. || entity->GetTeamNum() == local->GetTeamNum()
  442. || !(entity->GetFlags() & FL_ONGROUND))
  443. continue;
  444.  
  445. Vector hitbox;
  446. if (get_hitbox(local, entity, hitbox))
  447. continue;
  448.  
  449. float fov = get_fov(view_angle + (local->localPlayerExclusive()->GetAimPunchAngle() * 2.0f), compute_angle(local->GetEyePosition(), hitbox));
  450. if (fov < best_fov && fov < aim_fov)
  451. {
  452. if (MiscFunctions::IsVisible(local, entity, 0))
  453. {
  454. best_fov = fov;
  455. destination = hitbox;
  456. best_target = i;
  457. }
  458. }
  459. }
  460.  
  461. return best_target;
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement