2020-12-02 16:19:16 -05:00
|
|
|
#include "pch.h"
|
2021-09-20 08:41:40 -04:00
|
|
|
#include "util.h"
|
2021-06-15 09:30:48 -04:00
|
|
|
#include "psapi.h"
|
2021-07-17 03:51:02 -04:00
|
|
|
|
2021-08-01 21:41:48 -04:00
|
|
|
std::string Util::GetLocationName(CVector* pos)
|
|
|
|
{
|
2021-08-09 14:26:49 -04:00
|
|
|
CPlayerPed *pPlayer = FindPlayerPed();
|
|
|
|
|
2021-08-06 11:53:18 -04:00
|
|
|
#ifdef GTASA
|
2021-08-09 14:26:49 -04:00
|
|
|
int hplayer = CPools::GetPedRef(pPlayer);
|
2021-08-01 21:41:48 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-08-06 11:53:18 -04:00
|
|
|
case 0:
|
|
|
|
town = "CS";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
town = "LS";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
town = "SF";
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
town = "LV";
|
|
|
|
break;
|
2021-08-01 21:41:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-08-09 14:26:49 -04:00
|
|
|
if (pPlayer)
|
|
|
|
{
|
|
|
|
return std::to_string(pPlayer->m_nInterior) + ", Vice City" ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "Vice City";
|
|
|
|
}
|
2021-08-06 11:53:18 -04:00
|
|
|
#endif
|
2021-08-01 21:41:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GTASA
|
2021-07-17 03:51:02 -04:00
|
|
|
// Thanks DKPac22
|
|
|
|
RwTexture* Util::LoadTextureFromMemory(char* data, unsigned int size)
|
|
|
|
{
|
|
|
|
patch::SetChar(0x7CF9CA, rwSTREAMMEMORY);
|
|
|
|
RwMemory memoryImage;
|
|
|
|
RwInt32 width, height, depth, flags;
|
|
|
|
memoryImage.start = (RwUInt8*)data;
|
|
|
|
memoryImage.length = size;
|
|
|
|
RwImage* image = RtPNGImageRead((char*)&memoryImage);
|
|
|
|
RwImageFindRasterFormat(image, 4, &width, &height, &depth, &flags);
|
|
|
|
RwRaster* raster = RwRasterCreate(width, height, depth, flags);
|
|
|
|
RwRasterSetFromImage(raster, image);
|
|
|
|
RwImageDestroy(image);
|
|
|
|
patch::SetChar(0x7CF9CA, rwSTREAMFILENAME);
|
|
|
|
|
|
|
|
return RwTextureCreate(raster);
|
|
|
|
}
|
2020-12-02 16:19:16 -05:00
|
|
|
|
|
|
|
void Util::ClearCharTasksVehCheck(CPed* ped)
|
|
|
|
{
|
|
|
|
uint hped = CPools::GetPedRef(ped);
|
|
|
|
uint hveh = NULL;
|
2020-12-09 08:15:50 -05:00
|
|
|
bool veh_engine = true;
|
2020-12-02 16:19:16 -05:00
|
|
|
|
2021-02-15 18:58:02 -05:00
|
|
|
if (ped->m_nPedFlags.bInVehicle)
|
2020-12-09 08:15:50 -05:00
|
|
|
{
|
2020-12-02 16:19:16 -05:00
|
|
|
hveh = CPools::GetVehicleRef(ped->m_pVehicle);
|
2020-12-09 08:15:50 -05:00
|
|
|
veh_engine = ped->m_pVehicle->m_nVehicleFlags.bEngineOn;
|
|
|
|
}
|
2020-12-02 16:19:16 -05:00
|
|
|
|
|
|
|
Command<Commands::CLEAR_CHAR_TASKS_IMMEDIATELY>(hped);
|
|
|
|
|
|
|
|
if (hveh)
|
2020-12-09 08:15:50 -05:00
|
|
|
{
|
2020-12-02 16:19:16 -05:00
|
|
|
Command<Commands::TASK_WARP_CHAR_INTO_CAR_AS_DRIVER>(hped, hveh);
|
2020-12-09 08:15:50 -05:00
|
|
|
ped->m_pVehicle->m_nVehicleFlags.bEngineOn = veh_engine;
|
|
|
|
}
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Util::IsOnMission()
|
|
|
|
{
|
2021-06-18 12:49:11 -04:00
|
|
|
return FindPlayerPed()->CanPlayerStartMission() && !*(patch::Get<char*>(0x5D5380, false) +
|
|
|
|
CTheScripts::OnAMissionFlag);
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int Util::GetLargestGangInZone()
|
|
|
|
{
|
|
|
|
int gang_id = 0, max_density = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i != 10; ++i)
|
|
|
|
{
|
|
|
|
CVector pos = FindPlayerPed()->GetPosition();
|
2021-02-24 16:54:45 -05:00
|
|
|
|
2021-07-09 15:44:34 -04:00
|
|
|
CZoneExtraInfo* zone_info = CTheZones::GetZoneInfo(&pos, nullptr);
|
2020-12-02 16:19:16 -05:00
|
|
|
int density = zone_info->m_nGangDensity[i];
|
2021-08-06 11:53:18 -04:00
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
if (density > max_density)
|
|
|
|
{
|
|
|
|
max_density = density;
|
|
|
|
gang_id = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gang_id;
|
|
|
|
}
|
2021-08-09 14:26:49 -04:00
|
|
|
#endif
|
2020-12-02 16:19:16 -05:00
|
|
|
|
2021-01-03 06:48:07 -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-01-03 06:48:07 -05:00
|
|
|
CVehicle* Util::GetClosestVehicle()
|
2020-12-02 16:19:16 -05:00
|
|
|
{
|
2021-08-09 14:26:49 -04:00
|
|
|
CPlayerPed* player = FindPlayerPed();
|
|
|
|
|
|
|
|
#ifdef GTASA
|
2021-02-24 16:54:45 -05:00
|
|
|
CPedIntelligence* pedintel;
|
2020-12-02 16:19:16 -05:00
|
|
|
if (player && (pedintel = player->m_pIntelligence))
|
|
|
|
{
|
2021-02-24 16:54:45 -05:00
|
|
|
CVehicle* veh = nullptr;
|
2020-12-02 16:19:16 -05:00
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
{
|
2021-06-18 12:49:11 -04:00
|
|
|
veh = static_cast<CVehicle*>(pedintel->m_vehicleScanner.m_apEntities[i]);
|
2020-12-02 16:19:16 -05:00
|
|
|
if (veh && !veh->m_nVehicleFlags.bFadeOut)
|
|
|
|
break;
|
|
|
|
veh = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return veh;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2021-08-09 14:26:49 -04:00
|
|
|
#elif GTAVC
|
|
|
|
|
|
|
|
CVehicle *pClosestVeh = nullptr;
|
2021-08-10 01:18:26 -04:00
|
|
|
float distance = 999.0f;
|
2021-08-09 14:26:49 -04:00
|
|
|
|
|
|
|
CVector playerPos = player->GetPosition();
|
|
|
|
|
|
|
|
for (CVehicle *pVeh : CPools::ms_pVehiclePool)
|
|
|
|
{
|
|
|
|
CVector pos = pVeh->GetPosition();
|
|
|
|
float dist = DistanceBetweenPoints(playerPos, pos);
|
|
|
|
|
2021-09-23 02:56:23 -04:00
|
|
|
if (dist < distance)
|
2021-08-09 14:26:49 -04:00
|
|
|
{
|
|
|
|
pClosestVeh = pVeh;
|
|
|
|
distance = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pClosestVeh;
|
|
|
|
#endif
|
2020-12-02 16:19:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:48:07 -05:00
|
|
|
CPed* Util::GetClosestPed()
|
|
|
|
{
|
2021-08-09 14:26:49 -04:00
|
|
|
CPlayerPed* player = FindPlayerPed();
|
|
|
|
#ifdef GTASA
|
2021-02-24 16:54:45 -05:00
|
|
|
CPedIntelligence* pedintel;
|
2021-01-03 06:48:07 -05:00
|
|
|
if (player && (pedintel = player->m_pIntelligence))
|
|
|
|
{
|
2021-02-24 16:54:45 -05:00
|
|
|
CPed* ped = nullptr;
|
2021-01-03 06:48:07 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
{
|
2021-06-18 12:49:11 -04:00
|
|
|
ped = static_cast<CPed*>(pedintel->m_pedScanner.m_apEntities[i]);
|
2021-01-03 06:48:07 -05:00
|
|
|
if (ped && ped != player && (ped->m_nCreatedBy & 0xFF) == 1 && !ped->m_nPedFlags.bFadeOut)
|
|
|
|
break;
|
|
|
|
ped = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ped;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2021-08-09 14:26:49 -04:00
|
|
|
#elif GTAVC
|
|
|
|
return player->m_apNearPeds[0];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Util::IsOnCutscene()
|
|
|
|
{
|
|
|
|
#ifdef GTASA
|
|
|
|
return CCutsceneMgr::ms_running;
|
|
|
|
#elif GTAVC
|
|
|
|
return *(bool*)0xA10AB2; // CCutsceneMgr::ms_running
|
|
|
|
#endif
|
2021-01-03 06:48:07 -05:00
|
|
|
}
|
|
|
|
|
2021-02-24 16:54:45 -05:00
|
|
|
void Util::RainbowValues(int& r, int& g, int& b, float speed)
|
2020-12-02 16:19:16 -05:00
|
|
|
{
|
2021-02-24 16:54:45 -05:00
|
|
|
int timer = CTimer::m_snTimeInMilliseconds / 150;
|
2020-12-02 16:19:16 -05:00
|
|
|
r = sin(timer * speed) * 127 + 128;
|
|
|
|
g = sin(timer * speed + 2) * 127 + 128;
|
|
|
|
b = sin(timer * speed + 4) * 127 + 128;
|
2020-12-25 16:22:28 -05:00
|
|
|
}
|
|
|
|
|
2021-06-15 09:30:48 -04:00
|
|
|
void Util::GetCPUUsageInit()
|
|
|
|
{
|
2021-06-18 12:49:11 -04:00
|
|
|
PdhOpenQuery(nullptr, NULL, &cpuQuery);
|
2021-06-15 09:30:48 -04:00
|
|
|
PdhAddEnglishCounter(cpuQuery, "\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
|
|
|
|
PdhCollectQueryData(cpuQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Util::GetCurrentCPUUsage()
|
|
|
|
{
|
|
|
|
PDH_FMT_COUNTERVALUE counterVal;
|
|
|
|
|
|
|
|
PdhCollectQueryData(cpuQuery);
|
2021-06-18 12:49:11 -04:00
|
|
|
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, nullptr, &counterVal);
|
2021-06-15 09:30:48 -04:00
|
|
|
return counterVal.doubleValue;
|
2021-06-18 12:49:11 -04:00
|
|
|
}
|