diff --git a/AI/Nullkiller/Engine/PriorityEvaluator.cpp b/AI/Nullkiller/Engine/PriorityEvaluator.cpp index 0befb2a8b..4335570bf 100644 --- a/AI/Nullkiller/Engine/PriorityEvaluator.cpp +++ b/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; diff --git a/client/adventureMap/AdventureMapWidget.cpp b/client/adventureMap/AdventureMapWidget.cpp index b3382451e..b989bd4a4 100644 --- a/client/adventureMap/AdventureMapWidget.cpp +++ b/client/adventureMap/AdventureMapWidget.cpp @@ -286,7 +286,7 @@ std::shared_ptr AdventureMapWidget::buildResourceDateBar(const JsonN auto result = std::make_shared(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()]; diff --git a/client/windows/CCastleInterface.cpp b/client/windows/CCastleInterface.cpp index 78795e459..6cc31ff79 100644 --- a/client/windows/CCastleInterface.cpp +++ b/client/windows/CCastleInterface.cpp @@ -293,7 +293,7 @@ CDwellingInfoBox::CDwellingInfoBox(int centerX, int centerY, const CGTownInstanc available = std::make_shared(80,190, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[217] + text); costPerTroop = std::make_shared(80, 227, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, LIBRARY->generaltexth->allTexts[346]); - for(int i = 0; i(i); if(creature->getRecruitCost(res)) diff --git a/lib/ResourceSet.cpp b/lib/ResourceSet.cpp index b7d512c84..c47e0cfe1 100644 --- a/lib/ResourceSet.cpp +++ b/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(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(LIBRARY->resourceTypeHandler->getById(i)->getPrice()) * static_cast(operator[](i)); return total; } diff --git a/lib/gameState/GameStatistics.cpp b/lib/gameState/GameStatistics.cpp index 84f4ac262..42aeb0dae 100644 --- a/lib/gameState/GameStatistics.cpp +++ b/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); diff --git a/lib/json/JsonRandom.cpp b/lib/json/JsonRandom.cpp index 6262d2895..9f44a4f81 100644 --- a/lib/json/JsonRandom.cpp +++ b/lib/json/JsonRandom.cpp @@ -299,7 +299,7 @@ JsonRandom::JsonRandom(IGameInfoCallback * cb, IGameRandomizer & gameRandomizer) return ret; } - for (size_t i=0; iresourceTypeHandler->getAllObjects()) { ret[i] = loadValue(value[LIBRARY->resourceTypeHandler->getById(i)->getJsonKey()], variables); } diff --git a/lib/mapObjects/CQuest.cpp b/lib/mapObjects/CQuest.cpp index 37ab26d6e..45e6cccaa 100644 --- a/lib/mapObjects/CQuest.cpp +++ b/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") diff --git a/lib/rmg/CRmgTemplate.cpp b/lib/rmg/CRmgTemplate.cpp index 5ffbdb824..1dee90b47 100644 --- a/lib/rmg/CRmgTemplate.cpp +++ b/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); diff --git a/mapeditor/inspector/questwidget.cpp b/mapeditor/inspector/questwidget.cpp index 697306f2f..c77918312 100644 --- a/mapeditor/inspector/questwidget.cpp +++ b/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); diff --git a/mapeditor/inspector/rewardswidget.cpp b/mapeditor/inspector/rewardswidget.cpp index 103aa32da..ca9fd2098 100644 --- a/mapeditor/inspector/rewardswidget.cpp +++ b/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); diff --git a/mapeditor/inspector/towneventdialog.cpp b/mapeditor/inspector/towneventdialog.cpp index 0796eaab3..56fa31de6 100644 --- a/mapeditor/inspector/towneventdialog.cpp +++ b/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 (ui->resourcesTable->cellWidget(i, 1)); diff --git a/mapeditor/mapsettings/eventsettings.cpp b/mapeditor/mapsettings/eventsettings.cpp index e38616ab5..858e69438 100644 --- a/mapeditor/mapsettings/eventsettings.cpp +++ b/mapeditor/mapsettings/eventsettings.cpp @@ -43,7 +43,7 @@ std::set 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; } diff --git a/mapeditor/mapsettings/timedevent.cpp b/mapeditor/mapsettings/timedevent.cpp index 5f2eda41a..cdc288a82 100644 --- a/mapeditor/mapsettings/timedevent.cpp +++ b/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); diff --git a/mapeditor/mapsettings/victoryconditions.cpp b/mapeditor/mapsettings/victoryconditions.cpp index ea337b73b..1034dec1b 100644 --- a/mapeditor/mapsettings/victoryconditions.cpp +++ b/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())); } }