validator.cpp 5.2 KB

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