AdventureMapShortcuts.cpp 24 KB

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