From 3768473cd653f919ba044701b01091e7b230bbda Mon Sep 17 00:00:00 2001 From: Sergeanur Date: Thu, 10 Oct 2019 12:20:53 +0300 Subject: Full CRestart --- src/control/Restart.cpp | 262 ++++++++++++++++++++++++++++++++++++++++++++++-- src/control/Restart.h | 23 ++++- 2 files changed, 276 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/control/Restart.cpp b/src/control/Restart.cpp index 40b95c23..c82b3f1b 100644 --- a/src/control/Restart.cpp +++ b/src/control/Restart.cpp @@ -1,14 +1,262 @@ #include "common.h" #include "patcher.h" #include "Restart.h" +#include "Zones.h" +#include "PathFind.h" -bool &CRestart::OverrideHospitalLevel = *(bool*)0x95CD4C; -bool &CRestart::OverridePoliceStationLevel = *(bool*)0x95CD50; +uint8 &CRestart::OverrideHospitalLevel = *(uint8*)0x95CD4C; +uint8 &CRestart::OverridePoliceStationLevel = *(uint8*)0x95CD50; bool &CRestart::bFadeInAfterNextArrest = *(bool*)0x95CD69; bool &CRestart::bFadeInAfterNextDeath = *(bool*)0x95CD9D; -WRAPPER void CRestart::AddHospitalRestartPoint(const CVector&, float) { EAXJMP(0x436100); } -WRAPPER void CRestart::AddPoliceRestartPoint(const CVector&, float) { EAXJMP(0x436150); } -WRAPPER void CRestart::OverrideNextRestart(const CVector&, float) { EAXJMP(0x4366C0); } -WRAPPER void CRestart::FindClosestHospitalRestartPoint(const CVector &, CVector *, float *) { EAXJMP(0x4361A0); } -WRAPPER void CRestart::FindClosestPoliceRestartPoint(const CVector &, CVector *, float *) { EAXJMP(0x436450); } \ No newline at end of file +bool &CRestart::bOverrideRestart = *(bool*)0x95CD5D; +CVector &CRestart::OverridePosition = *(CVector*)0x8E2C00; +float &CRestart::OverrideHeading = *(float*)0x8F2A18; + +CVector(&CRestart::HospitalRestartPoints)[NUM_RESTART_POINTS] = *(CVector(*)[NUM_RESTART_POINTS])*(uintptr*)0x87F9B0; +float(&CRestart::HospitalRestartHeadings)[NUM_RESTART_POINTS] = *(float(*)[NUM_RESTART_POINTS])*(uintptr*)0x6F1D40; +uint16 &CRestart::NumberOfHospitalRestarts = *(uint16*)0x95CC46; + +CVector(&CRestart::PoliceRestartPoints)[NUM_RESTART_POINTS] = *(CVector(*)[NUM_RESTART_POINTS])*(uintptr*)0x846228; +float(&CRestart::PoliceRestartHeadings)[NUM_RESTART_POINTS] = *(float(*)[NUM_RESTART_POINTS])*(uintptr*)0x6F1D20; +uint16 &CRestart::NumberOfPoliceRestarts = *(uint16*)0x95CC44; + +void +CRestart::Initialise() +{ + OverridePoliceStationLevel = LEVEL_NONE; + OverrideHospitalLevel = LEVEL_NONE; + bFadeInAfterNextArrest = true; + bFadeInAfterNextDeath = true; + OverrideHeading = 0.0f; + OverridePosition = CVector(0.0f, 0.0f, 0.0f); + bOverrideRestart = false; + NumberOfPoliceRestarts = 0; + NumberOfHospitalRestarts = 0; + + for (int i = 0; i < NUM_RESTART_POINTS; i++) { + HospitalRestartPoints[i] = CVector(0.0f, 0.0f, 0.0f); + HospitalRestartHeadings[0] = 0.0f; + PoliceRestartPoints[i] = CVector(0.0f, 0.0f, 0.0f); + PoliceRestartHeadings[0] = 0.0f; + } +} + +void +CRestart::AddHospitalRestartPoint(const CVector &pos, float heading) +{ + HospitalRestartPoints[NumberOfHospitalRestarts] = pos; + HospitalRestartHeadings[NumberOfHospitalRestarts++] = heading; +} + +void +CRestart::AddPoliceRestartPoint(const CVector &pos, float heading) +{ + PoliceRestartPoints[NumberOfPoliceRestarts] = pos; + PoliceRestartHeadings[NumberOfPoliceRestarts++] = heading; +} + +void +CRestart::OverrideNextRestart(const CVector &pos, float heading) +{ + bOverrideRestart = true; + OverridePosition = pos; + OverrideHeading = heading; +} + +void +CRestart::CancelOverrideRestart() +{ + bOverrideRestart = false; +} + +void +CRestart::FindClosestHospitalRestartPoint(const CVector &pos, CVector *outPos, float *outHeading) +{ + if (bOverrideRestart) { + *outPos = OverridePosition; + *outHeading = OverrideHeading; + CancelOverrideRestart(); + return; + } + + eLevelName curlevel = CTheZones::FindZoneForPoint(pos); + float fMinDist = 16000000.0f; + int closestPoint = NUM_RESTART_POINTS; + + // find closest point on this level + for (int i = 0; i < NumberOfHospitalRestarts; i++) { + if (CTheZones::FindZoneForPoint(HospitalRestartPoints[i]) == (OverrideHospitalLevel != LEVEL_NONE ? OverrideHospitalLevel : curlevel)) { + float dist = (pos - HospitalRestartPoints[i]).MagnitudeSqr(); + if (fMinDist >= dist) { + fMinDist = dist; + closestPoint = i; + } + } + } + + // if we didn't find anything, find closest point on any level + if (closestPoint == NUM_RESTART_POINTS) { + for (int i = 0; i < NumberOfHospitalRestarts; i++) { + float dist = (pos - HospitalRestartPoints[i]).MagnitudeSqr(); + if (fMinDist >= dist) { + fMinDist = dist; + closestPoint = i; + } + } + } + + // if we still didn't find anything, find closest path node + if (closestPoint == NUM_RESTART_POINTS) { + *outPos = ThePaths.m_pathNodes[ThePaths.FindNodeClosestToCoors(pos, PATH_PED, 999999.9f)].pos; + *outHeading = 0.0f; + printf("Couldn't find a hospital restart zone near the player %f %f %f->%f %f %f\n", pos.x, pos.y, pos.z, outPos->x, outPos->y, outPos->z); + } else { + *outPos = HospitalRestartPoints[closestPoint]; + *outHeading = HospitalRestartHeadings[closestPoint]; + } +} + +void +CRestart::FindClosestPoliceRestartPoint(const CVector &pos, CVector *outPos, float *outHeading) +{ + if (bOverrideRestart) { + *outPos = OverridePosition; + *outHeading = OverrideHeading; + CancelOverrideRestart(); + return; + } + + eLevelName curlevel = CTheZones::FindZoneForPoint(pos); + float fMinDist = 16000000.0f; + int closestPoint = NUM_RESTART_POINTS; + + // find closest point on this level + for (int i = 0; i < NumberOfPoliceRestarts; i++) { + if (CTheZones::FindZoneForPoint(PoliceRestartPoints[i]) == (OverridePoliceStationLevel != LEVEL_NONE ? OverridePoliceStationLevel : curlevel)) { + float dist = (pos - PoliceRestartPoints[i]).MagnitudeSqr(); + if (fMinDist >= dist) { + fMinDist = dist; + closestPoint = i; + } + } + } + + // if we didn't find anything, find closest point on any level + if (closestPoint == NUM_RESTART_POINTS) { + for (int i = 0; i < NumberOfPoliceRestarts; i++) { + float dist = (pos - PoliceRestartPoints[i]).MagnitudeSqr(); + if (fMinDist >= dist) { + fMinDist = dist; + closestPoint = i; + } + } + } + + // if we still didn't find anything, find closest path node + if (closestPoint == NUM_RESTART_POINTS) { + printf("Couldn't find a police restart zone near the player\n"); + *outPos = ThePaths.m_pathNodes[ThePaths.FindNodeClosestToCoors(pos, PATH_PED, 999999.9f)].pos; + *outHeading = 0.0f; + } else { + *outPos = PoliceRestartPoints[closestPoint]; + *outHeading = PoliceRestartHeadings[closestPoint]; + } +} + +void +CRestart::LoadAllRestartPoints(uint8 *buf, uint32 size) +{ + Initialise(); + +INITSAVEBUF + CheckSaveHeader(buf, 'R','S','T','\0', size - SAVE_HEADER_SIZE); + + for (int i = 0; i < NUM_RESTART_POINTS; i++) { + HospitalRestartPoints[i] = ReadSaveBuf(buf); + HospitalRestartHeadings[i] = ReadSaveBuf(buf); + } + + for (int i = 0; i < NUM_RESTART_POINTS; i++) { + PoliceRestartPoints[i] = ReadSaveBuf(buf); + PoliceRestartHeadings[i] = ReadSaveBuf(buf); + } + + NumberOfHospitalRestarts = ReadSaveBuf(buf); + NumberOfPoliceRestarts = ReadSaveBuf(buf); + bOverrideRestart = ReadSaveBuf(buf); + + // skip something unused + ReadSaveBuf(buf); + ReadSaveBuf(buf); + + OverridePosition = ReadSaveBuf(buf); + OverrideHeading = ReadSaveBuf(buf); + bFadeInAfterNextDeath = ReadSaveBuf(buf); + bFadeInAfterNextArrest = ReadSaveBuf(buf); + OverrideHospitalLevel = ReadSaveBuf(buf); + OverridePoliceStationLevel = ReadSaveBuf(buf); +VALIDATESAVEBUF(size); +} + +void +CRestart::SaveAllRestartPoints(uint8 *buf, uint32 *size) +{ + *size = SAVE_HEADER_SIZE + + sizeof(HospitalRestartPoints) + + sizeof(HospitalRestartHeadings) + + sizeof(PoliceRestartPoints) + + sizeof(PoliceRestartHeadings) + + sizeof(NumberOfHospitalRestarts) + + sizeof(NumberOfPoliceRestarts) + + sizeof(bOverrideRestart) + + sizeof(uint8) + + sizeof(uint16) + + sizeof(OverridePosition) + + sizeof(OverrideHeading) + + sizeof(bFadeInAfterNextDeath) + + sizeof(bFadeInAfterNextArrest) + + sizeof(OverrideHospitalLevel) + + sizeof(OverridePoliceStationLevel); // == 292 + +INITSAVEBUF + WriteSaveHeader(buf, 'R','S','T','\0', *size - SAVE_HEADER_SIZE); + + for (int i = 0; i < NUM_RESTART_POINTS; i++) { + WriteSaveBuf(buf, HospitalRestartPoints[i]); + WriteSaveBuf(buf, HospitalRestartHeadings[i]); + } + + for (int i = 0; i < NUM_RESTART_POINTS; i++) { + WriteSaveBuf(buf, PoliceRestartPoints[i]); + WriteSaveBuf(buf, PoliceRestartHeadings[i]); + } + + WriteSaveBuf(buf, NumberOfHospitalRestarts); + WriteSaveBuf(buf, NumberOfPoliceRestarts); + WriteSaveBuf(buf, bOverrideRestart); + + WriteSaveBuf(buf, (uint8)0); + WriteSaveBuf(buf, (uint16)0); + + WriteSaveBuf(buf, OverridePosition); + WriteSaveBuf(buf, OverrideHeading); + WriteSaveBuf(buf, bFadeInAfterNextDeath); + WriteSaveBuf(buf, bFadeInAfterNextArrest); + WriteSaveBuf(buf, OverrideHospitalLevel); + WriteSaveBuf(buf, OverridePoliceStationLevel); +VALIDATESAVEBUF(*size); +} + + +STARTPATCHES + InjectHook(0x435E20, &CRestart::Initialise, PATCH_JUMP); + InjectHook(0x436100, &CRestart::AddHospitalRestartPoint, PATCH_JUMP); + InjectHook(0x436150, &CRestart::AddPoliceRestartPoint, PATCH_JUMP); + InjectHook(0x4366C0, &CRestart::OverrideNextRestart, PATCH_JUMP); + InjectHook(0x4366F0, &CRestart::CancelOverrideRestart, PATCH_JUMP); + InjectHook(0x4361A0, &CRestart::FindClosestHospitalRestartPoint, PATCH_JUMP); + InjectHook(0x436450, &CRestart::FindClosestPoliceRestartPoint, PATCH_JUMP); + InjectHook(0x436B20, &CRestart::LoadAllRestartPoints, PATCH_JUMP); + InjectHook(0x436700, &CRestart::SaveAllRestartPoints, PATCH_JUMP); +ENDPATCHES \ No newline at end of file diff --git a/src/control/Restart.h b/src/control/Restart.h index 9a4d2155..fb7806db 100644 --- a/src/control/Restart.h +++ b/src/control/Restart.h @@ -1,5 +1,7 @@ #pragma once +#define NUM_RESTART_POINTS 8 + class CRestart { public: @@ -9,9 +11,26 @@ public: static void FindClosestHospitalRestartPoint(const CVector &, CVector *, float *); static void FindClosestPoliceRestartPoint(const CVector &, CVector *, float *); + static void Initialise(); + static void CancelOverrideRestart(); + + static void LoadAllRestartPoints(uint8 *buf, uint32 size); + static void SaveAllRestartPoints(uint8 *buf, uint32 *size); - static bool &OverrideHospitalLevel; - static bool &OverridePoliceStationLevel; + static uint8 &OverrideHospitalLevel; + static uint8 &OverridePoliceStationLevel; static bool &bFadeInAfterNextArrest; static bool &bFadeInAfterNextDeath; + + static bool &bOverrideRestart; + static CVector &OverridePosition; + static float &OverrideHeading; + + static CVector(&HospitalRestartPoints)[NUM_RESTART_POINTS]; + static float (&HospitalRestartHeadings)[NUM_RESTART_POINTS]; + static uint16 &NumberOfHospitalRestarts; + + static CVector (&PoliceRestartPoints)[NUM_RESTART_POINTS]; + static float (&PoliceRestartHeadings)[NUM_RESTART_POINTS]; + static uint16 &NumberOfPoliceRestarts; }; -- cgit v1.2.3 From 03833f9e11521fc97eedcf95f4cc3d2cdca1db3c Mon Sep 17 00:00:00 2001 From: Sergeanur Date: Sat, 12 Oct 2019 17:28:38 +0300 Subject: Fixed iteration typo --- src/control/Restart.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/control/Restart.cpp b/src/control/Restart.cpp index c82b3f1b..8e983555 100644 --- a/src/control/Restart.cpp +++ b/src/control/Restart.cpp @@ -36,9 +36,9 @@ CRestart::Initialise() for (int i = 0; i < NUM_RESTART_POINTS; i++) { HospitalRestartPoints[i] = CVector(0.0f, 0.0f, 0.0f); - HospitalRestartHeadings[0] = 0.0f; + HospitalRestartHeadings[i] = 0.0f; PoliceRestartPoints[i] = CVector(0.0f, 0.0f, 0.0f); - PoliceRestartHeadings[0] = 0.0f; + PoliceRestartHeadings[i] = 0.0f; } } -- cgit v1.2.3