mapcontroller.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * mapcontroller.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 "mapcontroller.h"
  12. #include "../lib/GameConstants.h"
  13. #include "../lib/entities/artifact/CArtifact.h"
  14. #include "../lib/entities/hero/CHeroClass.h"
  15. #include "../lib/entities/hero/CHeroHandler.h"
  16. #include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
  17. #include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
  18. #include "../lib/mapObjects/ObjectTemplate.h"
  19. #include "../lib/mapping/CMapService.h"
  20. #include "../lib/mapping/CMap.h"
  21. #include "../lib/mapping/CMapEditManager.h"
  22. #include "../lib/mapping/ObstacleProxy.h"
  23. #include "../lib/modding/CModHandler.h"
  24. #include "../lib/modding/ModDescription.h"
  25. #include "../lib/TerrainHandler.h"
  26. #include "../lib/CSkillHandler.h"
  27. #include "../lib/spells/CSpellHandler.h"
  28. #include "../lib/CRandomGenerator.h"
  29. #include "../lib/serializer/CMemorySerializer.h"
  30. #include "mapsettings/modsettings.h"
  31. #include "mapview.h"
  32. #include "scenelayer.h"
  33. #include "maphandler.h"
  34. #include "mainwindow.h"
  35. #include "inspector/inspector.h"
  36. #include "GameLibrary.h"
  37. #include "PlayerSelectionDialog.h"
  38. MapController::MapController(QObject * parent)
  39. : QObject(parent)
  40. {
  41. }
  42. MapController::MapController(MainWindow * m): main(m)
  43. {
  44. for(int i : {0, 1})
  45. {
  46. _scenes[i].reset(new MapScene(i));
  47. _miniscenes[i].reset(new MinimapScene(i));
  48. }
  49. connectScenes();
  50. _cb = std::make_unique<EditorCallback>(nullptr);
  51. }
  52. void MapController::connectScenes()
  53. {
  54. for (int level = 0; level <= 1; level++)
  55. {
  56. //selections for both layers will be handled separately
  57. QObject::connect(_scenes[level].get(), &MapScene::selected, [this, level](bool anythingSelected)
  58. {
  59. main->onSelectionMade(level, anythingSelected);
  60. });
  61. }
  62. }
  63. MapController::~MapController()
  64. {
  65. main = nullptr;
  66. }
  67. void MapController::setCallback(std::unique_ptr<EditorCallback> cb)
  68. {
  69. _cb = std::move(cb);
  70. }
  71. EditorCallback * MapController::getCallback()
  72. {
  73. return _cb.get();
  74. }
  75. const std::unique_ptr<CMap> & MapController::getMapUniquePtr() const
  76. {
  77. return _map;
  78. }
  79. CMap * MapController::map()
  80. {
  81. return _map.get();
  82. }
  83. MapHandler * MapController::mapHandler()
  84. {
  85. return _mapHandler.get();
  86. }
  87. MapScene * MapController::scene(int level)
  88. {
  89. return _scenes[level].get();
  90. }
  91. MinimapScene * MapController::miniScene(int level)
  92. {
  93. return _miniscenes[level].get();
  94. }
  95. void MapController::repairMap()
  96. {
  97. repairMap(map());
  98. }
  99. void MapController::repairMap(CMap * map)
  100. {
  101. if(!map)
  102. return;
  103. assert(map->cb);
  104. //make sure events/rumors has name to have proper identifiers
  105. int emptyNameId = 1;
  106. for(auto & e : map->events)
  107. if(e.name.empty())
  108. e.name = "event_" + std::to_string(emptyNameId++);
  109. emptyNameId = 1;
  110. for(auto & e : map->rumors)
  111. if(e.name.empty())
  112. e.name = "rumor_" + std::to_string(emptyNameId++);
  113. //fix owners for objects
  114. std::vector<CGObjectInstance*> allImpactedObjects;
  115. for (const auto & object : map->objects)
  116. allImpactedObjects.push_back(object.get());
  117. for (const auto & hero : map->getHeroesInPool())
  118. allImpactedObjects.push_back(map->tryGetFromHeroPool(hero));
  119. for(auto obj : allImpactedObjects)
  120. {
  121. if(obj == nullptr)
  122. continue;
  123. //fix flags
  124. if(obj->asOwnable() != nullptr && obj->getOwner() == PlayerColor::UNFLAGGABLE)
  125. {
  126. obj->tempOwner = PlayerColor::NEUTRAL;
  127. }
  128. //fix hero instance
  129. if(auto * nih = dynamic_cast<CGHeroInstance*>(obj))
  130. {
  131. // All heroes present on map or in prisons need to be allowed to rehire them after they are defeated
  132. // FIXME: How about custom scenarios where defeated hero cannot be hired again?
  133. map->allowedHeroes.insert(nih->getHeroTypeID());
  134. auto const & type = LIBRARY->heroh->objects[nih->subID];
  135. assert(type->heroClass);
  136. if(nih->ID == Obj::HERO) //not prison
  137. nih->appearance = LIBRARY->objtypeh->getHandlerFor(Obj::HERO, type->heroClass->getIndex())->getTemplates().front();
  138. //fix spellbook
  139. if(nih->spellbookContainsSpell(SpellID::SPELLBOOK_PRESET))
  140. {
  141. nih->removeSpellFromSpellbook(SpellID::SPELLBOOK_PRESET);
  142. if(!nih->getArt(ArtifactPosition::SPELLBOOK) && type->haveSpellBook)
  143. nih->putArtifact(ArtifactPosition::SPELLBOOK, map->createArtifact(ArtifactID::SPELLBOOK));
  144. }
  145. }
  146. //fix town instance
  147. if(auto * tnh = dynamic_cast<CGTownInstance*>(obj))
  148. {
  149. if(tnh->getTown())
  150. {
  151. for(const auto & building : tnh->getBuildings())
  152. {
  153. if(!tnh->getTown()->buildings.count(building))
  154. tnh->removeBuilding(building);
  155. }
  156. vstd::erase_if(tnh->forbiddenBuildings, [tnh](BuildingID bid)
  157. {
  158. return !tnh->getTown()->buildings.count(bid);
  159. });
  160. }
  161. }
  162. //fix spell scrolls
  163. if(auto * art = dynamic_cast<CGArtifact*>(obj))
  164. {
  165. if(art->ID == Obj::SPELL_SCROLL && !art->getArtifactInstance())
  166. {
  167. std::vector<SpellID> out;
  168. for(auto const & spell : LIBRARY->spellh->objects) //spellh size appears to be greater (?)
  169. {
  170. //if(map->isAllowedSpell(spell->id))
  171. {
  172. out.push_back(spell->id);
  173. }
  174. }
  175. auto a = map->createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
  176. art->setArtifactInstance(a);
  177. }
  178. }
  179. //fix mines
  180. if(auto * mine = dynamic_cast<CGMine*>(obj))
  181. {
  182. if(!mine->isAbandoned())
  183. {
  184. mine->producedResource = GameResID(mine->subID);
  185. mine->producedQuantity = mine->defaultResProduction();
  186. }
  187. }
  188. }
  189. }
  190. void MapController::setMap(std::unique_ptr<CMap> cmap)
  191. {
  192. cmap->cb = _cb.get();
  193. _map = std::move(cmap);
  194. _cb->setMap(_map.get());
  195. repairMap();
  196. for(int i : {0, 1})
  197. {
  198. _scenes[i].reset(new MapScene(i));
  199. _miniscenes[i].reset(new MinimapScene(i));
  200. }
  201. resetMapHandler();
  202. sceneForceUpdate();
  203. connectScenes();
  204. _map->getEditManager()->getUndoManager().setUndoCallback([this](bool allowUndo, bool allowRedo)
  205. {
  206. if(!main)
  207. return;
  208. main->enableUndo(allowUndo);
  209. main->enableRedo(allowRedo);
  210. }
  211. );
  212. _map->getEditManager()->getUndoManager().clearAll();
  213. initObstaclePainters(_map.get());
  214. }
  215. void MapController::initObstaclePainters(CMap * map)
  216. {
  217. for (auto const & terrain : LIBRARY->terrainTypeHandler->objects)
  218. {
  219. auto terrainId = terrain->getId();
  220. _obstaclePainters[terrainId] = std::make_unique<EditorObstaclePlacer>(map);
  221. _obstaclePainters[terrainId]->collectPossibleObstacles(terrainId);
  222. }
  223. }
  224. void MapController::sceneForceUpdate()
  225. {
  226. _scenes[0]->updateViews();
  227. _miniscenes[0]->updateViews();
  228. if(_map->twoLevel)
  229. {
  230. _scenes[1]->updateViews();
  231. _miniscenes[1]->updateViews();
  232. }
  233. }
  234. void MapController::sceneForceUpdate(int level)
  235. {
  236. _scenes[level]->updateViews();
  237. _miniscenes[level]->updateViews();
  238. }
  239. void MapController::resetMapHandler()
  240. {
  241. if(!_mapHandler)
  242. _mapHandler.reset(new MapHandler());
  243. _mapHandler->reset(map());
  244. for(int i : {0, 1})
  245. {
  246. _scenes[i]->initialize(*this);
  247. _miniscenes[i]->initialize(*this);
  248. }
  249. }
  250. void MapController::commitTerrainChange(int level, const TerrainId & terrain)
  251. {
  252. static const int terrainDecorationPercentageLevel = 10;
  253. std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
  254. _scenes[level]->selectionTerrainView.selection().end());
  255. if(v.empty())
  256. return;
  257. _scenes[level]->selectionTerrainView.clear();
  258. _scenes[level]->selectionTerrainView.draw();
  259. _map->getEditManager()->getTerrainSelection().setSelection(v);
  260. _map->getEditManager()->drawTerrain(terrain, terrainDecorationPercentageLevel, &CRandomGenerator::getDefault());
  261. for(auto & t : v)
  262. _scenes[level]->terrainView.setDirty(t);
  263. _scenes[level]->terrainView.draw();
  264. _miniscenes[level]->updateViews();
  265. main->mapChanged();
  266. }
  267. void MapController::commitRoadOrRiverChange(int level, ui8 type, bool isRoad)
  268. {
  269. std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
  270. _scenes[level]->selectionTerrainView.selection().end());
  271. if(v.empty())
  272. return;
  273. _scenes[level]->selectionTerrainView.clear();
  274. _scenes[level]->selectionTerrainView.draw();
  275. _map->getEditManager()->getTerrainSelection().setSelection(v);
  276. if(isRoad)
  277. _map->getEditManager()->drawRoad(RoadId(type), &CRandomGenerator::getDefault());
  278. else
  279. _map->getEditManager()->drawRiver(RiverId(type), &CRandomGenerator::getDefault());
  280. for(auto & t : v)
  281. _scenes[level]->terrainView.setDirty(t);
  282. _scenes[level]->terrainView.draw();
  283. _miniscenes[level]->updateViews();
  284. main->mapChanged();
  285. }
  286. void MapController::commitObjectErase(int level)
  287. {
  288. auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
  289. if (selectedObjects.size() > 1)
  290. {
  291. //mass erase => undo in one operation
  292. _map->getEditManager()->removeObjects(selectedObjects);
  293. }
  294. else if (selectedObjects.size() == 1)
  295. {
  296. _map->getEditManager()->removeObject(*selectedObjects.begin());
  297. }
  298. else //nothing to erase - shouldn't be here
  299. {
  300. return;
  301. }
  302. for (auto & obj : selectedObjects)
  303. {
  304. //invalidate tiles under objects
  305. _mapHandler->removeObject(obj);
  306. _scenes[level]->objectsView.setDirty(obj);
  307. }
  308. _scenes[level]->selectionObjectsView.clear();
  309. _scenes[level]->objectsView.draw();
  310. _scenes[level]->selectionObjectsView.draw();
  311. _scenes[level]->passabilityView.update();
  312. _miniscenes[level]->updateViews();
  313. main->mapChanged();
  314. }
  315. void MapController::copyToClipboard(int level)
  316. {
  317. _clipboard.clear();
  318. _clipboardShiftIndex = 0;
  319. auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
  320. for(auto * obj : selectedObjects)
  321. {
  322. assert(obj->pos.z == level);
  323. _clipboard.push_back(CMemorySerializer::deepCopy(*obj, _cb.get()));
  324. }
  325. }
  326. void MapController::pasteFromClipboard(int level)
  327. {
  328. _scenes[level]->selectionObjectsView.clear();
  329. auto shift = int3::getDirs()[_clipboardShiftIndex++];
  330. if(_clipboardShiftIndex == int3::getDirs().size())
  331. _clipboardShiftIndex = 0;
  332. QStringList errors;
  333. for(auto & objUniquePtr : _clipboard)
  334. {
  335. auto obj = CMemorySerializer::deepCopyShared(*objUniquePtr, _cb.get());
  336. QString errorMsg;
  337. if(!canPlaceObject(obj.get(), errorMsg))
  338. {
  339. errors.push_back(std::move(errorMsg));
  340. continue;
  341. }
  342. auto newPos = objUniquePtr->pos + shift;
  343. if(_map->isInTheMap(newPos))
  344. obj->pos = newPos;
  345. obj->pos.z = level;
  346. obj->id = {};
  347. Initializer init(*this, obj.get(), defaultPlayer);
  348. _map->getEditManager()->insertObject(obj);
  349. _scenes[level]->selectionObjectsView.selectObject(obj.get());
  350. _mapHandler->invalidate(obj.get());
  351. }
  352. if(!errors.isEmpty())
  353. QMessageBox::warning(main, QObject::tr("Can't place object"), errors.join('\n'));
  354. _scenes[level]->objectsView.draw();
  355. _scenes[level]->passabilityView.update();
  356. _scenes[level]->selectionObjectsView.draw();
  357. _miniscenes[level]->updateViews();
  358. main->mapChanged();
  359. }
  360. bool MapController::discardObject(int level) const
  361. {
  362. _scenes[level]->selectionObjectsView.clear();
  363. if(_scenes[level]->selectionObjectsView.newObject)
  364. {
  365. _scenes[level]->selectionObjectsView.newObject.reset();
  366. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  367. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  368. _scenes[level]->selectionObjectsView.draw();
  369. return true;
  370. }
  371. return false;
  372. }
  373. void MapController::createObject(int level, std::shared_ptr<CGObjectInstance> obj) const
  374. {
  375. _scenes[level]->selectionObjectsView.newObject = obj;
  376. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  377. _scenes[level]->selectionObjectsView.draw();
  378. }
  379. void MapController::commitObstacleFill(int level)
  380. {
  381. auto selection = _scenes[level]->selectionTerrainView.selection();
  382. if(selection.empty())
  383. return;
  384. //split by zones
  385. for (auto & painter : _obstaclePainters)
  386. {
  387. painter.second->clearBlockedArea();
  388. }
  389. for(auto & t : selection)
  390. {
  391. auto tl = _map->getTile(t);
  392. if(tl.blocked() || tl.visitable())
  393. continue;
  394. auto terrain = tl.getTerrainID();
  395. _obstaclePainters[terrain]->addBlockedTile(t);
  396. }
  397. for(auto & sel : _obstaclePainters)
  398. {
  399. for(auto o : sel.second->placeObstacles(CRandomGenerator::getDefault()))
  400. {
  401. _mapHandler->invalidate(o.get());
  402. _scenes[level]->objectsView.setDirty(o.get());
  403. }
  404. }
  405. _scenes[level]->selectionTerrainView.clear();
  406. _scenes[level]->selectionTerrainView.draw();
  407. _scenes[level]->objectsView.draw();
  408. _scenes[level]->passabilityView.update();
  409. _miniscenes[level]->updateViews();
  410. main->mapChanged();
  411. }
  412. void MapController::commitObjectChange(int level)
  413. {
  414. for( auto * o : _scenes[level]->selectionObjectsView.getSelection())
  415. _scenes[level]->objectsView.setDirty(o);
  416. _scenes[level]->objectsView.draw();
  417. _scenes[level]->selectionObjectsView.draw();
  418. _scenes[level]->passabilityView.update();
  419. _miniscenes[level]->updateViews();
  420. main->mapChanged();
  421. }
  422. void MapController::commitChangeWithoutRedraw()
  423. {
  424. //DO NOT REDRAW
  425. main->mapChanged();
  426. }
  427. void MapController::commitObjectShift(int level)
  428. {
  429. auto shift = _scenes[level]->selectionObjectsView.shift;
  430. bool makeShift = !shift.isNull();
  431. if(makeShift)
  432. {
  433. for(auto * obj : _scenes[level]->selectionObjectsView.getSelection())
  434. {
  435. int3 pos = obj->pos;
  436. pos.z = level;
  437. pos.x += shift.x(); pos.y += shift.y();
  438. _scenes[level]->objectsView.setDirty(obj); //set dirty before movement
  439. _map->getEditManager()->moveObject(obj, pos);
  440. _mapHandler->invalidate(obj);
  441. }
  442. }
  443. _scenes[level]->selectionObjectsView.newObject = nullptr;
  444. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  445. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  446. if(makeShift)
  447. {
  448. _scenes[level]->objectsView.draw();
  449. _scenes[level]->selectionObjectsView.draw();
  450. _scenes[level]->passabilityView.update();
  451. _miniscenes[level]->updateViews();
  452. main->mapChanged();
  453. }
  454. }
  455. void MapController::commitObjectCreate(int level)
  456. {
  457. auto newObj = _scenes[level]->selectionObjectsView.newObject;
  458. if(!newObj)
  459. return;
  460. auto shift = _scenes[level]->selectionObjectsView.shift;
  461. int3 pos = newObj->pos;
  462. pos.z = level;
  463. pos.x += shift.x(); pos.y += shift.y();
  464. newObj->pos = pos;
  465. Initializer init(*this, newObj.get(), defaultPlayer);
  466. _map->getEditManager()->insertObject(newObj);
  467. _mapHandler->invalidate(newObj.get());
  468. _scenes[level]->objectsView.setDirty(newObj.get());
  469. _scenes[level]->selectionObjectsView.newObject = nullptr;
  470. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  471. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  472. _scenes[level]->objectsView.draw();
  473. _scenes[level]->selectionObjectsView.draw();
  474. _scenes[level]->passabilityView.update();
  475. _miniscenes[level]->updateViews();
  476. main->mapChanged();
  477. }
  478. bool MapController::canPlaceObject(const CGObjectInstance * newObj, QString & error) const
  479. {
  480. if(newObj->ID == Obj::GRAIL) //special case for grail
  481. return canPlaceGrail(newObj, error);
  482. if(defaultPlayer == PlayerColor::NEUTRAL && (newObj->ID == Obj::HERO || newObj->ID == Obj::RANDOM_HERO))
  483. return canPlaceHero(newObj, error);
  484. return checkRequiredMods(newObj, error);
  485. }
  486. bool MapController::canPlaceGrail(const CGObjectInstance * grailObj, QString & error) const
  487. {
  488. assert(grailObj->ID == Obj::GRAIL);
  489. //find all objects of such type
  490. int objCounter = 0;
  491. for(auto o : _map->objects)
  492. {
  493. if(o->ID == grailObj->ID && o->subID == grailObj->subID)
  494. {
  495. ++objCounter;
  496. }
  497. }
  498. if(objCounter >= 1)
  499. {
  500. error = QObject::tr("There can only be one grail object on the map.");
  501. return false; //maplimit reached
  502. }
  503. return true;
  504. }
  505. bool MapController::canPlaceHero(const CGObjectInstance * heroObj, QString & error) const
  506. {
  507. assert(heroObj->ID == Obj::HERO || heroObj->ID == Obj::RANDOM_HERO);
  508. PlayerSelectionDialog dialog(main);
  509. if(dialog.exec() == QDialog::Accepted)
  510. {
  511. main->switchDefaultPlayer(dialog.getSelectedPlayer());
  512. return true;
  513. }
  514. error = tr("Hero %1 cannot be created as NEUTRAL.").arg(QString::fromStdString(heroObj->instanceName));
  515. return false;
  516. }
  517. bool MapController::checkRequiredMods(const CGObjectInstance * obj, QString & error) const
  518. {
  519. ModCompatibilityInfo modsInfo;
  520. modAssessmentObject(obj, modsInfo);
  521. for(auto & mod : modsInfo)
  522. {
  523. if(!_map->mods.count(mod.first))
  524. {
  525. auto reply = QMessageBox::question(main,
  526. tr("Missing Required Mod"), modMissingMessage(mod.second) + tr("\n\nDo you want to do that now ?"),
  527. QMessageBox::Yes | QMessageBox::No);
  528. if(reply == QMessageBox::Yes)
  529. {
  530. _map->mods[mod.first] = LIBRARY->modh->getModInfo(mod.first).getVerificationInfo();
  531. Q_EMIT requestModsUpdate(modsInfo, true); // signal for MapSettings
  532. }
  533. else
  534. {
  535. error = tr("This object's mod is mandatory for map to remain valid.");
  536. return false;
  537. }
  538. }
  539. }
  540. return true;
  541. }
  542. QString MapController::modMissingMessage(const ModVerificationInfo & info)
  543. {
  544. QString modName = QString::fromStdString(info.name);
  545. QString submod;
  546. if(!info.parent.empty())
  547. submod = QObject::tr(" (submod of %1)").arg(QString::fromStdString(info.parent));
  548. return QObject::tr("The mod '%1'%2, is required by an object on the map.\n"
  549. "Add it to the map's required mods in Map->General settings.",
  550. "should be consistent with Map->General menu entry translation")
  551. .arg(modName, submod);
  552. }
  553. void MapController::undo()
  554. {
  555. _map->getEditManager()->getUndoManager().undo();
  556. resetMapHandler();
  557. sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
  558. main->mapChanged();
  559. }
  560. void MapController::redo()
  561. {
  562. _map->getEditManager()->getUndoManager().redo();
  563. resetMapHandler();
  564. sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
  565. main->mapChanged();
  566. }
  567. ModCompatibilityInfo MapController::modAssessmentAll()
  568. {
  569. ModCompatibilityInfo result;
  570. for(auto primaryID : LIBRARY->objtypeh->knownObjects())
  571. {
  572. for(auto secondaryID : LIBRARY->objtypeh->knownSubObjects(primaryID))
  573. {
  574. auto handler = LIBRARY->objtypeh->getHandlerFor(primaryID, secondaryID);
  575. auto modScope = handler->getModScope();
  576. if(modScope != "core")
  577. result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
  578. }
  579. }
  580. return result;
  581. }
  582. void MapController::modAssessmentObject(const CGObjectInstance * obj, ModCompatibilityInfo & result)
  583. {
  584. auto extractEntityMod = [&result](const auto & entity)
  585. {
  586. auto modScope = entity->getModScope();
  587. if(modScope != "core")
  588. result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
  589. };
  590. auto handler = obj->getObjectHandler();
  591. auto modScope = handler->getModScope();
  592. if(modScope != "core")
  593. result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
  594. if(obj->ID == Obj::TOWN || obj->ID == Obj::RANDOM_TOWN)
  595. {
  596. auto town = dynamic_cast<const CGTownInstance *>(obj);
  597. for(const auto & spellID : town->possibleSpells)
  598. {
  599. if(spellID == SpellID::PRESET)
  600. continue;
  601. extractEntityMod(spellID.toEntity(LIBRARY));
  602. }
  603. for(const auto & spellID : town->obligatorySpells)
  604. {
  605. extractEntityMod(spellID.toEntity(LIBRARY));
  606. }
  607. }
  608. if(obj->ID == Obj::HERO || obj->ID == Obj::RANDOM_HERO)
  609. {
  610. auto hero = dynamic_cast<const CGHeroInstance *>(obj);
  611. for(const auto & spellID : hero->getSpellsInSpellbook())
  612. {
  613. if(spellID == SpellID::PRESET || spellID == SpellID::SPELLBOOK_PRESET)
  614. continue;
  615. extractEntityMod(spellID.toEntity(LIBRARY));
  616. }
  617. for(const auto & [_, slotInfo] : hero->artifactsWorn)
  618. {
  619. extractEntityMod(slotInfo.getArt()->getTypeId().toEntity(LIBRARY));
  620. }
  621. for(const auto & art : hero->artifactsInBackpack)
  622. {
  623. extractEntityMod(art.getArt()->getTypeId().toEntity(LIBRARY));
  624. }
  625. }
  626. //TODO: terrains?
  627. }
  628. ModCompatibilityInfo MapController::modAssessmentMap(const CMap & map)
  629. {
  630. ModCompatibilityInfo result;
  631. for(auto obj : map.objects)
  632. {
  633. modAssessmentObject(obj.get(), result);
  634. }
  635. return result;
  636. }