1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Avoid some inefficiencies when handling ItemStack(Metadata)

This commit is contained in:
sfan5 2024-12-31 01:26:08 +01:00
parent d884a1624f
commit a2058f7f3a
7 changed files with 37 additions and 51 deletions

View file

@ -12,26 +12,33 @@
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <optional>
struct ItemDefinition;
class IItemDefManager;
/*
* NOTE: these structs intentionally use vector<pair<>> or map<> over unordered_map<>
* to avoid blowing up the structure sizes. Also because the linear "dumb" approach
* works better if you have just a handful of items.
*/
struct ToolGroupCap
{
std::unordered_map<int, float> times;
std::vector<std::pair<int, float>> times;
int maxlevel = 1;
int uses = 20;
ToolGroupCap() = default;
std::optional<float> getTime(int rating) const {
auto i = times.find(rating);
if (i == times.end())
return std::nullopt;
return i->second;
for (auto &it : times) {
if (it.first == rating)
return it.second;
}
return std::nullopt;
}
void toJson(Json::Value &object) const;
@ -39,16 +46,16 @@ struct ToolGroupCap
};
typedef std::unordered_map<std::string, struct ToolGroupCap> ToolGCMap;
typedef std::unordered_map<std::string, s16> DamageGroup;
typedef std::map<std::string, ToolGroupCap> ToolGCMap;
typedef std::map<std::string, s16> DamageGroup;
struct ToolCapabilities
{
float full_punch_interval;
int max_drop_level;
int punch_attack_uses;
ToolGCMap groupcaps;
DamageGroup damageGroups;
int punch_attack_uses;
ToolCapabilities(
float full_punch_interval_ = 1.4f,
@ -59,9 +66,9 @@ struct ToolCapabilities
):
full_punch_interval(full_punch_interval_),
max_drop_level(max_drop_level_),
punch_attack_uses(punch_attack_uses_),
groupcaps(groupcaps_),
damageGroups(damageGroups_),
punch_attack_uses(punch_attack_uses_)
damageGroups(damageGroups_)
{}
void serialize(std::ostream &os, u16 version) const;
@ -76,17 +83,18 @@ private:
struct WearBarParams
{
std::map<f32, video::SColor> colorStops;
enum BlendMode : u8 {
BLEND_MODE_CONSTANT,
BLEND_MODE_LINEAR,
BlendMode_END // Dummy for validity check
};
constexpr const static EnumString es_BlendMode[3] = {
{WearBarParams::BLEND_MODE_CONSTANT, "constant"},
{WearBarParams::BLEND_MODE_LINEAR, "linear"},
{BLEND_MODE_CONSTANT, "constant"},
{BLEND_MODE_LINEAR, "linear"},
{0, nullptr}
};
std::map<f32, video::SColor> colorStops;
BlendMode blend;
WearBarParams(const std::map<f32, video::SColor> &colorStops, BlendMode blend):
@ -102,6 +110,7 @@ struct WearBarParams
static WearBarParams deserialize(std::istream &is);
void serializeJson(std::ostream &os) const;
static std::optional<WearBarParams> deserializeJson(std::istream &is);
video::SColor getWearBarColor(f32 durabilityPercent);
};