AdventureMapShortcuts.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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/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. owner.hotkeySwitchMapLevel();
  142. }
  143. void AdventureMapShortcuts::showQuestlog()
  144. {
  145. GAME->interface()->showQuestLog();
  146. }
  147. void AdventureMapShortcuts::toggleTrackHero()
  148. {
  149. Settings s = settings.write["session"];
  150. s["adventureTrackHero"].Bool() = !settings["session"]["adventureTrackHero"].Bool();
  151. }
  152. void AdventureMapShortcuts::toggleGrid()
  153. {
  154. Settings s = settings.write["gameTweaks"];
  155. s["showGrid"].Bool() = !settings["gameTweaks"]["showGrid"].Bool();
  156. }
  157. void AdventureMapShortcuts::toggleVisitable()
  158. {
  159. Settings s = settings.write["session"];
  160. s["showVisitable"].Bool() = !settings["session"]["showVisitable"].Bool();
  161. }
  162. void AdventureMapShortcuts::toggleBlocked()
  163. {
  164. Settings s = settings.write["session"];
  165. s["showBlocked"].Bool() = !settings["session"]["showBlocked"].Bool();
  166. }
  167. void AdventureMapShortcuts::toggleSleepWake()
  168. {
  169. if (!optionHeroSelected())
  170. return;
  171. if (optionHeroAwake())
  172. setHeroSleeping();
  173. else
  174. setHeroAwake();
  175. }
  176. void AdventureMapShortcuts::setHeroSleeping()
  177. {
  178. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  179. if (h)
  180. {
  181. GAME->interface()->localState->setHeroAsleep(h);
  182. owner.onHeroChanged(h);
  183. nextHero();
  184. }
  185. }
  186. void AdventureMapShortcuts::setHeroAwake()
  187. {
  188. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  189. if (h)
  190. {
  191. GAME->interface()->localState->setHeroAwaken(h);
  192. owner.onHeroChanged(h);
  193. }
  194. }
  195. void AdventureMapShortcuts::moveHeroAlongPath()
  196. {
  197. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  198. if (!h || !GAME->interface()->localState->hasPath(h))
  199. return;
  200. GAME->interface()->moveHero(h, GAME->interface()->localState->getPath(h));
  201. }
  202. void AdventureMapShortcuts::showSpellbook()
  203. {
  204. if (!GAME->interface()->localState->getCurrentHero())
  205. return;
  206. owner.centerOnObject(GAME->interface()->localState->getCurrentHero());
  207. ENGINE->windows().createAndPushWindow<CSpellWindow>(GAME->interface()->localState->getCurrentHero(), GAME->interface(), false);
  208. }
  209. void AdventureMapShortcuts::adventureOptions()
  210. {
  211. ENGINE->windows().createAndPushWindow<AdventureOptions>();
  212. }
  213. void AdventureMapShortcuts::systemOptions()
  214. {
  215. ENGINE->windows().createAndPushWindow<SettingsMainWindow>();
  216. }
  217. void AdventureMapShortcuts::firstHero()
  218. {
  219. if (!GAME->interface()->localState->getWanderingHeroes().empty())
  220. {
  221. const auto * hero = GAME->interface()->localState->getWanderingHero(0);
  222. GAME->interface()->localState->setSelection(hero);
  223. owner.centerOnObject(hero);
  224. }
  225. }
  226. void AdventureMapShortcuts::nextHero()
  227. {
  228. const auto * currHero = GAME->interface()->localState->getCurrentHero();
  229. const auto * nextHero = GAME->interface()->localState->getNextWanderingHero(currHero);
  230. if (nextHero)
  231. {
  232. GAME->interface()->localState->setSelection(nextHero);
  233. owner.centerOnObject(nextHero);
  234. }
  235. }
  236. void AdventureMapShortcuts::endTurn()
  237. {
  238. if(!GAME->interface()->makingTurn)
  239. return;
  240. if(settings["adventure"]["heroReminder"].Bool())
  241. {
  242. for(auto hero : GAME->interface()->localState->getWanderingHeroes())
  243. {
  244. if(!GAME->interface()->localState->isHeroSleeping(hero) && hero->movementPointsRemaining() > 0)
  245. {
  246. // Only show hero reminder if conditions met:
  247. // - There still movement points
  248. // - Hero don't have a path or there not points for first step on path
  249. GAME->interface()->localState->verifyPath(hero);
  250. if(!GAME->interface()->localState->hasPath(hero))
  251. {
  252. GAME->interface()->showYesNoDialog( LIBRARY->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  253. return;
  254. }
  255. auto path = GAME->interface()->localState->getPath(hero);
  256. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  257. {
  258. GAME->interface()->showYesNoDialog( LIBRARY->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  259. return;
  260. }
  261. }
  262. }
  263. }
  264. owner.hotkeyEndingTurn();
  265. }
  266. void AdventureMapShortcuts::showThievesGuild()
  267. {
  268. //find first town with tavern
  269. auto itr = boost::range::find_if(GAME->interface()->localState->getOwnedTowns(), [](const CGTownInstance * town)
  270. {
  271. return town->hasBuilt(BuildingID::TAVERN);
  272. });
  273. if(itr != GAME->interface()->localState->getOwnedTowns().end())
  274. GAME->interface()->showThievesGuildWindow(*itr);
  275. else
  276. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  277. }
  278. void AdventureMapShortcuts::showScenarioInfo()
  279. {
  280. AdventureOptions::showScenarioInfo();
  281. }
  282. void AdventureMapShortcuts::toMainMenu()
  283. {
  284. GAME->interface()->showYesNoDialog(
  285. LIBRARY->generaltexth->allTexts[578],
  286. []()
  287. {
  288. GAME->server().endGameplay();
  289. GAME->mainmenu()->menu->switchToTab("main");
  290. },
  291. 0
  292. );
  293. }
  294. void AdventureMapShortcuts::newGame()
  295. {
  296. GAME->interface()->showYesNoDialog(
  297. LIBRARY->generaltexth->allTexts[578],
  298. []()
  299. {
  300. GAME->server().endGameplay();
  301. GAME->mainmenu()->menu->switchToTab("new");
  302. },
  303. nullptr
  304. );
  305. }
  306. void AdventureMapShortcuts::quitGame()
  307. {
  308. GAME->interface()->showYesNoDialog(
  309. LIBRARY->generaltexth->allTexts[578],
  310. [](){ GAME->onShutdownRequested(false);},
  311. nullptr
  312. );
  313. }
  314. void AdventureMapShortcuts::saveGame()
  315. {
  316. ENGINE->windows().createAndPushWindow<CSavingScreen>();
  317. }
  318. void AdventureMapShortcuts::loadGame()
  319. {
  320. GAME->interface()->proposeLoadingGame();
  321. }
  322. void AdventureMapShortcuts::digGrail()
  323. {
  324. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  325. if(h && GAME->interface()->makingTurn)
  326. GAME->interface()->tryDigging(h);
  327. }
  328. void AdventureMapShortcuts::viewPuzzleMap()
  329. {
  330. GAME->interface()->showPuzzleMap();
  331. }
  332. void AdventureMapShortcuts::restartGame()
  333. {
  334. GAME->interface()->showYesNoDialog(
  335. LIBRARY->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  336. []()
  337. {
  338. ENGINE->dispatchMainThread(
  339. []()
  340. {
  341. GAME->server().sendRestartGame();
  342. }
  343. );
  344. },
  345. nullptr
  346. );
  347. }
  348. void AdventureMapShortcuts::visitObject()
  349. {
  350. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  351. if(h)
  352. GAME->interface()->cb->moveHero(h, h->pos, false);
  353. }
  354. void AdventureMapShortcuts::openObject()
  355. {
  356. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  357. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  358. if(h)
  359. GAME->interface()->openHeroWindow(h);
  360. if(t)
  361. GAME->interface()->openTownWindow(t);
  362. }
  363. void AdventureMapShortcuts::showMarketplace()
  364. {
  365. //check if we have any marketplace
  366. const CGTownInstance *townWithMarket = nullptr;
  367. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  368. {
  369. if(t->hasBuilt(BuildingID::MARKETPLACE))
  370. {
  371. townWithMarket = t;
  372. break;
  373. }
  374. }
  375. if(townWithMarket) //if any town has marketplace, open window
  376. ENGINE->windows().createAndPushWindow<CMarketWindow>(townWithMarket, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  377. else //if not - complain
  378. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  379. }
  380. void AdventureMapShortcuts::firstTown()
  381. {
  382. if (!GAME->interface()->localState->getOwnedTowns().empty())
  383. {
  384. const auto * town = GAME->interface()->localState->getOwnedTown(0);
  385. GAME->interface()->localState->setSelection(town);
  386. owner.centerOnObject(town);
  387. }
  388. }
  389. void AdventureMapShortcuts::nextTown()
  390. {
  391. owner.hotkeyNextTown();
  392. }
  393. void AdventureMapShortcuts::zoom( int distance)
  394. {
  395. owner.hotkeyZoom(distance, false);
  396. }
  397. void AdventureMapShortcuts::search(bool next)
  398. {
  399. auto getColor = [](MapObjectID id ){
  400. switch (id)
  401. {
  402. case MapObjectID::HERO:
  403. return ColorRGBA{ 0, 192, 0};
  404. case MapObjectID::MONSTER:
  405. return ColorRGBA{ 255, 0, 0};
  406. case MapObjectID::TOWN:
  407. return ColorRGBA{ 100, 100, 255};
  408. case MapObjectID::MINE:
  409. return ColorRGBA{ 255, 153, 204};
  410. case MapObjectID::RESOURCE:
  411. return ColorRGBA{ 255, 51, 255};
  412. case MapObjectID::ARTIFACT:
  413. return ColorRGBA{ 192, 255, 0};
  414. default:
  415. return Colors::WHITE;
  416. }
  417. };
  418. // count of elements for each group (map is already sorted)
  419. std::map<std::pair<std::string, ColorRGBA>, int> mapObjCount;
  420. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  421. mapObjCount[{GAME->interface()->cb->getObjInstance(obj->id)->getObjectName(), getColor(obj->ID)}]++;
  422. // convert to vector for indexed access
  423. std::vector<std::pair<std::pair<std::string, ColorRGBA>, int>> textCountList;
  424. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  425. textCountList.push_back({(*itr).first, (*itr).second});
  426. // get pos of last selection
  427. int lastSel = 0;
  428. for(int i = 0; i < textCountList.size(); i++)
  429. if(textCountList[i].first.first == searchLast)
  430. lastSel = i;
  431. // create texts
  432. std::vector<std::string> texts;
  433. for(auto & obj : textCountList)
  434. texts.push_back("{" + Colors::colorToHexString(obj.first.second) + "|" + obj.first.first + "}" + " (" + std::to_string(obj.second) + ")");
  435. // function to center element from list on map
  436. auto selectObjOnMap = [this, textCountList](int index)
  437. {
  438. auto selObj = textCountList[index].first;
  439. // filter for matching objects
  440. std::vector<ObjectInstanceID> selVisitableObjInstances;
  441. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  442. if(selObj.first == GAME->interface()->cb->getObjInstance(obj->id)->getObjectName())
  443. selVisitableObjInstances.push_back(obj->id);
  444. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj.first)
  445. searchPos++;
  446. else
  447. searchPos = 0;
  448. auto objInst = GAME->interface()->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  449. owner.centerOnObject(objInst);
  450. searchLast = objInst->getObjectName();
  451. };
  452. if(next)
  453. selectObjOnMap(lastSel);
  454. else
  455. 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);
  456. }
  457. void AdventureMapShortcuts::nextObject()
  458. {
  459. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  460. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  461. if(h)
  462. nextHero();
  463. if(t)
  464. nextTown();
  465. }
  466. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  467. {
  468. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero(); //selected hero
  469. if(!h)
  470. return;
  471. if (GAME->map().hasOngoingAnimations())
  472. return;
  473. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  474. if (!GAME->map().isInMap((dst)))
  475. return;
  476. if ( !GAME->interface()->localState->setPath(h, dst))
  477. return;
  478. const CGPath & path = GAME->interface()->localState->getPath(h);
  479. if (path.nodes.size() > 2)
  480. owner.onHeroChanged(h);
  481. else
  482. if(path.nodes[0].turns == 0)
  483. GAME->interface()->moveHero(h, path);
  484. }
  485. bool AdventureMapShortcuts::optionCanViewQuests()
  486. {
  487. return optionInMapView() && !GAME->interface()->cb->getPlayerState(GAME->interface()->playerID)->quests.empty();
  488. }
  489. bool AdventureMapShortcuts::optionCanToggleLevel()
  490. {
  491. return optionSidePanelActive() && GAME->interface()->cb->getMapSize().z > 1;
  492. }
  493. bool AdventureMapShortcuts::optionMapLevelSurface()
  494. {
  495. return mapLevel == 0;
  496. }
  497. bool AdventureMapShortcuts::optionHeroSleeping()
  498. {
  499. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  500. return optionInMapView() && hero && GAME->interface()->localState->isHeroSleeping(hero);
  501. }
  502. bool AdventureMapShortcuts::optionHeroAwake()
  503. {
  504. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  505. return optionInMapView() && hero && !GAME->interface()->localState->isHeroSleeping(hero);
  506. }
  507. bool AdventureMapShortcuts::optionCanVisitObject()
  508. {
  509. if (!optionHeroSelected())
  510. return false;
  511. auto * hero = GAME->interface()->localState->getCurrentHero();
  512. auto objects = GAME->interface()->cb->getVisitableObjs(hero->visitablePos());
  513. return objects.size() > 1; // there is object other than our hero
  514. }
  515. bool AdventureMapShortcuts::optionHeroSelected()
  516. {
  517. return optionInMapView() && GAME->interface()->localState->getCurrentHero() != nullptr;
  518. }
  519. bool AdventureMapShortcuts::optionHeroCanMove()
  520. {
  521. const auto * hero = GAME->interface()->localState->getCurrentHero();
  522. return optionInMapView() && hero && GAME->interface()->localState->hasPath(hero) && GAME->interface()->localState->getPath(hero).nextNode().turns == 0;
  523. }
  524. bool AdventureMapShortcuts::optionHasNextHero()
  525. {
  526. const auto * hero = GAME->interface()->localState->getCurrentHero();
  527. const auto * nextSuitableHero = GAME->interface()->localState->getNextWanderingHero(hero);
  528. return optionInMapView() && nextSuitableHero != nullptr;
  529. }
  530. bool AdventureMapShortcuts::optionCanEndTurn()
  531. {
  532. return optionInMapView() && GAME->interface()->makingTurn;
  533. }
  534. bool AdventureMapShortcuts::optionSpellcasting()
  535. {
  536. return state == EAdventureState::CASTING_SPELL;
  537. }
  538. bool AdventureMapShortcuts::optionInMapView()
  539. {
  540. return state == EAdventureState::MAKING_TURN;
  541. }
  542. bool AdventureMapShortcuts::optionInWorldView()
  543. {
  544. return state == EAdventureState::WORLD_VIEW;
  545. }
  546. bool AdventureMapShortcuts::optionSidePanelActive()
  547. {
  548. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  549. }
  550. bool AdventureMapShortcuts::optionMapScrollingActive()
  551. {
  552. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  553. }
  554. bool AdventureMapShortcuts::optionMapViewActive()
  555. {
  556. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  557. }