Skip to content

Commit

Permalink
createBuilding
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Dec 18, 2023
1 parent 0de7d96 commit f8e7139
Show file tree
Hide file tree
Showing 28 changed files with 508 additions and 17 deletions.
18 changes: 18 additions & 0 deletions Client/game_sa/CBuildingSA.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CBuildingSA.cpp
* PURPOSE: Building entity
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"
#include "CBuildingSA.h"

CBuildingSA::CBuildingSA(CBuildingSAInterface* pInterface)
{
SetInterface(pInterface);
}
28 changes: 28 additions & 0 deletions Client/game_sa/CBuildingSA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: game_sa/CBuildingSA.h
* PURPOSE: Header file for game building class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once

#include <game/CBuilding.h>
#include "CEntitySA.h"

class CBuildingSAInterface : public CEntitySAInterface
{
};
static_assert(sizeof(CBuildingSAInterface) == 0x38, "Invalid size CBuildingSAInterface");

class CBuildingSA final : public virtual CBuilding, public virtual CEntitySA
{
public:
CBuildingSA(CBuildingSAInterface* pInterface);

CBuildingSAInterface* GetBuildingInterface() { return reinterpret_cast<CBuildingSAInterface*>(GetInterface()); };
};
6 changes: 6 additions & 0 deletions Client/game_sa/CEntitySA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ void CEntitySAInterface::UpdateRpHAnim()
((void(__thiscall*)(CEntitySAInterface*))0x532B20)(this);
}

void CEntitySAInterface::AddRect()
{
typedef void(__thiscall * add_rect_t)(CEntitySAInterface*);
((add_rect_t)(this->vtbl->Add))(this);
}

CRect* CEntitySAInterface::GetBoundRect_(CRect* pRect)
{
CColModelSAInterface* colModel = CModelInfoSAInterface::GetModelInfo(m_nModelIndex)->pColModel;
Expand Down
1 change: 1 addition & 0 deletions Client/game_sa/CEntitySA.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class CEntitySAInterface

uint8 m_pad0; // 55

void AddRect();
CRect* GetBoundRect_(CRect* pRect);
void TransformFromObjectSpace(CVector& outPosn, CVector const& offset);
CVector* GetBoundCentre(CVector* pOutCentre);
Expand Down
9 changes: 7 additions & 2 deletions Client/game_sa/CFileLoaderSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ void CFileLoaderSA::StaticSetHooks()
HookInstall(0x538690, (DWORD)CFileLoader_LoadObjectInstance, 5);
}

