validator.cpp 5.8 KB

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