AdventureMapInterface.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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 &mapPos)
  412. {
  413. if(!shortcuts->optionMapViewActive())
  414. return;
  415. if(!LOCPLINT->cb->isVisible(mapPos))
  416. {
  417. if(!spellBeingCasted || spellBeingCasted->id != SpellID::DIMENSION_DOOR)
  418. return;
  419. }
  420. if(!LOCPLINT->makingTurn)
  421. return;
  422. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  423. int3 selPos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  424. if(spellBeingCasted)
  425. {
  426. assert(shortcuts->optionSpellcasting());
  427. if (!isInScreenRange(selPos, mapPos))
  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(mapPos);
  435. break;
  436. case SpellID::DIMENSION_DOOR:
  437. const TerrainTile *targetTile = LOCPLINT->cb->getTileForDimensionDoor(mapPos, LOCPLINT->localState->getCurrentHero());
  438. if(targetTile && targetTile->isClear(heroTile))
  439. performSpellcasting(mapPos);
  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(mapPos);
  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() == mapPos)//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, mapPos);
  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 &mapPos)
  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. if(!LOCPLINT->cb->isVisible(mapPos))
  512. {
  513. GH.statusbar()->clear();
  514. if(!spellBeingCasted || spellBeingCasted->id != SpellID::DIMENSION_DOOR)
  515. {
  516. CCS->curh->set(Cursor::Map::POINTER);
  517. return;
  518. }
  519. }
  520. auto objRelations = PlayerRelations::ALLIES;
  521. const CGObjectInstance *objAtTile = LOCPLINT->cb->isVisible(mapPos) ? getActiveObject(mapPos) : nullptr;
  522. if(objAtTile)
  523. {
  524. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  525. std::string text = LOCPLINT->localState->getCurrentHero() ? objAtTile->getHoverText(LOCPLINT->localState->getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  526. boost::replace_all(text,"\n"," ");
  527. GH.statusbar()->write(text);
  528. }
  529. else
  530. {
  531. std::string hlp = CGI->mh->getTerrainDescr(mapPos, false);
  532. GH.statusbar()->write(hlp);
  533. }
  534. if(spellBeingCasted)
  535. {
  536. switch(spellBeingCasted->id)
  537. {
  538. case SpellID::SCUTTLE_BOAT:
  539. {
  540. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  541. if(objAtTile && objAtTile->ID == Obj::BOAT && isInScreenRange(hpos, mapPos))
  542. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  543. else
  544. CCS->curh->set(Cursor::Map::POINTER);
  545. return;
  546. }
  547. case SpellID::DIMENSION_DOOR:
  548. {
  549. const TerrainTile * t = LOCPLINT->cb->getTileForDimensionDoor(mapPos, LOCPLINT->localState->getCurrentHero());
  550. int3 heroPosition = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  551. if(t && t->isClear(LOCPLINT->cb->getTile(heroPosition))/* && isInScreenRange(hpos, mapPos)*/)
  552. CCS->curh->set(Cursor::Map::TELEPORT);
  553. else
  554. CCS->curh->set(Cursor::Map::POINTER);
  555. return;
  556. }
  557. }
  558. }
  559. if(LOCPLINT->localState->getCurrentArmy()->ID == Obj::TOWN || GH.isKeyboardCtrlDown())
  560. {
  561. if(objAtTile)
  562. {
  563. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  564. CCS->curh->set(Cursor::Map::TOWN);
  565. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  566. CCS->curh->set(Cursor::Map::HERO);
  567. else
  568. CCS->curh->set(Cursor::Map::POINTER);
  569. }
  570. else
  571. CCS->curh->set(Cursor::Map::POINTER);
  572. }
  573. else if(const CGHeroInstance * hero = LOCPLINT->localState->getCurrentHero())
  574. {
  575. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  576. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  577. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  578. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  579. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  580. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  581. 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, };
  582. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  583. assert(pathNode);
  584. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  585. {
  586. showMoveDetailsInStatusbar(*hero, *pathNode);
  587. }
  588. int turns = pathNode->turns;
  589. vstd::amin(turns, 3);
  590. switch(pathNode->action)
  591. {
  592. case EPathNodeAction::NORMAL:
  593. case EPathNodeAction::TELEPORT_NORMAL:
  594. if(pathNode->layer == EPathfindingLayer::LAND)
  595. CCS->curh->set(cursorMove[turns]);
  596. else
  597. CCS->curh->set(cursorSail[turns]);
  598. break;
  599. case EPathNodeAction::VISIT:
  600. case EPathNodeAction::BLOCKING_VISIT:
  601. case EPathNodeAction::TELEPORT_BLOCKING_VISIT:
  602. if(objAtTile && objAtTile->ID == Obj::HERO)
  603. {
  604. if(LOCPLINT->localState->getCurrentArmy() == objAtTile)
  605. CCS->curh->set(Cursor::Map::HERO);
  606. else
  607. CCS->curh->set(cursorExchange[turns]);
  608. }
  609. else if(pathNode->layer == EPathfindingLayer::LAND)
  610. CCS->curh->set(cursorVisit[turns]);
  611. else if (pathNode->layer == EPathfindingLayer::SAIL &&
  612. objAtTile &&
  613. objAtTile->isCoastVisitable() &&
  614. pathNode->theNodeBefore &&
  615. pathNode->theNodeBefore->layer == EPathfindingLayer::LAND )
  616. {
  617. // exception - when visiting shipwreck located on coast from land - show 'horse' cursor, not 'ship' cursor
  618. CCS->curh->set(cursorVisit[turns]);
  619. }
  620. else
  621. CCS->curh->set(cursorSailVisit[turns]);
  622. break;
  623. case EPathNodeAction::BATTLE:
  624. case EPathNodeAction::TELEPORT_BATTLE:
  625. CCS->curh->set(cursorAttack[turns]);
  626. break;
  627. case EPathNodeAction::EMBARK:
  628. CCS->curh->set(cursorSail[turns]);
  629. break;
  630. case EPathNodeAction::DISEMBARK:
  631. CCS->curh->set(cursorDisembark[turns]);
  632. break;
  633. default:
  634. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  635. {
  636. if(objAtTile->ID == Obj::TOWN)
  637. CCS->curh->set(Cursor::Map::TOWN);
  638. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  639. CCS->curh->set(Cursor::Map::HERO);
  640. else
  641. CCS->curh->set(Cursor::Map::POINTER);
  642. }
  643. else
  644. CCS->curh->set(Cursor::Map::POINTER);
  645. break;
  646. }
  647. }
  648. if(ourInaccessibleShipyard(objAtTile))
  649. {
  650. CCS->curh->set(Cursor::Map::T1_SAIL);
  651. }
  652. }
  653. void AdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  654. {
  655. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.movementPointsLimit(pathNode.layer == EPathfindingLayer::LAND) : hero.movementPointsRemaining();
  656. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  657. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  658. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  659. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  660. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  661. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  662. GH.statusbar()->write(result);
  663. }
  664. void AdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  665. {
  666. if(!shortcuts->optionMapViewActive())
  667. return;
  668. if(spellBeingCasted)
  669. {
  670. hotkeyAbortCastingMode();
  671. return;
  672. }
  673. if(!LOCPLINT->cb->isVisible(mapPos))
  674. {
  675. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  676. return;
  677. }
  678. const CGObjectInstance * obj = getActiveObject(mapPos);
  679. if(!obj)
  680. {
  681. // Bare or undiscovered terrain
  682. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  683. if(tile)
  684. {
  685. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  686. CRClickPopup::createAndPush(hlp);
  687. }
  688. return;
  689. }
  690. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  691. }
  692. void AdventureMapInterface::enterCastingMode(const CSpell * sp)
  693. {
  694. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  695. spellBeingCasted = sp;
  696. Settings config = settings.write["session"]["showSpellRange"];
  697. config->Bool() = true;
  698. setState(EAdventureState::CASTING_SPELL);
  699. }
  700. void AdventureMapInterface::exitCastingMode()
  701. {
  702. assert(spellBeingCasted);
  703. spellBeingCasted = nullptr;
  704. setState(EAdventureState::MAKING_TURN);
  705. Settings config = settings.write["session"]["showSpellRange"];
  706. config->Bool() = false;
  707. }
  708. void AdventureMapInterface::hotkeyAbortCastingMode()
  709. {
  710. exitCastingMode();
  711. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  712. }
  713. void AdventureMapInterface::performSpellcasting(const int3 & dest)
  714. {
  715. SpellID id = spellBeingCasted->id;
  716. exitCastingMode();
  717. LOCPLINT->cb->castSpell(LOCPLINT->localState->getCurrentHero(), id, dest);
  718. }
  719. Rect AdventureMapInterface::terrainAreaPixels() const
  720. {
  721. return widget->getMapView()->pos;
  722. }
  723. const IShipyard * AdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  724. {
  725. const IShipyard *ret = IShipyard::castFrom(obj);
  726. if(!ret ||
  727. obj->tempOwner != currentPlayerID ||
  728. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  729. return nullptr;
  730. return ret;
  731. }
  732. void AdventureMapInterface::hotkeyExitWorldView()
  733. {
  734. setState(EAdventureState::MAKING_TURN);
  735. widget->getMapView()->onViewMapActivated();
  736. }
  737. void AdventureMapInterface::openWorldView(int tileSize)
  738. {
  739. setState(EAdventureState::WORLD_VIEW);
  740. widget->getMapView()->onViewWorldActivated(tileSize);
  741. }
  742. void AdventureMapInterface::openWorldView()
  743. {
  744. openWorldView(11);
  745. }
  746. void AdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  747. {
  748. openWorldView(11);
  749. widget->getMapView()->onViewSpellActivated(11, objectPositions, showTerrain);
  750. }
  751. void AdventureMapInterface::hotkeyNextTown()
  752. {
  753. widget->getTownList()->selectNext();
  754. }
  755. void AdventureMapInterface::hotkeySwitchMapLevel()
  756. {
  757. widget->getMapView()->onMapLevelSwitched();
  758. }
  759. void AdventureMapInterface::hotkeyZoom(int delta)
  760. {
  761. widget->getMapView()->onMapZoomLevelChanged(delta);
  762. }
  763. void AdventureMapInterface::onScreenResize()
  764. {
  765. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  766. // remember our activation state and reactive after reconstruction
  767. // since othervice activate() calls for created elements will bypass virtual dispatch
  768. // and will call directly CIntObject::activate() instead of dispatching virtual function call
  769. bool widgetActive = isActive();
  770. if (widgetActive)
  771. deactivate();
  772. widget.reset();
  773. pos.x = pos.y = 0;
  774. pos.w = GH.screenDimensions().x;
  775. pos.h = GH.screenDimensions().y;
  776. widget = std::make_shared<AdventureMapWidget>(shortcuts);
  777. widget->getMapView()->onViewMapActivated();
  778. widget->setPlayer(currentPlayerID);
  779. widget->updateActiveState();
  780. widget->getMinimap()->update();
  781. widget->getInfoBar()->showSelection();
  782. if (LOCPLINT && LOCPLINT->localState->getCurrentArmy())
  783. widget->getMapView()->onCenteredObject(LOCPLINT->localState->getCurrentArmy());
  784. adjustActiveness();
  785. if (widgetActive)
  786. activate();
  787. }