From 917fa80c18e83172040870d1a1cf613b51cf628c Mon Sep 17 00:00:00 2001 From: Nikolay Korolev Date: Fri, 10 Apr 2020 11:03:38 +0300 Subject: [PATCH 1/6] record init --- src/control/Record.cpp | 178 ++++++++++++++++++++++++++++++++++++++++- src/control/Record.h | 36 ++++++++- src/core/Pad.cpp | 12 ++- src/core/Streaming.cpp | 4 +- src/core/Timer.cpp | 2 +- 5 files changed, 222 insertions(+), 10 deletions(-) diff --git a/src/control/Record.cpp b/src/control/Record.cpp index 7c330311..7a3763be 100644 --- a/src/control/Record.cpp +++ b/src/control/Record.cpp @@ -2,12 +2,184 @@ #include "patcher.h" #include "Record.h" +#include "FileMgr.h" +#include "Pad.h" +#include "Pools.h" +#include "Timer.h" + uint16 &CRecordDataForGame::RecordingState = *(uint16*)0x95CC24; +uint8*& CRecordDataForGame::pDataBuffer = *(uint8**)0x8F1B70; +uint8*& CRecordDataForGame::pDataBufferPointer = *(uint8**)0x8F1AB0; +int& CRecordDataForGame::FId = *(int*)0x885BA4; +tGameBuffer& CRecordDataForGame::pDataBufferForFrame = *(tGameBuffer*)0x72CED0; uint8 &CRecordDataForChase::Status = *(uint8*)0x95CDCE; -WRAPPER void CRecordDataForGame::SaveOrRetrieveDataForThisFrame(void) { EAXJMP(0x4341F0); } -WRAPPER void CRecordDataForGame::Init(void) { EAXJMP(0x4340F0); } +#define MEMORY_FOR_GAME_RECORD (150000) + +void CRecordDataForGame::Init(void) +{ + RecordingState = STATE_NONE; + if (pDataBuffer) + delete[] pDataBuffer; + pDataBufferPointer = nil; + pDataBuffer = nil; +#ifndef GTA_PS2 // this stuff is not present on PS2 + FId = CFileMgr::OpenFile("playback.dat", "r"); + if (FId <= 0) { + if ((FId = CFileMgr::OpenFile("record.dat", "r")) <= 0) + RecordingState = STATE_NONE; + else { + CFileMgr::CloseFile(FId); + FId = CFileMgr::OpenFileForWriting("record.dat"); + RecordingState = STATE_RECORD; + } + } + else { + RecordingState = STATE_PLAYBACK; + } + if (RecordingState == STATE_PLAYBACK) { + pDataBufferPointer = (uint8*)malloc(MEMORY_FOR_GAME_RECORD); + pDataBuffer = pDataBufferPointer; + pDataBuffer[CFileMgr::Read(FId, (char*)pDataBufferPointer, MEMORY_FOR_GAME_RECORD) + 8] = -1; + CFileMgr::CloseFile(FId); + } +#else + RecordingState = STATE_NONE; // second time to make sure +#endif +} + +void CRecordDataForGame::SaveOrRetrieveDataForThisFrame(void) +{ + switch (RecordingState) { + case STATE_RECORD: + { + pDataBufferForFrame.m_fTimeStep = CTimer::GetTimeStep(); + pDataBufferForFrame.m_nTimeInMilliseconds = CTimer::GetTimeInMilliseconds(); + pDataBufferForFrame.m_nSizeOfPads[0] = 0; + pDataBufferForFrame.m_nSizeOfPads[1] = 0; + pDataBufferForFrame.m_nChecksum = CalcGameChecksum(); + uint8* pController1 = PackCurrentPadValues(pDataBufferForFrame.m_ControllerBuffer, &CPad::GetPad(0)->OldState, &CPad::GetPad(0)->NewState); + pDataBufferForFrame.m_nSizeOfPads[0] = (pController1 - pDataBufferForFrame.m_ControllerBuffer) / 2; + uint8* pController2 = PackCurrentPadValues(pController1, &CPad::GetPad(1)->OldState, &CPad::GetPad(1)->NewState); + pDataBufferForFrame.m_nSizeOfPads[1] = (pController2 - pController1) / 2; + uint8* pEndPtr = pController2; + if ((pDataBufferForFrame.m_nSizeOfPads[0] + pDataBufferForFrame.m_nSizeOfPads[1]) & 1) + pEndPtr += 2; + CFileMgr::Write(FId, (char*)&pDataBufferForFrame, pEndPtr - (uint8*)&pDataBufferForFrame); + break; + } + case STATE_PLAYBACK: + if (pDataBufferPointer[8] == -1) + CPad::GetPad(0)->NewState.Clear(); + else { + tGameBuffer* pData = (tGameBuffer*)pDataBufferPointer; + CTimer::SetTimeInMilliseconds(pData->m_nTimeInMilliseconds); + CTimer::SetTimeStep(pData->m_fTimeStep); + uint8 size1 = pData->m_nSizeOfPads[0]; + uint8 size2 = pData->m_nSizeOfPads[1]; + pDataBufferPointer = (uint8*)&pData->m_ControllerBuffer; + pDataBufferPointer = UnPackCurrentPadValues(pDataBufferPointer, size1, &CPad::GetPad(0)->NewState); + pDataBufferPointer = UnPackCurrentPadValues(pDataBufferPointer, size2, &CPad::GetPad(1)->NewState); + if ((size1 + size2) & 1) + pDataBufferPointer += 2; + if (pData->m_nChecksum != CalcGameChecksum()) + printf("Playback out of sync\n"); + } + } +} + +#define PROCESS_BUTTON_STATE_STORE(buf, os, ns, field, id) \ + do { \ + if (os->field != os->field){ \ + *buf++ = id; \ + *buf++ = ns->field; \ + } \ + } while (0); + +uint8* CRecordDataForGame::PackCurrentPadValues(uint8* buf, CControllerState* os, CControllerState* ns) +{ + PROCESS_BUTTON_STATE_STORE(buf, os, ns, LeftStickX, 0); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, LeftStickY, 1); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, LeftShoulder1, 2); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, RightStickY, 3); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, LeftShoulder1, 4); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, LeftShoulder2, 5); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, RightShoulder1, 6); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, RightShoulder2, 7); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, DPadUp, 8); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, DPadDown, 9); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, DPadLeft, 10); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, DPadRight, 11); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, Start, 12); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, Select, 13); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, Square, 14); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, Triangle, 15); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, Cross, 16); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, Circle, 17); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, LeftShock, 18); + PROCESS_BUTTON_STATE_STORE(buf, os, ns, RightShock, 19); + return buf; +} +#undef PROCESS_BUTTON_STATE_STORE + +#define PROCESS_BUTTON_STATE_RESTORE(buf, state, field, id) case id: state->field = *buf++; break; + +uint8* CRecordDataForGame::UnPackCurrentPadValues(uint8* buf, uint8 total, CControllerState* state) +{ + for (uint8 i = 0; i < total; i++) { + switch (*buf++) { + PROCESS_BUTTON_STATE_RESTORE(buf, state, LeftStickX, 0); + PROCESS_BUTTON_STATE_RESTORE(buf, state, LeftStickY, 1); + PROCESS_BUTTON_STATE_RESTORE(buf, state, LeftShoulder1, 2); + PROCESS_BUTTON_STATE_RESTORE(buf, state, RightStickY, 3); + PROCESS_BUTTON_STATE_RESTORE(buf, state, LeftShoulder1, 4); + PROCESS_BUTTON_STATE_RESTORE(buf, state, LeftShoulder2, 5); + PROCESS_BUTTON_STATE_RESTORE(buf, state, RightShoulder1, 6); + PROCESS_BUTTON_STATE_RESTORE(buf, state, RightShoulder2, 7); + PROCESS_BUTTON_STATE_RESTORE(buf, state, DPadUp, 8); + PROCESS_BUTTON_STATE_RESTORE(buf, state, DPadDown, 9); + PROCESS_BUTTON_STATE_RESTORE(buf, state, DPadLeft, 10); + PROCESS_BUTTON_STATE_RESTORE(buf, state, DPadRight, 11); + PROCESS_BUTTON_STATE_RESTORE(buf, state, Start, 12); + PROCESS_BUTTON_STATE_RESTORE(buf, state, Select, 13); + PROCESS_BUTTON_STATE_RESTORE(buf, state, Square, 14); + PROCESS_BUTTON_STATE_RESTORE(buf, state, Triangle, 15); + PROCESS_BUTTON_STATE_RESTORE(buf, state, Cross, 16); + PROCESS_BUTTON_STATE_RESTORE(buf, state, Circle, 17); + PROCESS_BUTTON_STATE_RESTORE(buf, state, LeftShock, 18); + PROCESS_BUTTON_STATE_RESTORE(buf, state, RightShock, 19); + } + } + return buf; +} + +#undef PROCESS_BUTTON_STATE_RESTORE + +uint16 CRecordDataForGame::CalcGameChecksum(void) +{ + uint32 checksum = 0; + int i = CPools::GetPedPool()->GetSize(); + while (i--) { + CPed* pPed = CPools::GetPedPool()->GetSlot(i); + if (!pPed) + continue; + checksum ^= pPed->GetModelIndex() ^ *(uint32*)&pPed->GetPosition().z ^ *(uint32*)&pPed->GetPosition().y ^ *(uint32*)&pPed->GetPosition().x; + } + i = CPools::GetVehiclePool()->GetSize(); + while (i--) { + CVehicle* pVehicle = CPools::GetVehiclePool()->GetSlot(i); + if (!pVehicle) + continue; + checksum ^= pVehicle->GetModelIndex() ^ *(uint32*)&pVehicle->GetPosition().z ^ *(uint32*)&pVehicle->GetPosition().y ^ *(uint32*)&pVehicle->GetPosition().x; + } + return checksum ^ checksum >> 16; +} + +WRAPPER void CRecordDataForChase::Init(void) +{ + EAXJMP(0x434780); +} WRAPPER void CRecordDataForChase::SaveOrRetrieveDataForThisFrame(void) { EAXJMP(0x4347F0); } WRAPPER void CRecordDataForChase::ProcessControlCars(void) { EAXJMP(0x435540); } @@ -16,4 +188,4 @@ WRAPPER void CRecordDataForChase::StartChaseScene(float) { EAXJMP(0x435690); } WRAPPER void CRecordDataForChase::CleanUpChaseScene() { EAXJMP(0x4357C0); } WRAPPER void CRecordDataForChase::RemoveCarFromChase(int32) { EAXJMP(0x435BC0); } WRAPPER CVehicle* CRecordDataForChase::TurnChaseCarIntoScriptCar(int32) { EAXJMP(0x435C00); } -WRAPPER void CRecordDataForChase::Init(void) { EAXJMP(0x434780); } + diff --git a/src/control/Record.h b/src/control/Record.h index e52a623e..3cb09318 100644 --- a/src/control/Record.h +++ b/src/control/Record.h @@ -1,6 +1,8 @@ #pragma once +class CAutomobile; class CVehicle; +class CControllerState; enum { RECORDSTATE_0, @@ -10,8 +12,15 @@ enum { class CRecordDataForChase { + enum { + NUM_CHASE_CARS = 20 + }; public: static uint8 &Status; + static int &PositionChanges; + static uint8 &CurrentCar; + static CAutomobile*(&pChaseCars)[NUM_CHASE_CARS]; + static uint32 &AnimStartTime; static void SaveOrRetrieveDataForThisFrame(void); static void ProcessControlCars(void); @@ -23,12 +32,37 @@ public: static void Init(void); }; +struct tGameBuffer +{ + float m_fTimeStep; + uint32 m_nTimeInMilliseconds; + uint8 m_nSizeOfPads[2]; + uint16 m_nChecksum; + uint8 m_ControllerBuffer[116]; +}; class CRecordDataForGame { + enum { + STATE_NONE = 0, + STATE_RECORD = 1, + STATE_PLAYBACK = 2, + }; + static uint16& RecordingState; + static uint8* &pDataBuffer; + static uint8* &pDataBufferPointer; + static int &FId; + static tGameBuffer &pDataBufferForFrame; + public: - static uint16 &RecordingState; + static bool IsRecording() { return RecordingState == STATE_RECORD; } + static bool IsPlayingBack() { return RecordingState == STATE_PLAYBACK; } static void SaveOrRetrieveDataForThisFrame(void); static void Init(void); + +private: + static uint16 CalcGameChecksum(void); + static uint8* PackCurrentPadValues(uint8*, CControllerState*, CControllerState*); + static uint8* UnPackCurrentPadValues(uint8*, uint8, CControllerState*); }; diff --git a/src/core/Pad.cpp b/src/core/Pad.cpp index 6bbe00f2..ff7b2fb7 100644 --- a/src/core/Pad.cpp +++ b/src/core/Pad.cpp @@ -21,6 +21,7 @@ #include "Hud.h" #include "Text.h" #include "Timer.h" +#include "Record.h" #include "World.h" #include "Vehicle.h" #include "Ped.h" @@ -657,9 +658,14 @@ void CPad::Update(int16 unk) { OldState = NewState; - NewState = ReconcileTwoControllersInput(PCTempKeyState, PCTempJoyState); - NewState = ReconcileTwoControllersInput(PCTempMouseState, NewState); - +#if (defined GTA_PS2 || defined FIX_BUGS) + if (!CRecordDataForGame::IsPlayingBack() /* && !CRecordDataForChase::ShouldThisPadBeLeftAlone(unk) */) +#endif + { + NewState = ReconcileTwoControllersInput(PCTempKeyState, PCTempJoyState); + NewState = ReconcileTwoControllersInput(PCTempMouseState, NewState); + } + PCTempJoyState.Clear(); PCTempKeyState.Clear(); PCTempMouseState.Clear(); diff --git a/src/core/Streaming.cpp b/src/core/Streaming.cpp index 3dcb767a..d00edf51 100644 --- a/src/core/Streaming.cpp +++ b/src/core/Streaming.cpp @@ -1247,8 +1247,8 @@ CStreaming::StreamVehiclesAndPeds(void) static int timeBeforeNextLoad = 0; static int modelQualityClass = 0; - if(CRecordDataForGame::RecordingState == RECORDSTATE_1 || - CRecordDataForGame::RecordingState == RECORDSTATE_2) + if(CRecordDataForGame::IsRecording() || + CRecordDataForGame::IsPlayingBack()) return; if(FindPlayerPed()->m_pWanted->AreSwatRequired()){ diff --git a/src/core/Timer.cpp b/src/core/Timer.cpp index 18d6b6a3..3cd6862c 100644 --- a/src/core/Timer.cpp +++ b/src/core/Timer.cpp @@ -130,7 +130,7 @@ void CTimer::Update(void) ms_fTimeStepNonClipped = ms_fTimeStep; - if ( CRecordDataForGame::RecordingState != RECORDSTATE_2 ) + if ( !CRecordDataForGame::IsPlayingBack() ) { ms_fTimeStep = min(3.0f, ms_fTimeStep); From 92b6d775d67d01aab0706564b2cae3714bcf05b0 Mon Sep 17 00:00:00 2001 From: Nikolay Korolev Date: Sat, 11 Apr 2020 02:03:42 +0300 Subject: [PATCH 2/6] Record --- src/control/Record.cpp | 355 ++++++++++++++++++++++++++++++++++-- src/control/Record.h | 54 +++++- src/core/Pad.cpp | 3 +- src/core/Timer.cpp | 2 +- src/vehicles/Automobile.cpp | 2 +- 5 files changed, 394 insertions(+), 22 deletions(-) diff --git a/src/control/Record.cpp b/src/control/Record.cpp index 7a3763be..19fd8969 100644 --- a/src/control/Record.cpp +++ b/src/control/Record.cpp @@ -5,7 +5,10 @@ #include "FileMgr.h" #include "Pad.h" #include "Pools.h" +#include "Streaming.h" #include "Timer.h" +#include "VehicleModelInfo.h" +#include "World.h" uint16 &CRecordDataForGame::RecordingState = *(uint16*)0x95CC24; uint8*& CRecordDataForGame::pDataBuffer = *(uint8**)0x8F1B70; @@ -13,8 +16,6 @@ uint8*& CRecordDataForGame::pDataBufferPointer = *(uint8**)0x8F1AB0; int& CRecordDataForGame::FId = *(int*)0x885BA4; tGameBuffer& CRecordDataForGame::pDataBufferForFrame = *(tGameBuffer*)0x72CED0; -uint8 &CRecordDataForChase::Status = *(uint8*)0x95CDCE; - #define MEMORY_FOR_GAME_RECORD (150000) void CRecordDataForGame::Init(void) @@ -176,16 +177,348 @@ uint16 CRecordDataForGame::CalcGameChecksum(void) return checksum ^ checksum >> 16; } -WRAPPER void CRecordDataForChase::Init(void) +uint8& CRecordDataForChase::Status = *(uint8*)0x95CDCE; +int& CRecordDataForChase::PositionChanges = *(int*)0x8F59C8; +uint8& CRecordDataForChase::CurrentCar = *(uint8*)0x95CDC9; +CAutomobile* (&CRecordDataForChase::pChaseCars)[NUM_CHASE_CARS] = *(CAutomobile * (*)[NUM_CHASE_CARS])*(uintptr*)0x6F46A8; +uint32& CRecordDataForChase::AnimStartTime = *(uint32*)0x8F1AEC; +float& CRecordDataForChase::AnimTime = *(float*)0x880F88; +CCarStateEachFrame* (&CRecordDataForChase::pBaseMemForCar)[NUM_CHASE_CARS] = *(CCarStateEachFrame * (*)[NUM_CHASE_CARS])*(uintptr*)0x70EA18; +float& CRecordDataForChase::TimeMultiplier = *(float*)0x8E2A94; +int& CRecordDataForChase::FId2 = *(int*)0x8E2C18; + +#define CHASE_SCENE_LENGTH_IN_SECONDS (80) +#define CHASE_SCENE_FRAMES_PER_SECOND (15) // skipping every second frame +#define CHASE_SCENE_FRAMES_IN_RECORDING (CHASE_SCENE_LENGTH_IN_SECONDS * CHASE_SCENE_FRAMES_PER_SECOND) +#define CHASE_SCENE_LENGTH_IN_FRAMES (CHASE_SCENE_FRAMES_IN_RECORDING * 2) + +void CRecordDataForChase::Init(void) { - EAXJMP(0x434780); + Status = STATE_NONE; + PositionChanges = 0; + CurrentCar = 0; + for (int i = 0; i < NUM_CHASE_CARS; i++) + pChaseCars[i] = nil; + AnimStartTime = 0; } -WRAPPER void CRecordDataForChase::SaveOrRetrieveDataForThisFrame(void) { EAXJMP(0x4347F0); } -WRAPPER void CRecordDataForChase::ProcessControlCars(void) { EAXJMP(0x435540); } -WRAPPER void CRecordDataForChase::SaveOrRetrieveCarPositions(void) { EAXJMP(0x434B20); } -WRAPPER void CRecordDataForChase::StartChaseScene(float) { EAXJMP(0x435690); } -WRAPPER void CRecordDataForChase::CleanUpChaseScene() { EAXJMP(0x4357C0); } -WRAPPER void CRecordDataForChase::RemoveCarFromChase(int32) { EAXJMP(0x435BC0); } -WRAPPER CVehicle* CRecordDataForChase::TurnChaseCarIntoScriptCar(int32) { EAXJMP(0x435C00); } +void CRecordDataForChase::SaveOrRetrieveDataForThisFrame(void) +{ + switch (Status) { + case STATE_NONE: + return; + case STATE_RECORD: + { + if ((CTimer::GetFrameCounter() & 1) == 0) + StoreInfoForCar(pChaseCars[CurrentCar], &pBaseMemForCar[CurrentCar][CTimer::GetFrameCounter() / 2]); + if (CTimer::GetFrameCounter() < CHASE_SCENE_LENGTH_IN_FRAMES * 2) + return; + CFileMgr::SetDir("data\\paths"); + sprintf(gString, "chase%d.dat", CurrentCar); + int fid = CFileMgr::OpenFileForWriting(gString); + uint32 fs = CHASE_SCENE_LENGTH_IN_FRAMES * sizeof(CCarStateEachFrame); + printf("FileSize:%d\n", fs); + CFileMgr::Write(fid, (char*)pBaseMemForCar[CurrentCar], fs); + CFileMgr::CloseFile(fid); + CFileMgr::SetDir(""); + sprintf(gString, "car%d.max", CurrentCar); + int fid2 = CFileMgr::OpenFileForWriting(gString); + for (int i = 0; i < CHASE_SCENE_FRAMES_IN_RECORDING; i++) { + // WTF? Was it ever used? +#ifdef FIX_BUGS + CCarStateEachFrame* pState = pBaseMemForCar[CurrentCar]; +#else + CCarStateEachFrame* pState = (CCarStateEachFrame*)pChaseCars[CurrentCar]; +#endif + CVector right = CVector(pState->rightX, pState->rightY, pState->rightZ) / (UINT8_MAX / 2); + CVector forward = CVector(pState->forwardX, pState->forwardY, pState->forwardZ) / (UINT8_MAX / 2); + CVector up = CrossProduct(right, forward); + sprintf(gString, "%f %f %f\n", pState->pos.x, pState->pos.y, pState->pos.z); + CFileMgr::Write(fid2, gString, strlen(gString) - 1); + sprintf(gString, "%f %f %f\n", right.x, right.y, right.z); + CFileMgr::Write(fid2, gString, strlen(gString) - 1); + sprintf(gString, "%f %f %f\n", forward.x, forward.y, forward.z); + CFileMgr::Write(fid2, gString, strlen(gString) - 1); + sprintf(gString, "%f %f %f\n", up.x, up.y, up.z); + CFileMgr::Write(fid2, gString, strlen(gString) - 1); + } + CFileMgr::CloseFile(fid2); + } + case STATE_PLAYBACK: + case STATE_PLAYBACK_BEFORE_RECORDING: + case STATE_PLAYBACK_INIT: + break; + } +} + +struct tCoors { + CVector pos; + float angle; +}; + +// I guess developer was filling this with actual data before running the game +tCoors NewCoorsForRecordedCars[7]; + +void CRecordDataForChase::SaveOrRetrieveCarPositions(void) +{ + switch (Status) { + case STATE_NONE: + return; + case STATE_RECORD: + case STATE_PLAYBACK_BEFORE_RECORDING: + for (int i = 0; i < NUM_CHASE_CARS; i++) { + if (i != CurrentCar && CTimer::GetFrameCounter()) { + RestoreInfoForCar(pChaseCars[i], &pBaseMemForCar[i][CTimer::GetFrameCounter() / 2], false); + pChaseCars[i]->GetMatrix().UpdateRW(); + pChaseCars[i]->UpdateRwFrame(); + } + } + if (Status == STATE_PLAYBACK_BEFORE_RECORDING && CTimer::GetFrameCounter()) { + RestoreInfoForCar(pChaseCars[CurrentCar], &pBaseMemForCar[CurrentCar][CTimer::GetFrameCounter() / 2], false); + pChaseCars[CurrentCar]->GetMatrix().UpdateRW(); + pChaseCars[CurrentCar]->UpdateRwFrame(); + } + if (CPad::GetPad(0)->GetLeftShockJustDown() && CPad::GetPad(0)->GetRightShockJustDown()) { + if (!CPad::GetPad(0)->GetRightShockJustDown()) { + pChaseCars[CurrentCar]->GetPosition() = NewCoorsForRecordedCars[PositionChanges].pos; + pChaseCars[CurrentCar]->SetMoveSpeed(0.0f, 0.0f, 0.0f); + pChaseCars[CurrentCar]->GetMatrix().SetRotateZOnly(DEGTORAD(NewCoorsForRecordedCars[PositionChanges].angle)); + ++PositionChanges; + } + if (Status == STATE_PLAYBACK_BEFORE_RECORDING) { + Status = STATE_RECORD; + pChaseCars[CurrentCar]->m_status = STATUS_PLAYER; + } + } + break; + case STATE_PLAYBACK_INIT: + Status = STATE_PLAYBACK; + break; + case STATE_PLAYBACK: + { + TimeMultiplier += CTimer::GetTimeStepNonClippedInSeconds(); + float EndOfFrameTime = CHASE_SCENE_FRAMES_PER_SECOND * min(CHASE_SCENE_LENGTH_IN_SECONDS, TimeMultiplier); + for (int i = 0; i < NUM_CHASE_CARS; i++) { + if (!pBaseMemForCar[i]) + continue; + if (!pChaseCars[i]) + continue; + if (EndOfFrameTime < CHASE_SCENE_FRAMES_IN_RECORDING - 1) { + int FlooredEOFTime = EndOfFrameTime; + RestoreInfoForCar(pChaseCars[i], &pBaseMemForCar[i][FlooredEOFTime], false); + CMatrix tmp; + float dp = EndOfFrameTime - FlooredEOFTime; + RestoreInfoForMatrix(tmp, &pBaseMemForCar[i][FlooredEOFTime + 1]); + pChaseCars[i]->GetRight() += (tmp.GetRight() - pChaseCars[i]->GetRight()) * dp; + pChaseCars[i]->GetForward() += (tmp.GetForward() - pChaseCars[i]->GetForward()) * dp; + pChaseCars[i]->GetUp() += (tmp.GetUp() - pChaseCars[i]->GetUp()) * dp; + pChaseCars[i]->GetPosition() += (tmp.GetPosition() - pChaseCars[i]->GetPosition()) * dp; + } + else{ + RestoreInfoForCar(pChaseCars[i], &pBaseMemForCar[i][CHASE_SCENE_FRAMES_IN_RECORDING - 1], true); + if (i == 0) + pChaseCars[i]->GetPosition().z += 0.2f; + } + pChaseCars[i]->GetMatrix().UpdateRW(); + pChaseCars[i]->UpdateRwFrame(); + pChaseCars[i]->RemoveAndAdd(); + } + break; + } + } +} + +void CRecordDataForChase::StoreInfoForCar(CAutomobile* pCar, CCarStateEachFrame* pState) +{ + pState->rightX = UINT8_MAX / 2 * pCar->GetRight().x; + pState->rightY = UINT8_MAX / 2 * pCar->GetRight().y; + pState->rightZ = UINT8_MAX / 2 * pCar->GetRight().z; + pState->forwardX = UINT8_MAX / 2 * pCar->GetForward().x; + pState->forwardY = UINT8_MAX / 2 * pCar->GetForward().y; + pState->forwardZ = UINT8_MAX / 2 * pCar->GetForward().z; + pState->pos = pCar->GetPosition(); + pState->velX = 1.0f * (UINT16_MAX / 2) * pCar->GetMoveSpeed().x; + pState->velY = 1.0f * (UINT16_MAX / 2) * pCar->GetMoveSpeed().y; + pState->velZ = 1.0f * (UINT16_MAX / 2) * pCar->GetMoveSpeed().z; + pState->wheel = 20 * pCar->m_fSteerAngle; + pState->gas = 100 * pCar->m_fGasPedal; + pState->brake = 100 * pCar->m_fBrakePedal; + pState->handbrake = pCar->bIsHandbrakeOn; +} + +void CRecordDataForChase::RestoreInfoForMatrix(CMatrix& matrix, CCarStateEachFrame* pState) +{ + matrix.GetRight() = CVector(pState->rightX, pState->rightY, pState->rightZ) / (UINT8_MAX / 2); + matrix.GetForward() = CVector(pState->forwardX, pState->forwardY, pState->forwardZ) / (UINT8_MAX / 2); + matrix.GetUp() = CrossProduct(matrix.GetRight(), matrix.GetForward()); + matrix.GetPosition() = pState->pos; +} + +void CRecordDataForChase::RestoreInfoForCar(CAutomobile* pCar, CCarStateEachFrame* pState, bool stop) +{ + CVector oldPos = pCar->GetPosition(); + RestoreInfoForMatrix(pCar->GetMatrix(), pState); + pCar->SetMoveSpeed(CVector(pState->velX, pState->velY, pState->velZ) * 1.0f / (UINT16_MAX / 2)); + pCar->SetTurnSpeed(0.0f, 0.0f, 0.0f); + pCar->m_fSteerAngle = pState->wheel / 20.0f; + pCar->m_fGasPedal = pState->gas / 100.0f; + pCar->m_fBrakePedal = pState->brake / 100.0f; + pCar->bIsHandbrakeOn = pState->handbrake; + if ((oldPos - pCar->GetPosition()).Magnitude() > 15.0f) { + if (pCar == pChaseCars[14]) { + pCar->m_currentColour1 = 58; + pCar->m_currentColour2 = 1; + } + else + pCar->GetModelInfo()->ChooseVehicleColour(pCar->m_currentColour1, pCar->m_currentColour2); + } + pCar->m_fHealth = min(pCar->m_fHealth, 500.0f); + if (stop) { + pCar->m_fGasPedal = 0.0f; + pCar->m_fBrakePedal = 0.0f; + pCar->SetMoveSpeed(0.0f, 0.0f, 0.0f); + pCar->bIsHandbrakeOn = false; + } +} + +void CRecordDataForChase::ProcessControlCars(void) +{ + if (Status != STATE_PLAYBACK) + return; + for (int i = 0; i < NUM_CHASE_CARS; i++) { + if (pChaseCars[i]) + pChaseCars[i]->ProcessControl(); + } +} + +#if (defined(GTA_PS2) || defined(FIX_BUGS)) +bool CRecordDataForChase::ShouldThisPadBeLeftAlone(uint8 pad) +{ + // may be wrong + if (Status == STATE_NONE || Status == STATE_PLAYBACK) + return false; + return pad != 0; +} +#endif + +void CRecordDataForChase::GiveUsACar(int32 mi, CVector pos, float angle, CAutomobile** ppCar, uint8 colour1, uint8 colour2) +{ + CStreaming::RequestModel(mi, STREAMFLAGS_DEPENDENCY); + CStreaming::LoadAllRequestedModels(false); + if (!CStreaming::HasModelLoaded(mi)) + return; + CAutomobile* pCar = new CAutomobile(mi, MISSION_VEHICLE); + pCar->GetPosition() = pos; + pCar->m_status = STATUS_PLAYER_PLAYBACKFROMBUFFER; + pCar->GetMatrix().SetRotateZOnly(DEGTORAD(angle)); + pCar->pDriver = nil; + pCar->m_currentColour1 = colour1; + pCar->m_currentColour2 = colour2; + CWorld::Add(pCar); + *ppCar = pCar; +} + +void RemoveUnusedCollision(void) +{ + static const char* dontDeleteArray[] = { + "rd_SrRoad2A50", "rd_SrRoad2A20", "rd_CrossRda1w22", "rd_CrossRda1rw22", + "road_broadway02", "road_broadway01", "com_21way5", "com_21way50", + "cm1waycrosscom", "com_21way20", "com_21way10", "road_broadway04", + "com_rvroads52", "com_roadsrv", "com_roadkb23", "com_roadkb22" + }; + for (int i = 0; i < ARRAY_SIZE(dontDeleteArray); i++) + CModelInfo::GetModelInfo(dontDeleteArray[i], nil)->GetColModel()->level = LEVEL_NONE; + CModelInfo::RemoveColModelsFromOtherLevels(LEVEL_NONE); + for (int i = 0; i < ARRAY_SIZE(dontDeleteArray); i++) + CModelInfo::GetModelInfo(dontDeleteArray[i], nil)->GetColModel()->level = LEVEL_COMMERCIAL; +} + +void CRecordDataForChase::StartChaseScene(float startTime) +{ + char filename[28]; + SetUpCarsForChaseScene(); + Status = STATE_PLAYBACK; + AnimTime = startTime; + AnimStartTime = CTimer::GetTimeInMilliseconds(); + RemoveUnusedCollision(); + CStreaming::RemoveIslandsNotUsed(LEVEL_SUBURBAN); + CGame::TidyUpMemory(true, true); + CStreaming::ImGonnaUseStreamingMemory(); + CFileMgr::SetDir("data\\paths"); + for (int i = 0; i < NUM_CHASE_CARS; i++) { + if (!pChaseCars[i]) { + pBaseMemForCar[i] = nil; + continue; + } + sprintf(filename, "chase%d.dat", i); + FId2 = CFileMgr::OpenFile(filename, "rb"); + if (FId2 <= 0) { + pBaseMemForCar[i] = nil; + continue; + } + pBaseMemForCar[i] = new CCarStateEachFrame[CHASE_SCENE_FRAMES_IN_RECORDING]; + for (int j = 0; j < CHASE_SCENE_FRAMES_IN_RECORDING; j++) { + CFileMgr::Read(FId2, (char*)&pBaseMemForCar[i][j], sizeof(CCarStateEachFrame)); + CFileMgr::Seek(FId2, sizeof(CCarStateEachFrame), 1); + } + CFileMgr::CloseFile(FId2); + } + CFileMgr::SetDir(""); + CStreaming::IHaveUsedStreamingMemory(); + TimeMultiplier = 0.0f; +} + +void CRecordDataForChase::CleanUpChaseScene(void) +{ + if (Status != STATE_PLAYBACK_INIT && Status != STATE_PLAYBACK) + return; + Status = STATE_NONE; + CleanUpCarsForChaseScene(); + for (int i = 0; i < NUM_CHASE_CARS; i++) { + if (pBaseMemForCar[i]) { + delete[] pBaseMemForCar[i]; + pBaseMemForCar[i] = nil; + } + } +} + +void CRecordDataForChase::SetUpCarsForChaseScene(void) +{ + GiveUsACar(MI_POLICE, CVector(273.54221f, -1167.1907f, 24.880601f), 63.0f, &pChaseCars[0], 2, 1); + GiveUsACar(MI_ENFORCER, CVector(231.1783f, -1388.8322f, 25.978201f), 90.0f, &pChaseCars[1], 2, 1); + GiveUsACar(MI_TAXI, CVector(184.3156f, -1473.251f, 25.978201f), 0.0f, &pChaseCars[4], 6, 6); + GiveUsACar(MI_CHEETAH, CVector(173.8868f, -1377.6514f, 25.978201f), 0.0f, &pChaseCars[6], 4, 5); + GiveUsACar(MI_STINGER, CVector(102.5946f, -943.93628f, 25.9781f), 270.0f, &pChaseCars[7], 53, 53); + GiveUsACar(MI_CHEETAH, CVector(-177.7157f, -862.18652f, 25.978201f), 155.0f, &pChaseCars[10], 41, 1); + GiveUsACar(MI_STINGER, CVector(-170.56979f, -889.02362f, 25.978201f), 154.0f, &pChaseCars[11], 10, 10); + GiveUsACar(MI_KURUMA, CVector(402.60809f, -917.49628f, 37.381001f), 90.0f, &pChaseCars[14], 34, 1); + GiveUsACar(MI_TAXI, CVector(-33.496201f, -938.4563f, 25.9781f), 266.0f, &pChaseCars[16], 6, 6); + GiveUsACar(MI_KURUMA, CVector(49.363098f, -987.60498f, 25.9781f), 0.0f, &pChaseCars[18], 51, 1); + GiveUsACar(MI_TAXI, CVector(179.0049f, -1154.6686f, 25.9781f), 0.0f, &pChaseCars[19], 6, 76); + GiveUsACar(MI_RUMPO, CVector(-28.9762f, -1031.3367f, 25.990601f), 242.0f, &pChaseCars[2], 1, 75); + GiveUsACar(MI_PATRIOT, CVector(114.1564f, -796.69379f, 24.978201f), 180.0f, &pChaseCars[3], 0, 0); +} + +void CRecordDataForChase::CleanUpCarsForChaseScene(void) +{ + for (int i = 0; i < NUM_CHASE_CARS; i++) + RemoveCarFromChase(i); +} + +void CRecordDataForChase::RemoveCarFromChase(int32 i) +{ + if (!pChaseCars[i]) + return; + CWorld::Remove(pChaseCars[i]); + delete pChaseCars[i]; + pChaseCars[i] = nil; +} + +CVehicle* CRecordDataForChase::TurnChaseCarIntoScriptCar(int32 i) +{ + CVehicle* pVehicle = pChaseCars[i]; + pChaseCars[i] = nil; + pVehicle->m_status = STATUS_PHYSICS; + return pVehicle; +} diff --git a/src/control/Record.h b/src/control/Record.h index 3cb09318..4abeb68a 100644 --- a/src/control/Record.h +++ b/src/control/Record.h @@ -4,32 +4,70 @@ class CAutomobile; class CVehicle; class CControllerState; -enum { - RECORDSTATE_0, - RECORDSTATE_1, - RECORDSTATE_2, +class CCarStateEachFrame +{ +public: + int16 velX; + int16 velY; + int16 velZ; + int8 rightX; + int8 rightY; + int8 rightZ; + int8 forwardX; + int8 forwardY; + int8 forwardZ; + int8 wheel; + uint8 gas; + uint8 brake; + bool handbrake; + CVector pos; }; +extern char* gString; + class CRecordDataForChase { enum { NUM_CHASE_CARS = 20 }; -public: + enum { + STATE_NONE = 0, + STATE_RECORD = 1, + STATE_PLAYBACK_INIT = 2, + STATE_PLAYBACK = 3, + STATE_PLAYBACK_BEFORE_RECORDING = 4 + }; static uint8 &Status; static int &PositionChanges; static uint8 &CurrentCar; static CAutomobile*(&pChaseCars)[NUM_CHASE_CARS]; + static float &AnimTime; static uint32 &AnimStartTime; + static CCarStateEachFrame* (&pBaseMemForCar)[NUM_CHASE_CARS]; + static float &TimeMultiplier; + static int &FId2; +public: + static bool IsRecording(void) { return Status == STATE_RECORD; } + + static void Init(void); static void SaveOrRetrieveDataForThisFrame(void); - static void ProcessControlCars(void); static void SaveOrRetrieveCarPositions(void); + static void StoreInfoForCar(CAutomobile*, CCarStateEachFrame*); + static void RestoreInfoForMatrix(CMatrix&, CCarStateEachFrame*); + static void RestoreInfoForCar(CAutomobile*, CCarStateEachFrame*, bool); + static void ProcessControlCars(void); +#if (defined(GTA_PS2) || defined(FIX_BUGS)) + static bool ShouldThisPadBeLeftAlone(uint8 pad); +#endif + static void GiveUsACar(int32, CVector, float, CAutomobile**, uint8, uint8); static void StartChaseScene(float); - static void CleanUpChaseScene(); + static void CleanUpChaseScene(void); + static void SetUpCarsForChaseScene(void); + static void CleanUpCarsForChaseScene(void); static void RemoveCarFromChase(int32); static CVehicle* TurnChaseCarIntoScriptCar(int32); - static void Init(void); + }; struct tGameBuffer diff --git a/src/core/Pad.cpp b/src/core/Pad.cpp index b03272f9..f83998b8 100644 --- a/src/core/Pad.cpp +++ b/src/core/Pad.cpp @@ -26,6 +26,7 @@ #include "Vehicle.h" #include "Ped.h" #include "Population.h" +#include "Record.h" #include "Replay.h" #include "Weather.h" #include "win.h" @@ -969,7 +970,7 @@ void CPad::Update(int16 unk) OldState = NewState; #if (defined GTA_PS2 || defined FIX_BUGS) - if (!CRecordDataForGame::IsPlayingBack() /* && !CRecordDataForChase::ShouldThisPadBeLeftAlone(unk) */) + if (!CRecordDataForGame::IsPlayingBack() && !CRecordDataForChase::ShouldThisPadBeLeftAlone(unk)) #endif { NewState = ReconcileTwoControllersInput(PCTempKeyState, PCTempJoyState); diff --git a/src/core/Timer.cpp b/src/core/Timer.cpp index 3cd6862c..d0bb6c8b 100644 --- a/src/core/Timer.cpp +++ b/src/core/Timer.cpp @@ -138,7 +138,7 @@ void CTimer::Update(void) m_snTimeInMilliseconds = m_snPreviousTimeInMilliseconds + 60; } - if ( CRecordDataForChase::Status == RECORDSTATE_1 ) + if ( CRecordDataForChase::IsRecording() ) { ms_fTimeStep = 1.0f; m_snTimeInMilliseconds = m_snPreviousTimeInMilliseconds + 16; diff --git a/src/vehicles/Automobile.cpp b/src/vehicles/Automobile.cpp index 1c0d673e..13558920 100644 --- a/src/vehicles/Automobile.cpp +++ b/src/vehicles/Automobile.cpp @@ -356,7 +356,7 @@ CAutomobile::ProcessControl(void) PruneReferences(); - if(m_status == STATUS_PLAYER && CRecordDataForChase::Status != RECORDSTATE_1) + if(m_status == STATUS_PLAYER && CRecordDataForChase::IsRecording()) DoDriveByShootings(); } break; From 59d4a29f55712d9e8ea45e299c443b86b1adf93f Mon Sep 17 00:00:00 2001 From: Nikolay Korolev Date: Sat, 11 Apr 2020 02:11:18 +0300 Subject: [PATCH 3/6] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index ed4116c4..ca708494 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,6 @@ CObject CPacManPickups CPedPath CPools - save/loading -CRecordDataForChase -CRecordDataForGame CRoadBlocks CWeapon CWorld From bc90f958da4682a4b951cd77cbfd1aa3c12e2fad Mon Sep 17 00:00:00 2001 From: Nikolay Korolev Date: Sat, 11 Apr 2020 02:27:50 +0300 Subject: [PATCH 4/6] fix constants --- src/control/Record.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/control/Record.cpp b/src/control/Record.cpp index 19fd8969..e858bc1d 100644 --- a/src/control/Record.cpp +++ b/src/control/Record.cpp @@ -230,8 +230,8 @@ void CRecordDataForChase::SaveOrRetrieveDataForThisFrame(void) #else CCarStateEachFrame* pState = (CCarStateEachFrame*)pChaseCars[CurrentCar]; #endif - CVector right = CVector(pState->rightX, pState->rightY, pState->rightZ) / (UINT8_MAX / 2); - CVector forward = CVector(pState->forwardX, pState->forwardY, pState->forwardZ) / (UINT8_MAX / 2); + CVector right = CVector(pState->rightX, pState->rightY, pState->rightZ) / INT8_MAX; + CVector forward = CVector(pState->forwardX, pState->forwardY, pState->forwardZ) / INT8_MAX; CVector up = CrossProduct(right, forward); sprintf(gString, "%f %f %f\n", pState->pos.x, pState->pos.y, pState->pos.z); CFileMgr::Write(fid2, gString, strlen(gString) - 1); @@ -330,16 +330,16 @@ void CRecordDataForChase::SaveOrRetrieveCarPositions(void) void CRecordDataForChase::StoreInfoForCar(CAutomobile* pCar, CCarStateEachFrame* pState) { - pState->rightX = UINT8_MAX / 2 * pCar->GetRight().x; - pState->rightY = UINT8_MAX / 2 * pCar->GetRight().y; - pState->rightZ = UINT8_MAX / 2 * pCar->GetRight().z; - pState->forwardX = UINT8_MAX / 2 * pCar->GetForward().x; - pState->forwardY = UINT8_MAX / 2 * pCar->GetForward().y; - pState->forwardZ = UINT8_MAX / 2 * pCar->GetForward().z; + pState->rightX = INT8_MAX * pCar->GetRight().x; + pState->rightY = INT8_MAX * pCar->GetRight().y; + pState->rightZ = INT8_MAX * pCar->GetRight().z; + pState->forwardX = INT8_MAX * pCar->GetForward().x; + pState->forwardY = INT8_MAX * pCar->GetForward().y; + pState->forwardZ = INT8_MAX * pCar->GetForward().z; pState->pos = pCar->GetPosition(); - pState->velX = 1.0f * (UINT16_MAX / 2) * pCar->GetMoveSpeed().x; - pState->velY = 1.0f * (UINT16_MAX / 2) * pCar->GetMoveSpeed().y; - pState->velZ = 1.0f * (UINT16_MAX / 2) * pCar->GetMoveSpeed().z; + pState->velX = 0.5f * INT16_MAX * pCar->GetMoveSpeed().x; + pState->velY = 0.5f * INT16_MAX * pCar->GetMoveSpeed().y; + pState->velZ = 0.5f * INT16_MAX * pCar->GetMoveSpeed().z; pState->wheel = 20 * pCar->m_fSteerAngle; pState->gas = 100 * pCar->m_fGasPedal; pState->brake = 100 * pCar->m_fBrakePedal; @@ -348,8 +348,8 @@ void CRecordDataForChase::StoreInfoForCar(CAutomobile* pCar, CCarStateEachFrame* void CRecordDataForChase::RestoreInfoForMatrix(CMatrix& matrix, CCarStateEachFrame* pState) { - matrix.GetRight() = CVector(pState->rightX, pState->rightY, pState->rightZ) / (UINT8_MAX / 2); - matrix.GetForward() = CVector(pState->forwardX, pState->forwardY, pState->forwardZ) / (UINT8_MAX / 2); + matrix.GetRight() = CVector(pState->rightX, pState->rightY, pState->rightZ) / INT8_MAX; + matrix.GetForward() = CVector(pState->forwardX, pState->forwardY, pState->forwardZ) / INT8_MAX; matrix.GetUp() = CrossProduct(matrix.GetRight(), matrix.GetForward()); matrix.GetPosition() = pState->pos; } @@ -358,7 +358,7 @@ void CRecordDataForChase::RestoreInfoForCar(CAutomobile* pCar, CCarStateEachFram { CVector oldPos = pCar->GetPosition(); RestoreInfoForMatrix(pCar->GetMatrix(), pState); - pCar->SetMoveSpeed(CVector(pState->velX, pState->velY, pState->velZ) * 1.0f / (UINT16_MAX / 2)); + pCar->SetMoveSpeed(CVector(pState->velX, pState->velY, pState->velZ) / INT16_MAX / 0.5f); pCar->SetTurnSpeed(0.0f, 0.0f, 0.0f); pCar->m_fSteerAngle = pState->wheel / 20.0f; pCar->m_fGasPedal = pState->gas / 100.0f; From 1c0c0a046842c0b937a07e5a957950faf54611af Mon Sep 17 00:00:00 2001 From: Nikolay Korolev Date: Sat, 11 Apr 2020 13:20:41 +0300 Subject: [PATCH 5/6] fixed malloc/new --- src/control/Record.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/control/Record.cpp b/src/control/Record.cpp index e858bc1d..aead1720 100644 --- a/src/control/Record.cpp +++ b/src/control/Record.cpp @@ -40,7 +40,7 @@ void CRecordDataForGame::Init(void) RecordingState = STATE_PLAYBACK; } if (RecordingState == STATE_PLAYBACK) { - pDataBufferPointer = (uint8*)malloc(MEMORY_FOR_GAME_RECORD); + pDataBufferPointer = new uint8[MEMORY_FOR_GAME_RECORD]; pDataBuffer = pDataBufferPointer; pDataBuffer[CFileMgr::Read(FId, (char*)pDataBufferPointer, MEMORY_FOR_GAME_RECORD) + 8] = -1; CFileMgr::CloseFile(FId); From b48617dfde65a228cab72439f57a3763587137f9 Mon Sep 17 00:00:00 2001 From: Nikolay Korolev Date: Sat, 11 Apr 2020 21:09:52 +0300 Subject: [PATCH 6/6] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index ca708494..6f8b6d8a 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ CMenuManager - WIP CObject CPacManPickups CPedPath -CPools - save/loading CRoadBlocks CWeapon CWorld