Skip to content

Commit

Permalink
rename funcs, add isValidModel WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha committed Nov 25, 2024
1 parent 70809d5 commit 5af6b06
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 64 deletions.
1 change: 1 addition & 0 deletions Client/mods/deathmatch/StdInc.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
#include <luadefs/CLuaMarkerDefs.h>
#include <luadefs/CLuaNetworkDefs.h>
#include <luadefs/CLuaObjectDefs.h>
#include <luadefs/CLuaModelDefs.h>
#include <luadefs/CLuaPointLightDefs.h>
#include <luadefs/CLuaPedDefs.h>
#include <luadefs/CLuaPickupDefs.h>
Expand Down
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/lua/CLuaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ void CLuaManager::LoadCFunctions()
CLuaMarkerDefs::LoadFunctions();
CLuaNetworkDefs::LoadFunctions();
CLuaObjectDefs::LoadFunctions();
CLuaModelDefs::LoadFunctions();
CLuaPedDefs::LoadFunctions();
CLuaPickupDefs::LoadFunctions();
CLuaPlayerDefs::LoadFunctions();
Expand Down
77 changes: 77 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: mods/shared_logic/luadefs/CLuaModelDefs.cpp
* PURPOSE: Lua model definitions class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"
#include <lua/CLuaFunctionParser.h>
#include "CLodModels.h"
#include "CClientObjectManager.h"
#include "CClientBuildingManager.h"

void CLuaModelDefs::LoadFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
// Util func
{"IsValidModel", ArgumentParser<IsValidModel>},

// LOD funcs
{"getLowLODModel", ArgumentParser<GetLowLODModel>},
{"getHighLODModel", ArgumentParser<GetHighLODModel>},
{"setLowLODModel", ArgumentParser<SetLowLODModel>},
{"resetLowLODModel", ArgumentParser<ResetLowLODModel>},
{"resetLowLODModels", ArgumentParser<ResetLowLODModels>},
};

// Add functions
for (const auto& [name, func] : functions)
CLuaCFunctions::AddFunction(name, func);
}


bool CLuaModelDefs::IsValidModel(std::string modelPurpose, std::uint32_t id)
{
if (modelPurpose == "object")
return CClientObjectManager::IsValidModel(id);
else if (modelPurpose == "building")
return CClientBuildingManager::IsValidModel(id);

throw std::invalid_argument("Invalid model purpose passed, expected 'object' or 'building'");
}

std::variant<bool, std::uint32_t> CLuaModelDefs::GetLowLODModel(std::uint32_t hLODModel) noexcept
{
return CLodModels::GetLowLODModel(hLODModel);
}

std::variant<bool, std::uint32_t> CLuaModelDefs::GetHighLODModel(std::uint32_t lLODModel) noexcept
{
return CLodModels::GetHighLODModel(lLODModel);
}

bool CLuaModelDefs::SetLowLODModel(std::string modelPurpose, std::uint32_t hLODModel, std::uint32_t lLODModel)
{
if (!IsValidModel(modelPurpose, hLODModel))
throw std::invalid_argument("Invalid model ID passed for High-LOD argument");

if (!IsValidModel(modelPurpose, lLODModel))
throw std::invalid_argument("Invalid model ID passed for Low-LOD argument");

return CLodModels::SetLowLODModel(hLODModel, lLODModel);
}

bool CLuaModelDefs::ResetLowLODModel(std::uint32_t hLODModel) noexcept
{
return CLodModels::ResetLowLODModel(hLODModel);
}

void CLuaModelDefs::ResetLowLODModels() noexcept
{
CLodModels::ResetLowLODModels();
}
29 changes: 29 additions & 0 deletions Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto
* LICENSE: See LICENSE in the top level directory
* FILE: mods/shared_logic/luadefs/CLuaModelDefs.h
* PURPOSE: Lua model definitions class header
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once
#include "CLuaDefs.h"

