AdventureMapShortcuts.cpp 12 KB

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