AdventureMapInterface.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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())
  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(LOCPLINT->cb->getCurrentPlayer());
  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(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  296. || settings["session"]["spectate"].Bool())
  297. {
  298. widget->getMinimap()->setAIRadar(false);
  299. widget->getInfoBar()->showSelection();
  300. }
  301. widget->getHeroList()->updateWidget();
  302. widget->getTownList()->updateWidget();
  303. const CGHeroInstance * heroToSelect = nullptr;
  304. // find first non-sleeping hero
  305. for (auto hero : LOCPLINT->localState->getWanderingHeroes())
  306. {
  307. if (!LOCPLINT->localState->isHeroSleeping(hero))
  308. {
  309. heroToSelect = hero;
  310. break;
  311. }
  312. }
  313. //select first hero if available.
  314. if (heroToSelect != nullptr)
  315. {
  316. LOCPLINT->localState->setSelection(heroToSelect);
  317. }
  318. else if (LOCPLINT->localState->getOwnedTowns().size())
  319. {
  320. LOCPLINT->localState->setSelection(LOCPLINT->localState->getOwnedTown(0));
  321. }
  322. else
  323. {
  324. LOCPLINT->localState->setSelection(LOCPLINT->localState->getWanderingHero(0));
  325. }
  326. //show new day animation and sound on infobar, except for 1st day of the game
  327. if (LOCPLINT->cb->getDate(Date::DAY) != 1)
  328. widget->getInfoBar()->showDate();
  329. onHeroChanged(nullptr);
  330. Canvas canvas = Canvas::createFromSurface(screen);
  331. showAll(canvas);
  332. mapAudio->onPlayerTurnStarted();
  333. if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown())
  334. {
  335. if(auto iw = GH.windows().topWindow<CInfoWindow>())
  336. iw->close();
  337. hotkeyEndingTurn();
  338. }
  339. }
  340. void AdventureMapInterface::hotkeyEndingTurn()
  341. {
  342. if(settings["session"]["spectate"].Bool())
  343. return;
  344. if(!settings["general"]["startTurnAutosave"].Bool())
  345. {
  346. LOCPLINT->performAutosave();
  347. }
  348. LOCPLINT->makingTurn = false;
  349. LOCPLINT->cb->endTurn();
  350. mapAudio->onPlayerTurnEnded();
  351. }
  352. const CGObjectInstance* AdventureMapInterface::getActiveObject(const int3 &mapPos)
  353. {
  354. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  355. if (bobjs.empty())
  356. return nullptr;
  357. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  358. }
  359. void AdventureMapInterface::onTileLeftClicked(const int3 &mapPos)
  360. {
  361. if(!shortcuts->optionMapViewActive())
  362. return;
  363. //FIXME: this line breaks H3 behavior for Dimension Door
  364. if(!LOCPLINT->cb->isVisible(mapPos))
  365. return;
  366. if(!LOCPLINT->makingTurn)
  367. return;
  368. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  369. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  370. int3 selPos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  371. if(spellBeingCasted)
  372. {
  373. assert(shortcuts->optionSpellcasting());
  374. if (!isInScreenRange(selPos, mapPos))
  375. return;
  376. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  377. switch(spellBeingCasted->id)
  378. {
  379. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  380. if(topBlocking && topBlocking->ID == Obj::BOAT)
  381. performSpellcasting(mapPos);
  382. break;
  383. case SpellID::DIMENSION_DOOR:
  384. if(!tile || tile->isClear(heroTile))
  385. performSpellcasting(mapPos);
  386. break;
  387. }
  388. return;
  389. }
  390. //check if we can select this object
  391. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  392. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner) != PlayerRelations::ENEMIES;
  393. bool isHero = false;
  394. if(LOCPLINT->localState->getCurrentArmy()->ID != Obj::HERO) //hero is not selected (presumably town)
  395. {
  396. if(LOCPLINT->localState->getCurrentArmy() == topBlocking) //selected town clicked
  397. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  398. else if(canSelect)
  399. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  400. }
  401. else if(const CGHeroInstance * currentHero = LOCPLINT->localState->getCurrentHero()) //hero is selected
  402. {
  403. isHero = true;
  404. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  405. if(currentHero == topBlocking) //clicked selected hero
  406. {
  407. LOCPLINT->openHeroWindow(currentHero);
  408. return;
  409. }
  410. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  411. {
  412. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  413. return;
  414. }
  415. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  416. {
  417. if(LOCPLINT->localState->hasPath(currentHero) &&
  418. LOCPLINT->localState->getPath(currentHero).endPos() == mapPos)//we'll be moving
  419. {
  420. if(!CGI->mh->hasOngoingAnimations())
  421. LOCPLINT->moveHero(currentHero, LOCPLINT->localState->getPath(currentHero));
  422. return;
  423. }
  424. else //remove old path and find a new one if we clicked on accessible tile
  425. {
  426. LOCPLINT->localState->setPath(currentHero, mapPos);
  427. onHeroChanged(currentHero);
  428. }
  429. }
  430. } //end of hero is selected "case"
  431. else
  432. {
  433. throw std::runtime_error("Nothing is selected...");
  434. }
  435. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  436. if(isHero && shipyard != nullptr)
  437. {
  438. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  439. }
  440. }
  441. void AdventureMapInterface::onTileHovered(const int3 &mapPos)
  442. {
  443. if(!shortcuts->optionMapViewActive())
  444. return;
  445. //may occur just at the start of game (fake move before full intiialization)
  446. if(!LOCPLINT->localState->getCurrentArmy())
  447. return;
  448. if(!LOCPLINT->cb->isVisible(mapPos))
  449. {
  450. CCS->curh->set(Cursor::Map::POINTER);
  451. GH.statusbar()->clear();
  452. return;
  453. }
  454. auto objRelations = PlayerRelations::ALLIES;
  455. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  456. if(objAtTile)
  457. {
  458. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  459. std::string text = LOCPLINT->localState->getCurrentHero() ? objAtTile->getHoverText(LOCPLINT->localState->getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  460. boost::replace_all(text,"\n"," ");
  461. GH.statusbar()->write(text);
  462. }
  463. else
  464. {
  465. std::string hlp = CGI->mh->getTerrainDescr(mapPos, false);
  466. GH.statusbar()->write(hlp);
  467. }
  468. if(spellBeingCasted)
  469. {
  470. switch(spellBeingCasted->id)
  471. {
  472. case SpellID::SCUTTLE_BOAT:
  473. {
  474. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  475. if(objAtTile && objAtTile->ID == Obj::BOAT && isInScreenRange(hpos, mapPos))
  476. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  477. else
  478. CCS->curh->set(Cursor::Map::POINTER);
  479. return;
  480. }
  481. case SpellID::DIMENSION_DOOR:
  482. {
  483. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  484. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  485. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  486. CCS->curh->set(Cursor::Map::TELEPORT);
  487. else
  488. CCS->curh->set(Cursor::Map::POINTER);
  489. return;
  490. }
  491. }
  492. }
  493. if(LOCPLINT->localState->getCurrentArmy()->ID == Obj::TOWN)
  494. {
  495. if(objAtTile)
  496. {
  497. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  498. CCS->curh->set(Cursor::Map::TOWN);
  499. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  500. CCS->curh->set(Cursor::Map::HERO);
  501. else
  502. CCS->curh->set(Cursor::Map::POINTER);
  503. }
  504. else
  505. CCS->curh->set(Cursor::Map::POINTER);
  506. }
  507. else if(const CGHeroInstance * hero = LOCPLINT->localState->getCurrentHero())
  508. {
  509. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  510. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  511. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  512. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  513. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  514. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  515. 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, };
  516. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  517. assert(pathNode);
  518. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  519. {
  520. showMoveDetailsInStatusbar(*hero, *pathNode);
  521. }
  522. int turns = pathNode->turns;
  523. vstd::amin(turns, 3);
  524. switch(pathNode->action)
  525. {
  526. case EPathNodeAction::NORMAL:
  527. case EPathNodeAction::TELEPORT_NORMAL:
  528. if(pathNode->layer == EPathfindingLayer::LAND)
  529. CCS->curh->set(cursorMove[turns]);
  530. else
  531. CCS->curh->set(cursorSailVisit[turns]);
  532. break;
  533. case EPathNodeAction::VISIT:
  534. case EPathNodeAction::BLOCKING_VISIT:
  535. case EPathNodeAction::TELEPORT_BLOCKING_VISIT:
  536. if(objAtTile && objAtTile->ID == Obj::HERO)
  537. {
  538. if(LOCPLINT->localState->getCurrentArmy() == objAtTile)
  539. CCS->curh->set(Cursor::Map::HERO);
  540. else
  541. CCS->curh->set(cursorExchange[turns]);
  542. }
  543. else if(pathNode->layer == EPathfindingLayer::LAND)
  544. CCS->curh->set(cursorVisit[turns]);
  545. else
  546. CCS->curh->set(cursorSailVisit[turns]);
  547. break;
  548. case EPathNodeAction::BATTLE:
  549. case EPathNodeAction::TELEPORT_BATTLE:
  550. CCS->curh->set(cursorAttack[turns]);
  551. break;
  552. case EPathNodeAction::EMBARK:
  553. CCS->curh->set(cursorSail[turns]);
  554. break;
  555. case EPathNodeAction::DISEMBARK:
  556. CCS->curh->set(cursorDisembark[turns]);
  557. break;
  558. default:
  559. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  560. {
  561. if(objAtTile->ID == Obj::TOWN)
  562. CCS->curh->set(Cursor::Map::TOWN);
  563. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  564. CCS->curh->set(Cursor::Map::HERO);
  565. else
  566. CCS->curh->set(Cursor::Map::POINTER);
  567. }
  568. else
  569. CCS->curh->set(Cursor::Map::POINTER);
  570. break;
  571. }
  572. }
  573. if(ourInaccessibleShipyard(objAtTile))
  574. {
  575. CCS->curh->set(Cursor::Map::T1_SAIL);
  576. }
  577. }
  578. void AdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  579. {
  580. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.movementPointsLimit(pathNode.layer == EPathfindingLayer::LAND) : hero.movementPointsRemaining();
  581. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  582. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  583. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  584. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  585. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  586. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  587. GH.statusbar()->write(result);
  588. }
  589. void AdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  590. {
  591. if(!shortcuts->optionMapViewActive())
  592. return;
  593. if(spellBeingCasted)
  594. {
  595. hotkeyAbortCastingMode();
  596. return;
  597. }
  598. if(!LOCPLINT->cb->isVisible(mapPos))
  599. {
  600. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  601. return;
  602. }
  603. const CGObjectInstance * obj = getActiveObject(mapPos);
  604. if(!obj)
  605. {
  606. // Bare or undiscovered terrain
  607. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  608. if(tile)
  609. {
  610. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  611. CRClickPopup::createAndPush(hlp);
  612. }
  613. return;
  614. }
  615. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  616. }
  617. void AdventureMapInterface::enterCastingMode(const CSpell * sp)
  618. {
  619. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  620. spellBeingCasted = sp;
  621. Settings config = settings.write["session"]["showSpellRange"];
  622. config->Bool() = true;
  623. setState(EAdventureState::CASTING_SPELL);
  624. }
  625. void AdventureMapInterface::exitCastingMode()
  626. {
  627. assert(spellBeingCasted);
  628. spellBeingCasted = nullptr;
  629. setState(EAdventureState::MAKING_TURN);
  630. Settings config = settings.write["session"]["showSpellRange"];
  631. config->Bool() = false;
  632. }
  633. void AdventureMapInterface::hotkeyAbortCastingMode()
  634. {
  635. exitCastingMode();
  636. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  637. }
  638. void AdventureMapInterface::performSpellcasting(const int3 & dest)
  639. {
  640. SpellID id = spellBeingCasted->id;
  641. exitCastingMode();
  642. LOCPLINT->cb->castSpell(LOCPLINT->localState->getCurrentHero(), id, dest);
  643. }
  644. Rect AdventureMapInterface::terrainAreaPixels() const
  645. {
  646. return widget->getMapView()->pos;
  647. }
  648. const IShipyard * AdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  649. {
  650. const IShipyard *ret = IShipyard::castFrom(obj);
  651. if(!ret ||
  652. obj->tempOwner != currentPlayerID ||
  653. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  654. return nullptr;
  655. return ret;
  656. }
  657. void AdventureMapInterface::hotkeyExitWorldView()
  658. {
  659. setState(EAdventureState::MAKING_TURN);
  660. widget->getMapView()->onViewMapActivated();
  661. }
  662. void AdventureMapInterface::openWorldView(int tileSize)
  663. {
  664. setState(EAdventureState::WORLD_VIEW);
  665. widget->getMapView()->onViewWorldActivated(tileSize);
  666. }
  667. void AdventureMapInterface::openWorldView()
  668. {
  669. openWorldView(11);
  670. }
  671. void AdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  672. {
  673. openWorldView(11);
  674. widget->getMapView()->onViewSpellActivated(11, objectPositions, showTerrain);
  675. }
  676. void AdventureMapInterface::hotkeyNextTown()
  677. {
  678. widget->getTownList()->selectNext();
  679. }
  680. void AdventureMapInterface::hotkeySwitchMapLevel()
  681. {
  682. widget->getMapView()->onMapLevelSwitched();
  683. }
  684. void AdventureMapInterface::hotkeyZoom(int delta)
  685. {
  686. widget->getMapView()->onMapZoomLevelChanged(delta);
  687. }
  688. void AdventureMapInterface::onScreenResize()
  689. {
  690. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  691. // remember our activation state and reactive after reconstruction
  692. // since othervice activate() calls for created elements will bypass virtual dispatch
  693. // and will call directly CIntObject::activate() instead of dispatching virtual function call
  694. bool widgetActive = isActive();
  695. if (widgetActive)
  696. deactivate();
  697. widget.reset();
  698. pos.x = pos.y = 0;
  699. pos.w = GH.screenDimensions().x;
  700. pos.h = GH.screenDimensions().y;
  701. widget = std::make_shared<AdventureMapWidget>(shortcuts);
  702. widget->getMapView()->onViewMapActivated();
  703. widget->setPlayer(currentPlayerID);
  704. widget->updateActiveState();
  705. widget->getMinimap()->update();
  706. widget->getInfoBar()->showSelection();
  707. if (LOCPLINT && LOCPLINT->localState->getCurrentArmy())
  708. widget->getMapView()->onCenteredObject(LOCPLINT->localState->getCurrentArmy());
  709. adjustActiveness();
  710. if (widgetActive)
  711. activate();
  712. }