CAdvMapInt.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  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 "CAdvMapInt.h"
  12. #include "CAdvMapPanel.h"
  13. #include "CAdventureOptions.h"
  14. #include "CInGameConsole.h"
  15. #include "CMinimap.h"
  16. #include "CResDataBar.h"
  17. #include "CTerrainRect.h"
  18. #include "CList.h"
  19. #include "CInfoBar.h"
  20. #include "mapHandler.h"
  21. #include "../windows/CKingdomInterface.h"
  22. #include "../windows/CSpellWindow.h"
  23. #include "../windows/CTradeWindow.h"
  24. #include "../windows/GUIClasses.h"
  25. #include "../windows/InfoWindows.h"
  26. #include "../CGameInfo.h"
  27. #include "../CMusicHandler.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 "../../CCallback.h"
  39. #include "../../lib/CConfigHandler.h"
  40. #include "../../lib/CGeneralTextHandler.h"
  41. #include "../../lib/spells/CSpellHandler.h"
  42. #include "../../lib/mapObjects/CGHeroInstance.h"
  43. #include "../../lib/mapObjects/CGTownInstance.h"
  44. #include "../../lib/CPathfinder.h"
  45. #include "../../lib/mapping/CMap.h"
  46. #include "../../lib/UnlockGuard.h"
  47. #include "../../lib/TerrainHandler.h"
  48. #define ADVOPT (conf.go()->ac)
  49. std::shared_ptr<CAdvMapInt> adventureInt;
  50. void CAdvMapInt::setScrollingCursor(ui8 direction) const
  51. {
  52. if(direction & CAdvMapInt::RIGHT)
  53. {
  54. if(direction & CAdvMapInt::UP)
  55. CCS->curh->set(Cursor::Map::SCROLL_NORTHEAST);
  56. else if(direction & CAdvMapInt::DOWN)
  57. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  58. else
  59. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  60. }
  61. else if(direction & CAdvMapInt::LEFT)
  62. {
  63. if(direction & CAdvMapInt::UP)
  64. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  65. else if(direction & CAdvMapInt::DOWN)
  66. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  67. else
  68. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  69. }
  70. else if(direction & CAdvMapInt::UP)
  71. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  72. else if(direction & CAdvMapInt::DOWN)
  73. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  74. }
  75. CAdvMapInt::CAdvMapInt():
  76. mode(EAdvMapMode::NORMAL),
  77. minimap(new CMinimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH))),
  78. statusbar(CGStatusBar::create(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG)),
  79. heroList(new CHeroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD)),
  80. townList(new CTownList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD)),
  81. infoBar(new CInfoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192))),
  82. resdatabar(new CResDataBar),
  83. terrain(new CTerrainRect),
  84. state(NA),
  85. spellBeingCasted(nullptr), selection(nullptr),
  86. activeMapPanel(nullptr), duringAITurn(false), scrollingDir(0), scrollingState(false),
  87. swipeEnabled(settings["general"]["swipe"].Bool()), swipeMovementRequested(false),
  88. swipeTargetPosition(Point(0, 0))
  89. {
  90. pos.x = pos.y = 0;
  91. pos.w = GH.screenDimensions().x;
  92. pos.h = GH.screenDimensions().y;
  93. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  94. townList->onSelect = std::bind(&CAdvMapInt::selectionChanged,this);
  95. bg = IImage::createFromFile(ADVOPT.mainGraphic);
  96. if(!ADVOPT.worldViewGraphic.empty())
  97. {
  98. bgWorldView = IImage::createFromFile(ADVOPT.worldViewGraphic);
  99. }
  100. else
  101. {
  102. bgWorldView = nullptr;
  103. logGlobal->warn("ADVOPT.worldViewGraphic is empty => bitmap not loaded");
  104. }
  105. if (!bgWorldView)
  106. {
  107. logGlobal->warn("bgWorldView not defined in resolution config; fallback to VWorld.bmp");
  108. bgWorldView = IImage::createFromFile("VWorld.bmp");
  109. }
  110. worldViewIcons = std::make_shared<CAnimation>("VwSymbol");//todo: customize with ADVOPT
  111. worldViewIcons->preload();
  112. for(int g = 0; g < ADVOPT.gemG.size(); ++g)
  113. {
  114. gems.push_back(std::make_shared<CAnimImage>(ADVOPT.gemG[g], 0, 0, ADVOPT.gemX[g], ADVOPT.gemY[g]));
  115. }
  116. auto makeButton = [&](int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> std::shared_ptr<CButton>
  117. {
  118. auto button = std::make_shared<CButton>(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  119. for(auto image : info.additionalDefs)
  120. button->addImage(image);
  121. return button;
  122. };
  123. kingOverview = makeButton(293, std::bind(&CAdvMapInt::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  124. underground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  125. questlog = makeButton(295, std::bind(&CAdvMapInt::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  126. sleepWake = makeButton(296, std::bind(&CAdvMapInt::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  127. moveHero = makeButton(297, std::bind(&CAdvMapInt::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  128. spellbook = makeButton(298, std::bind(&CAdvMapInt::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  129. advOptions = makeButton(299, std::bind(&CAdvMapInt::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  130. sysOptions = makeButton(300, std::bind(&CAdvMapInt::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  131. nextHero = makeButton(301, std::bind(&CAdvMapInt::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  132. endTurn = makeButton(302, std::bind(&CAdvMapInt::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  133. int panelSpaceBottom = GH.screenDimensions().y - resdatabar->pos.h - 4;
  134. panelMain = std::make_shared<CAdvMapPanel>(nullptr, Point(0, 0));
  135. // TODO correct drawing position
  136. panelWorldView = std::make_shared<CAdvMapWorldViewPanel>(worldViewIcons, bgWorldView, Point(heroList->pos.x - 2, 195), panelSpaceBottom, LOCPLINT->playerID);
  137. panelMain->addChildColorableButton(kingOverview);
  138. panelMain->addChildColorableButton(underground);
  139. panelMain->addChildColorableButton(questlog);
  140. panelMain->addChildColorableButton(sleepWake);
  141. panelMain->addChildColorableButton(moveHero);
  142. panelMain->addChildColorableButton(spellbook);
  143. panelMain->addChildColorableButton(advOptions);
  144. panelMain->addChildColorableButton(sysOptions);
  145. panelMain->addChildColorableButton(nextHero);
  146. panelMain->addChildColorableButton(endTurn);
  147. // TODO move configs to resolutions.json, similarly to previous buttons
  148. config::ButtonInfo worldViewBackConfig = config::ButtonInfo();
  149. worldViewBackConfig.defName = "IOK6432.DEF";
  150. worldViewBackConfig.x = GH.screenDimensions().x - 73;
  151. worldViewBackConfig.y = 343 + 195;
  152. worldViewBackConfig.playerColoured = false;
  153. panelWorldView->addChildToPanel(
  154. makeButton(288, std::bind(&CAdvMapInt::fworldViewBack,this), worldViewBackConfig, SDLK_ESCAPE), ACTIVATE | DEACTIVATE);
  155. config::ButtonInfo worldViewPuzzleConfig = config::ButtonInfo();
  156. worldViewPuzzleConfig.defName = "VWPUZ.DEF";
  157. worldViewPuzzleConfig.x = GH.screenDimensions().x - 188;
  158. worldViewPuzzleConfig.y = 343 + 195;
  159. worldViewPuzzleConfig.playerColoured = false;
  160. panelWorldView->addChildToPanel( // no help text for this one
  161. std::make_shared<CButton>(Point(worldViewPuzzleConfig.x, worldViewPuzzleConfig.y), worldViewPuzzleConfig.defName, std::pair<std::string, std::string>(),
  162. std::bind(&CPlayerInterface::showPuzzleMap,LOCPLINT), SDLK_p, worldViewPuzzleConfig.playerColoured), ACTIVATE | DEACTIVATE);
  163. config::ButtonInfo worldViewScale1xConfig = config::ButtonInfo();
  164. worldViewScale1xConfig.defName = "VWMAG1.DEF";
  165. worldViewScale1xConfig.x = GH.screenDimensions().x - 191;
  166. worldViewScale1xConfig.y = 23 + 195;
  167. worldViewScale1xConfig.playerColoured = false;
  168. panelWorldView->addChildToPanel( // help text is wrong for this button
  169. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale1x,this), worldViewScale1xConfig, SDLK_1), ACTIVATE | DEACTIVATE);
  170. config::ButtonInfo worldViewScale2xConfig = config::ButtonInfo();
  171. worldViewScale2xConfig.defName = "VWMAG2.DEF";
  172. worldViewScale2xConfig.x = GH.screenDimensions().x- 191 + 63;
  173. worldViewScale2xConfig.y = 23 + 195;
  174. worldViewScale2xConfig.playerColoured = false;
  175. panelWorldView->addChildToPanel( // help text is wrong for this button
  176. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale2x,this), worldViewScale2xConfig, SDLK_2), ACTIVATE | DEACTIVATE);
  177. config::ButtonInfo worldViewScale4xConfig = config::ButtonInfo();
  178. worldViewScale4xConfig.defName = "VWMAG4.DEF";
  179. worldViewScale4xConfig.x = GH.screenDimensions().x- 191 + 126;
  180. worldViewScale4xConfig.y = 23 + 195;
  181. worldViewScale4xConfig.playerColoured = false;
  182. panelWorldView->addChildToPanel( // help text is wrong for this button
  183. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale4x,this), worldViewScale4xConfig, SDLK_4), ACTIVATE | DEACTIVATE);
  184. config::ButtonInfo worldViewUndergroundConfig = config::ButtonInfo();
  185. worldViewUndergroundConfig.defName = "IAM010.DEF";
  186. worldViewUndergroundConfig.additionalDefs.push_back("IAM003.DEF");
  187. worldViewUndergroundConfig.x = GH.screenDimensions().x - 115;
  188. worldViewUndergroundConfig.y = 343 + 195;
  189. worldViewUndergroundConfig.playerColoured = true;
  190. worldViewUnderground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), worldViewUndergroundConfig, SDLK_u);
  191. panelWorldView->addChildColorableButton(worldViewUnderground);
  192. setPlayer(LOCPLINT->playerID);
  193. int iconColorMultiplier = player.getNum() * 19;
  194. int wvLeft = heroList->pos.x - 2; // TODO correct drawing position
  195. //int wvTop = 195;
  196. for (int i = 0; i < 5; ++i)
  197. {
  198. panelWorldView->addChildIcon(std::pair<int, Point>(i, Point(5, 58 + i * 20)), iconColorMultiplier);
  199. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 263 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  200. Colors::WHITE, CGI->generaltexth->allTexts[612 + i]));
  201. }
  202. for (int i = 0; i < 7; ++i)
  203. {
  204. panelWorldView->addChildIcon(std::pair<int, Point>(i + 5, Point(5, 182 + i * 20)), iconColorMultiplier);
  205. panelWorldView->addChildIcon(std::pair<int, Point>(i + 12, Point(160, 182 + i * 20)), iconColorMultiplier);
  206. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 387 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  207. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  208. }
  209. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 5, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  210. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  211. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  212. Colors::WHITE, CGI->generaltexth->allTexts[618]));
  213. activeMapPanel = panelMain;
  214. changeMode(EAdvMapMode::NORMAL);
  215. underground->block(!CGI->mh->getMap()->twoLevel);
  216. questlog->block(!CGI->mh->getMap()->quests.size());
  217. worldViewUnderground->block(!CGI->mh->getMap()->twoLevel);
  218. addUsedEvents(MOVE);
  219. }
  220. void CAdvMapInt::fshowOverview()
  221. {
  222. GH.pushIntT<CKingdomInterface>();
  223. }
  224. void CAdvMapInt::fworldViewBack()
  225. {
  226. changeMode(EAdvMapMode::NORMAL);
  227. auto hero = curHero();
  228. if (hero)
  229. centerOn(hero);
  230. }
  231. void CAdvMapInt::fworldViewScale1x()
  232. {
  233. // TODO set corresponding scale button to "selected" mode
  234. changeMode(EAdvMapMode::WORLD_VIEW, 7); // 7 pixels per tile
  235. }
  236. void CAdvMapInt::fworldViewScale2x()
  237. {
  238. changeMode(EAdvMapMode::WORLD_VIEW, 11); // 11 pixels per tile
  239. }
  240. void CAdvMapInt::fworldViewScale4x()
  241. {
  242. changeMode(EAdvMapMode::WORLD_VIEW, 16); // 16 pixels per tile
  243. }
  244. void CAdvMapInt::fswitchLevel()
  245. {
  246. // with support for future multi-level maps :)
  247. int maxLevels = CGI->mh->getMap()->levels();
  248. if (maxLevels < 2)
  249. return;
  250. terrain->setLevel((terrain->getLevel() + 1) % maxLevels);
  251. underground->setIndex(terrain->getLevel(), true);
  252. underground->redraw();
  253. worldViewUnderground->setIndex(terrain->getLevel(), true);
  254. worldViewUnderground->redraw();
  255. minimap->setLevel(terrain->getLevel());
  256. }
  257. void CAdvMapInt::fshowQuestlog()
  258. {
  259. LOCPLINT->showQuestLog();
  260. }
  261. void CAdvMapInt::fsleepWake()
  262. {
  263. const CGHeroInstance *h = curHero();
  264. if (!h)
  265. return;
  266. bool newSleep = !isHeroSleeping(h);
  267. setHeroSleeping(h, newSleep);
  268. updateSleepWake(h);
  269. if (newSleep)
  270. {
  271. fnextHero();
  272. //moveHero.block(true);
  273. //uncomment to enable original HoMM3 behaviour:
  274. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  275. }
  276. }
  277. void CAdvMapInt::fmoveHero()
  278. {
  279. const CGHeroInstance *h = curHero();
  280. if (!h || !LOCPLINT->paths.hasPath(h) || CGI->mh->hasOngoingAnimations())
  281. return;
  282. LOCPLINT->moveHero(h, LOCPLINT->paths.getPath(h));
  283. }
  284. void CAdvMapInt::fshowSpellbok()
  285. {
  286. if (!curHero()) //checking necessary values
  287. return;
  288. centerOn(selection);
  289. GH.pushIntT<CSpellWindow>(curHero(), LOCPLINT, false);
  290. }
  291. void CAdvMapInt::fadventureOPtions()
  292. {
  293. GH.pushIntT<CAdventureOptions>();
  294. }
  295. void CAdvMapInt::fsystemOptions()
  296. {
  297. GH.pushIntT<SettingsMainWindow>();
  298. }
  299. void CAdvMapInt::fnextHero()
  300. {
  301. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  302. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  303. if (next < 0)
  304. return;
  305. select(LOCPLINT->wanderingHeroes[next], true);
  306. }
  307. void CAdvMapInt::fendTurn()
  308. {
  309. if(!LOCPLINT->makingTurn)
  310. return;
  311. if(settings["adventure"]["heroReminder"].Bool())
  312. {
  313. for(auto hero : LOCPLINT->wanderingHeroes)
  314. {
  315. if(!isHeroSleeping(hero) && hero->movement > 0)
  316. {
  317. // Only show hero reminder if conditions met:
  318. // - There still movement points
  319. // - Hero don't have a path or there not points for first step on path
  320. LOCPLINT->paths.verifyPath(hero);
  321. if(!LOCPLINT->paths.hasPath(hero))
  322. {
  323. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdvMapInt::endingTurn, this), nullptr );
  324. return;
  325. }
  326. auto path = LOCPLINT->paths.getPath(hero);
  327. if (path.nodes.size() < 2 || path.nodes[path.nodes.size() - 2].turns)
  328. {
  329. LOCPLINT->showYesNoDialog( CGI->generaltexth->allTexts[55], std::bind(&CAdvMapInt::endingTurn, this), nullptr );
  330. return;
  331. }
  332. }
  333. }
  334. }
  335. endingTurn();
  336. }
  337. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  338. {
  339. sleepWake->block(!h);
  340. if (!h)
  341. return;
  342. bool state = isHeroSleeping(h);
  343. sleepWake->setIndex(state ? 1 : 0, true);
  344. sleepWake->assignedKeys.clear();
  345. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  346. }
  347. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  348. {
  349. if(!h)
  350. {
  351. moveHero->block(true);
  352. return;
  353. }
  354. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  355. if(boost::logic::indeterminate(hasPath))
  356. hasPath = LOCPLINT->paths.hasPath(h);
  357. moveHero->block(!(bool)hasPath || (h->movement == 0));
  358. }
  359. void CAdvMapInt::updateSpellbook(const CGHeroInstance *h)
  360. {
  361. spellbook->block(!h);
  362. }
  363. int CAdvMapInt::getNextHeroIndex(int startIndex)
  364. {
  365. if (LOCPLINT->wanderingHeroes.size() == 0)
  366. return -1;
  367. if (startIndex < 0)
  368. startIndex = 0;
  369. int i = startIndex;
  370. do
  371. {
  372. i++;
  373. if (i >= LOCPLINT->wanderingHeroes.size())
  374. i = 0;
  375. }
  376. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  377. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  378. return i;
  379. else
  380. return -1;
  381. }
  382. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  383. {
  384. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  385. int next = getNextHeroIndex(start);
  386. if (next < 0)
  387. {
  388. nextHero->block(true);
  389. return;
  390. }
  391. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  392. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  393. nextHero->block(noActiveHeroes);
  394. }
  395. void CAdvMapInt::activate()
  396. {
  397. CIntObject::activate();
  398. if (!(active & KEYBOARD))
  399. CIntObject::activate(KEYBOARD);
  400. screenBuf = screen;
  401. GH.statusbar = statusbar;
  402. if(LOCPLINT)
  403. {
  404. LOCPLINT->cingconsole->activate();
  405. LOCPLINT->cingconsole->pos = this->pos;
  406. }
  407. if(!duringAITurn)
  408. {
  409. activeMapPanel->activate();
  410. if (mode == EAdvMapMode::NORMAL)
  411. {
  412. heroList->activate();
  413. townList->activate();
  414. infoBar->activate();
  415. }
  416. minimap->activate();
  417. terrain->activate();
  418. statusbar->activate();
  419. GH.fakeMouseMove(); //to restore the cursor
  420. }
  421. }
  422. void CAdvMapInt::deactivate()
  423. {
  424. CIntObject::deactivate();
  425. if(!duringAITurn)
  426. {
  427. scrollingDir = 0;
  428. CCS->curh->set(Cursor::Map::POINTER);
  429. activeMapPanel->deactivate();
  430. if (mode == EAdvMapMode::NORMAL)
  431. {
  432. heroList->deactivate();
  433. townList->deactivate();
  434. infoBar->deactivate();
  435. }
  436. minimap->deactivate();
  437. terrain->deactivate();
  438. statusbar->deactivate();
  439. }
  440. }
  441. void CAdvMapInt::showAll(SDL_Surface * to)
  442. {
  443. bg->draw(to, 0, 0);
  444. if(state != INGAME)
  445. return;
  446. switch (mode)
  447. {
  448. case EAdvMapMode::NORMAL:
  449. heroList->showAll(to);
  450. townList->showAll(to);
  451. infoBar->showAll(to);
  452. break;
  453. case EAdvMapMode::WORLD_VIEW:
  454. break;
  455. }
  456. activeMapPanel->showAll(to);
  457. minimap->showAll(to);
  458. show(to);
  459. resdatabar->showAll(to);
  460. statusbar->show(to);
  461. LOCPLINT->cingconsole->show(to);
  462. }
  463. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  464. {
  465. if (!hero)
  466. return false;
  467. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  468. }
  469. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  470. {
  471. if (sleep)
  472. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  473. else
  474. LOCPLINT->sleepingHeroes -= hero;
  475. updateNextHero(nullptr);
  476. }
  477. void CAdvMapInt::show(SDL_Surface * to)
  478. {
  479. if(state != INGAME)
  480. return;
  481. if(swipeEnabled)
  482. {
  483. handleSwipeUpdate();
  484. }
  485. #if defined(VCMI_MOBILE) // on mobile, map-moving mode is exclusive (TODO technically it might work with both enabled; to be checked)
  486. else
  487. #endif
  488. {
  489. handleMapScrollingUpdate();
  490. }
  491. for(int i = 0; i < 4; i++)
  492. {
  493. if(settings["session"]["spectate"].Bool())
  494. gems[i]->setFrame(PlayerColor(1).getNum());
  495. else
  496. gems[i]->setFrame(LOCPLINT->playerID.getNum());
  497. }
  498. terrain->show(to);
  499. for(int i = 0; i < 4; i++)
  500. gems[i]->showAll(to);
  501. LOCPLINT->cingconsole->show(to);
  502. infoBar->show(to);
  503. statusbar->showAll(to);
  504. }
  505. void CAdvMapInt::handleMapScrollingUpdate()
  506. {
  507. uint32_t timePassed = GH.mainFPSmng->getElapsedMilliseconds();
  508. double scrollSpeedPixels = settings["adventure"]["scrollSpeedPixels"].Float();
  509. int32_t scrollDistance = static_cast<int32_t>(scrollSpeedPixels * timePassed / 1000);
  510. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  511. if(scrollingDir & LEFT)
  512. terrain->moveViewBy(Point(-scrollDistance, 0));
  513. if(scrollingDir & RIGHT)
  514. terrain->moveViewBy(Point(+scrollDistance, 0));
  515. if(scrollingDir & UP)
  516. terrain->moveViewBy(Point(0, -scrollDistance));
  517. if(scrollingDir & DOWN)
  518. terrain->moveViewBy(Point(0, +scrollDistance));
  519. if(scrollingDir)
  520. {
  521. setScrollingCursor(scrollingDir);
  522. scrollingState = true;
  523. }
  524. else if(scrollingState)
  525. {
  526. CCS->curh->set(Cursor::Map::POINTER);
  527. scrollingState = false;
  528. }
  529. }
  530. void CAdvMapInt::handleSwipeUpdate()
  531. {
  532. if(swipeMovementRequested)
  533. {
  534. terrain->setViewCenter(swipeTargetPosition, terrain->getLevel());
  535. CCS->curh->set(Cursor::Map::POINTER);
  536. minimap->redraw();
  537. swipeMovementRequested = false;
  538. }
  539. }
  540. void CAdvMapInt::selectionChanged()
  541. {
  542. const CGTownInstance *to = LOCPLINT->towns[townList->getSelectedIndex()];
  543. if (selection != to)
  544. select(to);
  545. }
  546. void CAdvMapInt::centerOn(int3 on, bool fade)
  547. {
  548. bool switchedLevels = on.z != terrain->getLevel();
  549. if (fade)
  550. {
  551. terrain->fadeFromCurrentView();
  552. }
  553. terrain->setViewCenter(on);
  554. underground->setIndex(on.z,true); //change underground switch button image
  555. underground->redraw();
  556. worldViewUnderground->setIndex(on.z, true);
  557. worldViewUnderground->redraw();
  558. if (switchedLevels)
  559. minimap->setLevel(terrain->getLevel());
  560. minimap->redraw();
  561. }
  562. void CAdvMapInt::centerOn(const CGObjectInstance * obj, bool fade)
  563. {
  564. centerOn(obj->getSightCenter(), fade);
  565. }
  566. void CAdvMapInt::keyReleased(const SDL_Keycode &key)
  567. {
  568. if (mode == EAdvMapMode::WORLD_VIEW)
  569. return;
  570. switch (key)
  571. {
  572. case SDLK_s:
  573. if(isActive())
  574. GH.pushIntT<CSavingScreen>();
  575. return;
  576. default:
  577. {
  578. auto direction = keyToMoveDirection(key);
  579. if (!direction)
  580. return;
  581. ui8 Dir = (direction->x<0 ? LEFT : 0) |
  582. (direction->x>0 ? RIGHT : 0) |
  583. (direction->y<0 ? UP : 0) |
  584. (direction->y>0 ? DOWN : 0) ;
  585. scrollingDir &= ~Dir;
  586. }
  587. }
  588. }
  589. void CAdvMapInt::keyPressed(const SDL_Keycode & key)
  590. {
  591. if (mode == EAdvMapMode::WORLD_VIEW)
  592. return;
  593. const CGHeroInstance *h = curHero(); //selected hero
  594. const CGTownInstance *t = curTown(); //selected town
  595. switch(key)
  596. {
  597. case SDLK_g:
  598. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  599. return;
  600. {
  601. //find first town with tavern
  602. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  603. {
  604. return town->hasBuilt(BuildingID::TAVERN);
  605. });
  606. if(itr != LOCPLINT->towns.end())
  607. LOCPLINT->showThievesGuildWindow(*itr);
  608. else
  609. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  610. }
  611. return;
  612. case SDLK_i:
  613. if(isActive())
  614. CAdventureOptions::showScenarioInfo();
  615. return;
  616. case SDLK_l:
  617. if(isActive())
  618. LOCPLINT->proposeLoadingGame();
  619. return;
  620. case SDLK_d:
  621. {
  622. if(h && isActive() && LOCPLINT->makingTurn)
  623. LOCPLINT->tryDiggging(h);
  624. return;
  625. }
  626. case SDLK_p:
  627. if(isActive())
  628. LOCPLINT->showPuzzleMap();
  629. return;
  630. case SDLK_v:
  631. if(isActive())
  632. LOCPLINT->viewWorldMap();
  633. return;
  634. case SDLK_r:
  635. if(isActive() && GH.isKeyboardCtrlDown())
  636. {
  637. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  638. [](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
  639. }
  640. return;
  641. case SDLK_SPACE: //space - try to revisit current object with selected hero
  642. {
  643. if(!isActive())
  644. return;
  645. if(h)
  646. {
  647. auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
  648. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  649. //this thread leaves scope and tries to lock pim while holding gs,
  650. //network thread tries to lock gs (appluy cl) while holding pim
  651. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  652. LOCPLINT->cb->moveHero(h,h->pos);
  653. }
  654. }
  655. return;
  656. case SDLK_RETURN:
  657. {
  658. if(!isActive() || !selection)
  659. return;
  660. if(h)
  661. LOCPLINT->openHeroWindow(h);
  662. else if(t)
  663. LOCPLINT->openTownWindow(t);
  664. return;
  665. }
  666. case SDLK_ESCAPE:
  667. {
  668. if(isActive() || GH.topInt().get() != this || !spellBeingCasted)
  669. return;
  670. leaveCastingMode();
  671. return;
  672. }
  673. case SDLK_t:
  674. {
  675. //act on key down if marketplace windows is not already opened
  676. if(GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  677. return;
  678. if(GH.isKeyboardCtrlDown()) //CTRL + T => open marketplace
  679. {
  680. //check if we have any marketplace
  681. const CGTownInstance *townWithMarket = nullptr;
  682. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  683. {
  684. if(t->hasBuilt(BuildingID::MARKETPLACE))
  685. {
  686. townWithMarket = t;
  687. break;
  688. }
  689. }
  690. if(townWithMarket) //if any town has marketplace, open window
  691. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  692. else //if not - complain
  693. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  694. }
  695. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  696. {
  697. townList->selectNext();
  698. }
  699. return;
  700. }
  701. default:
  702. {
  703. auto direction = keyToMoveDirection(key);
  704. if (!direction)
  705. return;
  706. ui8 Dir = (direction->x<0 ? LEFT : 0) |
  707. (direction->x>0 ? RIGHT : 0) |
  708. (direction->y<0 ? UP : 0) |
  709. (direction->y>0 ? DOWN : 0) ;
  710. //ctrl makes arrow move screen, not hero
  711. if(GH.isKeyboardCtrlDown())
  712. {
  713. scrollingDir |= Dir;
  714. return;
  715. }
  716. if(!h || !isActive())
  717. return;
  718. if (CGI->mh->hasOngoingAnimations())
  719. return;
  720. if(*direction == Point(0,0))
  721. {
  722. centerOn(h);
  723. return;
  724. }
  725. int3 dst = h->visitablePos() + int3(direction->x, direction->y, 0);
  726. if (!CGI->mh->isInMap((dst)))
  727. return;
  728. if ( !LOCPLINT->paths.setPath(h, dst))
  729. return;
  730. const CGPath & path = LOCPLINT->paths.getPath(h);
  731. if (path.nodes.size() > 2)
  732. updateMoveHero(h);
  733. else
  734. if(!path.nodes[0].turns)
  735. LOCPLINT->moveHero(h, path);
  736. }
  737. return;
  738. }
  739. }
  740. boost::optional<Point> CAdvMapInt::keyToMoveDirection(const SDL_Keycode & key)
  741. {
  742. switch (key) {
  743. case SDLK_DOWN: return Point( 0, +1);
  744. case SDLK_LEFT: return Point(-1, 0);
  745. case SDLK_RIGHT: return Point(+1, 0);
  746. case SDLK_UP: return Point( 0, -1);
  747. case SDLK_KP_1: return Point(-1, +1);
  748. case SDLK_KP_2: return Point( 0, +1);
  749. case SDLK_KP_3: return Point(+1, +1);
  750. case SDLK_KP_4: return Point(-1, 0);
  751. case SDLK_KP_5: return Point( 0, 0);
  752. case SDLK_KP_6: return Point(+1, 0);
  753. case SDLK_KP_7: return Point(-1, -1);
  754. case SDLK_KP_8: return Point( 0, -1);
  755. case SDLK_KP_9: return Point(+1, -1);
  756. }
  757. return boost::none;
  758. }
  759. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView)
  760. {
  761. assert(sel);
  762. LOCPLINT->setSelection(sel);
  763. selection = sel;
  764. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  765. {
  766. auto pos = sel->visitablePos();
  767. auto tile = LOCPLINT->cb->getTile(pos);
  768. if(tile)
  769. CCS->musich->playMusicFromSet("terrain", tile->terType->getJsonKey(), true, false);
  770. }
  771. if(centerView)
  772. centerOn(sel);
  773. if(sel->ID==Obj::TOWN)
  774. {
  775. auto town = dynamic_cast<const CGTownInstance*>(sel);
  776. infoBar->showTownSelection(town);
  777. townList->select(town);
  778. heroList->select(nullptr);
  779. updateSleepWake(nullptr);
  780. updateMoveHero(nullptr);
  781. updateSpellbook(nullptr);
  782. }
  783. else //hero selected
  784. {
  785. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  786. infoBar->showHeroSelection(hero);
  787. heroList->select(hero);
  788. townList->select(nullptr);
  789. updateSleepWake(hero);
  790. updateMoveHero(hero);
  791. updateSpellbook(hero);
  792. }
  793. townList->redraw();
  794. heroList->redraw();
  795. }
  796. void CAdvMapInt::mouseMoved( const Point & cursorPosition )
  797. {
  798. #if defined(VCMI_MOBILE)
  799. if(swipeEnabled)
  800. return;
  801. #endif
  802. // adventure map scrolling with mouse
  803. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  804. // don't scroll if there is no window in focus - these events don't seem to correspond to the actual mouse movement
  805. if(!GH.isKeyboardCtrlDown() && isActive() && mode == EAdvMapMode::NORMAL)
  806. {
  807. if(cursorPosition.x<15)
  808. {
  809. scrollingDir |= LEFT;
  810. }
  811. else
  812. {
  813. scrollingDir &= ~LEFT;
  814. }
  815. if(cursorPosition.x > GH.screenDimensions().x - 15)
  816. {
  817. scrollingDir |= RIGHT;
  818. }
  819. else
  820. {
  821. scrollingDir &= ~RIGHT;
  822. }
  823. if(cursorPosition.y<15)
  824. {
  825. scrollingDir |= UP;
  826. }
  827. else
  828. {
  829. scrollingDir &= ~UP;
  830. }
  831. if(cursorPosition.y > GH.screenDimensions().y - 15)
  832. {
  833. scrollingDir |= DOWN;
  834. }
  835. else
  836. {
  837. scrollingDir &= ~DOWN;
  838. }
  839. }
  840. }
  841. bool CAdvMapInt::isActive()
  842. {
  843. return active & ~CIntObject::KEYBOARD;
  844. }
  845. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  846. {
  847. state = WAITING;
  848. }
  849. void CAdvMapInt::setPlayer(PlayerColor Player)
  850. {
  851. if (Player == player)
  852. return;
  853. player = Player;
  854. bg->playerColored(player);
  855. panelMain->setPlayerColor(player);
  856. panelWorldView->setPlayerColor(player);
  857. panelWorldView->recolorIcons(player, player.getNum() * 19);
  858. resdatabar->colorize(player);
  859. }
  860. void CAdvMapInt::startTurn()
  861. {
  862. state = INGAME;
  863. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  864. || settings["session"]["spectate"].Bool())
  865. {
  866. adjustActiveness(false);
  867. minimap->setAIRadar(false);
  868. }
  869. }
  870. void CAdvMapInt::initializeNewTurn()
  871. {
  872. heroList->update();
  873. townList->update();
  874. const CGHeroInstance * heroToSelect = nullptr;
  875. // find first non-sleeping hero
  876. for (auto hero : LOCPLINT->wanderingHeroes)
  877. {
  878. if (boost::range::find(LOCPLINT->sleepingHeroes, hero) == LOCPLINT->sleepingHeroes.end())
  879. {
  880. heroToSelect = hero;
  881. break;
  882. }
  883. }
  884. bool centerView = !settings["session"]["autoSkip"].Bool();
  885. //select first hero if available.
  886. if (heroToSelect != nullptr)
  887. {
  888. select(heroToSelect, centerView);
  889. }
  890. else if (LOCPLINT->towns.size())
  891. select(LOCPLINT->towns.front(), centerView);
  892. else
  893. select(LOCPLINT->wanderingHeroes.front());
  894. //show new day animation and sound on infobar
  895. infoBar->showDate();
  896. updateNextHero(nullptr);
  897. showAll(screen);
  898. if(settings["session"]["autoSkip"].Bool() && !GH.isKeyboardShiftDown())
  899. {
  900. if(CInfoWindow *iw = dynamic_cast<CInfoWindow *>(GH.topInt().get()))
  901. iw->close();
  902. endingTurn();
  903. }
  904. }
  905. void CAdvMapInt::endingTurn()
  906. {
  907. if(settings["session"]["spectate"].Bool())
  908. return;
  909. LOCPLINT->makingTurn = false;
  910. LOCPLINT->cb->endTurn();
  911. CCS->soundh->ambientStopAllChannels();
  912. }
  913. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  914. {
  915. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  916. if (bobjs.empty())
  917. return nullptr;
  918. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  919. /*
  920. if (bobjs.back()->ID == Obj::HERO)
  921. return bobjs.back();
  922. else
  923. return bobjs.front();*/
  924. }
  925. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  926. {
  927. if(mode != EAdvMapMode::NORMAL)
  928. return;
  929. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  930. return;
  931. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  932. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  933. int3 selPos = selection->getSightCenter();
  934. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  935. {
  936. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  937. switch(spellBeingCasted->id)
  938. {
  939. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  940. if(topBlocking && topBlocking->ID == Obj::BOAT)
  941. leaveCastingMode(true, mapPos);
  942. break;
  943. case SpellID::DIMENSION_DOOR:
  944. if(!tile || tile->isClear(heroTile))
  945. leaveCastingMode(true, mapPos);
  946. break;
  947. }
  948. return;
  949. }
  950. //check if we can select this object
  951. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  952. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  953. bool isHero = false;
  954. if(selection->ID != Obj::HERO) //hero is not selected (presumably town)
  955. {
  956. if(selection == topBlocking) //selected town clicked
  957. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  958. else if(canSelect)
  959. select(static_cast<const CArmedInstance*>(topBlocking), false);
  960. }
  961. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  962. {
  963. isHero = true;
  964. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  965. if(currentHero == topBlocking) //clicked selected hero
  966. {
  967. LOCPLINT->openHeroWindow(currentHero);
  968. return;
  969. }
  970. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  971. {
  972. select(static_cast<const CArmedInstance*>(topBlocking), false);
  973. return;
  974. }
  975. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  976. {
  977. if(LOCPLINT->paths.hasPath(currentHero) &&
  978. LOCPLINT->paths.getPath(currentHero).endPos() == mapPos)//we'll be moving
  979. {
  980. if(!CGI->mh->hasOngoingAnimations())
  981. LOCPLINT->moveHero(currentHero, LOCPLINT->paths.getPath(currentHero));
  982. return;
  983. }
  984. else //remove old path and find a new one if we clicked on accessible tile
  985. {
  986. LOCPLINT->paths.setPath(currentHero, mapPos);
  987. updateMoveHero(currentHero);
  988. }
  989. }
  990. } //end of hero is selected "case"
  991. else
  992. {
  993. throw std::runtime_error("Nothing is selected...");
  994. }
  995. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  996. if(isHero && shipyard != nullptr)
  997. {
  998. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  999. }
  1000. }
  1001. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1002. {
  1003. if(mode != EAdvMapMode::NORMAL //disable in world view
  1004. || !selection) //may occur just at the start of game (fake move before full intiialization)
  1005. return;
  1006. if(!LOCPLINT->cb->isVisible(mapPos))
  1007. {
  1008. CCS->curh->set(Cursor::Map::POINTER);
  1009. statusbar->clear();
  1010. return;
  1011. }
  1012. auto objRelations = PlayerRelations::ALLIES;
  1013. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1014. if(objAtTile)
  1015. {
  1016. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  1017. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1018. boost::replace_all(text,"\n"," ");
  1019. statusbar->write(text);
  1020. }
  1021. else
  1022. {
  1023. std::string hlp;
  1024. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1025. statusbar->write(hlp);
  1026. }
  1027. if(spellBeingCasted)
  1028. {
  1029. switch(spellBeingCasted->id)
  1030. {
  1031. case SpellID::SCUTTLE_BOAT:
  1032. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1033. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  1034. else
  1035. CCS->curh->set(Cursor::Map::POINTER);
  1036. return;
  1037. case SpellID::DIMENSION_DOOR:
  1038. {
  1039. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  1040. int3 hpos = selection->getSightCenter();
  1041. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1042. CCS->curh->set(Cursor::Map::TELEPORT);
  1043. else
  1044. CCS->curh->set(Cursor::Map::POINTER);
  1045. return;
  1046. }
  1047. }
  1048. }
  1049. if(selection->ID == Obj::TOWN)
  1050. {
  1051. if(objAtTile)
  1052. {
  1053. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  1054. CCS->curh->set(Cursor::Map::TOWN);
  1055. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1056. CCS->curh->set(Cursor::Map::HERO);
  1057. else
  1058. CCS->curh->set(Cursor::Map::POINTER);
  1059. }
  1060. else
  1061. CCS->curh->set(Cursor::Map::POINTER);
  1062. }
  1063. else if(const CGHeroInstance * hero = curHero())
  1064. {
  1065. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  1066. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  1067. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  1068. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  1069. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  1070. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  1071. 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, };
  1072. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  1073. assert(pathNode);
  1074. if((GH.isKeyboardAltDown() || settings["gameTweaks"]["forceMovementInfo"].Bool()) && pathNode->reachable()) //overwrite status bar text with movement info
  1075. {
  1076. showMoveDetailsInStatusbar(*hero, *pathNode);
  1077. }
  1078. int turns = pathNode->turns;
  1079. vstd::amin(turns, 3);
  1080. switch(pathNode->action)
  1081. {
  1082. case CGPathNode::NORMAL:
  1083. case CGPathNode::TELEPORT_NORMAL:
  1084. if(pathNode->layer == EPathfindingLayer::LAND)
  1085. CCS->curh->set(cursorMove[turns]);
  1086. else
  1087. CCS->curh->set(cursorSailVisit[turns]);
  1088. break;
  1089. case CGPathNode::VISIT:
  1090. case CGPathNode::BLOCKING_VISIT:
  1091. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  1092. if(objAtTile && objAtTile->ID == Obj::HERO)
  1093. {
  1094. if(selection == objAtTile)
  1095. CCS->curh->set(Cursor::Map::HERO);
  1096. else
  1097. CCS->curh->set(cursorExchange[turns]);
  1098. }
  1099. else if(pathNode->layer == EPathfindingLayer::LAND)
  1100. CCS->curh->set(cursorVisit[turns]);
  1101. else
  1102. CCS->curh->set(cursorSailVisit[turns]);
  1103. break;
  1104. case CGPathNode::BATTLE:
  1105. case CGPathNode::TELEPORT_BATTLE:
  1106. CCS->curh->set(cursorAttack[turns]);
  1107. break;
  1108. case CGPathNode::EMBARK:
  1109. CCS->curh->set(cursorSail[turns]);
  1110. break;
  1111. case CGPathNode::DISEMBARK:
  1112. CCS->curh->set(cursorDisembark[turns]);
  1113. break;
  1114. default:
  1115. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1116. {
  1117. if(objAtTile->ID == Obj::TOWN)
  1118. CCS->curh->set(Cursor::Map::TOWN);
  1119. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1120. CCS->curh->set(Cursor::Map::HERO);
  1121. else
  1122. CCS->curh->set(Cursor::Map::POINTER);
  1123. }
  1124. else
  1125. CCS->curh->set(Cursor::Map::POINTER);
  1126. break;
  1127. }
  1128. }
  1129. if(ourInaccessibleShipyard(objAtTile))
  1130. {
  1131. CCS->curh->set(Cursor::Map::T1_SAIL);
  1132. }
  1133. }
  1134. void CAdvMapInt::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  1135. {
  1136. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  1137. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  1138. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  1139. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  1140. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  1141. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  1142. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  1143. statusbar->write(result);
  1144. }
  1145. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1146. {
  1147. if(mode != EAdvMapMode::NORMAL)
  1148. return;
  1149. if(spellBeingCasted)
  1150. {
  1151. leaveCastingMode();
  1152. return;
  1153. }
  1154. if(!LOCPLINT->cb->isVisible(mapPos))
  1155. {
  1156. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1157. return;
  1158. }
  1159. const CGObjectInstance * obj = getActiveObject(mapPos);
  1160. if(!obj)
  1161. {
  1162. // Bare or undiscovered terrain
  1163. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1164. if (tile)
  1165. {
  1166. std::string hlp;
  1167. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1168. CRClickPopup::createAndPush(hlp);
  1169. }
  1170. return;
  1171. }
  1172. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  1173. }
  1174. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1175. {
  1176. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1177. spellBeingCasted = sp;
  1178. deactivate();
  1179. terrain->activate();
  1180. GH.fakeMouseMove();
  1181. }
  1182. void CAdvMapInt::leaveCastingMode(bool cast, int3 dest)
  1183. {
  1184. assert(spellBeingCasted);
  1185. SpellID id = spellBeingCasted->id;
  1186. spellBeingCasted = nullptr;
  1187. terrain->deactivate();
  1188. activate();
  1189. if(cast)
  1190. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1191. else
  1192. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1193. }
  1194. const CGHeroInstance * CAdvMapInt::curHero() const
  1195. {
  1196. if(selection && selection->ID == Obj::HERO)
  1197. return dynamic_cast<const CGHeroInstance *>(selection);
  1198. else
  1199. return nullptr;
  1200. }
  1201. const CGTownInstance * CAdvMapInt::curTown() const
  1202. {
  1203. if(selection && selection->ID == Obj::TOWN)
  1204. return dynamic_cast<const CGTownInstance *>(selection);
  1205. else
  1206. return nullptr;
  1207. }
  1208. const CArmedInstance * CAdvMapInt::curArmy() const
  1209. {
  1210. if (selection)
  1211. return dynamic_cast<const CArmedInstance *>(selection);
  1212. else
  1213. return nullptr;
  1214. }
  1215. Rect CAdvMapInt::terrainAreaPixels() const
  1216. {
  1217. return terrain->pos;
  1218. }
  1219. Rect CAdvMapInt::terrainAreaTiles() const
  1220. {
  1221. return terrain->visibleTilesArea();
  1222. }
  1223. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1224. {
  1225. const IShipyard *ret = IShipyard::castFrom(obj);
  1226. if(!ret ||
  1227. obj->tempOwner != player ||
  1228. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  1229. return nullptr;
  1230. return ret;
  1231. }
  1232. void CAdvMapInt::aiTurnStarted()
  1233. {
  1234. if(settings["session"]["spectate"].Bool())
  1235. return;
  1236. adjustActiveness(true);
  1237. CCS->musich->playMusicFromSet("enemy-turn", true, false);
  1238. adventureInt->minimap->setAIRadar(true);
  1239. adventureInt->infoBar->startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1240. adventureInt->infoBar->showAll(screen);//force refresh on inactive object
  1241. }
  1242. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1243. {
  1244. bool wasActive = isActive();
  1245. if(wasActive)
  1246. deactivate();
  1247. adventureInt->duringAITurn = aiTurnStart;
  1248. if(wasActive)
  1249. activate();
  1250. }
  1251. void CAdvMapInt::quickCombatLock()
  1252. {
  1253. if(!duringAITurn)
  1254. deactivate();
  1255. }
  1256. void CAdvMapInt::quickCombatUnlock()
  1257. {
  1258. if(!duringAITurn)
  1259. activate();
  1260. }
  1261. void CAdvMapInt::changeMode(EAdvMapMode newMode)
  1262. {
  1263. changeMode(newMode, 11);
  1264. }
  1265. void CAdvMapInt::changeMode(EAdvMapMode newMode, int tileSize)
  1266. {
  1267. if (mode != newMode)
  1268. {
  1269. mode = newMode;
  1270. switch (mode)
  1271. {
  1272. case EAdvMapMode::NORMAL:
  1273. panelMain->activate();
  1274. panelWorldView->deactivate();
  1275. activeMapPanel = panelMain;
  1276. townList->activate();
  1277. heroList->activate();
  1278. infoBar->activate();
  1279. worldViewOptions.clear();
  1280. terrain->setTileSize(32);
  1281. break;
  1282. case EAdvMapMode::WORLD_VIEW:
  1283. panelMain->deactivate();
  1284. panelWorldView->activate();
  1285. activeMapPanel = panelWorldView;
  1286. townList->deactivate();
  1287. heroList->deactivate();
  1288. infoBar->showSelection(); // to prevent new day animation interfering world view mode
  1289. infoBar->deactivate();
  1290. terrain->setTileSize(tileSize);
  1291. break;
  1292. }
  1293. redraw();
  1294. }
  1295. }
  1296. CAdvMapInt::WorldViewOptions::WorldViewOptions()
  1297. {
  1298. clear();
  1299. }
  1300. void CAdvMapInt::WorldViewOptions::clear()
  1301. {
  1302. showAllTerrain = false;
  1303. iconPositions.clear();
  1304. }