AdventureMapInterface.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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/CConfigHandler.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. int3 selPos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  424. if(spellBeingCasted)
  425. {
  426. assert(shortcuts->optionSpellcasting());
  427. if (!isInScreenRange(selPos, targetPosition))
  428. return;
  429. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  430. switch(spellBeingCasted->id)
  431. {
  432. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  433. if(topBlocking && topBlocking->ID == Obj::BOAT)
  434. performSpellcasting(targetPosition);
  435. break;
  436. case SpellID::DIMENSION_DOOR:
  437. const TerrainTile *targetTile = LOCPLINT->cb->getTileForDimensionDoor(targetPosition, LOCPLINT->localState->getCurrentHero());
  438. if(targetTile && targetTile->isClear(heroTile))
  439. performSpellcasting(targetPosition);
  440. break;
  441. }
  442. return;
  443. }
  444. //check if we can select this object
  445. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  446. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner) != PlayerRelations::ENEMIES;
  447. bool isHero = false;
  448. if(LOCPLINT->localState->getCurrentArmy()->ID != Obj::HERO) //hero is not selected (presumably town)
  449. {
  450. if(LOCPLINT->localState->getCurrentArmy() == topBlocking) //selected town clicked
  451. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  452. else if(canSelect)
  453. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  454. }
  455. else if(const CGHeroInstance * currentHero = LOCPLINT->localState->getCurrentHero()) //hero is selected
  456. {
  457. isHero = true;
  458. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(targetPosition);
  459. if(currentHero == topBlocking) //clicked selected hero
  460. {
  461. LOCPLINT->openHeroWindow(currentHero);
  462. return;
  463. }
  464. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  465. {
  466. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  467. return;
  468. }
  469. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  470. {
  471. if(LOCPLINT->localState->hasPath(currentHero) &&
  472. LOCPLINT->localState->getPath(currentHero).endPos() == targetPosition)//we'll be moving
  473. {
  474. assert(!CGI->mh->hasOngoingAnimations());
  475. if(!CGI->mh->hasOngoingAnimations() && LOCPLINT->localState->getPath(currentHero).nextNode().turns == 0)
  476. LOCPLINT->moveHero(currentHero, LOCPLINT->localState->getPath(currentHero));
  477. return;
  478. }
  479. else
  480. {
  481. if(GH.isKeyboardCtrlDown()) //normal click behaviour (as no hero selected)
  482. {
  483. if(canSelect)
  484. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  485. }
  486. else //remove old path and find a new one if we clicked on accessible tile
  487. {
  488. LOCPLINT->localState->setPath(currentHero, targetPosition);
  489. onHeroChanged(currentHero);
  490. }
  491. }
  492. }
  493. } //end of hero is selected "case"
  494. else
  495. {
  496. throw std::runtime_error("Nothing is selected...");
  497. }
  498. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  499. if(isHero && shipyard != nullptr)
  500. {
  501. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  502. }
  503. }
  504. void AdventureMapInterface::onTileHovered(const int3 &targetPosition)
  505. {
  506. if(!shortcuts->optionMapViewActive())
  507. return;
  508. //may occur just at the start of game (fake move before full intiialization)
  509. if(!LOCPLINT->localState->getCurrentArmy())
  510. return;
  511. bool isTargetPositionVisible = LOCPLINT->cb->isVisible(targetPosition);
  512. if(!isTargetPositionVisible)
  513. {
  514. GH.statusbar()->clear();
  515. if(!spellBeingCasted || spellBeingCasted->id != SpellID::DIMENSION_DOOR)
  516. {
  517. CCS->curh->set(Cursor::Map::POINTER);
  518. return;
  519. }
  520. }
  521. auto objRelations = PlayerRelations::ALLIES;
  522. const CGObjectInstance *objAtTile = isTargetPositionVisible ? getActiveObject(targetPosition) : nullptr;
  523. if(objAtTile)
  524. {
  525. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  526. std::string text = LOCPLINT->localState->getCurrentHero() ? objAtTile->getHoverText(LOCPLINT->localState->getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  527. boost::replace_all(text,"\n"," ");
  528. GH.statusbar()->write(text);
  529. }
  530. else if(isTargetPositionVisible)
  531. {
  532. std::string tileTooltipText = CGI->mh->getTerrainDescr(targetPosition, false);
  533. GH.statusbar()->write(tileTooltipText);
  534. }
  535. if(spellBeingCasted)
  536. {
  537. switch(spellBeingCasted->id)
  538. {
  539. case SpellID::SCUTTLE_BOAT:
  540. {
  541. int3 heroPosition = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  542. if(objAtTile && objAtTile->ID == Obj::BOAT && isInScreenRange(heroPosition, targetPosition))
  543. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  544. else
  545. CCS->curh->set(Cursor::Map::POINTER);
  546. return;
  547. }
  548. case SpellID::DIMENSION_DOOR:
  549. {
  550. const TerrainTile * t = LOCPLINT->cb->getTileForDimensionDoor(targetPosition, LOCPLINT->localState->getCurrentHero());
  551. int3 heroPosition = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  552. if(t && t->isClear(LOCPLINT->cb->getTile(heroPosition))/* && isInScreenRange(hpos, mapPos)*/)
  553. CCS->curh->set(Cursor::Map::TELEPORT); //TODO: something wrong with beyond east spell range border cursor on arrogance after TP-ing near underground portal on previous day
  554. else
  555. CCS->curh->set(Cursor::Map::POINTER);
  556. return;
  557. }
  558. }
  559. }
  560. if(LOCPLINT->localState->getCurrentArmy()->ID == Obj::TOWN || GH.isKeyboardCtrlDown())
  561. {
  562. if(objAtTile)
  563. {
  564. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  565. CCS->curh->set(Cursor::Map::TOWN);
  566. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  567. CCS->curh->set(Cursor::Map::HERO);
  568. else
  569. CCS->curh->set(Cursor::Map::POINTER);
  570. }
  571. else
  572. CCS->curh->set(Cursor::Map::POINTER);
  573. }
  574. else if(const CGHeroInstance * hero = LOCPLINT->localState->getCurrentHero())
  575. {
  576. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  577. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  578. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  579. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  580. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  581. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  582. 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, };
  583. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(targetPosition);
  584. assert(pathNode);
  585. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  586. {
  587. showMoveDetailsInStatusbar(*hero, *pathNode);
  588. }
  589. int turns = pathNode->turns;
  590. vstd::amin(turns, 3);
  591. switch(pathNode->action)
  592. {
  593. case EPathNodeAction::NORMAL:
  594. case EPathNodeAction::TELEPORT_NORMAL:
  595. if(pathNode->layer == EPathfindingLayer::LAND)
  596. CCS->curh->set(cursorMove[turns]);
  597. else
  598. CCS->curh->set(cursorSail[turns]);
  599. break;
  600. case EPathNodeAction::VISIT:
  601. case EPathNodeAction::BLOCKING_VISIT:
  602. case EPathNodeAction::TELEPORT_BLOCKING_VISIT:
  603. if(objAtTile && objAtTile->ID == Obj::HERO)
  604. {
  605. if(LOCPLINT->localState->getCurrentArmy() == objAtTile)
  606. CCS->curh->set(Cursor::Map::HERO);
  607. else
  608. CCS->curh->set(cursorExchange[turns]);
  609. }
  610. else if(pathNode->layer == EPathfindingLayer::LAND)
  611. CCS->curh->set(cursorVisit[turns]);
  612. else if (pathNode->layer == EPathfindingLayer::SAIL &&
  613. objAtTile &&
  614. objAtTile->isCoastVisitable() &&
  615. pathNode->theNodeBefore &&
  616. pathNode->theNodeBefore->layer == EPathfindingLayer::LAND )
  617. {
  618. // exception - when visiting shipwreck located on coast from land - show 'horse' cursor, not 'ship' cursor
  619. CCS->curh->set(cursorVisit[turns]);
  620. }
  621. else
  622. CCS->curh->set(cursorSailVisit[turns]);
  623. break;
  624. case EPathNodeAction::BATTLE:
  625. case EPathNodeAction::TELEPORT_BATTLE:
  626. CCS->curh->set(cursorAttack[turns]);
  627. break;
  628. case EPathNodeAction::EMBARK:
  629. CCS->curh->set(cursorSail[turns]);
  630. break;
  631. case EPathNodeAction::DISEMBARK:
  632. CCS->curh->set(cursorDisembark[turns]);
  633. break;
  634. default:
  635. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  636. {
  637. if(objAtTile->ID == Obj::TOWN)
  638. CCS->curh->set(Cursor::Map::TOWN);
  639. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  640. CCS->curh->set(Cursor::Map::HERO);
  641. else
  642. CCS->curh->set(Cursor::Map::POINTER);
  643. }
  644. else
  645. CCS->curh->set(Cursor::Map::POINTER);
  646. break;
  647. }
  648. }
  649. if(ourInaccessibleShipyard(objAtTile))
  650. {
  651. CCS->curh->set(Cursor::Map::T1_SAIL);
  652. }
  653. }
  654. void AdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  655. {
  656. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.movementPointsLimit(pathNode.layer == EPathfindingLayer::LAND) : hero.movementPointsRemaining();
  657. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  658. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  659. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  660. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  661. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  662. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  663. GH.statusbar()->write(result);
  664. }
  665. void AdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  666. {
  667. if(!shortcuts->optionMapViewActive())
  668. return;
  669. if(spellBeingCasted)
  670. {
  671. hotkeyAbortCastingMode();
  672. return;
  673. }
  674. if(!LOCPLINT->cb->isVisible(mapPos))
  675. {
  676. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  677. return;
  678. }
  679. const CGObjectInstance * obj = getActiveObject(mapPos);
  680. if(!obj)
  681. {
  682. // Bare or undiscovered terrain
  683. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  684. if(tile)
  685. {
  686. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  687. CRClickPopup::createAndPush(hlp);
  688. }
  689. return;
  690. }
  691. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  692. }
  693. void AdventureMapInterface::enterCastingMode(const CSpell * sp)
  694. {
  695. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  696. spellBeingCasted = sp;
  697. Settings config = settings.write["session"]["showSpellRange"];
  698. config->Bool() = true;
  699. setState(EAdventureState::CASTING_SPELL);
  700. }
  701. void AdventureMapInterface::exitCastingMode()
  702. {
  703. assert(spellBeingCasted);
  704. spellBeingCasted = nullptr;
  705. setState(EAdventureState::MAKING_TURN);
  706. Settings config = settings.write["session"]["showSpellRange"];
  707. config->Bool() = false;
  708. }
  709. void AdventureMapInterface::hotkeyAbortCastingMode()
  710. {
  711. exitCastingMode();
  712. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  713. }
  714. void AdventureMapInterface::performSpellcasting(const int3 & dest)
  715. {
  716. SpellID id = spellBeingCasted->id;
  717. exitCastingMode();
  718. LOCPLINT->cb->castSpell(LOCPLINT->localState->getCurrentHero(), id, dest);
  719. }
  720. Rect AdventureMapInterface::terrainAreaPixels() const
  721. {
  722. return widget->getMapView()->pos;
  723. }
  724. const IShipyard * AdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  725. {
  726. const IShipyard *ret = IShipyard::castFrom(obj);
  727. if(!ret ||
  728. obj->tempOwner != currentPlayerID ||
  729. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  730. return nullptr;
  731. return ret;
  732. }
  733. void AdventureMapInterface::hotkeyExitWorldView()
  734. {
  735. setState(EAdventureState::MAKING_TURN);
  736. widget->getMapView()->onViewMapActivated();
  737. }
  738. void AdventureMapInterface::openWorldView(int tileSize)
  739. {
  740. setState(EAdventureState::WORLD_VIEW);
  741. widget->getMapView()->onViewWorldActivated(tileSize);
  742. }
  743. void AdventureMapInterface::openWorldView()
  744. {
  745. openWorldView(11);
  746. }
  747. void AdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  748. {
  749. openWorldView(11);
  750. widget->getMapView()->onViewSpellActivated(11, objectPositions, showTerrain);
  751. }
  752. void AdventureMapInterface::hotkeyNextTown()
  753. {
  754. widget->getTownList()->selectNext();
  755. }
  756. void AdventureMapInterface::hotkeySwitchMapLevel()
  757. {
  758. widget->getMapView()->onMapLevelSwitched();
  759. }
  760. void AdventureMapInterface::hotkeyZoom(int delta)
  761. {
  762. widget->getMapView()->onMapZoomLevelChanged(delta);
  763. }
  764. void AdventureMapInterface::onScreenResize()
  765. {
  766. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  767. // remember our activation state and reactive after reconstruction
  768. // since othervice activate() calls for created elements will bypass virtual dispatch
  769. // and will call directly CIntObject::activate() instead of dispatching virtual function call
  770. bool widgetActive = isActive();
  771. if (widgetActive)
  772. deactivate();
  773. widget.reset();
  774. pos.x = pos.y = 0;
  775. pos.w = GH.screenDimensions().x;
  776. pos.h = GH.screenDimensions().y;
  777. widget = std::make_shared<AdventureMapWidget>(shortcuts);
  778. widget->getMapView()->onViewMapActivated();
  779. widget->setPlayer(currentPlayerID);
  780. widget->updateActiveState();
  781. widget->getMinimap()->update();
  782. widget->getInfoBar()->showSelection();
  783. if (LOCPLINT && LOCPLINT->localState->getCurrentArmy())
  784. widget->getMapView()->onCenteredObject(LOCPLINT->localState->getCurrentArmy());
  785. adjustActiveness();
  786. if (widgetActive)
  787. activate();
  788. }