Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <Windows.h>
- #include <TlHelp32.h>
- #include <vector>
- const char* cs_module_name = "hl.exe";
- const char* hw_module_name = "hw.dll";
- std::uint32_t players_offset = 0x12041DC;
- constexpr double Rad2Deg = 180.0 / 3.141592653589793238463;
- std::uint32_t find_cs_pid()
- {
- HANDLE process_snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- if (process_snap == NULL)
- {
- throw std::exception("Process snap failed");
- }
- PROCESSENTRY32 pe{ sizeof pe };
- Process32First(process_snap, &pe);
- do
- {
- bool equal = std::string(pe.szExeFile) == cs_module_name;
- if (equal)
- {
- return pe.th32ProcessID;
- }
- } while (Process32Next(process_snap, &pe));
- return 0;
- }
- std::uint32_t find_module_base(std::uint32_t p_id, std::string module_name)
- {
- HANDLE modules_snap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, p_id);
- if (modules_snap == NULL)
- {
- throw std::exception("Can't snap modules");
- }
- MODULEENTRY32 me{ sizeof(me) };
- Module32First(modules_snap, &me);
- do
- {
- if (module_name == me.szModule)
- {
- CloseHandle(modules_snap);
- return reinterpret_cast<std::uint32_t>(me.modBaseAddr);
- }
- } while (Module32Next(modules_snap, &me));
- CloseHandle(modules_snap);
- return 0;
- }
- struct Vector3
- {
- float x;
- float z;
- float y;
- };
- struct PlayerServerInfo
- {
- Vector3 position;
- bool isTerror;
- };
- struct ReadBuffer
- {
- std::uint8_t buffer[10];
- };
- std::vector<PlayerServerInfo> find_players_position(HANDLE csProcess, std::uint32_t name_addr)
- {
- char buffer[5];
- std::vector<PlayerServerInfo> players;
- do
- {
- if (ReadProcessMemory(csProcess, reinterpret_cast<void*>(name_addr), &buffer, sizeof buffer, NULL))
- {
- if (buffer[0] == '\\' && buffer[1] == 'n')
- {
- PlayerServerInfo player;
- ReadBuffer buffer;
- if (ReadProcessMemory(csProcess, reinterpret_cast<void*>(name_addr + 388), &buffer, sizeof buffer, NULL))
- {
- std::cout << std::endl;
- }
- if (ReadProcessMemory(csProcess, reinterpret_cast<void*>(name_addr + 388), &player, sizeof player, NULL))
- {
- players.push_back(player);
- name_addr += 592;
- continue;
- }
- }
- }
- break;
- } while (true);
- return players;
- }
- Vector3 find_player_position_server(HANDLE csProcess)
- {
- const std::uint32_t position_offset = 0x108AAA8;
- std::uint32_t module_base_addr = find_module_base(GetProcessId(csProcess), hw_module_name);
- Vector3 position;
- if (ReadProcessMemory(csProcess, reinterpret_cast<void*>(module_base_addr + position_offset), &position, sizeof position, NULL))
- {
- return position;
- }
- return {};
- }
- int find_nearest_player_index(const std::vector<PlayerServerInfo>& positions, const Vector3& player_position)
- {
- float min_distance = (std::numeric_limits<float>::max)();
- int nearest_index = -1;
- for (int i = 0; i < positions.size(); i++)
- {
- auto xSqr = (positions[i].position.x - player_position.x) * (positions[i].position.x - player_position.x);
- auto zSqr = (positions[i].position.z - player_position.z) * (positions[i].position.z - player_position.z);
- if (xSqr + zSqr < min_distance)
- {
- min_distance = xSqr + zSqr;
- nearest_index = i;
- }
- }
- return nearest_index;
- }
- int main()
- {
- std::cout << atan2f(92, 126) * (180.0 / 3.141592653589793238463) << std::endl;
- std::uint32_t pid = find_cs_pid();
- HANDLE csProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
- if (csProcess == NULL)
- {
- return 1;
- }
- std::uint32_t module_base_addr = find_module_base(pid, hw_module_name);
- std::uint32_t name_addr = module_base_addr + players_offset;
- while (true)
- {
- auto positions = find_players_position(csProcess, name_addr);
- auto position = find_player_position_server(csProcess);
- int nearest_index = find_nearest_player_index(positions, position);
- if (nearest_index != -1)
- {
- Vector3 directionVector{};
- directionVector.x = positions[nearest_index].position.x - position.x;
- directionVector.y = positions[nearest_index].position.y - position.y;
- directionVector.z = positions[nearest_index].position.z - position.z;
- float yRotation = atan2f(directionVector.z, directionVector.x) * Rad2Deg;
- float xRotation = -atan2f(directionVector.y, sqrtf(directionVector.z * directionVector.z + directionVector.x * directionVector.x)) * Rad2Deg;
- if (yRotation < 0)
- {
- yRotation = 360. + yRotation;
- }
- //hw.dll+108AA88
- WriteProcessMemory(csProcess, reinterpret_cast<void*>(module_base_addr + 0x108AA88), &yRotation, sizeof yRotation, NULL);
- WriteProcessMemory(csProcess, reinterpret_cast<void*>(module_base_addr + 0x108AA84), &xRotation, sizeof xRotation, NULL);
- }
- Sleep(20);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement