Skip to content

Commit 9c117fd

Browse files
optimize lookups
1 parent 0b771df commit 9c117fd

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

Shared/mods/deathmatch/logic/CLodModels.cpp

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
#include "StdInc.h"
1111
#include "CLodModels.h"
1212

13-
// This array contains all HLOD Object Model ID -> LLOD Object Model ID associations according to the game's IPL files (plaintext + binary).
1413
constexpr std::size_t OBJ_LOD_MODELS_COUNT = 4289;
15-
constexpr std::array<std::pair<std::uint32_t, std::uint32_t>, OBJ_LOD_MODELS_COUNT> OBJ_LOD_MODELS = {{
14+
constexpr std::pair<std::uint32_t, std::uint32_t> OBJ_LOD_MODELS_ARRAY[] = {
1615
{694, 784}, // sm_redwoodgrp => lod_redwoodgrp (countryS)
1716
{791, 785}, // vbg_fir_copse => lod_vbg_fir_co (countrye)
1817
{1259, 1268}, // billbd1 => lodlbd1 (LAe)
@@ -4302,54 +4301,76 @@ constexpr std::array<std::pair<std::uint32_t, std::uint32_t>, OBJ_LOD_MODELS_COU
43024301
{18552, 18554}, // cunts_ammun => cunts_ammun_lod (countryS)
43034302
{18561, 18562}, // cs_newbridge => cs_newbridge_lod (countryS)
43044303
{18563, 18564}, // cs_bsupport => cs_bsupportlod (countryS)
4305-
}};
4304+
};
4305+
4306+
const std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_predefinedLODModels = []()
4307+
{
4308+
std::unordered_map<std::uint32_t, std::uint32_t> map;
4309+
for (const auto& pair : OBJ_LOD_MODELS_ARRAY)
4310+
map.emplace(pair.first, pair.second);
4311+
return map;
4312+
}();
4313+
4314+
const std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_reversePredefinedLODModels = []()
4315+
{
4316+
std::unordered_map<std::uint32_t, std::uint32_t> map;
4317+
for (const auto& entry : OBJ_LOD_MODELS_ARRAY)
4318+
map[entry.second] = entry.first;
4319+
return map;
4320+
}();
43064321

4307-
// This map is for custom definitions of LLOD models for HLOD models.
43084322
std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_customLODModels;
4323+
std::unordered_map<std::uint32_t, std::uint32_t> CLodModels::m_reverseCustomLODModels;
4324+
4325+
void CLodModels::UpdateReverseCustomLODModels()
4326+
{
4327+
m_reverseCustomLODModels.clear();
4328+
for (const auto& entry : m_customLODModels)
4329+
m_reverseCustomLODModels[entry.second] = entry.first;
4330+
}
43094331

43104332
std::uint32_t CLodModels::GetObjectLowLODModel(std::uint32_t hLODModel) noexcept
43114333
{
4312-
auto it = CLodModels::m_customLODModels.find(hLODModel);
4313-
if (it != CLodModels::m_customLODModels.end())
4334+
// Check custom LOD models first
4335+
if (auto it = m_customLODModels.find(hLODModel); it != m_customLODModels.end())
43144336
return it->second;
43154337

4316-
for (const auto& entry : OBJ_LOD_MODELS)
4317-
{
4318-
if (entry.first == hLODModel)
4319-
return entry.second;
4320-
}
4338+
// Fallback to predefined LOD models
4339+
if (auto it2 = m_predefinedLODModels.find(hLODModel); it2 != m_predefinedLODModels.end())
4340+
return it2->second;
43214341

4342+
// Default value if no match found
43224343
return 0;
43234344
}
43244345

43254346
std::uint32_t CLodModels::GetObjectHighLODModel(std::uint32_t lLODModel) noexcept
43264347
{
4327-
for (const auto& entry : CLodModels::m_customLODModels)
4328-
{
4329-
if (entry.second == lLODModel)
4330-
return entry.first;
4331-
}
4348+
// Check custom reverse lookup map
4349+
if (auto it = m_reverseCustomLODModels.find(lLODModel); it != m_reverseCustomLODModels.end())
4350+
return it->second;
43324351

4333-
for (const auto& entry : OBJ_LOD_MODELS)
4334-
{
4335-
if (entry.second == lLODModel)
4336-
return entry.first;
4337-
}
4352+
// Check predefined reverse lookup map
4353+
if (auto it2 = m_reversePredefinedLODModels.find(lLODModel); it2 != m_reversePredefinedLODModels.end())
4354+
return it2->second;
43384355

4356+
// Default return value if no match found
43394357
return 0;
43404358
}
43414359

43424360
void CLodModels::SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept
43434361
{
4344-
CLodModels::m_customLODModels[hLODModel] = lLODModel;
4362+
m_customLODModels[hLODModel] = lLODModel;
4363+
UpdateReverseCustomLODModels();
43454364
}
43464365

43474366
void CLodModels::ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept
43484367
{
4349-
CLodModels::m_customLODModels.erase(hLODModel);
4368+
m_customLODModels.erase(hLODModel);
4369+
UpdateReverseCustomLODModels();
43504370
}
43514371

43524372
void CLodModels::ResetAllObjectCustomLowLODModels() noexcept
43534373
{
4354-
CLodModels::m_customLODModels.clear();
4374+
m_customLODModels.clear();
4375+
UpdateReverseCustomLODModels();
43554376
}

Shared/mods/deathmatch/logic/CLodModels.h

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

11-
#include <map>
12-
#include <memory>
13-
#include <cstdint>
14-
1511
class CLodModels
1612
{
1713
public:
1814
static std::uint32_t GetObjectLowLODModel(std::uint32_t hLODModel) noexcept;
1915
static std::uint32_t GetObjectHighLODModel(std::uint32_t lLODModel) noexcept;
2016

2117
static void SetObjectCustomLowLODModel(std::uint32_t hLODModel, std::uint32_t lLODModel) noexcept;
22-
static void ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept;
23-
static void ResetAllObjectCustomLowLODModels() noexcept;
18+
static void ResetObjectCustomLowLODModel(std::uint32_t hLODModel) noexcept;
19+
static void ResetAllObjectCustomLowLODModels() noexcept;
2420

2521
private:
22+
// This map contains all HLOD Object Model ID -> LLOD Object Model ID associations
23+
// according to the game's IPL files (plaintext + binary).
24+
static const std::unordered_map<std::uint32_t, std::uint32_t> m_predefinedLODModels;
25+
static const std::unordered_map<std::uint32_t, std::uint32_t> m_reversePredefinedLODModels;
26+
27+
// This map is for custom definitions of LLOD models for HLOD models.
2628
static std::unordered_map<std::uint32_t, std::uint32_t> m_customLODModels;
29+
static std::unordered_map<std::uint32_t, std::uint32_t> m_reverseCustomLODModels;
30+
31+
static void UpdateReverseCustomLODModels(); // Method to update the reverse map
2732
};

0 commit comments

Comments
 (0)