validator.cpp 6.1 KB

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