|
| 1 | +/***************************************************************************** |
| 2 | + * |
| 3 | + * PROJECT: Multi Theft Auto v1.0 |
| 4 | + * LICENSE: See LICENSE in the top level directory |
| 5 | + * FILE: game_sa/CBuildingsPoolSA.cpp |
| 6 | + * PURPOSE: Buildings pool class |
| 7 | + * |
| 8 | + * Multi Theft Auto is available from http://www.multitheftauto.com/ |
| 9 | + * |
| 10 | + *****************************************************************************/ |
| 11 | + |
| 12 | +#include "StdInc.h" |
| 13 | +#include "CBuildingsPoolSA.h" |
| 14 | + |
| 15 | +#include "CFileLoaderSA.h" |
| 16 | +#include <game/CWorld.h> |
| 17 | +#include "CGameSA.h" |
| 18 | +#include "CPtrNodeSingleListSA.h" |
| 19 | + |
| 20 | +extern CGameSA* pGame; |
| 21 | + |
| 22 | +class CClientEntity; |
| 23 | + |
| 24 | +CBuildingsPoolSA::CBuildingsPoolSA() : m_pOriginalBuildingsBackup(nullptr) |
| 25 | +{ |
| 26 | + m_ppBuildingPoolInterface = (CPoolSAInterface<CBuildingSAInterface>**)0xB74498; |
| 27 | +} |
| 28 | + |
| 29 | +inline bool CBuildingsPoolSA::AddBuildingToPool(CClientBuilding* pClientBuilding, CBuildingSA* pBuilding) |
| 30 | +{ |
| 31 | + // Grab the new object interface |
| 32 | + CBuildingSAInterface* pInterface = pBuilding->GetBuildingInterface(); |
| 33 | + |
| 34 | + if (!pInterface) |
| 35 | + return false; |
| 36 | + |
| 37 | + uint32_t dwElementIndexInPool = (*m_ppBuildingPoolInterface)->GetObjectIndexSafe(pInterface); |
| 38 | + if (dwElementIndexInPool == UINT_MAX) |
| 39 | + return false; |
| 40 | + |
| 41 | + m_buildingPool.arrayOfClientEntities[dwElementIndexInPool] = {pBuilding, (CClientEntity*)pClientBuilding}; |
| 42 | + |
| 43 | + // Increase the count of objects |
| 44 | + ++m_buildingPool.ulCount; |
| 45 | + |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +CBuilding* CBuildingsPoolSA::AddBuilding(CClientBuilding* pClientBuilding, uint16_t modelId, CVector* vPos, CVector4D* vRot, uint8_t interior) |
| 50 | +{ |
| 51 | + if (!HasFreeBuildingSlot()) |
| 52 | + return nullptr; |
| 53 | + |
| 54 | + // Load building |
| 55 | + SFileObjectInstance instance; |
| 56 | + instance.modelID = modelId; |
| 57 | + instance.lod = -1; |
| 58 | + instance.interiorID = interior; |
| 59 | + instance.position = *vPos; |
| 60 | + instance.rotation = *vRot; |
| 61 | + |
| 62 | + // Fix strange SA rotation |
| 63 | + instance.rotation.fW = -instance.rotation.fW; |
| 64 | + |
| 65 | + auto pBuilding = static_cast<CBuildingSAInterface*>(CFileLoaderSA::LoadObjectInstance(&instance)); |
| 66 | + |
| 67 | + // Disable lod and ipl |
| 68 | + pBuilding->m_pLod = nullptr; |
| 69 | + pBuilding->m_iplIndex = 0; |
| 70 | + |
| 71 | + // Always stream model collosion |
| 72 | + // TODO We can setup collison bounding box and use GTA streamer for it |
| 73 | + auto modelInfo = pGame->GetModelInfo(modelId); |
| 74 | + modelInfo->AddColRef(); |
| 75 | + |
| 76 | + // Add building in world |
| 77 | + auto pBuildingSA = new CBuildingSA(pBuilding); |
| 78 | + pGame->GetWorld()->Add(pBuildingSA, CBuildingPool_Constructor); |
| 79 | + |
| 80 | + // Add CBuildingSA object in pool |
| 81 | + AddBuildingToPool(pClientBuilding, pBuildingSA); |
| 82 | + |
| 83 | + return pBuildingSA; |
| 84 | +} |
| 85 | + |
| 86 | +void CBuildingsPoolSA::RemoveBuilding(CBuilding* pBuilding) |
| 87 | +{ |
| 88 | + assert(NULL != pBuilding); |
| 89 | + |
| 90 | + CBuildingSAInterface* pInterface = pBuilding->GetBuildingInterface(); |
| 91 | + |
| 92 | + uint32_t dwElementIndexInPool = (*m_ppBuildingPoolInterface)->GetObjectIndexSafe(pInterface); |
| 93 | + if (dwElementIndexInPool == UINT_MAX) |
| 94 | + return; |
| 95 | + |
| 96 | + // Remove building from world |
| 97 | + pGame->GetWorld()->Remove(pInterface, CBuildingPool_Destructor); |
| 98 | + |
| 99 | + // Remove building from cover list |
| 100 | + CPtrNodeSingleListSAInterface<CBuildingSAInterface>* coverList = reinterpret_cast<CPtrNodeSingleListSAInterface<CBuildingSAInterface>*>(0xC1A2B8); |
| 101 | + coverList->RemoveItem(pInterface); |
| 102 | + |
| 103 | + // Remove plant |
| 104 | + using CPlantColEntry_Remove = CEntitySAInterface* (*)(CEntitySAInterface*); |
| 105 | + ((CPlantColEntry_Remove)0x5DBEF0)(pInterface); |
| 106 | + |
| 107 | + // Remove col reference |
| 108 | + auto modelInfo = pGame->GetModelInfo(pBuilding->GetModelIndex()); |
| 109 | + modelInfo->RemoveColRef(); |
| 110 | + |
| 111 | + // Remove from BuildingSA pool |
| 112 | + auto* pBuildingSA = m_buildingPool.arrayOfClientEntities[dwElementIndexInPool].pEntity; |
| 113 | + m_buildingPool.arrayOfClientEntities[dwElementIndexInPool] = {nullptr, nullptr}; |
| 114 | + |
| 115 | + // Delete it from memory |
| 116 | + delete pBuildingSA; |
| 117 | + |
| 118 | + // Remove building from SA pool |
| 119 | + (*m_ppBuildingPoolInterface)->Release(dwElementIndexInPool); |
| 120 | + |
| 121 | + // Decrease the count of elements in the pool |
| 122 | + --m_buildingPool.ulCount; |
| 123 | +} |
| 124 | + |
| 125 | +void CBuildingsPoolSA::RemoveAllBuildings() |
| 126 | +{ |
| 127 | + if (m_pOriginalBuildingsBackup) |
| 128 | + return; |
| 129 | + |
| 130 | + m_pOriginalBuildingsBackup = std::make_unique<std::array<std::pair<bool, CBuildingSAInterface>, MAX_BUILDINGS>>(); |
| 131 | + |
| 132 | + auto pBuildsingsPool = (*m_ppBuildingPoolInterface); |
| 133 | + for (size_t i = 0; i < MAX_BUILDINGS; i++) |
| 134 | + { |
| 135 | + if (pBuildsingsPool->IsContains(i)) |
| 136 | + { |
| 137 | + auto building = pBuildsingsPool->GetObject(i); |
| 138 | + |
| 139 | + pGame->GetWorld()->Remove(building, CBuildingPool_Destructor); |
| 140 | + |
| 141 | + pBuildsingsPool->Release(i); |
| 142 | + |
| 143 | + (*m_pOriginalBuildingsBackup)[i].first = true; |
| 144 | + (*m_pOriginalBuildingsBackup)[i].second = *building; |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + (*m_pOriginalBuildingsBackup)[i].first = false; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +void CBuildingsPoolSA::RestoreAllBuildings() |
| 154 | +{ |
| 155 | + if (!m_pOriginalBuildingsBackup) |
| 156 | + return; |
| 157 | + |
| 158 | + auto& originalData = *m_pOriginalBuildingsBackup; |
| 159 | + auto pBuildsingsPool = (*m_ppBuildingPoolInterface); |
| 160 | + for (size_t i = 0; i < MAX_BUILDINGS; i++) |
| 161 | + { |
| 162 | + if (originalData[i].first) |
| 163 | + { |
| 164 | + pBuildsingsPool->AllocateAt(i); |
| 165 | + auto building = pBuildsingsPool->GetObject(i); |
| 166 | + *building = originalData[i].second; |
| 167 | + |
| 168 | + pGame->GetWorld()->Add(building, CBuildingPool_Constructor); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + m_pOriginalBuildingsBackup.release(); |
| 173 | +} |
| 174 | + |
| 175 | +bool CBuildingsPoolSA::HasFreeBuildingSlot() |
| 176 | +{ |
| 177 | + return (*m_ppBuildingPoolInterface)->GetFreeSlot() != -1; |
| 178 | +} |
0 commit comments