AdventureMapShortcuts.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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_VIEW_STATISTIC, optionViewStatistic(), [this]() { this->viewStatistic(); } },
  69. { EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL, optionCanToggleLevel(), [this]() { this->switchMapLevel(); } },
  70. { EShortcut::ADVENTURE_QUEST_LOG, optionCanViewQuests(), [this]() { this->showQuestlog(); } },
  71. { EShortcut::ADVENTURE_TOGGLE_SLEEP, optionHeroSelected(), [this]() { this->toggleSleepWake(); } },
  72. { EShortcut::ADVENTURE_TOGGLE_GRID, optionInMapView(), [this]() { this->toggleGrid(); } },
  73. { EShortcut::ADVENTURE_TOGGLE_VISITABLE, optionInMapView(), [this]() { this->toggleVisitable(); } },
  74. { EShortcut::ADVENTURE_TOGGLE_BLOCKED, optionInMapView(), [this]() { this->toggleBlocked(); } },
  75. { EShortcut::ADVENTURE_TRACK_HERO, optionInMapView(), [this]() { this->toggleTrackHero(); } },
  76. { EShortcut::ADVENTURE_SET_HERO_ASLEEP, optionHeroAwake(), [this]() { this->setHeroSleeping(); } },
  77. { EShortcut::ADVENTURE_SET_HERO_AWAKE, optionHeroSleeping(), [this]() { this->setHeroAwake(); } },
  78. { EShortcut::ADVENTURE_MOVE_HERO, optionHeroCanMove(), [this]() { this->moveHeroAlongPath(); } },
  79. { EShortcut::ADVENTURE_CAST_SPELL, optionHeroSelected(), [this]() { this->showSpellbook(); } },
  80. { EShortcut::ADVENTURE_GAME_OPTIONS, optionInMapView(), [this]() { this->adventureOptions(); } },
  81. { EShortcut::GLOBAL_OPTIONS, optionInMapView(), [this]() { this->systemOptions(); } },
  82. { EShortcut::ADVENTURE_FIRST_HERO, optionInMapView(), [this]() { this->firstHero(); } },
  83. { EShortcut::ADVENTURE_NEXT_HERO, optionHasNextHero(), [this]() { this->nextHero(); } },
  84. { EShortcut::ADVENTURE_END_TURN, optionCanEndTurn(), [this]() { this->endTurn(); } },
  85. { EShortcut::ADVENTURE_THIEVES_GUILD, optionInMapView(), [this]() { this->showThievesGuild(); } },
  86. { EShortcut::ADVENTURE_VIEW_SCENARIO, optionInMapView(), [this]() { this->showScenarioInfo(); } },
  87. { EShortcut::ADVENTURE_QUIT_GAME, optionInMapView(), [this]() { this->quitGame(); } },
  88. { EShortcut::ADVENTURE_TO_MAIN_MENU, optionInMapView(), [this]() { this->toMainMenu(); } },
  89. { EShortcut::ADVENTURE_SAVE_GAME, optionInMapView(), [this]() { this->saveGame(); } },
  90. { EShortcut::ADVENTURE_NEW_GAME, optionInMapView(), [this]() { this->newGame(); } },
  91. { EShortcut::ADVENTURE_LOAD_GAME, optionInMapView(), [this]() { this->loadGame(); } },
  92. { EShortcut::ADVENTURE_QUICK_SAVE, optionIsLocal(), [this]() { this->quickSaveGame(); } },
  93. { EShortcut::ADVENTURE_QUICK_LOAD, optionQuickSaveLoad(), [this]() { this->quickLoadGame(); } },
  94. { EShortcut::ADVENTURE_RESTART_GAME, optionInMapView(), [this]() { this->restartGame(); } },
  95. { EShortcut::ADVENTURE_DIG_GRAIL, optionHeroDig(), [this]() { this->digGrail(); } },
  96. { EShortcut::ADVENTURE_VIEW_PUZZLE, optionSidePanelActive(),[this]() { this->viewPuzzleMap(); } },
  97. { EShortcut::ADVENTURE_VISIT_OBJECT, optionCanVisitObject(), [this]() { this->visitObject(); } },
  98. { EShortcut::ADVENTURE_VIEW_SELECTED, optionInMapView(), [this]() { this->openObject(); } },
  99. { EShortcut::ADVENTURE_MARKETPLACE, optionMarketplace(), [this]() { this->showMarketplace(); } },
  100. { EShortcut::ADVENTURE_ZOOM_IN, optionSidePanelActive(),[this]() { this->zoom(+10); } },
  101. { EShortcut::ADVENTURE_ZOOM_OUT, optionSidePanelActive(),[this]() { this->zoom(-10); } },
  102. { EShortcut::ADVENTURE_ZOOM_RESET, optionSidePanelActive(),[this]() { this->zoom( 0); } },
  103. { EShortcut::ADVENTURE_FIRST_TOWN, optionInMapView(), [this]() { this->firstTown(); } },
  104. { EShortcut::ADVENTURE_NEXT_TOWN, optionInMapView(), [this]() { this->nextTown(); } },
  105. { EShortcut::ADVENTURE_NEXT_OBJECT, optionInMapView(), [this]() { this->nextObject(); } },
  106. { EShortcut::ADVENTURE_MOVE_HERO_SW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, +1}); } },
  107. { EShortcut::ADVENTURE_MOVE_HERO_SS, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, +1}); } },
  108. { EShortcut::ADVENTURE_MOVE_HERO_SE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, +1}); } },
  109. { EShortcut::ADVENTURE_MOVE_HERO_WW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, 0}); } },
  110. { EShortcut::ADVENTURE_MOVE_HERO_EE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, 0}); } },
  111. { EShortcut::ADVENTURE_MOVE_HERO_NW, optionHeroSelected(), [this]() { this->moveHeroDirectional({-1, -1}); } },
  112. { EShortcut::ADVENTURE_MOVE_HERO_NN, optionHeroSelected(), [this]() { this->moveHeroDirectional({ 0, -1}); } },
  113. { EShortcut::ADVENTURE_MOVE_HERO_NE, optionHeroSelected(), [this]() { this->moveHeroDirectional({+1, -1}); } },
  114. { EShortcut::ADVENTURE_SEARCH, optionSidePanelActive(),[this]() { this->search(false); } },
  115. { EShortcut::ADVENTURE_SEARCH_CONTINUE, optionSidePanelActive(),[this]() { this->search(true); } },
  116. { EShortcut::MAIN_MENU_LOBBY, optionSidePanelActive(),[ ]() { ENGINE->user().onGlobalLobbyInterfaceActivated(); } }
  117. };
  118. return result;
  119. }
  120. void AdventureMapShortcuts::showOverview()
  121. {
  122. ENGINE->windows().createAndPushWindow<CKingdomInterface>();
  123. }
  124. void AdventureMapShortcuts::worldViewBack()
  125. {
  126. owner.hotkeyExitWorldView();
  127. auto hero = GAME->interface()->localState->getCurrentHero();
  128. if (hero)
  129. owner.centerOnObject(hero);
  130. }
  131. void AdventureMapShortcuts::worldViewScale1x()
  132. {
  133. // TODO set corresponding scale button to "selected" mode
  134. owner.openWorldView(7);
  135. }
  136. void AdventureMapShortcuts::worldViewScale2x()
  137. {
  138. owner.openWorldView(11);
  139. }
  140. void AdventureMapShortcuts::worldViewScale4x()
  141. {
  142. owner.openWorldView(16);
  143. }
  144. void AdventureMapShortcuts::viewStatistic()
  145. {
  146. GAME->interface()->cb->requestStatistic();
  147. }
  148. void AdventureMapShortcuts::switchMapLevel()
  149. {
  150. owner.hotkeySwitchMapLevel();
  151. }
  152. void AdventureMapShortcuts::showQuestlog()
  153. {
  154. GAME->interface()->showQuestLog();
  155. }
  156. void AdventureMapShortcuts::toggleTrackHero()
  157. {
  158. Settings s = settings.write["session"];
  159. s["adventureTrackHero"].Bool() = !settings["session"]["adventureTrackHero"].Bool();
  160. }
  161. void AdventureMapShortcuts::toggleGrid()
  162. {
  163. Settings s = settings.write["gameTweaks"];
  164. s["showGrid"].Bool() = !settings["gameTweaks"]["showGrid"].Bool();
  165. }
  166. void AdventureMapShortcuts::toggleVisitable()
  167. {
  168. Settings s = settings.write["session"];
  169. s["showVisitable"].Bool() = !settings["session"]["showVisitable"].Bool();
  170. }
  171. void AdventureMapShortcuts::toggleBlocked()
  172. {
  173. Settings s = settings.write["session"];
  174. s["showBlocked"].Bool() = !settings["session"]["showBlocked"].Bool();
  175. }
  176. void AdventureMapShortcuts::toggleSleepWake()
  177. {
  178. if (!optionHeroSelected())
  179. return;
  180. if (optionHeroAwake())
  181. setHeroSleeping();
  182. else
  183. setHeroAwake();
  184. }
  185. void AdventureMapShortcuts::setHeroSleeping()
  186. {
  187. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  188. if (h)
  189. {
  190. GAME->interface()->localState->setHeroAsleep(h);
  191. owner.onHeroChanged(h);
  192. nextHero();
  193. }
  194. }
  195. void AdventureMapShortcuts::setHeroAwake()
  196. {
  197. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  198. if (h)
  199. {
  200. GAME->interface()->localState->setHeroAwaken(h);
  201. owner.onHeroChanged(h);
  202. }
  203. }
  204. void AdventureMapShortcuts::moveHeroAlongPath()
  205. {
  206. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  207. if (!h || !GAME->interface()->localState->hasPath(h))
  208. return;
  209. GAME->interface()->moveHero(h, GAME->interface()->localState->getPath(h));
  210. }
  211. void AdventureMapShortcuts::showSpellbook()
  212. {
  213. if (!GAME->interface()->localState->getCurrentHero())
  214. return;
  215. owner.centerOnObject(GAME->interface()->localState->getCurrentHero());
  216. ENGINE->windows().createAndPushWindow<CSpellWindow>(GAME->interface()->localState->getCurrentHero(), GAME->interface(), false);
  217. }
  218. void AdventureMapShortcuts::adventureOptions()
  219. {
  220. ENGINE->windows().createAndPushWindow<AdventureOptions>();
  221. }
  222. void AdventureMapShortcuts::systemOptions()
  223. {
  224. ENGINE->windows().createAndPushWindow<SettingsMainWindow>();
  225. }
  226. void AdventureMapShortcuts::firstHero()
  227. {
  228. if (!GAME->interface()->localState->getWanderingHeroes().empty())
  229. {
  230. const auto * hero = GAME->interface()->localState->getWanderingHero(0);
  231. GAME->interface()->localState->setSelection(hero);
  232. owner.centerOnObject(hero);
  233. }
  234. }
  235. void AdventureMapShortcuts::nextHero()
  236. {
  237. const auto * currHero = GAME->interface()->localState->getCurrentHero();
  238. const auto * nextHero = GAME->interface()->localState->getNextWanderingHero(currHero);
  239. if (nextHero)
  240. {
  241. GAME->interface()->localState->setSelection(nextHero);
  242. owner.centerOnObject(nextHero);
  243. }
  244. }
  245. void AdventureMapShortcuts::endTurn()
  246. {
  247. if(!GAME->interface()->makingTurn)
  248. return;
  249. if(settings["adventure"]["heroReminder"].Bool())
  250. {
  251. for(auto hero : GAME->interface()->localState->getWanderingHeroes())
  252. {
  253. if(!GAME->interface()->localState->isHeroSleeping(hero) && hero->movementPointsRemaining() > 0)
  254. {
  255. // Only show hero reminder if conditions met:
  256. // - There still movement points
  257. // - Hero don't have a path or there not points for first step on path
  258. GAME->interface()->localState->verifyPath(hero);
  259. if(!GAME->interface()->localState->hasPath(hero))
  260. {
  261. GAME->interface()->showYesNoDialog( LIBRARY->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  262. return;
  263. }
  264. auto path = GAME->interface()->localState->getPath(hero);
  265. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  266. {
  267. GAME->interface()->showYesNoDialog( LIBRARY->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  268. return;
  269. }
  270. }
  271. }
  272. }
  273. owner.hotkeyEndingTurn();
  274. }
  275. void AdventureMapShortcuts::showThievesGuild()
  276. {
  277. //find first town with tavern
  278. auto itr = boost::range::find_if(GAME->interface()->localState->getOwnedTowns(), [](const CGTownInstance * town)
  279. {
  280. return town->hasBuilt(BuildingID::TAVERN);
  281. });
  282. if(itr != GAME->interface()->localState->getOwnedTowns().end())
  283. GAME->interface()->showThievesGuildWindow(*itr);
  284. else
  285. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  286. }
  287. void AdventureMapShortcuts::showScenarioInfo()
  288. {
  289. AdventureOptions::showScenarioInfo();
  290. }
  291. void AdventureMapShortcuts::toMainMenu()
  292. {
  293. GAME->interface()->showYesNoDialog(
  294. LIBRARY->generaltexth->allTexts[578],
  295. []()
  296. {
  297. GAME->server().endGameplay();
  298. GAME->mainmenu()->menu->switchToTab("main");
  299. },
  300. 0
  301. );
  302. }
  303. void AdventureMapShortcuts::newGame()
  304. {
  305. GAME->interface()->showYesNoDialog(
  306. LIBRARY->generaltexth->allTexts[578],
  307. []()
  308. {
  309. GAME->server().endGameplay();
  310. GAME->mainmenu()->menu->switchToTab("new");
  311. },
  312. nullptr
  313. );
  314. }
  315. void AdventureMapShortcuts::quitGame()
  316. {
  317. GAME->interface()->showYesNoDialog(
  318. LIBRARY->generaltexth->allTexts[578],
  319. [](){ GAME->onShutdownRequested(false);},
  320. nullptr
  321. );
  322. }
  323. void AdventureMapShortcuts::saveGame()
  324. {
  325. ENGINE->windows().createAndPushWindow<CSavingScreen>();
  326. }
  327. void AdventureMapShortcuts::loadGame()
  328. {
  329. GAME->interface()->proposeLoadingGame();
  330. }
  331. void AdventureMapShortcuts::quickSaveGame()
  332. {
  333. GAME->interface()->quickSaveGame();
  334. }
  335. void AdventureMapShortcuts::quickLoadGame()
  336. {
  337. GAME->interface()->proposeQuickLoadingGame();
  338. }
  339. void AdventureMapShortcuts::digGrail()
  340. {
  341. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  342. if(h && GAME->interface()->makingTurn)
  343. GAME->interface()->tryDigging(h);
  344. }
  345. void AdventureMapShortcuts::viewPuzzleMap()
  346. {
  347. GAME->interface()->showPuzzleMap();
  348. }
  349. void AdventureMapShortcuts::restartGame()
  350. {
  351. GAME->interface()->showYesNoDialog(
  352. LIBRARY->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  353. []()
  354. {
  355. ENGINE->dispatchMainThread(
  356. []()
  357. {
  358. GAME->server().sendRestartGame();
  359. }
  360. );
  361. },
  362. nullptr
  363. );
  364. }
  365. void AdventureMapShortcuts::visitObject()
  366. {
  367. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  368. if(h)
  369. GAME->interface()->cb->moveHero(h, h->pos, false);
  370. }
  371. void AdventureMapShortcuts::openObject()
  372. {
  373. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  374. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  375. if(h)
  376. GAME->interface()->openHeroWindow(h);
  377. if(t)
  378. GAME->interface()->openTownWindow(t);
  379. }
  380. void AdventureMapShortcuts::showMarketplace()
  381. {
  382. //check if we have any marketplace
  383. const CGTownInstance *townWithMarket = nullptr;
  384. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  385. {
  386. if(t->hasBuilt(BuildingID::MARKETPLACE))
  387. {
  388. townWithMarket = t;
  389. break;
  390. }
  391. }
  392. if(townWithMarket) //if any town has marketplace, open window
  393. ENGINE->windows().createAndPushWindow<CMarketWindow>(townWithMarket, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  394. else //if not - complain
  395. GAME->interface()->showInfoDialog(LIBRARY->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  396. }
  397. void AdventureMapShortcuts::firstTown()
  398. {
  399. if (!GAME->interface()->localState->getOwnedTowns().empty())
  400. {
  401. const auto * town = GAME->interface()->localState->getOwnedTown(0);
  402. GAME->interface()->localState->setSelection(town);
  403. owner.centerOnObject(town);
  404. }
  405. }
  406. void AdventureMapShortcuts::nextTown()
  407. {
  408. owner.hotkeyNextTown();
  409. }
  410. void AdventureMapShortcuts::zoom( int distance)
  411. {
  412. owner.hotkeyZoom(distance, false);
  413. }
  414. void AdventureMapShortcuts::search(bool next)
  415. {
  416. auto getColor = [](MapObjectID id ){
  417. switch (id)
  418. {
  419. case MapObjectID::HERO:
  420. return ColorRGBA{ 0, 192, 0};
  421. case MapObjectID::MONSTER:
  422. return ColorRGBA{ 255, 0, 0};
  423. case MapObjectID::TOWN:
  424. return ColorRGBA{ 100, 100, 255};
  425. case MapObjectID::MINE:
  426. return ColorRGBA{ 255, 153, 204};
  427. case MapObjectID::RESOURCE:
  428. return ColorRGBA{ 255, 51, 255};
  429. case MapObjectID::ARTIFACT:
  430. return ColorRGBA{ 192, 255, 0};
  431. default:
  432. return Colors::WHITE;
  433. }
  434. };
  435. // count of elements for each group (map is already sorted)
  436. std::map<std::pair<std::string, ColorRGBA>, int> mapObjCount;
  437. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  438. mapObjCount[{GAME->interface()->cb->getObjInstance(obj->id)->getObjectName(), getColor(obj->ID)}]++;
  439. // convert to vector for indexed access
  440. std::vector<std::pair<std::pair<std::string, ColorRGBA>, int>> textCountList;
  441. for (auto itr = mapObjCount.begin(); itr != mapObjCount.end(); ++itr)
  442. textCountList.push_back({(*itr).first, (*itr).second});
  443. // get pos of last selection
  444. int lastSel = 0;
  445. for(int i = 0; i < textCountList.size(); i++)
  446. if(textCountList[i].first.first == searchLast)
  447. lastSel = i;
  448. // create texts
  449. std::vector<std::string> texts;
  450. for(auto & obj : textCountList)
  451. texts.push_back("{" + Colors::colorToHexString(obj.first.second) + "|" + obj.first.first + "}" + " (" + std::to_string(obj.second) + ")");
  452. // function to center element from list on map
  453. auto selectObjOnMap = [this, textCountList](int index)
  454. {
  455. auto selObj = textCountList[index].first;
  456. // filter for matching objects
  457. std::vector<ObjectInstanceID> selVisitableObjInstances;
  458. for(auto & obj : GAME->interface()->cb->getAllVisitableObjs())
  459. if(selObj.first == GAME->interface()->cb->getObjInstance(obj->id)->getObjectName())
  460. selVisitableObjInstances.push_back(obj->id);
  461. if(searchPos + 1 < selVisitableObjInstances.size() && searchLast == selObj.first)
  462. searchPos++;
  463. else
  464. searchPos = 0;
  465. auto objInst = GAME->interface()->cb->getObjInstance(selVisitableObjInstances[searchPos]);
  466. owner.centerOnObject(objInst);
  467. searchLast = objInst->getObjectName();
  468. };
  469. if(next)
  470. selectObjOnMap(lastSel);
  471. else
  472. 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);
  473. }
  474. void AdventureMapShortcuts::nextObject()
  475. {
  476. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero();
  477. const CGTownInstance *t = GAME->interface()->localState->getCurrentTown();
  478. if(h)
  479. nextHero();
  480. if(t)
  481. nextTown();
  482. }
  483. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  484. {
  485. const CGHeroInstance *h = GAME->interface()->localState->getCurrentHero(); //selected hero
  486. if(!h)
  487. return;
  488. if (GAME->map().hasOngoingAnimations())
  489. return;
  490. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  491. if (!GAME->map().isInMap((dst)))
  492. return;
  493. if ( !GAME->interface()->localState->setPath(h, dst))
  494. return;
  495. const CGPath & path = GAME->interface()->localState->getPath(h);
  496. if (path.nodes.size() > 2)
  497. owner.onHeroChanged(h);
  498. else
  499. if(path.nodes[0].turns == 0)
  500. GAME->interface()->moveHero(h, path);
  501. }
  502. bool AdventureMapShortcuts::optionCanViewQuests()
  503. {
  504. return optionInMapView() && !GAME->interface()->cb->getPlayerState(GAME->interface()->playerID)->quests.empty();
  505. }
  506. bool AdventureMapShortcuts::optionCanToggleLevel()
  507. {
  508. return optionSidePanelActive() && GAME->interface()->cb->getMapSize().z > 1;
  509. }
  510. int AdventureMapShortcuts::optionMapLevel()
  511. {
  512. return mapLevel;
  513. }
  514. bool AdventureMapShortcuts::optionHeroSleeping()
  515. {
  516. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  517. return optionInMapView() && hero && GAME->interface()->localState->isHeroSleeping(hero);
  518. }
  519. bool AdventureMapShortcuts::optionHeroAwake()
  520. {
  521. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  522. return optionInMapView() && hero && !GAME->interface()->localState->isHeroSleeping(hero);
  523. }
  524. bool AdventureMapShortcuts::optionCanVisitObject()
  525. {
  526. if (!optionHeroSelected())
  527. return false;
  528. auto * hero = GAME->interface()->localState->getCurrentHero();
  529. auto objects = GAME->interface()->cb->getVisitableObjs(hero->visitablePos());
  530. return objects.size() > 1; // there is object other than our hero
  531. }
  532. bool AdventureMapShortcuts::optionHeroSelected()
  533. {
  534. return optionInMapView() && GAME->interface()->localState->getCurrentHero() != nullptr;
  535. }
  536. bool AdventureMapShortcuts::optionHeroCanMove()
  537. {
  538. const auto * hero = GAME->interface()->localState->getCurrentHero();
  539. return optionInMapView() && hero && GAME->interface()->localState->hasPath(hero) && GAME->interface()->localState->getPath(hero).nextNode().turns == 0;
  540. }
  541. bool AdventureMapShortcuts::optionHasNextHero()
  542. {
  543. const auto * hero = GAME->interface()->localState->getCurrentHero();
  544. const auto * nextSuitableHero = GAME->interface()->localState->getNextWanderingHero(hero);
  545. return optionInMapView() && nextSuitableHero != nullptr;
  546. }
  547. bool AdventureMapShortcuts::optionCanEndTurn()
  548. {
  549. return optionInMapView() && GAME->interface()->makingTurn;
  550. }
  551. bool AdventureMapShortcuts::optionSpellcasting()
  552. {
  553. return state == EAdventureState::CASTING_SPELL;
  554. }
  555. bool AdventureMapShortcuts::optionInMapView()
  556. {
  557. return state == EAdventureState::MAKING_TURN;
  558. }
  559. bool AdventureMapShortcuts::optionInWorldView()
  560. {
  561. return state == EAdventureState::WORLD_VIEW;
  562. }
  563. bool AdventureMapShortcuts::optionSidePanelActive()
  564. {
  565. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  566. }
  567. bool AdventureMapShortcuts::optionMapScrollingActive()
  568. {
  569. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  570. }
  571. bool AdventureMapShortcuts::optionMapViewActive()
  572. {
  573. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  574. }
  575. bool AdventureMapShortcuts::optionMarketplace()
  576. {
  577. if(state != EAdventureState::MAKING_TURN)
  578. return false;
  579. for(const CGTownInstance *t : GAME->interface()->cb->getTownsInfo())
  580. if(t->hasBuilt(BuildingID::MARKETPLACE))
  581. return true;
  582. return false;
  583. }
  584. bool AdventureMapShortcuts::optionHeroBoat(EPathfindingLayer layer)
  585. {
  586. const CGHeroInstance *hero = GAME->interface()->localState->getCurrentHero();
  587. return optionInMapView() && hero && hero->inBoat() && hero->getBoat()->layer == layer;
  588. }
  589. bool AdventureMapShortcuts::optionHeroDig()
  590. {
  591. auto hero = GAME->interface()->localState->getCurrentHero();
  592. return optionInMapView() && hero && hero->diggingStatus() == EDiggingStatus::CAN_DIG;
  593. }
  594. bool AdventureMapShortcuts::optionViewStatistic()
  595. {
  596. if(!GAME->interface()->makingTurn)
  597. return false;
  598. auto day = GAME->interface()->cb->getDate(Date::DAY);
  599. return optionInMapView() && day > 1;
  600. }
  601. bool AdventureMapShortcuts::optionIsLocal()
  602. {
  603. if (!optionInMapView() || !GAME->server().isHost() || !(GAME->server().serverMode == EServerMode::LOCAL))
  604. return false;
  605. //exclude local multiplayer games (hot seat is ok)
  606. auto hostClientId = GAME->server().hostClientId;
  607. for(const auto& playerName : GAME->server().playerNames)
  608. {
  609. if(playerName.second.connection != hostClientId)
  610. return false;
  611. }
  612. return true;
  613. }
  614. bool AdventureMapShortcuts::optionQuickSaveLoad()
  615. {
  616. return optionIsLocal() && GAME->interface()->hasQuickSave;
  617. }