Skip to content

Commit 5af6b06

Browse files
rename funcs, add isValidModel WIP
1 parent 70809d5 commit 5af6b06

File tree

8 files changed

+137
-64
lines changed

8 files changed

+137
-64
lines changed

Client/mods/deathmatch/StdInc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
#include <luadefs/CLuaMarkerDefs.h>
131131
#include <luadefs/CLuaNetworkDefs.h>
132132
#include <luadefs/CLuaObjectDefs.h>
133+
#include <luadefs/CLuaModelDefs.h>
133134
#include <luadefs/CLuaPointLightDefs.h>
134135
#include <luadefs/CLuaPedDefs.h>
135136
#include <luadefs/CLuaPickupDefs.h>

Client/mods/deathmatch/logic/lua/CLuaManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ void CLuaManager::LoadCFunctions()
260260
CLuaMarkerDefs::LoadFunctions();
261261
CLuaNetworkDefs::LoadFunctions();
262262
CLuaObjectDefs::LoadFunctions();
263+
CLuaModelDefs::LoadFunctions();
263264
CLuaPedDefs::LoadFunctions();
264265
CLuaPickupDefs::LoadFunctions();
265266
CLuaPlayerDefs::LoadFunctions();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto
4+
* LICENSE: See LICENSE in the top level directory
5+
* FILE: mods/shared_logic/luadefs/CLuaModelDefs.cpp
6+
* PURPOSE: Lua model definitions class
7+
*
8+
* Multi Theft Auto is available from http://www.multitheftauto.com/
9+
*
10+
*****************************************************************************/
11+
12+
#include "StdInc.h"
13+
#include <lua/CLuaFunctionParser.h>
14+
#include "CLodModels.h"
15+
#include "CClientObjectManager.h"
16+
#include "CClientBuildingManager.h"
17+
18+
void CLuaModelDefs::LoadFunctions()
19+
{
20+
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
21+
// Util func
22+
{"IsValidModel", ArgumentParser<IsValidModel>},
23+
24+
// LOD funcs
25+
{"getLowLODModel", ArgumentParser<GetLowLODModel>},
26+
{"getHighLODModel", ArgumentParser<GetHighLODModel>},
27+
{"setLowLODModel", ArgumentParser<SetLowLODModel>},
28+
{"resetLowLODModel", ArgumentParser<ResetLowLODModel>},
29+
{"resetLowLODModels", ArgumentParser<ResetLowLODModels>},
30+
};
31+
32+
// Add functions
33+
for (const auto& [name, func] : functions)
34+
CLuaCFunctions::AddFunction(name, func);
35+
}
36+
37+
38+
bool CLuaModelDefs::IsValidModel(std::string modelPurpose, std::uint32_t id)
39+
{
40+
if (modelPurpose == "object")
41+
return CClientObjectManager::IsValidModel(id);
42+
else if (modelPurpose == "building")
43+
return CClientBuildingManager::IsValidModel(id);
44+
45+
throw std::invalid_argument("Invalid model purpose passed, expected 'object' or 'building'");
46+
}
47+
48+
std::variant<bool, std::uint32_t> CLuaModelDefs::GetLowLODModel(std::uint32_t hLODModel) noexcept
49+
{
50+
return CLodModels::GetLowLODModel(hLODModel);
51+
}
52+
53+
std::variant<bool, std::uint32_t> CLuaModelDefs::GetHighLODModel(std::uint32_t lLODModel) noexcept
54+
{
55+
return CLodModels::GetHighLODModel(lLODModel);
56+
}
57+
58+
bool CLuaModelDefs::SetLowLODModel(std::string modelPurpose, std::uint32_t hLODModel, std::uint32_t lLODModel)
59+
{
60+
if (!IsValidModel(modelPurpose, hLODModel))
61+
throw std::invalid_argument("Invalid model ID passed for High-LOD argument");
62+
63+
if (!IsValidModel(modelPurpose, lLODModel))
64+
throw std::invalid_argument("Invalid model ID passed for Low-LOD argument");
65+
66+
return CLodModels::SetLowLODModel(hLODModel, lLODModel);
67+
}
68+
69+
bool CLuaModelDefs::ResetLowLODModel(std::uint32_t hLODModel) noexcept
70+
{
71+
return CLodModels::ResetLowLODModel(hLODModel);
72+
}
73+
74+
void CLuaModelDefs::ResetLowLODModels() noexcept
75+
{
76+
CLodModels::ResetLowLODModels();
77+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*****************************************************************************
2+
*
3+
* PROJECT: Multi Theft Auto
4+
* LICENSE: See LICENSE in the top level directory
5+
* FILE: mods/shared_logic/luadefs/CLuaModelDefs.h
6+
* PURPOSE: Lua model definitions class header
7+
*
8+
* Multi Theft Auto is available from http://www.multitheftauto.com/
9+
*
10+
*****************************************************************************/
11+
12+
#pragma once
13+
#include "CLuaDefs.h"
14+
15+
class CLuaModelDefs : public CLuaDefs
16+
{
17+
public:
18+
static void LoadFunctions();
19+
20+
// Util func
21+
static bool IsValidModel(std::string modelPurpose, std::uint32_t id);
22+
23+
// LOD funcs
24+
static std::variant<bool, std::uint32_t> GetLowLODModel(std::uint32_t hLODModel) noexcept;
25+
static std::variant<bool, std::uint32_t> GetHighLODModel(std::uint32_t lLODModel) noexcept;
26+
static bool SetLowLODModel(std::string modelPurpose, std::uint32_t hLODModel, std::uint32_t lLODModel);
27+
static bool ResetLowLODModel(std::uint32_t hLODModel) noexcept;
28+
static void ResetLowLODModels() noexcept;
29+
};

Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.cpp

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "StdInc.h"
1313
#include <lua/CLuaFunctionParser.h>
1414
#include "CLodModels.h"
15+
#include "CClientObjectManager.h"
16+
#include "CClientBuildingManager.h"
1517

1618
void CLuaObjectDefs::LoadFunctions()
1719
{
@@ -39,13 +41,6 @@ void CLuaObjectDefs::LoadFunctions()
3941
{"toggleObjectRespawn", ToggleObjectRespawn},
4042
{"setObjectMass", SetObjectMass},
4143
{"setObjectProperty", SetObjectProperty},
42-
43-
// Object util functions
44-
{"getObjectLowLODModel", ArgumentParser<GetObjectLowLODModel>},
45-
{"getObjectHighLODModel", ArgumentParser<GetObjectHighLODModel>},
46-
{"setObjectCustomLowLODModel", ArgumentParser<SetObjectCustomLowLODModel>},
47-
{"resetObjectCustomLowLODModel", ArgumentParser<ResetObjectCustomLowLODModel>},
48-
{"resetAllObjectCustomLowLODModels", ArgumentParser<ResetAllObjectCustomLowLODModels>},
4944
};
5045

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

730725
return pObject->IsRespawnEnabled();
731726
}
732-
733-
std::variant<bool, std::uint32_t> CLuaObjectDefs::GetObjectLowLODModel(std::uint32_t hLODModel) noexcept
734-
{
735-
std::uint32_t lodModel = CLodModels::GetObjectLowLODModel(hLODModel);
736-
if (lodModel == 0) // LLOD Model not found for HLOD Model provided
737-
return false;
738-
return lodModel;
739-
}
740-
741-
std::variant<bool, std::uint32_t> CLuaObjectDefs::GetObjectHighLODModel(std::uint32_t lLODModel) noexcept
742-
{
743-
std::uint32_t objModel = CLodModels::GetObjectHighLODModel(lLODModel);
744-
if (objModel == 0) // HLOD Model not found for LLOD Model provided
745-
return false;
746-
return objModel;
747-
}
748-
749-
void CLuaObjectDefs::SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept
750-
{
751-
CLodModels::SetObjectCustomLowLODModel(hLODModel, lLODModel);
752-
}
753-
754-
void CLuaObjectDefs::ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept
755-
{
756-
CLodModels::ResetObjectCustomLowLODModel(hLODModel);
757-
}
758-
759-
void CLuaObjectDefs::ResetAllObjectCustomLowLODModels() noexcept
760-
{
761-
CLodModels::ResetAllObjectCustomLowLODModels();
762-
}

