validator.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. //checking all objects in the map
  67. for(auto o : map->objects)
  68. {
  69. //owners for objects
  70. if(o->getOwner() == PlayerColor::UNFLAGGABLE)
  71. {
  72. if(dynamic_cast<CGMine*>(o.get()) ||
  73. dynamic_cast<CGDwelling*>(o.get()) ||
  74. dynamic_cast<CGTownInstance*>(o.get()) ||
  75. dynamic_cast<CGGarrison*>(o.get()) ||
  76. dynamic_cast<CGHeroInstance*>(o.get()))
  77. {
  78. issues.emplace_back(QString("Armored instance %1 is UNFLAGGABLE but must have NEUTRAL or player owner").arg(o->instanceName.c_str()), true);
  79. }
  80. }
  81. if(o->getOwner() != PlayerColor::NEUTRAL && o->getOwner().getNum() < map->players.size())
  82. {
  83. if(!map->players[o->getOwner().getNum()].canAnyonePlay())
  84. issues.emplace_back(QString("Object %1 is assinged to non-playable player %2").arg(o->instanceName.c_str(), o->getOwner().getStr().c_str()), true);
  85. }
  86. //checking towns
  87. if(auto * ins = dynamic_cast<CGTownInstance*>(o.get()))
  88. {
  89. bool has = amountOfCastles.count(ins->getOwner().getNum());
  90. if(!has && ins->getOwner() != PlayerColor::NEUTRAL)
  91. issues.emplace_back(tr("Town %1 has undefined owner %2").arg(ins->instanceName.c_str(), ins->getOwner().getStr().c_str()), true);
  92. if(has)
  93. ++amountOfCastles[ins->getOwner().getNum()];
  94. }
  95. //checking heroes and prisons
  96. if(auto * ins = dynamic_cast<CGHeroInstance*>(o.get()))
  97. {
  98. if(ins->ID == Obj::PRISON)
  99. {
  100. if(ins->getOwner() != PlayerColor::NEUTRAL)
  101. issues.emplace_back(QString("Prison %1 must be a NEUTRAL").arg(ins->instanceName.c_str()), true);
  102. }
  103. else
  104. {
  105. bool has = amountOfCastles.count(ins->getOwner().getNum());
  106. if(!has)
  107. issues.emplace_back(QString("Hero %1 must have an owner").arg(ins->instanceName.c_str()), true);
  108. }
  109. if(ins->type)
  110. {
  111. if(!map->allowedHeroes[ins->type->getId().getNum()])
  112. issues.emplace_back(QString("Hero %1 is prohibited by map settings").arg(ins->type->getNameTranslated().c_str()), false);
  113. }
  114. else
  115. issues.emplace_back(QString("Hero %1 has an empty type and must be removed").arg(ins->instanceName.c_str()), true);
  116. }
  117. //checking for arts
  118. if(auto * ins = dynamic_cast<CGArtifact*>(o.get()))
  119. {
  120. if(ins->ID == Obj::SPELL_SCROLL)
  121. {
  122. if(ins->storedArtifact)
  123. {
  124. if(!map->allowedSpell[ins->storedArtifact->id.getNum()])
  125. issues.emplace_back(QString("Spell scroll %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false);
  126. }
  127. else
  128. issues.emplace_back(QString("Spell scroll %1 doesn't have instance assigned and must be removed").arg(ins->instanceName.c_str()), true);
  129. }
  130. else
  131. {
  132. if(ins->ID == Obj::ARTIFACT && !map->allowedArtifact[ins->subID])
  133. {
  134. issues.emplace_back(QString("Artifact %1 is prohibited by map settings").arg(ins->getObjectName().c_str()), false);
  135. }
  136. }
  137. }
  138. }
  139. //verification of starting towns
  140. for(auto & mp : amountOfCastles)
  141. if(mp.second == 0)
  142. issues.emplace_back(QString("Player %1 doesn't have any starting town").arg(mp.first), false);
  143. //verification of map name and description
  144. if(map->name.empty())
  145. issues.emplace_back("Map name is not specified", false);
  146. if(map->description.empty())
  147. issues.emplace_back("Map description is not specified", false);
  148. //verificationfor mods
  149. for(auto & mod : MapController::modAssessmentMap(*map))
  150. {
  151. if(!map->mods.count(mod.first))
  152. {
  153. 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);
  154. }
  155. }
  156. }
  157. catch(const std::exception & e)
  158. {
  159. issues.emplace_back(QString("Exception occurs during validation: %1").arg(e.what()), true);
  160. }
  161. catch(...)
  162. {
  163. issues.emplace_back("Unknown exception occurs during validation", true);
  164. }
  165. return issues;
  166. }