AdventureMapShortcuts.cpp 14 KB

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