From 84fa9a03d141f946c20c8d0350976510e19d18ca Mon Sep 17 00:00:00 2001 From: Uladzislau Nikalayevich Date: Mon, 25 Dec 2023 15:12:07 +0300 Subject: [PATCH] Fix from review --- Client/game_sa/CFileLoaderSA.cpp | 8 +++---- Client/game_sa/CFileLoaderSA.h | 6 ++--- Client/game_sa/CPoolsSA.cpp | 4 ++-- .../mods/deathmatch/logic/CClientBuilding.cpp | 17 ++++++++------ .../mods/deathmatch/logic/CClientBuilding.h | 2 +- .../logic/CClientBuildingManager.cpp | 22 +++++-------------- .../deathmatch/logic/CClientBuildingManager.h | 6 +---- Client/mods/deathmatch/logic/CClientManager.h | 2 +- Client/mods/deathmatch/logic/CClientModel.cpp | 4 ++-- .../logic/luadefs/CLuaBuildingDefs.cpp | 5 ++--- Shared/mods/deathmatch/logic/Utils.h | 2 +- 11 files changed, 32 insertions(+), 46 deletions(-) diff --git a/Client/game_sa/CFileLoaderSA.cpp b/Client/game_sa/CFileLoaderSA.cpp index 01372c29bb..c235479a22 100644 --- a/Client/game_sa/CFileLoaderSA.cpp +++ b/Client/game_sa/CFileLoaderSA.cpp @@ -28,9 +28,9 @@ void CFileLoaderSA::StaticSetHooks() HookInstall(0x538690, (DWORD)CFileLoader_LoadObjectInstance, 5); } -CBuildingSAInterface* CFileLoaderSA::LoadFileObjectInstance(SFileObjectInstance* obj) +CEntitySAInterface* CFileLoaderSA::LoadFileObjectInstance(SFileObjectInstance* obj) { - return ((CBuildingSAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(obj); + return ((CEntitySAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(obj); } class CAtomicModelInfo @@ -206,7 +206,7 @@ RpAtomic* CFileLoader_SetRelatedModelInfoCB(RpAtomic* atomic, SRelatedModelInfo* return atomic; } -CBuildingSAInterface* CFileLoader_LoadObjectInstance(const char* szLine) +CEntitySAInterface* CFileLoader_LoadObjectInstance(const char* szLine) { char szModelName[24]; SFileObjectInstance inst; @@ -222,5 +222,5 @@ CBuildingSAInterface* CFileLoader_LoadObjectInstance(const char* szLine) if (fLenSq > 0.0f && std::fabs(fLenSq - 1.0f) > std::numeric_limits::epsilon()) inst.rotation /= std::sqrt(fLenSq); - return ((CBuildingSAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(&inst); + return ((CEntitySAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(&inst); } diff --git a/Client/game_sa/CFileLoaderSA.h b/Client/game_sa/CFileLoaderSA.h index ad5103c75c..4fe983cc51 100644 --- a/Client/game_sa/CFileLoaderSA.h +++ b/Client/game_sa/CFileLoaderSA.h @@ -3,7 +3,7 @@ #include "CVector.h" #include "CVector4D.h" -class CBuildingSAInterface; +class CEntitySAInterface; struct RpAtomic; struct RpClump; struct RwStream; @@ -29,11 +29,11 @@ class CFileLoaderSA CFileLoaderSA(); ~CFileLoaderSA(); - CBuildingSAInterface* LoadFileObjectInstance(SFileObjectInstance*); + CEntitySAInterface* LoadFileObjectInstance(SFileObjectInstance*); static void StaticSetHooks(); }; bool CFileLoader_LoadAtomicFile(RwStream* stream, unsigned int modelId); RpAtomic* CFileLoader_SetRelatedModelInfoCB(RpAtomic* atomic, SRelatedModelInfo* pRelatedModelInfo); -CBuildingSAInterface* CFileLoader_LoadObjectInstance(const char* szLine); +CEntitySAInterface* CFileLoader_LoadObjectInstance(const char* szLine); diff --git a/Client/game_sa/CPoolsSA.cpp b/Client/game_sa/CPoolsSA.cpp index a8075fbe32..5efe19f7c1 100644 --- a/Client/game_sa/CPoolsSA.cpp +++ b/Client/game_sa/CPoolsSA.cpp @@ -375,7 +375,7 @@ CBuilding* CPoolsSA::AddBuilding(CClientBuilding* pClientBuilding, uint16_t mode instance.rotation.fW = -instance.rotation.fW; CFileLoaderSA loader{}; - auto pBuilding = loader.LoadFileObjectInstance(&instance); + auto pBuilding = static_cast(loader.LoadFileObjectInstance(&instance)); // Disable lod and ipl pBuilding->m_pLod = nullptr; @@ -807,7 +807,7 @@ DWORD CPoolsSA::GetObjectPoolIndex(std::uint8_t* pInterface) DWORD CPoolsSA::GetBuildingPoolIndex(std::uint8_t* pInterface) { - DWORD dwAlignedSize = 412; + DWORD dwAlignedSize = sizeof(CBuildingSAInterface); std::uint8_t* pTheObjects = (std::uint8_t*)(*m_ppBuildingPoolInterface)->m_pObjects; DWORD dwMaxIndex = MAX_BUILDINGS - 1; if (pInterface < pTheObjects || pInterface > pTheObjects + (dwMaxIndex * dwAlignedSize)) diff --git a/Client/mods/deathmatch/logic/CClientBuilding.cpp b/Client/mods/deathmatch/logic/CClientBuilding.cpp index 567eaadd97..ef282ed9e2 100644 --- a/Client/mods/deathmatch/logic/CClientBuilding.cpp +++ b/Client/mods/deathmatch/logic/CClientBuilding.cpp @@ -10,16 +10,16 @@ #include "StdInc.h" -CClientBuilding::CClientBuilding(class CClientManager* pManager, ElementID ID, uint16_t usModelId, CVector &pos, CVector &rot, uint8_t interior) +CClientBuilding::CClientBuilding(class CClientManager* pManager, ElementID ID, uint16_t usModelId, const CVector &pos, const CVector &rot, uint8_t interior) : ClassInit(this), CClientEntity(ID), m_pBuildingManager(pManager->GetBuildingManager()), m_usModelId(usModelId), m_vPos(pos), m_vRot(rot), - m_interior(interior) + m_interior(interior), + m_pBuilding(nullptr) { - m_pBuilding = nullptr; m_pManager = pManager; SetTypeName("building"); m_pBuildingManager->AddToList(this); @@ -29,26 +29,29 @@ CClientBuilding::CClientBuilding(class CClientManager* pManager, ElementID ID, u CClientBuilding::~CClientBuilding() { m_pBuildingManager->RemoveFromList(this); - if (m_pBuilding) - { - Destroy(); - } + Destroy(); } void CClientBuilding::SetPosition(const CVector& vecPosition) { + if (m_vPos == vecPosition) + return; m_vPos = vecPosition; Recreate(); } void CClientBuilding::SetRotationRadians(const CVector& vecRadians) { + if (m_vRot == vecRadians) + return; m_vRot = vecRadians; Recreate(); } void CClientBuilding::SetInterior(uint8_t ucInterior) { + if (m_interior == ucInterior) + return; m_interior = ucInterior; Recreate(); } diff --git a/Client/mods/deathmatch/logic/CClientBuilding.h b/Client/mods/deathmatch/logic/CClientBuilding.h index 3e7eb577b9..3e91effa94 100644 --- a/Client/mods/deathmatch/logic/CClientBuilding.h +++ b/Client/mods/deathmatch/logic/CClientBuilding.h @@ -20,7 +20,7 @@ class CClientBuilding : public CClientEntity friend class CClientBuildingManager; public: - CClientBuilding(class CClientManager* pManager, ElementID ID, uint16_t usModelId, CVector &pos, CVector &rot, uint8_t interior); + CClientBuilding(class CClientManager* pManager, ElementID ID, uint16_t usModelId, const CVector &pos, const CVector &rot, uint8_t interior); ~CClientBuilding(); void Unlink(){}; diff --git a/Client/mods/deathmatch/logic/CClientBuildingManager.cpp b/Client/mods/deathmatch/logic/CClientBuildingManager.cpp index fb9a512ff2..59e421b6c6 100644 --- a/Client/mods/deathmatch/logic/CClientBuildingManager.cpp +++ b/Client/mods/deathmatch/logic/CClientBuildingManager.cpp @@ -28,32 +28,20 @@ void CClientBuildingManager::RemoveAll() m_bRemoveFromList = false; // Run through our list deleting the buildings - auto iter = m_List.begin(); - for (; iter != m_List.end(); iter++) + for (CClientBuilding* building : m_List) { - delete *iter; + delete building; } + m_List.clear(); + // Allow list removal again m_bRemoveFromList = true; } bool CClientBuildingManager::Exists(CClientBuilding* pBuilding) { - // Matches given DFF? - auto iter = m_List.begin(); - for (; iter != m_List.end(); iter++) - { - // Match? - if (pBuilding == *iter) - { - // It exists - return true; - } - } - - // It doesn't - return false; + return std::find(m_List.begin(), m_List.end(), pBuilding) != m_List.end(); } void CClientBuildingManager::RemoveFromList(CClientBuilding* pBuilding) diff --git a/Client/mods/deathmatch/logic/CClientBuildingManager.h b/Client/mods/deathmatch/logic/CClientBuildingManager.h index dd595d3cb3..0e1c381ad7 100644 --- a/Client/mods/deathmatch/logic/CClientBuildingManager.h +++ b/Client/mods/deathmatch/logic/CClientBuildingManager.h @@ -26,16 +26,12 @@ class CClientBuildingManager void RemoveAll(); bool Exists(CClientBuilding* pBuilding); - std::list::const_iterator IterBegin() { return m_List.begin(); } - std::list::const_iterator IterEnd() { return m_List.end(); } + const std::list& GetBuildings() { return m_List; }; private: void AddToList(CClientBuilding* pBuilding) { m_List.push_back(pBuilding); } void RemoveFromList(CClientBuilding* pBuilding); - class CClientObjectManager* m_pObjectManager; - class CClientVehicleManager* m_pVehicleManager; - std::list m_List; bool m_bRemoveFromList; }; diff --git a/Client/mods/deathmatch/logic/CClientManager.h b/Client/mods/deathmatch/logic/CClientManager.h index 92f8481db8..0e9f0c7702 100644 --- a/Client/mods/deathmatch/logic/CClientManager.h +++ b/Client/mods/deathmatch/logic/CClientManager.h @@ -97,7 +97,7 @@ class CClientManager CClientEffectManager* GetEffectManager() { return m_pEffectManager; } CClientPointLightsManager* GetPointLightsManager() { return m_pPointLightsManager; } CClientIMGManager* GetIMGManager() { return m_pImgManager; } - CClientBuildingManager* GetBuildingManager() { return m_pBuildingManager; } + CClientBuildingManager* GetBuildingManager() const noexcept { return m_pBuildingManager; } bool IsGameLoaded() { return g_pGame->GetSystemState() == 9 && !m_bGameUnloadedFlag && g_pCore->GetNetwork()->GetServerBitStreamVersion(); } bool IsBeingDeleted() { return m_bBeingDeleted; } diff --git a/Client/mods/deathmatch/logic/CClientModel.cpp b/Client/mods/deathmatch/logic/CClientModel.cpp index a30bdabe0e..a80d637b8c 100644 --- a/Client/mods/deathmatch/logic/CClientModel.cpp +++ b/Client/mods/deathmatch/logic/CClientModel.cpp @@ -186,8 +186,8 @@ void CClientModel::RestoreDFF(CModelInfo* pModelInfo) // Restore buildings CClientBuildingManager* pBuildingsManager = g_pClientGame->GetManager()->GetBuildingManager(); - - unloadModelsAndCallEventsNonStreamed(pBuildingsManager->IterBegin(), pBuildingsManager->IterEnd(), usParentID, + auto& buildingsList = pBuildingsManager->GetBuildings(); + unloadModelsAndCallEventsNonStreamed(buildingsList.begin(), buildingsList.end(), usParentID, [=](auto& element) { element.SetModel(usParentID); }); // Restore COL diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBuildingDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaBuildingDefs.cpp index f2e174cbac..fced4f0519 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBuildingDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBuildingDefs.cpp @@ -49,12 +49,11 @@ CClientBuilding* CLuaBuildingDefs::CreateBuilding(lua_State* const luaVM, uint16 if (pos.fX < -3000.0f || pos.fX > 3000.0f || pos.fY < -3000.0f || pos.fY > 3000.0f) throw std::invalid_argument("Position is outside of game world"); - // Grab the resource root entity - CClientEntity* pRoot = pResource->GetResourceDynamicEntity(); + ConvertDegreesToRadians(rot); - // Create the building handle CClientBuilding* pBuilding = new CClientBuilding(m_pManager, INVALID_ELEMENT_ID, modelId, pos, rot, interior); + CClientEntity* pRoot = pResource->GetResourceDynamicEntity(); pBuilding->SetParent(pRoot); return pBuilding; diff --git a/Shared/mods/deathmatch/logic/Utils.h b/Shared/mods/deathmatch/logic/Utils.h index e8d27e3943..96a4429d1f 100644 --- a/Shared/mods/deathmatch/logic/Utils.h +++ b/Shared/mods/deathmatch/logic/Utils.h @@ -246,7 +246,7 @@ inline float GetSmallestWrapUnsigned(float fValue, float fHigh) void RotateVector(CVector& vecLine, const CVector& vecRotation); -inline void ConvertEulersToQuaternion(const CVector& vecFrom, CVector4D vecTo) +inline void ConvertEulersToQuaternion(const CVector& vecFrom, CVector4D &vecTo) { double cy = cos(vecFrom.fZ * 0.5); double sy = sin(vecFrom.fZ * 0.5);