From 134c145b98d71b6852ed663472bb9278e000e351 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Mon, 15 Sep 2025 23:48:55 +0200 Subject: [PATCH] convert ResourceSet to std::map --- AI/VCAI/ResourceManager.cpp | 2 +- client/windows/GUIClasses.cpp | 1 - lib/CPlayerState.cpp | 2 +- lib/ResourceSet.cpp | 14 ++++++------ lib/ResourceSet.h | 24 ++++++++++----------- scripting/lua/api/netpacks/SetResources.cpp | 2 +- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/AI/VCAI/ResourceManager.cpp b/AI/VCAI/ResourceManager.cpp index 9f6659354..2ff675308 100644 --- a/AI/VCAI/ResourceManager.cpp +++ b/AI/VCAI/ResourceManager.cpp @@ -323,7 +323,7 @@ TResources ResourceManager::freeResources() const myRes -= reservedResources(); //subtract the value of reserved goals for (auto & val : myRes) - vstd::amax(val, 0); //never negative + vstd::amax(val.second, 0); //never negative return myRes; } diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index 26eca17e3..c048bd580 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -1215,7 +1215,6 @@ void CHillFortWindow::updateGarrisons() for(int i=0; i(type), 0); + return resources[type]; } template diff --git a/lib/ResourceSet.cpp b/lib/ResourceSet.cpp index 10f435538..11e565f1d 100644 --- a/lib/ResourceSet.cpp +++ b/lib/ResourceSet.cpp @@ -45,7 +45,7 @@ void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string bool ResourceSet::nonZero() const { for(const auto & elem : *this) - if(elem) + if(elem.second) return true; return false; @@ -54,28 +54,28 @@ bool ResourceSet::nonZero() const void ResourceSet::amax(const TResourceCap &val) { for(auto & elem : *this) - vstd::amax(elem, val); + vstd::amax(elem.second, val); } void ResourceSet::amin(const TResourceCap &val) { for(auto & elem : *this) - vstd::amin(elem, val); + vstd::amin(elem.second, val); } void ResourceSet::positive() { for(auto & elem : *this) - vstd::amax(elem, 0); + vstd::amax(elem.second, 0); } void ResourceSet::applyHandicap(int percentage) { for(auto & elem : *this) { - int64_t newAmount = vstd::divideAndCeil(static_cast(elem) * percentage, 100); + int64_t newAmount = vstd::divideAndCeil(static_cast(elem.second) * percentage, 100); int64_t cap = GameConstants::PLAYER_RESOURCES_CAP; - elem = std::min(cap, newAmount); + elem.second = std::min(cap, newAmount); } } @@ -112,7 +112,7 @@ std::string ResourceSet::toString() const out << "["; for(auto it = begin(); it != end(); ++it) { - out << *it; + out << (*it).second; if(std::prev(end()) != it) out << ", "; } out << "]"; diff --git a/lib/ResourceSet.h b/lib/ResourceSet.h index 9c33db5fc..abd4514af 100644 --- a/lib/ResourceSet.h +++ b/lib/ResourceSet.h @@ -26,7 +26,7 @@ class ResourceSet; class ResourceSet { private: - std::array container = {}; + std::map container = {}; public: // read resources set from json. Format example: { "gold": 500, "wood":5 } DLL_LINKAGE ResourceSet(const JsonNode & node); @@ -37,7 +37,7 @@ public: ResourceSet& operator OPSIGN ## =(const TResource &rhs) \ { \ for(auto i = 0; i < container.size(); i++) \ - container.at(i) OPSIGN ## = rhs; \ + container[GameResID(i)] OPSIGN ## = rhs; \ \ return *this; \ } @@ -46,7 +46,7 @@ public: ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \ { \ for(auto i = 0; i < container.size(); i++) \ - container.at(i) OPSIGN ## = rhs[i]; \ + container[GameResID(i)] OPSIGN ## = rhs[i]; \ \ return *this; \ } @@ -94,18 +94,18 @@ public: TResource & operator[](size_t index) { - return container.at(index); + return container[GameResID(index)]; } const TResource & operator[](size_t index) const { - return container.at(index); + return container.at(GameResID(index)); } bool empty () const { for(const auto & res : *this) - if(res) + if(res.second) return false; return true; @@ -143,7 +143,7 @@ public: int ret = INT_MAX; for(int i = 0; i < container.size(); i++) if(rhs[i]) - vstd::amin(ret, container.at(i) / rhs[i]); + vstd::amin(ret, container[GameResID(i)] / rhs[i]); return ret; } @@ -153,14 +153,14 @@ public: int ret = 0; // Initialize to 0 because we want the maximum number of accumulations for (size_t i = 0; i < container.size(); ++i) { - if (container.at(i) > 0) { // We only care about fulfilling positive needs + if (container[GameResID(i)] > 0) { // We only care about fulfilling positive needs if (availableFunds[i] == 0) { // If income is 0 and we need a positive amount, it's impossible to fulfill return INT_MAX; } else { // Calculate the number of times we need to accumulate income to fulfill the need - int ceiledResult = vstd::divideAndCeil(container.at(i), availableFunds[i]); + int ceiledResult = vstd::divideAndCeil(container[GameResID(i)], availableFunds[i]); ret = std::max(ret, ceiledResult); } } @@ -170,8 +170,8 @@ public: ResourceSet & operator=(const TResource &rhs) { - for(int & i : container) - i = rhs; + for(auto & i : container) + i.second = rhs; return *this; } @@ -180,7 +180,7 @@ public: { ResourceSet ret; for(int i = 0; i < container.size(); i++) - ret[i] = -container.at(i); + ret[i] = -container.at(GameResID(i)); return ret; } diff --git a/scripting/lua/api/netpacks/SetResources.cpp b/scripting/lua/api/netpacks/SetResources.cpp index 9cd8a88cd..5b103669b 100644 --- a/scripting/lua/api/netpacks/SetResources.cpp +++ b/scripting/lua/api/netpacks/SetResources.cpp @@ -82,7 +82,7 @@ int SetResourcesProxy::getAmount(lua_State * L) S.clear(); - const TQuantity amount = vstd::atOrDefault(object->res, static_cast(type), 0); + const TQuantity amount = object->res[type]; S.push(amount); return 1; }