Przeglądaj źródła

convert to vector

Laserlicht 1 miesiąc temu
rodzic
commit
5a8f75f11d

+ 1 - 1
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;
 }

+ 13 - 9
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<int>(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<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;
-		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 << "]";

+ 16 - 18
lib/ResourceSet.h

@@ -26,7 +26,7 @@ class ResourceSet;
 class ResourceSet
 {
 private:
-	std::map<GameResID, TResource> container = {};
+	std::vector<TResource> 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;
 	}
 

+ 2 - 2
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<Resource> 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;

+ 1 - 1
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);