AdventureMapInterface.cpp 26 KB

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