CBuildingSAInterface* CFileLoaderSA::LoadFileObjectInstance(SFileObjectInstance* obj)
{
return ((CBuildingSAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(obj);
}

class CAtomicModelInfo
{
public:
Expand Down Expand Up @@ -201,7 +206,7 @@ RpAtomic* CFileLoader_SetRelatedModelInfoCB(RpAtomic* atomic, SRelatedModelInfo*
return atomic;
}

CEntitySAInterface* CFileLoader_LoadObjectInstance(const char* szLine)
CBuildingSAInterface* CFileLoader_LoadObjectInstance(const char* szLine)
{
char szModelName[24];
SFileObjectInstance inst;
Expand All @@ -217,5 +222,5 @@ CEntitySAInterface* CFileLoader_LoadObjectInstance(const char* szLine)
if (fLenSq > 0.0f && std::fabs(fLenSq - 1.0f) > std::numeric_limits<float>::epsilon())
inst.rotation /= std::sqrt(fLenSq);

return ((CEntitySAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(&inst);
return ((CBuildingSAInterface * (__cdecl*)(SFileObjectInstance*))0x538090)(&inst);
}
6 changes: 4 additions & 2 deletions Client/game_sa/CFileLoaderSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "CVector.h"
#include "CVector4D.h"

class CEntitySAInterface;
class CBuildingSAInterface;
struct RpAtomic;
struct RpClump;
struct RwStream;
Expand All @@ -29,9 +29,11 @@ class CFileLoaderSA
CFileLoaderSA();
~CFileLoaderSA();

CBuildingSAInterface* LoadFileObjectInstance(SFileObjectInstance*);

static void StaticSetHooks();
};

bool CFileLoader_LoadAtomicFile(RwStream* stream, unsigned int modelId);
RpAtomic* CFileLoader_SetRelatedModelInfoCB(RpAtomic* atomic, SRelatedModelInfo* pRelatedModelInfo);
CEntitySAInterface* CFileLoader_LoadObjectInstance(const char* szLine);
CBuildingSAInterface* CFileLoader_LoadObjectInstance(const char* szLine);
94 changes: 93 additions & 1 deletion Client/game_sa/CPoolsSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "CBikeSA.h"
#include "CBmxSA.h"
#include "CBoatSA.h"
#include "CBuildingSA.h"
#include "CGameSA.h"
#include "CHeliSA.h"
#include "CMonsterTruckSA.h"
Expand All @@ -24,6 +25,7 @@
#include "CTrainSA.h"
#include "CWorldSA.h"
#include "CKeyGenSA.h"
#include "CFileLoaderSA.h"

extern CGameSA* pGame;

Expand All @@ -33,6 +35,7 @@ CPoolsSA::CPoolsSA()
m_ppObjectPoolInterface = (CPoolSAInterface<CObjectSAInterface>**)0xB7449C;
m_ppVehiclePoolInterface = (CPoolSAInterface<CVehicleSAInterface>**)0xB74494;
m_ppTxdPoolInterface = (CPoolSAInterface<CTextureDictonarySAInterface>**)0xC8800C;
m_ppBuildingPoolInterface = (CPoolSAInterface<CBuildingSAInterface>**)0xB74498;

m_bGetVehicleEnabled = true;
}
Expand Down Expand Up @@ -329,6 +332,83 @@ void CPoolsSA::DeleteAllObjects()
}
}

//////////////////////////////////////////////////////////////////////////////////////////
// BUILDINGS POOL //
//////////////////////////////////////////////////////////////////////////////////////////

inline bool CPoolsSA::AddBuildingToPool(CClientBuilding* pClientBuilding, CBuildingSA* pBuilding)
{
// Grab the new object interface
CBuildingSAInterface* pInterface = pBuilding->GetBuildingInterface();

if (!pInterface)
{
return false;
}
else
{
DWORD dwElementIndexInPool = GetBuildingPoolIndex((std::uint8_t*)pInterface);
if (dwElementIndexInPool >= MAX_BUILDINGS)
{
return false;
}

m_buildingPool.arrayOfClientEntities[dwElementIndexInPool] = {pBuilding, (CClientEntity*)pClientBuilding};

// Increase the count of objects
++m_buildingPool.ulCount;
}

return true;
}

CBuilding* CPoolsSA::AddBuilding(class CClientBuilding* pClientBuilding, uint16_t modelId, CVector vPos, CVector4D vRot, uint8_t interior)
{
CFileLoaderSA loader{};

SFileObjectInstance instance;
instance.modelID = modelId;
instance.lod = -1;
instance.interiorID = interior;
instance.position = vPos;
instance.rotation = vRot;

auto pBuilding = loader.LoadFileObjectInstance(&instance);

pBuilding->m_pLod = nullptr;
pBuilding->m_iplIndex = 0;

pBuilding->AddRect();

auto pBuildingSA = new CBuildingSA(pBuilding);

AddBuildingToPool(pClientBuilding, pBuildingSA);

return pBuildingSA;
}

void CPoolsSA::RemoveBuilding(CBuilding* pBuilding)
{
assert(NULL != pBuilding);

CBuildingSAInterface* pInterface = pBuilding->GetBuildingInterface();

DWORD dwElementIndexInPool = GetBuildingPoolIndex((std::uint8_t*)pInterface);
if (dwElementIndexInPool >= MAX_BUILDINGS)
{
return;
}

auto* pBuildingSA = m_buildingPool.arrayOfClientEntities[dwElementIndexInPool].pEntity;
m_buildingPool.arrayOfClientEntities[dwElementIndexInPool] = {nullptr, nullptr};

// Delete it from memory
delete pBuildingSA;

// Decrease the count of elements in the pool
--m_buildingPool.ulCount;
}

//////////////////////////////////////////////////////////////////////////////////////////
// PEDS POOL //
//////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -676,7 +756,7 @@ DWORD CPoolsSA::GetPedPoolIndex(std::uint8_t* pInterface)
{
return MAX_PEDS;
}
return ((pInterface - pTheObjects) / dwAlignedSize);
return ((pInterface - pTheObjects) / dwAlignedSize);
}

DWORD CPoolsSA::GetVehiclePoolIndex(std::uint8_t* pInterface)
Expand All @@ -703,6 +783,18 @@ DWORD CPoolsSA::GetObjectPoolIndex(std::uint8_t* pInterface)
return ((pInterface - pTheObjects) / dwAlignedSize);
}

DWORD CPoolsSA::GetBuildingPoolIndex(std::uint8_t* pInterface)
{
DWORD dwAlignedSize = 412;
std::uint8_t* pTheObjects = (std::uint8_t*)(*m_ppBuildingPoolInterface)->m_pObjects;
DWORD dwMaxIndex = MAX_BUILDINGS - 1;
if (pInterface < pTheObjects || pInterface > pTheObjects + (dwMaxIndex * dwAlignedSize))
{
return MAX_BUILDINGS;
}
return ((pInterface - pTheObjects) / dwAlignedSize);
}

