From 5af6b06aa4e6d8e43ad2b10ee6363e66cd006cd3 Mon Sep 17 00:00:00 2001 From: Fernando-A-Rocha Date: Mon, 25 Nov 2024 16:07:09 +0000 Subject: [PATCH] rename funcs, add isValidModel WIP --- Client/mods/deathmatch/StdInc.h | 1 + .../mods/deathmatch/logic/lua/CLuaManager.cpp | 1 + .../logic/luadefs/CLuaModelDefs.cpp | 77 +++++++++++++++++++ .../deathmatch/logic/luadefs/CLuaModelDefs.h | 29 +++++++ .../logic/luadefs/CLuaObjectDefs.cpp | 40 +--------- .../deathmatch/logic/luadefs/CLuaObjectDefs.h | 8 +- Shared/mods/deathmatch/logic/CLodModels.cpp | 30 ++++---- Shared/mods/deathmatch/logic/CLodModels.h | 15 ++-- 8 files changed, 137 insertions(+), 64 deletions(-) create mode 100644 Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.cpp create mode 100644 Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.h diff --git a/Client/mods/deathmatch/StdInc.h b/Client/mods/deathmatch/StdInc.h index 465478454f..591bf8d8f6 100644 --- a/Client/mods/deathmatch/StdInc.h +++ b/Client/mods/deathmatch/StdInc.h @@ -130,6 +130,7 @@ #include #include #include +#include #include #include #include diff --git a/Client/mods/deathmatch/logic/lua/CLuaManager.cpp b/Client/mods/deathmatch/logic/lua/CLuaManager.cpp index c2a28809af..1560185580 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaManager.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaManager.cpp @@ -260,6 +260,7 @@ void CLuaManager::LoadCFunctions() CLuaMarkerDefs::LoadFunctions(); CLuaNetworkDefs::LoadFunctions(); CLuaObjectDefs::LoadFunctions(); + CLuaModelDefs::LoadFunctions(); CLuaPedDefs::LoadFunctions(); CLuaPickupDefs::LoadFunctions(); CLuaPlayerDefs::LoadFunctions(); diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.cpp new file mode 100644 index 0000000000..b3d67e09a9 --- /dev/null +++ b/Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.cpp @@ -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 +#include "CLodModels.h" +#include "CClientObjectManager.h" +#include "CClientBuildingManager.h" + +void CLuaModelDefs::LoadFunctions() +{ + constexpr static const std::pair functions[]{ + // Util func + {"IsValidModel", ArgumentParser}, + + // LOD funcs + {"getLowLODModel", ArgumentParser}, + {"getHighLODModel", ArgumentParser}, + {"setLowLODModel", ArgumentParser}, + {"resetLowLODModel", ArgumentParser}, + {"resetLowLODModels", ArgumentParser}, + }; + + // 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 CLuaModelDefs::GetLowLODModel(std::uint32_t hLODModel) noexcept +{ + return CLodModels::GetLowLODModel(hLODModel); +} + +std::variant 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(); +} diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.h new file mode 100644 index 0000000000..80d14a0d15 --- /dev/null +++ b/Client/mods/deathmatch/logic/luadefs/CLuaModelDefs.h @@ -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 GetLowLODModel(std::uint32_t hLODModel) noexcept; + static std::variant 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; +}; diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp index e4b7701550..707cbfe2d8 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp @@ -12,6 +12,8 @@ #include "StdInc.h" #include #include "CLodModels.h" +#include "CClientObjectManager.h" +#include "CClientBuildingManager.h" void CLuaObjectDefs::LoadFunctions() { @@ -39,13 +41,6 @@ void CLuaObjectDefs::LoadFunctions() {"toggleObjectRespawn", ToggleObjectRespawn}, {"setObjectMass", SetObjectMass}, {"setObjectProperty", SetObjectProperty}, - - // Object util functions - {"getObjectLowLODModel", ArgumentParser}, - {"getObjectHighLODModel", ArgumentParser}, - {"setObjectCustomLowLODModel", ArgumentParser}, - {"resetObjectCustomLowLODModel", ArgumentParser}, - {"resetAllObjectCustomLowLODModels", ArgumentParser}, }; // Add functions @@ -729,34 +724,3 @@ bool CLuaObjectDefs::IsObjectRespawnable(CClientEntity* const pEntity) noexcept return pObject->IsRespawnEnabled(); } - -std::variant 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 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(); -} diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h index b7128cb7ce..580d040dd5 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h @@ -44,10 +44,6 @@ class CLuaObjectDefs : public CLuaDefs LUA_DECLARE(SetObjectMass); LUA_DECLARE(SetObjectProperty); - // Object util funcs - static std::variant GetObjectLowLODModel(std::uint32_t hLODModel) noexcept; - static std::variant 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 modelPurpose) noexcept; }; diff --git a/Shared/mods/deathmatch/logic/CLodModels.cpp b/Shared/mods/deathmatch/logic/CLodModels.cpp index d0f62f6ea5..eac25251dc 100644 --- a/Shared/mods/deathmatch/logic/CLodModels.cpp +++ b/Shared/mods/deathmatch/logic/CLodModels.cpp @@ -4322,54 +4322,56 @@ const std::unordered_map CLodModels::m_reversePred std::unordered_map CLodModels::m_customLODModels; std::unordered_map 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 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 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(); diff --git a/Shared/mods/deathmatch/logic/CLodModels.h b/Shared/mods/deathmatch/logic/CLodModels.h index c9efac2c7b..9488e23af9 100644 --- a/Shared/mods/deathmatch/logic/CLodModels.h +++ b/Shared/mods/deathmatch/logic/CLodModels.h @@ -8,15 +8,18 @@ *****************************************************************************/ #pragma once +#include + 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 GetLowLODModel(std::uint32_t hLODModel) noexcept; + static std::variant 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 @@ -28,5 +31,5 @@ class CLodModels static std::unordered_map m_customLODModels; static std::unordered_map m_reverseCustomLODModels; - static void UpdateReverseCustomLODModels(); // Method to update the reverse map + static void UpdateReverseCustomLODModels() noexcept; };