AdventureMapInterface.cpp 26 KB

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