AdventureMapShortcuts.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. * AdventureMapShortcuts.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 "AdventureMapShortcuts.h"
  12. #include "../CGameInfo.h"
  13. #include "../CMT.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../CServerHandler.h"
  16. #include "../PlayerLocalState.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../gui/WindowHandler.h"
  20. #include "../lobby/CSavingScreen.h"
  21. #include "../mapView/mapHandler.h"
  22. #include "../windows/CKingdomInterface.h"
  23. #include "../windows/CSpellWindow.h"
  24. #include "../windows/CMarketWindow.h"
  25. #include "../windows/GUIClasses.h"
  26. #include "../windows/settings/SettingsMainWindow.h"
  27. #include "AdventureMapInterface.h"
  28. #include "AdventureOptions.h"
  29. #include "AdventureState.h"
  30. #include "../../CCallback.h"
  31. #include "../../lib/CConfigHandler.h"
  32. #include "../../lib/texts/CGeneralTextHandler.h"
  33. #include "../../lib/mapObjects/CGHeroInstance.h"
  34. #include "../../lib/mapObjects/CGTownInstance.h"
  35. #include "../../lib/mapping/CMap.h"
  36. #include "../../lib/pathfinder/CGPathNode.h"
  37. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  38. AdventureMapShortcuts::AdventureMapShortcuts(AdventureMapInterface & owner)
  39. : owner(owner)
  40. , state(EAdventureState::NOT_INITIALIZED)
  41. , mapLevel(0)
  42. , searchLast({MapObjectID::NO_OBJ, 0})
  43. , searchPos(0)
  44. {}
  45. void AdventureMapShortcuts::setState(EAdventureState newState)
  46. {
  47. state = newState;
  48. }
  49. EAdventureState AdventureMapShortcuts::getState() const
  50. {
  51. return state;
  52. }
  53. void AdventureMapShortcuts::onMapViewMoved(const Rect & visibleArea, int newMapLevel)
  54. {
  55. mapLevel = newMapLevel;
  56. }
  57. std::vector<AdventureMapShortcutState> AdventureMapShortcuts::getShortcuts()
  58. {
  59. std::vector<AdventureMapShortcutState> result = {
  60. { EShortcut::ADVENTURE_KINGDOM_OVERVIEW, optionInMapView(), [this]() { this->showOverview(); } },
  61. { EShortcut::ADVENTURE_EXIT_WORLD_VIEW, optionInWorldView(), [this]() { this->worldViewBack(); } },
  62. { EShortcut::ADVENTURE_VIEW_WORLD, optionInMapView(), [this]() { this->worldViewScale1x(); } },
  63. { EShortcut::ADVENTURE_VIEW_WORLD_X1, optionInWorldView(), [this]() { this->worldViewScale1x(); } },
  64. { EShortcut::ADVENTURE_VIEW_WORLD_X2, optionInWorldView(), [this]() { this->worldViewScale2x(); } },
  65. { EShortcut::ADVENTURE_VIEW_WORLD_X4, optionInWorldView(), [this]() { this->worldViewScale4x(); } },
  66. { EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL, optionCanToggleLevel(), [this]() { this->switchMapLevel(); } },
  67. { EShortcut::ADVENTURE_QUEST_LOG, optionCanViewQuests(), [this]() { this->showQuestlog(); } },
  68. { EShortcut::ADVENTURE_TOGGLE_SLEEP, optionHeroSelected(), [this]() { this->toggleSleepWake(); } },
  69. { EShortcut::ADVENTURE_TOGGLE_GRID, optionInMapView(), [this]() { this->toggleGrid(); } },
  70. { EShortcut::ADVENTURE_TOGGLE_VISITABLE, optionInMapView(), [this]() { this->toggleVisitable(); } },
  71. { EShortcut::ADVENTURE_TOGGLE_BLOCKED, optionInMapView(), [this]() { this->toggleBlocked(); } },
  72. { EShortcut::ADVENTURE_TRACK_HERO, optionInMapView(), [this]() { this->toggleTrackHero(); } },
  73. { EShortcut::ADVENTURE_SET_HERO_ASLEEP, optionHeroAwake(), [this]() { this->setHeroSleeping(); } },
  74. { EShortcut::ADVENTURE_SET_HERO_AWAKE, optionHeroSleeping(), [this]() { this->setHeroAwake(); } },
  75. { EShortcut::ADVENTURE_MOVE_HERO, optionHeroCanMove(), [this]() { this->moveHeroAlongPath(); } },
  76. { EShortcut::ADVENTURE_CAST_SPELL, optionHeroSelected(), [this]() { this->showSpellbook(); } },
  77. { EShortcut::ADVENTURE_GAME_OPTIONS, optionInMapView(), [this]() { this->adventureOptions(); } },
  78. { EShortcut::GLOBAL_OPTIONS, optionInMapView(), [this]() { this->systemOptions(); } },
  79. { EShortcut::ADVENTURE_FIRST_HERO, optionInMapView(), [this]() { this->firstHero(); } },
  80. { EShortcut::ADVENTURE_NEXT_HERO, optionHasNextHero(), [this]() { this->nextHero(); } },
  81. { EShortcut::ADVENTURE_END_TURN, optionCanEndTurn(), [this]() { this->endTurn(); } },
  82. { EShortcut::ADVENTURE_THIEVES_GUILD, optionInMapView(), [this]() { this->showThievesGuild(); } },
  83. { EShortcut::ADVENTURE_VIEW_SCENARIO, optionInMapView(), [this]() { this->showScenarioInfo(); } },
  84. { EShortcut::ADVENTURE_QUIT_GAME, optionInMapView(), [this]() { this->quitGame(); } },
  85. { EShortcut::ADVENTURE_TO_MAIN_MENU, optionInMapView(), [this]() { this->toMainMenu(); } },
  86. { EShortcut::ADVENTURE_SAVE_GAME, optionInMapView(), [this]() { this->saveGame(); } },
  87. { EShortcut::ADVENTURE_NEW_GAME, optionInMapView(), [this]() { this->newGame(); } },
  88. { EShortcut::ADVENTURE_LOAD_GAME, optionInMapView(), [this]() { this->loadGame(); } },
  89. { EShortcut::ADVENTURE_RESTART_GAME, optionInMapView(), [this]() { this->restartGame(); } },
  90. { EShortcut::ADVENTURE_DIG_GRAIL, optionHeroSelected(), [this]() { this->digGrail(); } },
  91. { EShortcut::ADVENTURE_VIEW_PUZZLE, optionSidePanelActive(),[this]() { this->viewPuzzleMap(); } },
  92. { EShortcut::ADVENTURE_VISIT_OBJECT, optionCanVisitObject(), [this]() { this->visitObject(); } },
  93. { EShortcut::ADVENTURE_VIEW_SELECTED, optionInMapView(), [this]() { this->openObject(); } },
  94. { EShortcut::ADVENTURE_MARKETPLACE, optionInMapView(), [this]() { this->showMarketplace(); } },
  95. { EShortcut::ADVENTURE_ZOOM_IN, optionSidePanelActive(),[this]() { this->zoom(+10); } },
  96. { EShortcut::ADVENTURE_ZOOM_OUT, optionSidePanelActive(),[this]() { this->zoom(-10); } },
  97. { EShortcut::ADVENTURE_ZOOM_RESET, optionSidePanelActive(),[this]() { this->zoom( 0); } },
  98. { EShortcut::ADVENTURE_FIRST_TOWN, optionInMapView(), [this]() { this->firstTown(); } },
  99. { EShortcut::ADVENTURE_NEXT_TOWN, optionInMapView(), [this]() { this->nextTown(); } },
  100. { EShortcut::ADVENTURE_NEXT_OBJECT, optionInMapView(), [this]() { this->nextObject(); } },
  101. { EShortcut::ADVENTURE_MOVE_HERO_SW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, +1}); } },
  102. { EShortcut::ADVENTURE_MOVE_HERO_SS, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, +1}); } },
  103. { EShortcut::ADVENTURE_MOVE_HERO_SE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, +1}); } },
  104. { EShortcut::ADVENTURE_MOVE_HERO_WW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, 0}); } },
  105. { EShortcut::ADVENTURE_MOVE_HERO_EE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, 0}); } },
  106. { EShortcut::ADVENTURE_MOVE_HERO_NW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, -1}); } },
  107. { EShortcut::ADVENTURE_MOVE_HERO_NN, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, -1}); } },
  108. { EShortcut::ADVENTURE_MOVE_HERO_NE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, -1}); } },
  109. { EShortcut::ADVENTURE_SEARCH, optionSidePanelActive(),[this]() { this->search(); } }
  110. };
  111. return result;
  112. }
  113. void AdventureMapShortcuts::showOverview()
  114. {
  115. GH.windows().createAndPushWindow<CKingdomInterface>();
  116. }
  117. void AdventureMapShortcuts::worldViewBack()
  118. {
  119. owner.hotkeyExitWorldView();
  120. auto hero = LOCPLINT->localState->getCurrentHero();
  121. if (hero)
  122. owner.centerOnObject(hero);
  123. }
  124. void AdventureMapShortcuts::worldViewScale1x()
  125. {
  126. // TODO set corresponding scale button to "selected" mode
  127. owner.openWorldView(7);
  128. }
  129. void AdventureMapShortcuts::worldViewScale2x()
  130. {
  131. owner.openWorldView(11);
  132. }
  133. void AdventureMapShortcuts::worldViewScale4x()
  134. {
  135. owner.openWorldView(16);
  136. }
  137. void AdventureMapShortcuts::switchMapLevel()
  138. {
  139. int maxLevels = LOCPLINT->cb->getMapSize().z;
  140. if (maxLevels < 2)
  141. return;
  142. owner.hotkeySwitchMapLevel();
  143. }
  144. void AdventureMapShortcuts::showQuestlog()
  145. {
  146. LOCPLINT->showQuestLog();
  147. }
  148. void AdventureMapShortcuts::toggleTrackHero()
  149. {
  150. Settings s = settings.write["session"];
  151. s["adventureTrackHero"].Bool() = !settings["session"]["adventureTrackHero"].Bool();
  152. }
  153. void AdventureMapShortcuts::toggleGrid()
  154. {
  155. Settings s = settings.write["gameTweaks"];
  156. s["showGrid"].Bool() = !settings["gameTweaks"]["showGrid"].Bool();
  157. }
  158. void AdventureMapShortcuts::toggleVisitable()
  159. {
  160. Settings s = settings.write["session"];
  161. s["showVisitable"].Bool() = !settings["session"]["showVisitable"].Bool();
  162. }
  163. void AdventureMapShortcuts::toggleBlocked()
  164. {
  165. Settings s = settings.write["session"];
  166. s["showBlocked"].Bool() = !settings["session"]["showBlocked"].Bool();
  167. }
  168. void AdventureMapShortcuts::toggleSleepWake()
  169. {
  170. if (!optionHeroSelected())
  171. return;
  172. if (optionHeroAwake())
  173. setHeroSleeping();
  174. else
  175. setHeroAwake();
  176. }
  177. void AdventureMapShortcuts::setHeroSleeping()
  178. {
  179. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  180. if (h)
  181. {
  182. LOCPLINT->localState->setHeroAsleep(h);
  183. owner.onHeroChanged(h);
  184. nextHero();
  185. }
  186. }
  187. void AdventureMapShortcuts::setHeroAwake()
  188. {
  189. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  190. if (h)
  191. {
  192. LOCPLINT->localState->setHeroAwaken(h);
  193. owner.onHeroChanged(h);
  194. }
  195. }
  196. void AdventureMapShortcuts::moveHeroAlongPath()
  197. {
  198. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  199. if (!h || !LOCPLINT->localState->hasPath(h))
  200. return;
  201. LOCPLINT->moveHero(h, LOCPLINT->localState->getPath(h));
  202. }
  203. void AdventureMapShortcuts::showSpellbook()
  204. {
  205. if (!LOCPLINT->localState->getCurrentHero())
  206. return;
  207. owner.centerOnObject(LOCPLINT->localState->getCurrentHero());
  208. GH.windows().createAndPushWindow<CSpellWindow>(LOCPLINT->localState->getCurrentHero(), LOCPLINT, false);
  209. }
  210. void AdventureMapShortcuts::adventureOptions()
  211. {
  212. GH.windows().createAndPushWindow<AdventureOptions>();
  213. }
  214. void AdventureMapShortcuts::systemOptions()
  215. {
  216. GH.windows().createAndPushWindow<SettingsMainWindow>();
  217. }
  218. void AdventureMapShortcuts::firstHero()
  219. {
  220. if (!LOCPLINT->localState->getWanderingHeroes().empty())
  221. {
  222. const auto * hero = LOCPLINT->localState->getWanderingHero(0);
  223. LOCPLINT->localState->setSelection(hero);
  224. owner.centerOnObject(hero);
  225. }
  226. }
  227. void AdventureMapShortcuts::nextHero()
  228. {
  229. const auto * currHero = LOCPLINT->localState->getCurrentHero();
  230. const auto * nextHero = LOCPLINT->localState->getNextWanderingHero(currHero);
  231. if (nextHero)
  232. {
  233. LOCPLINT->localState->setSelection(nextHero);
  234. owner.centerOnObject(nextHero);
  235. }
  236. }
  237. void AdventureMapShortcuts::endTurn()
  238. {
  239. if(!LOCPLINT->makingTurn)
  240. return;
  241. if(settings["adventure"]["heroReminder"].Bool())
  242. {
  243. for(auto hero : LOCPLINT->localState->getWanderingHeroes())
  244. {
  245. if(!LOCPLINT->localState->isHeroSleeping(hero) && hero->movementPointsRemaining() > 0)
  246. {
  247. // Only show hero reminder if conditions met:
  248. // - There still movement points
  249. // - Hero don't have a path or there not points for first step on path
  250. LOCPLINT->localState->verifyPath(hero);
  251. if(!LOCPLINT->localState->hasPath(hero))
  252. {
  253. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  254. return;
  255. }
  256. auto path = LOCPLINT->localState->getPath(hero);
  257. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  258. {
  259. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  260. return;
  261. }
  262. }
  263. }
  264. }
  265. owner.hotkeyEndingTurn();
  266. }
  267. void AdventureMapShortcuts::showThievesGuild()
  268. {
  269. //find first town with tavern
  270. auto itr = range::find_if(LOCPLINT->localState->getOwnedTowns(), [](const CGTownInstance * town)
  271. {
  272. return town->hasBuilt(BuildingID::TAVERN);
  273. });
  274. if(itr != LOCPLINT->localState->getOwnedTowns().end())
  275. LOCPLINT->showThievesGuildWindow(*itr);
  276. else
  277. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  278. }
  279. void AdventureMapShortcuts::showScenarioInfo()
  280. {
  281. AdventureOptions::showScenarioInfo();
  282. }
  283. void AdventureMapShortcuts::toMainMenu()
  284. {
  285. LOCPLINT->showYesNoDialog(
  286. CGI->generaltexth->allTexts[578],
  287. []()
  288. {
  289. CSH->endGameplay();
  290. CMM->menu->switchToTab("main");
  291. },
  292. 0
  293. );
  294. }
  295. void AdventureMapShortcuts::newGame()
  296. {
  297. LOCPLINT->showYesNoDialog(
  298. CGI->generaltexth->allTexts[578],
  299. []()
  300. {
  301. CSH->endGameplay();
  302. CMM->menu->switchToTab("new");
  303. },
  304. nullptr
  305. );
  306. }
  307. void AdventureMapShortcuts::quitGame()
  308. {
  309. LOCPLINT->showYesNoDialog(
  310. CGI->generaltexth->allTexts[578],
  311. []()
  312. {
  313. GH.dispatchMainThread( []()
  314. {
  315. handleQuit(false);
  316. });
  317. },
  318. 0
  319. );
  320. }
  321. void AdventureMapShortcuts::saveGame()
  322. {
  323. GH.windows().createAndPushWindow<CSavingScreen>();
  324. }
  325. void AdventureMapShortcuts::loadGame()
  326. {
  327. LOCPLINT->proposeLoadingGame();
  328. }
  329. void AdventureMapShortcuts::digGrail()
  330. {
  331. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  332. if(h && LOCPLINT->makingTurn)
  333. LOCPLINT->tryDigging(h);
  334. }
  335. void AdventureMapShortcuts::viewPuzzleMap()
  336. {
  337. LOCPLINT->showPuzzleMap();
  338. }
  339. void AdventureMapShortcuts::restartGame()
  340. {
  341. LOCPLINT->showYesNoDialog(
  342. CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  343. []()
  344. {
  345. GH.dispatchMainThread(
  346. []()
  347. {
  348. CSH->sendRestartGame();
  349. }
  350. );
  351. },
  352. nullptr
  353. );
  354. }
  355. void AdventureMapShortcuts::visitObject()
  356. {
  357. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  358. if(h)
  359. LOCPLINT->cb->moveHero(h, h->pos, false);
  360. }
  361. void AdventureMapShortcuts::openObject()
  362. {
  363. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  364. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown();
  365. if(h)
  366. LOCPLINT->openHeroWindow(h);
  367. if(t)
  368. LOCPLINT->openTownWindow(t);
  369. }
  370. void AdventureMapShortcuts::showMarketplace()
  371. {
  372. //check if we have any marketplace
  373. const CGTownInstance *townWithMarket = nullptr;
  374. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  375. {
  376. if(t->hasBuilt(BuildingID::MARKETPLACE))
  377. {
  378. townWithMarket = t;
  379. break;
  380. }
  381. }
  382. if(townWithMarket) //if any town has marketplace, open window
  383. GH.windows().createAndPushWindow<CMarketWindow>(townWithMarket, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  384. else //if not - complain
  385. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  386. }
  387. void AdventureMapShortcuts::firstTown()
  388. {
  389. if (!LOCPLINT->localState->getOwnedTowns().empty())
  390. {
  391. const auto * town = LOCPLINT->localState->getOwnedTown(0);
  392. LOCPLINT->localState->setSelection(town);
  393. owner.centerOnObject(town);
  394. }
  395. }
  396. void AdventureMapShortcuts::nextTown()
  397. {
  398. owner.hotkeyNextTown();
  399. }
  400. void AdventureMapShortcuts::zoom( int distance)
  401. {
  402. owner.hotkeyZoom(distance, false);
  403. }
  404. void AdventureMapShortcuts::search()
  405. {
  406. // get all relevant objects
  407. std::vector<ObjectInstanceID> visitableObjInstances;
  408. int3 mapSizes = LOCPLINT->cb->getMapSize();
  409. for(int x = 0; x < mapSizes.x; x++)
  410. for(int y = 0; y < mapSizes.y; y++)
  411. for(int z = 0; z < mapSizes.z; z++)
  412. for(auto & obj : LOCPLINT->cb->getVisitableObjs(int3(x, y, z), false))
  413. if(obj->ID != MapObjectID::MONSTER && obj->ID != MapObjectID::EVENT && obj->ID != MapObjectID::HERO)
  414. visitableObjInstances.push_back(obj->id);
  415. // count of elements for each group
  416. std::map<std::pair<MapObjectID, MapObjectSubID>, int> mapObjCount;
  417. for(auto & obj : visitableObjInstances)
  418. mapObjCount[{ LOCPLINT->cb->getObjInstance(obj)->getObjGroupIndex(), LOCPLINT->cb->getObjInstance(obj)->getObjTypeIndex() }]++;
  419. // sort by name
  420. std::vector<std::pair<std::pair<MapObjectID, MapObjectSubID>, int>> mapObjCountList;
  421. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  422. mapObjCountList.push_back(*itr);
  423. std::sort(mapObjCountList.begin(), mapObjCountList.end(),
  424. [=](std::pair<std::pair<MapObjectID, MapObjectSubID>, int>& a, std::pair<std::pair<MapObjectID, MapObjectSubID>, int>& b)
  425. {
  426. return VLC->objtypeh->getObjectName(a.first.first, a.first.second) < VLC->objtypeh->getObjectName(b.first.first, b.first.second);
  427. }
  428. );
  429. // get pos of last selection
  430. int lastSel = 0;
  431. for(int i = 0; i < mapObjCountList.size(); i++)
  432. if(mapObjCountList[i].first == searchLast)
  433. lastSel = i;
  434. // create texts
  435. std::vector<std::string> texts;
  436. for(auto & obj : mapObjCountList)
  437. texts.push_back(VLC->objtypeh->getObjectName(obj.first.first, obj.first.second) + " (" + std::to_string(obj.second) + ")");
  438. GH.windows().createAndPushWindow<CObjectListWindow>(texts, nullptr, CGI->generaltexth->translate("vcmi.adventureMap.search.hover"), CGI->generaltexth->translate("vcmi.adventureMap.search.help"),
  439. [this, mapObjCountList, visitableObjInstances](int index)
  440. {
  441. auto selObj = mapObjCountList[index].first;
  442. // filter for matching objects
  443. std::vector<ObjectInstanceID> selVisitableObjInstances;
  444. for(auto & obj : visitableObjInstances)
  445. if(selObj == std::pair<MapObjectID, MapObjectSubID>{ LOCPLINT->cb->getObjInstance(obj)->getObjGroupIndex(), LOCPLINT->cb->getObjInstance(obj)->getObjTypeIndex() })
  446. selVisitableObjInstances.push_back(obj);
  447. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj)
  448. searchPos++;
  449. else
  450. searchPos = 0;
  451. auto objInst = LOCPLINT->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  452. owner.centerOnObject(objInst);
  453. searchLast = { objInst->getObjGroupIndex(), objInst->getObjTypeIndex() };
  454. }, lastSel);
  455. }
  456. void AdventureMapShortcuts::nextObject()
  457. {
  458. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  459. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown();
  460. if(h)
  461. nextHero();
  462. if(t)
  463. nextTown();
  464. }
  465. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  466. {
  467. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  468. if(!h)
  469. return;
  470. if (CGI->mh->hasOngoingAnimations())
  471. return;
  472. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  473. if (!CGI->mh->isInMap((dst)))
  474. return;
  475. if ( !LOCPLINT->localState->setPath(h, dst))
  476. return;
  477. const CGPath & path = LOCPLINT->localState->getPath(h);
  478. if (path.nodes.size() > 2)
  479. owner.onHeroChanged(h);
  480. else
  481. if(path.nodes[0].turns == 0)
  482. LOCPLINT->moveHero(h, path);
  483. }
  484. bool AdventureMapShortcuts::optionCanViewQuests()
  485. {
  486. return optionInMapView() && !CGI->mh->getMap()->quests.empty();
  487. }
  488. bool AdventureMapShortcuts::optionCanToggleLevel()
  489. {
  490. return optionSidePanelActive() && LOCPLINT->cb->getMapSize().z > 1;
  491. }
  492. bool AdventureMapShortcuts::optionMapLevelSurface()
  493. {
  494. return mapLevel == 0;
  495. }
  496. bool AdventureMapShortcuts::optionHeroSleeping()
  497. {
  498. const CGHeroInstance *hero = LOCPLINT->localState->getCurrentHero();
  499. return optionInMapView() && hero && LOCPLINT->localState->isHeroSleeping(hero);
  500. }
  501. bool AdventureMapShortcuts::optionHeroAwake()
  502. {
  503. const CGHeroInstance *hero = LOCPLINT->localState->getCurrentHero();
  504. return optionInMapView() && hero && !LOCPLINT->localState->isHeroSleeping(hero);
  505. }
  506. bool AdventureMapShortcuts::optionCanVisitObject()
  507. {
  508. if (!optionHeroSelected())
  509. return false;
  510. auto * hero = LOCPLINT->localState->getCurrentHero();
  511. auto objects = LOCPLINT->cb->getVisitableObjs(hero->visitablePos());
  512. return objects.size() > 1; // there is object other than our hero
  513. }
  514. bool AdventureMapShortcuts::optionHeroSelected()
  515. {
  516. return optionInMapView() && LOCPLINT->localState->getCurrentHero() != nullptr;
  517. }
  518. bool AdventureMapShortcuts::optionHeroCanMove()
  519. {
  520. const auto * hero = LOCPLINT->localState->getCurrentHero();
  521. return optionInMapView() && hero && LOCPLINT->localState->hasPath(hero) && LOCPLINT->localState->getPath(hero).nextNode().turns == 0;
  522. }
  523. bool AdventureMapShortcuts::optionHasNextHero()
  524. {
  525. const auto * hero = LOCPLINT->localState->getCurrentHero();
  526. const auto * nextSuitableHero = LOCPLINT->localState->getNextWanderingHero(hero);
  527. return optionInMapView() && nextSuitableHero != nullptr;
  528. }
  529. bool AdventureMapShortcuts::optionCanEndTurn()
  530. {
  531. return optionInMapView() && LOCPLINT->makingTurn;
  532. }
  533. bool AdventureMapShortcuts::optionSpellcasting()
  534. {
  535. return state == EAdventureState::CASTING_SPELL;
  536. }
  537. bool AdventureMapShortcuts::optionInMapView()
  538. {
  539. return state == EAdventureState::MAKING_TURN;
  540. }
  541. bool AdventureMapShortcuts::optionInWorldView()
  542. {
  543. return state == EAdventureState::WORLD_VIEW;
  544. }
  545. bool AdventureMapShortcuts::optionSidePanelActive()
  546. {
  547. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  548. }
  549. bool AdventureMapShortcuts::optionMapScrollingActive()
  550. {
  551. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  552. }
  553. bool AdventureMapShortcuts::optionMapViewActive()
  554. {
  555. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  556. }