AdventureMapInterface.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * AdventureMapInterface.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 "AdventureMapInterface.h"
  12. #include "AdventureOptions.h"
  13. #include "AdventureState.h"
  14. #include "CInGameConsole.h"
  15. #include "CMinimap.h"
  16. #include "CList.h"
  17. #include "CInfoBar.h"
  18. #include "MapAudioPlayer.h"
  19. #include "AdventureMapWidget.h"
  20. #include "AdventureMapShortcuts.h"
  21. #include "../mapView/mapHandler.h"
  22. #include "../mapView/MapView.h"
  23. #include "../windows/InfoWindows.h"
  24. #include "../CGameInfo.h"
  25. #include "../gui/CursorHandler.h"
  26. #include "../gui/CGuiHandler.h"
  27. #include "../gui/Shortcut.h"
  28. #include "../CMT.h"
  29. #include "../PlayerLocalState.h"
  30. #include "../CPlayerInterface.h"
  31. #include "../../CCallback.h"
  32. #include "../../lib/CConfigHandler.h"
  33. #include "../../lib/CGeneralTextHandler.h"
  34. #include "../../lib/spells/CSpellHandler.h"
  35. #include "../../lib/mapObjects/CGHeroInstance.h"
  36. #include "../../lib/CPathfinder.h"
  37. #include "../../lib/mapping/CMap.h"
  38. std::shared_ptr<AdventureMapInterface> adventureInt;
  39. AdventureMapInterface::AdventureMapInterface():
  40. mapAudio(new MapAudioPlayer()),
  41. spellBeingCasted(nullptr),
  42. scrollingWasActive(false),
  43. scrollingWasBlocked(false)
  44. {
  45. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  46. pos.x = pos.y = 0;
  47. pos.w = GH.screenDimensions().x;
  48. pos.h = GH.screenDimensions().y;
  49. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  50. shortcuts = std::make_shared<AdventureMapShortcuts>(*this);
  51. widget = std::make_shared<AdventureMapWidget>(shortcuts);
  52. shortcuts->setState(EAdventureState::MAKING_TURN);
  53. widget->getMapView()->onViewMapActivated();
  54. addUsedEvents(KEYBOARD);
  55. }
  56. void AdventureMapInterface::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  57. {
  58. shortcuts->onMapViewMoved(visibleArea, mapLevel);
  59. widget->getMinimap()->onMapViewMoved(visibleArea, mapLevel);
  60. widget->onMapViewMoved(visibleArea, mapLevel);
  61. }
  62. void AdventureMapInterface::onAudioResumed()
  63. {
  64. mapAudio->onAudioResumed();
  65. }
  66. void AdventureMapInterface::onAudioPaused()
  67. {
  68. mapAudio->onAudioPaused();
  69. }
  70. void AdventureMapInterface::onHeroMovementStarted(const CGHeroInstance * hero)
  71. {
  72. widget->getInfoBar()->popAll();
  73. widget->getInfoBar()->showSelection();
  74. }
  75. void AdventureMapInterface::onHeroChanged(const CGHeroInstance *h)
  76. {
  77. widget->getHeroList()->update(h);
  78. if (h && h == LOCPLINT->localState->getCurrentHero() && !widget->getInfoBar()->showingComponents())
  79. widget->getInfoBar()->showSelection();
  80. widget->updateActiveState();
  81. }
  82. void AdventureMapInterface::onTownChanged(const CGTownInstance * town)
  83. {
  84. widget->getTownList()->update(town);
  85. if (town && town == LOCPLINT->localState->getCurrentTown() && !widget->getInfoBar()->showingComponents())
  86. widget->getInfoBar()->showSelection();
  87. }
  88. void AdventureMapInterface::showInfoBoxMessage(const std::vector<Component> & components, std::string message, int timer)
  89. {
  90. widget->getInfoBar()->pushComponents(components, message, timer);
  91. }
  92. void AdventureMapInterface::activate()
  93. {
  94. CIntObject::activate();
  95. adjustActiveness();
  96. screenBuf = screen;
  97. if(LOCPLINT)
  98. {
  99. LOCPLINT->cingconsole->activate();
  100. LOCPLINT->cingconsole->pos = this->pos;
  101. }
  102. GH.fakeMouseMove(); //to restore the cursor
  103. }
  104. void AdventureMapInterface::deactivate()
  105. {
  106. CIntObject::deactivate();
  107. CCS->curh->set(Cursor::Map::POINTER);
  108. }
  109. void AdventureMapInterface::showAll(SDL_Surface * to)
  110. {
  111. CIntObject::showAll(to);
  112. LOCPLINT->cingconsole->show(to);
  113. }
  114. void AdventureMapInterface::show(SDL_Surface * to)
  115. {
  116. handleMapScrollingUpdate();
  117. CIntObject::show(to);
  118. LOCPLINT->cingconsole->show(to);
  119. }
  120. void AdventureMapInterface::handleMapScrollingUpdate()
  121. {
  122. /// Width of window border, in pixels, that triggers map scrolling
  123. static constexpr uint32_t borderScrollWidth = 15;
  124. uint32_t timePassed = GH.mainFPSmng->getElapsedMilliseconds();
  125. uint32_t scrollSpeedPixels = settings["adventure"]["scrollSpeedPixels"].Float();
  126. uint32_t scrollDistance = scrollSpeedPixels * timePassed / 1000;
  127. Point cursorPosition = GH.getCursorPosition();
  128. Point scrollDirection;
  129. if (cursorPosition.x < borderScrollWidth)
  130. scrollDirection.x = -1;
  131. if (cursorPosition.x > GH.screenDimensions().x - borderScrollWidth)
  132. scrollDirection.x = +1;
  133. if (cursorPosition.y < borderScrollWidth)
  134. scrollDirection.y = -1;
  135. if (cursorPosition.y > GH.screenDimensions().y - borderScrollWidth)
  136. scrollDirection.y = +1;
  137. Point scrollDelta = scrollDirection * scrollDistance;
  138. bool cursorInScrollArea = scrollDelta != Point(0,0);
  139. bool scrollingActive = cursorInScrollArea && active && shortcuts->optionSidePanelActive() && !scrollingWasBlocked;
  140. bool scrollingBlocked = GH.isKeyboardCtrlDown();
  141. if (!scrollingWasActive && scrollingBlocked)
  142. {
  143. scrollingWasBlocked = true;
  144. return;
  145. }
  146. if (!cursorInScrollArea && scrollingWasBlocked)
  147. {
  148. scrollingWasBlocked = false;
  149. return;
  150. }
  151. if (scrollingActive)
  152. widget->getMapView()->onMapScrolled(scrollDelta);
  153. if (!scrollingActive && !scrollingWasActive)
  154. return;
  155. if(scrollDelta.x > 0)
  156. {
  157. if(scrollDelta.y < 0)
  158. CCS->curh->set(Cursor::Map::SCROLL_NORTHEAST);
  159. if(scrollDelta.y > 0)
  160. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  161. if(scrollDelta.y == 0)
  162. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  163. }
  164. if(scrollDelta.x < 0)
  165. {
  166. if(scrollDelta.y < 0)
  167. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  168. if(scrollDelta.y > 0)
  169. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  170. if(scrollDelta.y == 0)
  171. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  172. }
  173. if (scrollDelta.x == 0)
  174. {
  175. if(scrollDelta.y < 0)
  176. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  177. if(scrollDelta.y > 0)
  178. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  179. if(scrollDelta.y == 0)
  180. CCS->curh->set(Cursor::Map::POINTER);
  181. }
  182. scrollingWasActive = scrollingActive;
  183. }
  184. void AdventureMapInterface::centerOnTile(int3 on)
  185. {
  186. widget->getMapView()->onCenteredTile(on);
  187. }
  188. void AdventureMapInterface::centerOnObject(const CGObjectInstance * obj)
  189. {
  190. widget->getMapView()->onCenteredObject(obj);
  191. }
  192. void AdventureMapInterface::keyPressed(EShortcut key)
  193. {
  194. if (key == EShortcut::GLOBAL_CANCEL && spellBeingCasted)
  195. hotkeyAbortCastingMode();
  196. //fake mouse use to trigger onTileHovered()
  197. GH.fakeMouseMove();
  198. }
  199. void AdventureMapInterface::onSelectionChanged(const CArmedInstance *sel)
  200. {
  201. assert(sel);
  202. widget->getInfoBar()->popAll();
  203. mapAudio->onSelectionChanged(sel);
  204. bool centerView = !settings["session"]["autoSkip"].Bool();
  205. if (centerView)
  206. centerOnObject(sel);
  207. if(sel->ID==Obj::TOWN)
  208. {
  209. auto town = dynamic_cast<const CGTownInstance*>(sel);
  210. widget->getInfoBar()->showTownSelection(town);
  211. widget->getTownList()->select(town);
  212. widget->getHeroList()->select(nullptr);
  213. onHeroChanged(nullptr);
  214. }
  215. else //hero selected
  216. {
  217. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  218. widget->getInfoBar()->showHeroSelection(hero);
  219. widget->getHeroList()->select(hero);
  220. widget->getTownList()->select(nullptr);
  221. LOCPLINT->localState->verifyPath(hero);
  222. onHeroChanged(hero);
  223. }
  224. widget->updateActiveState();
  225. widget->getHeroList()->redraw();
  226. widget->getTownList()->redraw();
  227. }
  228. void AdventureMapInterface::onMapTilesChanged(boost::optional<std::unordered_set<int3>> positions)
  229. {
  230. if (positions)
  231. widget->getMinimap()->updateTiles(*positions);
  232. else
  233. widget->getMinimap()->update();
  234. }
  235. void AdventureMapInterface::onHotseatWaitStarted(PlayerColor playerID)
  236. {
  237. onCurrentPlayerChanged(playerID);
  238. setState(EAdventureState::HOTSEAT_WAIT);
  239. }
  240. void AdventureMapInterface::onEnemyTurnStarted(PlayerColor playerID)
  241. {
  242. if(settings["session"]["spectate"].Bool())
  243. return;
  244. mapAudio->onEnemyTurnStarted();
  245. widget->getMinimap()->setAIRadar(true);
  246. widget->getInfoBar()->startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  247. setState(EAdventureState::ENEMY_TURN);
  248. }
  249. void AdventureMapInterface::setState(EAdventureState state)
  250. {
  251. shortcuts->setState(state);
  252. adjustActiveness();
  253. widget->updateActiveState();
  254. }
  255. void AdventureMapInterface::adjustActiveness()
  256. {
  257. bool widgetMustBeActive = active && shortcuts->optionSidePanelActive();
  258. bool mapViewMustBeActive = active && (shortcuts->optionMapViewActive());
  259. if (widgetMustBeActive && !widget->active)
  260. widget->activate();
  261. if (!widgetMustBeActive && widget->active)
  262. widget->deactivate();
  263. if (mapViewMustBeActive && !widget->getMapView()->active)
  264. widget->getMapView()->activate();
  265. if (!mapViewMustBeActive && widget->getMapView()->active)
  266. widget->getMapView()->deactivate();
  267. }
  268. void AdventureMapInterface::onCurrentPlayerChanged(PlayerColor playerID)
  269. {
  270. LOCPLINT->localState->setSelection(nullptr);
  271. if (playerID == currentPlayerID)
  272. return;
  273. currentPlayerID = playerID;
  274. widget->setPlayer(playerID);
  275. }
  276. void AdventureMapInterface::onPlayerTurnStarted(PlayerColor playerID)
  277. {
  278. onCurrentPlayerChanged(playerID);
  279. setState(EAdventureState::MAKING_TURN);
  280. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  281. || settings["session"]["spectate"].Bool())
  282. {
  283. widget->getMinimap()->setAIRadar(false);
  284. widget->getInfoBar()->showSelection();
  285. }
  286. widget->getHeroList()->update();
  287. widget->getTownList()->update();
  288. const CGHeroInstance * heroToSelect = nullptr;
  289. // find first non-sleeping hero
  290. for (auto hero : LOCPLINT->localState->getWanderingHeroes())
  291. {
  292. if (!LOCPLINT->localState->isHeroSleeping(hero))
  293. {
  294. heroToSelect = hero;
  295. break;
  296. }
  297. }
  298. //select first hero if available.
  299. if (heroToSelect != nullptr)
  300. {
  301. LOCPLINT->localState->setSelection(heroToSelect);
  302. }
  303. else if (LOCPLINT->localState->getOwnedTowns().size())
  304. {
  305. LOCPLINT->localState->setSelection(LOCPLINT->localState->getOwnedTown(0));
  306. }
  307. else
  308. {
  309. LOCPLINT->localState->setSelection(LOCPLINT->localState->getWanderingHero(0));
  310. }
  311. //show new day animation and sound on infobar
  312. widget->getInfoBar()->showDate();
  313. onHeroChanged(nullptr);
  314. showAll(screen);
  315. mapAudio->onPlayerTurnStarted();
  316. if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown())
  317. {
  318. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt().get()))
  319. iw->close();
  320. hotkeyEndingTurn();
  321. }
  322. }
  323. void AdventureMapInterface::hotkeyEndingTurn()
  324. {
  325. if(settings["session"]["spectate"].Bool())
  326. return;
  327. LOCPLINT->makingTurn = false;
  328. LOCPLINT->cb->endTurn();
  329. mapAudio->onPlayerTurnEnded();
  330. }
  331. const CGObjectInstance* AdventureMapInterface::getActiveObject(const int3 &mapPos)
  332. {
  333. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  334. if (bobjs.empty())
  335. return nullptr;
  336. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  337. }
  338. void AdventureMapInterface::onTileLeftClicked(const int3 &mapPos)
  339. {
  340. if(!shortcuts->optionMapViewActive())
  341. return;
  342. //FIXME: this line breaks H3 behavior for Dimension Door
  343. if(!LOCPLINT->cb->isVisible(mapPos))
  344. return;
  345. if(!LOCPLINT->makingTurn)
  346. return;
  347. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  348. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  349. int3 selPos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  350. if(spellBeingCasted)
  351. {
  352. assert(shortcuts->optionSpellcasting());
  353. if (!isInScreenRange(selPos, mapPos))
  354. return;
  355. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  356. switch(spellBeingCasted->id)
  357. {
  358. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  359. if(topBlocking && topBlocking->ID == Obj::BOAT)
  360. performSpellcasting(mapPos);
  361. break;
  362. case SpellID::DIMENSION_DOOR:
  363. if(!tile || tile->isClear(heroTile))
  364. performSpellcasting(mapPos);
  365. break;
  366. }
  367. return;
  368. }
  369. //check if we can select this object
  370. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  371. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  372. bool isHero = false;
  373. if(LOCPLINT->localState->getCurrentArmy()->ID != Obj::HERO) //hero is not selected (presumably town)
  374. {
  375. if(LOCPLINT->localState->getCurrentArmy() == topBlocking) //selected town clicked
  376. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  377. else if(canSelect)
  378. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  379. }
  380. else if(const CGHeroInstance * currentHero = LOCPLINT->localState->getCurrentHero()) //hero is selected
  381. {
  382. isHero = true;
  383. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  384. if(currentHero == topBlocking) //clicked selected hero
  385. {
  386. LOCPLINT->openHeroWindow(currentHero);
  387. return;
  388. }
  389. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  390. {
  391. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  392. return;
  393. }
  394. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  395. {
  396. if(LOCPLINT->localState->hasPath(currentHero) &&
  397. LOCPLINT->localState->getPath(currentHero).endPos() == mapPos)//we'll be moving
  398. {
  399. if(!CGI->mh->hasOngoingAnimations())
  400. LOCPLINT->moveHero(currentHero, LOCPLINT->localState->getPath(currentHero));
  401. return;
  402. }
  403. else //remove old path and find a new one if we clicked on accessible tile
  404. {
  405. LOCPLINT->localState->setPath(currentHero, mapPos);
  406. onHeroChanged(currentHero);
  407. }
  408. }
  409. } //end of hero is selected "case"
  410. else
  411. {
  412. throw std::runtime_error("Nothing is selected...");
  413. }
  414. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  415. if(isHero && shipyard != nullptr)
  416. {
  417. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  418. }
  419. }
  420. void AdventureMapInterface::onTileHovered(const int3 &mapPos)
  421. {
  422. if(!shortcuts->optionMapViewActive())
  423. return;
  424. //may occur just at the start of game (fake move before full intiialization)
  425. if(!LOCPLINT->localState->getCurrentArmy())
  426. return;
  427. if(!LOCPLINT->cb->isVisible(mapPos))
  428. {
  429. CCS->curh->set(Cursor::Map::POINTER);
  430. GH.statusbar->clear();
  431. return;
  432. }
  433. auto objRelations = PlayerRelations::ALLIES;
  434. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  435. if(objAtTile)
  436. {
  437. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  438. std::string text = LOCPLINT->localState->getCurrentHero() ? objAtTile->getHoverText(LOCPLINT->localState->getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  439. boost::replace_all(text,"\n"," ");
  440. GH.statusbar->write(text);
  441. }
  442. else
  443. {
  444. std::string hlp = CGI->mh->getTerrainDescr(mapPos, false);
  445. GH.statusbar->write(hlp);
  446. }
  447. if(spellBeingCasted)
  448. {
  449. switch(spellBeingCasted->id)
  450. {
  451. case SpellID::SCUTTLE_BOAT:
  452. {
  453. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  454. if(objAtTile && objAtTile->ID == Obj::BOAT && isInScreenRange(hpos, mapPos))
  455. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  456. else
  457. CCS->curh->set(Cursor::Map::POINTER);
  458. return;
  459. }
  460. case SpellID::DIMENSION_DOOR:
  461. {
  462. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  463. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  464. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  465. CCS->curh->set(Cursor::Map::TELEPORT);
  466. else
  467. CCS->curh->set(Cursor::Map::POINTER);
  468. return;
  469. }
  470. }
  471. }
  472. if(LOCPLINT->localState->getCurrentArmy()->ID == Obj::TOWN)
  473. {
  474. if(objAtTile)
  475. {
  476. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  477. CCS->curh->set(Cursor::Map::TOWN);
  478. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  479. CCS->curh->set(Cursor::Map::HERO);
  480. else
  481. CCS->curh->set(Cursor::Map::POINTER);
  482. }
  483. else
  484. CCS->curh->set(Cursor::Map::POINTER);
  485. }
  486. else if(const CGHeroInstance * hero = LOCPLINT->localState->getCurrentHero())
  487. {
  488. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  489. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  490. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  491. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  492. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  493. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  494. 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, };
  495. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  496. assert(pathNode);
  497. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  498. {
  499. showMoveDetailsInStatusbar(*hero, *pathNode);
  500. }
  501. int turns = pathNode->turns;
  502. vstd::amin(turns, 3);
  503. switch(pathNode->action)
  504. {
  505. case CGPathNode::NORMAL:
  506. case CGPathNode::TELEPORT_NORMAL:
  507. if(pathNode->layer == EPathfindingLayer::LAND)
  508. CCS->curh->set(cursorMove[turns]);
  509. else
  510. CCS->curh->set(cursorSailVisit[turns]);
  511. break;
  512. case CGPathNode::VISIT:
  513. case CGPathNode::BLOCKING_VISIT:
  514. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  515. if(objAtTile && objAtTile->ID == Obj::HERO)
  516. {
  517. if(LOCPLINT->localState->getCurrentArmy() == objAtTile)
  518. CCS->curh->set(Cursor::Map::HERO);
  519. else
  520. CCS->curh->set(cursorExchange[turns]);
  521. }
  522. else if(pathNode->layer == EPathfindingLayer::LAND)
  523. CCS->curh->set(cursorVisit[turns]);
  524. else
  525. CCS->curh->set(cursorSailVisit[turns]);
  526. break;
  527. case CGPathNode::BATTLE:
  528. case CGPathNode::TELEPORT_BATTLE:
  529. CCS->curh->set(cursorAttack[turns]);
  530. break;
  531. case CGPathNode::EMBARK:
  532. CCS->curh->set(cursorSail[turns]);
  533. break;
  534. case CGPathNode::DISEMBARK:
  535. CCS->curh->set(cursorDisembark[turns]);
  536. break;
  537. default:
  538. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  539. {
  540. if(objAtTile->ID == Obj::TOWN)
  541. CCS->curh->set(Cursor::Map::TOWN);
  542. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  543. CCS->curh->set(Cursor::Map::HERO);
  544. else
  545. CCS->curh->set(Cursor::Map::POINTER);
  546. }
  547. else
  548. CCS->curh->set(Cursor::Map::POINTER);
  549. break;
  550. }
  551. }
  552. if(ourInaccessibleShipyard(objAtTile))
  553. {
  554. CCS->curh->set(Cursor::Map::T1_SAIL);
  555. }
  556. }
  557. void AdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  558. {
  559. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  560. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  561. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  562. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  563. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  564. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  565. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  566. GH.statusbar->write(result);
  567. }
  568. void AdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  569. {
  570. if(!shortcuts->optionMapViewActive())
  571. return;
  572. if(spellBeingCasted)
  573. {
  574. hotkeyAbortCastingMode();
  575. return;
  576. }
  577. if(!LOCPLINT->cb->isVisible(mapPos))
  578. {
  579. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  580. return;
  581. }
  582. const CGObjectInstance * obj = getActiveObject(mapPos);
  583. if(!obj)
  584. {
  585. // Bare or undiscovered terrain
  586. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  587. if(tile)
  588. {
  589. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  590. CRClickPopup::createAndPush(hlp);
  591. }
  592. return;
  593. }
  594. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  595. }
  596. void AdventureMapInterface::enterCastingMode(const CSpell * sp)
  597. {
  598. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  599. spellBeingCasted = sp;
  600. Settings config = settings.write["session"]["showSpellRange"];
  601. config->Bool() = true;
  602. setState(EAdventureState::CASTING_SPELL);
  603. }
  604. void AdventureMapInterface::exitCastingMode()
  605. {
  606. assert(spellBeingCasted);
  607. spellBeingCasted = nullptr;
  608. setState(EAdventureState::MAKING_TURN);
  609. Settings config = settings.write["session"]["showSpellRange"];
  610. config->Bool() = false;
  611. }
  612. void AdventureMapInterface::hotkeyAbortCastingMode()
  613. {
  614. exitCastingMode();
  615. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  616. }
  617. void AdventureMapInterface::performSpellcasting(const int3 & dest)
  618. {
  619. SpellID id = spellBeingCasted->id;
  620. exitCastingMode();
  621. LOCPLINT->cb->castSpell(LOCPLINT->localState->getCurrentHero(), id, dest);
  622. }
  623. Rect AdventureMapInterface::terrainAreaPixels() const
  624. {
  625. return widget->getMapView()->pos;
  626. }
  627. const IShipyard * AdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  628. {
  629. const IShipyard *ret = IShipyard::castFrom(obj);
  630. if(!ret ||
  631. obj->tempOwner != currentPlayerID ||
  632. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  633. return nullptr;
  634. return ret;
  635. }
  636. void AdventureMapInterface::hotkeyExitWorldView()
  637. {
  638. setState(EAdventureState::MAKING_TURN);
  639. widget->getMapView()->onViewMapActivated();
  640. }
  641. void AdventureMapInterface::openWorldView(int tileSize)
  642. {
  643. setState(EAdventureState::WORLD_VIEW);
  644. widget->getMapView()->onViewWorldActivated(tileSize);
  645. }
  646. void AdventureMapInterface::openWorldView()
  647. {
  648. openWorldView(11);
  649. }
  650. void AdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  651. {
  652. openWorldView(11);
  653. widget->getMapView()->onViewSpellActivated(11, objectPositions, showTerrain);
  654. }
  655. void AdventureMapInterface::hotkeyNextTown()
  656. {
  657. widget->getTownList()->selectNext();
  658. }
  659. void AdventureMapInterface::hotkeySwitchMapLevel()
  660. {
  661. widget->getMapView()->onMapLevelSwitched();
  662. }
  663. void AdventureMapInterface::onScreenResize()
  664. {
  665. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  666. widget.reset();
  667. pos.x = pos.y = 0;
  668. pos.w = GH.screenDimensions().x;
  669. pos.h = GH.screenDimensions().y;
  670. widget = std::make_shared<AdventureMapWidget>(shortcuts);
  671. widget->getMapView()->onViewMapActivated();
  672. widget->setPlayer(currentPlayerID);
  673. widget->updateActiveState();
  674. widget->getMinimap()->update();
  675. widget->getInfoBar()->showSelection();
  676. adjustActiveness();
  677. }