AdventureMapShortcuts.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "../windows/CKingdomInterface.h"
  13. #include "../windows/CSpellWindow.h"
  14. #include "../windows/CTradeWindow.h"
  15. #include "../lobby/CSavingScreen.h"
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/Shortcut.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../PlayerLocalState.h"
  20. #include "../CGameInfo.h"
  21. #include "CAdventureMapInterface.h"
  22. #include "CAdventureOptions.h"
  23. #include "../windows/settings/SettingsMainWindow.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/CGeneralTextHandler.h"
  27. #include "../../lib/mapObjects/CGHeroInstance.h"
  28. #include "../../lib/mapObjects/CGTownInstance.h"
  29. #include "../../lib/CPathfinder.h"
  30. AdventureMapShortcuts::AdventureMapShortcuts(CAdventureMapInterface & owner)
  31. :owner(owner)
  32. {}
  33. std::map<EShortcut, std::function<void()>> AdventureMapShortcuts::getFunctors()
  34. {
  35. std::map<EShortcut, std::function<void()>> result = {
  36. { EShortcut::ADVENTURE_KINGDOM_OVERVIEW, [this]() { this->showOverview(); } },
  37. { EShortcut::NONE, [this]() { this->worldViewBack(); } },
  38. { EShortcut::NONE, [this]() { this->worldViewScale1x(); } },
  39. { EShortcut::NONE, [this]() { this->worldViewScale2x(); } },
  40. { EShortcut::NONE, [this]() { this->worldViewScale4x(); } },
  41. { EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL, [this]() { this->switchMapLevel(); } },
  42. { EShortcut::ADVENTURE_QUEST_LOG, [this]() { this->showQuestlog(); } },
  43. { EShortcut::ADVENTURE_TOGGLE_SLEEP, [this]() { this->toggleSleepWake(); } },
  44. { EShortcut::ADVENTURE_SET_HERO_ASLEEP, [this]() { this->setHeroSleeping(); } },
  45. { EShortcut::ADVENTURE_SET_HERO_AWAKE, [this]() { this->setHeroAwake(); } },
  46. { EShortcut::ADVENTURE_MOVE_HERO, [this]() { this->moveHeroAlongPath(); } },
  47. { EShortcut::ADVENTURE_CAST_SPELL, [this]() { this->showSpellbook(); } },
  48. { EShortcut::ADVENTURE_GAME_OPTIONS, [this]() { this->adventureOptions(); } },
  49. { EShortcut::GLOBAL_OPTIONS, [this]() { this->systemOptions(); } },
  50. { EShortcut::ADVENTURE_NEXT_HERO, [this]() { this->nextHero(); } },
  51. { EShortcut::GAME_END_TURN, [this]() { this->endTurn(); } },
  52. { EShortcut::ADVENTURE_THIEVES_GUILD, [this]() { this->showThievesGuild(); } },
  53. { EShortcut::ADVENTURE_VIEW_SCENARIO, [this]() { this->showScenarioInfo(); } },
  54. { EShortcut::GAME_SAVE_GAME, [this]() { this->saveGame(); } },
  55. { EShortcut::GAME_LOAD_GAME, [this]() { this->loadGame(); } },
  56. { EShortcut::ADVENTURE_DIG_GRAIL, [this]() { this->digGrail(); } },
  57. { EShortcut::ADVENTURE_VIEW_PUZZLE, [this]() { this->viewPuzzleMap(); } },
  58. { EShortcut::ADVENTURE_VIEW_WORLD, [this]() { this->viewWorldMap(); } },
  59. { EShortcut::GAME_RESTART_GAME, [this]() { this->restartGame(); } },
  60. { EShortcut::ADVENTURE_VISIT_OBJECT, [this]() { this->visitObject(); } },
  61. { EShortcut::ADVENTURE_VIEW_SELECTED, [this]() { this->openObject(); } },
  62. { EShortcut::GLOBAL_CANCEL, [this]() { this->abortSpellcasting(); } },
  63. { EShortcut::GAME_OPEN_MARKETPLACE, [this]() { this->showMarketplace(); } },
  64. { EShortcut::ADVENTURE_NEXT_TOWN, [this]() { this->nextTown(); } },
  65. // { EShortcut::ADVENTURE_NEXT_OBJECT, [this]() { this->nextObject(); } },
  66. { EShortcut::ADVENTURE_MOVE_HERO_SW, [this]() { this->moveHeroDirectional({-1, +1}); } },
  67. { EShortcut::ADVENTURE_MOVE_HERO_SS, [this]() { this->moveHeroDirectional({ 0, +1}); } },
  68. { EShortcut::ADVENTURE_MOVE_HERO_SE, [this]() { this->moveHeroDirectional({+1, +1}); } },
  69. { EShortcut::ADVENTURE_MOVE_HERO_WW, [this]() { this->moveHeroDirectional({-1, 0}); } },
  70. { EShortcut::ADVENTURE_MOVE_HERO_EE, [this]() { this->moveHeroDirectional({+1, 0}); } },
  71. { EShortcut::ADVENTURE_MOVE_HERO_NW, [this]() { this->moveHeroDirectional({-1, -1}); } },
  72. { EShortcut::ADVENTURE_MOVE_HERO_NN, [this]() { this->moveHeroDirectional({ 0, -1}); } },
  73. { EShortcut::ADVENTURE_MOVE_HERO_NE, [this]() { this->moveHeroDirectional({+1, -1}); } },
  74. };
  75. return result;
  76. }
  77. void AdventureMapShortcuts::showOverview()
  78. {
  79. GH.pushIntT<CKingdomInterface>();
  80. }
  81. void AdventureMapShortcuts::worldViewBack()
  82. {
  83. owner.hotkeyExitWorldView();
  84. auto hero = LOCPLINT->localState->getCurrentHero();
  85. if (hero)
  86. owner.centerOnObject(hero);
  87. }
  88. void AdventureMapShortcuts::worldViewScale1x()
  89. {
  90. // TODO set corresponding scale button to "selected" mode
  91. owner.openWorldView(7);
  92. }
  93. void AdventureMapShortcuts::worldViewScale2x()
  94. {
  95. owner.openWorldView(11);
  96. }
  97. void AdventureMapShortcuts::worldViewScale4x()
  98. {
  99. owner.openWorldView(16);
  100. }
  101. void AdventureMapShortcuts::switchMapLevel()
  102. {
  103. // with support for future multi-level maps :)
  104. int maxLevels = LOCPLINT->cb->getMapSize().z;
  105. if (maxLevels < 2)
  106. return;
  107. owner.hotkeySwitchMapLevel();
  108. }
  109. void AdventureMapShortcuts::showQuestlog()
  110. {
  111. LOCPLINT->showQuestLog();
  112. }
  113. void AdventureMapShortcuts::toggleSleepWake()
  114. {
  115. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  116. if (!h)
  117. return;
  118. bool newSleep = !LOCPLINT->localState->isHeroSleeping(h);
  119. if (newSleep)
  120. LOCPLINT->localState->setHeroAsleep(h);
  121. else
  122. LOCPLINT->localState->setHeroAwaken(h);
  123. owner.onHeroChanged(h);
  124. if (newSleep)
  125. nextHero();
  126. }
  127. void AdventureMapShortcuts::setHeroSleeping()
  128. {
  129. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  130. if (h)
  131. {
  132. LOCPLINT->localState->setHeroAsleep(h);
  133. owner.onHeroChanged(h);
  134. nextHero();
  135. }
  136. }
  137. void AdventureMapShortcuts::setHeroAwake()
  138. {
  139. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  140. if (h)
  141. {
  142. LOCPLINT->localState->setHeroAsleep(h);
  143. owner.onHeroChanged(h);
  144. }
  145. }
  146. void AdventureMapShortcuts::moveHeroAlongPath()
  147. {
  148. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  149. if (!h || !LOCPLINT->localState->hasPath(h))
  150. return;
  151. LOCPLINT->moveHero(h, LOCPLINT->localState->getPath(h));
  152. }
  153. void AdventureMapShortcuts::showSpellbook()
  154. {
  155. if (!LOCPLINT->localState->getCurrentHero()) //checking necessary values
  156. return;
  157. owner.centerOnObject(LOCPLINT->localState->getCurrentHero());
  158. GH.pushIntT<CSpellWindow>(LOCPLINT->localState->getCurrentHero(), LOCPLINT, false);
  159. }
  160. void AdventureMapShortcuts::adventureOptions()
  161. {
  162. GH.pushIntT<CAdventureOptions>();
  163. }
  164. void AdventureMapShortcuts::systemOptions()
  165. {
  166. GH.pushIntT<SettingsMainWindow>();
  167. }
  168. void AdventureMapShortcuts::nextHero()
  169. {
  170. const auto * currHero = LOCPLINT->localState->getCurrentHero();
  171. const auto * nextHero = LOCPLINT->localState->getNextWanderingHero(currHero);
  172. if (nextHero)
  173. {
  174. LOCPLINT->localState->setSelection(nextHero);
  175. owner.centerOnObject(nextHero);
  176. }
  177. }
  178. void AdventureMapShortcuts::endTurn()
  179. {
  180. if(!LOCPLINT->makingTurn)
  181. return;
  182. if(settings["adventure"]["heroReminder"].Bool())
  183. {
  184. for(auto hero : LOCPLINT->localState->getWanderingHeroes())
  185. {
  186. if(!LOCPLINT->localState->isHeroSleeping(hero) && hero->movement > 0)
  187. {
  188. // Only show hero reminder if conditions met:
  189. // - There still movement points
  190. // - Hero don't have a path or there not points for first step on path
  191. LOCPLINT->localState->verifyPath(hero);
  192. if(!LOCPLINT->localState->hasPath(hero))
  193. {
  194. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  195. return;
  196. }
  197. auto path = LOCPLINT->localState->getPath(hero);
  198. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  199. {
  200. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  201. return;
  202. }
  203. }
  204. }
  205. }
  206. owner.hotkeyEndingTurn();
  207. }
  208. void AdventureMapShortcuts::showThievesGuild()
  209. {
  210. //find first town with tavern
  211. auto itr = range::find_if(LOCPLINT->localState->getOwnedTowns(), [](const CGTownInstance * town)
  212. {
  213. return town->hasBuilt(BuildingID::TAVERN);
  214. });
  215. if(itr != LOCPLINT->localState->getOwnedTowns().end())
  216. LOCPLINT->showThievesGuildWindow(*itr);
  217. else
  218. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  219. }
  220. void AdventureMapShortcuts::showScenarioInfo()
  221. {
  222. CAdventureOptions::showScenarioInfo();
  223. }
  224. void AdventureMapShortcuts::saveGame()
  225. {
  226. GH.pushIntT<CSavingScreen>();
  227. }
  228. void AdventureMapShortcuts::loadGame()
  229. {
  230. LOCPLINT->proposeLoadingGame();
  231. }
  232. void AdventureMapShortcuts::digGrail()
  233. {
  234. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  235. if(h && LOCPLINT->makingTurn)
  236. LOCPLINT->tryDiggging(h);
  237. return;
  238. }
  239. void AdventureMapShortcuts::viewPuzzleMap()
  240. {
  241. LOCPLINT->showPuzzleMap();
  242. }
  243. void AdventureMapShortcuts::viewWorldMap()
  244. {
  245. LOCPLINT->viewWorldMap();
  246. }
  247. void AdventureMapShortcuts::restartGame()
  248. {
  249. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  250. [](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
  251. }
  252. void AdventureMapShortcuts::visitObject()
  253. {
  254. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  255. if(h)
  256. LOCPLINT->cb->moveHero(h,h->pos);
  257. }
  258. void AdventureMapShortcuts::openObject()
  259. {
  260. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  261. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown();
  262. if(h)
  263. LOCPLINT->openHeroWindow(h);
  264. if(t)
  265. LOCPLINT->openTownWindow(t);
  266. return;
  267. }
  268. void AdventureMapShortcuts::abortSpellcasting()
  269. {
  270. owner.hotkeyAbortCastingMode();
  271. }
  272. void AdventureMapShortcuts::showMarketplace()
  273. {
  274. //check if we have any marketplace
  275. const CGTownInstance *townWithMarket = nullptr;
  276. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  277. {
  278. if(t->hasBuilt(BuildingID::MARKETPLACE))
  279. {
  280. townWithMarket = t;
  281. break;
  282. }
  283. }
  284. if(townWithMarket) //if any town has marketplace, open window
  285. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  286. else //if not - complain
  287. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  288. }
  289. void AdventureMapShortcuts::nextTown()
  290. {
  291. owner.hotkeyEndingTurn();
  292. }
  293. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  294. {
  295. owner.hotkeyMoveHeroDirectional(direction);
  296. }