AdventureMapShortcuts.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 "../gui/Shortcut.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../lobby/CSavingScreen.h"
  20. #include "../mapView/mapHandler.h"
  21. #include "../windows/CKingdomInterface.h"
  22. #include "../windows/CSpellWindow.h"
  23. #include "../windows/CMarketWindow.h"
  24. #include "../windows/GUIClasses.h"
  25. #include "../windows/settings/SettingsMainWindow.h"
  26. #include "AdventureMapInterface.h"
  27. #include "AdventureOptions.h"
  28. #include "AdventureState.h"
  29. #include "../../CCallback.h"
  30. #include "../../lib/CConfigHandler.h"
  31. #include "../../lib/texts/CGeneralTextHandler.h"
  32. #include "../../lib/mapObjects/CGHeroInstance.h"
  33. #include "../../lib/mapObjects/CGTownInstance.h"
  34. #include "../../lib/mapping/CMap.h"
  35. #include "../../lib/pathfinder/CGPathNode.h"
  36. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  37. AdventureMapShortcuts::AdventureMapShortcuts(AdventureMapInterface & owner)
  38. : owner(owner)
  39. , state(EAdventureState::NOT_INITIALIZED)
  40. , mapLevel(0)
  41. , searchLast("")
  42. , searchPos(0)
  43. {}
  44. void AdventureMapShortcuts::setState(EAdventureState newState)
  45. {
  46. state = newState;
  47. }
  48. EAdventureState AdventureMapShortcuts::getState() const
  49. {
  50. return state;
  51. }
  52. void AdventureMapShortcuts::onMapViewMoved(const Rect & visibleArea, int newMapLevel)
  53. {
  54. mapLevel = newMapLevel;
  55. }
  56. std::vector<AdventureMapShortcutState> AdventureMapShortcuts::getShortcuts()
  57. {
  58. std::vector<AdventureMapShortcutState> result = {
  59. { EShortcut::ADVENTURE_KINGDOM_OVERVIEW, optionInMapView(), [this]() { this->showOverview(); } },
  60. { EShortcut::ADVENTURE_EXIT_WORLD_VIEW, optionInWorldView(), [this]() { this->worldViewBack(); } },
  61. { EShortcut::ADVENTURE_VIEW_WORLD, optionInMapView(), [this]() { this->worldViewScale1x(); } },
  62. { EShortcut::ADVENTURE_VIEW_WORLD_X1, optionInWorldView(), [this]() { this->worldViewScale1x(); } },
  63. { EShortcut::ADVENTURE_VIEW_WORLD_X2, optionInWorldView(), [this]() { this->worldViewScale2x(); } },
  64. { EShortcut::ADVENTURE_VIEW_WORLD_X4, optionInWorldView(), [this]() { this->worldViewScale4x(); } },
  65. { EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL, optionCanToggleLevel(), [this]() { this->switchMapLevel(); } },
  66. { EShortcut::ADVENTURE_QUEST_LOG, optionCanViewQuests(), [this]() { this->showQuestlog(); } },
  67. { EShortcut::ADVENTURE_TOGGLE_SLEEP, optionHeroSelected(), [this]() { this->toggleSleepWake(); } },
  68. { EShortcut::ADVENTURE_TOGGLE_GRID, optionInMapView(), [this]() { this->toggleGrid(); } },
  69. { EShortcut::ADVENTURE_TOGGLE_VISITABLE, optionInMapView(), [this]() { this->toggleVisitable(); } },
  70. { EShortcut::ADVENTURE_TOGGLE_BLOCKED, optionInMapView(), [this]() { this->toggleBlocked(); } },
  71. { EShortcut::ADVENTURE_TRACK_HERO, optionInMapView(), [this]() { this->toggleTrackHero(); } },
  72. { EShortcut::ADVENTURE_SET_HERO_ASLEEP, optionHeroAwake(), [this]() { this->setHeroSleeping(); } },
  73. { EShortcut::ADVENTURE_SET_HERO_AWAKE, optionHeroSleeping(), [this]() { this->setHeroAwake(); } },
  74. { EShortcut::ADVENTURE_MOVE_HERO, optionHeroCanMove(), [this]() { this->moveHeroAlongPath(); } },
  75. { EShortcut::ADVENTURE_CAST_SPELL, optionHeroSelected(), [this]() { this->showSpellbook(); } },
  76. { EShortcut::ADVENTURE_GAME_OPTIONS, optionInMapView(), [this]() { this->adventureOptions(); } },
  77. { EShortcut::GLOBAL_OPTIONS, optionInMapView(), [this]() { this->systemOptions(); } },
  78. { EShortcut::ADVENTURE_FIRST_HERO, optionInMapView(), [this]() { this->firstHero(); } },
  79. { EShortcut::ADVENTURE_NEXT_HERO, optionHasNextHero(), [this]() { this->nextHero(); } },
  80. { EShortcut::ADVENTURE_END_TURN, optionCanEndTurn(), [this]() { this->endTurn(); } },
  81. { EShortcut::ADVENTURE_THIEVES_GUILD, optionInMapView(), [this]() { this->showThievesGuild(); } },
  82. { EShortcut::ADVENTURE_VIEW_SCENARIO, optionInMapView(), [this]() { this->showScenarioInfo(); } },
  83. { EShortcut::ADVENTURE_QUIT_GAME, optionInMapView(), [this]() { this->quitGame(); } },
  84. { EShortcut::ADVENTURE_TO_MAIN_MENU, optionInMapView(), [this]() { this->toMainMenu(); } },
  85. { EShortcut::ADVENTURE_SAVE_GAME, optionInMapView(), [this]() { this->saveGame(); } },
  86. { EShortcut::ADVENTURE_NEW_GAME, optionInMapView(), [this]() { this->newGame(); } },
  87. { EShortcut::ADVENTURE_LOAD_GAME, optionInMapView(), [this]() { this->loadGame(); } },
  88. { EShortcut::ADVENTURE_RESTART_GAME, optionInMapView(), [this]() { this->restartGame(); } },
  89. { EShortcut::ADVENTURE_DIG_GRAIL, optionHeroSelected(), [this]() { this->digGrail(); } },
  90. { EShortcut::ADVENTURE_VIEW_PUZZLE, optionSidePanelActive(),[this]() { this->viewPuzzleMap(); } },
  91. { EShortcut::ADVENTURE_VISIT_OBJECT, optionCanVisitObject(), [this]() { this->visitObject(); } },
  92. { EShortcut::ADVENTURE_VIEW_SELECTED, optionInMapView(), [this]() { this->openObject(); } },
  93. { EShortcut::ADVENTURE_MARKETPLACE, optionInMapView(), [this]() { this->showMarketplace(); } },
  94. { EShortcut::ADVENTURE_ZOOM_IN, optionSidePanelActive(),[this]() { this->zoom(+10); } },
  95. { EShortcut::ADVENTURE_ZOOM_OUT, optionSidePanelActive(),[this]() { this->zoom(-10); } },
  96. { EShortcut::ADVENTURE_ZOOM_RESET, optionSidePanelActive(),[this]() { this->zoom( 0); } },
  97. { EShortcut::ADVENTURE_FIRST_TOWN, optionInMapView(), [this]() { this->firstTown(); } },
  98. { EShortcut::ADVENTURE_NEXT_TOWN, optionInMapView(), [this]() { this->nextTown(); } },
  99. { EShortcut::ADVENTURE_NEXT_OBJECT, optionInMapView(), [this]() { this->nextObject(); } },
  100. { EShortcut::ADVENTURE_MOVE_HERO_SW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, +1}); } },
  101. { EShortcut::ADVENTURE_MOVE_HERO_SS, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, +1}); } },
  102. { EShortcut::ADVENTURE_MOVE_HERO_SE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, +1}); } },
  103. { EShortcut::ADVENTURE_MOVE_HERO_WW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, 0}); } },
  104. { EShortcut::ADVENTURE_MOVE_HERO_EE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, 0}); } },
  105. { EShortcut::ADVENTURE_MOVE_HERO_NW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, -1}); } },
  106. { EShortcut::ADVENTURE_MOVE_HERO_NN, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, -1}); } },
  107. { EShortcut::ADVENTURE_MOVE_HERO_NE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, -1}); } },
  108. { EShortcut::ADVENTURE_SEARCH, optionSidePanelActive(),[this]() { this->search(false); } },
  109. { EShortcut::ADVENTURE_SEARCH_CONTINUE, optionSidePanelActive(),[this]() { this->search(true); } }
  110. };
  111. return result;
  112. }
  113. void AdventureMapShortcuts::showOverview()
  114. {
  115. ENGINE->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. ENGINE->windows().createAndPushWindow<CSpellWindow>(LOCPLINT->localState->getCurrentHero(), LOCPLINT, false);
  209. }
  210. void AdventureMapShortcuts::adventureOptions()
  211. {
  212. ENGINE->windows().createAndPushWindow<AdventureOptions>();
  213. }
  214. void AdventureMapShortcuts::systemOptions()
  215. {
  216. ENGINE->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( VLC->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( VLC->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(VLC->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  278. }
  279. void AdventureMapShortcuts::showScenarioInfo()
  280. {
  281. AdventureOptions::showScenarioInfo();
  282. }
  283. void AdventureMapShortcuts::toMainMenu()
  284. {
  285. LOCPLINT->showYesNoDialog(
  286. VLC->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. VLC->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. VLC->generaltexth->allTexts[578],
  311. []()
  312. {
  313. ENGINE->dispatchMainThread( []()
  314. {
  315. handleQuit(false);
  316. });
  317. },
  318. 0
  319. );
  320. }
  321. void AdventureMapShortcuts::saveGame()
  322. {
  323. ENGINE->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. VLC->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  343. []()
  344. {
  345. ENGINE->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. ENGINE->windows().createAndPushWindow<CMarketWindow>(townWithMarket, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  384. else //if not - complain
  385. LOCPLINT->showInfoDialog(VLC->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(bool next)
  405. {
  406. // get all relevant objects
  407. std::vector<ObjectInstanceID> visitableObjInstances;
  408. for(auto & obj : LOCPLINT->cb->getAllVisitableObjs())
  409. if(obj->ID != MapObjectID::MONSTER && obj->ID != MapObjectID::HERO && obj->ID != MapObjectID::TOWN)
  410. visitableObjInstances.push_back(obj->id);
  411. // count of elements for each group (map is already sorted)
  412. std::map<std::string, int> mapObjCount;
  413. for(auto & obj : visitableObjInstances)
  414. mapObjCount[{ LOCPLINT->cb->getObjInstance(obj)->getObjectName() }]++;
  415. // convert to vector for indexed access
  416. std::vector<std::pair<std::string, int>> textCountList;
  417. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  418. textCountList.push_back(*itr);
  419. // get pos of last selection
  420. int lastSel = 0;
  421. for(int i = 0; i < textCountList.size(); i++)
  422. if(textCountList[i].first == searchLast)
  423. lastSel = i;
  424. // create texts
  425. std::vector<std::string> texts;
  426. for(auto & obj : textCountList)
  427. texts.push_back(obj.first + " (" + std::to_string(obj.second) + ")");
  428. // function to center element from list on map
  429. auto selectObjOnMap = [this, textCountList, visitableObjInstances](int index)
  430. {
  431. auto selObj = textCountList[index].first;
  432. // filter for matching objects
  433. std::vector<ObjectInstanceID> selVisitableObjInstances;
  434. for(auto & obj : visitableObjInstances)
  435. if(selObj == LOCPLINT->cb->getObjInstance(obj)->getObjectName())
  436. selVisitableObjInstances.push_back(obj);
  437. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj)
  438. searchPos++;
  439. else
  440. searchPos = 0;
  441. auto objInst = LOCPLINT->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  442. owner.centerOnObject(objInst);
  443. searchLast = objInst->getObjectName();
  444. };
  445. if(next)
  446. selectObjOnMap(lastSel);
  447. else
  448. 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);
  449. }
  450. void AdventureMapShortcuts::nextObject()
  451. {
  452. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  453. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown();
  454. if(h)
  455. nextHero();
  456. if(t)
  457. nextTown();
  458. }
  459. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  460. {
  461. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  462. if(!h)
  463. return;
  464. if (MAPHANDLER->hasOngoingAnimations())
  465. return;
  466. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  467. if (!MAPHANDLER->isInMap((dst)))
  468. return;
  469. if ( !LOCPLINT->localState->setPath(h, dst))
  470. return;
  471. const CGPath & path = LOCPLINT->localState->getPath(h);
  472. if (path.nodes.size() > 2)
  473. owner.onHeroChanged(h);
  474. else
  475. if(path.nodes[0].turns == 0)
  476. LOCPLINT->moveHero(h, path);
  477. }
  478. bool AdventureMapShortcuts::optionCanViewQuests()
  479. {
  480. return optionInMapView() && !MAPHANDLER->getMap()->quests.empty();
  481. }
  482. bool AdventureMapShortcuts::optionCanToggleLevel()
  483. {
  484. return optionSidePanelActive() && LOCPLINT->cb->getMapSize().z > 1;
  485. }
  486. bool AdventureMapShortcuts::optionMapLevelSurface()
  487. {
  488. return mapLevel == 0;
  489. }
  490. bool AdventureMapShortcuts::optionHeroSleeping()
  491. {
  492. const CGHeroInstance *hero = LOCPLINT->localState->getCurrentHero();
  493. return optionInMapView() && hero && LOCPLINT->localState->isHeroSleeping(hero);
  494. }
  495. bool AdventureMapShortcuts::optionHeroAwake()
  496. {
  497. const CGHeroInstance *hero = LOCPLINT->localState->getCurrentHero();
  498. return optionInMapView() && hero && !LOCPLINT->localState->isHeroSleeping(hero);
  499. }
  500. bool AdventureMapShortcuts::optionCanVisitObject()
  501. {
  502. if (!optionHeroSelected())
  503. return false;
  504. auto * hero = LOCPLINT->localState->getCurrentHero();
  505. auto objects = LOCPLINT->cb->getVisitableObjs(hero->visitablePos());
  506. return objects.size() > 1; // there is object other than our hero
  507. }
  508. bool AdventureMapShortcuts::optionHeroSelected()
  509. {
  510. return optionInMapView() && LOCPLINT->localState->getCurrentHero() != nullptr;
  511. }
  512. bool AdventureMapShortcuts::optionHeroCanMove()
  513. {
  514. const auto * hero = LOCPLINT->localState->getCurrentHero();
  515. return optionInMapView() && hero && LOCPLINT->localState->hasPath(hero) && LOCPLINT->localState->getPath(hero).nextNode().turns == 0;
  516. }
  517. bool AdventureMapShortcuts::optionHasNextHero()
  518. {
  519. const auto * hero = LOCPLINT->localState->getCurrentHero();
  520. const auto * nextSuitableHero = LOCPLINT->localState->getNextWanderingHero(hero);
  521. return optionInMapView() && nextSuitableHero != nullptr;
  522. }
  523. bool AdventureMapShortcuts::optionCanEndTurn()
  524. {
  525. return optionInMapView() && LOCPLINT->makingTurn;
  526. }
  527. bool AdventureMapShortcuts::optionSpellcasting()
  528. {
  529. return state == EAdventureState::CASTING_SPELL;
  530. }
  531. bool AdventureMapShortcuts::optionInMapView()
  532. {
  533. return state == EAdventureState::MAKING_TURN;
  534. }
  535. bool AdventureMapShortcuts::optionInWorldView()
  536. {
  537. return state == EAdventureState::WORLD_VIEW;
  538. }
  539. bool AdventureMapShortcuts::optionSidePanelActive()
  540. {
  541. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  542. }
  543. bool AdventureMapShortcuts::optionMapScrollingActive()
  544. {
  545. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  546. }
  547. bool AdventureMapShortcuts::optionMapViewActive()
  548. {
  549. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  550. }