AdventureMapShortcuts.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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( LIBRARY->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( LIBRARY->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(LIBRARY->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. LIBRARY->generaltexth->allTexts[578],
  288. []()
  289. {
  290. GAME->server().endGameplay();
  291. GAME->mainmenu()->menu->switchToTab("main");
  292. },
  293. 0
  294. );
  295. }
  296. void AdventureMapShortcuts::newGame()
  297. {
  298. GAME->interface()->showYesNoDialog(
  299. LIBRARY->generaltexth->allTexts[578],
  300. []()
  301. {
  302. GAME->server().endGameplay();
  303. GAME->mainmenu()->menu->switchToTab("new");
  304. },
  305. nullptr
  306. );
  307. }
  308. void AdventureMapShortcuts::quitGame()
  309. {
  310. GAME->interface()->showYesNoDialog(
  311. LIBRARY->generaltexth->allTexts[578],
  312. [](){ GAME->onShutdownRequested(false);},
  313. nullptr
  314. );
  315. }
  316. void AdventureMapShortcuts::saveGame()
  317. {
  318. ENGINE->windows().createAndPushWindow<CSavingScreen>();
  319. }
  320. void AdventureMapShortcuts::loadGame()
  321. {
  322. GAME->interface()->proposeLoadingGame();
  323. }
  324. void AdventureMapShortcuts::digGrail()
  325. {
  326. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  327. if(h && GAME->interface()->makingTurn)
  328. GAME->interface()->tryDigging(h);
  329. }
  330. void AdventureMapShortcuts::viewPuzzleMap()
  331. {
  332. GAME->interface()->showPuzzleMap();
  333. }
  334. void AdventureMapShortcuts::restartGame()
  335. {
  336. GAME->interface()->showYesNoDialog(
  337. LIBRARY->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  338. []()
  339. {
  340. ENGINE->dispatchMainThread(
  341. []()
  342. {
  343. GAME->server().sendRestartGame();
  344. }
  345. );
  346. },
  347. nullptr
  348. );
  349. }
  350. void AdventureMapShortcuts::visitObject()
  351. {
  352. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  353. if(h)
  354. GAME->interface()->cb->moveHero(h, h->pos, false);
  355. }
  356. void AdventureMapShortcuts::openObject()
  357. {
  358. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  359. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  360. if(h)
  361. GAME->interface()->openHeroWindow(h);
  362. if(t)
  363. GAME->interface()->openTownWindow(t);
  364. }
  365. void AdventureMapShortcuts::showMarketplace()
  366. {
  367. //check if we have any marketplace
  368. const CGTownInstance *townWithMarket = nullptr;
  369. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  370. {
  371. if(t->hasBuilt(BuildingID::MARKETPLACE))
  372. {
  373. townWithMarket = t;
  374. break;
  375. }
  376. }
  377. if(townWithMarket) //if any town has marketplace, open window
  378. ENGINE->windows().createAndPushWindow<CMarketWindow>(townWithMarket, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  379. else //if not - complain
  380. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  381. }
  382. void AdventureMapShortcuts::firstTown()
  383. {
  384. if (!GAME->interface()->localState->getOwnedTowns().empty())
  385. {
  386. const auto * town = GAME->interface()->localState->getOwnedTown(0);
  387. GAME->interface()->localState->setSelection(town);
  388. owner.centerOnObject(town);
  389. }
  390. }
  391. void AdventureMapShortcuts::nextTown()
  392. {
  393. owner.hotkeyNextTown();
  394. }
  395. void AdventureMapShortcuts::zoom( int distance)
  396. {
  397. owner.hotkeyZoom(distance, false);
  398. }
  399. void AdventureMapShortcuts::search(bool next)
  400. {
  401. // get all relevant objects
  402. std::vector<ObjectInstanceID> visitableObjInstances;
  403. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  404. if(obj->ID != MapObjectID::MONSTER && obj->ID != MapObjectID::HERO && obj->ID != MapObjectID::TOWN)
  405. visitableObjInstances.push_back(obj->id);
  406. // count of elements for each group (map is already sorted)
  407. std::map<std::string, int> mapObjCount;
  408. for(auto & obj : visitableObjInstances)
  409. mapObjCount[{ GAME->interface()->cb->getObjInstance(obj)->getObjectName() }]++;
  410. // convert to vector for indexed access
  411. std::vector<std::pair<std::string, int>> textCountList;
  412. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  413. textCountList.push_back(*itr);
  414. // get pos of last selection
  415. int lastSel = 0;
  416. for(int i = 0; i < textCountList.size(); i++)
  417. if(textCountList[i].first == searchLast)
  418. lastSel = i;
  419. // create texts
  420. std::vector<std::string> texts;
  421. for(auto & obj : textCountList)
  422. texts.push_back(obj.first + " (" + std::to_string(obj.second) + ")");
  423. // function to center element from list on map
  424. auto selectObjOnMap = [this, textCountList, visitableObjInstances](int index)
  425. {
  426. auto selObj = textCountList[index].first;
  427. // filter for matching objects
  428. std::vector<ObjectInstanceID> selVisitableObjInstances;
  429. for(auto & obj : visitableObjInstances)
  430. if(selObj == GAME->interface()->cb->getObjInstance(obj)->getObjectName())
  431. selVisitableObjInstances.push_back(obj);
  432. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj)
  433. searchPos++;
  434. else
  435. searchPos = 0;
  436. auto objInst = GAME->interface()->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  437. owner.centerOnObject(objInst);
  438. searchLast = objInst->getObjectName();
  439. };
  440. if(next)
  441. selectObjOnMap(lastSel);
  442. else
  443. ENGINE->windows().createAndPushWindow<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("vcmi.adventureMap.search.hover"), LIBRARY->generaltexth->translate("vcmi.adventureMap.search.help"), [selectObjOnMap](int index){ selectObjOnMap(index); }, lastSel, std::vector<std::shared_ptr<IImage>>(), true);
  444. }
  445. void AdventureMapShortcuts::nextObject()
  446. {
  447. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  448. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  449. if(h)
  450. nextHero();
  451. if(t)
  452. nextTown();
  453. }
  454. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  455. {
  456. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero(); //selected hero
  457. if(!h)
  458. return;
  459. if (GAME->map().hasOngoingAnimations())
  460. return;
  461. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  462. if (!GAME->map().isInMap((dst)))
  463. return;
  464. if ( !GAME->interface()->localState->setPath(h, dst))
  465. return;
  466. const CGPath & path = GAME->interface()->localState->getPath(h);
  467. if (path.nodes.size() > 2)
  468. owner.onHeroChanged(h);
  469. else
  470. if(path.nodes[0].turns == 0)
  471. GAME->interface()->moveHero(h, path);
  472. }
  473. bool AdventureMapShortcuts::optionCanViewQuests()
  474. {
  475. return optionInMapView() && !GAME->map().getMap()->quests.empty();
  476. }
  477. bool AdventureMapShortcuts::optionCanToggleLevel()
  478. {
  479. return optionSidePanelActive() && GAME->interface()->cb->getMapSize().z > 1;
  480. }
  481. bool AdventureMapShortcuts::optionMapLevelSurface()
  482. {
  483. return mapLevel == 0;
  484. }
  485. bool AdventureMapShortcuts::optionHeroSleeping()
  486. {
  487. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  488. return optionInMapView() && hero && GAME->interface()->localState->isHeroSleeping(hero);
  489. }
  490. bool AdventureMapShortcuts::optionHeroAwake()
  491. {
  492. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  493. return optionInMapView() && hero && !GAME->interface()->localState->isHeroSleeping(hero);
  494. }
  495. bool AdventureMapShortcuts::optionCanVisitObject()
  496. {
  497. if (!optionHeroSelected())
  498. return false;
  499. auto * hero = GAME->interface()->localState->getCurrentHero();
  500. auto objects = GAME->interface()->cb->getVisitableObjs(hero->visitablePos());
  501. return objects.size() > 1; // there is object other than our hero
  502. }
  503. bool AdventureMapShortcuts::optionHeroSelected()
  504. {
  505. return optionInMapView() && GAME->interface()->localState->getCurrentHero() != nullptr;
  506. }
  507. bool AdventureMapShortcuts::optionHeroCanMove()
  508. {
  509. const auto * hero = GAME->interface()->localState->getCurrentHero();
  510. return optionInMapView() && hero && GAME->interface()->localState->hasPath(hero) && GAME->interface()->localState->getPath(hero).nextNode().turns == 0;
  511. }
  512. bool AdventureMapShortcuts::optionHasNextHero()
  513. {
  514. const auto * hero = GAME->interface()->localState->getCurrentHero();
  515. const auto * nextSuitableHero = GAME->interface()->localState->getNextWanderingHero(hero);
  516. return optionInMapView() && nextSuitableHero != nullptr;
  517. }
  518. bool AdventureMapShortcuts::optionCanEndTurn()
  519. {
  520. return optionInMapView() && GAME->interface()->makingTurn;
  521. }
  522. bool AdventureMapShortcuts::optionSpellcasting()
  523. {
  524. return state == EAdventureState::CASTING_SPELL;
  525. }
  526. bool AdventureMapShortcuts::optionInMapView()
  527. {
  528. return state == EAdventureState::MAKING_TURN;
  529. }
  530. bool AdventureMapShortcuts::optionInWorldView()
  531. {
  532. return state == EAdventureState::WORLD_VIEW;
  533. }
  534. bool AdventureMapShortcuts::optionSidePanelActive()
  535. {
  536. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  537. }
  538. bool AdventureMapShortcuts::optionMapScrollingActive()
  539. {
  540. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  541. }
  542. bool AdventureMapShortcuts::optionMapViewActive()
  543. {
  544. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  545. }