AdventureMapInterface.cpp 24 KB

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