AdventureMapInterface.cpp 27 KB

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