CAdventureMapInterface.cpp 23 KB

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