AdventureMapShortcuts.cpp 13 KB

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