AdventureMapInterface.cpp 24 KB

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