-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
410 lines (367 loc) · 12.1 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "MemoryManager.h"
#include <iostream>
#include <vector>
#include <thread>
#include <fstream>
#include <string>
MemoryManager* Mem;
// offsets
#define dwLocalPlayer 0xD3DBEC
#define m_iTeamNum 0xF4
#define dwEntityList 0x4D523EC
#define m_angEyeAnglesX 0xB37C
#define m_angEyeAnglesY 0xB380
#define m_vecOrigin 0x138
#define m_bSpotted 0x93D
#define m_iTeamNum 0xF4
#define m_bBombPlanted 0x99D
#define m_szLastPlaceName 0x35B4
#define dwGameRulesProxy 0x526F38C
#define m_bFreezePeriod 0x20
#define dwClientState 0x58ADD4
#define dwClientState_ViewAngles 0x4D88
const std::string inputFN = "com.txt"; // file to communicate between ai and csgo helper
// player information structure, for obtaining specific information from a region in memory
struct player_info_t
{
int64_t __pad0;
union {
int64_t xuid;
struct {
int xuidlow;
int xuidhigh;
};
};
char name[128];
int userid;
char guid[33];
unsigned int friendsid;
char friendsname[128];
bool fakeplayer;
bool ishltv;
unsigned int customfiles[4];
unsigned char filesdownloaded;
};
struct Spot { // structure representing an occurrence of spotting a player
long int tick;
float posX, posY, posZ;
wchar_t* uid;
};
struct Vec3 {
float x, y, z;
};
void MoveXhair(float endPitch, float endYaw) { // moves player crosshair
Sleep(1000);
std::cout << "Moving Xhair..." << std::endl;
bool stop = false;
const float pitchSpeed = 0.2, yawSpeed = 0.2; // in degrees/ms
// obtains time for measuring speeds and performing stop conditions
DWORD startTime = GetTickCount();
DWORD currentTimePitch, startTimePitch = GetTickCount();
DWORD previousTimePitch = startTimePitch;
DWORD currentTimeYaw, startTimeYaw = GetTickCount();
DWORD previousTimeYaw = startTimeYaw;
DWORD LocalPlayer_Base = Mem->Read<DWORD>(Mem->ClientDLL_Base + dwLocalPlayer); // information about the current player
DWORD clientState = Mem->Read<DWORD>(Mem->EngineDLL_Base + dwClientState); // modifiable variables for the current player
float myEyePitch = Mem->Read<float>(LocalPlayer_Base + m_angEyeAnglesX);
float myEyeYaw = Mem->Read<float>(LocalPlayer_Base + m_angEyeAnglesY);
int pitchDirection = 0, yawDirection = 0; // determines the direction in which to move the crosshair
bool pitchComplete = false, yawComplete = false; // whether the movement is complete
Vec3 eyeVec;
eyeVec.x = myEyePitch, eyeVec.y = myEyeYaw, eyeVec.z = 0;
if (myEyePitch == endPitch) {
pitchComplete = true;
}
if (myEyeYaw == endYaw) {
yawComplete = true;
}
if (myEyePitch > 90) {
// converts pitch to range -90 to 90
myEyePitch -= 360;
}
if (endPitch > 90) {
endPitch -= 360;
}
if (myEyePitch > endPitch) {
pitchDirection = -1;
}
else {
pitchDirection = 1;
}
// yaw in range 0 to 360
if (myEyeYaw > endYaw) {
if ((myEyeYaw - endYaw) > 180) {
yawDirection = -1;
}
else {
yawDirection = 1;
}
}
else {
if ((endYaw - myEyeYaw) > 180) {
yawDirection = 1;
}
else {
yawDirection = -1;
}
}
while (!pitchComplete || !yawComplete) {
if ((GetTickCount() - startTime) > 3000) {
// more than 3s elapsed since crosshair started moving
break;
}
LocalPlayer_Base = Mem->Read<DWORD>(Mem->ClientDLL_Base + dwLocalPlayer);
currentTimePitch = GetTickCount();
if (!pitchComplete) {
if (pitchDirection == -1) {
if (myEyePitch <= endPitch) {
pitchComplete = true;
}
}
else {
if (myEyePitch >= endPitch) {
pitchComplete = true;
}
}
myEyePitch += (currentTimePitch - previousTimePitch) * pitchSpeed * pitchDirection;
if (myEyePitch > 89) {
myEyePitch = 89;
pitchComplete = true;
}
else if (myEyePitch < -89) {
myEyePitch = -89;
pitchComplete = true;
}
clientState = Mem->Read<DWORD>(Mem->EngineDLL_Base + dwClientState);
eyeVec.x = myEyePitch;
}
previousTimePitch = currentTimePitch;
currentTimeYaw = GetTickCount();
if (!yawComplete) {
if (yawDirection == 1) {
// clockwise
if (myEyeYaw < 0) {
if ((myEyeYaw + 360) < endYaw) {
if ((endYaw - myEyeYaw - 360) < 90) {
yawComplete = true;
}
}
}
else {
if (myEyeYaw < endYaw) {
if ((endYaw - myEyeYaw) < 90) {
yawComplete = true;
}
}
}
}
else {
// anticlockwise
if (myEyeYaw < 0) {
if ((myEyeYaw + 360) > endYaw) {
if ((myEyeYaw + 360 - endYaw) < 90) {
yawComplete = true;
}
}
}
else {
if (myEyeYaw > endYaw) {
if ((myEyeYaw - endYaw) < 90) {
yawComplete = true;
}
}
}
}
myEyeYaw += (currentTimeYaw - previousTimeYaw) * yawSpeed * (-yawDirection);
if (myEyeYaw < 0) {
// modulo check
myEyeYaw += 360;
}
if (myEyeYaw >= 360) {
myEyeYaw -= 360;
}
if (myEyeYaw > 180) {
myEyeYaw -= 360;
}
eyeVec.y = myEyeYaw;
}
previousTimeYaw = currentTimeYaw;
Mem->Write<Vec3>(clientState + dwClientState_ViewAngles, eyeVec); // writes to game memory
}
}
// writes the game state to the shared file with the ai
void SendInfo(const unsigned long long startTime, std::vector<Spot> &spottedList, bool &stopFlag) {
DWORD LocalPlayer_Base, Grp_Base;
int myTeamId;
float myEyePitch, myEyeYaw;
float myPosX, myPosY, myPosZ;
DWORD currentTime;
int currentTick;
bool bombPlanted, hold;
long long lastOutputTime = 0;
while(!stopFlag) {
if (GetAsyncKeyState(VK_F5)) {
//Retrieve player information
LocalPlayer_Base = Mem->Read<DWORD>(Mem->ClientDLL_Base + dwLocalPlayer);
myTeamId = Mem->Read<int>(LocalPlayer_Base + m_iTeamNum);
myEyePitch = Mem->Read<float>(LocalPlayer_Base + m_angEyeAnglesX);
myEyeYaw = Mem->Read<float>(LocalPlayer_Base + m_angEyeAnglesY);
myPosX = Mem->Read<float>(LocalPlayer_Base + m_vecOrigin);
myPosY = Mem->Read<float>(LocalPlayer_Base + m_vecOrigin + 0x4);
myPosZ = Mem->Read<float>(LocalPlayer_Base + m_vecOrigin + 0x8);
std::cout << "Eye angles: " << myEyePitch << " " << myEyeYaw << std::endl;
std::cout << "Position: " << myPosX << " " << myPosY << " " << myPosZ << std::endl;
currentTime = GetTickCount() - startTime;
currentTick = currentTime / 1000 * 64;
std::cout << "Current tick: " << currentTick << std::endl;
Grp_Base = Mem->Read<DWORD>(Mem->ClientDLL_Base + dwGameRulesProxy);
bombPlanted = Mem->Read<bool>(Grp_Base + m_bBombPlanted);
hold = false;
if (myTeamId == 2) {
// terrorist
hold = (bombPlanted ? true : false);
}
else if (myTeamId == 3) {
// CT
std::cout << bombPlanted;
hold = (bombPlanted ? false : true);
}
std::cout << "Hold: " << (hold ? "yes" : "no");
std::cout << "Spotted list: " << std::endl;
for (auto& spot : spottedList) {
std::cout << "Tick " << spot.tick << ": " << spot.posX << "," << spot.posY << "," << spot.posZ << " ->" << spot.uid << std::endl;
}
std::string fileInput = "\n";
fileInput += "input," + std::to_string(currentTick) + ",";
fileInput += std::to_string(currentTick) + "," + std::to_string(myPosX) + "," + std::to_string(myPosY) + "," + std::to_string(myPosZ) + ",";
fileInput += std::to_string(myEyePitch) + "," + std::to_string(myEyeYaw) + ",spotted" + std::to_string(spottedList.size()) + ",hold" + (hold ? "1" : "0");
for (auto& spot : spottedList) {
fileInput += "," + std::to_string(spot.posX) + "," + std::to_string(spot.posY) + "," + std::to_string(spot.posZ) + "," + std::to_string(spot.tick);
}
fileInput += "\n";
std::cout << fileInput << std::endl;
std::fstream myfile;
myfile.open(inputFN, std::ios_base::app);
myfile << fileInput;
myfile.close();
// write to file the input to the ai
// reads ideal yaw and pitch from shared file with ai
for (int pollFileCount = 0; pollFileCount < 15; pollFileCount++) {
// continuously polls the file for 3 seconds to receive the output from the ai
std::fstream myfile2;
myfile2.open(inputFN);
// reads the last line of the file
myfile2.seekg(-1, std::ios_base::end);
bool keepLooping = true;
while (keepLooping) {
char ch;
myfile2.get(ch);
if ((int)myfile2.tellg() <= 1) {
myfile2.seekg(0);
keepLooping = false;
}
else if (ch == '\n') {
keepLooping = false;
}
else {
myfile2.seekg(-2, std::ios_base::cur);
}
}
std::string lastLine;
std::getline(myfile2, lastLine);
myfile2.close();
int linePos1 = 0, linePos2 = 0;
float outputPitch, outputYaw;
linePos2 = lastLine.find(",");
std::cout << lastLine << std::endl;
if (lastLine.substr(linePos1, linePos2) == "output") { // checks if line type is output
linePos1 = linePos2+1;
linePos2 = lastLine.find(",");
int newOutputTime = std::stoll(lastLine.substr(linePos1, linePos2 - linePos1));
if (newOutputTime > lastOutputTime) { // checks if line was written after the last previous read
lastOutputTime = newOutputTime;
// move crosshair based on ideal yaw and pitch calculated by the ai
linePos1 = linePos2+1;
linePos2 = lastLine.find(",");
outputPitch = std::stof(lastLine.substr(linePos1, linePos2 - linePos1));
linePos1 = linePos2+1;
outputYaw = std::stof(lastLine.substr(linePos1, lastLine.length() - linePos1));
MoveXhair(outputPitch, outputYaw);
break;
}
}
Sleep(200);
}
Sleep(200);
}
Sleep(10);
}
}
void GetStats() { // records the instantaneous state of the game and
std::vector<Spot> spottedList(0); // list of spotted players
const unsigned long long startTime = GetTickCount();
bool stopFlag = false;
// creates a thread to wait for key press to activate the ai
std::thread tSendInfo(SendInfo, startTime, std::ref(spottedList), std::ref(stopFlag));
const int tickrate = 64;
DWORD LocalPlayer_Base, Grp_Base;
int myTeamId;
bool freezetime;
std::cout << "Started recording stats..." << std::endl;
while (true) {
LocalPlayer_Base = Mem->Read<DWORD>(Mem->ClientDLL_Base + dwLocalPlayer);
myTeamId = Mem->Read<int>(LocalPlayer_Base + m_iTeamNum);
Grp_Base = Mem->Read<DWORD>(Mem->ClientDLL_Base + dwGameRulesProxy);
freezetime = Mem->Read<bool>(Grp_Base + m_bFreezePeriod);
// check if round is over
if (freezetime) {
spottedList.clear();
}
for (int i = 1; i < 64; i++) {
// iterates over all entities in the list, includes players
DWORD playerBase = Mem->Read<DWORD>(Mem->ClientDLL_Base + dwEntityList + (i * 0x10));
if (!playerBase) continue;
int playerTeamId = Mem->Read<int>(playerBase + m_iTeamNum);
if (playerTeamId != 2 && playerTeamId != 3) continue; // checks if the entity is a valid player
wchar_t* playerLastPlace = Mem->Read<wchar_t*>(playerBase + m_szLastPlaceName); // reads the player's last position's name
if (playerTeamId != myTeamId) { // player not on user's team
bool spotted = Mem->Read<bool>(playerBase + m_bSpotted);
if (spotted) {
Spot newSpot;
newSpot.posX = Mem->Read<float>(playerBase + m_vecOrigin);
newSpot.posY = Mem->Read<float>(playerBase + m_vecOrigin + 0x4);
newSpot.posZ = Mem->Read<float>(playerBase + m_vecOrigin + 0x8);
newSpot.posZ += 64.06;
auto currentTime = GetTickCount();
currentTime -= startTime;
newSpot.tick = currentTime / 1000 * 64;;
newSpot.uid = Mem->Read<wchar_t*>(playerBase + m_szLastPlaceName);
// merges duplicate spots
for (int x = spottedList.size()-1; x >= 0; x--) {
if (newSpot.tick - spottedList[x].tick < 256) {
// spot occurred <256ticks (4s) before current spot
if (spottedList[x].uid == newSpot.uid) {
spottedList.erase(spottedList.begin() + x);
break;
}
}
else {
break;
}
}
spottedList.push_back(newSpot);
}
}
}
}
stopFlag = true;
tSendInfo.join();
}
int main()
{
Mem = new MemoryManager();
std::cout << "CSAI started :^D" << std::endl;
GetStats();
delete Mem;
return 0;
}