mapcontroller.cpp 19 KB

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