2
0

AdventureMapInterface.cpp 23 KB

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