Code:
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
#include <random>
#include <ctime>
#include <string>
using namespace std;
void click();
const DWORD DwEntityList = 0x4A4CCC4;
const DWORD DwEnginePointer = 0x5B92A4;
const DWORD DwLocalPlayer = 0xA31504;
const DWORD DwCrosshairId = 0x0000AA44;
const DWORD DwViewAngle = 0x00004D0C;
const DWORD DwVecViewOrigin = 0x104;
const DWORD DwVecOrigin = 0x134;
const DWORD DwVecPunch = 0x00003018;
const DWORD DwTeamNumber = 0xF0;
const DWORD DwShotsFired = 0xA2B0;
const DWORD DwFlags = 0x100;
const DWORD DwBoneMatrix = 0x00002698;
const DWORD DwEntitySize = 0x10;
const DWORD DwHealth = 0xFC;
const DWORD DwLifeState = 0x25B;
const DWORD DwVecVelocity = 0x110;
const DWORD vecViewOffset = 0x104;
const DWORD DwIndex = 0x00000064;
const DWORD DwSpottedByMask = 0x0000097C;
const DWORD DwSpotted = 0x00000939;
const DWORD DwAttack = 0x02E8CCD0;
int targetBone = 6;
void click();
typedef struct Vector_
{
float x;
float y;
float z;
}Vector;
struct
{
template <class type>
type Read(HANDLE hProc, DWORD address)
{
type val;
ReadProcessMemory(hProc, (LPCVOID)(address), &val, sizeof(val), NULL);
return val;
}
template <class type>
void Write(HANDLE hProc, DWORD address, type val)
{
WriteProcessMemory(hProc, (LPVOID)(address), &val, sizeof(val), NULL);
}
}Mem;
DWORD getDll(DWORD pId, char* NAME)
{
DWORD Dll = 0x0;
MODULEENTRY32 moduleEntry;
moduleEntry.dwSize = sizeof(MODULEENTRY32);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pId);
do
{
if (!strcmp(moduleEntry.szModule, NAME))
{
Dll = (DWORD)moduleEntry.modBaseAddr;
break;
}
} while (Module32Next(hSnap, &moduleEntry));
CloseHandle(hSnap);
return Dll;
}
struct
{
DWORD getEntityBase(HANDLE hProc, DWORD clientDll, int playerNumber)
{
return Mem.Read<DWORD>(hProc, (clientDll + DwEntityList + (DwEntitySize * (playerNumber - 1))));
}
Vector getHeadPos(HANDLE hProc, DWORD clientDll, int playerNumber)
{
DWORD entityBase = getEntityBase(hProc, clientDll, playerNumber);
if (entityBase)
{
DWORD boneMatrix = Mem.Read<DWORD>(hProc, (entityBase + DwBoneMatrix));
if (boneMatrix)
{
Vector bonePosition{};
bonePosition.x = Mem.Read<float>(hProc, (boneMatrix + 0x30 * targetBone + 0x0C));
bonePosition.y = Mem.Read<float>(hProc, (boneMatrix + 0x30 * targetBone + 0x1C));
bonePosition.z = Mem.Read<float>(hProc, (boneMatrix + 0x30 * targetBone + 0x2C));
return bonePosition;
}
}
}
int getTeam(HANDLE hProc, DWORD clientDll, int playerNumber)
{
DWORD entityBase = getEntityBase(hProc, clientDll, playerNumber);
if (entityBase)
{
return Mem.Read<int>(hProc, (entityBase + DwTeamNumber));
}
}
int getHealth(HANDLE hProc, DWORD clientDll, int playerNumber)
{
DWORD entityBase = getEntityBase(hProc, clientDll, playerNumber);
if (entityBase)
{
return Mem.Read<int>(hProc, (entityBase + DwHealth));
}
}
}Entity;
struct
{
DWORD getLocalPlayer(HANDLE hProc, DWORD clientDll)
{
return Mem.Read<DWORD>(hProc, (clientDll + DwLocalPlayer));
}
Vector getViewAngle(HANDLE hProc, DWORD engineDll)
{
DWORD anglePointer = Mem.Read<DWORD>(hProc, (engineDll + DwEnginePointer));
if (anglePointer)
{
Vector angles{};
angles = Mem.Read<Vector>(hProc, (anglePointer + DwViewAngle));
return angles;
}
}
int getTeam(HANDLE hProc, DWORD clientDll)
{
DWORD localPlayer = getLocalPlayer(hProc, clientDll);
if (localPlayer)
{
return Mem.Read<int>(hProc, (localPlayer + DwTeamNumber));
}
}
Vector getPosition(HANDLE hProc, DWORD clientDll)
{
DWORD playerBase = getLocalPlayer(hProc, clientDll);
if (playerBase)
{
return Mem.Read<Vector>(hProc, (playerBase + DwVecOrigin));
}
}
Vector getEyePosition(HANDLE hProc, DWORD clientDll)
{
DWORD playerBase = getLocalPlayer(hProc, clientDll);
if (playerBase)
{
return Mem.Read<Vector>(hProc, (playerBase + vecViewOffset));
}
}
}Player;
void Normalize(Vector vec)
{
if (vec.y > 180.f)
vec.y -= 360.f;
else if (vec.y < -180.f)
vec.y += 360.f;
if (vec.x > 89.f)
vec.x = 89.f;
else if (vec.y < -89.f)
vec.x = -89.f;
vec.z = 0.f;
}
Vector CalcAngle(Vector& src, Vector& dst, HANDLE hProc, DWORD clientDll)
{
Vector vAngle;
Vector delta;
delta.x = (src.x - dst.x);
delta.y = (src.y - dst.y);
delta.z = (src.z - dst.z);
Normalize(delta);
double hyp = sqrt(delta.x*delta.x + delta.y*delta.y);
vAngle.x = (float)(atan2f(delta.z, hyp) * 57.295779513082f);
vAngle.y = (float)(atanf(delta.y / delta.x) * 57.295779513082f);
vAngle.z = 0.0f;
Normalize(vAngle);
if (delta.x >= 0.0)
vAngle.y += 180.0f;
Normalize(vAngle);
return vAngle;
}
// -179 till -89 på x-axeln(pitch).
int main()
{
DWORD pId;
HWND hwnd = FindWindow(NULL, "Counter-Strike: Global Offensive");
GetWindowThreadProcessId(hwnd, &pId);
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, NULL, pId);
DWORD clientDll = getDll(pId, "client.dll");
DWORD engineDll = getDll(pId, "engine.dll");
while (true)
{
Vector localPosition{};
localPosition.x = Player.getPosition(hProc, clientDll).x + Player.getEyePosition(hProc, clientDll).x;
localPosition.y = Player.getPosition(hProc, clientDll).y + Player.getEyePosition(hProc, clientDll).y;
localPosition.z = Player.getPosition(hProc, clientDll).z + Player.getEyePosition(hProc, clientDll).z;
Vector viewAngle = Player.getViewAngle(hProc, engineDll);
cout << viewAngle.y << endl;
for (int i = 0; i < 64; ++i)
{
Vector headBone = Entity.getHeadPos(hProc, clientDll, i);
Vector vAngle = CalcAngle(localPosition, headBone, hProc, clientDll);
Vector vAnglePlus{};
Vector vAngleMinus{};
vAnglePlus.y = vAngle.y + 1.0f;
vAnglePlus.x = 0.0f;
vAnglePlus.z = 0.0f;
vAngleMinus.y = vAngle.y - 1.0f;
vAngleMinus.x = 0.0f;
vAngleMinus.z = 0.0f;
Normalize(vAnglePlus);
Normalize(vAngleMinus);
if (viewAngle.y <= vAnglePlus.y && viewAngle.y >= vAngleMinus.y && GetAsyncKeyState(0x05) && Entity.getHealth(hProc, clientDll, i) > 0 && Entity.getTeam(hProc, clientDll, i) != Player.getTeam(hProc, clientDll)) // I dont care about the elevation.
{
click();
}
}
Sleep(1); system("cls");
}
return 0;
}
void click()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, NULL, NULL, NULL, NULL);
Sleep(5);
mouse_event(MOUSEEVENTF_LEFTUP, NULL, NULL, NULL, NULL);
Sleep(10);
}
I tried with adding a clamp in normalize function. But nothing changed.