mapcontroller.cpp 19 KB

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