AdventureMapInterface.cpp 24 KB

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