AdventureMapInterface.cpp 26 KB

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