class CLuaModelDefs : public CLuaDefs
{
public:
static void LoadFunctions();

// Util func
static bool IsValidModel(std::string modelPurpose, std::uint32_t id);

// LOD funcs
static std::variant<bool, std::uint32_t> GetLowLODModel(std::uint32_t hLODModel) noexcept;
static std::variant<bool, std::uint32_t> GetHighLODModel(std::uint32_t lLODModel) noexcept;
static bool SetLowLODModel(std::string modelPurpose, std::uint32_t hLODModel, std::uint32_t lLODModel);
static bool ResetLowLODModel(std::uint32_t hLODModel) noexcept;
static void ResetLowLODModels() noexcept;
};
40 changes: 2 additions & 38 deletions Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "StdInc.h"
#include <lua/CLuaFunctionParser.h>
#include "CLodModels.h"
#include "CClientObjectManager.h"
#include "CClientBuildingManager.h"

void CLuaObjectDefs::LoadFunctions()
{
Expand Down Expand Up @@ -39,13 +41,6 @@ void CLuaObjectDefs::LoadFunctions()
{"toggleObjectRespawn", ToggleObjectRespawn},
{"setObjectMass", SetObjectMass},
{"setObjectProperty", SetObjectProperty},

// Object util functions
{"getObjectLowLODModel", ArgumentParser<GetObjectLowLODModel>},
{"getObjectHighLODModel", ArgumentParser<GetObjectHighLODModel>},
{"setObjectCustomLowLODModel", ArgumentParser<SetObjectCustomLowLODModel>},
{"resetObjectCustomLowLODModel", ArgumentParser<ResetObjectCustomLowLODModel>},
{"resetAllObjectCustomLowLODModels", ArgumentParser<ResetAllObjectCustomLowLODModels>},
};

// Add functions
Expand Down Expand Up @@ -729,34 +724,3 @@ bool CLuaObjectDefs::IsObjectRespawnable(CClientEntity* const pEntity) noexcept

return pObject->IsRespawnEnabled();
}

std::variant<bool, std::uint32_t> CLuaObjectDefs::GetObjectLowLODModel(std::uint32_t hLODModel) noexcept
{
std::uint32_t lodModel = CLodModels::GetObjectLowLODModel(hLODModel);
if (lodModel == 0) // LLOD Model not found for HLOD Model provided
return false;
return lodModel;
}

std::variant<bool, std::uint32_t> CLuaObjectDefs::GetObjectHighLODModel(std::uint32_t lLODModel) noexcept
{
std::uint32_t objModel = CLodModels::GetObjectHighLODModel(lLODModel);
if (objModel == 0) // HLOD Model not found for LLOD Model provided
return false;
return objModel;
}

void CLuaObjectDefs::SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept
{
CLodModels::SetObjectCustomLowLODModel(hLODModel, lLODModel);
}

void CLuaObjectDefs::ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept
{
CLodModels::ResetObjectCustomLowLODModel(hLODModel);
}

void CLuaObjectDefs::ResetAllObjectCustomLowLODModels() noexcept
{
CLodModels::ResetAllObjectCustomLowLODModels();
}
8 changes: 2 additions & 6 deletions Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ class CLuaObjectDefs : public CLuaDefs
LUA_DECLARE(SetObjectMass);
LUA_DECLARE(SetObjectProperty);

// Object util funcs
static std::variant<bool, std::uint32_t> GetObjectLowLODModel(std::uint32_t hLODModel) noexcept;
static std::variant<bool, std::uint32_t> GetObjectHighLODModel(std::uint32_t lLODModel) noexcept;
static void SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept;
static void ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept;
static void ResetAllObjectCustomLowLODModels() noexcept;
// Object util func
static bool IsValidObjectModel(std::uint32_t id, std::optional<std::string> modelPurpose) noexcept;
};
30 changes: 16 additions & 14 deletions Shared/mods/deathmatch/logic/CLodModels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4322,54 +4322,56 @@ const std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_reversePred
std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_customLODModels;
std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_reverseCustomLODModels;

