AdventureMapInterface.cpp 27 KB

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