AdventureMapShortcuts.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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, optionHeroSelected(), [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. };
  114. return result;
  115. }
  116. void AdventureMapShortcuts::showOverview()
  117. {
  118. ENGINE->windows().createAndPushWindow<CKingdomInterface>();
  119. }
  120. void AdventureMapShortcuts::worldViewBack()
  121. {
  122. owner.hotkeyExitWorldView();
  123. auto hero = GAME->interface()->localState->getCurrentHero();
  124. if (hero)
  125. owner.centerOnObject(hero);
  126. }
  127. void AdventureMapShortcuts::worldViewScale1x()
  128. {
  129. // TODO set corresponding scale button to "selected" mode
  130. owner.openWorldView(7);
  131. }
  132. void AdventureMapShortcuts::worldViewScale2x()
  133. {
  134. owner.openWorldView(11);
  135. }
  136. void AdventureMapShortcuts::worldViewScale4x()
  137. {
  138. owner.openWorldView(16);
  139. }
  140. void AdventureMapShortcuts::switchMapLevel()
  141. {
  142. owner.hotkeySwitchMapLevel();
  143. }
  144. void AdventureMapShortcuts::showQuestlog()
  145. {
  146. GAME->interface()->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 = GAME->interface()->localState->getCurrentHero();
  180. if (h)
  181. {
  182. GAME->interface()->localState->setHeroAsleep(h);
  183. owner.onHeroChanged(h);
  184. nextHero();
  185. }
  186. }
  187. void AdventureMapShortcuts::setHeroAwake()
  188. {
  189. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  190. if (h)
  191. {
  192. GAME->interface()->localState->setHeroAwaken(h);
  193. owner.onHeroChanged(h);
  194. }
  195. }
  196. void AdventureMapShortcuts::moveHeroAlongPath()
  197. {
  198. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  199. if (!h || !GAME->interface()->localState->hasPath(h))
  200. return;
  201. GAME->interface()->moveHero(h, GAME->interface()->localState->getPath(h));
  202. }
  203. void AdventureMapShortcuts::showSpellbook()
  204. {
  205. if (!GAME->interface()->localState->getCurrentHero())
  206. return;
  207. owner.centerOnObject(GAME->interface()->localState->getCurrentHero());
  208. ENGINE->windows().createAndPushWindow<CSpellWindow>(GAME->interface()->localState->getCurrentHero(), GAME->interface(), 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 (!GAME->interface()->localState->getWanderingHeroes().empty())
  221. {
  222. const auto * hero = GAME->interface()->localState->getWanderingHero(0);
  223. GAME->interface()->localState->setSelection(hero);
  224. owner.centerOnObject(hero);
  225. }
  226. }
  227. void AdventureMapShortcuts::nextHero()
  228. {
  229. const auto * currHero = GAME->interface()->localState->getCurrentHero();
  230. const auto * nextHero = GAME->interface()->localState->getNextWanderingHero(currHero);
  231. if (nextHero)
  232. {
  233. GAME->interface()->localState->setSelection(nextHero);
  234. owner.centerOnObject(nextHero);
  235. }
  236. }
  237. void AdventureMapShortcuts::endTurn()
  238. {
  239. if(!GAME->interface()->makingTurn)
  240. return;
  241. if(settings["adventure"]["heroReminder"].Bool())
  242. {
  243. for(auto hero : GAME->interface()->localState->getWanderingHeroes())
  244. {
  245. if(!GAME->interface()->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. GAME->interface()->localState->verifyPath(hero);
  251. if(!GAME->interface()->localState->hasPath(hero))
  252. {
  253. GAME->interface()->showYesNoDialog( LIBRARY->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  254. return;
  255. }
  256. auto path = GAME->interface()->localState->getPath(hero);
  257. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  258. {
  259. GAME->interface()->showYesNoDialog( LIBRARY->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 = boost::range::find_if(GAME->interface()->localState->getOwnedTowns(), [](const CGTownInstance * town)
  271. {
  272. return town->hasBuilt(BuildingID::TAVERN);
  273. });
  274. if(itr != GAME->interface()->localState->getOwnedTowns().end())
  275. GAME->interface()->showThievesGuildWindow(*itr);
  276. else
  277. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  278. }
  279. void AdventureMapShortcuts::showScenarioInfo()
  280. {
  281. AdventureOptions::showScenarioInfo();
  282. }
  283. void AdventureMapShortcuts::toMainMenu()
  284. {
  285. GAME->interface()->showYesNoDialog(
  286. LIBRARY->generaltexth->allTexts[578],
  287. []()
  288. {
  289. GAME->server().endGameplay();
  290. GAME->mainmenu()->menu->switchToTab("main");
  291. },
  292. 0
  293. );
  294. }
  295. void AdventureMapShortcuts::newGame()
  296. {
  297. GAME->interface()->showYesNoDialog(
  298. LIBRARY->generaltexth->allTexts[578],
  299. []()
  300. {
  301. GAME->server().endGameplay();
  302. GAME->mainmenu()->menu->switchToTab("new");
  303. },
  304. nullptr
  305. );
  306. }
  307. void AdventureMapShortcuts::quitGame()
  308. {
  309. GAME->interface()->showYesNoDialog(
  310. LIBRARY->generaltexth->allTexts[578],
  311. [](){ GAME->onShutdownRequested(false);},
  312. nullptr
  313. );
  314. }
  315. void AdventureMapShortcuts::saveGame()
  316. {
  317. ENGINE->windows().createAndPushWindow<CSavingScreen>();
  318. }
  319. void AdventureMapShortcuts::loadGame()
  320. {
  321. GAME->interface()->proposeLoadingGame();
  322. }
  323. void AdventureMapShortcuts::digGrail()
  324. {
  325. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  326. if(h && GAME->interface()->makingTurn)
  327. GAME->interface()->tryDigging(h);
  328. }
  329. void AdventureMapShortcuts::viewPuzzleMap()
  330. {
  331. GAME->interface()->showPuzzleMap();
  332. }
  333. void AdventureMapShortcuts::restartGame()
  334. {
  335. GAME->interface()->showYesNoDialog(
  336. LIBRARY->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  337. []()
  338. {
  339. ENGINE->dispatchMainThread(
  340. []()
  341. {
  342. GAME->server().sendRestartGame();
  343. }
  344. );
  345. },
  346. nullptr
  347. );
  348. }
  349. void AdventureMapShortcuts::visitObject()
  350. {
  351. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  352. if(h)
  353. GAME->interface()->cb->moveHero(h, h->pos, false);
  354. }
  355. void AdventureMapShortcuts::openObject()
  356. {
  357. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  358. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  359. if(h)
  360. GAME->interface()->openHeroWindow(h);
  361. if(t)
  362. GAME->interface()->openTownWindow(t);
  363. }
  364. void AdventureMapShortcuts::showMarketplace()
  365. {
  366. //check if we have any marketplace
  367. const CGTownInstance *townWithMarket = nullptr;
  368. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  369. {
  370. if(t->hasBuilt(BuildingID::MARKETPLACE))
  371. {
  372. townWithMarket = t;
  373. break;
  374. }
  375. }
  376. if(townWithMarket) //if any town has marketplace, open window
  377. ENGINE->windows().createAndPushWindow<CMarketWindow>(townWithMarket, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  378. else //if not - complain
  379. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  380. }
  381. void AdventureMapShortcuts::firstTown()
  382. {
  383. if (!GAME->interface()->localState->getOwnedTowns().empty())
  384. {
  385. const auto * town = GAME->interface()->localState->getOwnedTown(0);
  386. GAME->interface()->localState->setSelection(town);
  387. owner.centerOnObject(town);
  388. }
  389. }
  390. void AdventureMapShortcuts::nextTown()
  391. {
  392. owner.hotkeyNextTown();
  393. }
  394. void AdventureMapShortcuts::zoom( int distance)
  395. {
  396. owner.hotkeyZoom(distance, false);
  397. }
  398. void AdventureMapShortcuts::search(bool next)
  399. {
  400. auto getColor = [](MapObjectID id ){
  401. switch (id)
  402. {
  403. case MapObjectID::HERO:
  404. return ColorRGBA{ 0, 192, 0};
  405. case MapObjectID::MONSTER:
  406. return ColorRGBA{ 255, 0, 0};
  407. case MapObjectID::TOWN:
  408. return ColorRGBA{ 100, 100, 255};
  409. case MapObjectID::MINE:
  410. return ColorRGBA{ 255, 153, 204};
  411. case MapObjectID::RESOURCE:
  412. return ColorRGBA{ 255, 51, 255};
  413. case MapObjectID::ARTIFACT:
  414. return ColorRGBA{ 192, 255, 0};
  415. default:
  416. return Colors::WHITE;
  417. }
  418. };
  419. // count of elements for each group (map is already sorted)
  420. std::map<std::pair<std::string, ColorRGBA>, int> mapObjCount;
  421. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  422. mapObjCount[{GAME->interface()->cb->getObjInstance(obj->id)->getObjectName(), getColor(obj->ID)}]++;
  423. // convert to vector for indexed access
  424. std::vector<std::pair<std::pair<std::string, ColorRGBA>, int>> textCountList;
  425. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  426. textCountList.push_back({(*itr).first, (*itr).second});
  427. // get pos of last selection
  428. int lastSel = 0;
  429. for(int i = 0; i < textCountList.size(); i++)
  430. if(textCountList[i].first.first == searchLast)
  431. lastSel = i;
  432. // create texts
  433. std::vector<std::string> texts;
  434. for(auto & obj : textCountList)
  435. texts.push_back("{" + Colors::colorToHexString(obj.first.second) + "|" + obj.first.first + "}" + " (" + std::to_string(obj.second) + ")");
  436. // function to center element from list on map
  437. auto selectObjOnMap = [this, textCountList](int index)
  438. {
  439. auto selObj = textCountList[index].first;
  440. // filter for matching objects
  441. std::vector<ObjectInstanceID> selVisitableObjInstances;
  442. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  443. if(selObj.first == GAME->interface()->cb->getObjInstance(obj->id)->getObjectName())
  444. selVisitableObjInstances.push_back(obj->id);
  445. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj.first)
  446. searchPos++;
  447. else
  448. searchPos = 0;
  449. auto objInst = GAME->interface()->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  450. owner.centerOnObject(objInst);
  451. searchLast = objInst->getObjectName();
  452. };
  453. if(next)
  454. selectObjOnMap(lastSel);
  455. else
  456. 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);
  457. }
  458. void AdventureMapShortcuts::nextObject()
  459. {
  460. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  461. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  462. if(h)
  463. nextHero();
  464. if(t)
  465. nextTown();
  466. }
  467. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  468. {
  469. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero(); //selected hero
  470. if(!h)
  471. return;
  472. if (GAME->map().hasOngoingAnimations())
  473. return;
  474. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  475. if (!GAME->map().isInMap((dst)))
  476. return;
  477. if ( !GAME->interface()->localState->setPath(h, dst))
  478. return;
  479. const CGPath & path = GAME->interface()->localState->getPath(h);
  480. if (path.nodes.size() > 2)
  481. owner.onHeroChanged(h);
  482. else
  483. if(path.nodes[0].turns == 0)
  484. GAME->interface()->moveHero(h, path);
  485. }
  486. bool AdventureMapShortcuts::optionCanViewQuests()
  487. {
  488. return optionInMapView() && !GAME->interface()->cb->getPlayerState(GAME->interface()->playerID)->quests.empty();
  489. }
  490. bool AdventureMapShortcuts::optionCanToggleLevel()
  491. {
  492. return optionSidePanelActive() && GAME->interface()->cb->getMapSize().z > 1;
  493. }
  494. int AdventureMapShortcuts::optionMapLevel()
  495. {
  496. return mapLevel;
  497. }
  498. bool AdventureMapShortcuts::optionHeroSleeping()
  499. {
  500. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  501. return optionInMapView() && hero && GAME->interface()->localState->isHeroSleeping(hero);
  502. }
  503. bool AdventureMapShortcuts::optionHeroAwake()
  504. {
  505. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  506. return optionInMapView() && hero && !GAME->interface()->localState->isHeroSleeping(hero);
  507. }
  508. bool AdventureMapShortcuts::optionCanVisitObject()
  509. {
  510. if (!optionHeroSelected())
  511. return false;
  512. auto * hero = GAME->interface()->localState->getCurrentHero();
  513. auto objects = GAME->interface()->cb->getVisitableObjs(hero->visitablePos());
  514. return objects.size() > 1; // there is object other than our hero
  515. }
  516. bool AdventureMapShortcuts::optionHeroSelected()
  517. {
  518. return optionInMapView() && GAME->interface()->localState->getCurrentHero() != nullptr;
  519. }
  520. bool AdventureMapShortcuts::optionHeroCanMove()
  521. {
  522. const auto * hero = GAME->interface()->localState->getCurrentHero();
  523. return optionInMapView() && hero && GAME->interface()->localState->hasPath(hero) && GAME->interface()->localState->getPath(hero).nextNode().turns == 0;
  524. }
  525. bool AdventureMapShortcuts::optionHasNextHero()
  526. {
  527. const auto * hero = GAME->interface()->localState->getCurrentHero();
  528. const auto * nextSuitableHero = GAME->interface()->localState->getNextWanderingHero(hero);
  529. return optionInMapView() && nextSuitableHero != nullptr;
  530. }
  531. bool AdventureMapShortcuts::optionCanEndTurn()
  532. {
  533. return optionInMapView() && GAME->interface()->makingTurn;
  534. }
  535. bool AdventureMapShortcuts::optionSpellcasting()
  536. {
  537. return state == EAdventureState::CASTING_SPELL;
  538. }
  539. bool AdventureMapShortcuts::optionInMapView()
  540. {
  541. return state == EAdventureState::MAKING_TURN;
  542. }
  543. bool AdventureMapShortcuts::optionInWorldView()
  544. {
  545. return state == EAdventureState::WORLD_VIEW;
  546. }
  547. bool AdventureMapShortcuts::optionSidePanelActive()
  548. {
  549. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  550. }
  551. bool AdventureMapShortcuts::optionMapScrollingActive()
  552. {
  553. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  554. }
  555. bool AdventureMapShortcuts::optionMapViewActive()
  556. {
  557. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  558. }
  559. bool AdventureMapShortcuts::optionMarketplace()
  560. {
  561. if(state != EAdventureState::MAKING_TURN)
  562. return false;
  563. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  564. if(t->hasBuilt(BuildingID::MARKETPLACE))
  565. return true;
  566. return false;
  567. }
  568. bool AdventureMapShortcuts::optionHeroGround()
  569. {
  570. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  571. return optionInMapView() && hero && !hero->inBoat();
  572. }
  573. bool AdventureMapShortcuts::optionHeroBoat(EPathfindingLayer layer)
  574. {
  575. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  576. return optionInMapView() && hero && hero->inBoat() && hero->getBoat()->layer == layer;
  577. }