Client/mods/deathmatch/logic/luadefs/CLuaObjectDefs.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ class CLuaObjectDefs : public CLuaDefs
4444
LUA_DECLARE(SetObjectMass);
4545
LUA_DECLARE(SetObjectProperty);
4646

47-
// Object util funcs
48-
static std::variant<bool, std::uint32_t> GetObjectLowLODModel(std::uint32_t hLODModel) noexcept;
49-
static std::variant<bool, std::uint32_t> GetObjectHighLODModel(std::uint32_t lLODModel) noexcept;
50-
static void SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept;
51-
static void ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept;
52-
static void ResetAllObjectCustomLowLODModels() noexcept;
47+
// Object util func
48+
static bool IsValidObjectModel(std::uint32_t id, std::optional<std::string> modelPurpose) noexcept;
5349
};

Shared/mods/deathmatch/logic/CLodModels.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4322,54 +4322,56 @@ const std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_reversePred
43224322
std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_customLODModels;
43234323
std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_reverseCustomLODModels;
43244324

4325-
void CLodModels::UpdateReverseCustomLODModels()
4325+
void CLodModels::UpdateReverseCustomLODModels() noexcept
43264326
{
43274327
m_reverseCustomLODModels.clear();
43284328
for (const auto& entry : m_customLODModels)
43294329
m_reverseCustomLODModels[entry.second] = entry.first;
43304330
}
43314331

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

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

