AdventureMapShortcuts.cpp 14 KB

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