From c1f3ce8cceb675a9c76b4dc4e7cdfb18b17c4f2f Mon Sep 17 00:00:00 2001 From: aap Date: Sun, 7 Jul 2019 18:36:55 +0200 Subject: [PATCH] implemented CDoor --- src/control/Replay.cpp | 4 +- src/math/Matrix.h | 9 +++ src/vehicles/Automobile.cpp | 4 +- src/vehicles/Automobile.h | 17 +---- src/vehicles/Door.cpp | 126 ++++++++++++++++++++++++++++++++++++ src/vehicles/Door.h | 36 +++++++++++ 6 files changed, 177 insertions(+), 19 deletions(-) create mode 100644 src/vehicles/Door.cpp create mode 100644 src/vehicles/Door.h diff --git a/src/control/Replay.cpp b/src/control/Replay.cpp index 06995663..d0264415 100644 --- a/src/control/Replay.cpp +++ b/src/control/Replay.cpp @@ -686,8 +686,8 @@ void CReplay::ProcessCarUpdate(CVehicle *vehicle, float interpolation, CAddressI car->m_aSuspensionSpringRatio[i] = vp->wheel_susp_dist[i] / 50.0f; car->m_aWheelRotation[i] = vp->wheel_rotation[i] * M_PI / 128.0f; } - car->Doors[2].m_fAngle = car->Doors[2].m_fPreviousAngle = vp->door_angles[0] * M_PI / 127.0f; - car->Doors[3].m_fAngle = car->Doors[3].m_fPreviousAngle = vp->door_angles[1] * M_PI / 127.0f; + car->Doors[2].m_fAngle = car->Doors[2].m_fPrevAngle = vp->door_angles[0] * M_PI / 127.0f; + car->Doors[3].m_fAngle = car->Doors[3].m_fPrevAngle = vp->door_angles[1] * M_PI / 127.0f; if (vp->door_angles[0]) car->Damage.SetDoorStatus(2, 2); if (vp->door_angles[1]) diff --git a/src/math/Matrix.h b/src/math/Matrix.h index 6e1001cb..5cc7d12f 100644 --- a/src/math/Matrix.h +++ b/src/math/Matrix.h @@ -306,6 +306,15 @@ Multiply3x3(const CMatrix &mat, const CVector &vec) mat.m_matrix.right.z * vec.x + mat.m_matrix.up.z * vec.y + mat.m_matrix.at.z * vec.z); } +inline CVector +Multiply3x3(const CVector &vec, const CMatrix &mat) +{ + return CVector( + mat.m_matrix.right.x * vec.x + mat.m_matrix.right.y * vec.y + mat.m_matrix.right.z * vec.z, + mat.m_matrix.up.x * vec.x + mat.m_matrix.up.y * vec.y + mat.m_matrix.up.z * vec.z, + mat.m_matrix.at.x * vec.x + mat.m_matrix.at.y * vec.y + mat.m_matrix.at.z * vec.z); +} + class CCompressedMatrixNotAligned { CVector m_vecPos; diff --git a/src/vehicles/Automobile.cpp b/src/vehicles/Automobile.cpp index 54eed17a..80131179 100644 --- a/src/vehicles/Automobile.cpp +++ b/src/vehicles/Automobile.cpp @@ -14,5 +14,5 @@ WRAPPER void CAutomobile::SetPanelDamage(int32, uint32, bool) { EAXJMP(0x5301A0) WRAPPER void CAutomobile::SetBumperDamage(int32, uint32, bool) { EAXJMP(0x530120); } STARTPATCHES -InjectHook(0x52D170, &CAutomobile::dtor, PATCH_JUMP); -ENDPATCHES \ No newline at end of file + InjectHook(0x52D170, &CAutomobile::dtor, PATCH_JUMP); +ENDPATCHES diff --git a/src/vehicles/Automobile.h b/src/vehicles/Automobile.h index 630635c7..c20d078b 100644 --- a/src/vehicles/Automobile.h +++ b/src/vehicles/Automobile.h @@ -1,21 +1,8 @@ #pragma once -#include "DamageManager.h" #include "Vehicle.h" - -struct CDoor -{ - float m_fAngleWhenOpened; - float m_fAngleWhenClosed; - char field_8; - char field_9; - char field_10; - char field_11; - float m_fAngle; - float m_fPreviousAngle; - float m_fAngularVelocity; - CVector m_vecVelocity; -}; +#include "DamageManager.h" +#include "Door.h" class CAutomobile : public CVehicle { diff --git a/src/vehicles/Door.cpp b/src/vehicles/Door.cpp new file mode 100644 index 00000000..ec5eb223 --- /dev/null +++ b/src/vehicles/Door.cpp @@ -0,0 +1,126 @@ +#include "common.h" +#include "patcher.h" +#include "Vehicle.h" +#include "Door.h" + +CDoor::CDoor(void) +{ + memset(this, 0, sizeof(*this)); +} + +void +CDoor::Open(float ratio) +{ + float open; + + m_fPrevAngle = m_fAngle; + open = RetAngleWhenOpen(); + if(ratio < 1.0f){ + m_fAngle = open*ratio; + if(m_fAngle == 0.0f) + m_fAngVel = 0.0f; + }else{ + m_nDoorState = DOORST_OPEN; + m_fAngle = open; + } +} + +void +CDoor::Process(CVehicle *vehicle) +{ + static CVector vecOffset(1.0f, 0.0f, 0.0f); + CVector speed = vehicle->GetSpeed(vecOffset); + CVector vecSpeedDiff = speed - m_vecSpeed; + vecSpeedDiff = Multiply3x3(vecSpeedDiff, vehicle->GetMatrix()); + + // air resistance + float fSpeedDiff = 0.0f; // uninitialized in game + switch(m_nAxis){ + case 0: // x-axis + if(m_nDirn) + fSpeedDiff = vecSpeedDiff.y + vecSpeedDiff.z; + else + fSpeedDiff = -(vecSpeedDiff.y + vecSpeedDiff.z); + break; + + // we don't support y axis apparently? + + case 2: // z-axis + if(m_nDirn) + fSpeedDiff = -(vecSpeedDiff.x + vecSpeedDiff.y); + else + fSpeedDiff = vecSpeedDiff.x + vecSpeedDiff.y; + break; + } + fSpeedDiff = clamp(fSpeedDiff, -0.2f, 0.2f); + if(fabs(fSpeedDiff) > 0.002f) + m_fAngVel += fSpeedDiff; + m_fAngVel *= 0.945f; + m_fAngVel = clamp(m_fAngVel, -0.3f, 0.3f); + + m_fAngle += m_fAngVel; + m_nDoorState = DOORST_SWINGING; + if(m_fAngle > m_fMaxAngle){ + m_fAngle = m_fMaxAngle; + m_fAngVel *= -0.8f; + m_nDoorState = DOORST_OPEN; + } + if(m_fAngle < m_fMinAngle){ + m_fAngle = m_fMinAngle; + m_fAngVel *= -0.8f; + m_nDoorState = DOORST_CLOSED; + } + m_vecSpeed = speed; +} + +float +CDoor::RetAngleWhenClosed(void) +{ + if(fabs(m_fMaxAngle) < fabs(m_fMinAngle)) + return m_fMaxAngle; + else + return m_fMinAngle; +} + +float +CDoor::RetAngleWhenOpen(void) +{ + if(fabs(m_fMaxAngle) < fabs(m_fMinAngle)) + return m_fMinAngle; + else + return m_fMaxAngle; +} + +float +CDoor::GetAngleOpenRatio(void) +{ + float open = RetAngleWhenOpen(); + if(open == 0.0f) + return 0.0f; + return m_fAngle/open; +} + +bool +CDoor::IsFullyOpen(void) +{ + // why -0.5? that's around 28 deg less than fully open + if(fabs(m_fAngle) < fabs(RetAngleWhenOpen()) - 0.5f) + return false; + return true; +} + +bool +CDoor::IsClosed(void) +{ + return m_fAngle == RetAngleWhenClosed(); +} + +STARTPATCHES + InjectHook(0x545EF0, &CDoor::Open, PATCH_JUMP); + InjectHook(0x545BD0, &CDoor::Process, PATCH_JUMP); + InjectHook(0x545FE0, &CDoor::RetAngleWhenClosed, PATCH_JUMP); + InjectHook(0x546020, &CDoor::RetAngleWhenOpen, PATCH_JUMP); + InjectHook(0x545F80, &CDoor::GetAngleOpenRatio, PATCH_JUMP); + InjectHook(0x546090, &CDoor::IsFullyOpen, PATCH_JUMP); + InjectHook(0x546060, &CDoor::IsClosed, PATCH_JUMP); +ENDPATCHES diff --git a/src/vehicles/Door.h b/src/vehicles/Door.h new file mode 100644 index 00000000..fc771a40 --- /dev/null +++ b/src/vehicles/Door.h @@ -0,0 +1,36 @@ +#pragma once + +class CVehicle; + +enum eDoorState +{ + DOORST_SWINGING, + // actually wrong though, + // OPEN is really MAX_ANGLE and CLOSED is MIN_ANGLE + DOORST_OPEN, + DOORST_CLOSED +}; + +struct CDoor +{ + float m_fMaxAngle; + float m_fMinAngle; + // direction of rotation for air resistance + int8 m_nDirn; + // axis in which this door rotates + int8 m_nAxis; + int8 m_nDoorState; + float m_fAngle; + float m_fPrevAngle; + float m_fAngVel; + CVector m_vecSpeed; + + CDoor(void); + void Open(float ratio); + void Process(CVehicle *veh); + float RetAngleWhenClosed(void); + float RetAngleWhenOpen(void); + float GetAngleOpenRatio(void); + bool IsFullyOpen(void); + bool IsClosed(void); +};