AdventureMapInterface.cpp 25 KB

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