CheatMenuSA/src/util.cpp

266 lines
6.1 KiB
C++
Raw Normal View History

2021-10-24 18:08:00 -04:00
#include "pch.h"
2021-10-25 10:03:27 -04:00
#include "util.h"
#include "psapi.h"
2021-07-17 03:51:02 -04:00
bool Util::IsInVehicle(CPed *pPed)
{
if (!pPed)
{
return false;
}
return BY_GAME(pPed->m_nPedFlags.bInVehicle, pPed->m_bInVehicle, pPed->m_bInVehicle);
}
void Util::FixVehicle(CVehicle *pVeh)
{
#ifdef GTASA
pVeh->Fix();
#else
switch (pVeh->m_nVehicleClass)
{
case VEHICLE_AUTOMOBILE:
{
reinterpret_cast<CAutomobile*>(pVeh)->Fix();
break;
}
#ifdef GTAVC
case VEHICLE_BIKE:
{
reinterpret_cast<CBike *>(pVeh)->Fix();
break;
}
#endif
}
#endif
pVeh->m_fHealth = 1000.0f;
}
void Util::FlipVehicle(CVehicle *pVeh)
{
#ifdef GTASA
int hveh = CPools::GetVehicleRef(pVeh);
float roll;
Command<Commands::GET_CAR_ROLL>(hveh, &roll);
roll += 180;
Command<Commands::SET_CAR_ROLL>(hveh, roll);
Command<Commands::SET_CAR_ROLL>(hveh, roll); // z rot fix
#elif GTAVC
float x,y,z;
pVeh->m_placement.GetOrientation(x, y, z);
y += 135.0f;
pVeh->m_placement.SetOrientation(x, y, z);
#else
float x,y,z;
pVeh->GetOrientation(x, y, z);
y += 135.0f;
pVeh->SetOrientation(x, y, z);
#endif
}
void Util::SetCarForwardSpeed(CVehicle *pVeh, float speed)
2021-08-01 21:41:48 -04:00
{
#ifdef GTA3
CVector inVec = pVeh->m_matrix.up;
double speedFactor = speed * 0.016766668;
pVeh->m_vecMoveSpeed.x = speedFactor * inVec.x;
pVeh->m_vecMoveSpeed.y = speedFactor * inVec.y;
pVeh->m_vecMoveSpeed.z = speedFactor * inVec.z;
#else
2022-01-21 12:33:31 -05:00
Command<Commands::SET_CAR_FORWARD_SPEED>(CPools::GetVehicleRef(pVeh), speed);
#endif
}
2021-08-09 14:26:49 -04:00
std::string Util::GetLocationName(CVector* pos)
{
2021-08-06 11:53:18 -04:00
#ifdef GTASA
2022-01-07 03:18:00 -05:00
CPlayerPed *pPlayer = FindPlayerPed();
int hplayer = CPools::GetPedRef(pPlayer);
int interior = 0;
Command<Commands::GET_AREA_VISIBLE>(&interior);
std::string town = "San Andreas";
int city;
Command<Commands::GET_CITY_PLAYER_IS_IN>(&hplayer, &city);
switch (city)
{
case 0:
town = "CS";
break;
case 1:
town = "LS";
break;
case 2:
town = "SF";
break;
case 3:
town = "LV";
break;
}
if (interior == 0)
{
return CTheZones::FindSmallestZoneForPosition(*pos, true)->GetTranslatedName() + std::string(", ") + town;
}
return std::string("Interior ") + std::to_string(interior) + ", " + town;
2021-08-06 11:53:18 -04:00
#elif GTAVC
2022-01-07 03:18:00 -05:00
return "Vice City";
2021-10-21 18:23:02 -04:00
#else
2022-01-07 03:18:00 -05:00
return "Liberty City";
2021-08-06 11:53:18 -04:00
#endif
2021-08-01 21:41:48 -04:00
}
#ifdef GTASA
void Util::ClearCharTasksVehCheck(CPed* pPed)
2020-12-02 16:19:16 -05:00
{
uint hped = CPools::GetPedRef(pPed);
2022-01-07 03:18:00 -05:00
uint hveh = NULL;
bool veh_engine = true;
float speed;
if (IsInVehicle(pPed))
2022-01-07 03:18:00 -05:00
{
hveh = CPools::GetVehicleRef(pPed->m_pVehicle);
veh_engine = pPed->m_pVehicle->m_nVehicleFlags.bEngineOn;
speed = pPed->m_pVehicle->m_vecMoveSpeed.Magnitude() * 50.0f;
2022-01-07 03:18:00 -05:00
}
Command<Commands::CLEAR_CHAR_TASKS_IMMEDIATELY>(hped);
if (hveh)
{
Command<Commands::TASK_WARP_CHAR_INTO_CAR_AS_DRIVER>(hped, hveh);
pPed->m_pVehicle->m_nVehicleFlags.bEngineOn = veh_engine;
2022-01-07 03:18:00 -05:00
Command<Commands::SET_CAR_FORWARD_SPEED>(hveh, speed);
}
2020-12-02 16:19:16 -05:00
}
bool Util::IsOnMission()
{
2022-01-07 03:18:00 -05:00
return FindPlayerPed()->CanPlayerStartMission() && !*(patch::Get<char*>(0x5D5380, false) + CTheScripts::OnAMissionFlag);
2020-12-02 16:19:16 -05:00
}
int Util::GetLargestGangInZone()
{
2022-01-07 03:18:00 -05:00
int gang_id = 0, max_density = 0;
2020-12-02 16:19:16 -05:00
2022-01-07 03:18:00 -05:00
for (int i = 0; i != 10; ++i)
{
CVector pos = FindPlayerPed()->GetPosition();
2021-02-24 16:54:45 -05:00
2022-01-07 03:18:00 -05:00
CZoneInfo* zone_info = CTheZones::GetZoneInfo(&pos, nullptr);
int density = zone_info->m_nGangDensity[i];
2021-08-06 11:53:18 -04:00
2022-01-07 03:18:00 -05:00
if (density > max_density)
{
max_density = density;
gang_id = i;
}
}
2020-12-02 16:19:16 -05:00
2022-01-07 03:18:00 -05:00
return gang_id;
2020-12-02 16:19:16 -05:00
}
2022-01-07 03:18:00 -05:00
#endif
2020-12-02 16:19:16 -05:00
// implemention of opcode 0AB5 (STORE_CLOSEST_ENTITIES)
2020-12-02 16:19:16 -05:00
// https://github.com/cleolibrary/CLEO4/blob/916d400f4a731ba1dd0ff16e52bdb056f42b7038/source/CCustomOpcodeSystem.cpp#L1671
2021-10-25 10:03:27 -04:00
CVehicle* Util::GetClosestVehicle()
2020-12-02 16:19:16 -05:00
{
2022-01-07 03:18:00 -05:00
CPlayerPed* player = FindPlayerPed();
2021-08-09 14:26:49 -04:00
#ifdef GTASA
2022-01-07 03:18:00 -05:00
CPedIntelligence* pedintel;
if (player && (pedintel = player->m_pIntelligence))
{
CVehicle* veh = nullptr;
for (int i = 0; i < 16; i++)
{
veh = static_cast<CVehicle*>(pedintel->m_vehicleScanner.m_apEntities[i]);
if (veh && !veh->m_nVehicleFlags.bFadeOut)
break;
veh = nullptr;
}
return veh;
}
return nullptr;
#else
CVehicle *pClosestVeh = nullptr;
float distance = 999.0f;
CVector playerPos = player->GetPosition();
for (CVehicle *pVeh : CPools::ms_pVehiclePool)
{
CVector pos = pVeh->GetPosition();
float dist = DistanceBetweenPoints(playerPos, pos);
if (dist < distance)
{
pClosestVeh = pVeh;
distance = dist;
}
}
return pClosestVeh;
2021-08-09 14:26:49 -04:00
#endif
2020-12-02 16:19:16 -05:00
}
2021-10-25 10:03:27 -04:00
CPed* Util::GetClosestPed()
{
2022-01-07 03:18:00 -05:00
CPlayerPed* player = FindPlayerPed();
2021-08-09 14:26:49 -04:00
#ifdef GTASA
2022-01-07 03:18:00 -05:00
CPedIntelligence* pedintel;
if (player && (pedintel = player->m_pIntelligence))
{
CPed* ped = nullptr;
for (int i = 0; i < 16; i++)
{
ped = static_cast<CPed*>(pedintel->m_pedScanner.m_apEntities[i]);
if (ped && ped != player && (ped->m_nCreatedBy & 0xFF) == 1 && !ped->m_nPedFlags.bFadeOut)
break;
ped = nullptr;
}
return ped;
}
return nullptr;
#else
return player->m_apNearPeds[0];
2021-08-09 14:26:49 -04:00
#endif
}
bool Util::IsOnCutscene()
{
2022-01-07 03:18:00 -05:00
return BY_GAME(CCutsceneMgr::ms_running, *(bool*)0xA10AB2, *(bool*)0x95CCF5);
}
2021-10-25 10:03:27 -04:00
void Util::RainbowValues(int& r, int& g, int& b, float speed)
2020-12-02 16:19:16 -05:00
{
2022-01-07 03:18:00 -05:00
int timer = CTimer::m_snTimeInMilliseconds / 150;
r = sin(timer * speed) * 127 + 128;
g = sin(timer * speed + 2) * 127 + 128;
b = sin(timer * speed + 4) * 127 + 128;
}
void Util::GetCPUUsageInit()
{
2022-01-07 03:18:00 -05:00
PdhOpenQuery(nullptr, NULL, &cpuQuery);
PdhAddEnglishCounter(cpuQuery, "\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PdhCollectQueryData(cpuQuery);
}
double Util::GetCurrentCPUUsage()
{
2022-01-07 03:18:00 -05:00
PDH_FMT_COUNTERVALUE counterVal;
2022-01-07 03:18:00 -05:00
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, nullptr, &counterVal);
return counterVal.doubleValue;
2021-06-18 12:49:11 -04:00
}