CAdventureMapInterface.cpp 24 KB

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