validator.cpp 4.8 KB

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