1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +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

@ -47,7 +47,7 @@ void ToolGroupCap::fromJson(const Json::Value &json)
for (Json::ArrayIndex i = 0; i < size; ++i) {
if (times_object[i].isDouble())
times[i] = times_object[i].asFloat();
times.emplace_back(i, times_object[i].asFloat());
}
}
@ -106,7 +106,7 @@ void ToolCapabilities::deSerialize(std::istream &is)
for(u32 i = 0; i < times_size; i++) {
int level = readS16(is);
float time = readF32(is);
cap.times[level] = time;
cap.times.emplace_back(level, time);
}
groupcaps[name] = cap;
}
@ -272,21 +272,11 @@ std::optional<WearBarParams> WearBarParams::deserializeJson(std::istream &is)
return WearBarParams(colorStops, blend);
}
video::SColor WearBarParams::getWearBarColor(f32 durabilityPercent) {
video::SColor WearBarParams::getWearBarColor(f32 durabilityPercent)
{
if (colorStops.empty())
return video::SColor();
/*
* Strategy:
* Find upper bound of durabilityPercent
*
* if it == stops.end() -> return last color in the map
* if it == stops.begin() -> return first color in the map
*
* else:
* lower_bound = it - 1
* interpolate/do constant
*/
auto upper = colorStops.upper_bound(durabilityPercent);
if (upper == colorStops.end()) // durability is >= the highest defined color stop
@ -295,6 +285,7 @@ video::SColor WearBarParams::getWearBarColor(f32 durabilityPercent) {
if (upper == colorStops.begin()) // durability is <= the lowest defined color stop
return upper->second;
// between two values, interpolate
auto lower = std::prev(upper);
f32 lower_bound = lower->first;
video::SColor lower_color = lower->second;