2
0

AdventureMapShortcuts.cpp 14 KB

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