validator.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/CHeroHandler.h"
  17. #include "../lib/CModHandler.h"
  18. Validator::Validator(const CMap * map, QWidget *parent) :
  19. QDialog(parent),
  20. ui(new Ui::Validator)
  21. {
  22. ui->setupUi(this);
  23. show();
  24. setAttribute(Qt::WA_DeleteOnClose);
  25. std::array<QString, 2> icons{"mapeditor/icons/mod-update.png", "mapeditor/icons/mod-delete.png"};
  26. for(auto & issue : Validator::validate(map))
  27. {
  28. auto * item = new QListWidgetItem(QIcon(icons[issue.critical ? 1 : 0]), issue.message);
  29. ui->listWidget->addItem(item);
  30. }
  31. }
  32. Validator::~Validator()
  33. {
  34. delete ui;
  35. }
  36. std::list<Validator::Issue> Validator::validate(const CMap * map)
  37. {
  38. std::list<Validator::Issue> issues;
  39. if(!map)
  40. {
  41. issues.emplace_back(tr("Map is not loaded"), true);
  42. return issues;
  43. }
  44. try
  45. {
  46. //check player settings
  47. int hplayers = 0;
  48. int cplayers = 0;
  49. std::map<int, int> amountOfCastles;
  50. for(int i = 0; i < map->players.size(); ++i)
  51. {
  52. auto & p = map->players[i];
  53. if(p.canAnyonePlay())
  54. amountOfCastles[i] = 0;
  55. if(p.canComputerPlay)
  56. ++cplayers;
  57. if(p.canHumanPlay)
  58. ++hplayers;
  59. if(p.allowedFactions.empty())
  60. issues.emplace_back(QString(tr("No factions allowed for player %1")).arg(i), true);
  61. }
  62. if(hplayers + cplayers == 0)
  63. issues.emplace_back(tr("No players allowed to play this map"), true);
  64. if(hplayers + cplayers == 1)
  65. issues.emplace_back(tr("Map is allowed for one player and cannot be started"), true);
  66. if(!hplayers)
  67. issues.emplace_back(tr("No human players allowed to play this map"), true);
  68. std::set<CHero*> allHeroesOnMap; //used to find hero duplicated
  69. //checking all objects in the map
  70. for(auto o : map->objects)
  71. {
  72. //owners for objects
  73. if(o->getOwner() == PlayerColor::UNFLAGGABLE)
  74. {
  75. if(dynamic_cast<CGMine*>(o.get()) ||
  76. dynamic_cast<CGDwelling*>(o.get()) ||
  77. dynamic_cast<CGTownInstance*>(o.get()) ||
  78. dynamic_cast<CGGarrison*>(o.get()) ||
  79. dynamic_cast<CGHeroInstance*>(o.get()))
  80. {
  81. issues.emplace_back(QString(tr("Armored instance %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.emplace_back(QString(tr("Object %1 is assigned to non-playable player %2")).arg(o->instanceName.c_str(), o->getOwner().getStr().c_str()), true);
  88. }
  89. //checking towns
  90. if(auto * ins = dynamic_cast<CGTownInstance*>(o.get()))
  91. {
  92. bool has = amountOfCastles.count(ins->getOwner().getNum());
  93. if(!has && ins->getOwner() != PlayerColor::NEUTRAL)
  94. issues.emplace_back(tr("Town %1 has undefined owner %2").arg(ins->instanceName.c_str(), ins->getOwner().getStr().c_str()), true);
  95. if(has)
  96. ++amountOfCastles[ins->getOwner().getNum()];
  97. }
  98. //checking 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.emplace_back(QString(tr("Prison %1 must be a NEUTRAL")).arg(ins->instanceName.c_str()), true);
  105. }
  106. else
  107. {
  108. bool has = amountOfCastles.count(ins->getOwner().getNum());
  109. if(!has)
  110. issues.emplace_back(QString(tr("Hero %1 must have an owner")).arg(ins->instanceName.c_str()), true);
  111. }
  112. if(ins->type)
  113. {
  114. if(!map->allowedHeroes[ins->type->getId().getNum()])
  115. issues.emplace_back(QString(tr("Hero %1 is prohibited by map settings")).arg(ins->type->getNameTranslated().c_str()), false);
  116. if(!allHeroesOnMap.insert(ins->type).second)
  117. issues.emplace_back(QString(tr("Hero %1 has duplicate on map")).arg(ins->type->getNameTranslated().c_str()), false);
  118. }
  119. else
  120. issues.emplace_back(QString(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[ins->storedArtifact->getId().getNum()])
  130. issues.emplace_back(QString(tr("Spell scroll %1 is prohibited by map settings")).arg(ins->getObjectName().c_str()), false);
  131. }
  132. else
  133. issues.emplace_back(QString(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[ins->subID])
  138. {
  139. issues.emplace_back(QString(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(auto & mp : amountOfCastles)
  146. if(mp.second == 0)
  147. issues.emplace_back(QString(tr("Player %1 doesn't have any starting town")).arg(mp.first), false);
  148. //verification of map name and description
  149. if(map->name.empty())
  150. issues.emplace_back(tr("Map name is not specified"), false);
  151. if(map->description.empty())
  152. issues.emplace_back(tr("Map description is not specified"), false);
  153. //verificationfor mods
  154. for(auto & mod : MapController::modAssessmentMap(*map))
  155. {
  156. if(!map->mods.count(mod.first))
  157. {
  158. issues.emplace_back(QString(tr("Map contains object from mod \"%1\", but doesn't require it")).arg(QString::fromStdString(VLC->modh->getModInfo(mod.first).name)), true);
  159. }
  160. }
  161. }
  162. catch(const std::exception & e)
  163. {
  164. issues.emplace_back(QString(tr("Exception occurs during validation: %1")).arg(e.what()), true);
  165. }
  166. catch(...)
  167. {
  168. issues.emplace_back(tr("Unknown exception occurs during validation"), true);
  169. }
  170. return issues;
  171. }