AdventureMapShortcuts.cpp 14 KB

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