AdventureMapInterface.cpp 23 KB

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