validator.cpp 5.0 KB

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