AdventureMapInterface.cpp 24 KB

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