Skip to content

Commit

Permalink
Fix custom models and setElementModel
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Dec 18, 2023
1 parent 388b4fe commit 96bfdb8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 38 deletions.
9 changes: 9 additions & 0 deletions Client/mods/deathmatch/logic/CClientBuilding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ void CClientBuilding::SetInterior(uint8_t ucInterior)
{
m_interior = ucInterior;
Recreate();
}

void CClientBuilding::SetModel(uint16_t model)
{
if (CClientObjectManager::IsValidModel(model))
{
m_usModelId = model;
Recreate();
}
}

void CClientBuilding::Create()
Expand Down
3 changes: 3 additions & 0 deletions Client/mods/deathmatch/logic/CClientBuilding.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class CClientBuilding : public CClientEntity

void SetInterior(uint8_t ucInterior) override;

uint16_t GetModel() { return m_usModelId; };
void SetModel(uint16_t ulModel);

eClientEntityType GetType() const { return CCLIENTBUILDING; }

private:
Expand Down
32 changes: 28 additions & 4 deletions Client/mods/deathmatch/logic/CClientModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ void CClientModel::RestoreEntitiesUsingThisModel()

void CClientModel::RestoreDFF(CModelInfo* pModelInfo)
{
auto callElementChangeEvent = [](auto &element, unsigned short usParentID, auto modelId) {
CLuaArguments Arguments;
Arguments.PushNumber(modelId);
Arguments.PushNumber(usParentID);
element.CallEvent("onClientElementModelChange", Arguments, true);
};

auto unloadModelsAndCallEvents = [&](auto iterBegin, auto iterEnd, unsigned short usParentID, auto setElementModelLambda) {
for (auto iter = iterBegin; iter != iterEnd; iter++)
{
Expand All @@ -135,10 +142,21 @@ void CClientModel::RestoreDFF(CModelInfo* pModelInfo)

setElementModelLambda(element);

CLuaArguments Arguments;
Arguments.PushNumber(m_iModelID);
Arguments.PushNumber(usParentID);
element.CallEvent("onClientElementModelChange", Arguments, true);
callElementChangeEvent(element, usParentID, m_iModelID);
}
};

auto unloadModelsAndCallEventsNonStreamed = [&](auto iterBegin, auto iterEnd, unsigned short usParentID, auto setElementModelLambda)
{
for (auto iter = iterBegin; iter != iterEnd; iter++)
{
auto& element = **iter;

if (element.GetModel() != m_iModelID)
continue;

setElementModelLambda(element);
callElementChangeEvent(element, usParentID, m_iModelID);
}
};

Expand Down Expand Up @@ -166,6 +184,12 @@ void CClientModel::RestoreDFF(CModelInfo* pModelInfo)

unloadModelsAndCallEvents(pPickupManager->IterBegin(), pPickupManager->IterEnd(), usParentID, [=](auto& element) { element.SetModel(usParentID); });

// Restore buildings
CClientBuildingManager* pBuildingsManager = g_pClientGame->GetManager()->GetBuildingManager();

unloadModelsAndCallEventsNonStreamed(pBuildingsManager->IterBegin(), pBuildingsManager->IterEnd(), usParentID,
[=](auto& element) { element.SetModel(usParentID); });

// Restore COL
g_pClientGame->GetManager()->GetColModelManager()->RestoreModel(m_iModelID);
break;
Expand Down
67 changes: 33 additions & 34 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,26 @@ bool CStaticFunctionDefinitions::SetElementModel(CClientEntity& Entity, unsigned
{
RUN_CHILDREN(SetElementModel(**iter, usModel))

auto callOnChangeEvent = [](auto &element, uint16_t usCurrentModel, uint16_t usModel) {
CLuaArguments Arguments;
Arguments.PushNumber(usCurrentModel);
Arguments.PushNumber(usModel);
bool bContinue = element.CallEvent("onClientElementModelChange", Arguments, true);

// Check for another call to setElementModel
if (usModel != element.GetModel())
return false;

if (!bContinue)
{
// Change canceled
element.SetModel(usCurrentModel);
return false;
}

return true;
};

switch (Entity.GetType())
{
case CCLIENTPED:
Expand All @@ -1476,23 +1496,7 @@ bool CStaticFunctionDefinitions::SetElementModel(CClientEntity& Entity, unsigned
if (!Ped.SetModel(usModel))
return false;

CLuaArguments Arguments;
Arguments.PushNumber(usCurrentModel);
Arguments.PushNumber(usModel);
bool bContinue = Ped.CallEvent("onClientElementModelChange", Arguments, true);

// Check for another call to setElementModel
if (usModel != Ped.GetModel())
return false;

if (!bContinue)
{
// Change canceled
Ped.SetModel(usCurrentModel);
return false;
}

break;
return callOnChangeEvent(Ped, usCurrentModel, usModel);
}
case CCLIENTVEHICLE:
{
Expand Down Expand Up @@ -1539,23 +1543,22 @@ bool CStaticFunctionDefinitions::SetElementModel(CClientEntity& Entity, unsigned

Object.SetModel(usModel);

CLuaArguments Arguments;
Arguments.PushNumber(usCurrentModel);
Arguments.PushNumber(usModel);
bool bContinue = Object.CallEvent("onClientElementModelChange", Arguments, true);
return callOnChangeEvent(Object, usCurrentModel, usModel);
}
case CCLIENTBUILDING:
{
CClientBuilding& Object = static_cast<CClientBuilding&>(Entity);
const unsigned short usCurrentModel = Object.GetModel();

// Check for another call to setElementModel
if (usModel != Object.GetModel())
if (usCurrentModel == usModel)
return false;

if (!bContinue)
{
// Change canceled
Object.SetModel(usCurrentModel);
if (!CClientObjectManager::IsValidModel(usModel))
return false;
}

break;
Object.SetModel(usModel);

return callOnChangeEvent(Object, usCurrentModel, usModel);
}
case CCLIENTPROJECTILE:
{
Expand All @@ -1570,11 +1573,7 @@ bool CStaticFunctionDefinitions::SetElementModel(CClientEntity& Entity, unsigned

Projectile.SetModel(usModel);

CLuaArguments Arguments;
Arguments.PushNumber(usCurrentModel);
Arguments.PushNumber(usModel);
Projectile.CallEvent("onClientElementModelChange", Arguments, true);
break;
return callOnChangeEvent(Projectile, usCurrentModel, usModel);
}
default:
return false;
Expand Down
3 changes: 3 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaBuildingDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ CClientBuilding* CLuaBuildingDefs::CreateBuilding(lua_State* const luaVM, uint16
if (!pResource)
return false;

if (!CClientObjectManager::IsValidModel(modelId))
throw std::invalid_argument("Invalid model id");

// Grab the resource root entity
CClientEntity* pRoot = pResource->GetResourceDynamicEntity();

Expand Down

0 comments on commit 96bfdb8

Please sign in to comment.