From 5a8f75f11dc2fd910612a1a06c0a2225226047f7 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Tue, 16 Sep 2025 22:58:05 +0200 Subject: [PATCH] convert to vector --- AI/VCAI/ResourceManager.cpp | 2 +- lib/ResourceSet.cpp | 22 ++++++++++-------- lib/ResourceSet.h | 34 +++++++++++++--------------- lib/entities/ResourceTypeHandler.cpp | 4 ++-- lib/modding/ModManager.cpp | 2 +- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/AI/VCAI/ResourceManager.cpp b/AI/VCAI/ResourceManager.cpp index 2ff675308..9f6659354 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.second, 0); //never negative + vstd::amax(val, 0); //never negative return myRes; } diff --git a/lib/ResourceSet.cpp b/lib/ResourceSet.cpp index f47625df3..32871af24 100644 --- a/lib/ResourceSet.cpp +++ b/lib/ResourceSet.cpp @@ -18,10 +18,14 @@ VCMI_LIB_NAMESPACE_BEGIN -ResourceSet::ResourceSet() = default; +ResourceSet::ResourceSet() +{ + container.resize(LIBRARY->resourceTypeHandler->getAllObjects().size()); +}; ResourceSet::ResourceSet(const JsonNode & node) { + container.resize(LIBRARY->resourceTypeHandler->getAllObjects().size()); for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects()) container[i] = static_cast(node[i.toResource()->getJsonKey()].Float()); } @@ -35,13 +39,13 @@ void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects()) { handler.serializeInt(idx.toResource()->getJsonKey(), this->operator[](idx), 0); - } +} } bool ResourceSet::nonZero() const { for(const auto & elem : *this) - if(elem.second) + if(elem) return true; return false; @@ -50,28 +54,28 @@ bool ResourceSet::nonZero() const void ResourceSet::amax(const TResourceCap &val) { for(auto & elem : *this) - vstd::amax(elem.second, val); + vstd::amax(elem, val); } void ResourceSet::amin(const TResourceCap &val) { for(auto & elem : *this) - vstd::amin(elem.second, val); + vstd::amin(elem, val); } void ResourceSet::positive() { for(auto & elem : *this) - vstd::amax(elem.second, 0); + vstd::amax(elem, 0); } void ResourceSet::applyHandicap(int percentage) { for(auto & elem : *this) { - int64_t newAmount = vstd::divideAndCeil(static_cast(elem.second) * percentage, 100); + int64_t newAmount = vstd::divideAndCeil(static_cast(elem) * percentage, 100); int64_t cap = GameConstants::PLAYER_RESOURCES_CAP; - elem.second = std::min(cap, newAmount); + elem = std::min(cap, newAmount); } } @@ -108,7 +112,7 @@ std::string ResourceSet::toString() const out << "["; for(auto it = begin(); it != end(); ++it) { - out << (*it).second; + out << *it; if(std::prev(end()) != it) out << ", "; } out << "]"; diff --git a/lib/ResourceSet.h b/lib/ResourceSet.h index 534b8eefe..55e5a69ba 100644 --- a/lib/ResourceSet.h +++ b/lib/ResourceSet.h @@ -26,7 +26,7 @@ class ResourceSet; class ResourceSet { private: - std::map container = {}; + std::vector 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[GameResID(i)] OPSIGN ## = rhs; \ + container.at(i) OPSIGN ## = rhs; \ \ return *this; \ } @@ -45,8 +45,8 @@ public: #define vectorOperator(OPSIGN) \ ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \ { \ - for(auto i = 0; i < rhs.size(); i++) \ - container[GameResID(i)] OPSIGN ## = rhs[i]; \ + for(auto i = 0; i < container.size(); i++) \ + container.at(i) OPSIGN ## = rhs[i]; \ \ return *this; \ } @@ -94,23 +94,21 @@ public: TResource & operator[](size_t index) { - return container[GameResID(index)]; + return container.at(index); } const TResource & operator[](size_t index) const { - auto it = container.find(GameResID(index)); - if (it != container.end()) - return it->second; - - static const TResource default_resource{}; - return default_resource; + if(index >= container.size()) + logGlobal->error("Try to access resource which is not existing! Maybe new resources in mod not marked as modType=Resources?"); + + return container.at(index); } bool empty () const { for(const auto & res : *this) - if(res.second) + if(res) return false; return true; @@ -148,7 +146,7 @@ public: int ret = INT_MAX; for(int i = 0; i < container.size(); i++) if(rhs[i]) - vstd::amin(ret, container[GameResID(i)] / rhs[i]); + vstd::amin(ret, container.at(i) / rhs[i]); return ret; } @@ -158,14 +156,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[GameResID(i)] > 0) { // We only care about fulfilling positive needs + if (container.at(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[GameResID(i)], availableFunds[i]); + int ceiledResult = vstd::divideAndCeil(container.at(i), availableFunds[i]); ret = std::max(ret, ceiledResult); } } @@ -175,8 +173,8 @@ public: ResourceSet & operator=(const TResource &rhs) { - for(auto & i : container) - i.second = rhs; + for(int & i : container) + i = rhs; return *this; } @@ -185,7 +183,7 @@ public: { ResourceSet ret; for(int i = 0; i < container.size(); i++) - ret[i] = -container.at(GameResID(i)); + ret[i] = -container.at(i); return ret; } diff --git a/lib/entities/ResourceTypeHandler.cpp b/lib/entities/ResourceTypeHandler.cpp index 88350024b..a22654c89 100644 --- a/lib/entities/ResourceTypeHandler.cpp +++ b/lib/entities/ResourceTypeHandler.cpp @@ -20,7 +20,7 @@ VCMI_LIB_NAMESPACE_BEGIN std::string Resource::getNameTextID() const { - if(id.getNum() < 7) // OH3 resources + if(id.getNum() < GameConstants::RESOURCE_QUANTITY) // OH3 resources return TextIdentifier("core.restypes", id).get(); return TextIdentifier( "resources", modScope, identifier, "name" ).get(); } @@ -57,7 +57,7 @@ std::shared_ptr ResourceTypeHandler::loadFromJson(const std::string & ret->iconMedium = json["images"]["medium"].String(); ret->iconLarge = json["images"]["large"].String(); - if(ret->id.getNum() >= 7) // not OH3 resources + if(ret->id.getNum() >= GameConstants::RESOURCE_QUANTITY) // not OH3 resources LIBRARY->generaltexth->registerString(scope, ret->getNameTextID(), json["name"]); return ret; diff --git a/lib/modding/ModManager.cpp b/lib/modding/ModManager.cpp index 5918e7510..cf5339098 100644 --- a/lib/modding/ModManager.cpp +++ b/lib/modding/ModManager.cpp @@ -807,7 +807,7 @@ void ModDependenciesResolver::tryAddMods(TModList modsToResolve, const ModsStora { resolvedOnCurrentTreeLevel.insert(*it); // Not to the resolvedModIDs, so current node children will be resolved on the next iteration assert(!vstd::contains(sortedValidMods, *it)); - if(storage.getMod(*it).getValue("modType").String() == "Resources") // Resources needs to load before core to make it possible to override core elements with new resources + if(storage.getMod(*it).getValue("modType").String() == "Resources") // Resources needs to load before core to make it possible to override core elements with new resources and for correct init of ResourceSet sortedValidMods.insert(sortedValidMods.begin(), *it); else sortedValidMods.push_back(*it);