mapcontroller.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 "mapcontroller.h"
  11. #include "../lib/GameConstants.h"
  12. #include "../lib/mapping/CMapService.h"
  13. #include "../lib/mapping/CMap.h"
  14. #include "../lib/mapping/CMapEditManager.h"
  15. #include "../lib/TerrainHandler.h"
  16. #include "../lib/mapObjects/CObjectClassesHandler.h"
  17. #include "../lib/rmg/ObstaclePlacer.h"
  18. #include "../lib/CSkillHandler.h"
  19. #include "../lib/spells/CSpellHandler.h"
  20. #include "../lib/CHeroHandler.h"
  21. #include "../lib/serializer/CMemorySerializer.h"
  22. #include "mapview.h"
  23. #include "scenelayer.h"
  24. #include "maphandler.h"
  25. #include "mainwindow.h"
  26. #include "inspector/inspector.h"
  27. MapController::MapController(MainWindow * m): main(m)
  28. {
  29. for(int i : {0, 1})
  30. {
  31. _scenes[i].reset(new MapScene(i));
  32. _miniscenes[i].reset(new MinimapScene(i));
  33. }
  34. connectScenes();
  35. }
  36. void MapController::connectScenes()
  37. {
  38. for (int level = 0; level <= 1; level++)
  39. {
  40. //selections for both layers will be handled separately
  41. QObject::connect(_scenes[level].get(), &MapScene::selected, [this, level](bool anythingSelected)
  42. {
  43. main->onSelectionMade(level, anythingSelected);
  44. });
  45. }
  46. }
  47. MapController::~MapController()
  48. {
  49. }
  50. const std::unique_ptr<CMap> & MapController::getMapUniquePtr() const
  51. {
  52. return _map;
  53. }
  54. CMap * MapController::map()
  55. {
  56. return _map.get();
  57. }
  58. MapHandler * MapController::mapHandler()
  59. {
  60. return _mapHandler.get();
  61. }
  62. MapScene * MapController::scene(int level)
  63. {
  64. return _scenes[level].get();
  65. }
  66. MinimapScene * MapController::miniScene(int level)
  67. {
  68. return _miniscenes[level].get();
  69. }
  70. void MapController::repairMap()
  71. {
  72. //there might be extra skills, arts and spells not imported from map
  73. if(VLC->skillh->getDefaultAllowed().size() > map()->allowedAbilities.size())
  74. {
  75. map()->allowedAbilities.resize(VLC->skillh->getDefaultAllowed().size());
  76. }
  77. if(VLC->arth->getDefaultAllowed().size() > map()->allowedArtifact.size())
  78. {
  79. map()->allowedArtifact.resize(VLC->arth->getDefaultAllowed().size());
  80. }
  81. if(VLC->spellh->getDefaultAllowed().size() > map()->allowedSpell.size())
  82. {
  83. map()->allowedSpell.resize(VLC->spellh->getDefaultAllowed().size());
  84. }
  85. if(VLC->heroh->getDefaultAllowed().size() > map()->allowedHeroes.size())
  86. {
  87. map()->allowedHeroes.resize(VLC->heroh->getDefaultAllowed().size());
  88. }
  89. //fix owners for objects
  90. for(auto obj : _map->objects)
  91. {
  92. //setup proper names (hero name will be fixed later
  93. if(obj->ID != Obj::HERO && obj->ID != Obj::PRISON && (obj->typeName.empty() || obj->subTypeName.empty()))
  94. {
  95. auto handler = VLC->objtypeh->getHandlerFor(obj->ID, obj->subID);
  96. obj->typeName = handler->getTypeName();
  97. obj->subTypeName = handler->getSubTypeName();
  98. }
  99. //fix flags
  100. if(obj->getOwner() == PlayerColor::UNFLAGGABLE)
  101. {
  102. if(dynamic_cast<CGMine*>(obj.get()) ||
  103. dynamic_cast<CGDwelling*>(obj.get()) ||
  104. dynamic_cast<CGTownInstance*>(obj.get()) ||
  105. dynamic_cast<CGGarrison*>(obj.get()) ||
  106. dynamic_cast<CGShipyard*>(obj.get()) ||
  107. dynamic_cast<CGLighthouse*>(obj.get()) ||
  108. dynamic_cast<CGHeroInstance*>(obj.get()))
  109. obj->tempOwner = PlayerColor::NEUTRAL;
  110. }
  111. //fix hero instance
  112. if(auto * nih = dynamic_cast<CGHeroInstance*>(obj.get()))
  113. {
  114. map()->allowedHeroes.at(nih->subID) = true;
  115. auto type = VLC->heroh->objects[nih->subID];
  116. assert(type->heroClass);
  117. //TODO: find a way to get proper type name
  118. if(obj->ID == Obj::HERO)
  119. {
  120. nih->typeName = "hero";
  121. nih->subTypeName = type->heroClass->getJsonKey();
  122. }
  123. if(obj->ID == Obj::PRISON)
  124. {
  125. nih->typeName = "prison";
  126. nih->subTypeName = "prison";
  127. }
  128. nih->type = type;
  129. if(nih->ID == Obj::HERO) //not prison
  130. nih->appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, type->heroClass->getIndex())->getTemplates().front();
  131. //fix spells
  132. if(nih->spellbookContainsSpell(SpellID::PRESET))
  133. {
  134. nih->removeSpellFromSpellbook(SpellID::PRESET);
  135. }
  136. else
  137. {
  138. for(auto spellID : type->spells)
  139. nih->addSpellToSpellbook(spellID);
  140. }
  141. //fix portrait
  142. if(nih->portrait < 0 || nih->portrait == 255)
  143. nih->portrait = type->imageIndex;
  144. }
  145. //fix town instance
  146. if(auto * tnh = dynamic_cast<CGTownInstance*>(obj.get()))
  147. {
  148. if(tnh->getTown())
  149. {
  150. vstd::erase_if(tnh->builtBuildings, [tnh](BuildingID bid)
  151. {
  152. return !tnh->getTown()->buildings.count(bid);
  153. });
  154. vstd::erase_if(tnh->forbiddenBuildings, [tnh](BuildingID bid)
  155. {
  156. return !tnh->getTown()->buildings.count(bid);
  157. });
  158. }
  159. }
  160. //fix spell scrolls
  161. if(auto * art = dynamic_cast<CGArtifact*>(obj.get()))
  162. {
  163. if(art->ID == Obj::SPELL_SCROLL && !art->storedArtifact)
  164. {
  165. std::vector<SpellID> out;
  166. for(auto spell : VLC->spellh->objects) //spellh size appears to be greater (?)
  167. {
  168. //if(map->isAllowedSpell(spell->id))
  169. {
  170. out.push_back(spell->id);
  171. }
  172. }
  173. auto a = CArtifactInstance::createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
  174. art->storedArtifact = a;
  175. }
  176. else
  177. map()->allowedArtifact.at(art->subID) = true;
  178. }
  179. }
  180. }
  181. void MapController::setMap(std::unique_ptr<CMap> cmap)
  182. {
  183. _map = std::move(cmap);
  184. repairMap();
  185. for(int i : {0, 1})
  186. {
  187. _scenes[i].reset(new MapScene(i));
  188. _miniscenes[i].reset(new MinimapScene(i));
  189. }
  190. resetMapHandler();
  191. sceneForceUpdate();
  192. connectScenes();
  193. _map->getEditManager()->getUndoManager().setUndoCallback([this](bool allowUndo, bool allowRedo)
  194. {
  195. main->enableUndo(allowUndo);
  196. main->enableRedo(allowRedo);
  197. }
  198. );
  199. _map->getEditManager()->getUndoManager().clearAll();
  200. }
  201. void MapController::sceneForceUpdate()
  202. {
  203. _scenes[0]->updateViews();
  204. _miniscenes[0]->updateViews();
  205. if(_map->twoLevel)
  206. {
  207. _scenes[1]->updateViews();
  208. _miniscenes[1]->updateViews();
  209. }
  210. }
  211. void MapController::sceneForceUpdate(int level)
  212. {
  213. _scenes[level]->updateViews();
  214. _miniscenes[level]->updateViews();
  215. }
  216. void MapController::resetMapHandler()
  217. {
  218. if(!_mapHandler)
  219. _mapHandler.reset(new MapHandler());
  220. _mapHandler->reset(map());
  221. for(int i : {0, 1})
  222. {
  223. _scenes[i]->initialize(*this);
  224. _miniscenes[i]->initialize(*this);
  225. }
  226. }
  227. void MapController::commitTerrainChange(int level, const TerrainId & terrain)
  228. {
  229. std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
  230. _scenes[level]->selectionTerrainView.selection().end());
  231. if(v.empty())
  232. return;
  233. _scenes[level]->selectionTerrainView.clear();
  234. _scenes[level]->selectionTerrainView.draw();
  235. _map->getEditManager()->getTerrainSelection().setSelection(v);
  236. _map->getEditManager()->drawTerrain(terrain, &CRandomGenerator::getDefault());
  237. for(auto & t : v)
  238. _scenes[level]->terrainView.setDirty(t);
  239. _scenes[level]->terrainView.draw();
  240. _miniscenes[level]->updateViews();
  241. main->mapChanged();
  242. }
  243. void MapController::commitRoadOrRiverChange(int level, ui8 type, bool isRoad)
  244. {
  245. std::vector<int3> v(_scenes[level]->selectionTerrainView.selection().begin(),
  246. _scenes[level]->selectionTerrainView.selection().end());
  247. if(v.empty())
  248. return;
  249. _scenes[level]->selectionTerrainView.clear();
  250. _scenes[level]->selectionTerrainView.draw();
  251. _map->getEditManager()->getTerrainSelection().setSelection(v);
  252. if(isRoad)
  253. _map->getEditManager()->drawRoad(RoadId(type), &CRandomGenerator::getDefault());
  254. else
  255. _map->getEditManager()->drawRiver(RiverId(type), &CRandomGenerator::getDefault());
  256. for(auto & t : v)
  257. _scenes[level]->terrainView.setDirty(t);
  258. _scenes[level]->terrainView.draw();
  259. _miniscenes[level]->updateViews();
  260. main->mapChanged();
  261. }
  262. void MapController::commitObjectErase(int level)
  263. {
  264. auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
  265. if (selectedObjects.size() > 1)
  266. {
  267. //mass erase => undo in one operation
  268. _map->getEditManager()->removeObjects(selectedObjects);
  269. }
  270. else if (selectedObjects.size() == 1)
  271. {
  272. _map->getEditManager()->removeObject(*selectedObjects.begin());
  273. }
  274. else //nothing to erase - shouldn't be here
  275. {
  276. return;
  277. }
  278. for (auto obj : selectedObjects)
  279. {
  280. //invalidate tiles under objects
  281. _mapHandler->invalidate(_mapHandler->getTilesUnderObject(obj));
  282. _scenes[level]->objectsView.setDirty(obj);
  283. }
  284. _scenes[level]->selectionObjectsView.clear();
  285. _scenes[level]->objectsView.draw();
  286. _scenes[level]->selectionObjectsView.draw();
  287. _scenes[level]->passabilityView.update();
  288. _miniscenes[level]->updateViews();
  289. main->mapChanged();
  290. }
  291. void MapController::copyToClipboard(int level)
  292. {
  293. _clipboard.clear();
  294. _clipboardShiftIndex = 0;
  295. auto selectedObjects = _scenes[level]->selectionObjectsView.getSelection();
  296. for(auto * obj : selectedObjects)
  297. {
  298. assert(obj->pos.z == level);
  299. _clipboard.push_back(CMemorySerializer::deepCopy(*obj));
  300. }
  301. }
  302. void MapController::pasteFromClipboard(int level)
  303. {
  304. _scenes[level]->selectionObjectsView.clear();
  305. auto shift = int3::getDirs()[_clipboardShiftIndex++];
  306. if(_clipboardShiftIndex == int3::getDirs().size())
  307. _clipboardShiftIndex = 0;
  308. for(auto & objUniquePtr : _clipboard)
  309. {
  310. auto * obj = CMemorySerializer::deepCopy(*objUniquePtr).release();
  311. auto newPos = objUniquePtr->pos + shift;
  312. if(_map->isInTheMap(newPos))
  313. obj->pos = newPos;
  314. obj->pos.z = level;
  315. Initializer init(obj, defaultPlayer);
  316. _map->getEditManager()->insertObject(obj);
  317. _scenes[level]->selectionObjectsView.selectObject(obj);
  318. _mapHandler->invalidate(obj);
  319. }
  320. _scenes[level]->objectsView.draw();
  321. _scenes[level]->passabilityView.update();
  322. _scenes[level]->selectionObjectsView.draw();
  323. _miniscenes[level]->updateViews();
  324. main->mapChanged();
  325. }
  326. bool MapController::discardObject(int level) const
  327. {
  328. _scenes[level]->selectionObjectsView.clear();
  329. if(_scenes[level]->selectionObjectsView.newObject)
  330. {
  331. delete _scenes[level]->selectionObjectsView.newObject;
  332. _scenes[level]->selectionObjectsView.newObject = nullptr;
  333. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  334. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  335. _scenes[level]->selectionObjectsView.draw();
  336. return true;
  337. }
  338. return false;
  339. }
  340. void MapController::createObject(int level, CGObjectInstance * obj) const
  341. {
  342. _scenes[level]->selectionObjectsView.newObject = obj;
  343. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::MOVEMENT;
  344. _scenes[level]->selectionObjectsView.draw();
  345. }
  346. void MapController::commitObstacleFill(int level)
  347. {
  348. auto selection = _scenes[level]->selectionTerrainView.selection();
  349. if(selection.empty())
  350. return;
  351. //split by zones
  352. std::map<TerrainId, std::unique_ptr<EditorObstaclePlacer>> terrainSelected;
  353. for(auto & t : selection)
  354. {
  355. auto tl = _map->getTile(t);
  356. if(tl.blocked || tl.visitable)
  357. continue;
  358. auto terrain = tl.terType->getId();
  359. //FIXME: This map can be populated once at launch, not need to collect objects every time
  360. terrainSelected[terrain] = std::make_unique<EditorObstaclePlacer>(_map.get());
  361. terrainSelected[terrain]->addBlockedTile(t);
  362. }
  363. for(auto & sel : terrainSelected)
  364. {
  365. sel.second->collectPossibleObstacles(sel.first);
  366. sel.second->placeObstacles(CRandomGenerator::getDefault());
  367. }
  368. _mapHandler->invalidateObjects();
  369. _scenes[level]->selectionTerrainView.clear();
  370. _scenes[level]->selectionTerrainView.draw();
  371. _scenes[level]->objectsView.draw(false); //TODO: enable smart invalidation (setDirty)
  372. _scenes[level]->passabilityView.update();
  373. _miniscenes[level]->updateViews();
  374. main->mapChanged();
  375. }
  376. void MapController::commitObjectChange(int level)
  377. {
  378. for( auto * o : _scenes[level]->selectionObjectsView.getSelection())
  379. _scenes[level]->objectsView.setDirty(o);
  380. _scenes[level]->objectsView.draw();
  381. _scenes[level]->selectionObjectsView.draw();
  382. _scenes[level]->passabilityView.update();
  383. _miniscenes[level]->updateViews();
  384. main->mapChanged();
  385. }
  386. void MapController::commitChangeWithoutRedraw()
  387. {
  388. //DO NOT REDRAW
  389. main->mapChanged();
  390. }
  391. void MapController::commitObjectShift(int level)
  392. {
  393. auto shift = _scenes[level]->selectionObjectsView.shift;
  394. bool makeShift = !shift.isNull();
  395. if(makeShift)
  396. {
  397. for(auto * obj : _scenes[level]->selectionObjectsView.getSelection())
  398. {
  399. int3 pos = obj->pos;
  400. pos.z = level;
  401. pos.x += shift.x(); pos.y += shift.y();
  402. auto prevPositions = _mapHandler->getTilesUnderObject(obj);
  403. _scenes[level]->objectsView.setDirty(obj); //set dirty before movement
  404. _map->getEditManager()->moveObject(obj, pos);
  405. _mapHandler->invalidate(prevPositions);
  406. _mapHandler->invalidate(obj);
  407. }
  408. }
  409. _scenes[level]->selectionObjectsView.newObject = nullptr;
  410. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  411. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  412. if(makeShift)
  413. {
  414. _scenes[level]->objectsView.draw();
  415. _scenes[level]->selectionObjectsView.draw();
  416. _scenes[level]->passabilityView.update();
  417. _miniscenes[level]->updateViews();
  418. main->mapChanged();
  419. }
  420. }
  421. void MapController::commitObjectCreate(int level)
  422. {
  423. auto * newObj = _scenes[level]->selectionObjectsView.newObject;
  424. if(!newObj)
  425. return;
  426. auto shift = _scenes[level]->selectionObjectsView.shift;
  427. int3 pos = newObj->pos;
  428. pos.z = level;
  429. pos.x += shift.x(); pos.y += shift.y();
  430. newObj->pos = pos;
  431. Initializer init(newObj, defaultPlayer);
  432. _map->getEditManager()->insertObject(newObj);
  433. _mapHandler->invalidate(newObj);
  434. _scenes[level]->objectsView.setDirty(newObj);
  435. _scenes[level]->selectionObjectsView.newObject = nullptr;
  436. _scenes[level]->selectionObjectsView.shift = QPoint(0, 0);
  437. _scenes[level]->selectionObjectsView.selectionMode = SelectionObjectsLayer::NOTHING;
  438. _scenes[level]->objectsView.draw();
  439. _scenes[level]->selectionObjectsView.draw();
  440. _scenes[level]->passabilityView.update();
  441. _miniscenes[level]->updateViews();
  442. main->mapChanged();
  443. }
  444. bool MapController::canPlaceObject(int level, CGObjectInstance * newObj, QString & error) const
  445. {
  446. //find all objects of such type
  447. int objCounter = 0;
  448. for(auto o : _map->objects)
  449. {
  450. if(o->ID == newObj->ID && o->subID == newObj->subID)
  451. {
  452. ++objCounter;
  453. }
  454. }
  455. if(newObj->ID == Obj::GRAIL && objCounter >= 1) //special case for grail
  456. {
  457. auto typeName = QString::fromStdString(newObj->typeName);
  458. auto subTypeName = QString::fromStdString(newObj->subTypeName);
  459. error = QString("There can be only one grail object on the map");
  460. return false; //maplimit reached
  461. }
  462. if(defaultPlayer == PlayerColor::NEUTRAL && (newObj->ID == Obj::HERO || newObj->ID == Obj::RANDOM_HERO))
  463. {
  464. error = "Hero cannot be created as NEUTRAL";
  465. return false;
  466. }
  467. return true;
  468. }
  469. void MapController::undo()
  470. {
  471. _map->getEditManager()->getUndoManager().undo();
  472. resetMapHandler();
  473. sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
  474. main->mapChanged();
  475. }
  476. void MapController::redo()
  477. {
  478. _map->getEditManager()->getUndoManager().redo();
  479. resetMapHandler();
  480. sceneForceUpdate(); //TODO: use smart invalidation (setDirty)
  481. main->mapChanged();
  482. }
  483. ModCompatibilityInfo MapController::modAssessmentAll()
  484. {
  485. ModCompatibilityInfo result;
  486. for(auto primaryID : VLC->objtypeh->knownObjects())
  487. {
  488. for(auto secondaryID : VLC->objtypeh->knownSubObjects(primaryID))
  489. {
  490. auto handler = VLC->objtypeh->getHandlerFor(primaryID, secondaryID);
  491. auto modName = QString::fromStdString(handler->getJsonKey()).split(":").at(0).toStdString();
  492. if(modName != "core")
  493. result[modName] = VLC->modh->getModInfo(modName).version;
  494. }
  495. }
  496. return result;
  497. }
  498. ModCompatibilityInfo MapController::modAssessmentMap(const CMap & map)
  499. {
  500. ModCompatibilityInfo result;
  501. for(auto obj : map.objects)
  502. {
  503. if(obj->ID == Obj::HERO)
  504. continue; //stub!
  505. auto handler = VLC->objtypeh->getHandlerFor(obj->ID, obj->subID);
  506. auto modName = QString::fromStdString(handler->getJsonKey()).split(":").at(0).toStdString();
  507. if(modName != "core")
  508. result[modName] = VLC->modh->getModInfo(modName).version;
  509. }
  510. //TODO: terrains?
  511. return result;
  512. }