mapcontroller.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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; i < MAX_LEVELS; i++)
  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 i = 0; i < MAX_LEVELS; i++)
  55. {
  56. //selections for both layers will be handled separately
  57. QObject::connect(_scenes[i].get(), &MapScene::selected, [this, i](bool anythingSelected)
  58. {
  59. main->onSelectionMade(i, 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; i < _map->mapLevels; i++)
  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. for(int i = 0; i < _map->mapLevels; i++)
  227. {
  228. _scenes[i]->updateViews();
  229. _miniscenes[i]->updateViews();
  230. }
  231. }
  232. void MapController::sceneForceUpdate(int level)
  233. {
  234. _scenes[level]->updateViews();
  235. _miniscenes[level]->updateViews();
  236. }
  237. void MapController::resetMapHandler()
  238. {
  239. if(!_mapHandler)
  240. _mapHandler.reset(new MapHandler());
  241. _mapHandler->reset(map());
  242. for(int i = 0; i < MAX_LEVELS; i++)
  243. {
  244. _scenes[i]->initialize(*this);
  245. _miniscenes[i]->initialize(*this);
  246. }
  247. }
  248. void MapController::commitTerrainChange(int level, const TerrainId & terrain)
  249. {
  250. static const int terrainDecorationPercentageLevel = 10;
  251. std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
  252. _scenes[level]->selectionTerrainView.selection().end());
  253. if(v.empty())
  254. return;
  255. _scenes[level]->selectionTerrainView.clear();
  256. _scenes[level]->selectionTerrainView.draw();
  257. _map->getEditManager()->getTerrainSelection().setSelection(v);
  258. _map->getEditManager()->drawTerrain(terrain, terrainDecorationPercentageLevel, &CRandomGenerator::getDefault());
  259. for(auto & t : v)
  260. _scenes[level]->terrainView.setDirty(t);
  261. _scenes[level]->terrainView.draw();
  262. _miniscenes[level]->updateViews();
  263. main->mapChanged();
  264. }
  265. void MapController::commitRoadOrRiverChange(int level, ui8 type, bool isRoad)
  266. {
  267. std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
  268. _scenes[level]->selectionTerrainView.selection().end());
  269. if(v.empty())
  270. return;
  271. _scenes[level]->selectionTerrainView.clear();
  272. _scenes[level]->selectionTerrainView.draw();
  273. _map->getEditManager()->getTerrainSelection().setSelection(v);
  274. if(isRoad)
  275. _map->getEditManager()->drawRoad(RoadId(type), &CRandomGenerator::getDefault());
  276. else
  277. _map->getEditManager()->drawRiver(RiverId(type), &CRandomGenerator::getDefault());
  278. for(auto & t : v)
  279. _scenes[level]->terrainView.setDirty(t);
  280. _scenes[level]->terrainView.draw();
  281. _miniscenes[level]->updateViews();
  282. main->mapChanged();
  283. }
  284. void MapController::commitObjectErase(int level)
  285. {
  286. auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
  287. if (selectedObjects.size() > 1)
  288. {
  289. //mass erase => undo in one operation
  290. _map->getEditManager()->removeObjects(selectedObjects);
  291. }
  292. else if (selectedObjects.size() == 1)
  293. {
  294. _map->getEditManager()->removeObject(*selectedObjects.begin());
  295. }
  296. else //nothing to erase - shouldn't be here
  297. {
  298. return;
  299. }
  300. for (auto & obj : selectedObjects)
  301. {
  302. //invalidate tiles under objects
  303. _mapHandler->removeObject(obj);
  304. _scenes[level]->objectsView.setDirty(obj);
  305. }
  306. _scenes[level]->selectionObjectsView.clear();
  307. _scenes[level]->objectsView.draw();
  308. _scenes[level]->selectionObjectsView.draw();
  309. _scenes[level]->passabilityView.update();
  310. _miniscenes[level]->updateViews();
  311. main->mapChanged();
  312. }
  313. void MapController::copyToClipboard(int level)
  314. {
  315. _clipboard.clear();
  316. _clipboardShiftIndex = 0;
  317. auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
  318. for(auto * obj : selectedObjects)
  319. {
  320. assert(obj->pos.z == level);
  321. _clipboard.push_back(CMemorySerializer::deepCopy(*obj, _cb.get()));
  322. }
  323. }
  324. void MapController::pasteFromClipboard(int level)
  325. {
  326. _scenes[level]->selectionObjectsView.clear();
  327. auto shift = int3::getDirs()[_clipboardShiftIndex++];
  328. if(_clipboardShiftIndex == int3::getDirs().size())
  329. _clipboardShiftIndex = 0;
  330. QStringList errors;
  331. for(auto & objUniquePtr : _clipboard)
  332. {
  333. auto obj = CMemorySerializer::deepCopyShared(*objUniquePtr, _cb.get());
  334. QString errorMsg;
  335. if(!canPlaceObject(obj.get(), errorMsg))
  336. {
  337. errors.push_back(std::move(errorMsg));
  338. continue;
  339. }
  340. auto newPos = objUniquePtr->pos + shift;
  341. if(_map->isInTheMap(newPos))
  342. obj->pos = newPos;
  343. obj->pos.z = level;
  344. obj->id = {};
  345. Initializer init(*this, obj.get(), defaultPlayer);
  346. _map->getEditManager()->insertObject(obj);
  347. _scenes[level]->selectionObjectsView.selectObject(obj.get());
  348. _mapHandler->invalidate(obj.get());
  349. }
  350. if(!errors.isEmpty())
  351. QMessageBox::warning(main, QObject::tr("Can't place object"), errors.join('\n'));
  352. _scenes[level]->objectsView.draw();
  353. _scenes[level]->passabilityView.update();
  354. _scenes[level]->selectionObjectsView.draw();
  355. _miniscenes[level]->updateViews();
  356. main->mapChanged();
  357. }
  358. bool MapController::discardObject(int level) const
  359. {
  360. _scenes[level]->selectionObjectsView.clear();
  361. if(_scenes[level]->selectionObjectsView.newObject)
  362. {
  363. _scenes[level]->selectionObjectsView.newObject.reset();
  364. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  365. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  366. _scenes[level]->selectionObjectsView.draw();
  367. return true;
  368. }
  369. return false;
  370. }
  371. void MapController::createObject(int level, std::shared_ptr<CGObjectInstance> obj) const
  372. {
  373. _scenes[level]->selectionObjectsView.newObject = obj;
  374. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  375. _scenes[level]->selectionObjectsView.draw();
  376. }
  377. void MapController::commitObstacleFill(int level)
  378. {
  379. auto selection = _scenes[level]->selectionTerrainView.selection();
  380. if(selection.empty())
  381. return;
  382. //split by zones
  383. for (auto & painter : _obstaclePainters)
  384. {
  385. painter.second->clearBlockedArea();
  386. }
  387. for(auto & t : selection)
  388. {
  389. auto tl = _map->getTile(t);
  390. if(tl.blocked() || tl.visitable())
  391. continue;
  392. auto terrain = tl.getTerrainID();
  393. _obstaclePainters[terrain]->addBlockedTile(t);
  394. }
  395. for(auto & sel : _obstaclePainters)
  396. {
  397. for(auto o : sel.second->placeObstacles(CRandomGenerator::getDefault()))
  398. {
  399. _mapHandler->invalidate(o.get());
  400. _scenes[level]->objectsView.setDirty(o.get());
  401. }
  402. }
  403. _scenes[level]->selectionTerrainView.clear();
  404. _scenes[level]->selectionTerrainView.draw();
  405. _scenes[level]->objectsView.draw();
  406. _scenes[level]->passabilityView.update();
  407. _miniscenes[level]->updateViews();
  408. main->mapChanged();
  409. }
  410. void MapController::commitObjectChange(int level)
  411. {
  412. for( auto * o : _scenes[level]->selectionObjectsView.getSelection())
  413. _scenes[level]->objectsView.setDirty(o);
  414. _scenes[level]->objectsView.draw();
  415. _scenes[level]->selectionObjectsView.draw();
  416. _scenes[level]->passabilityView.update();
  417. _miniscenes[level]->updateViews();
  418. main->mapChanged();
  419. }
  420. void MapController::commitChangeWithoutRedraw()
  421. {
  422. //DO NOT REDRAW
  423. main->mapChanged();
  424. }
  425. void MapController::commitObjectShift(int level)
  426. {
  427. auto shift = _scenes[level]->selectionObjectsView.shift;
  428. bool makeShift = !shift.isNull();
  429. if(makeShift)
  430. {
  431. for(auto * obj : _scenes[level]->selectionObjectsView.getSelection())
  432. {
  433. int3 pos = obj->pos;
  434. pos.z = level;
  435. pos.x += shift.x(); pos.y += shift.y();
  436. _scenes[level]->objectsView.setDirty(obj); //set dirty before movement
  437. _map->getEditManager()->moveObject(obj, pos);
  438. _mapHandler->invalidate(obj);
  439. }
  440. }
  441. _scenes[level]->selectionObjectsView.newObject = nullptr;
  442. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  443. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  444. if(makeShift)
  445. {
  446. _scenes[level]->objectsView.draw();
  447. _scenes[level]->selectionObjectsView.draw();
  448. _scenes[level]->passabilityView.update();
  449. _miniscenes[level]->updateViews();
  450. main->mapChanged();
  451. }
  452. }
  453. void MapController::commitObjectCreate(int level)
  454. {
  455. auto newObj = _scenes[level]->selectionObjectsView.newObject;
  456. if(!newObj)
  457. return;
  458. auto shift = _scenes[level]->selectionObjectsView.shift;
  459. int3 pos = newObj->pos;
  460. pos.z = level;
  461. pos.x += shift.x(); pos.y += shift.y();
  462. newObj->pos = pos;
  463. Initializer init(*this, newObj.get(), defaultPlayer);
  464. _map->getEditManager()->insertObject(newObj);
  465. _mapHandler->invalidate(newObj.get());
  466. _scenes[level]->objectsView.setDirty(newObj.get());
  467. _scenes[level]->selectionObjectsView.newObject = nullptr;
  468. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  469. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  470. _scenes[level]->objectsView.draw();
  471. _scenes[level]->selectionObjectsView.draw();
  472. _scenes[level]->passabilityView.update();
  473. _miniscenes[level]->updateViews();
  474. main->mapChanged();
  475. }
  476. bool MapController::canPlaceObject(const CGObjectInstance * newObj, QString & error) const
  477. {
  478. if(newObj->ID == Obj::GRAIL) //special case for grail
  479. return canPlaceGrail(newObj, error);
  480. if(defaultPlayer == PlayerColor::NEUTRAL && (newObj->ID == Obj::HERO || newObj->ID == Obj::RANDOM_HERO))
  481. return canPlaceHero(newObj, error);
  482. return checkRequiredMods(newObj, error);
  483. }
  484. bool MapController::canPlaceGrail(const CGObjectInstance * grailObj, QString & error) const
  485. {
  486. assert(grailObj->ID == Obj::GRAIL);
  487. //find all objects of such type
  488. int objCounter = 0;
  489. for(auto o : _map->objects)
  490. {
  491. if(o->ID == grailObj->ID && o->subID == grailObj->subID)
  492. {
  493. ++objCounter;
  494. }
  495. }
  496. if(objCounter >= 1)
  497. {
  498. error = QObject::tr("There can only be one grail object on the map.");
  499. return false; //maplimit reached
  500. }
  501. return true;
  502. }
  503. bool MapController::canPlaceHero(const CGObjectInstance * heroObj, QString & error) const
  504. {
  505. assert(heroObj->ID == Obj::HERO || heroObj->ID == Obj::RANDOM_HERO);
  506. PlayerSelectionDialog dialog(main);
  507. if(dialog.exec() == QDialog::Accepted)
  508. {
  509. main->switchDefaultPlayer(dialog.getSelectedPlayer());
  510. return true;
  511. }
  512. error = tr("Hero %1 cannot be created as NEUTRAL.").arg(QString::fromStdString(heroObj->instanceName));
  513. return false;
  514. }
  515. bool MapController::checkRequiredMods(const CGObjectInstance * obj, QString & error) const
  516. {
  517. ModCompatibilityInfo modsInfo;
  518. modAssessmentObject(obj, modsInfo);
  519. for(auto & mod : modsInfo)
  520. {
  521. if(!_map->mods.count(mod.first))
  522. {
  523. auto reply = QMessageBox::question(main,
  524. tr("Missing Required Mod"), modMissingMessage(mod.second) + tr("\n\nDo you want to do that now ?"),
  525. QMessageBox::Yes | QMessageBox::No);
  526. if(reply == QMessageBox::Yes)
  527. {
  528. _map->mods[mod.first] = LIBRARY->modh->getModInfo(mod.first).getVerificationInfo();
  529. Q_EMIT requestModsUpdate(modsInfo, true); // signal for MapSettings
  530. }
  531. else
  532. {
  533. error = tr("This object's mod is mandatory for map to remain valid.");
  534. return false;
  535. }
  536. }
  537. }
  538. return true;
  539. }
  540. QString MapController::modMissingMessage(const ModVerificationInfo & info)
  541. {
  542. QString modName = QString::fromStdString(info.name);
  543. QString submod;
  544. if(!info.parent.empty())
  545. submod = QObject::tr(" (submod of %1)").arg(QString::fromStdString(info.parent));
  546. return QObject::tr("The mod '%1'%2, is required by an object on the map.\n"
  547. "Add it to the map's required mods in Map->General settings.",
  548. "should be consistent with Map->General menu entry translation")
  549. .arg(modName, submod);
  550. }
  551. void MapController::undo()
  552. {
  553. _map->getEditManager()->getUndoManager().undo();
  554. resetMapHandler();
  555. sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
  556. main->mapChanged();
  557. }
  558. void MapController::redo()
  559. {
  560. _map->getEditManager()->getUndoManager().redo();
  561. resetMapHandler();
  562. sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
  563. main->mapChanged();
  564. }
  565. ModCompatibilityInfo MapController::modAssessmentAll()
  566. {
  567. ModCompatibilityInfo result;
  568. for(auto primaryID : LIBRARY->objtypeh->knownObjects())
  569. {
  570. for(auto secondaryID : LIBRARY->objtypeh->knownSubObjects(primaryID))
  571. {
  572. auto handler = LIBRARY->objtypeh->getHandlerFor(primaryID, secondaryID);
  573. auto modScope = handler->getModScope();
  574. if(modScope != "core")
  575. result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
  576. }
  577. }
  578. return result;
  579. }
  580. void MapController::modAssessmentObject(const CGObjectInstance * obj, ModCompatibilityInfo & result)
  581. {
  582. auto extractEntityMod = [&result](const auto & entity)
  583. {
  584. auto modScope = entity->getModScope();
  585. if(modScope != "core")
  586. result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
  587. };
  588. auto handler = obj->getObjectHandler();
  589. auto modScope = handler->getModScope();
  590. if(modScope != "core")
  591. result[modScope] = LIBRARY->modh->getModInfo(modScope).getVerificationInfo();
  592. if(obj->ID == Obj::TOWN || obj->ID == Obj::RANDOM_TOWN)
  593. {
  594. auto town = dynamic_cast<const CGTownInstance *>(obj);
  595. for(const auto & spellID : town->possibleSpells)
  596. {
  597. if(spellID == SpellID::PRESET)
  598. continue;
  599. extractEntityMod(spellID.toEntity(LIBRARY));
  600. }
  601. for(const auto & spellID : town->obligatorySpells)
  602. {
  603. extractEntityMod(spellID.toEntity(LIBRARY));
  604. }
  605. }
  606. if(obj->ID == Obj::HERO || obj->ID == Obj::RANDOM_HERO)
  607. {
  608. auto hero = dynamic_cast<const CGHeroInstance *>(obj);
  609. for(const auto & spellID : hero->getSpellsInSpellbook())
  610. {
  611. if(spellID == SpellID::PRESET || spellID == SpellID::SPELLBOOK_PRESET)
  612. continue;
  613. extractEntityMod(spellID.toEntity(LIBRARY));
  614. }
  615. for(const auto & [_, slotInfo] : hero->artifactsWorn)
  616. {
  617. extractEntityMod(slotInfo.getArt()->getTypeId().toEntity(LIBRARY));
  618. }
  619. for(const auto & art : hero->artifactsInBackpack)
  620. {
  621. extractEntityMod(art.getArt()->getTypeId().toEntity(LIBRARY));
  622. }
  623. }
  624. //TODO: terrains?
  625. }
  626. ModCompatibilityInfo MapController::modAssessmentMap(const CMap & map)
  627. {
  628. ModCompatibilityInfo result;
  629. for(auto obj : map.objects)
  630. {
  631. modAssessmentObject(obj.get(), result);
  632. }
  633. return result;
  634. }