CAdvMapInt.cpp 43 KB

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