2
0

mapcontroller.cpp 19 KB

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