AdventureMapInterface.cpp 25 KB

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