Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #include <regex>
  2. #include <vector>
  3. #include "../valve_sdk/csgostructs.hpp"
  4. #include <iostream>
  5. #include "../helpers/math.hpp"
  6. #include "../options.hpp"
  7. #include <fstream>
  8. #include <iomanip>
  9. #include <dirent.h>
  10. #include <sys/stat.h>
  11.  
  12. namespace movementRecorder {
  13.  
  14. struct custom_cmd
  15. {
  16. QAngle viewAngles;
  17. Vector pos;
  18. float forwardmove;
  19. float sidemove;
  20. float upmove;
  21. int buttons;
  22. };
  23.  
  24. std::vector<custom_cmd> CmdFinal;
  25. custom_cmd tempCmd;
  26.  
  27. bool recordBool;
  28. bool playBool;
  29. bool crossDistBool;
  30. bool AimToFirstRecord;
  31. bool recordAfterDonePlaying;
  32.  
  33. QAngle viewPos;
  34. Vector lastPos;
  35. bool tempBool = true;
  36. bool isPlayingback = false;
  37. int i = 0;
  38.  
  39. void writeVec(std::ostream& os, const std::vector<custom_cmd>& cmd)
  40. {
  41. typename std::vector<custom_cmd>::size_type size = cmd.size();
  42. os.write((char*)&size, sizeof(size));
  43. os.write((char*)&cmd[0], cmd.size() * sizeof(custom_cmd));
  44. }
  45.  
  46. void readVec(std::istream& is, std::vector<custom_cmd>& cmd)
  47. {
  48. typename std::vector<custom_cmd>::size_type size = 0;
  49. is.read((char*)&size, sizeof(size));
  50. cmd.resize(size);
  51. is.read((char*)&cmd[0], cmd.size() * sizeof(custom_cmd));
  52. }
  53.  
  54. void saveToFile()
  55. {
  56. const char* mapname = g_EngineClient->GetMapGroupName();
  57.  
  58. std::string ayyy(mapname);
  59.  
  60. std::string result = "customPaths/";
  61.  
  62. std::regex re("\/(?!.*\/)(.*)");
  63.  
  64. std::smatch match;
  65.  
  66. if (std::regex_search(ayyy, match, re) && !CmdFinal.empty())
  67. {
  68. result += match.str(1);
  69. result += ".bin";
  70. std::ofstream out(result, std::ios::out | std::ios::binary);
  71. writeVec(out, CmdFinal);
  72. out.close();
  73. }
  74. }
  75.  
  76. void readFromFile()
  77. {
  78. const char* mapname = g_EngineClient->GetMapGroupName();
  79.  
  80. CmdFinal = {};
  81.  
  82. DIR* dpdf;
  83. struct dirent* epdf;
  84. struct stat file_stats;
  85.  
  86. std::string tempname = "customPaths/";
  87.  
  88. dpdf = opendir("C:/Steam/steamapps/common/Counter-Strike Global Offensive/customPaths/");
  89.  
  90. if (dpdf != NULL) {
  91. while (epdf = readdir(dpdf)) {
  92. if (*epdf->d_name == '.')
  93. continue;
  94.  
  95.  
  96. std::string temp = (std::string)epdf->d_name;
  97. temp = temp.substr(0, temp.size() - 4);
  98.  
  99. if (strstr(mapname, temp.c_str()))
  100. {
  101. tempname += temp + ".bin";
  102. std::ifstream in(tempname.c_str(), std::ios::out | std::ios::binary);
  103. readVec(in, CmdFinal);
  104. in.close();
  105. break;
  106. }
  107. }
  108. }
  109. closedir(dpdf);
  110.  
  111. }
  112.  
  113. void record(CUserCmd* cmd)
  114. {
  115. if (!g_Options.movement_recording)
  116. return;
  117.  
  118. if (g_Options.recordBool)
  119. {
  120. custom_cmd tempCmd = {};
  121.  
  122. tempCmd.buttons = cmd->buttons;
  123. tempCmd.forwardmove = cmd->forwardmove;
  124. tempCmd.sidemove = cmd->sidemove;
  125. tempCmd.upmove = cmd->upmove;
  126. tempCmd.viewAngles = cmd->viewangles;
  127. tempCmd.pos = g_LocalPlayer->m_vecOrigin();
  128.  
  129. if (tempBool)
  130. {
  131. CmdFinal = {};
  132. }
  133. CmdFinal.push_back(tempCmd);
  134. tempBool = false;
  135. }
  136. else
  137. {
  138. tempBool = true;
  139. }
  140. }
  141.  
  142. void play(CUserCmd* cmd)
  143. {
  144. if (!g_Options.movement_recording)
  145. return;
  146.  
  147. if (CmdFinal.size() <= 0)
  148. return;
  149.  
  150. if (g_Options.playBool) {
  151. if (!isPlayingback) {
  152. auto dist = Math::VectorDistance(g_LocalPlayer->m_vecOrigin(), CmdFinal[0].pos);
  153.  
  154. if (dist <= 1.f) {
  155. isPlayingback = true;
  156. }
  157. else {
  158. if (!crossDistBool) {
  159. auto viewPos = Vector(CmdFinal[0].pos[0], CmdFinal[0].pos[1], CmdFinal[0].pos[2] + 64.f);
  160.  
  161. Vector temp2D;
  162.  
  163. float crossDist = 0;
  164.  
  165. if (Math::WorldToScreen(viewPos, temp2D)) {
  166. crossDist = DistanceBetweenCross(temp2D[0], temp2D[1]);
  167. }
  168. else {
  169. crossDist = 10000;
  170. }
  171.  
  172. if (crossDist <= 1.f) {
  173. crossDistBool = true;
  174. }
  175. }
  176. else {
  177.  
  178. //move to start point
  179. auto finalVec = CmdFinal[0].pos - g_LocalPlayer->m_vecOrigin();
  180.  
  181. QAngle wishAngle;
  182. Math::VectorAngles(finalVec, wishAngle);
  183.  
  184. g_EngineClient->SetViewAngles(&wishAngle);
  185.  
  186. cmd->viewangles = wishAngle;
  187. cmd->forwardmove = dist;
  188.  
  189. }
  190.  
  191.  
  192.  
  193. }
  194. }
  195. else
  196. {
  197. if (GetAsyncKeyState(VK_LBUTTON) & 0x8000 && GetAsyncKeyState('H') & 0x8000) { //When left mouse button and H pressed we remove ending of the playing record
  198. while (i != CmdFinal.size()) {
  199. CmdFinal.pop_back();
  200. }
  201. *g_Options.recordBool = false;
  202. *g_Options.playBool = false;
  203. return;
  204. }
  205. if (GetAsyncKeyState(VK_RBUTTON) & 0x8000 && GetAsyncKeyState('H') & 0x8000) { //When right mouse button and H pressed we remove ending of the playing record and instantly start recording
  206. while (i != CmdFinal.size()) {
  207. CmdFinal.pop_back();
  208. }
  209. *g_Options.playBool = false;
  210. *g_Options.recordBool = true;
  211. tempBool = false;
  212. return;
  213. }
  214. if (i >= CmdFinal.size()) {
  215. i = 0;//reset
  216. isPlayingback = false;
  217. crossDistBool = false;
  218. AimToFirstRecord = true;
  219. if (!recordAfterDonePlaying)
  220. *g_Options.playBool = false;
  221. else {
  222. *g_Options.playBool = false;
  223. *g_Options.recordBool = true;
  224. tempBool = false;
  225. }
  226. }
  227. else
  228. {
  229.  
  230. if (AimToFirstRecord) {
  231. Vector aimVec;
  232. Math::AngleVectors(CmdFinal[0].viewAngles, aimVec);
  233. Vector curVec;
  234. Math::AngleVectors(cmd->viewangles, curVec);
  235.  
  236. auto delta = aimVec - curVec;
  237.  
  238. const auto smoothed = curVec + delta / 64.f;
  239.  
  240. QAngle aimAng;
  241. Math::VectorAngles(smoothed, aimAng);
  242.  
  243. Math::Normalize3(aimAng);
  244. Math::ClampAngles(aimAng);
  245.  
  246. g_EngineClient->SetViewAngles(&aimAng);
  247.  
  248. cmd->viewangles = aimAng;
  249.  
  250. auto deltadist = Math::VectorLength(delta);
  251.  
  252.  
  253. if (deltadist <= 0.0001f) {
  254. AimToFirstRecord = false;
  255. }
  256.  
  257. }
  258. else {
  259. cmd->buttons = CmdFinal[i].buttons;
  260. cmd->forwardmove = CmdFinal[i].forwardmove;
  261. cmd->sidemove = CmdFinal[i].sidemove;
  262. cmd->upmove = CmdFinal[i].upmove;
  263. cmd->viewangles = CmdFinal[i].viewAngles;
  264.  
  265. g_EngineClient->SetViewAngles(&CmdFinal[i].viewAngles);
  266.  
  267. i++;
  268. }
  269.  
  270. }
  271. }
  272. }
  273. else
  274. {
  275. i = 0;//reset
  276. isPlayingback = false;//stop playback
  277. crossDistBool = false;
  278. AimToFirstRecord = true;
  279.  
  280. }
  281. }
  282.  
  283. float DistanceBetweenCross(float X, float Y)
  284. {
  285. float ydist = (Y - GetSystemMetrics(SM_CYSCREEN) / 2);
  286. float xdist = (X - GetSystemMetrics(SM_CXSCREEN) / 2);
  287. float Hypotenuse = sqrt(pow(ydist, 2) + pow(xdist, 2));
  288. return Hypotenuse;
  289. }
  290.  
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement