AdventureMapShortcuts.cpp 21 KB

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