1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00

convert to vector

This commit is contained in:
Laserlicht
2025-09-16 22:58:05 +02:00
parent ee5ff7eeb1
commit 5a8f75f11d
5 changed files with 33 additions and 31 deletions

View File

@@ -323,7 +323,7 @@ TResources ResourceManager::freeResources() const
myRes -= reservedResources(); //subtract the value of reserved goals myRes -= reservedResources(); //subtract the value of reserved goals
for (auto & val : myRes) for (auto & val : myRes)
vstd::amax(val.second, 0); //never negative vstd::amax(val, 0); //never negative
return myRes; return myRes;
} }

View File

@@ -18,10 +18,14 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
ResourceSet::ResourceSet() = default; ResourceSet::ResourceSet()
{
container.resize(LIBRARY->resourceTypeHandler->getAllObjects().size());
};
ResourceSet::ResourceSet(const JsonNode & node) ResourceSet::ResourceSet(const JsonNode & node)
{ {
container.resize(LIBRARY->resourceTypeHandler->getAllObjects().size());
for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects()) for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
container[i] = static_cast<int>(node[i.toResource()->getJsonKey()].Float()); container[i] = static_cast<int>(node[i.toResource()->getJsonKey()].Float());
} }
@@ -35,13 +39,13 @@ void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string
for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects()) for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
{ {
handler.serializeInt(idx.toResource()->getJsonKey(), this->operator[](idx), 0); handler.serializeInt(idx.toResource()->getJsonKey(), this->operator[](idx), 0);
} }
} }
bool ResourceSet::nonZero() const bool ResourceSet::nonZero() const
{ {
for(const auto & elem : *this) for(const auto & elem : *this)
if(elem.second) if(elem)
return true; return true;
return false; return false;
@@ -50,28 +54,28 @@ bool ResourceSet::nonZero() const
void ResourceSet::amax(const TResourceCap &val) void ResourceSet::amax(const TResourceCap &val)
{ {
for(auto & elem : *this) for(auto & elem : *this)
vstd::amax(elem.second, val); vstd::amax(elem, val);
} }
void ResourceSet::amin(const TResourceCap &val) void ResourceSet::amin(const TResourceCap &val)
{ {
for(auto & elem : *this) for(auto & elem : *this)
vstd::amin(elem.second, val); vstd::amin(elem, val);
} }
void ResourceSet::positive() void ResourceSet::positive()
{ {
for(auto & elem : *this) for(auto & elem : *this)
vstd::amax(elem.second, 0); vstd::amax(elem, 0);
} }
void ResourceSet::applyHandicap(int percentage) void ResourceSet::applyHandicap(int percentage)
{ {
for(auto & elem : *this) for(auto & elem : *this)
{ {
int64_t newAmount = vstd::divideAndCeil(static_cast<int64_t>(elem.second) * percentage, 100); int64_t newAmount = vstd::divideAndCeil(static_cast<int64_t>(elem) * percentage, 100);
int64_t cap = GameConstants::PLAYER_RESOURCES_CAP; 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 << "["; out << "[";
for(auto it = begin(); it != end(); ++it) for(auto it = begin(); it != end(); ++it)
{ {
out << (*it).second; out << *it;
if(std::prev(end()) != it) out << ", "; if(std::prev(end()) != it) out << ", ";
} }
out << "]"; out << "]";

View File

@@ -26,7 +26,7 @@ class ResourceSet;
class ResourceSet class ResourceSet
{ {
private: private:
std::map<GameResID, TResource> container = {}; std::vector<TResource> container = {};
public: public:
// read resources set from json. Format example: { "gold": 500, "wood":5 } // read resources set from json. Format example: { "gold": 500, "wood":5 }
DLL_LINKAGE ResourceSet(const JsonNode & node); DLL_LINKAGE ResourceSet(const JsonNode & node);
@@ -37,7 +37,7 @@ public:
ResourceSet& operator OPSIGN ## =(const TResource &rhs) \ ResourceSet& operator OPSIGN ## =(const TResource &rhs) \
{ \ { \
for(auto i = 0; i < container.size(); i++) \ for(auto i = 0; i < container.size(); i++) \
container[GameResID(i)] OPSIGN ## = rhs; \ container.at(i) OPSIGN ## = rhs; \
\ \
return *this; \ return *this; \
} }
@@ -45,8 +45,8 @@ public:
#define vectorOperator(OPSIGN) \ #define vectorOperator(OPSIGN) \
ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \ ResourceSet& operator OPSIGN ## =(const ResourceSet &rhs) \
{ \ { \
for(auto i = 0; i < rhs.size(); i++) \ for(auto i = 0; i < container.size(); i++) \
container[GameResID(i)] OPSIGN ## = rhs[i]; \ container.at(i) OPSIGN ## = rhs[i]; \
\ \
return *this; \ return *this; \
} }
@@ -94,23 +94,21 @@ public:
TResource & operator[](size_t index) TResource & operator[](size_t index)
{ {
return container[GameResID(index)]; return container.at(index);
} }
const TResource & operator[](size_t index) const const TResource & operator[](size_t index) const
{ {
auto it = container.find(GameResID(index)); if(index >= container.size())
if (it != container.end()) logGlobal->error("Try to access resource which is not existing! Maybe new resources in mod not marked as modType=Resources?");
return it->second;
return container.at(index);
static const TResource default_resource{};
return default_resource;
} }
bool empty () const bool empty () const
{ {
for(const auto & res : *this) for(const auto & res : *this)
if(res.second) if(res)
return false; return false;
return true; return true;
@@ -148,7 +146,7 @@ public:
int ret = INT_MAX; int ret = INT_MAX;
for(int i = 0; i < container.size(); i++) for(int i = 0; i < container.size(); i++)
if(rhs[i]) if(rhs[i])
vstd::amin(ret, container[GameResID(i)] / rhs[i]); vstd::amin(ret, container.at(i) / rhs[i]);
return ret; return ret;
} }
@@ -158,14 +156,14 @@ public:
int ret = 0; // Initialize to 0 because we want the maximum number of accumulations int ret = 0; // Initialize to 0 because we want the maximum number of accumulations
for (size_t i = 0; i < container.size(); ++i) { 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 (availableFunds[i] == 0) {
// If income is 0 and we need a positive amount, it's impossible to fulfill // If income is 0 and we need a positive amount, it's impossible to fulfill
return INT_MAX; return INT_MAX;
} }
else { else {
// Calculate the number of times we need to accumulate income to fulfill the need // 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); ret = std::max(ret, ceiledResult);
} }
} }
@@ -175,8 +173,8 @@ public:
ResourceSet & operator=(const TResource &rhs) ResourceSet & operator=(const TResource &rhs)
{ {
for(auto & i : container) for(int & i : container)
i.second = rhs; i = rhs;
return *this; return *this;
} }
@@ -185,7 +183,7 @@ public:
{ {
ResourceSet ret; ResourceSet ret;
for(int i = 0; i < container.size(); i++) for(int i = 0; i < container.size(); i++)
ret[i] = -container.at(GameResID(i)); ret[i] = -container.at(i);
return ret; return ret;
} }

View File

@@ -20,7 +20,7 @@ VCMI_LIB_NAMESPACE_BEGIN
std::string Resource::getNameTextID() const 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("core.restypes", id).get();
return TextIdentifier( "resources", modScope, identifier, "name" ).get(); return TextIdentifier( "resources", modScope, identifier, "name" ).get();
} }
@@ -57,7 +57,7 @@ std::shared_ptr<Resource> ResourceTypeHandler::loadFromJson(const std::string &
ret->iconMedium = json["images"]["medium"].String(); ret->iconMedium = json["images"]["medium"].String();
ret->iconLarge = json["images"]["large"].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"]); LIBRARY->generaltexth->registerString(scope, ret->getNameTextID(), json["name"]);
return ret; return ret;

View File

@@ -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 resolvedOnCurrentTreeLevel.insert(*it); // Not to the resolvedModIDs, so current node children will be resolved on the next iteration
assert(!vstd::contains(sortedValidMods, *it)); 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); sortedValidMods.insert(sortedValidMods.begin(), *it);
else else
sortedValidMods.push_back(*it); sortedValidMods.push_back(*it);