AdventureMapInterface.cpp 28 KB

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