Bläddra i källkod

convert ResourceSet to std::map

Laserlicht 1 månad sedan
förälder
incheckning
134c145b98

+ 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, 0); //never negative
+		vstd::amax(val.second, 0); //never negative
 
 	return myRes;
 }

+ 0 - 1
client/windows/GUIClasses.cpp

@@ -1215,7 +1215,6 @@ void CHillFortWindow::updateGarrisons()
 
 	for(int i=0; i<slotsCount; i++)
 	{
-		std::fill(costs[i].begin(), costs[i].end(), 0);
 		State newState = getState(SlotID(i));
 		if(newState != State::EMPTY)
 		{

+ 1 - 1
lib/CPlayerState.cpp

@@ -98,7 +98,7 @@ const IBonusBearer * PlayerState::getBonusBearer() const
 
 int PlayerState::getResourceAmount(int type) const
 {
-	return vstd::atOrDefault(resources, static_cast<size_t>(type), 0);
+	return resources[type];
 }
 
 template<typename T>

+ 7 - 7
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<int64_t>(elem) * percentage, 100);
+		int64_t newAmount = vstd::divideAndCeil(static_cast<int64_t>(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 << "]";

+ 12 - 12
lib/ResourceSet.h

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

+ 1 - 1
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<size_t>(type), 0);
+	const TQuantity amount = object->res[type];
 	S.push(amount);
 	return 1;
 }