validator.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(dynamic_cast<CGMine *>(o.get()) ||
  80. dynamic_cast<CGDwelling *>(o.get()) ||
  81. dynamic_cast<CGTownInstance *>(o.get()) ||
  82. dynamic_cast<CGGarrison *>(o.get()) ||
  83. dynamic_cast<CGHeroInstance *>(o.get()))
  84. {
  85. issues.insert({ tr("Armored instance %1 is UNFLAGGABLE but must have NEUTRAL or player owner").arg(o->instanceName.c_str()), true });
  86. }
  87. }
  88. if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
  89. {
  90. if(!map->players[o->getOwner().getNum()].canAnyonePlay())
  91. issues.insert({ tr("Object %1 is assigned to non-playable player %2").arg(o->instanceName.c_str(), o->getOwner().toString().c_str()), true });
  92. }
  93. //count towns
  94. if(auto * ins = dynamic_cast<CGTownInstance *>(o.get()))
  95. {
  96. ++amountOfTowns[ins->getOwner()];
  97. }
  98. //checking and counting heroes and prisons
  99. if(auto * ins = dynamic_cast<CGHeroInstance *>(o.get()))
  100. {
  101. if(ins->ID == Obj::PRISON)
  102. {
  103. if(ins->getOwner() != PlayerColor::NEUTRAL)
  104. issues.insert({ tr("Prison %1 must be a NEUTRAL").arg(ins->instanceName.c_str()), true });
  105. }
  106. else
  107. {
  108. if(ins->getOwner() == PlayerColor::NEUTRAL)
  109. issues.insert({ tr("Hero %1 must have an owner").arg(ins->instanceName.c_str()), true });
  110. ++amountOfHeroes[ins->getOwner()];
  111. }
  112. if(ins->getHeroTypeID().hasValue())
  113. {
  114. if(map->allowedHeroes.count(ins->getHeroTypeID()) == 0)
  115. issues.insert({ tr("Hero %1 is prohibited by map settings").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  116. if(!allHeroesOnMap.insert(ins->getHeroType()).second)
  117. issues.insert({ tr("Hero %1 has duplicate on map").arg(ins->getHeroType()->getNameTranslated().c_str()), false });
  118. }
  119. else if(ins->ID != Obj::RANDOM_HERO)
  120. issues.insert({ tr("Hero %1 has an empty type and must be removed").arg(ins->instanceName.c_str()), true });
  121. }
  122. //checking for arts
  123. if(auto * ins = dynamic_cast<CGArtifact *>(o.get()))
  124. {
  125. if(ins->ID == Obj::SPELL_SCROLL)
  126. {
  127. if (ins->storedArtifact)
  128. {
  129. if (map->allowedSpells.count(ins->storedArtifact->getScrollSpellID()) == 0)
  130. issues.insert({ tr("Spell scroll %1 is prohibited by map settings").arg(ins->storedArtifact->getScrollSpellID().toEntity(VLC->spells())->getNameTranslated().c_str()), false });
  131. }
  132. else
  133. issues.insert({ tr("Spell scroll %1 doesn't have instance assigned and must be removed").arg(ins->instanceName.c_str()), true });
  134. }
  135. else
  136. {
  137. if(ins->ID == Obj::ARTIFACT && map->allowedArtifact.count(ins->getArtifact()) == 0)
  138. {
  139. issues.insert({ tr("Artifact %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false });
  140. }
  141. }
  142. }
  143. }
  144. //verification of starting towns
  145. for (const auto & [player, counter] : amountOfTowns)
  146. {
  147. if (counter == 0)
  148. {
  149. // FIXME: heroesNames are empty even though heroes are on the map
  150. // if(map->players[playerTCounter.first].heroesNames.empty())
  151. if(amountOfHeroes.count(player) == 0)
  152. issues.insert({ tr("Player %1 has no towns and heroes assigned").arg(player + 1), true });
  153. else
  154. issues.insert({ tr("Player %1 doesn't have any starting town").arg(player + 1), false });
  155. }
  156. }
  157. //verification of map name and description
  158. if(map->name.empty())
  159. issues.insert({ tr("Map name is not specified"), false });
  160. if(map->description.empty())
  161. issues.insert({ tr("Map description is not specified"), false });
  162. //verificationfor mods
  163. for(auto & mod : MapController::modAssessmentMap(*map))
  164. {
  165. if(!map->mods.count(mod.first))
  166. {
  167. issues.insert({ tr("Map contains object from mod \"%1\", but doesn't require it").arg(QString::fromStdString(VLC->modh->getModInfo(mod.first).getVerificationInfo().name)), true });
  168. }
  169. }
  170. }
  171. catch(const std::exception & e)
  172. {
  173. issues.insert({ tr("Exception occurs during validation: %1").arg(e.what()), true });
  174. }
  175. catch(...)
  176. {
  177. issues.insert({ tr("Unknown exception occurs during validation"), true });
  178. }
  179. return issues;
  180. }