4342-
// Default value if no match found
4343-
return 0;
4340+
return false;
43444341
}
43454342

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

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

4356-
// Default return value if no match found
4357-
return 0;
4351+
return false;
43584352
}
43594353

4360-
void CLodModels::SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept
4354+
bool CLodModels::SetLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept
43614355
{
4356+
if (m_customLODModels.find(hLODModel) != m_customLODModels.end())
4357+
return false;
4358+
43624359
m_customLODModels[hLODModel] = lLODModel;
43634360
UpdateReverseCustomLODModels();
4361+
return true;
43644362
}
43654363

4366-
void CLodModels::ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept
4364+
bool CLodModels::ResetLowLODModel(std::uint32_t hLODModel) noexcept
43674365
{
4366+
if (m_customLODModels.find(hLODModel) == m_customLODModels.end())
4367+
return false;
4368+
43684369
m_customLODModels.erase(hLODModel);
43694370
UpdateReverseCustomLODModels();
4371+
return true;
43704372
}
43714373

4372-
void CLodModels::ResetAllObjectCustomLowLODModels() noexcept
4374+
void CLodModels::ResetLowLODModels() noexcept
43734375
{
43744376
m_customLODModels.clear();
43754377
UpdateReverseCustomLODModels();

Shared/mods/deathmatch/logic/CLodModels.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
*****************************************************************************/
99
#pragma once
1010

11+
#include <variant>
12+
1113
class CLodModels
1214
{
1315
public:
14-
static std::uint32_t GetObjectLowLODModel(std::uint32_t hLODModel) noexcept;
15-
static std::uint32_t GetObjectHighLODModel(std::uint32_t lLODModel) noexcept;
16+
static std::variant<bool, std::uint32_t> GetLowLODModel(std::uint32_t hLODModel) noexcept;
17+
static std::variant<bool, std::uint32_t> GetHighLODModel(std::uint32_t lLODModel) noexcept;
18+
19+
static bool SetLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept;
20+
static bool ResetLowLODModel(std::uint32_t hLODModel) noexcept;
1621

17-
static void SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept;
18-
static void ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept;
19-
static void ResetAllObjectCustomLowLODModels() noexcept;
22+
static void ResetLowLODModels() noexcept;
2023

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

31-
static void UpdateReverseCustomLODModels(); // Method to update the reverse map
34+
static void UpdateReverseCustomLODModels() noexcept;
3235
};

0 commit comments

Comments
 (0)