CAdventureMapInterface.cpp 23 KB

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