CAdvMapInt.cpp 42 KB

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