CAdventureMapInterface.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. CSDL_Ext::fillSurface(to, CSDL_Ext::toSDL(ColorRGBA(255, 0,255,255)));// FIXME: CONFIGURABLE ADVMAP - debug fill to detect any empty areas
  239. CIntObject::showAll(to);
  240. LOCPLINT->cingconsole->show(to);
  241. }
  242. void CAdventureMapInterface::show(SDL_Surface * to)
  243. {
  244. handleMapScrollingUpdate();
  245. CIntObject::show(to);
  246. LOCPLINT->cingconsole->show(to);
  247. }
  248. void CAdventureMapInterface::handleMapScrollingUpdate()
  249. {
  250. /// Width of window border, in pixels, that triggers map scrolling
  251. static constexpr uint32_t borderScrollWidth = 15;
  252. uint32_t timePassed = GH.mainFPSmng->getElapsedMilliseconds();
  253. uint32_t scrollSpeedPixels = settings["adventure"]["scrollSpeedPixels"].Float();
  254. uint32_t scrollDistance = scrollSpeedPixels * timePassed / 1000;
  255. bool scrollingActive = !GH.isKeyboardCtrlDown() && isActive() && widget->getState() == EGameState::MAKING_TURN;
  256. Point cursorPosition = GH.getCursorPosition();
  257. Point scrollDirection;
  258. if (cursorPosition.x < borderScrollWidth)
  259. scrollDirection.x = -1;
  260. if (cursorPosition.x > GH.screenDimensions().x - borderScrollWidth)
  261. scrollDirection.x = +1;
  262. if (cursorPosition.y < borderScrollWidth)
  263. scrollDirection.y = -1;
  264. if (cursorPosition.y > GH.screenDimensions().y - borderScrollWidth)
  265. scrollDirection.y = +1;
  266. Point scrollDelta = scrollDirection * scrollDistance;
  267. if (scrollingActive && scrollDelta != Point(0,0))
  268. widget->getMapView()->onMapScrolled(scrollDelta);
  269. if (scrollDelta == Point(0,0) && !scrollingCursorSet)
  270. return;
  271. if(scrollDelta.x > 0)
  272. {
  273. if(scrollDelta.y < 0)
  274. CCS->curh->set(Cursor::Map::SCROLL_NORTHEAST);
  275. if(scrollDelta.y > 0)
  276. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  277. if(scrollDelta.y == 0)
  278. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  279. }
  280. if(scrollDelta.x < 0)
  281. {
  282. if(scrollDelta.y < 0)
  283. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  284. if(scrollDelta.y > 0)
  285. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  286. if(scrollDelta.y == 0)
  287. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  288. }
  289. if (scrollDelta.x == 0)
  290. {
  291. if(scrollDelta.y < 0)
  292. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  293. if(scrollDelta.y > 0)
  294. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  295. if(scrollDelta.y == 0)
  296. CCS->curh->set(Cursor::Map::POINTER);
  297. }
  298. scrollingCursorSet = scrollDelta != Point(0,0);
  299. }
  300. void CAdventureMapInterface::centerOnTile(int3 on)
  301. {
  302. widget->getMapView()->onCenteredTile(on);
  303. }
  304. void CAdventureMapInterface::centerOnObject(const CGObjectInstance * obj)
  305. {
  306. widget->getMapView()->onCenteredObject(obj);
  307. }
  308. void CAdventureMapInterface::keyPressed(EShortcut key)
  309. {
  310. if (widget->getState() != EGameState::MAKING_TURN)
  311. return;
  312. //fake mouse use to trigger onTileHovered()
  313. GH.fakeMouseMove();
  314. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  315. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown(); //selected town
  316. switch(key)
  317. {
  318. case EShortcut::ADVENTURE_THIEVES_GUILD:
  319. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  320. return;
  321. {
  322. //find first town with tavern
  323. auto itr = range::find_if(LOCPLINT->localState->getOwnedTowns(), [](const CGTownInstance * town)
  324. {
  325. return town->hasBuilt(BuildingID::TAVERN);
  326. });
  327. if(itr != LOCPLINT->localState->getOwnedTowns().end())
  328. LOCPLINT->showThievesGuildWindow(*itr);
  329. else
  330. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  331. }
  332. return;
  333. case EShortcut::ADVENTURE_VIEW_SCENARIO:
  334. if(isActive())
  335. CAdventureOptions::showScenarioInfo();
  336. return;
  337. case EShortcut::GAME_SAVE_GAME:
  338. if(isActive())
  339. GH.pushIntT<CSavingScreen>();
  340. return;
  341. case EShortcut::GAME_LOAD_GAME:
  342. if(isActive())
  343. LOCPLINT->proposeLoadingGame();
  344. return;
  345. case EShortcut::ADVENTURE_DIG_GRAIL:
  346. {
  347. if(h && isActive() && LOCPLINT->makingTurn)
  348. LOCPLINT->tryDiggging(h);
  349. return;
  350. }
  351. case EShortcut::ADVENTURE_VIEW_PUZZLE:
  352. if(isActive())
  353. LOCPLINT->showPuzzleMap();
  354. return;
  355. case EShortcut::ADVENTURE_VIEW_WORLD:
  356. if(isActive())
  357. LOCPLINT->viewWorldMap();
  358. return;
  359. case EShortcut::GAME_RESTART_GAME:
  360. if(isActive() && GH.isKeyboardCtrlDown())
  361. {
  362. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  363. [](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
  364. }
  365. return;
  366. case EShortcut::ADVENTURE_VISIT_OBJECT: //space - try to revisit current object with selected hero
  367. {
  368. if(!isActive())
  369. return;
  370. if(h)
  371. {
  372. LOCPLINT->cb->moveHero(h,h->pos);
  373. }
  374. }
  375. return;
  376. case EShortcut::ADVENTURE_VIEW_SELECTED:
  377. {
  378. if(!isActive() || !LOCPLINT->localState->getCurrentArmy())
  379. return;
  380. if(h)
  381. LOCPLINT->openHeroWindow(h);
  382. else if(t)
  383. LOCPLINT->openTownWindow(t);
  384. return;
  385. }
  386. case EShortcut::GLOBAL_CANCEL:
  387. {
  388. //FIXME: this case is never executed since AdvMapInt is disabled while in spellcasting mode
  389. if(!isActive() || GH.topInt().get() != this || !spellBeingCasted)
  390. return;
  391. abortCastingMode();
  392. return;
  393. }
  394. case EShortcut::GAME_OPEN_MARKETPLACE:
  395. {
  396. //act on key down if marketplace windows is not already opened
  397. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  398. return;
  399. if(GH.isKeyboardCtrlDown()) //CTRL + T => open marketplace
  400. {
  401. //check if we have any marketplace
  402. const CGTownInstance *townWithMarket = nullptr;
  403. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  404. {
  405. if(t->hasBuilt(BuildingID::MARKETPLACE))
  406. {
  407. townWithMarket = t;
  408. break;
  409. }
  410. }
  411. if(townWithMarket) //if any town has marketplace, open window
  412. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  413. else //if not - complain
  414. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  415. }
  416. case EShortcut::ADVENTURE_NEXT_TOWN:
  417. if(isActive() && !GH.isKeyboardCtrlDown()) //no ctrl, advmapint is on the top => switch to town
  418. {
  419. widget->getTownList()->selectNext();
  420. }
  421. return;
  422. }
  423. case EShortcut::ADVENTURE_MOVE_HERO_SW: return hotkeyMoveHeroDirectional({-1, +1});
  424. case EShortcut::ADVENTURE_MOVE_HERO_SS: return hotkeyMoveHeroDirectional({ 0, +1});
  425. case EShortcut::ADVENTURE_MOVE_HERO_SE: return hotkeyMoveHeroDirectional({+1, +1});
  426. case EShortcut::ADVENTURE_MOVE_HERO_WW: return hotkeyMoveHeroDirectional({-1, 0});
  427. case EShortcut::ADVENTURE_MOVE_HERO_EE: return hotkeyMoveHeroDirectional({+1, 0});
  428. case EShortcut::ADVENTURE_MOVE_HERO_NW: return hotkeyMoveHeroDirectional({-1, -1});
  429. case EShortcut::ADVENTURE_MOVE_HERO_NN: return hotkeyMoveHeroDirectional({ 0, -1});
  430. case EShortcut::ADVENTURE_MOVE_HERO_NE: return hotkeyMoveHeroDirectional({+1, -1});
  431. }
  432. }
  433. void CAdventureMapInterface::hotkeyMoveHeroDirectional(Point direction)
  434. {
  435. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  436. if(!h || !isActive())
  437. return;
  438. if (CGI->mh->hasOngoingAnimations())
  439. return;
  440. if(direction == Point(0,0))
  441. {
  442. centerOnObject(h);
  443. return;
  444. }
  445. int3 dst = h->visitablePos() + int3(direction.x, direction.y, 0);
  446. if (!CGI->mh->isInMap((dst)))
  447. return;
  448. if ( !LOCPLINT->localState->setPath(h, dst))
  449. return;
  450. const CGPath & path = LOCPLINT->localState->getPath(h);
  451. if (path.nodes.size() > 2)
  452. onHeroChanged(h);
  453. else
  454. if(!path.nodes[0].turns)
  455. LOCPLINT->moveHero(h, path);
  456. }
  457. void CAdventureMapInterface::onSelectionChanged(const CArmedInstance *sel)
  458. {
  459. assert(sel);
  460. widget->getInfoBar()->popAll();
  461. mapAudio->onSelectionChanged(sel);
  462. bool centerView = !settings["session"]["autoSkip"].Bool();
  463. if (centerView)
  464. centerOnObject(sel);
  465. if(sel->ID==Obj::TOWN)
  466. {
  467. auto town = dynamic_cast<const CGTownInstance*>(sel);
  468. widget->getInfoBar()->showTownSelection(town);
  469. widget->getTownList()->select(town);
  470. widget->getHeroList()->select(nullptr);
  471. onHeroChanged(nullptr);
  472. }
  473. else //hero selected
  474. {
  475. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  476. widget->getInfoBar()->showHeroSelection(hero);
  477. widget->getHeroList()->select(hero);
  478. widget->getTownList()->select(nullptr);
  479. LOCPLINT->localState->verifyPath(hero);
  480. onHeroChanged(hero);
  481. }
  482. updateButtons();
  483. widget->getHeroList()->redraw();
  484. widget->getTownList()->redraw();
  485. }
  486. bool CAdventureMapInterface::isActive()
  487. {
  488. return active & ~CIntObject::KEYBOARD;
  489. }
  490. void CAdventureMapInterface::onMapTilesChanged(boost::optional<std::unordered_set<int3>> positions)
  491. {
  492. if (positions)
  493. widget->getMinimap()->updateTiles(*positions);
  494. else
  495. widget->getMinimap()->update();
  496. }
  497. void CAdventureMapInterface::onHotseatWaitStarted(PlayerColor playerID)
  498. {
  499. onCurrentPlayerChanged(playerID);
  500. widget->setState(EGameState::HOTSEAT_WAIT);
  501. }
  502. void CAdventureMapInterface::onEnemyTurnStarted(PlayerColor playerID)
  503. {
  504. if(settings["session"]["spectate"].Bool())
  505. return;
  506. adjustActiveness(true);
  507. mapAudio->onEnemyTurnStarted();
  508. widget->getMinimap()->setAIRadar(true);
  509. widget->getInfoBar()->startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  510. widget->getMinimap()->showAll(screen);//force refresh on inactive object
  511. widget->getInfoBar()->showAll(screen);//force refresh on inactive object
  512. }
  513. void CAdventureMapInterface::adjustActiveness(bool aiTurnStart)
  514. {
  515. bool wasActive = isActive();
  516. if(wasActive)
  517. deactivate();
  518. if (aiTurnStart)
  519. widget->setState(EGameState::ENEMY_TURN);
  520. else
  521. widget->setState(EGameState::MAKING_TURN);
  522. if(wasActive)
  523. activate();
  524. }
  525. void CAdventureMapInterface::onCurrentPlayerChanged(PlayerColor playerID)
  526. {
  527. LOCPLINT->localState->setSelection(nullptr);
  528. if (playerID == currentPlayerID)
  529. return;
  530. currentPlayerID = playerID;
  531. widget->setPlayer(playerID);
  532. }
  533. void CAdventureMapInterface::onPlayerTurnStarted(PlayerColor playerID)
  534. {
  535. onCurrentPlayerChanged(playerID);
  536. widget->setState(EGameState::MAKING_TURN);
  537. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  538. || settings["session"]["spectate"].Bool())
  539. {
  540. adjustActiveness(false);
  541. widget->getMinimap()->setAIRadar(false);
  542. widget->getInfoBar()->showSelection();
  543. }
  544. widget->getHeroList()->update();
  545. widget->getTownList()->update();
  546. const CGHeroInstance * heroToSelect = nullptr;
  547. // find first non-sleeping hero
  548. for (auto hero : LOCPLINT->localState->getWanderingHeroes())
  549. {
  550. if (!LOCPLINT->localState->isHeroSleeping(hero))
  551. {
  552. heroToSelect = hero;
  553. break;
  554. }
  555. }
  556. //select first hero if available.
  557. if (heroToSelect != nullptr)
  558. {
  559. LOCPLINT->localState->setSelection(heroToSelect);
  560. }
  561. else if (LOCPLINT->localState->getOwnedTowns().size())
  562. {
  563. LOCPLINT->localState->setSelection(LOCPLINT->localState->getOwnedTown(0));
  564. }
  565. else
  566. {
  567. LOCPLINT->localState->setSelection(LOCPLINT->localState->getWanderingHero(0));
  568. }
  569. //show new day animation and sound on infobar
  570. widget->getInfoBar()->showDate();
  571. onHeroChanged(nullptr);
  572. showAll(screen);
  573. mapAudio->onPlayerTurnStarted();
  574. if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown())
  575. {
  576. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt().get()))
  577. iw->close();
  578. endingTurn();
  579. }
  580. }
  581. void CAdventureMapInterface::endingTurn()
  582. {
  583. if(settings["session"]["spectate"].Bool())
  584. return;
  585. LOCPLINT->makingTurn = false;
  586. LOCPLINT->cb->endTurn();
  587. mapAudio->onPlayerTurnEnded();
  588. }
  589. const CGObjectInstance* CAdventureMapInterface::getActiveObject(const int3 &mapPos)
  590. {
  591. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  592. if (bobjs.empty())
  593. return nullptr;
  594. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  595. /*
  596. if (bobjs.back()->ID == Obj::HERO)
  597. return bobjs.back();
  598. else
  599. return bobjs.front();*/
  600. }
  601. void CAdventureMapInterface::onTileLeftClicked(const int3 &mapPos)
  602. {
  603. if(widget->getState() == EGameState::MAKING_TURN)
  604. return;
  605. //FIXME: this line breaks H3 behavior for Dimension Door
  606. if(!LOCPLINT->cb->isVisible(mapPos))
  607. return;
  608. if(!LOCPLINT->makingTurn)
  609. return;
  610. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  611. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  612. int3 selPos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  613. if(spellBeingCasted)
  614. {
  615. if (!isInScreenRange(selPos, mapPos))
  616. return;
  617. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  618. switch(spellBeingCasted->id)
  619. {
  620. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  621. if(topBlocking && topBlocking->ID == Obj::BOAT)
  622. leaveCastingMode(mapPos);
  623. break;
  624. case SpellID::DIMENSION_DOOR:
  625. if(!tile || tile->isClear(heroTile))
  626. leaveCastingMode(mapPos);
  627. break;
  628. }
  629. return;
  630. }
  631. //check if we can select this object
  632. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  633. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  634. bool isHero = false;
  635. if(LOCPLINT->localState->getCurrentArmy()->ID != Obj::HERO) //hero is not selected (presumably town)
  636. {
  637. if(LOCPLINT->localState->getCurrentArmy() == topBlocking) //selected town clicked
  638. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  639. else if(canSelect)
  640. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  641. }
  642. else if(const CGHeroInstance * currentHero = LOCPLINT->localState->getCurrentHero()) //hero is selected
  643. {
  644. isHero = true;
  645. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  646. if(currentHero == topBlocking) //clicked selected hero
  647. {
  648. LOCPLINT->openHeroWindow(currentHero);
  649. return;
  650. }
  651. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  652. {
  653. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  654. return;
  655. }
  656. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  657. {
  658. if(LOCPLINT->localState->hasPath(currentHero) &&
  659. LOCPLINT->localState->getPath(currentHero).endPos() == mapPos)//we'll be moving
  660. {
  661. if(!CGI->mh->hasOngoingAnimations())
  662. LOCPLINT->moveHero(currentHero, LOCPLINT->localState->getPath(currentHero));
  663. return;
  664. }
  665. else //remove old path and find a new one if we clicked on accessible tile
  666. {
  667. LOCPLINT->localState->setPath(currentHero, mapPos);
  668. onHeroChanged(currentHero);
  669. }
  670. }
  671. } //end of hero is selected "case"
  672. else
  673. {
  674. throw std::runtime_error("Nothing is selected...");
  675. }
  676. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  677. if(isHero && shipyard != nullptr)
  678. {
  679. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  680. }
  681. }
  682. void CAdventureMapInterface::onTileHovered(const int3 &mapPos)
  683. {
  684. if(widget->getState() != EGameState::MAKING_TURN)
  685. return;
  686. //may occur just at the start of game (fake move before full intiialization)
  687. if(!LOCPLINT->localState->getCurrentArmy())
  688. return;
  689. if(!LOCPLINT->cb->isVisible(mapPos))
  690. {
  691. CCS->curh->set(Cursor::Map::POINTER);
  692. GH.statusbar->clear();
  693. return;
  694. }
  695. auto objRelations = PlayerRelations::ALLIES;
  696. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  697. if(objAtTile)
  698. {
  699. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  700. std::string text = LOCPLINT->localState->getCurrentHero() ? objAtTile->getHoverText(LOCPLINT->localState->getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  701. boost::replace_all(text,"\n"," ");
  702. GH.statusbar->write(text);
  703. }
  704. else
  705. {
  706. std::string hlp = CGI->mh->getTerrainDescr(mapPos, false);
  707. GH.statusbar->write(hlp);
  708. }
  709. if(spellBeingCasted)
  710. {
  711. switch(spellBeingCasted->id)
  712. {
  713. case SpellID::SCUTTLE_BOAT:
  714. {
  715. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  716. if(objAtTile && objAtTile->ID == Obj::BOAT && isInScreenRange(hpos, mapPos))
  717. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  718. else
  719. CCS->curh->set(Cursor::Map::POINTER);
  720. return;
  721. }
  722. case SpellID::DIMENSION_DOOR:
  723. {
  724. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  725. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  726. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  727. CCS->curh->set(Cursor::Map::TELEPORT);
  728. else
  729. CCS->curh->set(Cursor::Map::POINTER);
  730. return;
  731. }
  732. }
  733. }
  734. if(LOCPLINT->localState->getCurrentArmy()->ID == Obj::TOWN)
  735. {
  736. if(objAtTile)
  737. {
  738. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  739. CCS->curh->set(Cursor::Map::TOWN);
  740. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  741. CCS->curh->set(Cursor::Map::HERO);
  742. else
  743. CCS->curh->set(Cursor::Map::POINTER);
  744. }
  745. else
  746. CCS->curh->set(Cursor::Map::POINTER);
  747. }
  748. else if(const CGHeroInstance * hero = LOCPLINT->localState->getCurrentHero())
  749. {
  750. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  751. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  752. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  753. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  754. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  755. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  756. 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, };
  757. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  758. assert(pathNode);
  759. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  760. {
  761. showMoveDetailsInStatusbar(*hero, *pathNode);
  762. }
  763. int turns = pathNode->turns;
  764. vstd::amin(turns, 3);
  765. switch(pathNode->action)
  766. {
  767. case CGPathNode::NORMAL:
  768. case CGPathNode::TELEPORT_NORMAL:
  769. if(pathNode->layer == EPathfindingLayer::LAND)
  770. CCS->curh->set(cursorMove[turns]);
  771. else
  772. CCS->curh->set(cursorSailVisit[turns]);
  773. break;
  774. case CGPathNode::VISIT:
  775. case CGPathNode::BLOCKING_VISIT:
  776. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  777. if(objAtTile && objAtTile->ID == Obj::HERO)
  778. {
  779. if(LOCPLINT->localState->getCurrentArmy() == objAtTile)
  780. CCS->curh->set(Cursor::Map::HERO);
  781. else
  782. CCS->curh->set(cursorExchange[turns]);
  783. }
  784. else if(pathNode->layer == EPathfindingLayer::LAND)
  785. CCS->curh->set(cursorVisit[turns]);
  786. else
  787. CCS->curh->set(cursorSailVisit[turns]);
  788. break;
  789. case CGPathNode::BATTLE:
  790. case CGPathNode::TELEPORT_BATTLE:
  791. CCS->curh->set(cursorAttack[turns]);
  792. break;
  793. case CGPathNode::EMBARK:
  794. CCS->curh->set(cursorSail[turns]);
  795. break;
  796. case CGPathNode::DISEMBARK:
  797. CCS->curh->set(cursorDisembark[turns]);
  798. break;
  799. default:
  800. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  801. {
  802. if(objAtTile->ID == Obj::TOWN)
  803. CCS->curh->set(Cursor::Map::TOWN);
  804. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  805. CCS->curh->set(Cursor::Map::HERO);
  806. else
  807. CCS->curh->set(Cursor::Map::POINTER);
  808. }
  809. else
  810. CCS->curh->set(Cursor::Map::POINTER);
  811. break;
  812. }
  813. }
  814. if(ourInaccessibleShipyard(objAtTile))
  815. {
  816. CCS->curh->set(Cursor::Map::T1_SAIL);
  817. }
  818. }
  819. void CAdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  820. {
  821. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  822. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  823. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  824. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  825. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  826. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  827. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  828. GH.statusbar->write(result);
  829. }
  830. void CAdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  831. {
  832. if(widget->getState() != EGameState::MAKING_TURN)
  833. return;
  834. if(spellBeingCasted)
  835. {
  836. abortCastingMode();
  837. return;
  838. }
  839. if(!LOCPLINT->cb->isVisible(mapPos))
  840. {
  841. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  842. return;
  843. }
  844. const CGObjectInstance * obj = getActiveObject(mapPos);
  845. if(!obj)
  846. {
  847. // Bare or undiscovered terrain
  848. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  849. if(tile)
  850. {
  851. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  852. CRClickPopup::createAndPush(hlp);
  853. }
  854. return;
  855. }
  856. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  857. }
  858. void CAdventureMapInterface::enterCastingMode(const CSpell * sp)
  859. {
  860. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  861. spellBeingCasted = sp;
  862. Settings config = settings.write["session"]["showSpellRange"];
  863. config->Bool() = true;
  864. widget->setState(EGameState::CASTING_SPELL);
  865. }
  866. void CAdventureMapInterface::exitCastingMode()
  867. {
  868. assert(spellBeingCasted);
  869. spellBeingCasted = nullptr;
  870. widget->setState(EGameState::MAKING_TURN);
  871. Settings config = settings.write["session"]["showSpellRange"];
  872. config->Bool() = false;
  873. }
  874. void CAdventureMapInterface::abortCastingMode()
  875. {
  876. exitCastingMode();
  877. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  878. }
  879. void CAdventureMapInterface::leaveCastingMode(const int3 & dest)
  880. {
  881. SpellID id = spellBeingCasted->id;
  882. exitCastingMode();
  883. LOCPLINT->cb->castSpell(LOCPLINT->localState->getCurrentHero(), id, dest);
  884. }
  885. Rect CAdventureMapInterface::terrainAreaPixels() const
  886. {
  887. return widget->getMapView()->pos;
  888. }
  889. const IShipyard * CAdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  890. {
  891. const IShipyard *ret = IShipyard::castFrom(obj);
  892. if(!ret ||
  893. obj->tempOwner != currentPlayerID ||
  894. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  895. return nullptr;
  896. return ret;
  897. }
  898. void CAdventureMapInterface::exitWorldView()
  899. {
  900. widget->setState(EGameState::MAKING_TURN);
  901. widget->getMapView()->onViewMapActivated();
  902. }
  903. void CAdventureMapInterface::openWorldView(int tileSize)
  904. {
  905. widget->setState(EGameState::WORLD_VIEW);
  906. widget->getMapView()->onViewWorldActivated(tileSize);
  907. }
  908. void CAdventureMapInterface::openWorldView()
  909. {
  910. openWorldView(11);
  911. }
  912. void CAdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  913. {
  914. openWorldView(11);
  915. widget->getMapView()->onViewSpellActivated(11, objectPositions, showTerrain);
  916. }