AdventureMapShortcuts.cpp 19 KB

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