AdventureMapInterface.cpp 28 KB

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