validator.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * validator.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "validator.h"
  12. #include "mapcontroller.h"
  13. #include "ui_validator.h"
  14. #include "../lib/entities/hero/CHero.h"
  15. #include "../lib/mapping/CMap.h"
  16. #include "../lib/mapObjects/MapObjects.h"
  17. #include "../lib/modding/CModHandler.h"
  18. #include "../lib/modding/ModDescription.h"
  19. #include "../lib/spells/CSpellHandler.h"
  20. Validator::Validator(const CMap * map, QWidget *parent) :
  21. QDialog(parent),
  22. ui(new Ui::Validator)
  23. {
  24. ui->setupUi(this);
  25. setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
  26. show();
  27. setAttribute(Qt::WA_DeleteOnClose);
  28. std::array<QString, 2> icons{":/icons/mod-update.png", ":/icons/mod-delete.png"};
  29. for(auto & issue : Validator::validate(map))
  30. {
  31. auto * item = new QListWidgetItem(QIcon(icons[issue.critical ? 1 : 0]), issue.message);
  32. ui->listWidget->addItem(item);
  33. }
  34. }
  35. Validator::~Validator()
  36. {
  37. delete ui;
  38. }
  39. std::set<Validator::Issue> Validator::validate(const CMap * map)
  40. {
  41. std::set<Validator::Issue> issues;
  42. if(!map)
  43. {
  44. issues.insert({ tr("Map is not loaded"), true });
  45. return issues;
  46. }
  47. try
  48. {
  49. //check player settings
  50. int hplayers = 0;
  51. int cplayers = 0;
  52. std::map<PlayerColor, int> amountOfTowns;
  53. std::map<PlayerColor, int> amountOfHeroes;
  54. for(int i = 0; i < map->players.size(); ++i)
  55. {
  56. auto & p = map->players[i];
  57. if (p.canAnyonePlay())
  58. amountOfTowns[PlayerColor(i)] = 0;
  59. if(p.canComputerPlay)
  60. ++cplayers;
  61. if(p.canHumanPlay)
  62. ++hplayers;
  63. if(p.allowedFactions.empty())
  64. issues.insert({ tr("No factions allowed for player %1").arg(i), true });
  65. }
  66. if(hplayers + cplayers == 0)
  67. issues.insert({ tr("No players allowed to play this map"), true });
  68. if(hplayers + cplayers == 1)
  69. issues.insert({ tr("Map is allowed for one player and cannot be started"), true });
  70. if(!hplayers)
  71. issues.insert({ tr("No human players allowed to play this map"), true });
  72. std::set<const CHero * > allHeroesOnMap; //used to find hero duplicated
  73. //checking all objects in the map
  74. for(auto o : map->objects)
  75. {
  76. //owners for objects
  77. if(o->getOwner() == PlayerColor::UNFLAGGABLE)
  78. {
  79. if(o->asOwnable())
  80. {
  81. issues.insert({ tr("Ownable object %1 is UNFLAGGABLE but must have NEUTRAL or player owner").arg(o->instanceName.c_str()), true });
  82. }
  83. }
  84. if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
  85. {
  86. if(!map->players[o->getOwner().getNum()].canAnyonePlay())
  87. issues.insert({ tr("Object %1 is assigned to non-playable player %2").arg(o->instanceName.c_str(), o->getOwner().toString().c_str()), true });
  88. }
  89. //count towns
  90. if(auto * ins = dynamic_cast<CGTownInstance *>(o.get()))
  91. {
  92. ++amountOfTowns[ins->getOwner()];
  93. }
  94. //checking and counting heroes and prisons
  95. if(auto * ins = dynamic_cast<CGHeroInstance *>(o.get()))
  96. {
  97. if(ins->ID == Obj::PRISON)
  98. {
  99. if(ins->getOwner() != PlayerColor::NEUTRAL)
  100. issues.insert({ tr("Prison %1 must be a NEUTRAL").arg(ins->instanceName.c_str()), true });
  101. }
  102. else
  103. {
  104. if(ins->getOwner() == PlayerColor::NEUTRAL)
  105. issues.insert({ tr("Hero %1 must have an owner").arg(ins->instanceName.c_str()), true });
  106. ++amountOfHeroes[ins->getOwner()];
  107. }
  108. if(ins->getHeroTypeID().hasValue())
  109. {
  110. if(map->allowedHeroes.count(ins->getHeroTypeID()) == 0)
  111. issues.insert({ tr("Hero %1 is prohibited by map settings").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  112. if(!allHeroesOnMap.insert(ins->getHeroType()).second)
  113. issues.insert({ tr("Hero %1 has duplicate on map").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  114. }
  115. else if(ins->ID != Obj::RANDOM_HERO)
  116. issues.insert({ tr("Hero %1 has an empty type and must be removed").arg(ins->instanceName.c_str()), true });
  117. }
  118. //checking for arts
  119. if(auto * ins = dynamic_cast<CGArtifact *>(o.get()))
  120. {
  121. if(ins->ID == Obj::SPELL_SCROLL)
  122. {
  123. if (ins->getArtifactInstance())
  124. {
  125. if (map->allowedSpells.count(ins->getArtifactInstance()->getScrollSpellID()) == 0)
  126. issues.insert({ tr("Spell scroll %1 is prohibited by map settings").arg(ins->getArtifactInstance()->getScrollSpellID().toEntity(LIBRARY->spells())->getNameTranslated().c_str()), false });
  127. }
  128. else
  129. issues.insert({ tr("Spell scroll %1 doesn't have instance assigned and must be removed").arg(ins->instanceName.c_str()), true });
  130. }
  131. else
  132. {
  133. if(ins->ID == Obj::ARTIFACT && map->allowedArtifact.count(ins->getArtifactType()) == 0)
  134. {
  135. issues.insert({ tr("Artifact %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false });
  136. }
  137. }
  138. }
  139. }
  140. //verification of starting towns
  141. for (const auto & [player, counter] : amountOfTowns)
  142. {
  143. if (counter == 0)
  144. {
  145. // FIXME: heroesNames are empty even though heroes are on the map
  146. // if(map->players[playerTCounter.first].heroesNames.empty())
  147. if(amountOfHeroes.count(player) == 0)
  148. issues.insert({ tr("Player %1 has no towns and heroes assigned").arg(player + 1), true });
  149. else
  150. issues.insert({ tr("Player %1 doesn't have any starting town").arg(player + 1), false });
  151. }
  152. }
  153. //verification of map name and description
  154. if(map->name.empty())
  155. issues.insert({ tr("Map name is not specified"), false });
  156. if(map->description.empty())
  157. issues.insert({ tr("Map description is not specified"), false });
  158. //verificationfor mods
  159. for(auto & mod : MapController::modAssessmentMap(*map))
  160. {
  161. if(!map->mods.count(mod.first))
  162. {
  163. issues.insert({ tr("Map contains object from mod \"%1\", but doesn't require it").arg(QString::fromStdString(LIBRARY->modh->getModInfo(mod.first).getVerificationInfo().name)), true });
  164. }
  165. }
  166. }
  167. catch(const std::exception & e)
  168. {
  169. issues.insert({ tr("Exception occurs during validation: %1").arg(e.what()), true });
  170. }
  171. catch(...)
  172. {
  173. issues.insert({ tr("Unknown exception occurs during validation"), true });
  174. }
  175. return issues;
  176. }