AdventureMapShortcuts.cpp 15 KB

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