AdventureMapShortcuts.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 "../CMT.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../CServerHandler.h"
  15. #include "../PlayerLocalState.h"
  16. #include "../GameEngine.h"
  17. #include "../GameInstance.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("")
  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(false); } },
  110. { EShortcut::ADVENTURE_SEARCH_CONTINUE, optionSidePanelActive(),[this]() { this->search(true); } }
  111. };
  112. return result;
  113. }
  114. void AdventureMapShortcuts::showOverview()
  115. {
  116. ENGINE->windows().createAndPushWindow<CKingdomInterface>();
  117. }
  118. void AdventureMapShortcuts::worldViewBack()
  119. {
  120. owner.hotkeyExitWorldView();
  121. auto hero = GAME->interface()->localState->getCurrentHero();
  122. if (hero)
  123. owner.centerOnObject(hero);
  124. }
  125. void AdventureMapShortcuts::worldViewScale1x()
  126. {
  127. // TODO set corresponding scale button to "selected" mode
  128. owner.openWorldView(7);
  129. }
  130. void AdventureMapShortcuts::worldViewScale2x()
  131. {
  132. owner.openWorldView(11);
  133. }
  134. void AdventureMapShortcuts::worldViewScale4x()
  135. {
  136. owner.openWorldView(16);
  137. }
  138. void AdventureMapShortcuts::switchMapLevel()
  139. {
  140. int maxLevels = GAME->interface()->cb->getMapSize().z;
  141. if (maxLevels < 2)
  142. return;
  143. owner.hotkeySwitchMapLevel();
  144. }
  145. void AdventureMapShortcuts::showQuestlog()
  146. {
  147. GAME->interface()->showQuestLog();
  148. }
  149. void AdventureMapShortcuts::toggleTrackHero()
  150. {
  151. Settings s = settings.write["session"];
  152. s["adventureTrackHero"].Bool() = !settings["session"]["adventureTrackHero"].Bool();
  153. }
  154. void AdventureMapShortcuts::toggleGrid()
  155. {
  156. Settings s = settings.write["gameTweaks"];
  157. s["showGrid"].Bool() = !settings["gameTweaks"]["showGrid"].Bool();
  158. }
  159. void AdventureMapShortcuts::toggleVisitable()
  160. {
  161. Settings s = settings.write["session"];
  162. s["showVisitable"].Bool() = !settings["session"]["showVisitable"].Bool();
  163. }
  164. void AdventureMapShortcuts::toggleBlocked()
  165. {
  166. Settings s = settings.write["session"];
  167. s["showBlocked"].Bool() = !settings["session"]["showBlocked"].Bool();
  168. }
  169. void AdventureMapShortcuts::toggleSleepWake()
  170. {
  171. if (!optionHeroSelected())
  172. return;
  173. if (optionHeroAwake())
  174. setHeroSleeping();
  175. else
  176. setHeroAwake();
  177. }
  178. void AdventureMapShortcuts::setHeroSleeping()
  179. {
  180. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  181. if (h)
  182. {
  183. GAME->interface()->localState->setHeroAsleep(h);
  184. owner.onHeroChanged(h);
  185. nextHero();
  186. }
  187. }
  188. void AdventureMapShortcuts::setHeroAwake()
  189. {
  190. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  191. if (h)
  192. {
  193. GAME->interface()->localState->setHeroAwaken(h);
  194. owner.onHeroChanged(h);
  195. }
  196. }
  197. void AdventureMapShortcuts::moveHeroAlongPath()
  198. {
  199. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  200. if (!h || !GAME->interface()->localState->hasPath(h))
  201. return;
  202. GAME->interface()->moveHero(h, GAME->interface()->localState->getPath(h));
  203. }
  204. void AdventureMapShortcuts::showSpellbook()
  205. {
  206. if (!GAME->interface()->localState->getCurrentHero())
  207. return;
  208. owner.centerOnObject(GAME->interface()->localState->getCurrentHero());
  209. ENGINE->windows().createAndPushWindow<CSpellWindow>(GAME->interface()->localState->getCurrentHero(), GAME->interface(), false);
  210. }
  211. void AdventureMapShortcuts::adventureOptions()
  212. {
  213. ENGINE->windows().createAndPushWindow<AdventureOptions>();
  214. }
  215. void AdventureMapShortcuts::systemOptions()
  216. {
  217. ENGINE->windows().createAndPushWindow<SettingsMainWindow>();
  218. }
  219. void AdventureMapShortcuts::firstHero()
  220. {
  221. if (!GAME->interface()->localState->getWanderingHeroes().empty())
  222. {
  223. const auto * hero = GAME->interface()->localState->getWanderingHero(0);
  224. GAME->interface()->localState->setSelection(hero);
  225. owner.centerOnObject(hero);
  226. }
  227. }
  228. void AdventureMapShortcuts::nextHero()
  229. {
  230. const auto * currHero = GAME->interface()->localState->getCurrentHero();
  231. const auto * nextHero = GAME->interface()->localState->getNextWanderingHero(currHero);
  232. if (nextHero)
  233. {
  234. GAME->interface()->localState->setSelection(nextHero);
  235. owner.centerOnObject(nextHero);
  236. }
  237. }
  238. void AdventureMapShortcuts::endTurn()
  239. {
  240. if(!GAME->interface()->makingTurn)
  241. return;
  242. if(settings["adventure"]["heroReminder"].Bool())
  243. {
  244. for(auto hero : GAME->interface()->localState->getWanderingHeroes())
  245. {
  246. if(!GAME->interface()->localState->isHeroSleeping(hero) && hero->movementPointsRemaining() > 0)
  247. {
  248. // Only show hero reminder if conditions met:
  249. // - There still movement points
  250. // - Hero don't have a path or there not points for first step on path
  251. GAME->interface()->localState->verifyPath(hero);
  252. if(!GAME->interface()->localState->hasPath(hero))
  253. {
  254. GAME->interface()->showYesNoDialog( VLC->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  255. return;
  256. }
  257. auto path = GAME->interface()->localState->getPath(hero);
  258. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  259. {
  260. GAME->interface()->showYesNoDialog( VLC->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  261. return;
  262. }
  263. }
  264. }
  265. }
  266. owner.hotkeyEndingTurn();
  267. }
  268. void AdventureMapShortcuts::showThievesGuild()
  269. {
  270. //find first town with tavern
  271. auto itr = range::find_if(GAME->interface()->localState->getOwnedTowns(), [](const CGTownInstance * town)
  272. {
  273. return town->hasBuilt(BuildingID::TAVERN);
  274. });
  275. if(itr != GAME->interface()->localState->getOwnedTowns().end())
  276. GAME->interface()->showThievesGuildWindow(*itr);
  277. else
  278. GAME->interface()->showInfoDialog(VLC->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  279. }
  280. void AdventureMapShortcuts::showScenarioInfo()
  281. {
  282. AdventureOptions::showScenarioInfo();
  283. }
  284. void AdventureMapShortcuts::toMainMenu()
  285. {
  286. GAME->interface()->showYesNoDialog(
  287. VLC->generaltexth->allTexts[578],
  288. []()
  289. {
  290. GAME->server().endGameplay();
  291. CMM->menu->switchToTab("main");
  292. },
  293. 0
  294. );
  295. }
  296. void AdventureMapShortcuts::newGame()
  297. {
  298. GAME->interface()->showYesNoDialog(
  299. VLC->generaltexth->allTexts[578],
  300. []()
  301. {
  302. GAME->server().endGameplay();
  303. CMM->menu->switchToTab("new");
  304. },
  305. nullptr
  306. );
  307. }
  308. void AdventureMapShortcuts::quitGame()
  309. {
  310. GAME->interface()->showYesNoDialog(
  311. VLC->generaltexth->allTexts[578],
  312. []()
  313. {
  314. ENGINE->dispatchMainThread( []()
  315. {
  316. handleQuit(false);
  317. });
  318. },
  319. 0
  320. );
  321. }
  322. void AdventureMapShortcuts::saveGame()
  323. {
  324. ENGINE->windows().createAndPushWindow<CSavingScreen>();
  325. }
  326. void AdventureMapShortcuts::loadGame()
  327. {
  328. GAME->interface()->proposeLoadingGame();
  329. }
  330. void AdventureMapShortcuts::digGrail()
  331. {
  332. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  333. if(h && GAME->interface()->makingTurn)
  334. GAME->interface()->tryDigging(h);
  335. }
  336. void AdventureMapShortcuts::viewPuzzleMap()
  337. {
  338. GAME->interface()->showPuzzleMap();
  339. }
  340. void AdventureMapShortcuts::restartGame()
  341. {
  342. GAME->interface()->showYesNoDialog(
  343. VLC->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  344. []()
  345. {
  346. ENGINE->dispatchMainThread(
  347. []()
  348. {
  349. GAME->server().sendRestartGame();
  350. }
  351. );
  352. },
  353. nullptr
  354. );
  355. }
  356. void AdventureMapShortcuts::visitObject()
  357. {
  358. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  359. if(h)
  360. GAME->interface()->cb->moveHero(h, h->pos, false);
  361. }
  362. void AdventureMapShortcuts::openObject()
  363. {
  364. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  365. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  366. if(h)
  367. GAME->interface()->openHeroWindow(h);
  368. if(t)
  369. GAME->interface()->openTownWindow(t);
  370. }
  371. void AdventureMapShortcuts::showMarketplace()
  372. {
  373. //check if we have any marketplace
  374. const CGTownInstance *townWithMarket = nullptr;
  375. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  376. {
  377. if(t->hasBuilt(BuildingID::MARKETPLACE))
  378. {
  379. townWithMarket = t;
  380. break;
  381. }
  382. }
  383. if(townWithMarket) //if any town has marketplace, open window
  384. ENGINE->windows().createAndPushWindow<CMarketWindow>(townWithMarket, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  385. else //if not - complain
  386. GAME->interface()->showInfoDialog(VLC->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  387. }
  388. void AdventureMapShortcuts::firstTown()
  389. {
  390. if (!GAME->interface()->localState->getOwnedTowns().empty())
  391. {
  392. const auto * town = GAME->interface()->localState->getOwnedTown(0);
  393. GAME->interface()->localState->setSelection(town);
  394. owner.centerOnObject(town);
  395. }
  396. }
  397. void AdventureMapShortcuts::nextTown()
  398. {
  399. owner.hotkeyNextTown();
  400. }
  401. void AdventureMapShortcuts::zoom( int distance)
  402. {
  403. owner.hotkeyZoom(distance, false);
  404. }
  405. void AdventureMapShortcuts::search(bool next)
  406. {
  407. // get all relevant objects
  408. std::vector<ObjectInstanceID> visitableObjInstances;
  409. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  410. if(obj->ID != MapObjectID::MONSTER && obj->ID != MapObjectID::HERO && obj->ID != MapObjectID::TOWN)
  411. visitableObjInstances.push_back(obj->id);
  412. // count of elements for each group (map is already sorted)
  413. std::map<std::string, int> mapObjCount;
  414. for(auto & obj : visitableObjInstances)
  415. mapObjCount[{ GAME->interface()->cb->getObjInstance(obj)->getObjectName() }]++;
  416. // convert to vector for indexed access
  417. std::vector<std::pair<std::string, int>> textCountList;
  418. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  419. textCountList.push_back(*itr);
  420. // get pos of last selection
  421. int lastSel = 0;
  422. for(int i = 0; i < textCountList.size(); i++)
  423. if(textCountList[i].first == searchLast)
  424. lastSel = i;
  425. // create texts
  426. std::vector<std::string> texts;
  427. for(auto & obj : textCountList)
  428. texts.push_back(obj.first + " (" + std::to_string(obj.second) + ")");
  429. // function to center element from list on map
  430. auto selectObjOnMap = [this, textCountList, visitableObjInstances](int index)
  431. {
  432. auto selObj = textCountList[index].first;
  433. // filter for matching objects
  434. std::vector<ObjectInstanceID> selVisitableObjInstances;
  435. for(auto & obj : visitableObjInstances)
  436. if(selObj == GAME->interface()->cb->getObjInstance(obj)->getObjectName())
  437. selVisitableObjInstances.push_back(obj);
  438. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj)
  439. searchPos++;
  440. else
  441. searchPos = 0;
  442. auto objInst = GAME->interface()->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  443. owner.centerOnObject(objInst);
  444. searchLast = objInst->getObjectName();
  445. };
  446. if(next)
  447. selectObjOnMap(lastSel);
  448. else
  449. ENGINE->windows().createAndPushWindow<CObjectListWindow>(texts, nullptr, VLC->generaltexth->translate("vcmi.adventureMap.search.hover"), VLC->generaltexth->translate("vcmi.adventureMap.search.help"), [selectObjOnMap](int index){ selectObjOnMap(index); }, lastSel, std::vector<std::shared_ptr<IImage>>(), true);
  450. }
  451. void AdventureMapShortcuts::nextObject()
  452. {
  453. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  454. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  455. if(h)
  456. nextHero();
  457. if(t)
  458. nextTown();
  459. }
  460. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  461. {
  462. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero(); //selected hero
  463. if(!h)
  464. return;
  465. if (GAME->map().hasOngoingAnimations())
  466. return;
  467. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  468. if (!GAME->map().isInMap((dst)))
  469. return;
  470. if ( !GAME->interface()->localState->setPath(h, dst))
  471. return;
  472. const CGPath & path = GAME->interface()->localState->getPath(h);
  473. if (path.nodes.size() > 2)
  474. owner.onHeroChanged(h);
  475. else
  476. if(path.nodes[0].turns == 0)
  477. GAME->interface()->moveHero(h, path);
  478. }
  479. bool AdventureMapShortcuts::optionCanViewQuests()
  480. {
  481. return optionInMapView() && !GAME->map().getMap()->quests.empty();
  482. }
  483. bool AdventureMapShortcuts::optionCanToggleLevel()
  484. {
  485. return optionSidePanelActive() && GAME->interface()->cb->getMapSize().z > 1;
  486. }
  487. bool AdventureMapShortcuts::optionMapLevelSurface()
  488. {
  489. return mapLevel == 0;
  490. }
  491. bool AdventureMapShortcuts::optionHeroSleeping()
  492. {
  493. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  494. return optionInMapView() && hero && GAME->interface()->localState->isHeroSleeping(hero);
  495. }
  496. bool AdventureMapShortcuts::optionHeroAwake()
  497. {
  498. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  499. return optionInMapView() && hero && !GAME->interface()->localState->isHeroSleeping(hero);
  500. }
  501. bool AdventureMapShortcuts::optionCanVisitObject()
  502. {
  503. if (!optionHeroSelected())
  504. return false;
  505. auto * hero = GAME->interface()->localState->getCurrentHero();
  506. auto objects = GAME->interface()->cb->getVisitableObjs(hero->visitablePos());
  507. return objects.size() > 1; // there is object other than our hero
  508. }
  509. bool AdventureMapShortcuts::optionHeroSelected()
  510. {
  511. return optionInMapView() && GAME->interface()->localState->getCurrentHero() != nullptr;
  512. }
  513. bool AdventureMapShortcuts::optionHeroCanMove()
  514. {
  515. const auto * hero = GAME->interface()->localState->getCurrentHero();
  516. return optionInMapView() && hero && GAME->interface()->localState->hasPath(hero) && GAME->interface()->localState->getPath(hero).nextNode().turns == 0;
  517. }
  518. bool AdventureMapShortcuts::optionHasNextHero()
  519. {
  520. const auto * hero = GAME->interface()->localState->getCurrentHero();
  521. const auto * nextSuitableHero = GAME->interface()->localState->getNextWanderingHero(hero);
  522. return optionInMapView() && nextSuitableHero != nullptr;
  523. }
  524. bool AdventureMapShortcuts::optionCanEndTurn()
  525. {
  526. return optionInMapView() && GAME->interface()->makingTurn;
  527. }
  528. bool AdventureMapShortcuts::optionSpellcasting()
  529. {
  530. return state == EAdventureState::CASTING_SPELL;
  531. }
  532. bool AdventureMapShortcuts::optionInMapView()
  533. {
  534. return state == EAdventureState::MAKING_TURN;
  535. }
  536. bool AdventureMapShortcuts::optionInWorldView()
  537. {
  538. return state == EAdventureState::WORLD_VIEW;
  539. }
  540. bool AdventureMapShortcuts::optionSidePanelActive()
  541. {
  542. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  543. }
  544. bool AdventureMapShortcuts::optionMapScrollingActive()
  545. {
  546. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  547. }
  548. bool AdventureMapShortcuts::optionMapViewActive()
  549. {
  550. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  551. }