validator.cpp 5.8 KB

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