Skip to content

Commit

Permalink
Update main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
subugt authored Jul 24, 2024
1 parent 3c299d3 commit b1da94c
Showing 1 changed file with 59 additions and 37 deletions.
96 changes: 59 additions & 37 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#include <iostream>
#ifdef _WIN32
#include <Windows.h>
#endif
#include <string>
#include <sstream>
#include <cmath>
// added support for linux too ( just for github )
#ifdef _WIN32
#include <Windows.h>
#endif

const DWORD CAR_X_ADDRESS = 0x0018BDF8; // x adress found via cheat engine.
const DWORD CAR_Y_ADDRESS = 0x0018C078; // y adress found via cheat engine.
#ifdef _WIN32
const DWORD CAR_X_ADDRESS = 0x0018BDF8;
const DWORD CAR_Y_ADDRESS = 0x0018C078;
#else
const uintptr_t CAR_X_ADDRESS = 0x0018BDF8;
const uintptr_t CAR_Y_ADDRESS = 0x0018C078;
#endif

#ifdef _WIN32
float readFloatFromMemory(HANDLE hProcess, DWORD address) {
float value = 0;
SIZE_T bytesRead;
Expand All @@ -22,57 +29,77 @@ void sendKeyPress(UINT key, int duration) {
keybd_event(key, 0, KEYEVENTF_KEYUP, 0);
}

void setConsoleAlwaysOnTop() {
HWND hwndConsole = GetConsoleWindow();
if (hwndConsole != NULL) {
SetWindowPos(hwndConsole, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
}
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <fcntl.h>
#include <cstring>

float readFloatFromMemory(uintptr_t address) {
return 0.0f;
}

void sendKeyPress(int key, int duration) {
}

void setConsoleAlwaysOnTop() {
}
#endif

void updateDisplay(float carPosX, float carPosY, const std::string& action) {
std::stringstream ss;
ss << "Car Position (X, Y): (" << carPosX << ", " << carPosY << ")\n";
ss << "Action: " << action;


system("cls"); // clear console
std::cout << "\033[2J\033[1;1H";
std::cout << ss.str();
}

void controlCar(HANDLE hProcess) {
void controlCar() {
const float POSITION_TOLERANCE = 1.0f;
const int GAS_PRESS_DURATION = 800; // ms
const int REVERSE_PRESS_DURATION = 500; // ms
const int MANEUVER_PRESS_DURATION = 500; // ms
const int CHECK_INTERVAL = 5000; // ms
const int GAS_PRESS_DURATION = 800;
const int REVERSE_PRESS_DURATION = 500;
const int MANEUVER_PRESS_DURATION = 500;
const int CHECK_INTERVAL = 5000;

float lastPosX = readFloatFromMemory(hProcess, CAR_X_ADDRESS);
float lastPosY = readFloatFromMemory(hProcess, CAR_Y_ADDRESS);
float lastPosX = readFloatFromMemory(CAR_X_ADDRESS);
float lastPosY = readFloatFromMemory(CAR_Y_ADDRESS);
bool isReversing = false;
bool lastManeuverLeft = false;
DWORD lastCheckTime = GetTickCount();
unsigned long lastCheckTime = 0;

while (true) {
float currentPosX = readFloatFromMemory(hProcess, CAR_X_ADDRESS);
float currentPosY = readFloatFromMemory(hProcess, CAR_Y_ADDRESS);
float currentPosX = readFloatFromMemory(CAR_X_ADDRESS);
float currentPosY = readFloatFromMemory(CAR_Y_ADDRESS);

DWORD currentTime = GetTickCount();
unsigned long currentTime = std::time(nullptr) * 1000;
if (currentTime - lastCheckTime >= CHECK_INTERVAL) {
if (fabs(currentPosX - lastPosX) > POSITION_TOLERANCE || fabs(currentPosY - lastPosY) > POSITION_TOLERANCE) {
if (isReversing) {
isReversing = false;
updateDisplay(currentPosX, currentPosY, "Applying Gas (W)");
sendKeyPress('W', GAS_PRESS_DURATION);
}
else {
} else {
updateDisplay(currentPosX, currentPosY, "Applying Gas (W)");
sendKeyPress('W', GAS_PRESS_DURATION);
}
lastPosX = currentPosX;
lastPosY = currentPosY;
}
else {
} else {
if (!isReversing) {
isReversing = true;
updateDisplay(currentPosX, currentPosY, "Reversing and Maneuvering (Z, A, D)");

if (lastManeuverLeft) {
sendKeyPress('D', MANEUVER_PRESS_DURATION);
}
else {
} else {
sendKeyPress('A', MANEUVER_PRESS_DURATION);
}

Expand All @@ -82,31 +109,23 @@ void controlCar(HANDLE hProcess) {

if (lastManeuverLeft) {
sendKeyPress('A', MANEUVER_PRESS_DURATION);
}
else {
} else {
sendKeyPress('D', MANEUVER_PRESS_DURATION);
}

sendKeyPress('W', GAS_PRESS_DURATION);
lastCheckTime = GetTickCount();
lastCheckTime = std::time(nullptr) * 1000;
}
}
}
Sleep(100);
}
}

void setConsoleAlwaysOnTop() {
HWND hwndConsole = GetConsoleWindow();
if (hwndConsole != NULL) {
SetWindowPos(hwndConsole, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
usleep(100000);
}
}

int main() {

setConsoleAlwaysOnTop(); // Set console to always on top
setConsoleAlwaysOnTop();

#ifdef _WIN32
DWORD processID;
HWND hwnd = FindWindow(NULL, L"Need for Speed™ Most Wanted");
if (hwnd == NULL) {
Expand All @@ -124,6 +143,9 @@ int main() {
controlCar(hProcess);

CloseHandle(hProcess);
#else
controlCar();
#endif

return 0;
}

0 comments on commit b1da94c

Please sign in to comment.