CAdvMapInt.cpp 43 KB

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