CAdventureMapInterface.cpp 23 KB

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