2
0

CAdventureMapInterface.cpp 23 KB

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