validator.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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{"mapeditor/icons/mod-update.png", "mapeditor/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::list<Validator::Issue> Validator::validate(const CMap * map)
  39. {
  40. std::list<Validator::Issue> issues;
  41. if(!map)
  42. {
  43. issues.emplace_back(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<int, int> amountOfCastles;
  52. for(int i = 0; i < map->players.size(); ++i)
  53. {
  54. auto & p = map->players[i];
  55. if(p.canAnyonePlay())
  56. amountOfCastles[i] = 0;
  57. if(p.canComputerPlay)
  58. ++cplayers;
  59. if(p.canHumanPlay)
  60. ++hplayers;
  61. if(p.allowedFactions.empty())
  62. issues.emplace_back(QString(tr("No factions allowed for player %1")).arg(i), true);
  63. }
  64. if(hplayers + cplayers == 0)
  65. issues.emplace_back(tr("No players allowed to play this map"), true);
  66. if(hplayers + cplayers == 1)
  67. issues.emplace_back(tr("Map is allowed for one player and cannot be started"), true);
  68. if(!hplayers)
  69. issues.emplace_back(tr("No human players allowed to play this map"), true);
  70. std::set<CHero*> allHeroesOnMap; //used to find hero duplicated
  71. //checking all objects in the map
  72. for(auto o : map->objects)
  73. {
  74. //owners for objects
  75. if(o->getOwner() == PlayerColor::UNFLAGGABLE)
  76. {
  77. if(dynamic_cast<CGMine*>(o.get()) ||
  78. dynamic_cast<CGDwelling*>(o.get()) ||
  79. dynamic_cast<CGTownInstance*>(o.get()) ||
  80. dynamic_cast<CGGarrison*>(o.get()) ||
  81. dynamic_cast<CGHeroInstance*>(o.get()))
  82. {
  83. issues.emplace_back(QString(tr("Armored instance %1 is UNFLAGGABLE but must have NEUTRAL or player owner")).arg(o->instanceName.c_str()), true);
  84. }
  85. }
  86. if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
  87. {
  88. if(!map->players[o->getOwner().getNum()].canAnyonePlay())
  89. issues.emplace_back(QString(tr("Object %1 is assigned to non-playable player %2")).arg(o->instanceName.c_str(), o->getOwner().toString().c_str()), true);
  90. }
  91. //checking towns
  92. if(auto * ins = dynamic_cast<CGTownInstance*>(o.get()))
  93. {
  94. bool has = amountOfCastles.count(ins->getOwner().getNum());
  95. if(!has && ins->getOwner() != PlayerColor::NEUTRAL)
  96. issues.emplace_back(tr("Town %1 has undefined owner %2").arg(ins->instanceName.c_str(), ins->getOwner().toString().c_str()), true);
  97. if(has)
  98. ++amountOfCastles[ins->getOwner().getNum()];
  99. }
  100. //checking heroes and prisons
  101. if(auto * ins = dynamic_cast<CGHeroInstance*>(o.get()))
  102. {
  103. if(ins->ID == Obj::PRISON)
  104. {
  105. if(ins->getOwner() != PlayerColor::NEUTRAL)
  106. issues.emplace_back(QString(tr("Prison %1 must be a NEUTRAL")).arg(ins->instanceName.c_str()), true);
  107. }
  108. else
  109. {
  110. bool has = amountOfCastles.count(ins->getOwner().getNum());
  111. if(!has)
  112. issues.emplace_back(QString(tr("Hero %1 must have an owner")).arg(ins->instanceName.c_str()), true);
  113. }
  114. if(ins->type)
  115. {
  116. if(map->allowedHeroes.count(ins->getHeroType()) == 0)
  117. issues.emplace_back(QString(tr("Hero %1 is prohibited by map settings")).arg(ins->type->getNameTranslated().c_str()), false);
  118. if(!allHeroesOnMap.insert(ins->type).second)
  119. issues.emplace_back(QString(tr("Hero %1 has duplicate on map")).arg(ins->type->getNameTranslated().c_str()), false);
  120. }
  121. else if(ins->ID != Obj::RANDOM_HERO)
  122. issues.emplace_back(QString(tr("Hero %1 has an empty type and must be removed")).arg(ins->instanceName.c_str()), true);
  123. }
  124. //checking for arts
  125. if(auto * ins = dynamic_cast<CGArtifact*>(o.get()))
  126. {
  127. if(ins->ID == Obj::SPELL_SCROLL)
  128. {
  129. if(ins->storedArtifact)
  130. {
  131. if(map->allowedSpells.count(ins->storedArtifact->getScrollSpellID()) == 0)
  132. issues.emplace_back(QString(tr("Spell scroll %1 is prohibited by map settings")).arg(ins->storedArtifact->getScrollSpellID().toEntity(VLC->spells())->getNameTranslated().c_str()), false);
  133. }
  134. else
  135. issues.emplace_back(QString(tr("Spell scroll %1 doesn't have instance assigned and must be removed")).arg(ins->instanceName.c_str()), true);
  136. }
  137. else
  138. {
  139. if(ins->ID == Obj::ARTIFACT && map->allowedArtifact.count(ins->getArtifact()) == 0)
  140. {
  141. issues.emplace_back(QString(tr("Artifact %1 is prohibited by map settings")).arg(ins->getObjectName().c_str()), false);
  142. }
  143. }
  144. }
  145. }
  146. //verification of starting towns
  147. for(auto & mp : amountOfCastles)
  148. if(mp.second == 0)
  149. issues.emplace_back(QString(tr("Player %1 doesn't have any starting town")).arg(mp.first), false);
  150. //verification of map name and description
  151. if(map->name.empty())
  152. issues.emplace_back(tr("Map name is not specified"), false);
  153. if(map->description.empty())
  154. issues.emplace_back(tr("Map description is not specified"), false);
  155. //verificationfor mods
  156. for(auto & mod : MapController::modAssessmentMap(*map))
  157. {
  158. if(!map->mods.count(mod.first))
  159. {
  160. issues.emplace_back(QString(tr("Map contains object from mod \"%1\", but doesn't require it")).arg(QString::fromStdString(VLC->modh->getModInfo(mod.first).getVerificationInfo().name)), true);
  161. }
  162. }
  163. }
  164. catch(const std::exception & e)
  165. {
  166. issues.emplace_back(QString(tr("Exception occurs during validation: %1")).arg(e.what()), true);
  167. }
  168. catch(...)
  169. {
  170. issues.emplace_back(tr("Unknown exception occurs during validation"), true);
  171. }
  172. return issues;
  173. }