AdventureMapShortcuts.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 "CAdventureMapInterface.h"
  24. #include "CAdventureOptions.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(CAdventureMapInterface & 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. if(mapLevel == newMapLevel)
  45. return;
  46. mapLevel = newMapLevel;
  47. }
  48. std::vector<AdventureMapShortcutState> AdventureMapShortcuts::getShortcuts()
  49. {
  50. std::vector<AdventureMapShortcutState> result = {
  51. { EShortcut::ADVENTURE_KINGDOM_OVERVIEW, optionInMapView(), [this]() { this->showOverview(); } },
  52. { EShortcut::ADVENTURE_EXIT_WORLD_VIEW, optionInWorldView(), [this]() { this->worldViewBack(); } },
  53. { EShortcut::ADVENTURE_VIEW_WORLD, optionInMapView(), [this]() { this->worldViewScale1x(); } },
  54. { EShortcut::ADVENTURE_VIEW_WORLD_X1, optionInWorldView(), [this]() { this->worldViewScale1x(); } },
  55. { EShortcut::ADVENTURE_VIEW_WORLD_X2, optionInWorldView(), [this]() { this->worldViewScale2x(); } },
  56. { EShortcut::ADVENTURE_VIEW_WORLD_X4, optionInWorldView(), [this]() { this->worldViewScale4x(); } },
  57. { EShortcut::ADVENTURE_TOGGLE_MAP_LEVEL, optionCanToggleLevel(), [this]() { this->switchMapLevel(); } },
  58. { EShortcut::ADVENTURE_QUEST_LOG, optionCanViewQuests(), [this]() { this->showQuestlog(); } },
  59. { EShortcut::ADVENTURE_TOGGLE_SLEEP, optionHeroSelected(), [this]() { this->toggleSleepWake(); } },
  60. { EShortcut::ADVENTURE_SET_HERO_ASLEEP, optionHeroAwake(), [this]() { this->setHeroSleeping(); } },
  61. { EShortcut::ADVENTURE_SET_HERO_AWAKE, optionHeroSleeping(), [this]() { this->setHeroAwake(); } },
  62. { EShortcut::ADVENTURE_MOVE_HERO, optionHeroCanMove(), [this]() { this->moveHeroAlongPath(); } },
  63. { EShortcut::ADVENTURE_CAST_SPELL, optionHeroSelected(), [this]() { this->showSpellbook(); } },
  64. { EShortcut::ADVENTURE_GAME_OPTIONS, optionInMapView(), [this]() { this->adventureOptions(); } },
  65. { EShortcut::GLOBAL_OPTIONS, optionInMapView(), [this]() { this->systemOptions(); } },
  66. { EShortcut::ADVENTURE_NEXT_HERO, optionHasNextHero(), [this]() { this->nextHero(); } },
  67. { EShortcut::GAME_END_TURN, optionInMapView(), [this]() { this->endTurn(); } },
  68. { EShortcut::ADVENTURE_THIEVES_GUILD, optionInMapView(), [this]() { this->showThievesGuild(); } },
  69. { EShortcut::ADVENTURE_VIEW_SCENARIO, optionInMapView(), [this]() { this->showScenarioInfo(); } },
  70. { EShortcut::GAME_SAVE_GAME, optionInMapView(), [this]() { this->saveGame(); } },
  71. { EShortcut::GAME_LOAD_GAME, optionInMapView(), [this]() { this->loadGame(); } },
  72. { EShortcut::ADVENTURE_DIG_GRAIL, optionHeroSelected(), [this]() { this->digGrail(); } },
  73. { EShortcut::ADVENTURE_VIEW_PUZZLE, optionSidePanelActive(),[this]() { this->viewPuzzleMap(); } },
  74. { EShortcut::GAME_RESTART_GAME, optionInMapView(), [this]() { this->restartGame(); } },
  75. { EShortcut::ADVENTURE_VISIT_OBJECT, optionHeroSelected(), [this]() { this->visitObject(); } },
  76. { EShortcut::ADVENTURE_VIEW_SELECTED, optionInMapView(), [this]() { this->openObject(); } },
  77. { EShortcut::GLOBAL_CANCEL, optionSpellcasting(), [this]() { this->abortSpellcasting(); } },
  78. { EShortcut::GAME_OPEN_MARKETPLACE, optionInMapView(), [this]() { this->showMarketplace(); } },
  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. // with support for future multi-level maps :)
  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. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  131. if (!h)
  132. return;
  133. bool newSleep = !LOCPLINT->localState->isHeroSleeping(h);
  134. if (newSleep)
  135. LOCPLINT->localState->setHeroAsleep(h);
  136. else
  137. LOCPLINT->localState->setHeroAwaken(h);
  138. owner.onHeroChanged(h);
  139. if (newSleep)
  140. nextHero();
  141. }
  142. void AdventureMapShortcuts::setHeroSleeping()
  143. {
  144. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  145. if (h)
  146. {
  147. LOCPLINT->localState->setHeroAsleep(h);
  148. owner.onHeroChanged(h);
  149. nextHero();
  150. }
  151. }
  152. void AdventureMapShortcuts::setHeroAwake()
  153. {
  154. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  155. if (h)
  156. {
  157. LOCPLINT->localState->setHeroAwaken(h);
  158. owner.onHeroChanged(h);
  159. }
  160. }
  161. void AdventureMapShortcuts::moveHeroAlongPath()
  162. {
  163. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  164. if (!h || !LOCPLINT->localState->hasPath(h))
  165. return;
  166. LOCPLINT->moveHero(h, LOCPLINT->localState->getPath(h));
  167. }
  168. void AdventureMapShortcuts::showSpellbook()
  169. {
  170. if (!LOCPLINT->localState->getCurrentHero()) //checking necessary values
  171. return;
  172. owner.centerOnObject(LOCPLINT->localState->getCurrentHero());
  173. GH.pushIntT<CSpellWindow>(LOCPLINT->localState->getCurrentHero(), LOCPLINT, false);
  174. }
  175. void AdventureMapShortcuts::adventureOptions()
  176. {
  177. GH.pushIntT<CAdventureOptions>();
  178. }
  179. void AdventureMapShortcuts::systemOptions()
  180. {
  181. GH.pushIntT<SettingsMainWindow>();
  182. }
  183. void AdventureMapShortcuts::nextHero()
  184. {
  185. const auto * currHero = LOCPLINT->localState->getCurrentHero();
  186. const auto * nextHero = LOCPLINT->localState->getNextWanderingHero(currHero);
  187. if (nextHero)
  188. {
  189. LOCPLINT->localState->setSelection(nextHero);
  190. owner.centerOnObject(nextHero);
  191. }
  192. }
  193. void AdventureMapShortcuts::endTurn()
  194. {
  195. if(!LOCPLINT->makingTurn)
  196. return;
  197. if(settings["adventure"]["heroReminder"].Bool())
  198. {
  199. for(auto hero : LOCPLINT->localState->getWanderingHeroes())
  200. {
  201. if(!LOCPLINT->localState->isHeroSleeping(hero) && hero->movement > 0)
  202. {
  203. // Only show hero reminder if conditions met:
  204. // - There still movement points
  205. // - Hero don't have a path or there not points for first step on path
  206. LOCPLINT->localState->verifyPath(hero);
  207. if(!LOCPLINT->localState->hasPath(hero))
  208. {
  209. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  210. return;
  211. }
  212. auto path = LOCPLINT->localState->getPath(hero);
  213. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  214. {
  215. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], [this](){ owner.hotkeyEndingTurn(); }, nullptr);
  216. return;
  217. }
  218. }
  219. }
  220. }
  221. owner.hotkeyEndingTurn();
  222. }
  223. void AdventureMapShortcuts::showThievesGuild()
  224. {
  225. //find first town with tavern
  226. auto itr = range::find_if(LOCPLINT->localState->getOwnedTowns(), [](const CGTownInstance * town)
  227. {
  228. return town->hasBuilt(BuildingID::TAVERN);
  229. });
  230. if(itr != LOCPLINT->localState->getOwnedTowns().end())
  231. LOCPLINT->showThievesGuildWindow(*itr);
  232. else
  233. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  234. }
  235. void AdventureMapShortcuts::showScenarioInfo()
  236. {
  237. CAdventureOptions::showScenarioInfo();
  238. }
  239. void AdventureMapShortcuts::saveGame()
  240. {
  241. GH.pushIntT<CSavingScreen>();
  242. }
  243. void AdventureMapShortcuts::loadGame()
  244. {
  245. LOCPLINT->proposeLoadingGame();
  246. }
  247. void AdventureMapShortcuts::digGrail()
  248. {
  249. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  250. if(h && LOCPLINT->makingTurn)
  251. LOCPLINT->tryDiggging(h);
  252. }
  253. void AdventureMapShortcuts::viewPuzzleMap()
  254. {
  255. LOCPLINT->showPuzzleMap();
  256. }
  257. void AdventureMapShortcuts::restartGame()
  258. {
  259. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  260. [](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
  261. }
  262. void AdventureMapShortcuts::visitObject()
  263. {
  264. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  265. if(h)
  266. LOCPLINT->cb->moveHero(h,h->pos);
  267. }
  268. void AdventureMapShortcuts::openObject()
  269. {
  270. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  271. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown();
  272. if(h)
  273. LOCPLINT->openHeroWindow(h);
  274. if(t)
  275. LOCPLINT->openTownWindow(t);
  276. }
  277. void AdventureMapShortcuts::abortSpellcasting()
  278. {
  279. owner.hotkeyAbortCastingMode();
  280. }
  281. void AdventureMapShortcuts::showMarketplace()
  282. {
  283. //check if we have any marketplace
  284. const CGTownInstance *townWithMarket = nullptr;
  285. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  286. {
  287. if(t->hasBuilt(BuildingID::MARKETPLACE))
  288. {
  289. townWithMarket = t;
  290. break;
  291. }
  292. }
  293. if(townWithMarket) //if any town has marketplace, open window
  294. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  295. else //if not - complain
  296. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  297. }
  298. void AdventureMapShortcuts::nextTown()
  299. {
  300. owner.hotkeyNextTown();
  301. }
  302. void AdventureMapShortcuts::nextObject()
  303. {
  304. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  305. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown();
  306. if(h)
  307. nextHero();
  308. if(t)
  309. nextTown();
  310. }
  311. void AdventureMapShortcuts::moveHeroDirectional(const Point & direction)
  312. {
  313. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  314. if(!h)
  315. return;
  316. if (CGI->mh->hasOngoingAnimations())
  317. return;
  318. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  319. if (!CGI->mh->isInMap((dst)))
  320. return;
  321. if ( !LOCPLINT->localState->setPath(h, dst))
  322. return;
  323. const CGPath & path = LOCPLINT->localState->getPath(h);
  324. if (path.nodes.size() > 2)
  325. owner.onHeroChanged(h);
  326. else
  327. if(path.nodes[0].turns == 0)
  328. LOCPLINT->moveHero(h, path);
  329. }
  330. bool AdventureMapShortcuts::optionCanViewQuests()
  331. {
  332. return optionInMapView() && CGI->mh->getMap()->quests.empty();
  333. }
  334. bool AdventureMapShortcuts::optionCanToggleLevel()
  335. {
  336. return optionInMapView() && LOCPLINT->cb->getMapSize().z > 0;
  337. }
  338. bool AdventureMapShortcuts::optionMapLevelSurface()
  339. {
  340. return mapLevel == 0;
  341. }
  342. bool AdventureMapShortcuts::optionHeroSleeping()
  343. {
  344. const CGHeroInstance *hero = LOCPLINT->localState->getCurrentHero();
  345. return optionInMapView() && hero && LOCPLINT->localState->isHeroSleeping(hero);
  346. }
  347. bool AdventureMapShortcuts::optionHeroAwake()
  348. {
  349. const CGHeroInstance *hero = LOCPLINT->localState->getCurrentHero();
  350. return optionInMapView() && hero && !LOCPLINT->localState->isHeroSleeping(hero);
  351. }
  352. bool AdventureMapShortcuts::optionHeroSelected()
  353. {
  354. return optionInMapView() && LOCPLINT->localState->getCurrentHero() != nullptr;
  355. }
  356. bool AdventureMapShortcuts::optionHeroCanMove()
  357. {
  358. const auto * hero = LOCPLINT->localState->getCurrentHero();
  359. return optionInMapView() && hero && hero->movement != 0 && LOCPLINT->localState->hasPath(hero);
  360. }
  361. bool AdventureMapShortcuts::optionHasNextHero()
  362. {
  363. const auto * hero = LOCPLINT->localState->getCurrentHero();
  364. const auto * nextSuitableHero = LOCPLINT->localState->getNextWanderingHero(hero);
  365. return optionInMapView() && nextSuitableHero != nullptr;
  366. }
  367. bool AdventureMapShortcuts::optionSpellcasting()
  368. {
  369. return state == EAdventureState::CASTING_SPELL;
  370. }
  371. bool AdventureMapShortcuts::optionInMapView()
  372. {
  373. return state == EAdventureState::MAKING_TURN;
  374. }
  375. bool AdventureMapShortcuts::optionInWorldView()
  376. {
  377. return state == EAdventureState::WORLD_VIEW;
  378. }
  379. bool AdventureMapShortcuts::optionSidePanelActive()
  380. {
  381. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW;
  382. }
  383. bool AdventureMapShortcuts::optionMapViewActive()
  384. {
  385. return state == EAdventureState::MAKING_TURN || state == EAdventureState::WORLD_VIEW || state == EAdventureState::CASTING_SPELL;
  386. }