CAdventureMapInterface.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * CAdvMapInt.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 "CAdvMapPanel.h"
  13. #include "CAdventureOptions.h"
  14. #include "CInGameConsole.h"
  15. #include "CMinimap.h"
  16. #include "CResDataBar.h"
  17. #include "CList.h"
  18. #include "CInfoBar.h"
  19. #include "MapAudioPlayer.h"
  20. #include "../mapView/mapHandler.h"
  21. #include "../mapView/MapView.h"
  22. #include "../windows/CKingdomInterface.h"
  23. #include "../windows/CSpellWindow.h"
  24. #include "../windows/CTradeWindow.h"
  25. #include "../windows/GUIClasses.h"
  26. #include "../windows/InfoWindows.h"
  27. #include "../CGameInfo.h"
  28. #include "../CPlayerInterface.h"
  29. #include "../lobby/CSavingScreen.h"
  30. #include "../render/CAnimation.h"
  31. #include "../gui/CursorHandler.h"
  32. #include "../render/IImage.h"
  33. #include "../gui/CGuiHandler.h"
  34. #include "../widgets/TextControls.h"
  35. #include "../widgets/Buttons.h"
  36. #include "../windows/settings/SettingsMainWindow.h"
  37. #include "../CMT.h"
  38. #include "../PlayerLocalState.h"
  39. #include "../../CCallback.h"
  40. #include "../../lib/CConfigHandler.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/CPathfinder.h"
  46. #include "../../lib/mapping/CMap.h"
  47. #define ADVOPT (conf.go()->ac)
  48. std::shared_ptr<CAdventureMapInterface> adventureInt;
  49. CAdventureMapInterface::CAdventureMapInterface():
  50. minimap(new CMinimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH))),
  51. statusbar(CGStatusBar::create(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG)),
  52. heroList(new CHeroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD)),
  53. townList(new CTownList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD)),
  54. infoBar(new CInfoBar(Point(ADVOPT.infoboxX, ADVOPT.infoboxY))),
  55. resdatabar(new CResDataBar),
  56. mapAudio(new MapAudioPlayer()),
  57. terrain(new MapView(Point(ADVOPT.advmapX, ADVOPT.advmapY), Point(ADVOPT.advmapW, ADVOPT.advmapH))),
  58. state(EGameState::NOT_INITIALIZED),
  59. spellBeingCasted(nullptr),
  60. activeMapPanel(nullptr),
  61. scrollingCursorSet(false)
  62. {
  63. pos.x = pos.y = 0;
  64. pos.w = GH.screenDimensions().x;
  65. pos.h = GH.screenDimensions().y;
  66. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  67. bg = IImage::createFromFile(ADVOPT.mainGraphic);
  68. if(!ADVOPT.worldViewGraphic.empty())
  69. {
  70. bgWorldView = IImage::createFromFile(ADVOPT.worldViewGraphic);
  71. }
  72. else
  73. {
  74. bgWorldView = nullptr;
  75. logGlobal->warn("ADVOPT.worldViewGraphic is empty => bitmap not loaded");
  76. }
  77. if (!bgWorldView)
  78. {
  79. logGlobal->warn("bgWorldView not defined in resolution config; fallback to VWorld.bmp");
  80. bgWorldView = IImage::createFromFile("VWorld.bmp");
  81. }
  82. worldViewIcons = std::make_shared<CAnimation>("VwSymbol");//todo: customize with ADVOPT
  83. worldViewIcons->preload();
  84. for(int g = 0; g < ADVOPT.gemG.size(); ++g)
  85. {
  86. gems.push_back(std::make_shared<CAnimImage>(ADVOPT.gemG[g], 0, 0, ADVOPT.gemX[g], ADVOPT.gemY[g]));
  87. }
  88. auto makeButton = [&](int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> std::shared_ptr<CButton>
  89. {
  90. auto button = std::make_shared<CButton>(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  91. for(auto image : info.additionalDefs)
  92. button->addImage(image);
  93. return button;
  94. };
  95. kingOverview = makeButton(293, std::bind(&CAdventureMapInterface::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  96. underground = makeButton(294, std::bind(&CAdventureMapInterface::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  97. questlog = makeButton(295, std::bind(&CAdventureMapInterface::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  98. sleepWake = makeButton(296, std::bind(&CAdventureMapInterface::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  99. moveHero = makeButton(297, std::bind(&CAdventureMapInterface::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  100. spellbook = makeButton(298, std::bind(&CAdventureMapInterface::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  101. advOptions = makeButton(299, std::bind(&CAdventureMapInterface::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  102. sysOptions = makeButton(300, std::bind(&CAdventureMapInterface::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  103. nextHero = makeButton(301, std::bind(&CAdventureMapInterface::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  104. endTurn = makeButton(302, std::bind(&CAdventureMapInterface::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  105. int panelSpaceBottom = GH.screenDimensions().y - resdatabar->pos.h - 4;
  106. panelMain = std::make_shared<CAdvMapPanel>(nullptr, Point(0, 0));
  107. // TODO correct drawing position
  108. panelWorldView = std::make_shared<CAdvMapWorldViewPanel>(worldViewIcons, bgWorldView, Point(heroList->pos.x - 2, 195), panelSpaceBottom, LOCPLINT->playerID);
  109. panelMain->addChildColorableButton(kingOverview);
  110. panelMain->addChildColorableButton(underground);
  111. panelMain->addChildColorableButton(questlog);
  112. panelMain->addChildColorableButton(sleepWake);
  113. panelMain->addChildColorableButton(moveHero);
  114. panelMain->addChildColorableButton(spellbook);
  115. panelMain->addChildColorableButton(advOptions);
  116. panelMain->addChildColorableButton(sysOptions);
  117. panelMain->addChildColorableButton(nextHero);
  118. panelMain->addChildColorableButton(endTurn);
  119. // TODO move configs to resolutions.json, similarly to previous buttons
  120. config::ButtonInfo worldViewBackConfig = config::ButtonInfo();
  121. worldViewBackConfig.defName = "IOK6432.DEF";
  122. worldViewBackConfig.x = GH.screenDimensions().x - 73;
  123. worldViewBackConfig.y = 343 + 195;
  124. worldViewBackConfig.playerColoured = false;
  125. panelWorldView->addChildToPanel(
  126. makeButton(288, std::bind(&CAdventureMapInterface::fworldViewBack,this), worldViewBackConfig, SDLK_ESCAPE), ACTIVATE | DEACTIVATE);
  127. config::ButtonInfo worldViewPuzzleConfig = config::ButtonInfo();
  128. worldViewPuzzleConfig.defName = "VWPUZ.DEF";
  129. worldViewPuzzleConfig.x = GH.screenDimensions().x - 188;
  130. worldViewPuzzleConfig.y = 343 + 195;
  131. worldViewPuzzleConfig.playerColoured = false;
  132. panelWorldView->addChildToPanel( // no help text for this one
  133. std::make_shared<CButton>(Point(worldViewPuzzleConfig.x, worldViewPuzzleConfig.y), worldViewPuzzleConfig.defName, std::pair<std::string, std::string>(),
  134. std::bind(&CPlayerInterface::showPuzzleMap,LOCPLINT), SDLK_p, worldViewPuzzleConfig.playerColoured), ACTIVATE | DEACTIVATE);
  135. config::ButtonInfo worldViewScale1xConfig = config::ButtonInfo();
  136. worldViewScale1xConfig.defName = "VWMAG1.DEF";
  137. worldViewScale1xConfig.x = GH.screenDimensions().x - 191;
  138. worldViewScale1xConfig.y = 23 + 195;
  139. worldViewScale1xConfig.playerColoured = false;
  140. panelWorldView->addChildToPanel( // help text is wrong for this button
  141. makeButton(291, std::bind(&CAdventureMapInterface::fworldViewScale1x,this), worldViewScale1xConfig, SDLK_1), ACTIVATE | DEACTIVATE);
  142. config::ButtonInfo worldViewScale2xConfig = config::ButtonInfo();
  143. worldViewScale2xConfig.defName = "VWMAG2.DEF";
  144. worldViewScale2xConfig.x = GH.screenDimensions().x- 191 + 63;
  145. worldViewScale2xConfig.y = 23 + 195;
  146. worldViewScale2xConfig.playerColoured = false;
  147. panelWorldView->addChildToPanel( // help text is wrong for this button
  148. makeButton(291, std::bind(&CAdventureMapInterface::fworldViewScale2x,this), worldViewScale2xConfig, SDLK_2), ACTIVATE | DEACTIVATE);
  149. config::ButtonInfo worldViewScale4xConfig = config::ButtonInfo();
  150. worldViewScale4xConfig.defName = "VWMAG4.DEF";
  151. worldViewScale4xConfig.x = GH.screenDimensions().x- 191 + 126;
  152. worldViewScale4xConfig.y = 23 + 195;
  153. worldViewScale4xConfig.playerColoured = false;
  154. panelWorldView->addChildToPanel( // help text is wrong for this button
  155. makeButton(291, std::bind(&CAdventureMapInterface::fworldViewScale4x,this), worldViewScale4xConfig, SDLK_4), ACTIVATE | DEACTIVATE);
  156. config::ButtonInfo worldViewUndergroundConfig = config::ButtonInfo();
  157. worldViewUndergroundConfig.defName = "IAM010.DEF";
  158. worldViewUndergroundConfig.additionalDefs.push_back("IAM003.DEF");
  159. worldViewUndergroundConfig.x = GH.screenDimensions().x - 115;
  160. worldViewUndergroundConfig.y = 343 + 195;
  161. worldViewUndergroundConfig.playerColoured = true;
  162. worldViewUnderground = makeButton(294, std::bind(&CAdventureMapInterface::fswitchLevel,this), worldViewUndergroundConfig, SDLK_u);
  163. panelWorldView->addChildColorableButton(worldViewUnderground);
  164. onCurrentPlayerChanged(LOCPLINT->playerID);
  165. int iconColorMultiplier = currentPlayerID.getNum() * 19;
  166. int wvLeft = heroList->pos.x - 2; // TODO correct drawing position
  167. //int wvTop = 195;
  168. for (int i = 0; i < 5; ++i)
  169. {
  170. panelWorldView->addChildIcon(std::pair<int, Point>(i, Point(5, 58 + i * 20)), iconColorMultiplier);
  171. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 263 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  172. Colors::WHITE, CGI->generaltexth->allTexts[612 + i]));
  173. }
  174. for (int i = 0; i < 7; ++i)
  175. {
  176. panelWorldView->addChildIcon(std::pair<int, Point>(i + 5, Point(5, 182 + i * 20)), iconColorMultiplier);
  177. panelWorldView->addChildIcon(std::pair<int, Point>(i + 12, Point(160, 182 + i * 20)), iconColorMultiplier);
  178. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 387 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  179. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  180. }
  181. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 5, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  182. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  183. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  184. Colors::WHITE, CGI->generaltexth->allTexts[618]));
  185. activeMapPanel = panelMain;
  186. exitWorldView();
  187. underground->block(!CGI->mh->getMap()->twoLevel);
  188. questlog->block(!CGI->mh->getMap()->quests.size());
  189. worldViewUnderground->block(!CGI->mh->getMap()->twoLevel);
  190. }
  191. void CAdventureMapInterface::fshowOverview()
  192. {
  193. GH.pushIntT<CKingdomInterface>();
  194. }
  195. void CAdventureMapInterface::fworldViewBack()
  196. {
  197. exitWorldView();
  198. auto hero = LOCPLINT->localState->getCurrentHero();
  199. if (hero)
  200. centerOnObject(hero);
  201. }
  202. void CAdventureMapInterface::fworldViewScale1x()
  203. {
  204. // TODO set corresponding scale button to "selected" mode
  205. openWorldView(7);
  206. }
  207. void CAdventureMapInterface::fworldViewScale2x()
  208. {
  209. openWorldView(11);
  210. }
  211. void CAdventureMapInterface::fworldViewScale4x()
  212. {
  213. openWorldView(16);
  214. }
  215. void CAdventureMapInterface::fswitchLevel()
  216. {
  217. // with support for future multi-level maps :)
  218. int maxLevels = CGI->mh->getMap()->levels();
  219. if (maxLevels < 2)
  220. return;
  221. terrain->onMapLevelSwitched();
  222. }
  223. void CAdventureMapInterface::onMapViewMoved(const Rect & visibleArea, int mapLevel)
  224. {
  225. underground->setIndex(mapLevel, true);
  226. underground->redraw();
  227. worldViewUnderground->setIndex(mapLevel, true);
  228. worldViewUnderground->redraw();
  229. minimap->onMapViewMoved(visibleArea, mapLevel);
  230. }
  231. void CAdventureMapInterface::onAudioResumed()
  232. {
  233. mapAudio->onAudioResumed();
  234. }
  235. void CAdventureMapInterface::onAudioPaused()
  236. {
  237. mapAudio->onAudioPaused();
  238. }
  239. void CAdventureMapInterface::fshowQuestlog()
  240. {
  241. LOCPLINT->showQuestLog();
  242. }
  243. void CAdventureMapInterface::fsleepWake()
  244. {
  245. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  246. if (!h)
  247. return;
  248. bool newSleep = !LOCPLINT->localState->isHeroSleeping(h);
  249. if (newSleep)
  250. LOCPLINT->localState->setHeroAsleep(h);
  251. else
  252. LOCPLINT->localState->setHeroAwaken(h);
  253. onHeroChanged(h);
  254. if (newSleep)
  255. fnextHero();
  256. // redraw to update the image of sleep/wake button
  257. panelMain->redraw();
  258. }
  259. void CAdventureMapInterface::fmoveHero()
  260. {
  261. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero();
  262. if (!h || !LOCPLINT->localState->hasPath(h) || CGI->mh->hasOngoingAnimations())
  263. return;
  264. LOCPLINT->moveHero(h, LOCPLINT->localState->getPath(h));
  265. }
  266. void CAdventureMapInterface::fshowSpellbok()
  267. {
  268. if (!LOCPLINT->localState->getCurrentHero()) //checking necessary values
  269. return;
  270. centerOnObject(LOCPLINT->localState->getCurrentHero());
  271. GH.pushIntT<CSpellWindow>(LOCPLINT->localState->getCurrentHero(), LOCPLINT, false);
  272. }
  273. void CAdventureMapInterface::fadventureOPtions()
  274. {
  275. GH.pushIntT<CAdventureOptions>();
  276. }
  277. void CAdventureMapInterface::fsystemOptions()
  278. {
  279. GH.pushIntT<SettingsMainWindow>();
  280. }
  281. void CAdventureMapInterface::fnextHero()
  282. {
  283. const auto * currHero = LOCPLINT->localState->getCurrentHero();
  284. const auto * nextHero = LOCPLINT->localState->getNextWanderingHero(currHero);
  285. if (nextHero)
  286. {
  287. LOCPLINT->localState->setSelection(nextHero);
  288. centerOnObject(nextHero);
  289. }
  290. }
  291. void CAdventureMapInterface::fendTurn()
  292. {
  293. if(!LOCPLINT->makingTurn)
  294. return;
  295. if(settings["adventure"]["heroReminder"].Bool())
  296. {
  297. for(auto hero : LOCPLINT->localState->getWanderingHeroes())
  298. {
  299. if(!LOCPLINT->localState->isHeroSleeping(hero) && hero->movement > 0)
  300. {
  301. // Only show hero reminder if conditions met:
  302. // - There still movement points
  303. // - Hero don't have a path or there not points for first step on path
  304. LOCPLINT->localState->verifyPath(hero);
  305. if(!LOCPLINT->localState->hasPath(hero))
  306. {
  307. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdventureMapInterface::endingTurn, this), nullptr );
  308. return;
  309. }
  310. auto path = LOCPLINT->localState->getPath(hero);
  311. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  312. {
  313. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdventureMapInterface::endingTurn, this), nullptr );
  314. return;
  315. }
  316. }
  317. }
  318. }
  319. endingTurn();
  320. }
  321. void CAdventureMapInterface::updateButtons()
  322. {
  323. const auto * hero = LOCPLINT->localState->getCurrentHero();
  324. sleepWake->block(!hero);
  325. spellbook->block(!hero);
  326. moveHero->block(!hero || !LOCPLINT->localState->hasPath(hero) || hero->movement == 0);
  327. const auto * nextSuitableHero = LOCPLINT->localState->getNextWanderingHero(hero);
  328. nextHero->block(nextSuitableHero == nullptr);
  329. if(hero)
  330. {
  331. bool state = LOCPLINT->localState->isHeroSleeping(hero);
  332. sleepWake->setIndex(state ? 1 : 0, true);
  333. sleepWake->assignedKeys = {state ? SDLK_w : SDLK_z};
  334. sleepWake->redraw();
  335. }
  336. }
  337. void CAdventureMapInterface::onHeroChanged(const CGHeroInstance *h)
  338. {
  339. heroList->update(h);
  340. if (h == LOCPLINT->localState->getCurrentHero())
  341. infoBar->showSelection();
  342. updateButtons();
  343. }
  344. void CAdventureMapInterface::onTownChanged(const CGTownInstance * town)
  345. {
  346. townList->update(town);
  347. infoBar->showSelection();
  348. }
  349. void CAdventureMapInterface::showInfoBoxMessage(const std::vector<Component> & components, std::string message, int timer)
  350. {
  351. infoBar->pushComponents(components, message, timer);
  352. }
  353. void CAdventureMapInterface::activate()
  354. {
  355. CIntObject::activate();
  356. if (!(active & KEYBOARD))
  357. CIntObject::activate(KEYBOARD);
  358. screenBuf = screen;
  359. GH.statusbar = statusbar;
  360. if(LOCPLINT)
  361. {
  362. LOCPLINT->cingconsole->activate();
  363. LOCPLINT->cingconsole->pos = this->pos;
  364. }
  365. if(state != EGameState::ENEMY_TURN)
  366. {
  367. assert(state == EGameState::MAKING_TURN);
  368. activeMapPanel->activate();
  369. if (state == EGameState::MAKING_TURN)
  370. {
  371. heroList->activate();
  372. townList->activate();
  373. infoBar->activate();
  374. }
  375. minimap->activate();
  376. terrain->activate();
  377. statusbar->activate();
  378. GH.fakeMouseMove(); //to restore the cursor
  379. }
  380. }
  381. void CAdventureMapInterface::deactivate()
  382. {
  383. CIntObject::deactivate();
  384. if(state != EGameState::ENEMY_TURN)
  385. {
  386. assert(state == EGameState::MAKING_TURN);
  387. CCS->curh->set(Cursor::Map::POINTER);
  388. activeMapPanel->deactivate();
  389. if (state == EGameState::MAKING_TURN)
  390. {
  391. heroList->deactivate();
  392. townList->deactivate();
  393. infoBar->deactivate();
  394. }
  395. minimap->deactivate();
  396. terrain->deactivate();
  397. statusbar->deactivate();
  398. }
  399. }
  400. void CAdventureMapInterface::showAll(SDL_Surface * to)
  401. {
  402. bg->draw(to, 0, 0);
  403. // if(state != EGameState::MAKING_TURN)
  404. // return;
  405. heroList->showAll(to);
  406. townList->showAll(to);
  407. infoBar->showAll(to);
  408. activeMapPanel->showAll(to);
  409. minimap->showAll(to);
  410. terrain->showAll(to);
  411. show(to);
  412. resdatabar->showAll(to);
  413. statusbar->show(to);
  414. LOCPLINT->cingconsole->show(to);
  415. }
  416. void CAdventureMapInterface::show(SDL_Surface * to)
  417. {
  418. // if(state != EGameState::MAKING_TURN)
  419. // return;
  420. handleMapScrollingUpdate();
  421. for(int i = 0; i < 4; i++)
  422. {
  423. if(settings["session"]["spectate"].Bool())
  424. gems[i]->setFrame(PlayerColor(1).getNum());
  425. else
  426. gems[i]->setFrame(LOCPLINT->playerID.getNum());
  427. }
  428. minimap->show(to);
  429. terrain->show(to);
  430. for(int i = 0; i < 4; i++)
  431. gems[i]->showAll(to);
  432. LOCPLINT->cingconsole->show(to);
  433. infoBar->show(to);
  434. statusbar->showAll(to);
  435. }
  436. void CAdventureMapInterface::handleMapScrollingUpdate()
  437. {
  438. uint32_t timePassed = GH.mainFPSmng->getElapsedMilliseconds();
  439. uint32_t scrollSpeedPixels = settings["adventure"]["scrollSpeedPixels"].Float();
  440. uint32_t scrollDistance = scrollSpeedPixels * timePassed / 1000;
  441. bool scrollingActive = !GH.isKeyboardCtrlDown() && isActive() && state == EGameState::MAKING_TURN;
  442. Point cursorPosition = GH.getCursorPosition();
  443. Point scrollDirection;
  444. if (cursorPosition.x < 15)
  445. scrollDirection.x = -1;
  446. if (cursorPosition.x > GH.screenDimensions().x - 15)
  447. scrollDirection.x = +1;
  448. if (cursorPosition.y < 15)
  449. scrollDirection.y = -1;
  450. if (cursorPosition.y > GH.screenDimensions().y - 15)
  451. scrollDirection.y = +1;
  452. Point scrollDelta = scrollDirection * scrollDistance;
  453. if (scrollingActive && scrollDelta != Point(0,0))
  454. terrain->onMapScrolled(scrollDelta);
  455. if (scrollDelta == Point(0,0) && !scrollingCursorSet)
  456. return;
  457. if(scrollDelta.x > 0)
  458. {
  459. if(scrollDelta.y < 0)
  460. CCS->curh->set(Cursor::Map::SCROLL_NORTHEAST);
  461. if(scrollDelta.y > 0)
  462. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  463. if(scrollDelta.y == 0)
  464. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  465. }
  466. if(scrollDelta.x < 0)
  467. {
  468. if(scrollDelta.y < 0)
  469. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  470. if(scrollDelta.y > 0)
  471. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  472. if(scrollDelta.y == 0)
  473. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  474. }
  475. if (scrollDelta.x == 0)
  476. {
  477. if(scrollDelta.y < 0)
  478. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  479. if(scrollDelta.y > 0)
  480. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  481. if(scrollDelta.y == 0)
  482. CCS->curh->set(Cursor::Map::POINTER);
  483. }
  484. scrollingCursorSet = scrollDelta != Point(0,0);
  485. }
  486. void CAdventureMapInterface::centerOnTile(int3 on)
  487. {
  488. terrain->onCenteredTile(on);
  489. }
  490. void CAdventureMapInterface::centerOnObject(const CGObjectInstance * obj)
  491. {
  492. terrain->onCenteredObject(obj);
  493. }
  494. void CAdventureMapInterface::keyPressed(const SDL_Keycode & key)
  495. {
  496. if (state != EGameState::MAKING_TURN)
  497. return;
  498. const CGHeroInstance *h = LOCPLINT->localState->getCurrentHero(); //selected hero
  499. const CGTownInstance *t = LOCPLINT->localState->getCurrentTown(); //selected town
  500. switch(key)
  501. {
  502. case SDLK_g:
  503. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  504. return;
  505. {
  506. //find first town with tavern
  507. auto itr = range::find_if(LOCPLINT->localState->getOwnedTowns(), [](const CGTownInstance * town)
  508. {
  509. return town->hasBuilt(BuildingID::TAVERN);
  510. });
  511. if(itr != LOCPLINT->localState->getOwnedTowns().end())
  512. LOCPLINT->showThievesGuildWindow(*itr);
  513. else
  514. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  515. }
  516. return;
  517. case SDLK_i:
  518. if(isActive())
  519. CAdventureOptions::showScenarioInfo();
  520. return;
  521. case SDLK_s:
  522. if(isActive())
  523. GH.pushIntT<CSavingScreen>();
  524. return;
  525. case SDLK_l:
  526. if(isActive())
  527. LOCPLINT->proposeLoadingGame();
  528. return;
  529. case SDLK_d:
  530. {
  531. if(h && isActive() && LOCPLINT->makingTurn)
  532. LOCPLINT->tryDiggging(h);
  533. return;
  534. }
  535. case SDLK_p:
  536. if(isActive())
  537. LOCPLINT->showPuzzleMap();
  538. return;
  539. case SDLK_v:
  540. if(isActive())
  541. LOCPLINT->viewWorldMap();
  542. return;
  543. case SDLK_r:
  544. if(isActive() && GH.isKeyboardCtrlDown())
  545. {
  546. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  547. [](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
  548. }
  549. return;
  550. case SDLK_SPACE: //space - try to revisit current object with selected hero
  551. {
  552. if(!isActive())
  553. return;
  554. if(h)
  555. {
  556. LOCPLINT->cb->moveHero(h,h->pos);
  557. }
  558. }
  559. return;
  560. case SDLK_RETURN:
  561. {
  562. if(!isActive() || !LOCPLINT->localState->getCurrentArmy())
  563. return;
  564. if(h)
  565. LOCPLINT->openHeroWindow(h);
  566. else if(t)
  567. LOCPLINT->openTownWindow(t);
  568. return;
  569. }
  570. case SDLK_ESCAPE:
  571. {
  572. //FIXME: this case is never executed since AdvMapInt is disabled while in spellcasting mode
  573. if(!isActive() || GH.topInt().get() != this || !spellBeingCasted)
  574. return;
  575. abortCastingMode();
  576. return;
  577. }
  578. case SDLK_t:
  579. {
  580. //act on key down if marketplace windows is not already opened
  581. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  582. return;
  583. if(GH.isKeyboardCtrlDown()) //CTRL + T => open marketplace
  584. {
  585. //check if we have any marketplace
  586. const CGTownInstance *townWithMarket = nullptr;
  587. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  588. {
  589. if(t->hasBuilt(BuildingID::MARKETPLACE))
  590. {
  591. townWithMarket = t;
  592. break;
  593. }
  594. }
  595. if(townWithMarket) //if any town has marketplace, open window
  596. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  597. else //if not - complain
  598. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  599. }
  600. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  601. {
  602. townList->selectNext();
  603. }
  604. return;
  605. }
  606. case SDLK_LALT:
  607. case SDLK_RALT:
  608. {
  609. //fake mouse use to trigger onTileHovered()
  610. GH.fakeMouseMove();
  611. return;
  612. }
  613. default:
  614. {
  615. auto direction = keyToMoveDirection(key);
  616. if (!direction)
  617. return;
  618. if(!h || !isActive())
  619. return;
  620. if (CGI->mh->hasOngoingAnimations())
  621. return;
  622. if(*direction == Point(0,0))
  623. {
  624. centerOnObject(h);
  625. return;
  626. }
  627. int3 dst = h->visitablePos() + int3(direction->x, direction->y, 0);
  628. if (!CGI->mh->isInMap((dst)))
  629. return;
  630. if ( !LOCPLINT->localState->setPath(h, dst))
  631. return;
  632. const CGPath & path = LOCPLINT->localState->getPath(h);
  633. if (path.nodes.size() > 2)
  634. onHeroChanged(h);
  635. else
  636. if(!path.nodes[0].turns)
  637. LOCPLINT->moveHero(h, path);
  638. }
  639. return;
  640. }
  641. }
  642. std::optional<Point> CAdventureMapInterface::keyToMoveDirection(const SDL_Keycode & key)
  643. {
  644. switch (key) {
  645. case SDLK_DOWN: return Point( 0, +1);
  646. case SDLK_LEFT: return Point(-1, 0);
  647. case SDLK_RIGHT: return Point(+1, 0);
  648. case SDLK_UP: return Point( 0, -1);
  649. case SDLK_KP_1: return Point(-1, +1);
  650. case SDLK_KP_2: return Point( 0, +1);
  651. case SDLK_KP_3: return Point(+1, +1);
  652. case SDLK_KP_4: return Point(-1, 0);
  653. case SDLK_KP_5: return Point( 0, 0);
  654. case SDLK_KP_6: return Point(+1, 0);
  655. case SDLK_KP_7: return Point(-1, -1);
  656. case SDLK_KP_8: return Point( 0, -1);
  657. case SDLK_KP_9: return Point(+1, -1);
  658. }
  659. return std::nullopt;
  660. }
  661. void CAdventureMapInterface::onSelectionChanged(const CArmedInstance *sel)
  662. {
  663. assert(sel);
  664. infoBar->popAll();
  665. mapAudio->onSelectionChanged(sel);
  666. bool centerView = !settings["session"]["autoSkip"].Bool();
  667. if (centerView)
  668. centerOnObject(sel);
  669. if(sel->ID==Obj::TOWN)
  670. {
  671. auto town = dynamic_cast<const CGTownInstance*>(sel);
  672. infoBar->showTownSelection(town);
  673. townList->select(town);
  674. heroList->select(nullptr);
  675. onHeroChanged(nullptr);
  676. }
  677. else //hero selected
  678. {
  679. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  680. infoBar->showHeroSelection(hero);
  681. heroList->select(hero);
  682. townList->select(nullptr);
  683. LOCPLINT->localState->verifyPath(hero);
  684. onHeroChanged(hero);
  685. }
  686. updateButtons();
  687. townList->redraw();
  688. heroList->redraw();
  689. }
  690. bool CAdventureMapInterface::isActive()
  691. {
  692. return active & ~CIntObject::KEYBOARD;
  693. }
  694. void CAdventureMapInterface::onMapTilesChanged(boost::optional<std::unordered_set<int3>> positions)
  695. {
  696. if (positions)
  697. minimap->updateTiles(*positions);
  698. else
  699. minimap->update();
  700. }
  701. void CAdventureMapInterface::onHotseatWaitStarted(PlayerColor playerID)
  702. {
  703. onCurrentPlayerChanged(playerID);
  704. state = EGameState::HOTSEAT_WAIT;
  705. }
  706. void CAdventureMapInterface::onEnemyTurnStarted(PlayerColor playerID)
  707. {
  708. if(settings["session"]["spectate"].Bool())
  709. return;
  710. adjustActiveness(true);
  711. mapAudio->onEnemyTurnStarted();
  712. minimap->setAIRadar(true);
  713. infoBar->startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  714. minimap->showAll(screen);//force refresh on inactive object
  715. infoBar->showAll(screen);//force refresh on inactive object
  716. }
  717. void CAdventureMapInterface::adjustActiveness(bool aiTurnStart)
  718. {
  719. bool wasActive = isActive();
  720. if(wasActive)
  721. deactivate();
  722. if (aiTurnStart)
  723. state = EGameState::ENEMY_TURN;
  724. else
  725. state = EGameState::MAKING_TURN;
  726. if(wasActive)
  727. activate();
  728. }
  729. void CAdventureMapInterface::onCurrentPlayerChanged(PlayerColor playerID)
  730. {
  731. LOCPLINT->localState->setSelection(nullptr);
  732. if (playerID == currentPlayerID)
  733. return;
  734. currentPlayerID = playerID;
  735. bg->playerColored(currentPlayerID);
  736. panelMain->setPlayerColor(currentPlayerID);
  737. panelWorldView->setPlayerColor(currentPlayerID);
  738. panelWorldView->recolorIcons(currentPlayerID, currentPlayerID.getNum() * 19);
  739. resdatabar->colorize(currentPlayerID);
  740. }
  741. void CAdventureMapInterface::onPlayerTurnStarted(PlayerColor playerID)
  742. {
  743. onCurrentPlayerChanged(playerID);
  744. state = EGameState::MAKING_TURN;
  745. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  746. || settings["session"]["spectate"].Bool())
  747. {
  748. adjustActiveness(false);
  749. minimap->setAIRadar(false);
  750. infoBar->showSelection();
  751. }
  752. heroList->update();
  753. townList->update();
  754. const CGHeroInstance * heroToSelect = nullptr;
  755. // find first non-sleeping hero
  756. for (auto hero : LOCPLINT->localState->getWanderingHeroes())
  757. {
  758. if (!LOCPLINT->localState->isHeroSleeping(hero))
  759. {
  760. heroToSelect = hero;
  761. break;
  762. }
  763. }
  764. //select first hero if available.
  765. if (heroToSelect != nullptr)
  766. {
  767. LOCPLINT->localState->setSelection(heroToSelect);
  768. }
  769. else if (LOCPLINT->localState->getOwnedTowns().size())
  770. {
  771. LOCPLINT->localState->setSelection(LOCPLINT->localState->getOwnedTown(0));
  772. }
  773. else
  774. {
  775. LOCPLINT->localState->setSelection(LOCPLINT->localState->getWanderingHero(0));
  776. }
  777. //show new day animation and sound on infobar
  778. infoBar->showDate();
  779. onHeroChanged(nullptr);
  780. showAll(screen);
  781. mapAudio->onPlayerTurnStarted();
  782. if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown())
  783. {
  784. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt().get()))
  785. iw->close();
  786. endingTurn();
  787. }
  788. }
  789. void CAdventureMapInterface::endingTurn()
  790. {
  791. if(settings["session"]["spectate"].Bool())
  792. return;
  793. LOCPLINT->makingTurn = false;
  794. LOCPLINT->cb->endTurn();
  795. mapAudio->onPlayerTurnEnded();
  796. }
  797. const CGObjectInstance* CAdventureMapInterface::getActiveObject(const int3 &mapPos)
  798. {
  799. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  800. if (bobjs.empty())
  801. return nullptr;
  802. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  803. /*
  804. if (bobjs.back()->ID == Obj::HERO)
  805. return bobjs.back();
  806. else
  807. return bobjs.front();*/
  808. }
  809. void CAdventureMapInterface::onTileLeftClicked(const int3 &mapPos)
  810. {
  811. if(state != EGameState::MAKING_TURN)
  812. return;
  813. //FIXME: this line breaks H3 behavior for Dimension Door
  814. if(!LOCPLINT->cb->isVisible(mapPos))
  815. return;
  816. if(!LOCPLINT->makingTurn)
  817. return;
  818. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  819. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  820. int3 selPos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  821. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  822. {
  823. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  824. switch(spellBeingCasted->id)
  825. {
  826. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  827. if(topBlocking && topBlocking->ID == Obj::BOAT)
  828. leaveCastingMode(mapPos);
  829. break;
  830. case SpellID::DIMENSION_DOOR:
  831. if(!tile || tile->isClear(heroTile))
  832. leaveCastingMode(mapPos);
  833. break;
  834. }
  835. return;
  836. }
  837. //check if we can select this object
  838. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  839. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  840. bool isHero = false;
  841. if(LOCPLINT->localState->getCurrentArmy()->ID != Obj::HERO) //hero is not selected (presumably town)
  842. {
  843. if(LOCPLINT->localState->getCurrentArmy() == topBlocking) //selected town clicked
  844. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  845. else if(canSelect)
  846. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  847. }
  848. else if(const CGHeroInstance * currentHero = LOCPLINT->localState->getCurrentHero()) //hero is selected
  849. {
  850. isHero = true;
  851. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  852. if(currentHero == topBlocking) //clicked selected hero
  853. {
  854. LOCPLINT->openHeroWindow(currentHero);
  855. return;
  856. }
  857. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  858. {
  859. LOCPLINT->localState->setSelection(static_cast<const CArmedInstance*>(topBlocking));
  860. return;
  861. }
  862. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  863. {
  864. if(LOCPLINT->localState->hasPath(currentHero) &&
  865. LOCPLINT->localState->getPath(currentHero).endPos() == mapPos)//we'll be moving
  866. {
  867. if(!CGI->mh->hasOngoingAnimations())
  868. LOCPLINT->moveHero(currentHero, LOCPLINT->localState->getPath(currentHero));
  869. return;
  870. }
  871. else //remove old path and find a new one if we clicked on accessible tile
  872. {
  873. LOCPLINT->localState->setPath(currentHero, mapPos);
  874. onHeroChanged(currentHero);
  875. }
  876. }
  877. } //end of hero is selected "case"
  878. else
  879. {
  880. throw std::runtime_error("Nothing is selected...");
  881. }
  882. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  883. if(isHero && shipyard != nullptr)
  884. {
  885. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  886. }
  887. }
  888. void CAdventureMapInterface::onTileHovered(const int3 &mapPos)
  889. {
  890. if(state != EGameState::MAKING_TURN)
  891. return;
  892. //may occur just at the start of game (fake move before full intiialization)
  893. if(!LOCPLINT->localState->getCurrentArmy())
  894. return;
  895. if(!LOCPLINT->cb->isVisible(mapPos))
  896. {
  897. CCS->curh->set(Cursor::Map::POINTER);
  898. statusbar->clear();
  899. return;
  900. }
  901. auto objRelations = PlayerRelations::ALLIES;
  902. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  903. if(objAtTile)
  904. {
  905. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  906. std::string text = LOCPLINT->localState->getCurrentHero() ? objAtTile->getHoverText(LOCPLINT->localState->getCurrentHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  907. boost::replace_all(text,"\n"," ");
  908. statusbar->write(text);
  909. }
  910. else
  911. {
  912. std::string hlp = CGI->mh->getTerrainDescr(mapPos, false);
  913. statusbar->write(hlp);
  914. }
  915. if(spellBeingCasted)
  916. {
  917. switch(spellBeingCasted->id)
  918. {
  919. case SpellID::SCUTTLE_BOAT:
  920. if(objAtTile && objAtTile->ID == Obj::BOAT)
  921. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  922. else
  923. CCS->curh->set(Cursor::Map::POINTER);
  924. return;
  925. case SpellID::DIMENSION_DOOR:
  926. {
  927. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  928. int3 hpos = LOCPLINT->localState->getCurrentArmy()->getSightCenter();
  929. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  930. CCS->curh->set(Cursor::Map::TELEPORT);
  931. else
  932. CCS->curh->set(Cursor::Map::POINTER);
  933. return;
  934. }
  935. }
  936. }
  937. if(LOCPLINT->localState->getCurrentArmy()->ID == Obj::TOWN)
  938. {
  939. if(objAtTile)
  940. {
  941. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  942. CCS->curh->set(Cursor::Map::TOWN);
  943. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  944. CCS->curh->set(Cursor::Map::HERO);
  945. else
  946. CCS->curh->set(Cursor::Map::POINTER);
  947. }
  948. else
  949. CCS->curh->set(Cursor::Map::POINTER);
  950. }
  951. else if(const CGHeroInstance * hero = LOCPLINT->localState->getCurrentHero())
  952. {
  953. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  954. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  955. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  956. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  957. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  958. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  959. 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, };
  960. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  961. assert(pathNode);
  962. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  963. {
  964. showMoveDetailsInStatusbar(*hero, *pathNode);
  965. }
  966. int turns = pathNode->turns;
  967. vstd::amin(turns, 3);
  968. switch(pathNode->action)
  969. {
  970. case CGPathNode::NORMAL:
  971. case CGPathNode::TELEPORT_NORMAL:
  972. if(pathNode->layer == EPathfindingLayer::LAND)
  973. CCS->curh->set(cursorMove[turns]);
  974. else
  975. CCS->curh->set(cursorSailVisit[turns]);
  976. break;
  977. case CGPathNode::VISIT:
  978. case CGPathNode::BLOCKING_VISIT:
  979. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  980. if(objAtTile && objAtTile->ID == Obj::HERO)
  981. {
  982. if(LOCPLINT->localState->getCurrentArmy() == objAtTile)
  983. CCS->curh->set(Cursor::Map::HERO);
  984. else
  985. CCS->curh->set(cursorExchange[turns]);
  986. }
  987. else if(pathNode->layer == EPathfindingLayer::LAND)
  988. CCS->curh->set(cursorVisit[turns]);
  989. else
  990. CCS->curh->set(cursorSailVisit[turns]);
  991. break;
  992. case CGPathNode::BATTLE:
  993. case CGPathNode::TELEPORT_BATTLE:
  994. CCS->curh->set(cursorAttack[turns]);
  995. break;
  996. case CGPathNode::EMBARK:
  997. CCS->curh->set(cursorSail[turns]);
  998. break;
  999. case CGPathNode::DISEMBARK:
  1000. CCS->curh->set(cursorDisembark[turns]);
  1001. break;
  1002. default:
  1003. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1004. {
  1005. if(objAtTile->ID == Obj::TOWN)
  1006. CCS->curh->set(Cursor::Map::TOWN);
  1007. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1008. CCS->curh->set(Cursor::Map::HERO);
  1009. else
  1010. CCS->curh->set(Cursor::Map::POINTER);
  1011. }
  1012. else
  1013. CCS->curh->set(Cursor::Map::POINTER);
  1014. break;
  1015. }
  1016. }
  1017. if(ourInaccessibleShipyard(objAtTile))
  1018. {
  1019. CCS->curh->set(Cursor::Map::T1_SAIL);
  1020. }
  1021. }
  1022. void CAdventureMapInterface::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  1023. {
  1024. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  1025. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  1026. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  1027. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  1028. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  1029. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  1030. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  1031. statusbar->write(result);
  1032. }
  1033. void CAdventureMapInterface::onTileRightClicked(const int3 &mapPos)
  1034. {
  1035. if(state != EGameState::MAKING_TURN)
  1036. return;
  1037. if(spellBeingCasted)
  1038. {
  1039. abortCastingMode();
  1040. return;
  1041. }
  1042. if(!LOCPLINT->cb->isVisible(mapPos))
  1043. {
  1044. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1045. return;
  1046. }
  1047. const CGObjectInstance * obj = getActiveObject(mapPos);
  1048. if(!obj)
  1049. {
  1050. // Bare or undiscovered terrain
  1051. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1052. if(tile)
  1053. {
  1054. std::string hlp = CGI->mh->getTerrainDescr(mapPos, true);
  1055. CRClickPopup::createAndPush(hlp);
  1056. }
  1057. return;
  1058. }
  1059. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  1060. }
  1061. void CAdventureMapInterface::enterCastingMode(const CSpell * sp)
  1062. {
  1063. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1064. spellBeingCasted = sp;
  1065. deactivate();
  1066. terrain->activate();
  1067. GH.fakeMouseMove();
  1068. }
  1069. void CAdventureMapInterface::abortCastingMode()
  1070. {
  1071. assert(spellBeingCasted);
  1072. spellBeingCasted = nullptr;
  1073. terrain->deactivate();
  1074. activate();
  1075. }
  1076. void CAdventureMapInterface::leaveCastingMode(const int3 & dest)
  1077. {
  1078. SpellID id = spellBeingCasted->id;
  1079. abortCastingMode();
  1080. // if(cast)
  1081. LOCPLINT->cb->castSpell(LOCPLINT->localState->getCurrentHero(), id, dest);
  1082. // else
  1083. // LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1084. }
  1085. Rect CAdventureMapInterface::terrainAreaPixels() const
  1086. {
  1087. return terrain->pos;
  1088. }
  1089. const IShipyard * CAdventureMapInterface::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1090. {
  1091. const IShipyard *ret = IShipyard::castFrom(obj);
  1092. if(!ret ||
  1093. obj->tempOwner != currentPlayerID ||
  1094. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  1095. return nullptr;
  1096. return ret;
  1097. }
  1098. void CAdventureMapInterface::exitWorldView()
  1099. {
  1100. state = EGameState::MAKING_TURN;
  1101. panelMain->activate();
  1102. panelWorldView->deactivate();
  1103. activeMapPanel = panelMain;
  1104. townList->activate();
  1105. heroList->activate();
  1106. infoBar->activate();
  1107. redraw();
  1108. terrain->onViewMapActivated();
  1109. }
  1110. void CAdventureMapInterface::openWorldView(int tileSize)
  1111. {
  1112. state = EGameState::WORLD_VIEW;
  1113. panelMain->deactivate();
  1114. panelWorldView->activate();
  1115. activeMapPanel = panelWorldView;
  1116. townList->deactivate();
  1117. heroList->deactivate();
  1118. infoBar->showSelection(); // to prevent new day animation interfering world view mode
  1119. infoBar->deactivate();
  1120. redraw();
  1121. terrain->onViewWorldActivated(tileSize);
  1122. }
  1123. void CAdventureMapInterface::openWorldView()
  1124. {
  1125. openWorldView(11);
  1126. }
  1127. void CAdventureMapInterface::openWorldView(const std::vector<ObjectPosInfo>& objectPositions, bool showTerrain)
  1128. {
  1129. openWorldView(11);
  1130. terrain->onViewSpellActivated(11, objectPositions, showTerrain);
  1131. }