CAdvMapInt.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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); // 7 pixels per tile
  229. }
  230. void CAdvMapInt::fworldViewScale2x()
  231. {
  232. changeMode(EAdvMapMode::WORLD_VIEW, 0.36f); // 11 pixels per tile
  233. }
  234. void CAdvMapInt::fworldViewScale4x()
  235. {
  236. changeMode(EAdvMapMode::WORLD_VIEW, 0.5f); // 16 pixels per tile
  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. int3 CAdvMapInt::verifyPos(int3 ver)
  806. {
  807. if (ver.x<0)
  808. ver.x=0;
  809. if (ver.y<0)
  810. ver.y=0;
  811. if (ver.z<0)
  812. ver.z=0;
  813. if (ver.x>=CGI->mh->sizes.x)
  814. ver.x=CGI->mh->sizes.x-1;
  815. if (ver.y>=CGI->mh->sizes.y)
  816. ver.y=CGI->mh->sizes.y-1;
  817. if (ver.z>=CGI->mh->sizes.z)
  818. ver.z=CGI->mh->sizes.z-1;
  819. return ver;
  820. }
  821. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView)
  822. {
  823. assert(sel);
  824. LOCPLINT->setSelection(sel);
  825. selection = sel;
  826. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  827. {
  828. auto pos = sel->visitablePos();
  829. auto tile = LOCPLINT->cb->getTile(pos);
  830. if(tile)
  831. CCS->musich->playMusicFromSet("terrain", tile->terType->getJsonKey(), true, false);
  832. }
  833. if(centerView)
  834. centerOn(sel);
  835. terrain.currentPath = nullptr;
  836. if(sel->ID==Obj::TOWN)
  837. {
  838. auto town = dynamic_cast<const CGTownInstance*>(sel);
  839. infoBar.showTownSelection(town);
  840. townList.select(town);
  841. heroList.select(nullptr);
  842. updateSleepWake(nullptr);
  843. updateMoveHero(nullptr);
  844. updateSpellbook(nullptr);
  845. }
  846. else //hero selected
  847. {
  848. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  849. infoBar.showHeroSelection(hero);
  850. heroList.select(hero);
  851. townList.select(nullptr);
  852. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  853. updateSleepWake(hero);
  854. updateMoveHero(hero);
  855. updateSpellbook(hero);
  856. }
  857. townList.redraw();
  858. heroList.redraw();
  859. }
  860. void CAdvMapInt::mouseMoved( const Point & cursorPosition )
  861. {
  862. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  863. if(swipeEnabled)
  864. return;
  865. #endif
  866. // adventure map scrolling with mouse
  867. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  868. // don't scroll if there is no window in focus - these events don't seem to correspond to the actual mouse movement
  869. if(!GH.isKeyboardCtrlDown() && isActive() && mode == EAdvMapMode::NORMAL)
  870. {
  871. if(cursorPosition.x<15)
  872. {
  873. scrollingDir |= LEFT;
  874. }
  875. else
  876. {
  877. scrollingDir &= ~LEFT;
  878. }
  879. if(cursorPosition.x > GH.screenDimensions().x - 15)
  880. {
  881. scrollingDir |= RIGHT;
  882. }
  883. else
  884. {
  885. scrollingDir &= ~RIGHT;
  886. }
  887. if(cursorPosition.y<15)
  888. {
  889. scrollingDir |= UP;
  890. }
  891. else
  892. {
  893. scrollingDir &= ~UP;
  894. }
  895. if(cursorPosition.y > GH.screenDimensions().y - 15)
  896. {
  897. scrollingDir |= DOWN;
  898. }
  899. else
  900. {
  901. scrollingDir &= ~DOWN;
  902. }
  903. }
  904. }
  905. bool CAdvMapInt::isActive()
  906. {
  907. return active & ~CIntObject::KEYBOARD;
  908. }
  909. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  910. {
  911. state = WAITING;
  912. }
  913. void CAdvMapInt::setPlayer(PlayerColor Player)
  914. {
  915. player = Player;
  916. bg->playerColored(player);
  917. panelMain->setPlayerColor(player);
  918. panelWorldView->setPlayerColor(player);
  919. panelWorldView->recolorIcons(player, player.getNum() * 19);
  920. resdatabar.background->colorize(player);
  921. }
  922. void CAdvMapInt::startTurn()
  923. {
  924. state = INGAME;
  925. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  926. || settings["session"]["spectate"].Bool())
  927. {
  928. adjustActiveness(false);
  929. minimap.setAIRadar(false);
  930. }
  931. }
  932. void CAdvMapInt::endingTurn()
  933. {
  934. if(settings["session"]["spectate"].Bool())
  935. return;
  936. LOCPLINT->makingTurn = false;
  937. LOCPLINT->cb->endTurn();
  938. CCS->soundh->ambientStopAllChannels();
  939. }
  940. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  941. {
  942. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  943. if (bobjs.empty())
  944. return nullptr;
  945. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  946. /*
  947. if (bobjs.back()->ID == Obj::HERO)
  948. return bobjs.back();
  949. else
  950. return bobjs.front();*/
  951. }
  952. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  953. {
  954. if(mode != EAdvMapMode::NORMAL)
  955. return;
  956. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  957. return;
  958. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  959. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  960. int3 selPos = selection->getSightCenter();
  961. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  962. {
  963. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  964. switch(spellBeingCasted->id)
  965. {
  966. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  967. if(topBlocking && topBlocking->ID == Obj::BOAT)
  968. leaveCastingMode(true, mapPos);
  969. break;
  970. case SpellID::DIMENSION_DOOR:
  971. if(!tile || tile->isClear(heroTile))
  972. leaveCastingMode(true, mapPos);
  973. break;
  974. }
  975. return;
  976. }
  977. //check if we can select this object
  978. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  979. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  980. bool isHero = false;
  981. if(selection->ID != Obj::HERO) //hero is not selected (presumably town)
  982. {
  983. assert(!terrain.currentPath); //path can be active only when hero is selected
  984. if(selection == topBlocking) //selected town clicked
  985. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  986. else if(canSelect)
  987. select(static_cast<const CArmedInstance*>(topBlocking), false);
  988. }
  989. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  990. {
  991. isHero = true;
  992. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  993. if(currentHero == topBlocking) //clicked selected hero
  994. {
  995. LOCPLINT->openHeroWindow(currentHero);
  996. return;
  997. }
  998. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  999. {
  1000. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1001. return;
  1002. }
  1003. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1004. {
  1005. if(terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1006. {
  1007. if(CGI->mh->canStartHeroMovement())
  1008. LOCPLINT->moveHero(currentHero, *terrain.currentPath);
  1009. return;
  1010. }
  1011. else //remove old path and find a new one if we clicked on accessible tile
  1012. {
  1013. CGPath &path = LOCPLINT->paths[currentHero];
  1014. CGPath newpath;
  1015. bool gotPath = LOCPLINT->cb->getPathsInfo(currentHero)->getPath(newpath, mapPos); //try getting path, erase if failed
  1016. if(gotPath && newpath.nodes.size())
  1017. path = newpath;
  1018. if(path.nodes.size())
  1019. terrain.currentPath = &path;
  1020. else
  1021. LOCPLINT->eraseCurrentPathOf(currentHero);
  1022. updateMoveHero(currentHero);
  1023. }
  1024. }
  1025. } //end of hero is selected "case"
  1026. else
  1027. {
  1028. throw std::runtime_error("Nothing is selected...");
  1029. }
  1030. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  1031. if(isHero && shipyard != nullptr)
  1032. {
  1033. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1034. }
  1035. }
  1036. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1037. {
  1038. if(mode != EAdvMapMode::NORMAL //disable in world view
  1039. || !selection) //may occur just at the start of game (fake move before full intiialization)
  1040. return;
  1041. if(!LOCPLINT->cb->isVisible(mapPos))
  1042. {
  1043. CCS->curh->set(Cursor::Map::POINTER);
  1044. statusbar->clear();
  1045. return;
  1046. }
  1047. auto objRelations = PlayerRelations::ALLIES;
  1048. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1049. if(objAtTile)
  1050. {
  1051. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  1052. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1053. boost::replace_all(text,"\n"," ");
  1054. statusbar->write(text);
  1055. }
  1056. else
  1057. {
  1058. std::string hlp;
  1059. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1060. statusbar->write(hlp);
  1061. }
  1062. if(spellBeingCasted)
  1063. {
  1064. switch(spellBeingCasted->id)
  1065. {
  1066. case SpellID::SCUTTLE_BOAT:
  1067. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1068. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  1069. else
  1070. CCS->curh->set(Cursor::Map::POINTER);
  1071. return;
  1072. case SpellID::DIMENSION_DOOR:
  1073. {
  1074. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  1075. int3 hpos = selection->getSightCenter();
  1076. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1077. CCS->curh->set(Cursor::Map::TELEPORT);
  1078. else
  1079. CCS->curh->set(Cursor::Map::POINTER);
  1080. return;
  1081. }
  1082. }
  1083. }
  1084. if(selection->ID == Obj::TOWN)
  1085. {
  1086. if(objAtTile)
  1087. {
  1088. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  1089. CCS->curh->set(Cursor::Map::TOWN);
  1090. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1091. CCS->curh->set(Cursor::Map::HERO);
  1092. else
  1093. CCS->curh->set(Cursor::Map::POINTER);
  1094. }
  1095. else
  1096. CCS->curh->set(Cursor::Map::POINTER);
  1097. }
  1098. else if(const CGHeroInstance * hero = curHero())
  1099. {
  1100. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  1101. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  1102. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  1103. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  1104. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  1105. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  1106. 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, };
  1107. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  1108. assert(pathNode);
  1109. if(GH.isKeyboardAltDown() && pathNode->reachable()) //overwrite status bar text with movement info
  1110. {
  1111. showMoveDetailsInStatusbar(*hero, *pathNode);
  1112. }
  1113. int turns = pathNode->turns;
  1114. vstd::amin(turns, 3);
  1115. switch(pathNode->action)
  1116. {
  1117. case CGPathNode::NORMAL:
  1118. case CGPathNode::TELEPORT_NORMAL:
  1119. if(pathNode->layer == EPathfindingLayer::LAND)
  1120. CCS->curh->set(cursorMove[turns]);
  1121. else
  1122. CCS->curh->set(cursorSailVisit[turns]);
  1123. break;
  1124. case CGPathNode::VISIT:
  1125. case CGPathNode::BLOCKING_VISIT:
  1126. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  1127. if(objAtTile && objAtTile->ID == Obj::HERO)
  1128. {
  1129. if(selection == objAtTile)
  1130. CCS->curh->set(Cursor::Map::HERO);
  1131. else
  1132. CCS->curh->set(cursorExchange[turns]);
  1133. }
  1134. else if(pathNode->layer == EPathfindingLayer::LAND)
  1135. CCS->curh->set(cursorVisit[turns]);
  1136. else
  1137. CCS->curh->set(cursorSailVisit[turns]);
  1138. break;
  1139. case CGPathNode::BATTLE:
  1140. case CGPathNode::TELEPORT_BATTLE:
  1141. CCS->curh->set(cursorAttack[turns]);
  1142. break;
  1143. case CGPathNode::EMBARK:
  1144. CCS->curh->set(cursorSail[turns]);
  1145. break;
  1146. case CGPathNode::DISEMBARK:
  1147. CCS->curh->set(cursorDisembark[turns]);
  1148. break;
  1149. default:
  1150. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1151. {
  1152. if(objAtTile->ID == Obj::TOWN)
  1153. CCS->curh->set(Cursor::Map::TOWN);
  1154. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1155. CCS->curh->set(Cursor::Map::HERO);
  1156. else
  1157. CCS->curh->set(Cursor::Map::POINTER);
  1158. }
  1159. else
  1160. CCS->curh->set(Cursor::Map::POINTER);
  1161. break;
  1162. }
  1163. }
  1164. if(ourInaccessibleShipyard(objAtTile))
  1165. {
  1166. CCS->curh->set(Cursor::Map::T1_SAIL);
  1167. }
  1168. }
  1169. void CAdvMapInt::showMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  1170. {
  1171. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  1172. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  1173. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  1174. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  1175. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  1176. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  1177. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  1178. statusbar->write(result);
  1179. }
  1180. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1181. {
  1182. if(mode != EAdvMapMode::NORMAL)
  1183. return;
  1184. if(spellBeingCasted)
  1185. {
  1186. leaveCastingMode();
  1187. return;
  1188. }
  1189. if(!LOCPLINT->cb->isVisible(mapPos))
  1190. {
  1191. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1192. return;
  1193. }
  1194. const CGObjectInstance * obj = getActiveObject(mapPos);
  1195. if(!obj)
  1196. {
  1197. // Bare or undiscovered terrain
  1198. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1199. if (tile)
  1200. {
  1201. std::string hlp;
  1202. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1203. CRClickPopup::createAndPush(hlp);
  1204. }
  1205. return;
  1206. }
  1207. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  1208. }
  1209. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1210. {
  1211. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1212. spellBeingCasted = sp;
  1213. deactivate();
  1214. terrain.activate();
  1215. GH.fakeMouseMove();
  1216. }
  1217. void CAdvMapInt::leaveCastingMode(bool cast, int3 dest)
  1218. {
  1219. assert(spellBeingCasted);
  1220. SpellID id = spellBeingCasted->id;
  1221. spellBeingCasted = nullptr;
  1222. terrain.deactivate();
  1223. activate();
  1224. if(cast)
  1225. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1226. else
  1227. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1228. }
  1229. const CGHeroInstance * CAdvMapInt::curHero() const
  1230. {
  1231. if(selection && selection->ID == Obj::HERO)
  1232. return static_cast<const CGHeroInstance *>(selection);
  1233. else
  1234. return nullptr;
  1235. }
  1236. const CGTownInstance * CAdvMapInt::curTown() const
  1237. {
  1238. if(selection && selection->ID == Obj::TOWN)
  1239. return static_cast<const CGTownInstance *>(selection);
  1240. else
  1241. return nullptr;
  1242. }
  1243. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1244. {
  1245. const IShipyard *ret = IShipyard::castFrom(obj);
  1246. if(!ret ||
  1247. obj->tempOwner != player ||
  1248. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  1249. return nullptr;
  1250. return ret;
  1251. }
  1252. void CAdvMapInt::aiTurnStarted()
  1253. {
  1254. if(settings["session"]["spectate"].Bool())
  1255. return;
  1256. adjustActiveness(true);
  1257. CCS->musich->playMusicFromSet("enemy-turn", true, false);
  1258. adventureInt->minimap.setAIRadar(true);
  1259. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1260. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1261. }
  1262. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1263. {
  1264. bool wasActive = isActive();
  1265. if(wasActive)
  1266. deactivate();
  1267. adventureInt->duringAITurn = aiTurnStart;
  1268. if(wasActive)
  1269. activate();
  1270. }
  1271. void CAdvMapInt::quickCombatLock()
  1272. {
  1273. if(!duringAITurn)
  1274. deactivate();
  1275. }
  1276. void CAdvMapInt::quickCombatUnlock()
  1277. {
  1278. if(!duringAITurn)
  1279. activate();
  1280. }
  1281. void CAdvMapInt::changeMode(EAdvMapMode newMode, float newScale)
  1282. {
  1283. if (mode != newMode)
  1284. {
  1285. mode = newMode;
  1286. switch (mode)
  1287. {
  1288. case EAdvMapMode::NORMAL:
  1289. panelMain->activate();
  1290. panelWorldView->deactivate();
  1291. activeMapPanel = panelMain;
  1292. townList.activate();
  1293. heroList.activate();
  1294. infoBar.activate();
  1295. worldViewOptions.clear();
  1296. break;
  1297. case EAdvMapMode::WORLD_VIEW:
  1298. panelMain->deactivate();
  1299. panelWorldView->activate();
  1300. activeMapPanel = panelWorldView;
  1301. townList.deactivate();
  1302. heroList.deactivate();
  1303. infoBar.showSelection(); // to prevent new day animation interfering world view mode
  1304. infoBar.deactivate();
  1305. break;
  1306. }
  1307. worldViewScale = newScale;
  1308. redraw();
  1309. }
  1310. else if (worldViewScale != newScale) // still in world view mode, but the scale changed
  1311. {
  1312. worldViewScale = newScale;
  1313. redraw();
  1314. }
  1315. }
  1316. CAdvMapInt::WorldViewOptions::WorldViewOptions()
  1317. {
  1318. clear();
  1319. }
  1320. void CAdvMapInt::WorldViewOptions::clear()
  1321. {
  1322. showAllTerrain = false;
  1323. iconPositions.clear();
  1324. }
  1325. void CAdvMapInt::WorldViewOptions::adjustDrawingInfo(MapDrawingInfo& info)
  1326. {
  1327. info.showAllTerrain = showAllTerrain;
  1328. info.additionalIcons = &iconPositions;
  1329. }