AdventureMapShortcuts.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 "../../lib/CConfigHandler.h"
  31. #include "../../lib/CPlayerState.h"
  32. #include "../../lib/callback/CCallback.h"
  33. #include "../../lib/texts/CGeneralTextHandler.h"
  34. #include "../../lib/mapObjects/CGHeroInstance.h"
  35. #include "../../lib/mapObjects/CGTownInstance.h"
  36. #include "../../lib/mapObjects/MiscObjects.h"
  37. #include "../../lib/mapping/CMap.h"
  38. #include "../../lib/pathfinder/CGPathNode.h"
  39. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  40. AdventureMapShortcuts::AdventureMapShortcuts(AdventureMapInterface & owner)
  41. : owner(owner)
  42. , state(EAdventureState::NOT_INITIALIZED)
  43. , mapLevel(0)
  44. , searchLast("")
  45. , searchPos(0)
  46. {}
  47. void AdventureMapShortcuts::setState(EAdventureState newState)
  48. {
  49. state = newState;
  50. }
  51. EAdventureState AdventureMapShortcuts::getState() const
  52. {
  53. return state;
  54. }
  55. void AdventureMapShortcuts::onMapViewMoved(const Rect & visibleArea, int newMapLevel)
  56. {
  57. mapLevel = newMapLevel;
  58. }
  59. std::vector<AdventureMapShortcutState> AdventureMapShortcuts::getShortcuts()
  60. {
  61. std::vector<AdventureMapShortcutState> result = {
  62. { EShortcut::ADVENTURE_KINGDOM_OVERVIEW, optionInMapView(), [this]() { this->showOverview(); } },
  63. { EShortcut::ADVENTURE_EXIT_WORLD_VIEW, optionInWorldView(), [this]() { this->worldViewBack(); } },
  64. { EShortcut::ADVENTURE_VIEW_WORLD, optionInMapView(), [this]() { this->worldViewScale1x(); } },
  65. { EShortcut::ADVENTURE_VIEW_WORLD_X1, optionInWorldView(), [this]() { this->worldViewScale1x(); } },
  66. { EShortcut::ADVENTURE_VIEW_WORLD_X2, optionInWorldView(), [this]() { this->worldViewScale2x(); } },
  67. { EShortcut::ADVENTURE_VIEW_WORLD_X4, optionInWorldView(), [this]() { this->worldViewScale4x(); } },
  68. { EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL, optionCanToggleLevel(), [this]() { this->switchMapLevel(); } },
  69. { EShortcut::ADVENTURE_QUEST_LOG, optionCanViewQuests(), [this]() { this->showQuestlog(); } },
  70. { EShortcut::ADVENTURE_TOGGLE_SLEEP, optionHeroSelected(), [this]() { this->toggleSleepWake(); } },
  71. { EShortcut::ADVENTURE_TOGGLE_GRID, optionInMapView(), [this]() { this->toggleGrid(); } },
  72. { EShortcut::ADVENTURE_TOGGLE_VISITABLE, optionInMapView(), [this]() { this->toggleVisitable(); } },
  73. { EShortcut::ADVENTURE_TOGGLE_BLOCKED, optionInMapView(), [this]() { this->toggleBlocked(); } },
  74. { EShortcut::ADVENTURE_TRACK_HERO, optionInMapView(), [this]() { this->toggleTrackHero(); } },
  75. { EShortcut::ADVENTURE_SET_HERO_ASLEEP, optionHeroAwake(), [this]() { this->setHeroSleeping(); } },
  76. { EShortcut::ADVENTURE_SET_HERO_AWAKE, optionHeroSleeping(), [this]() { this->setHeroAwake(); } },
  77. { EShortcut::ADVENTURE_MOVE_HERO, optionHeroCanMove(), [this]() { this->moveHeroAlongPath(); } },
  78. { EShortcut::ADVENTURE_CAST_SPELL, optionHeroSelected(), [this]() { this->showSpellbook(); } },
  79. { EShortcut::ADVENTURE_GAME_OPTIONS, optionInMapView(), [this]() { this->adventureOptions(); } },
  80. { EShortcut::GLOBAL_OPTIONS, optionInMapView(), [this]() { this->systemOptions(); } },
  81. { EShortcut::ADVENTURE_FIRST_HERO, optionInMapView(), [this]() { this->firstHero(); } },
  82. { EShortcut::ADVENTURE_NEXT_HERO, optionHasNextHero(), [this]() { this->nextHero(); } },
  83. { EShortcut::ADVENTURE_END_TURN, optionCanEndTurn(), [this]() { this->endTurn(); } },
  84. { EShortcut::ADVENTURE_THIEVES_GUILD, optionInMapView(), [this]() { this->showThievesGuild(); } },
  85. { EShortcut::ADVENTURE_VIEW_SCENARIO, optionInMapView(), [this]() { this->showScenarioInfo(); } },
  86. { EShortcut::ADVENTURE_QUIT_GAME, optionInMapView(), [this]() { this->quitGame(); } },
  87. { EShortcut::ADVENTURE_TO_MAIN_MENU, optionInMapView(), [this]() { this->toMainMenu(); } },
  88. { EShortcut::ADVENTURE_SAVE_GAME, optionInMapView(), [this]() { this->saveGame(); } },
  89. { EShortcut::ADVENTURE_NEW_GAME, optionInMapView(), [this]() { this->newGame(); } },
  90. { EShortcut::ADVENTURE_LOAD_GAME, optionInMapView(), [this]() { this->loadGame(); } },
  91. { EShortcut::ADVENTURE_RESTART_GAME, optionInMapView(), [this]() { this->restartGame(); } },
  92. { EShortcut::ADVENTURE_DIG_GRAIL, optionHeroDig(), [this]() { this->digGrail(); } },
  93. { EShortcut::ADVENTURE_VIEW_PUZZLE, optionSidePanelActive(),[this]() { this->viewPuzzleMap(); } },
  94. { EShortcut::ADVENTURE_VISIT_OBJECT, optionCanVisitObject(), [this]() { this->visitObject(); } },
  95. { EShortcut::ADVENTURE_VIEW_SELECTED, optionInMapView(), [this]() { this->openObject(); } },
  96. { EShortcut::ADVENTURE_MARKETPLACE, optionMarketplace(), [this]() { this->showMarketplace(); } },
  97. { EShortcut::ADVENTURE_ZOOM_IN, optionSidePanelActive(),[this]() { this->zoom(+10); } },
  98. { EShortcut::ADVENTURE_ZOOM_OUT, optionSidePanelActive(),[this]() { this->zoom(-10); } },
  99. { EShortcut::ADVENTURE_ZOOM_RESET, optionSidePanelActive(),[this]() { this->zoom( 0); } },
  100. { EShortcut::ADVENTURE_FIRST_TOWN, optionInMapView(), [this]() { this->firstTown(); } },
  101. { EShortcut::ADVENTURE_NEXT_TOWN, optionInMapView(), [this]() { this->nextTown(); } },
  102. { EShortcut::ADVENTURE_NEXT_OBJECT, optionInMapView(), [this]() { this->nextObject(); } },
  103. { EShortcut::ADVENTURE_MOVE_HERO_SW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, +1}); } },
  104. { EShortcut::ADVENTURE_MOVE_HERO_SS, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, +1}); } },
  105. { EShortcut::ADVENTURE_MOVE_HERO_SE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, +1}); } },
  106. { EShortcut::ADVENTURE_MOVE_HERO_WW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, 0}); } },
  107. { EShortcut::ADVENTURE_MOVE_HERO_EE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, 0}); } },
  108. { EShortcut::ADVENTURE_MOVE_HERO_NW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, -1}); } },
  109. { EShortcut::ADVENTURE_MOVE_HERO_NN, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, -1}); } },
  110. { EShortcut::ADVENTURE_MOVE_HERO_NE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, -1}); } },
  111. { EShortcut::ADVENTURE_SEARCH, optionSidePanelActive(),[this]() { this->search(false); } },
  112. { EShortcut::ADVENTURE_SEARCH_CONTINUE, optionSidePanelActive(),[this]() { this->search(true); } },
  113. { EShortcut::MAIN_MENU_LOBBY, optionSidePanelActive(),[ ]() { ENGINE->user().onGlobalLobbyInterfaceActivated(); } }
  114. };
  115. return result;
  116. }
  117. void AdventureMapShortcuts::showOverview()
  118. {
  119. ENGINE->windows().createAndPushWindow<CKingdomInterface>();
  120. }
  121. void AdventureMapShortcuts::worldViewBack()
  122. {
  123. owner.hotkeyExitWorldView();
  124. auto hero = GAME->interface()->localState->getCurrentHero();
  125. if (hero)
  126. owner.centerOnObject(hero);
  127. }
  128. void AdventureMapShortcuts::worldViewScale1x()
  129. {
  130. // TODO set corresponding scale button to "selected" mode
  131. owner.openWorldView(7);
  132. }
  133. void AdventureMapShortcuts::worldViewScale2x()
  134. {
  135. owner.openWorldView(11);
  136. }
  137. void AdventureMapShortcuts::worldViewScale4x()
  138. {
  139. owner.openWorldView(16);
  140. }
  141. void AdventureMapShortcuts::switchMapLevel()
  142. {
  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 = boost::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. auto getColor = [](MapObjectID id ){
  402. switch (id)
  403. {
  404. case MapObjectID::HERO:
  405. return ColorRGBA{ 0, 192, 0};
  406. case MapObjectID::MONSTER:
  407. return ColorRGBA{ 255, 0, 0};
  408. case MapObjectID::TOWN:
  409. return ColorRGBA{ 100, 100, 255};
  410. case MapObjectID::MINE:
  411. return ColorRGBA{ 255, 153, 204};
  412. case MapObjectID::RESOURCE:
  413. return ColorRGBA{ 255, 51, 255};
  414. case MapObjectID::ARTIFACT:
  415. return ColorRGBA{ 192, 255, 0};
  416. default:
  417. return Colors::WHITE;
  418. }
  419. };
  420. // count of elements for each group (map is already sorted)
  421. std::map<std::pair<std::string, ColorRGBA>, int> mapObjCount;
  422. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  423. mapObjCount[{GAME->interface()->cb->getObjInstance(obj->id)->getObjectName(), getColor(obj->ID)}]++;
  424. // convert to vector for indexed access
  425. std::vector<std::pair<std::pair<std::string, ColorRGBA>, int>> textCountList;
  426. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  427. textCountList.push_back({(*itr).first, (*itr).second});
  428. // get pos of last selection
  429. int lastSel = 0;
  430. for(int i = 0; i < textCountList.size(); i++)
  431. if(textCountList[i].first.first == searchLast)
  432. lastSel = i;
  433. // create texts
  434. std::vector<std::string> texts;
  435. for(auto & obj : textCountList)
  436. texts.push_back("{" + Colors::colorToHexString(obj.first.second) + "|" + obj.first.first + "}" + " (" + std::to_string(obj.second) + ")");
  437. // function to center element from list on map
  438. auto selectObjOnMap = [this, textCountList](int index)
  439. {
  440. auto selObj = textCountList[index].first;
  441. // filter for matching objects
  442. std::vector<ObjectInstanceID> selVisitableObjInstances;
  443. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  444. if(selObj.first == GAME->interface()->cb->getObjInstance(obj->id)->getObjectName())
  445. selVisitableObjInstances.push_back(obj->id);
  446. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj.first)
  447. searchPos++;
  448. else
  449. searchPos = 0;
  450. auto objInst = GAME->interface()->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  451. owner.centerOnObject(objInst);
  452. searchLast = objInst->getObjectName();
  453. };
  454. if(next)
  455. selectObjOnMap(lastSel);
  456. else
  457. 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);
  458. }
  459. void AdventureMapShortcuts::nextObject()
  460. {
  461. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  462. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  463. if(h)
  464. nextHero();
  465. if(t)
  466. nextTown();
  467. }
  468. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  469. {
  470. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero(); //selected hero
  471. if(!h)
  472. return;
  473. if (GAME->map().hasOngoingAnimations())
  474. return;
  475. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  476. if (!GAME->map().isInMap((dst)))
  477. return;
  478. if ( !GAME->interface()->localState->setPath(h, dst))
  479. return;
  480. const CGPath & path = GAME->interface()->localState->getPath(h);
  481. if (path.nodes.size() > 2)
  482. owner.onHeroChanged(h);
  483. else
  484. if(path.nodes[0].turns == 0)
  485. GAME->interface()->moveHero(h, path);
  486. }
  487. bool AdventureMapShortcuts::optionCanViewQuests()
  488. {
  489. return optionInMapView() && !GAME->interface()->cb->getPlayerState(GAME->interface()->playerID)->quests.empty();
  490. }
  491. bool AdventureMapShortcuts::optionCanToggleLevel()
  492. {
  493. return optionSidePanelActive() && GAME->interface()->cb->getMapSize().z > 1;
  494. }
  495. int AdventureMapShortcuts::optionMapLevel()
  496. {
  497. return mapLevel;
  498. }
  499. bool AdventureMapShortcuts::optionHeroSleeping()
  500. {
  501. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  502. return optionInMapView() && hero && GAME->interface()->localState->isHeroSleeping(hero);
  503. }
  504. bool AdventureMapShortcuts::optionHeroAwake()
  505. {
  506. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  507. return optionInMapView() && hero && !GAME->interface()->localState->isHeroSleeping(hero);
  508. }
  509. bool AdventureMapShortcuts::optionCanVisitObject()
  510. {
  511. if (!optionHeroSelected())
  512. return false;
  513. auto * hero = GAME->interface()->localState->getCurrentHero();
  514. auto objects = GAME->interface()->cb->getVisitableObjs(hero->visitablePos());
  515. return objects.size() > 1; // there is object other than our hero
  516. }
  517. bool AdventureMapShortcuts::optionHeroSelected()
  518. {
  519. return optionInMapView() && GAME->interface()->localState->getCurrentHero() != nullptr;
  520. }
  521. bool AdventureMapShortcuts::optionHeroCanMove()
  522. {
  523. const auto * hero = GAME->interface()->localState->getCurrentHero();
  524. return optionInMapView() && hero && GAME->interface()->localState->hasPath(hero) && GAME->interface()->localState->getPath(hero).nextNode().turns == 0;
  525. }
  526. bool AdventureMapShortcuts::optionHasNextHero()
  527. {
  528. const auto * hero = GAME->interface()->localState->getCurrentHero();
  529. const auto * nextSuitableHero = GAME->interface()->localState->getNextWanderingHero(hero);
  530. return optionInMapView() && nextSuitableHero != nullptr;
  531. }
  532. bool AdventureMapShortcuts::optionCanEndTurn()
  533. {
  534. return optionInMapView() && GAME->interface()->makingTurn;
  535. }
  536. bool AdventureMapShortcuts::optionSpellcasting()
  537. {
  538. return state == EAdventureState::CASTING_SPELL;
  539. }
  540. bool AdventureMapShortcuts::optionInMapView()
  541. {
  542. return state == EAdventureState::MAKING_TURN;
  543. }
  544. bool AdventureMapShortcuts::optionInWorldView()
  545. {
  546. return state == EAdventureState::WORLD_VIEW;
  547. }
  548. bool AdventureMapShortcuts::optionSidePanelActive()
  549. {
  550. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  551. }
  552. bool AdventureMapShortcuts::optionMapScrollingActive()
  553. {
  554. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  555. }
  556. bool AdventureMapShortcuts::optionMapViewActive()
  557. {
  558. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  559. }
  560. bool AdventureMapShortcuts::optionMarketplace()
  561. {
  562. if(state != EAdventureState::MAKING_TURN)
  563. return false;
  564. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  565. if(t->hasBuilt(BuildingID::MARKETPLACE))
  566. return true;
  567. return false;
  568. }
  569. bool AdventureMapShortcuts::optionHeroBoat(EPathfindingLayer layer)
  570. {
  571. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  572. return optionInMapView() && hero && hero->inBoat() && hero->getBoat()->layer == layer;
  573. }
  574. bool AdventureMapShortcuts::optionHeroDig()
  575. {
  576. auto hero = GAME->interface()->localState->getCurrentHero();
  577. return optionInMapView() && hero && hero->diggingStatus() == EDiggingStatus::CAN_DIG;
  578. }