AdventureMapInterface.cpp 26 KB

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