CAdventureMapInterface.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * CAdventureMapInterface.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 "CAdventureMapInterface.h"
  12. #include "CAdventureOptions.h"
  13. #include "CInGameConsole.h"
  14. #include "CMinimap.h"
  15. #include "CList.h"
  16. #include "CInfoBar.h"
  17. #include "MapAudioPlayer.h"
  18. #include "CAdventureMapWidget.h"
  19. #include "../mapView/mapHandler.h"
  20. #include "../mapView/MapView.h"
  21. #include "../windows/CKingdomInterface.h"
  22. #include "../windows/CSpellWindow.h"
  23. #include "../windows/CTradeWindow.h"
  24. #include "../windows/GUIClasses.h"
  25. #include "../windows/InfoWindows.h"
  26. #include "../CGameInfo.h"
  27. #include "../CPlayerInterface.h"
  28. #include "../lobby/CSavingScreen.h"
  29. #include "../render/CAnimation.h"
  30. #include "../gui/CursorHandler.h"
  31. #include "../render/IImage.h"
  32. #include "../renderSDL/SDL_Extensions.h"
  33. #include "../gui/CGuiHandler.h"
  34. #include "../gui/Shortcut.h"
  35. #include "../widgets/TextControls.h"
  36. #include "../widgets/Buttons.h"
  37. #include "../windows/settings/SettingsMainWindow.h"
  38. #include "../CMT.h"
  39. #include "../PlayerLocalState.h"
  40. #include "../../CCallback.h"
  41. #include "../../lib/CConfigHandler.h"
  42. #include "../../lib/CGeneralTextHandler.h"
  43. #include "../../lib/spells/CSpellHandler.h"
  44. #include "../../lib/mapObjects/CGHeroInstance.h"
  45. #include "../../lib/mapObjects/CGTownInstance.h"
  46. #include "../../lib/CPathfinder.h"
  47. #include "../../lib/mapping/CMap.h"
  48. std::shared_ptr<CAdventureMapInterface> adventureInt;
  49. CAdventureMapInterface::CAdventureMapInterface():
  50. mapAudio(new MapAudioPlayer()),
  51. spellBeingCasted(nullptr),
  52. scrollingCursorSet(false)
  53. {
  54. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  55. pos.x = pos.y = 0;
  56. pos.w = GH.screenDimensions().x;
  57. pos.h = GH.screenDimensions().y;
  58. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  59. widget = std::make_shared<CAdventureMapWidget>();
  60. exitWorldView();
  61. widget->setOptionHasQuests(!CGI->mh->getMap()->quests.empty());
  62. widget->setOptionHasUnderground(CGI->mh->getMap()->twoLevel);
  63. }
  64. void CAdventureMapInterface::fshowOverview()
  65. {
  66. GH.pushIntT<CKingdomInterface>();
  67. }
  68. void CAdventureMapInterface::fworldViewBack()
  69. {
  70. exitWorldView();
  71. auto hero = LOCPLINT->localState->getCurrentHero();
  72. if (hero)
  73. centerOnObject(hero);
  74. }
  75. void CAdventureMapInterface::fworldViewScale1x()
  76. {
  77. // TODO set corresponding scale button to "selected" mode
  78. openWorldView(7);
  79. }
  80. void CAdventureMapInterface::fworldViewScale2x()
  81. {
  82. openWorldView(11);
  83. }
  84. void CAdventureMapInterface::fworldViewScale4x()
  85. {
  86. openWorldView(16);
  87. }
  88. void CAdventureMapInterface::fswitchLevel()
  89. {
  90. // with support for future multi-level maps :)
  91. int maxLevels = CGI->mh->getMap()->levels();
  92. if (maxLevels < 2)
  93. return;
  94. widget->getMapView()->onMapLevelSwitched();
  95. }
  96. void CAdventureMapInterface::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  97. {
  98. widget->setOptionUndergroundLevel(mapLevel > 0);
  99. widget->getMinimap()->onMapViewMoved(visibleArea, mapLevel);
  100. }
  101. void CAdventureMapInterface::onAudioResumed()
  102. {
  103. mapAudio->onAudioResumed();
  104. }
  105. void CAdventureMapInterface::onAudioPaused()
  106. {
  107. mapAudio->onAudioPaused();
  108. }
  109. void CAdventureMapInterface::fshowQuestlog()
  110. {
  111. LOCPLINT->showQuestLog();
  112. }
  113. void CAdventureMapInterface::fsleepWake()
  114. {
  115. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  116. if (!h)
  117. return;
  118. bool newSleep = !LOCPLINT->localState->isHeroSleeping(h);
  119. if (newSleep)
  120. LOCPLINT->localState->setHeroAsleep(h);
  121. else
  122. LOCPLINT->localState->setHeroAwaken(h);
  123. onHeroChanged(h);
  124. if (newSleep)
  125. fnextHero();
  126. }
  127. void CAdventureMapInterface::fmoveHero()
  128. {
  129. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  130. if (!h || !LOCPLINT->localState->hasPath(h) || CGI->mh->hasOngoingAnimations())
  131. return;
  132. LOCPLINT->moveHero(h, LOCPLINT->localState->getPath(h));
  133. }
  134. void CAdventureMapInterface::fshowSpellbok()
  135. {
  136. if (!LOCPLINT->localState->getCurrentHero()) //checking necessary values
  137. return;
  138. centerOnObject(LOCPLINT->localState->getCurrentHero());
  139. GH.pushIntT<CSpellWindow>(LOCPLINT->localState->getCurrentHero(), LOCPLINT, false);
  140. }
  141. void CAdventureMapInterface::fadventureOPtions()
  142. {
  143. GH.pushIntT<CAdventureOptions>();
  144. }
  145. void CAdventureMapInterface::fsystemOptions()
  146. {
  147. GH.pushIntT<SettingsMainWindow>();
  148. }
  149. void CAdventureMapInterface::fnextHero()
  150. {
  151. const auto * currHero = LOCPLINT->localState->getCurrentHero();
  152. const auto * nextHero = LOCPLINT->localState->getNextWanderingHero(currHero);
  153. if (nextHero)
  154. {
  155. LOCPLINT->localState->setSelection(nextHero);
  156. centerOnObject(nextHero);
  157. }
  158. }
  159. void CAdventureMapInterface::fendTurn()
  160. {
  161. if(!LOCPLINT->makingTurn)
  162. return;
  163. if(settings["adventure"]["heroReminder"].Bool())
  164. {
  165. for(auto hero : LOCPLINT->localState->getWanderingHeroes())
  166. {
  167. if(!LOCPLINT->localState->isHeroSleeping(hero) && hero->movement > 0)
  168. {
  169. // Only show hero reminder if conditions met:
  170. // - There still movement points
  171. // - Hero don't have a path or there not points for first step on path
  172. LOCPLINT->localState->verifyPath(hero);
  173. if(!LOCPLINT->localState->hasPath(hero))
  174. {
  175. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdventureMapInterface::endingTurn, this), nullptr );
  176. return;
  177. }
  178. auto path = LOCPLINT->localState->getPath(hero);
  179. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  180. {
  181. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdventureMapInterface::endingTurn, this), nullptr );
  182. return;
  183. }
  184. }
  185. }
  186. }
  187. endingTurn();
  188. }
  189. void CAdventureMapInterface::updateButtons()
  190. {
  191. const auto * hero = LOCPLINT->localState->getCurrentHero();
  192. const auto * nextSuitableHero = LOCPLINT->localState->getNextWanderingHero(hero);
  193. widget->setOptionHeroSelected(hero != nullptr);
  194. widget->setOptionHeroCanMove(hero && LOCPLINT->localState->hasPath(hero) && hero->movement != 0);
  195. widget->setOptionHasNextHero(nextSuitableHero != nullptr);
  196. widget->setOptionHeroSleeping(hero && LOCPLINT->localState->isHeroSleeping(hero));
  197. }
  198. void CAdventureMapInterface::onHeroMovementStarted(const CGHeroInstance * hero)
  199. {
  200. widget->getInfoBar()->popAll();
  201. widget->getInfoBar()->showSelection();
  202. }
  203. void CAdventureMapInterface::onHeroChanged(const CGHeroInstance *h)
  204. {
  205. widget->getHeroList()->update(h);
  206. if (h && h == LOCPLINT->localState->getCurrentHero() && !widget->getInfoBar()->showingComponents())
  207. widget->getInfoBar()->showSelection();
  208. updateButtons();
  209. }
  210. void CAdventureMapInterface::onTownChanged(const CGTownInstance * town)
  211. {
  212. widget->getTownList()->update(town);
  213. if (town && town == LOCPLINT->localState->getCurrentTown() && !widget->getInfoBar()->showingComponents())
  214. widget->getInfoBar()->showSelection();
  215. }
  216. void CAdventureMapInterface::showInfoBoxMessage(const std::vector<Component> & components, std::string message, int timer)
  217. {
  218. widget->getInfoBar()->pushComponents(components, message, timer);
  219. }
  220. void CAdventureMapInterface::activate()
  221. {
  222. CIntObject::activate();
  223. screenBuf = screen;
  224. if(LOCPLINT)
  225. {
  226. LOCPLINT->cingconsole->activate();
  227. LOCPLINT->cingconsole->pos = this->pos;
  228. }
  229. GH.fakeMouseMove(); //to restore the cursor
  230. }
  231. void CAdventureMapInterface::deactivate()
  232. {
  233. CIntObject::deactivate();
  234. CCS->curh->set(Cursor::Map::POINTER);
  235. }
  236. void CAdventureMapInterface::showAll(SDL_Surface * to)
  237. {
  238. CIntObject::showAll(to);
  239. LOCPLINT->cingconsole->show(to);
  240. }
  241. void CAdventureMapInterface::show(SDL_Surface * to)
  242. {
  243. handleMapScrollingUpdate();
  244. CIntObject::show(to);
  245. LOCPLINT->cingconsole->show(to);
  246. }
  247. void CAdventureMapInterface::handleMapScrollingUpdate()
  248. {
  249. /// Width of window border, in pixels, that triggers map scrolling
  250. static constexpr uint32_t borderScrollWidth = 15;
  251. uint32_t timePassed = GH.mainFPSmng->getElapsedMilliseconds();
  252. uint32_t scrollSpeedPixels = settings["adventure"]["scrollSpeedPixels"].Float();
  253. uint32_t scrollDistance = scrollSpeedPixels * timePassed / 1000;
  254. bool scrollingActive = !GH.isKeyboardCtrlDown() && isActive() && widget->getState() == EGameState::MAKING_TURN;
  255. Point cursorPosition = GH.getCursorPosition();
  256. Point scrollDirection;
  257. if (cursorPosition.x < borderScrollWidth)
  258. scrollDirection.x = -1;
  259. if (cursorPosition.x > GH.screenDimensions().x - borderScrollWidth)
  260. scrollDirection.x = +1;
  261. if (cursorPosition.y < borderScrollWidth)
  262. scrollDirection.y = -1;
  263. if (cursorPosition.y > GH.screenDimensions().y - borderScrollWidth)
  264. scrollDirection.y = +1;
  265. Point scrollDelta = scrollDirection * scrollDistance;
  266. if (scrollingActive && scrollDelta != Point(0,0))
  267. widget->getMapView()->onMapScrolled(scrollDelta);
  268. if (scrollDelta == Point(0,0) && !scrollingCursorSet)
  269. return;
  270. if(scrollDelta.x > 0)
  271. {
  272. if(scrollDelta.y < 0)
  273. CCS->curh->set(Cursor::Map::SCROLL_NORTHEAST);
  274. if(scrollDelta.y > 0)
  275. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  276. if(scrollDelta.y == 0)
  277. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  278. }
  279. if(scrollDelta.x < 0)
  280. {
  281. if(scrollDelta.y < 0)
  282. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  283. if(scrollDelta.y > 0)
  284. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  285. if(scrollDelta.y == 0)
  286. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  287. }
  288. if (scrollDelta.x == 0)
  289. {
  290. if(scrollDelta.y < 0)
  291. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  292. if(scrollDelta.y > 0)
  293. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  294. if(scrollDelta.y == 0)
  295. CCS->curh->set(Cursor::Map::POINTER);
  296. }
  297. scrollingCursorSet = scrollDelta != Point(0,0);
  298. }
  299. void CAdventureMapInterface::centerOnTile(int3 on)
  300. {
  301. widget->getMapView()->onCenteredTile(on);
  302. }
  303. void CAdventureMapInterface::centerOnObject(const CGObjectInstance * obj)
  304. {
  305. widget->getMapView()->onCenteredObject(obj);
  306. }
  307. void CAdventureMapInterface::keyPressed(EShortcut key)
  308. {
  309. if (widget->getState() != EGameState::MAKING_TURN)
  310. return;
  311. //fake mouse use to trigger onTileHovered()
  312. GH.fakeMouseMove();
  313. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  314. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown(); //selected town
  315. switch(key)
  316. {
  317. case EShortcut::ADVENTURE_THIEVES_GUILD:
  318. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  319. return;
  320. {
  321. //find first town with tavern
  322. auto itr = range::find_if(LOCPLINT->localState->getOwnedTowns(), [](const CGTownInstance * town)
  323. {
  324. return town->hasBuilt(BuildingID::TAVERN);
  325. });
  326. if(itr != LOCPLINT->localState->getOwnedTowns().end())
  327. LOCPLINT->showThievesGuildWindow(*itr);
  328. else
  329. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  330. }
  331. return;
  332. case EShortcut::ADVENTURE_VIEW_SCENARIO:
  333. if(isActive())
  334. CAdventureOptions::showScenarioInfo();
  335. return;
  336. case EShortcut::GAME_SAVE_GAME:
  337. if(isActive())
  338. GH.pushIntT<CSavingScreen>();
  339. return;
  340. case EShortcut::GAME_LOAD_GAME:
  341. if(isActive())
  342. LOCPLINT->proposeLoadingGame();
  343. return;
  344. case EShortcut::ADVENTURE_DIG_GRAIL:
  345. {
  346. if(h && isActive() && LOCPLINT->makingTurn)
  347. LOCPLINT->tryDiggging(h);
  348. return;
  349. }
  350. case EShortcut::ADVENTURE_VIEW_PUZZLE:
  351. if(isActive())
  352. LOCPLINT->showPuzzleMap();
  353. return;
  354. case EShortcut::ADVENTURE_VIEW_WORLD:
  355. if(isActive())
  356. LOCPLINT->viewWorldMap();
  357. return;
  358. case EShortcut::GAME_RESTART_GAME:
  359. if(isActive() && GH.isKeyboardCtrlDown())
  360. {
  361. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  362. [](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
  363. }
  364. return;
  365. case EShortcut::ADVENTURE_VISIT_OBJECT: //space - try to revisit current object with selected hero
  366. {
  367. if(!isActive())
  368. return;
  369. if(h)
  370. {
  371. LOCPLINT->cb->moveHero(h,h->pos);
  372. }
  373. }
  374. return;
  375. case EShortcut::ADVENTURE_VIEW_SELECTED:
  376. {
  377. if(!isActive() || !LOCPLINT->localState->getCurrentArmy())
  378. return;
  379. if(h)
  380. LOCPLINT->openHeroWindow(h);
  381. else if(t)
  382. LOCPLINT->openTownWindow(t);
  383. return;
  384. }
  385. case EShortcut::GLOBAL_CANCEL:
  386. {
  387. //FIXME: this case is never executed since AdvMapInt is disabled while in spellcasting mode
  388. if(!isActive() || GH.topInt().get() != this || !spellBeingCasted)
  389. return;
  390. abortCastingMode();
  391. return;
  392. }
  393. case EShortcut::GAME_OPEN_MARKETPLACE:
  394. {
  395. //act on key down if marketplace windows is not already opened
  396. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  397. return;
  398. if(GH.isKeyboardCtrlDown()) //CTRL + T => open marketplace
  399. {
  400. //check if we have any marketplace
  401. const CGTownInstance *townWithMarket = nullptr;
  402. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  403. {
  404. if(t->hasBuilt(BuildingID::MARKETPLACE))
  405. {
  406. townWithMarket = t;
  407. break;
  408. }
  409. }
  410. if(townWithMarket) //if any town has marketplace, open window
  411. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  412. else //if not - complain
  413. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  414. }
  415. case EShortcut::ADVENTURE_NEXT_TOWN:
  416. if(isActive() && !GH.isKeyboardCtrlDown()) //no ctrl, advmapint is on the top => switch to town
  417. {
  418. widget->getTownList()->selectNext();
  419. }
  420. return;
  421. }
  422. case EShortcut::ADVENTURE_MOVE_HERO_SW: return hotkeyMoveHeroDirectional({-1, +1});
  423. case EShortcut::ADVENTURE_MOVE_HERO_SS: return hotkeyMoveHeroDirectional({ 0, +1});
  424. case EShortcut::ADVENTURE_MOVE_HERO_SE: return hotkeyMoveHeroDirectional({+1, +1});
  425. case EShortcut::ADVENTURE_MOVE_HERO_WW: return hotkeyMoveHeroDirectional({-1, 0});
  426. case EShortcut::ADVENTURE_MOVE_HERO_EE: return hotkeyMoveHeroDirectional({+1, 0});
  427. case EShortcut::ADVENTURE_MOVE_HERO_NW: return hotkeyMoveHeroDirectional({-1, -1});
  428. case EShortcut::ADVENTURE_MOVE_HERO_NN: return hotkeyMoveHeroDirectional({ 0, -1});
  429. case EShortcut::ADVENTURE_MOVE_HERO_NE: return hotkeyMoveHeroDirectional({+1, -1});
  430. }
  431. }
  432. void CAdventureMapInterface::hotkeyMoveHeroDirectional(Point direction)
  433. {
  434. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  435. if(!h || !isActive())
  436. return;
  437. if (CGI->mh->hasOngoingAnimations())
  438. return;
  439. if(direction == Point(0,0))
  440. {
  441. centerOnObject(h);
  442. return;
  443. }
  444. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  445. if (!CGI->mh->isInMap((dst)))
  446. return;
  447. if ( !LOCPLINT->localState->setPath(h, dst))
  448. return;
  449. const CGPath & path = LOCPLINT->localState->getPath(h);
  450. if (path.nodes.size() > 2)
  451. onHeroChanged(h);
  452. else
  453. if(!path.nodes[0].turns)
  454. LOCPLINT->moveHero(h, path);
  455. }
  456. void CAdventureMapInterface::onSelectionChanged(const CArmedInstance *sel)
  457. {
  458. assert(sel);
  459. widget->getInfoBar()->popAll();
  460. mapAudio->onSelectionChanged(sel);
  461. bool centerView = !settings["session"]["autoSkip"].Bool();
  462. if (centerView)
  463. centerOnObject(sel);
  464. if(sel->ID==Obj::TOWN)
  465. {
  466. auto town = dynamic_cast<const CGTownInstance*>(sel);
  467. widget->getInfoBar()->showTownSelection(town);
  468. widget->getTownList()->select(town);
  469. widget->getHeroList()->select(nullptr);
  470. onHeroChanged(nullptr);
  471. }
  472. else //hero selected
  473. {
  474. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  475. widget->getInfoBar()->showHeroSelection(hero);
  476. widget->getHeroList()->select(hero);
  477. widget->getTownList()->select(nullptr);
  478. LOCPLINT->localState->verifyPath(hero);
  479. onHeroChanged(hero);
  480. }
  481. updateButtons();
  482. widget->getHeroList()->redraw();
  483. widget->getTownList()->redraw();
  484. }
  485. bool CAdventureMapInterface::isActive()
  486. {
  487. return active & ~CIntObject::KEYBOARD;
  488. }
  489. void CAdventureMapInterface::onMapTilesChanged(boost::optional<std::unordered_set<int3>> positions)
  490. {
  491. if (positions)
  492. widget->getMinimap()->updateTiles(*positions);
  493. else
  494. widget->getMinimap()->update();
  495. }
  496. void CAdventureMapInterface::onHotseatWaitStarted(PlayerColor playerID)
  497. {
  498. onCurrentPlayerChanged(playerID);
  499. widget->setState(EGameState::HOTSEAT_WAIT);
  500. }
  501. void CAdventureMapInterface::onEnemyTurnStarted(PlayerColor playerID)
  502. {
  503. if(settings["session"]["spectate"].Bool())
  504. return;
  505. adjustActiveness(true);
  506. mapAudio->onEnemyTurnStarted();
  507. widget->getMinimap()->setAIRadar(true);
  508. widget->getInfoBar()->startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  509. widget->getMinimap()->showAll(screen);//force refresh on inactive object
  510. widget->getInfoBar()->showAll(screen);//force refresh on inactive object
  511. }
  512. void CAdventureMapInterface::adjustActiveness(bool aiTurnStart)
  513. {
  514. bool wasActive = isActive();
  515. if(wasActive)
  516. deactivate();
  517. if (aiTurnStart)
  518. widget->setState(EGameState::ENEMY_TURN);
  519. else
  520. widget->setState(EGameState::MAKING_TURN);
  521. if(wasActive)
  522. activate();
  523. }
  524. void CAdventureMapInterface::onCurrentPlayerChanged(PlayerColor playerID)
  525. {
  526. LOCPLINT->localState->setSelection(nullptr);
  527. if (playerID == currentPlayerID)
  528. return;
  529. currentPlayerID = playerID;
  530. widget->setPlayer(playerID);
  531. }
  532. void CAdventureMapInterface::onPlayerTurnStarted(PlayerColor playerID)
  533. {
  534. onCurrentPlayerChanged(playerID);
  535. widget->setState(EGameState::MAKING_TURN);
  536. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  537. || settings["session"]["spectate"].Bool())
  538. {
  539. adjustActiveness(false);
  540. widget->getMinimap()->setAIRadar(false);
  541. widget->getInfoBar()->showSelection();
  542. }
  543. widget->getHeroList()->update();
  544. widget->getTownList()->update();
  545. const CGHeroInstance * heroToSelect = nullptr;
  546. // find first non-sleeping hero
  547. for (auto hero : LOCPLINT->localState->getWanderingHeroes())
  548. {
  549. if (!LOCPLINT->localState->isHeroSleeping(hero))
  550. {
  551. heroToSelect = hero;
  552. break;
  553. }
  554. }
  555. //select first hero if available.
  556. if (heroToSelect != nullptr)
  557. {
  558. LOCPLINT->localState->setSelection(heroToSelect);
  559. }
  560. else if (LOCPLINT->localState->getOwnedTowns().size())
  561. {
  562. LOCPLINT->localState->setSelection(LOCPLINT->localState->getOwnedTown(0));
  563. }
  564. else
  565. {
  566. LOCPLINT->localState->setSelection(LOCPLINT->localState->getWanderingHero(0));
  567. }
  568. //show new day animation and sound on infobar
  569. widget->getInfoBar()->showDate();
  570. onHeroChanged(nullptr);
  571. showAll(screen);
  572. mapAudio->onPlayerTurnStarted();
  573. if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown())
  574. {
  575. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt().get()))
  576. iw->close();
  577. endingTurn();
  578. }
  579. }
  580. void CAdventureMapInterface::endingTurn()
  581. {
  582. if(settings["session"]["spectate"].Bool())
  583. return;
  584. LOCPLINT->makingTurn = false;
  585. LOCPLINT->cb->endTurn();
  586. mapAudio->onPlayerTurnEnded();
  587. }
  588. const CGObjectInstance* CAdventureMapInterface::getActiveObject(const int3 &mapPos)
  589. {
  590. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  591. if (bobjs.empty())
  592. return nullptr;
  593. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  594. /*
  595. if (bobjs.back()->ID == Obj::HERO)
  596. return bobjs.back();
  597. else
  598. return bobjs.front();*/
  599. }
  600. void CAdventureMapInterface::onTileLeftClicked(const int3 &mapPos)
  601. {
  602. if(widget->getState() == EGameState::MAKING_TURN)
  603. return;
  604. //FIXME: this line breaks H3 behavior for Dimension Door
  605. if(!LOCPLINT->cb->isVisible(mapPos))
  606. return;
  607. if(!LOCPLINT->makingTurn)
  608. return;
  609. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  610. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  611. int3 selPos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  612. if(spellBeingCasted)
  613. {
  614. if (!isInScreenRange(selPos, mapPos))
  615. return;
  616. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  617. switch(spellBeingCasted->id)
  618. {
  619. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  620. if(topBlocking && topBlocking->ID == Obj::BOAT)
  621. performSpellcasting(mapPos);
  622. break;
  623. case SpellID::DIMENSION_DOOR:
  624. if(!tile || tile->isClear(heroTile))
  625. performSpellcasting(mapPos);
  626. break;
  627. }
  628. return;
  629. }
  630. //check if we can select this object
  631. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  632. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  633. bool isHero = false;
  634. if(LOCPLINT->localState->getCurrentArmy()->ID != Obj::HERO) //hero is not selected (presumably town)
  635. {
  636. if(LOCPLINT->localState->getCurrentArmy() == topBlocking) //selected town clicked
  637. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  638. else if(canSelect)
  639. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  640. }
  641. else if(const CGHeroInstance * currentHero = LOCPLINT->localState->getCurrentHero()) //hero is selected
  642. {
  643. isHero = true;
  644. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  645. if(currentHero == topBlocking) //clicked selected hero
  646. {
  647. LOCPLINT->openHeroWindow(currentHero);
  648. return;
  649. }
  650. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  651. {
  652. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  653. return;
  654. }
  655. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  656. {
  657. if(LOCPLINT->localState->hasPath(currentHero) &&
  658. LOCPLINT->localState->getPath(currentHero).endPos() == mapPos)//we'll be moving
  659. {
  660. if(!CGI->mh->hasOngoingAnimations())
  661. LOCPLINT->moveHero(currentHero, LOCPLINT->localState->getPath(currentHero));
  662. return;
  663. }
  664. else //remove old path and find a new one if we clicked on accessible tile
  665. {
  666. LOCPLINT->localState->setPath(currentHero, mapPos);
  667. onHeroChanged(currentHero);
  668. }
  669. }
  670. } //end of hero is selected "case"
  671. else
  672. {
  673. throw std::runtime_error("Nothing is selected...");
  674. }
  675. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  676. if(isHero && shipyard != nullptr)
  677. {
  678. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  679. }
  680. }
  681. void CAdventureMapInterface::onTileHovered(const int3 &mapPos)
  682. {
  683. if(widget->getState() != EGameState::MAKING_TURN)
  684. return;
  685. //may occur just at the start of game (fake move before full intiialization)
  686. if(!LOCPLINT->localState->getCurrentArmy())
  687. return;
  688. if(!LOCPLINT->cb->isVisible(mapPos))
  689. {
  690. CCS->curh->set(Cursor::Map::POINTER);
  691. GH.statusbar->clear();
  692. return;
  693. }
  694. auto objRelations = PlayerRelations::ALLIES;
  695. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  696. if(objAtTile)
  697. {
  698. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  699. std::string text = LOCPLINT->localState->getCurrentHero() ? objAtTile->getHoverText(LOCPLINT->localState->getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  700. boost::replace_all(text,"\n"," ");
  701. GH.statusbar->write(text);
  702. }
  703. else
  704. {
  705. std::string hlp = CGI->mh->getTerrainDescr(mapPos, false);
  706. GH.statusbar->write(hlp);
  707. }
  708. if(spellBeingCasted)
  709. {
  710. switch(spellBeingCasted->id)
  711. {
  712. case SpellID::SCUTTLE_BOAT:
  713. {
  714. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  715. if(objAtTile && objAtTile->ID == Obj::BOAT && isInScreenRange(hpos, mapPos))
  716. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  717. else
  718. CCS->curh->set(Cursor::Map::POINTER);
  719. return;
  720. }
  721. case SpellID::DIMENSION_DOOR:
  722. {
  723. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  724. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  725. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  726. CCS->curh->set(Cursor::Map::TELEPORT);
  727. else
  728. CCS->curh->set(Cursor::Map::POINTER);
  729. return;
  730. }
  731. }
  732. }
  733. if(LOCPLINT->localState->getCurrentArmy()->ID == Obj::TOWN)
  734. {
  735. if(objAtTile)
  736. {
  737. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  738. CCS->curh->set(Cursor::Map::TOWN);
  739. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  740. CCS->curh->set(Cursor::Map::HERO);
  741. else
  742. CCS->curh->set(Cursor::Map::POINTER);
  743. }
  744. else
  745. CCS->curh->set(Cursor::Map::POINTER);
  746. }
  747. else if(const CGHeroInstance * hero = LOCPLINT->localState->getCurrentHero())
  748. {
  749. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  750. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  751. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  752. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  753. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  754. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  755. std::array<Cursor::Map, 4> cursorSailVisit = { Cursor::Map::T1_SAIL_VISIT, Cursor::Map::T2_SAIL_VISIT, Cursor::Map::T3_SAIL_VISIT, Cursor::Map::T4_SAIL_VISIT, };
  756. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  757. assert(pathNode);
  758. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  759. {
  760. showMoveDetailsInStatusbar(*hero, *pathNode);
  761. }
  762. int turns = pathNode->turns;
  763. vstd::amin(turns, 3);
  764. switch(pathNode->action)
  765. {
  766. case CGPathNode::NORMAL:
  767. case CGPathNode::TELEPORT_NORMAL:
  768. if(pathNode->layer == EPathfindingLayer::LAND)
  769. CCS->curh->set(cursorMove[turns]);
  770. else
  771. CCS->curh->set(cursorSailVisit[turns]);
  772. break;
  773. case CGPathNode::VISIT:
  774. case CGPathNode::BLOCKING_VISIT:
  775. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  776. if(objAtTile && objAtTile->ID == Obj::HERO)
  777. {
  778. if(LOCPLINT->localState->getCurrentArmy() == objAtTile)
  779. CCS->curh->set(Cursor::Map::HERO);
  780. else
  781. CCS->curh->set(cursorExchange[turns]);
  782. }
  783. else if(pathNode->layer == EPathfindingLayer::LAND)
  784. CCS->curh->set(cursorVisit[turns]);
  785. else
  786. CCS->curh->set(cursorSailVisit[turns]);
  787. break;
  788. case CGPathNode::BATTLE:
  789. case CGPathNode::TELEPORT_BATTLE:
  790. CCS->curh->set(cursorAttack[turns]);
  791. break;
  792. case CGPathNode::EMBARK:
  793. CCS->curh->set(cursorSail[turns]);
  794. break;
  795. case CGPathNode::DISEMBARK:
  796. CCS->curh->set(cursorDisembark[turns]);
  797. break;
  798. default:
  799. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  800. {
  801. if(objAtTile->ID == Obj::TOWN)
  802. CCS->curh->set(Cursor::Map::TOWN);
  803. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  804. CCS->curh->set(Cursor::Map::HERO);
  805. else
  806. CCS->curh->set(Cursor::Map::POINTER);
  807. }
  808. else
  809. CCS->curh->set(Cursor::Map::POINTER);
  810. break;
  811. }
  812. }
  813. if(ourInaccessibleShipyard(objAtTile))
  814. {
  815. CCS->curh->set(Cursor::Map::T1_SAIL);
  816. }
  817. }
  818. void CAdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  819. {
  820. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  821. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  822. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  823. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  824. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  825. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  826. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  827. GH.statusbar->write(result);
  828. }
  829. void CAdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  830. {
  831. if(widget->getState() != EGameState::MAKING_TURN)
  832. return;
  833. if(spellBeingCasted)
  834. {
  835. abortCastingMode();
  836. return;
  837. }
  838. if(!LOCPLINT->cb->isVisible(mapPos))
  839. {
  840. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  841. return;
  842. }
  843. const CGObjectInstance * obj = getActiveObject(mapPos);
  844. if(!obj)
  845. {
  846. // Bare or undiscovered terrain
  847. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  848. if(tile)
  849. {
  850. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  851. CRClickPopup::createAndPush(hlp);
  852. }
  853. return;
  854. }
  855. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  856. }
  857. void CAdventureMapInterface::enterCastingMode(const CSpell * sp)
  858. {
  859. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  860. spellBeingCasted = sp;
  861. Settings config = settings.write["session"]["showSpellRange"];
  862. config->Bool() = true;
  863. widget->setState(EGameState::CASTING_SPELL);
  864. }
  865. void CAdventureMapInterface::exitCastingMode()
  866. {
  867. assert(spellBeingCasted);
  868. spellBeingCasted = nullptr;
  869. widget->setState(EGameState::MAKING_TURN);
  870. Settings config = settings.write["session"]["showSpellRange"];
  871. config->Bool() = false;
  872. }
  873. void CAdventureMapInterface::abortCastingMode()
  874. {
  875. exitCastingMode();
  876. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  877. }
  878. void CAdventureMapInterface::performSpellcasting(const int3 & dest)
  879. {
  880. SpellID id = spellBeingCasted->id;
  881. exitCastingMode();
  882. LOCPLINT->cb->castSpell(LOCPLINT->localState->getCurrentHero(), id, dest);
  883. }
  884. Rect CAdventureMapInterface::terrainAreaPixels() const
  885. {
  886. return widget->getMapView()->pos;
  887. }
  888. const IShipyard * CAdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  889. {
  890. const IShipyard *ret = IShipyard::castFrom(obj);
  891. if(!ret ||
  892. obj->tempOwner != currentPlayerID ||
  893. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  894. return nullptr;
  895. return ret;
  896. }
  897. void CAdventureMapInterface::exitWorldView()
  898. {
  899. widget->setState(EGameState::MAKING_TURN);
  900. widget->getMapView()->onViewMapActivated();
  901. }
  902. void CAdventureMapInterface::openWorldView(int tileSize)
  903. {
  904. widget->setState(EGameState::WORLD_VIEW);
  905. widget->getMapView()->onViewWorldActivated(tileSize);
  906. }
  907. void CAdventureMapInterface::openWorldView()
  908. {
  909. openWorldView(11);
  910. }
  911. void CAdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  912. {
  913. openWorldView(11);
  914. widget->getMapView()->onViewSpellActivated(11, objectPositions, showTerrain);
  915. }