void CLodModels::UpdateReverseCustomLODModels()
void CLodModels::UpdateReverseCustomLODModels() noexcept
{
m_reverseCustomLODModels.clear();
for (const auto& entry : m_customLODModels)
m_reverseCustomLODModels[entry.second] = entry.first;
}

std::uint32_t CLodModels::GetObjectLowLODModel(std::uint32_t hLODModel) noexcept
std::variant<bool, std::uint32_t> CLodModels::GetLowLODModel(std::uint32_t hLODModel) noexcept
{
// Check custom LOD models first
if (auto it = m_customLODModels.find(hLODModel); it != m_customLODModels.end())
return it->second;

// Check predefined LOD models
if (auto it2 = m_predefinedLODModels.find(hLODModel); it2 != m_predefinedLODModels.end())
return it2->second;

// Default value if no match found
return 0;
return false;
}

std::uint32_t CLodModels::GetObjectHighLODModel(std::uint32_t lLODModel) noexcept
std::variant<bool, std::uint32_t> CLodModels::GetHighLODModel(std::uint32_t lLODModel) noexcept
{
// Check custom LOD models reverse lookup map
if (auto it = m_reverseCustomLODModels.find(lLODModel); it != m_reverseCustomLODModels.end())
return it->second;

// Check predefined LOD models reverse lookup map
if (auto it2 = m_reversePredefinedLODModels.find(lLODModel); it2 != m_reversePredefinedLODModels.end())
return it2->second;

// Default return value if no match found
return 0;
return false;
}

void CLodModels::SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept
bool CLodModels::SetLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept
{
if (m_customLODModels.find(hLODModel) != m_customLODModels.end())
return false;

m_customLODModels[hLODModel] = lLODModel;
UpdateReverseCustomLODModels();
return true;
}

void CLodModels::ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept
bool CLodModels::ResetLowLODModel(std::uint32_t hLODModel) noexcept
{
if (m_customLODModels.find(hLODModel) == m_customLODModels.end())
return false;

m_customLODModels.erase(hLODModel);
UpdateReverseCustomLODModels();
return true;
}

void CLodModels::ResetAllObjectCustomLowLODModels() noexcept
void CLodModels::ResetLowLODModels() noexcept
{
m_customLODModels.clear();
UpdateReverseCustomLODModels();
Expand Down
15 changes: 9 additions & 6 deletions Shared/mods/deathmatch/logic/CLodModels.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
*****************************************************************************/
#pragma once

#include <variant>

class CLodModels
{
public:
static std::uint32_t GetObjectLowLODModel(std::uint32_t hLODModel) noexcept;
static std::uint32_t GetObjectHighLODModel(std::uint32_t lLODModel) noexcept;
static std::variant<bool, std::uint32_t> GetLowLODModel(std::uint32_t hLODModel) noexcept;
static std::variant<bool, std::uint32_t> GetHighLODModel(std::uint32_t lLODModel) noexcept;

static bool SetLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept;
static bool ResetLowLODModel(std::uint32_t hLODModel) noexcept;

static void SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept;
static void ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept;
static void ResetAllObjectCustomLowLODModels() noexcept;
static void ResetLowLODModels() noexcept;

private:
// This map contains all HLOD Object Model ID -> LLOD Object Model ID associations
Expand All @@ -28,5 +31,5 @@ class CLodModels
static std::unordered_map<std::uint32_t, std::uint32_t> m_customLODModels;
static std::unordered_map<std::uint32_t, std::uint32_t> m_reverseCustomLODModels;

static void UpdateReverseCustomLODModels(); // Method to update the reverse map
static void UpdateReverseCustomLODModels() noexcept;
};

0 comments on commit 5af6b06

Please sign in to comment.