uint CPoolsSA::GetModelIdFromClump(RpClump* pRpClump)
{
// Search our pools for a match
Expand Down
30 changes: 20 additions & 10 deletions Client/game_sa/CPoolsSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "CPedSA.h"
#include "CVehicleSA.h"
#include "CObjectSA.h"
#include "CBuildingSA.h"
#include "CTextureDictonarySA.h"

#define INVALID_POOL_ARRAY_ID 0xFFFFFFFF
Expand Down Expand Up @@ -170,6 +171,14 @@ class CPoolsSA : public CPools
unsigned long GetObjectCount() { return m_objectPool.ulCount; }
void DeleteAllObjects();

// Buildings pool
private:
bool AddBuildingToPool(CClientBuilding* pClientBuilding, CBuildingSA* pBuilding);

public:
CBuilding* AddBuilding(class CClientBuilding*, uint16_t modelId, CVector vPos, CVector4D vRot, uint8_t interior);
void RemoveBuilding(CBuilding* pBuilding);

// Peds pool
CPed* AddPed(CClientPed* pClientPed, unsigned int nModelIndex);
CPed* AddPed(CClientPed* pClientPed, DWORD* pGameInterface);
Expand All @@ -195,6 +204,7 @@ class CPoolsSA : public CPools
DWORD GetPedPoolIndex(std::uint8_t* pInterface);
DWORD GetVehiclePoolIndex(std::uint8_t* pInterfacee);
DWORD GetObjectPoolIndex(std::uint8_t* pInterface);
DWORD GetBuildingPoolIndex(std::uint8_t* pInterface);

int GetNumberOfUsedSpaces(ePools pools);
int GetPoolDefaultCapacity(ePools pool);
Expand Down Expand Up @@ -231,16 +241,16 @@ class CPoolsSA : public CPools
};

// Pools
typedef SPoolData<CVehicleSA, CVehicleSAInterface, MAX_VEHICLES> vehiclePool_t;
typedef SPoolData<CPedSA, CPedSAInterface, MAX_PEDS> pedPool_t;
typedef SPoolData<CObjectSA, CObjectSAInterface, MAX_OBJECTS> objectPool_t;
vehiclePool_t m_vehiclePool;
pedPool_t m_pedPool;
objectPool_t m_objectPool;
CPoolSAInterface<CPedSAInterface>** m_ppPedPoolInterface;
CPoolSAInterface<CObjectSAInterface>** m_ppObjectPoolInterface;
CPoolSAInterface<CVehicleSAInterface>** m_ppVehiclePoolInterface;
CPoolSAInterface<CTextureDictonarySAInterface>** m_ppTxdPoolInterface;
SPoolData<CVehicleSA, CVehicleSAInterface, MAX_VEHICLES> m_vehiclePool;
SPoolData<CPedSA, CPedSAInterface, MAX_PEDS> m_pedPool;
SPoolData<CObjectSA, CObjectSAInterface, MAX_OBJECTS> m_objectPool;
SPoolData<CBuildingSA, CBuildingSAInterface, MAX_BUILDINGS> m_buildingPool;

CPoolSAInterface<CPedSAInterface>** m_ppPedPoolInterface;
CPoolSAInterface<CObjectSAInterface>** m_ppObjectPoolInterface;
CPoolSAInterface<CVehicleSAInterface>** m_ppVehiclePoolInterface;
CPoolSAInterface<CTextureDictonarySAInterface>** m_ppTxdPoolInterface;
CPoolSAInterface<CBuildingSAInterface>** m_ppBuildingPoolInterface;

bool m_bGetVehicleEnabled;
};
Expand Down
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/StdInc.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <CClientCommon.h>
#include <CClientManager.h>
#include <CClient3DMarker.h>
#include <CClientBuilding.h>
#include <CClientCheckpoint.h>
#include <CClientColShape.h>
#include <CClientColCircle.h>
Expand Down Expand Up @@ -143,6 +144,7 @@
#include <luadefs/CLuaWaterDefs.h>
#include <luadefs/CLuaWeaponDefs.h>
#include <luadefs/CLuaWorldDefs.h>
#include <luadefs/CLuaBuildingDefs.h>
#include <CRemoteCalls.h>

// Shared includes
Expand Down
53 changes: 53 additions & 0 deletions Client/mods/deathmatch/logic/CClientBuilding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* (Shared logic for modifications)
* LICENSE: See LICENSE in the top level directory
* FILE: mods/shared_logic/CClientBuilding.cpp
* PURPOSE: Buildings handling class
*
*****************************************************************************/

#include "StdInc.h"

CClientBuilding::CClientBuilding(class CClientManager* pManager, ElementID ID, uint16_t usModelId, CVector pos, CVector4D 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_pBuilding = nullptr;
m_pManager = pManager;
SetTypeName("building");
m_pBuildingManager->AddToList(this);
Create();
}

CClientBuilding::~CClientBuilding()
{
m_pBuildingManager->RemoveFromList(this);
if (m_pBuilding)
{
Destroy();
}
}

void CClientBuilding::Create()
{
if (m_pBuilding)
return;

m_pBuilding = g_pGame->GetPools()->AddBuilding(this, m_usModelId, m_vPos, m_vRot, m_interior);
}

void CClientBuilding::Destroy()
{
if (m_pBuilding)
{
g_pGame->GetPools()->RemoveBuilding(m_pBuilding);
m_pBuilding = nullptr;
}
}
Loading

0 comments on commit f8e7139

Please sign in to comment.