AdventureMapShortcuts.cpp 15 KB

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