AdventureMapShortcuts.cpp 14 KB

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