Browse Source

replace RESOURCE_QUANTITY where possible

Laserlicht 2 months ago
parent
commit
f034584dfa

+ 2 - 1
AI/Nullkiller/Engine/PriorityEvaluator.cpp

@@ -12,6 +12,7 @@
 
 #include "Nullkiller.h"
 #include "../../../lib/entities/artifact/CArtifact.h"
+#include "../../../lib/entities/ResourceTypeHandler.h"
 #include "../../../lib/mapObjectConstructors/AObjectTypeHandler.h"
 #include "../../../lib/mapObjectConstructors/CObjectClassesHandler.h"
 #include "../../../lib/mapObjects/CGResource.h"
@@ -1590,7 +1591,7 @@ float PriorityEvaluator::evaluate(Goals::TSubgoal task, int priorityTier)
 						needed.positive();
 						int turnsTo = needed.maxPurchasableCount(income);
 						bool haveEverythingButGold = true;
-						for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
+						for (auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 						{
 							if (i != GameResID::GOLD && resourcesAvailable[i] < evaluationContext.buildingCost[i])
 								haveEverythingButGold = false;

+ 1 - 1
client/adventureMap/AdventureMapWidget.cpp

@@ -286,7 +286,7 @@ std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceDateBar(const JsonN
 
 	auto result = std::make_shared<CResDataBar>(image, area.topLeft());
 
-	for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
+	for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++) //TODO: configurable resource support
 	{
 		const auto & node = input[LIBRARY->resourceTypeHandler->getById(i)->getJsonKey()];
 

+ 1 - 1
client/windows/CCastleInterface.cpp

@@ -293,7 +293,7 @@ CDwellingInfoBox::CDwellingInfoBox(int centerX, int centerY, const CGTownInstanc
 	available = std::make_shared<CLabel>(80,190, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[217] + text);
 	costPerTroop = std::make_shared<CLabel>(80, 227, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[346]);
 
-	for(int i = 0; i<GameConstants::RESOURCE_QUANTITY; i++)
+	for(int i = 0; i<GameConstants::RESOURCE_QUANTITY; i++) //TODO: configurable resource support
 	{
 		auto res = static_cast<EGameResID>(i);
 		if(creature->getRecruitCost(res))

+ 10 - 6
lib/ResourceSet.cpp

@@ -22,7 +22,7 @@ ResourceSet::ResourceSet() = default;
 
 ResourceSet::ResourceSet(const JsonNode & node)
 {
-	for(auto i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 		container[i] = static_cast<int>(node[LIBRARY->resourceTypeHandler->getById(i)->getJsonKey()].Float());
 }
 
@@ -32,9 +32,14 @@ void ResourceSet::serializeJson(JsonSerializeFormat & handler, const std::string
 		return;
 	auto s = handler.enterStruct(fieldName);
 
-	//TODO: add proper support for mithril to map format
-	for(int idx = 0; idx < GameConstants::RESOURCE_QUANTITY - 1; idx ++)
+	for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
+	{
+		//TODO: add proper support for mithril to map format
+		if(idx == EGameResID::MITHRIL)
+			continue;
+
 		handler.serializeInt(LIBRARY->resourceTypeHandler->getById(idx)->getJsonKey(), this->operator[](idx), 0);
+	}
 }
 
 bool ResourceSet::nonZero() const
@@ -76,8 +81,7 @@ void ResourceSet::applyHandicap(int percentage)
 
 static bool canAfford(const ResourceSet &res, const ResourceSet &price)
 {
-	assert(res.size() == price.size() && price.size() == GameConstants::RESOURCE_QUANTITY);
-	for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 		if(price[i] > res[i])
 			return false;
 
@@ -97,7 +101,7 @@ bool ResourceSet::canAfford(const ResourceSet &price) const
 TResourceCap ResourceSet::marketValue() const
 {
 	TResourceCap total = 0;
-	for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; i++)
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 		total += static_cast<TResourceCap>(LIBRARY->resourceTypeHandler->getById(i)->getPrice()) * static_cast<TResourceCap>(operator[](i));
 	return total;
 }

+ 3 - 2
lib/gameState/GameStatistics.cpp

@@ -106,8 +106,9 @@ void StatisticDataSetEntry::serializeJson(JsonSerializeFormat & handler)
 	handler.serializeBool("hasGrail", hasGrail);
 	{
 		auto zonesData = handler.enterStruct("numMines");
-		for(TResource idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
-			handler.serializeInt(LIBRARY->resourceTypeHandler->getById(idx)->getJsonKey(), numMines[idx], 0);
+		for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
+			if(idx != GameResID::MITHRIL)
+				handler.serializeInt(LIBRARY->resourceTypeHandler->getById(idx)->getJsonKey(), numMines[idx], 0);
 	}
 	handler.serializeInt("score", score);
 	handler.serializeInt("maxHeroLevel", maxHeroLevel);

+ 1 - 1
lib/json/JsonRandom.cpp

@@ -299,7 +299,7 @@ JsonRandom::JsonRandom(IGameInfoCallback * cb, IGameRandomizer & gameRandomizer)
 			return ret;
 		}
 
-		for (size_t i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
+		for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 		{
 			ret[i] = loadValue(value[LIBRARY->resourceTypeHandler->getById(i)->getJsonKey()], variables);
 		}

+ 4 - 5
lib/mapObjects/CQuest.cpp

@@ -373,11 +373,10 @@ void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fi
 		if(missionType == "Resources")
 		{
 			auto r = handler.enterStruct("resources");
-
-			for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
-			{
-				handler.serializeInt(LIBRARY->resourceTypeHandler->getById(idx)->getJsonKey(), mission.resources[idx], 0);
-			}
+			
+			for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
+				if(idx != GameResID::MITHRIL)
+					handler.serializeInt(LIBRARY->resourceTypeHandler->getById(idx)->getJsonKey(), mission.resources[idx], 0);
 		}
 		
 		if(missionType == "Hero")

+ 3 - 4
lib/rmg/CRmgTemplate.cpp

@@ -535,10 +535,9 @@ void ZoneOptions::serializeJson(JsonSerializeFormat & handler)
 	{
 		auto minesData = handler.enterStruct("mines");
 
-		for(TResource idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
-		{
-			handler.serializeInt(LIBRARY->resourceTypeHandler->getById(idx)->getJsonKey(), mines[idx], 0);
-		}
+		for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
+			if(idx != GameResID::MITHRIL)
+				handler.serializeInt(LIBRARY->resourceTypeHandler->getById(idx)->getJsonKey(), mines[idx], 0);
 	}
 
 	handler.serializeStruct("customObjects", objectConfig);

+ 7 - 3
mapeditor/inspector/questwidget.cpp

@@ -17,6 +17,7 @@
 #include "../lib/CCreatureHandler.h"
 #include "../lib/constants/StringConstants.h"
 #include "../lib/entities/artifact/CArtHandler.h"
+#include "../lib/entities/ResourceTypeHandler.h"
 #include "../lib/mapping/CMap.h"
 #include "../lib/mapObjects/CGHeroInstance.h"
 #include "../lib/mapObjects/CGCreature.h"
@@ -40,13 +41,16 @@ QuestWidget::QuestWidget(MapController & _controller, CQuest & _sh, QWidget *par
 		ui->lDayOfWeek->addItem(tr("Day %1").arg(i));
 	
 	//fill resources
-	ui->lResources->setRowCount(GameConstants::RESOURCE_QUANTITY - 1);
-	for(int i = 0; i < GameConstants::RESOURCE_QUANTITY - 1; ++i)
+	ui->lResources->setRowCount(LIBRARY->resourceTypeHandler->getAllObjects().size() - 1);
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 	{
+		if(i == EGameResID::MITHRIL)
+			continue;
+
 		MetaString str;
 		str.appendName(GameResID(i));
 		auto * item = new QTableWidgetItem(QString::fromStdString(str.toString()));
-		item->setData(Qt::UserRole, QVariant::fromValue(i));
+		item->setData(Qt::UserRole, QVariant::fromValue(i.getNum()));
 		ui->lResources->setItem(i, 0, item);
 		auto * spinBox = new QSpinBox;
 		spinBox->setMaximum(i == GameResID::GOLD ? 999999 : 999);

+ 8 - 4
mapeditor/inspector/rewardswidget.cpp

@@ -17,6 +17,7 @@
 #include "../lib/CCreatureHandler.h"
 #include "../lib/constants/StringConstants.h"
 #include "../lib/entities/artifact/CArtifact.h"
+#include "../lib/entities/ResourceTypeHandler.h"
 #include "../lib/mapping/CMap.h"
 #include "../lib/modding/IdentifierStorage.h"
 #include "../lib/modding/ModScope.h"
@@ -55,16 +56,19 @@ RewardsWidget::RewardsWidget(CMap & m, CRewardableObject & p, QWidget *parent) :
 		ui->lDayOfWeek->addItem(tr("Day %1").arg(i));
 	
 	//fill resources
-	ui->rResources->setRowCount(GameConstants::RESOURCE_QUANTITY - 1);
-	ui->lResources->setRowCount(GameConstants::RESOURCE_QUANTITY - 1);
-	for(int i = 0; i < GameConstants::RESOURCE_QUANTITY - 1; ++i)
+	ui->rResources->setRowCount(LIBRARY->resourceTypeHandler->getAllObjects().size() - 1);
+	ui->lResources->setRowCount(LIBRARY->resourceTypeHandler->getAllObjects().size() - 1);
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 	{
+		if(i == EGameResID::MITHRIL)
+			continue;
+
 		MetaString str;
 		str.appendName(GameResID(i));
 		for(auto * w : {ui->rResources, ui->lResources})
 		{
 			auto * item = new QTableWidgetItem(QString::fromStdString(str.toString()));
-			item->setData(Qt::UserRole, QVariant::fromValue(i));
+			item->setData(Qt::UserRole, QVariant::fromValue(i.getNum()));
 			w->setItem(i, 0, item);
 			auto * spinBox = new QSpinBox;
 			spinBox->setMaximum(i == GameResID::GOLD ? 999999 : 999);

+ 3 - 3
mapeditor/inspector/towneventdialog.cpp

@@ -81,9 +81,9 @@ void TownEventDialog::initPlayers()
 
 void TownEventDialog::initResources()
 {
-	ui->resourcesTable->setRowCount(GameConstants::RESOURCE_QUANTITY);
+	ui->resourcesTable->setRowCount(LIBRARY->resourceTypeHandler->getAllObjects().size());
 	auto resourcesMap = params.value("resources").toMap();
-	for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 	{
 		MetaString str;
 		str.appendName(GameResID(i));
@@ -230,7 +230,7 @@ QVariant TownEventDialog::playersToVariant()
 QVariantMap TownEventDialog::resourcesToVariant()
 {
 	auto res = params.value("resources").toMap();
-	for (int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 	{
 		auto itemType = QString::fromStdString(LIBRARY->resourceTypeHandler->getById(i)->getJsonKey());
 		auto * itemQty = static_cast<QSpinBox *> (ui->resourcesTable->cellWidget(i, 1));

+ 1 - 1
mapeditor/mapsettings/eventsettings.cpp

@@ -43,7 +43,7 @@ std::set<PlayerColor> playersFromVariant(const QVariant & v)
 QVariant toVariant(const TResources & resources)
 {
 	QVariantMap result;
-	for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 		result[QString::fromStdString(LIBRARY->resourceTypeHandler->getById(i)->getJsonKey())] = QVariant::fromValue(resources[i]);
 	return result;
 }

+ 3 - 3
mapeditor/mapsettings/timedevent.cpp

@@ -47,8 +47,8 @@ TimedEvent::TimedEvent(MapController & c, QListWidgetItem * t, QWidget *parent)
 		ui->playersAffected->addItem(item);
 	}
 
-	ui->resources->setRowCount(GameConstants::RESOURCE_QUANTITY);
-	for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
+	ui->resources->setRowCount(LIBRARY->resourceTypeHandler->getAllObjects().size());
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 	{
 		MetaString str;
 		str.appendName(GameResID(i));
@@ -96,7 +96,7 @@ void TimedEvent::on_TimedEvent_finished(int result)
 	descriptor["players"] = QVariant::fromValue(players);
 
 	auto res = target->data(Qt::UserRole).toMap().value("resources").toMap();
-	for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
+	for(auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
 	{
 		auto itemType = QString::fromStdString(LIBRARY->resourceTypeHandler->getById(i)->getJsonKey());
 		auto * itemQty = ui->resources->item(i, 1);

+ 4 - 2
mapeditor/mapsettings/victoryconditions.cpp

@@ -12,9 +12,11 @@
 #include "ui_victoryconditions.h"
 #include "../mapcontroller.h"
 
+#include "../../lib/GameLibrary.h"
 #include "../../lib/constants/StringConstants.h"
 #include "../../lib/entities/artifact/CArtHandler.h"
 #include "../../lib/entities/faction/CTownHandler.h"
+#include "../../lib/entities/ResourceTypeHandler.h"
 #include "../../lib/mapObjects/CGCreature.h"
 #include "../../lib/texts/CGeneralTextHandler.h"
 
@@ -406,12 +408,12 @@ void VictoryConditions::on_victoryComboBox_currentIndexChanged(int index)
 			victoryTypeWidget = new QComboBox;
 			ui->victoryParamsLayout->addWidget(victoryTypeWidget);
 			{
-				for(int resType = 0; resType < GameConstants::RESOURCE_QUANTITY; ++resType)
+				for(auto & resType : LIBRARY->resourceTypeHandler->getAllObjects())
 				{
 					MetaString str;
 					str.appendName(GameResID(resType));
 					auto resName = QString::fromStdString(str.toString());
-					victoryTypeWidget->addItem(resName, QVariant::fromValue(resType));
+					victoryTypeWidget->addItem(resName, QVariant::fromValue(resType.getNum()));
 				}
 			}