mapcontroller.cpp 19 KB

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