CAdvmapInterface.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  1. /*
  2. * CAdvmapInterface.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/CCursorHandler.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. #ifdef _MSC_VER
  52. #pragma warning (disable : 4355)
  53. #endif
  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->changeGraphic(ECursor::ADVENTURE, 33);
  63. else if(direction & CAdvMapInt::DOWN)
  64. CCS->curh->changeGraphic(ECursor::ADVENTURE, 35);
  65. else
  66. CCS->curh->changeGraphic(ECursor::ADVENTURE, 34);
  67. }
  68. else if(direction & CAdvMapInt::LEFT)
  69. {
  70. if(direction & CAdvMapInt::UP)
  71. CCS->curh->changeGraphic(ECursor::ADVENTURE, 39);
  72. else if(direction & CAdvMapInt::DOWN)
  73. CCS->curh->changeGraphic(ECursor::ADVENTURE, 37);
  74. else
  75. CCS->curh->changeGraphic(ECursor::ADVENTURE, 38);
  76. }
  77. else if(direction & CAdvMapInt::UP)
  78. CCS->curh->changeGraphic(ECursor::ADVENTURE, 32);
  79. else if(direction & CAdvMapInt::DOWN)
  80. CCS->curh->changeGraphic(ECursor::ADVENTURE, 36);
  81. }
  82. CTerrainRect::CTerrainRect()
  83. : fadeSurface(nullptr),
  84. lastRedrawStatus(EMapAnimRedrawStatus::OK),
  85. fadeAnim(std::make_shared<CFadeAnimation>()),
  86. curHoveredTile(-1,-1,-1),
  87. currentPath(nullptr)
  88. {
  89. tilesw=(ADVOPT.advmapW+31)/32;
  90. tilesh=(ADVOPT.advmapH+31)/32;
  91. pos.x=ADVOPT.advmapX;
  92. pos.y=ADVOPT.advmapY;
  93. pos.w=ADVOPT.advmapW;
  94. pos.h=ADVOPT.advmapH;
  95. moveX = moveY = 0;
  96. addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
  97. }
  98. CTerrainRect::~CTerrainRect()
  99. {
  100. if(fadeSurface)
  101. SDL_FreeSurface(fadeSurface);
  102. }
  103. void CTerrainRect::deactivate()
  104. {
  105. CIntObject::deactivate();
  106. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  107. }
  108. void CTerrainRect::clickLeft(tribool down, bool previousState)
  109. {
  110. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  111. return;
  112. if(indeterminate(down))
  113. return;
  114. #ifdef VCMI_ANDROID
  115. // todo ios
  116. if(adventureInt->swipeEnabled)
  117. {
  118. if(handleSwipeStateChange((bool)down == true))
  119. {
  120. return; // if swipe is enabled, we don't process "down" events and wait for "up" (to make sure this wasn't a swiping gesture)
  121. }
  122. }
  123. else
  124. {
  125. #endif
  126. if(down == false)
  127. return;
  128. #ifdef VCMI_ANDROID
  129. }
  130. #endif
  131. int3 mp = whichTileIsIt();
  132. if(mp.x < 0 || mp.y < 0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  133. return;
  134. adventureInt->tileLClicked(mp);
  135. }
  136. void CTerrainRect::clickRight(tribool down, bool previousState)
  137. {
  138. #ifdef VCMI_ANDROID
  139. // todo ios
  140. if(adventureInt->swipeEnabled && isSwiping)
  141. return;
  142. #endif
  143. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  144. return;
  145. int3 mp = whichTileIsIt();
  146. if(CGI->mh->map->isInTheMap(mp) && down)
  147. adventureInt->tileRClicked(mp);
  148. }
  149. void CTerrainRect::clickMiddle(tribool down, bool previousState)
  150. {
  151. handleSwipeStateChange((bool)down == true);
  152. }
  153. void CTerrainRect::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  154. {
  155. handleHover(sEvent);
  156. if(!adventureInt->swipeEnabled)
  157. return;
  158. handleSwipeMove(sEvent);
  159. }
  160. void CTerrainRect::handleSwipeMove(const SDL_MouseMotionEvent & sEvent)
  161. {
  162. #ifdef VCMI_ANDROID
  163. // todo ios
  164. if(sEvent.state == 0) // any "button" is enough on android
  165. #else //!VCMI_ANDROID
  166. if((sEvent.state & SDL_BUTTON_MMASK) == 0) // swipe only works with middle mouse on other platforms
  167. #endif //!VCMI_ANDROID
  168. {
  169. return;
  170. }
  171. if(!isSwiping)
  172. {
  173. // try to distinguish if this touch was meant to be a swipe or just fat-fingering press
  174. if(abs(sEvent.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
  175. abs(sEvent.y - swipeInitialRealPos.y) > SwipeTouchSlop)
  176. {
  177. isSwiping = true;
  178. }
  179. }
  180. if(isSwiping)
  181. {
  182. adventureInt->swipeTargetPosition.x =
  183. swipeInitialMapPos.x + static_cast<si32>(swipeInitialRealPos.x - sEvent.x) / 32;
  184. adventureInt->swipeTargetPosition.y =
  185. swipeInitialMapPos.y + static_cast<si32>(swipeInitialRealPos.y - sEvent.y) / 32;
  186. adventureInt->swipeMovementRequested = true;
  187. }
  188. }
  189. bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
  190. {
  191. if(btnPressed)
  192. {
  193. swipeInitialRealPos = int3(GH.current->motion.x, GH.current->motion.y, 0);
  194. swipeInitialMapPos = int3(adventureInt->position);
  195. return true;
  196. }
  197. else if(isSwiping) // only accept this touch if it wasn't a swipe
  198. {
  199. isSwiping = false;
  200. return true;
  201. }
  202. return false;
  203. }
  204. void CTerrainRect::handleHover(const SDL_MouseMotionEvent &sEvent)
  205. {
  206. int3 tHovered = whichTileIsIt(sEvent.x, sEvent.y);
  207. int3 pom = adventureInt->verifyPos(tHovered);
  208. if(tHovered != pom) //tile outside the map
  209. {
  210. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  211. return;
  212. }
  213. if (pom != curHoveredTile)
  214. {
  215. curHoveredTile = pom;
  216. adventureInt->tileHovered(pom);
  217. }
  218. }
  219. void CTerrainRect::hover(bool on)
  220. {
  221. if (!on)
  222. {
  223. adventureInt->statusbar->clear();
  224. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  225. }
  226. //Hoverable::hover(on);
  227. }
  228. void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
  229. {
  230. const static int pns[9][9] = {
  231. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  232. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  233. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  234. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  235. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  236. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  237. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  238. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  239. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  240. }; //table of magic values TODO meaning, change variable name
  241. for (int i = 0; i < -1 + (int)currentPath->nodes.size(); ++i)
  242. {
  243. const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
  244. if(curPos.z != adventureInt->position.z)
  245. continue;
  246. int pn=-1;//number of picture
  247. if (i==0) //last tile
  248. {
  249. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  250. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  251. if (x<0 || y<0 || x>pos.w || y>pos.h)
  252. continue;
  253. pn=0;
  254. }
  255. else
  256. {
  257. const int3 &prevPos = currentPath->nodes[i-1].coord;
  258. std::vector<CGPathNode> & cv = currentPath->nodes;
  259. /* Vector directions
  260. * 0 1 2
  261. * \ | /
  262. * 3 - 4 - 5
  263. * / | \
  264. * 6 7 8
  265. *For example:
  266. * |
  267. * |__\
  268. * /
  269. * is id1=7, id2=5 (pns[7][5])
  270. */
  271. bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
  272. if(pathContinuous && cv[i].action != CGPathNode::EMBARK && cv[i].action != CGPathNode::DISEMBARK)
  273. {
  274. int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
  275. int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
  276. pn=pns[id1][id2];
  277. }
  278. else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
  279. {
  280. pn = 0;
  281. }
  282. }
  283. if (currentPath->nodes[i].turns)
  284. pn+=25;
  285. if (pn>=0)
  286. {
  287. const auto arrow = graphics->heroMoveArrows->getImage(pn);
  288. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  289. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  290. if (x< -32 || y< -32 || x>pos.w || y>pos.h)
  291. continue;
  292. int hvx = (x + arrow->width()) - (pos.x + pos.w),
  293. hvy = (y + arrow->height()) - (pos.y + pos.h);
  294. SDL_Rect prevClip;
  295. SDL_GetClipRect(to, &prevClip);
  296. SDL_SetClipRect(to, extRect); //preventing blitting outside of that rect
  297. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  298. {
  299. if (hvx<0 && hvy<0)
  300. {
  301. arrow->draw(to, x + moveX, y + moveY);
  302. }
  303. else if(hvx<0)
  304. {
  305. Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  306. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  307. }
  308. else if (hvy<0)
  309. {
  310. Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  311. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  312. }
  313. else
  314. {
  315. Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  316. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  317. }
  318. }
  319. else //standard version
  320. {
  321. if (hvx<0 && hvy<0)
  322. {
  323. arrow->draw(to, x, y);
  324. }
  325. else if(hvx<0)
  326. {
  327. Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  328. arrow->draw(to, x, y, &srcRect);
  329. }
  330. else if (hvy<0)
  331. {
  332. Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  333. arrow->draw(to, x, y, &srcRect);
  334. }
  335. else
  336. {
  337. Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  338. arrow->draw(to, x, y, &srcRect);
  339. }
  340. }
  341. SDL_SetClipRect(to, &prevClip);
  342. }
  343. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  344. }
  345. void CTerrainRect::show(SDL_Surface * to)
  346. {
  347. if (adventureInt->mode == EAdvMapMode::NORMAL)
  348. {
  349. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), &pos);
  350. info.otherheroAnim = true;
  351. info.anim = adventureInt->anim;
  352. info.heroAnim = adventureInt->heroAnim;
  353. if (ADVOPT.smoothMove)
  354. info.movement = int3(moveX, moveY, 0);
  355. lastRedrawStatus = CGI->mh->drawTerrainRectNew(to, &info);
  356. if (fadeAnim->isFading())
  357. {
  358. Rect r(pos);
  359. fadeAnim->update();
  360. fadeAnim->draw(to, nullptr, &r);
  361. }
  362. if (currentPath/* && adventureInt->position.z==currentPath->startPos().z*/) //drawing path
  363. {
  364. showPath(&pos, to);
  365. }
  366. }
  367. }
  368. void CTerrainRect::showAll(SDL_Surface * to)
  369. {
  370. // world view map is static and doesn't need redraw every frame
  371. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  372. {
  373. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), &pos, adventureInt->worldViewIcons);
  374. info.scaled = true;
  375. info.scale = adventureInt->worldViewScale;
  376. adventureInt->worldViewOptions.adjustDrawingInfo(info);
  377. CGI->mh->drawTerrainRectNew(to, &info);
  378. }
  379. }
  380. void CTerrainRect::showAnim(SDL_Surface * to)
  381. {
  382. if (fadeAnim->isFading())
  383. show(to);
  384. else if (lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED)
  385. show(to); // currently the same; maybe we should pass some flag to map handler so it redraws ONLY tiles that need redraw instead of full
  386. }
  387. int3 CTerrainRect::whichTileIsIt(const int x, const int y)
  388. {
  389. int3 ret;
  390. ret.x = adventureInt->position.x + ((x-CGI->mh->offsetX-pos.x)/32);
  391. ret.y = adventureInt->position.y + ((y-CGI->mh->offsetY-pos.y)/32);
  392. ret.z = adventureInt->position.z;
  393. return ret;
  394. }
  395. int3 CTerrainRect::whichTileIsIt()
  396. {
  397. if(GH.current)
  398. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  399. else
  400. return int3(-1);
  401. }
  402. int3 CTerrainRect::tileCountOnScreen()
  403. {
  404. switch (adventureInt->mode)
  405. {
  406. default:
  407. logGlobal->error("Unknown map mode %d", (int)adventureInt->mode);
  408. return int3();
  409. case EAdvMapMode::NORMAL:
  410. return int3(tilesw, tilesh, 1);
  411. case EAdvMapMode::WORLD_VIEW:
  412. return int3((si32)(tilesw / adventureInt->worldViewScale), (si32)(tilesh / adventureInt->worldViewScale), 1);
  413. }
  414. }
  415. void CTerrainRect::fadeFromCurrentView()
  416. {
  417. if (!ADVOPT.screenFading)
  418. return;
  419. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  420. return;
  421. if (!fadeSurface)
  422. fadeSurface = CSDL_Ext::newSurface(pos.w, pos.h);
  423. SDL_BlitSurface(screen, &pos, fadeSurface, nullptr);
  424. fadeAnim->init(CFadeAnimation::EMode::OUT, fadeSurface);
  425. }
  426. bool CTerrainRect::needsAnimUpdate()
  427. {
  428. return fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED;
  429. }
  430. void CResDataBar::clickRight(tribool down, bool previousState)
  431. {
  432. }
  433. CResDataBar::CResDataBar(const std::string & defname, int x, int y, int offx, int offy, int resdist, int datedist)
  434. {
  435. pos.x += x;
  436. pos.y += y;
  437. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  438. background = std::make_shared<CPicture>(defname, 0, 0);
  439. background->colorize(LOCPLINT->playerID);
  440. pos.w = background->bg->w;
  441. pos.h = background->bg->h;
  442. txtpos.resize(8);
  443. for (int i = 0; i < 8 ; i++)
  444. {
  445. txtpos[i].first = pos.x + offx + resdist*i;
  446. txtpos[i].second = pos.y + offy;
  447. }
  448. txtpos[7].first = txtpos[6].first + datedist;
  449. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  450. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  451. addUsedEvents(RCLICK);
  452. }
  453. CResDataBar::CResDataBar()
  454. {
  455. pos.x += ADVOPT.resdatabarX;
  456. pos.y += ADVOPT.resdatabarY;
  457. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  458. background = std::make_shared<CPicture>(ADVOPT.resdatabarG, 0, 0);
  459. background->colorize(LOCPLINT->playerID);
  460. pos.w = background->bg->w;
  461. pos.h = background->bg->h;
  462. txtpos.resize(8);
  463. for (int i = 0; i < 8 ; i++)
  464. {
  465. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  466. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  467. }
  468. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  469. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  470. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  471. }
  472. CResDataBar::~CResDataBar() = default;
  473. void CResDataBar::draw(SDL_Surface * to)
  474. {
  475. //TODO: all this should be labels, but they require proper text update on change
  476. for (auto i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  477. {
  478. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  479. graphics->fonts[FONT_SMALL]->renderTextLeft(to, text, Colors::WHITE, Point(txtpos[i].first,txtpos[i].second));
  480. }
  481. std::vector<std::string> temp;
  482. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  483. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  484. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  485. graphics->fonts[FONT_SMALL]->renderTextLeft(to, processStr(datetext,temp), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second));
  486. }
  487. void CResDataBar::show(SDL_Surface * to)
  488. {
  489. }
  490. void CResDataBar::showAll(SDL_Surface * to)
  491. {
  492. CIntObject::showAll(to);
  493. draw(to);
  494. }
  495. CAdvMapInt::CAdvMapInt():
  496. mode(EAdvMapMode::NORMAL),
  497. worldViewScale(0.0f), //actual init later in changeMode
  498. minimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH)),
  499. statusbar(CGStatusBar::create(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG)),
  500. heroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD),
  501. townList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD),
  502. infoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192)), state(NA),
  503. spellBeingCasted(nullptr), position(int3(0, 0, 0)), selection(nullptr),
  504. updateScreen(false), anim(0), animValHitCount(0), heroAnim(0), heroAnimValHitCount(0),
  505. activeMapPanel(nullptr), duringAITurn(false), scrollingDir(0), scrollingState(false),
  506. swipeEnabled(settings["general"]["swipe"].Bool()), swipeMovementRequested(false),
  507. swipeTargetPosition(int3(-1, -1, -1))
  508. {
  509. pos.x = pos.y = 0;
  510. pos.w = screen->w;
  511. pos.h = screen->h;
  512. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  513. townList.onSelect = std::bind(&CAdvMapInt::selectionChanged,this);
  514. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  515. if (ADVOPT.worldViewGraphic != "")
  516. {
  517. bgWorldView = BitmapHandler::loadBitmap(ADVOPT.worldViewGraphic);
  518. }
  519. else
  520. {
  521. bgWorldView = nullptr;
  522. logGlobal->warn("ADVOPT.worldViewGraphic is empty => bitmap not loaded");
  523. }
  524. if (!bgWorldView)
  525. {
  526. logGlobal->warn("bgWorldView not defined in resolution config; fallback to VWorld.bmp");
  527. bgWorldView = BitmapHandler::loadBitmap("VWorld.bmp");
  528. }
  529. worldViewIcons = std::make_shared<CAnimation>("VwSymbol");//todo: customize with ADVOPT
  530. worldViewIcons->preload();
  531. for(int g = 0; g < ADVOPT.gemG.size(); ++g)
  532. {
  533. gems.push_back(std::make_shared<CAnimImage>(ADVOPT.gemG[g], 0, 0, ADVOPT.gemX[g], ADVOPT.gemY[g]));
  534. }
  535. auto makeButton = [&](int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> std::shared_ptr<CButton>
  536. {
  537. auto button = std::make_shared<CButton>(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  538. for(auto image : info.additionalDefs)
  539. button->addImage(image);
  540. return button;
  541. };
  542. kingOverview = makeButton(293, std::bind(&CAdvMapInt::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  543. underground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  544. questlog = makeButton(295, std::bind(&CAdvMapInt::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  545. sleepWake = makeButton(296, std::bind(&CAdvMapInt::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  546. moveHero = makeButton(297, std::bind(&CAdvMapInt::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  547. spellbook = makeButton(298, std::bind(&CAdvMapInt::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  548. advOptions = makeButton(299, std::bind(&CAdvMapInt::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  549. sysOptions = makeButton(300, std::bind(&CAdvMapInt::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  550. nextHero = makeButton(301, std::bind(&CAdvMapInt::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  551. endTurn = makeButton(302, std::bind(&CAdvMapInt::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  552. int panelSpaceBottom = screen->h - resdatabar.pos.h - 4;
  553. panelMain = std::make_shared<CAdvMapPanel>(nullptr, Point(0, 0));
  554. // TODO correct drawing position
  555. panelWorldView = std::make_shared<CAdvMapWorldViewPanel>(worldViewIcons, bgWorldView, Point(heroList.pos.x - 2, 195), panelSpaceBottom, LOCPLINT->playerID);
  556. panelMain->addChildColorableButton(kingOverview);
  557. panelMain->addChildColorableButton(underground);
  558. panelMain->addChildColorableButton(questlog);
  559. panelMain->addChildColorableButton(sleepWake);
  560. panelMain->addChildColorableButton(moveHero);
  561. panelMain->addChildColorableButton(spellbook);
  562. panelMain->addChildColorableButton(advOptions);
  563. panelMain->addChildColorableButton(sysOptions);
  564. panelMain->addChildColorableButton(nextHero);
  565. panelMain->addChildColorableButton(endTurn);
  566. // TODO move configs to resolutions.json, similarly to previous buttons
  567. config::ButtonInfo worldViewBackConfig = config::ButtonInfo();
  568. worldViewBackConfig.defName = "IOK6432.DEF";
  569. worldViewBackConfig.x = screen->w - 73;
  570. worldViewBackConfig.y = 343 + 195;
  571. worldViewBackConfig.playerColoured = false;
  572. panelWorldView->addChildToPanel(
  573. makeButton(288, std::bind(&CAdvMapInt::fworldViewBack,this), worldViewBackConfig, SDLK_ESCAPE), ACTIVATE | DEACTIVATE);
  574. config::ButtonInfo worldViewPuzzleConfig = config::ButtonInfo();
  575. worldViewPuzzleConfig.defName = "VWPUZ.DEF";
  576. worldViewPuzzleConfig.x = screen->w - 188;
  577. worldViewPuzzleConfig.y = 343 + 195;
  578. worldViewPuzzleConfig.playerColoured = false;
  579. panelWorldView->addChildToPanel( // no help text for this one
  580. std::make_shared<CButton>(Point(worldViewPuzzleConfig.x, worldViewPuzzleConfig.y), worldViewPuzzleConfig.defName, std::pair<std::string, std::string>(),
  581. std::bind(&CPlayerInterface::showPuzzleMap,LOCPLINT), SDLK_p, worldViewPuzzleConfig.playerColoured), ACTIVATE | DEACTIVATE);
  582. config::ButtonInfo worldViewScale1xConfig = config::ButtonInfo();
  583. worldViewScale1xConfig.defName = "VWMAG1.DEF";
  584. worldViewScale1xConfig.x = screen->w - 191;
  585. worldViewScale1xConfig.y = 23 + 195;
  586. worldViewScale1xConfig.playerColoured = false;
  587. panelWorldView->addChildToPanel( // help text is wrong for this button
  588. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale1x,this), worldViewScale1xConfig, SDLK_1), ACTIVATE | DEACTIVATE);
  589. config::ButtonInfo worldViewScale2xConfig = config::ButtonInfo();
  590. worldViewScale2xConfig.defName = "VWMAG2.DEF";
  591. worldViewScale2xConfig.x = screen->w - 191 + 63;
  592. worldViewScale2xConfig.y = 23 + 195;
  593. worldViewScale2xConfig.playerColoured = false;
  594. panelWorldView->addChildToPanel( // help text is wrong for this button
  595. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale2x,this), worldViewScale2xConfig, SDLK_2), ACTIVATE | DEACTIVATE);
  596. config::ButtonInfo worldViewScale4xConfig = config::ButtonInfo();
  597. worldViewScale4xConfig.defName = "VWMAG4.DEF";
  598. worldViewScale4xConfig.x = screen->w - 191 + 126;
  599. worldViewScale4xConfig.y = 23 + 195;
  600. worldViewScale4xConfig.playerColoured = false;
  601. panelWorldView->addChildToPanel( // help text is wrong for this button
  602. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale4x,this), worldViewScale4xConfig, SDLK_4), ACTIVATE | DEACTIVATE);
  603. config::ButtonInfo worldViewUndergroundConfig = config::ButtonInfo();
  604. worldViewUndergroundConfig.defName = "IAM010.DEF";
  605. worldViewUndergroundConfig.additionalDefs.push_back("IAM003.DEF");
  606. worldViewUndergroundConfig.x = screen->w - 115;
  607. worldViewUndergroundConfig.y = 343 + 195;
  608. worldViewUndergroundConfig.playerColoured = true;
  609. worldViewUnderground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), worldViewUndergroundConfig, SDLK_u);
  610. panelWorldView->addChildColorableButton(worldViewUnderground);
  611. setPlayer(LOCPLINT->playerID);
  612. int iconColorMultiplier = player.getNum() * 19;
  613. int wvLeft = heroList.pos.x - 2; // TODO correct drawing position
  614. //int wvTop = 195;
  615. for (int i = 0; i < 5; ++i)
  616. {
  617. panelWorldView->addChildIcon(std::pair<int, Point>(i, Point(5, 58 + i * 20)), iconColorMultiplier);
  618. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 263 + i * 20, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  619. Colors::WHITE, CGI->generaltexth->allTexts[612 + i]));
  620. }
  621. for (int i = 0; i < 7; ++i)
  622. {
  623. panelWorldView->addChildIcon(std::pair<int, Point>(i + 5, Point(5, 182 + i * 20)), iconColorMultiplier);
  624. panelWorldView->addChildIcon(std::pair<int, Point>(i + 12, Point(160, 182 + i * 20)), iconColorMultiplier);
  625. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 387 + i * 20, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  626. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  627. }
  628. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 5, 367, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  629. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  630. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 367, EFonts::FONT_SMALL, EAlignment::TOPLEFT,
  631. Colors::WHITE, CGI->generaltexth->allTexts[618]));
  632. activeMapPanel = panelMain;
  633. changeMode(EAdvMapMode::NORMAL);
  634. underground->block(!CGI->mh->map->twoLevel);
  635. questlog->block(!CGI->mh->map->quests.size());
  636. worldViewUnderground->block(!CGI->mh->map->twoLevel);
  637. addUsedEvents(MOVE);
  638. }
  639. CAdvMapInt::~CAdvMapInt()
  640. {
  641. SDL_FreeSurface(bg);
  642. }
  643. void CAdvMapInt::fshowOverview()
  644. {
  645. GH.pushIntT<CKingdomInterface>();
  646. }
  647. void CAdvMapInt::fworldViewBack()
  648. {
  649. changeMode(EAdvMapMode::NORMAL);
  650. CGI->mh->discardWorldViewCache();
  651. auto hero = curHero();
  652. if (hero)
  653. centerOn(hero);
  654. }
  655. void CAdvMapInt::fworldViewScale1x()
  656. {
  657. // TODO set corresponding scale button to "selected" mode
  658. changeMode(EAdvMapMode::WORLD_VIEW, 0.22f);
  659. }
  660. void CAdvMapInt::fworldViewScale2x()
  661. {
  662. changeMode(EAdvMapMode::WORLD_VIEW, 0.36f);
  663. }
  664. void CAdvMapInt::fworldViewScale4x()
  665. {
  666. changeMode(EAdvMapMode::WORLD_VIEW, 0.5f);
  667. }
  668. void CAdvMapInt::fswitchLevel()
  669. {
  670. // with support for future multi-level maps :)
  671. int maxLevels = CGI->mh->map->levels();
  672. if (maxLevels < 2)
  673. return;
  674. position.z = (position.z + 1) % maxLevels;
  675. underground->setIndex(position.z, true);
  676. underground->redraw();
  677. worldViewUnderground->setIndex(position.z, true);
  678. worldViewUnderground->redraw();
  679. updateScreen = true;
  680. minimap.setLevel(position.z);
  681. if (mode == EAdvMapMode::WORLD_VIEW)
  682. terrain.redraw();
  683. }
  684. void CAdvMapInt::fshowQuestlog()
  685. {
  686. LOCPLINT->showQuestLog();
  687. }
  688. void CAdvMapInt::fsleepWake()
  689. {
  690. const CGHeroInstance *h = curHero();
  691. if (!h)
  692. return;
  693. bool newSleep = !isHeroSleeping(h);
  694. setHeroSleeping(h, newSleep);
  695. updateSleepWake(h);
  696. if (newSleep)
  697. {
  698. fnextHero();
  699. //moveHero.block(true);
  700. //uncomment to enable original HoMM3 behaviour:
  701. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  702. }
  703. }
  704. void CAdvMapInt::fmoveHero()
  705. {
  706. const CGHeroInstance *h = curHero();
  707. if (!h || !terrain.currentPath || !CGI->mh->canStartHeroMovement())
  708. return;
  709. LOCPLINT->moveHero(h, *terrain.currentPath);
  710. }
  711. void CAdvMapInt::fshowSpellbok()
  712. {
  713. if (!curHero()) //checking necessary values
  714. return;
  715. centerOn(selection);
  716. GH.pushIntT<CSpellWindow>(curHero(), LOCPLINT, false);
  717. }
  718. void CAdvMapInt::fadventureOPtions()
  719. {
  720. GH.pushIntT<CAdventureOptions>();
  721. }
  722. void CAdvMapInt::fsystemOptions()
  723. {
  724. GH.pushIntT<CSystemOptionsWindow>();
  725. }
  726. void CAdvMapInt::fnextHero()
  727. {
  728. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  729. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  730. if (next < 0)
  731. return;
  732. select(LOCPLINT->wanderingHeroes[next], true);
  733. }
  734. void CAdvMapInt::fendTurn()
  735. {
  736. if(!LOCPLINT->makingTurn)
  737. return;
  738. if(settings["adventure"]["heroReminder"].Bool())
  739. {
  740. for(auto hero : LOCPLINT->wanderingHeroes)
  741. {
  742. if(!isHeroSleeping(hero) && hero->movement > 0)
  743. {
  744. // Only show hero reminder if conditions met:
  745. // - There still movement points
  746. // - Hero don't have a path or there not points for first step on path
  747. auto path = LOCPLINT->getAndVerifyPath(hero);
  748. if(!path || path->nodes.size() < 2 || !path->nodes[path->nodes.size()-2].turns)
  749. {
  750. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], std::bind(&CAdvMapInt::endingTurn, this), nullptr);
  751. return;
  752. }
  753. }
  754. }
  755. }
  756. endingTurn();
  757. }
  758. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  759. {
  760. sleepWake->block(!h);
  761. if (!h)
  762. return;
  763. bool state = isHeroSleeping(h);
  764. sleepWake->setIndex(state ? 1 : 0, true);
  765. sleepWake->assignedKeys.clear();
  766. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  767. }
  768. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  769. {
  770. if(!h)
  771. {
  772. moveHero->block(true);
  773. return;
  774. }
  775. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  776. if(boost::logic::indeterminate(hasPath))
  777. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  778. moveHero->block(!(bool)hasPath || (h->movement == 0));
  779. }
  780. void CAdvMapInt::updateSpellbook(const CGHeroInstance *h)
  781. {
  782. spellbook->block(!h);
  783. }
  784. int CAdvMapInt::getNextHeroIndex(int startIndex)
  785. {
  786. if (LOCPLINT->wanderingHeroes.size() == 0)
  787. return -1;
  788. if (startIndex < 0)
  789. startIndex = 0;
  790. int i = startIndex;
  791. do
  792. {
  793. i++;
  794. if (i >= LOCPLINT->wanderingHeroes.size())
  795. i = 0;
  796. }
  797. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  798. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  799. return i;
  800. else
  801. return -1;
  802. }
  803. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  804. {
  805. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  806. int next = getNextHeroIndex(start);
  807. if (next < 0)
  808. {
  809. nextHero->block(true);
  810. return;
  811. }
  812. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  813. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  814. nextHero->block(noActiveHeroes);
  815. }
  816. void CAdvMapInt::activate()
  817. {
  818. CIntObject::activate();
  819. if (!(active & KEYBOARD))
  820. CIntObject::activate(KEYBOARD);
  821. screenBuf = screen;
  822. GH.statusbar = statusbar;
  823. if(!duringAITurn)
  824. {
  825. activeMapPanel->activate();
  826. if (mode == EAdvMapMode::NORMAL)
  827. {
  828. heroList.activate();
  829. townList.activate();
  830. infoBar.activate();
  831. }
  832. minimap.activate();
  833. terrain.activate();
  834. if(LOCPLINT)
  835. LOCPLINT->cingconsole->activate();
  836. GH.fakeMouseMove(); //to restore the cursor
  837. }
  838. }
  839. void CAdvMapInt::deactivate()
  840. {
  841. CIntObject::deactivate();
  842. if(!duringAITurn)
  843. {
  844. scrollingDir = 0;
  845. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  846. activeMapPanel->deactivate();
  847. if (mode == EAdvMapMode::NORMAL)
  848. {
  849. heroList.deactivate();
  850. townList.deactivate();
  851. infoBar.deactivate();
  852. }
  853. minimap.deactivate();
  854. terrain.deactivate();
  855. if(LOCPLINT)
  856. LOCPLINT->cingconsole->deactivate();
  857. }
  858. }
  859. void CAdvMapInt::showAll(SDL_Surface * to)
  860. {
  861. blitAt(bg,0,0,to);
  862. if(state != INGAME)
  863. return;
  864. switch (mode)
  865. {
  866. case EAdvMapMode::NORMAL:
  867. heroList.showAll(to);
  868. townList.showAll(to);
  869. infoBar.showAll(to);
  870. break;
  871. case EAdvMapMode::WORLD_VIEW:
  872. terrain.showAll(to);
  873. break;
  874. }
  875. activeMapPanel->showAll(to);
  876. updateScreen = true;
  877. minimap.showAll(to);
  878. show(to);
  879. resdatabar.showAll(to);
  880. statusbar->show(to);
  881. LOCPLINT->cingconsole->show(to);
  882. }
  883. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  884. {
  885. if (!hero)
  886. return false;
  887. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  888. }
  889. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  890. {
  891. if (sleep)
  892. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  893. else
  894. LOCPLINT->sleepingHeroes -= hero;
  895. updateNextHero(nullptr);
  896. }
  897. void CAdvMapInt::show(SDL_Surface * to)
  898. {
  899. if(state != INGAME)
  900. return;
  901. ++animValHitCount; //for animations
  902. if(animValHitCount % 2 == 0)
  903. {
  904. ++heroAnim;
  905. }
  906. if(animValHitCount == 8)
  907. {
  908. CGI->mh->updateWater();
  909. animValHitCount = 0;
  910. ++anim;
  911. updateScreen = true;
  912. }
  913. if(swipeEnabled)
  914. {
  915. handleSwipeUpdate();
  916. }
  917. #ifdef VCMI_ANDROID // on android, map-moving mode is exclusive (TODO technically it might work with both enabled; to be checked)
  918. // todo ios
  919. else
  920. #endif // VCMI_ANDROID
  921. {
  922. handleMapScrollingUpdate();
  923. }
  924. for(int i = 0; i < 4; i++)
  925. {
  926. if(settings["session"]["spectate"].Bool())
  927. gems[i]->setFrame(PlayerColor(1).getNum());
  928. else
  929. gems[i]->setFrame(LOCPLINT->playerID.getNum());
  930. }
  931. if(updateScreen)
  932. {
  933. int3 betterPos = LOCPLINT->repairScreenPos(position);
  934. if (betterPos != position)
  935. {
  936. logGlobal->warn("Incorrect position for adventure map!");
  937. position = betterPos;
  938. }
  939. terrain.show(to);
  940. for(int i = 0; i < 4; i++)
  941. gems[i]->showAll(to);
  942. updateScreen=false;
  943. LOCPLINT->cingconsole->show(to);
  944. }
  945. else if (terrain.needsAnimUpdate())
  946. {
  947. terrain.showAnim(to);
  948. for(int i = 0; i < 4; i++)
  949. gems[i]->showAll(to);
  950. }
  951. infoBar.show(to);
  952. statusbar->showAll(to);
  953. }
  954. void CAdvMapInt::handleMapScrollingUpdate()
  955. {
  956. int scrollSpeed = static_cast<int>(settings["adventure"]["scrollSpeed"].Float());
  957. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  958. if((animValHitCount % (4 / scrollSpeed)) == 0
  959. && ((GH.topInt().get() == this) || isCtrlKeyDown()))
  960. {
  961. if((scrollingDir & LEFT) && (position.x > -CGI->mh->frameW))
  962. position.x--;
  963. if((scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW))
  964. position.x++;
  965. if((scrollingDir & UP) && (position.y > -CGI->mh->frameH))
  966. position.y--;
  967. if((scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH))
  968. position.y++;
  969. if(scrollingDir)
  970. {
  971. setScrollingCursor(scrollingDir);
  972. scrollingState = true;
  973. updateScreen = true;
  974. minimap.redraw();
  975. if(mode == EAdvMapMode::WORLD_VIEW)
  976. terrain.redraw();
  977. }
  978. else if(scrollingState)
  979. {
  980. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  981. scrollingState = false;
  982. }
  983. }
  984. }
  985. void CAdvMapInt::handleSwipeUpdate()
  986. {
  987. if(swipeMovementRequested)
  988. {
  989. auto fixedPos = LOCPLINT->repairScreenPos(swipeTargetPosition);
  990. position.x = fixedPos.x;
  991. position.y = fixedPos.y;
  992. CCS->curh->changeGraphic(ECursor::DEFAULT, 0);
  993. updateScreen = true;
  994. minimap.redraw();
  995. swipeMovementRequested = false;
  996. }
  997. }
  998. void CAdvMapInt::selectionChanged()
  999. {
  1000. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  1001. if (selection != to)
  1002. select(to);
  1003. }
  1004. void CAdvMapInt::centerOn(int3 on, bool fade)
  1005. {
  1006. bool switchedLevels = on.z != position.z;
  1007. if (fade)
  1008. {
  1009. terrain.fadeFromCurrentView();
  1010. }
  1011. switch (mode)
  1012. {
  1013. default:
  1014. case EAdvMapMode::NORMAL:
  1015. on.x -= CGI->mh->frameW; // is this intentional? frame size doesn't really have to correspond to camera size...
  1016. on.y -= CGI->mh->frameH;
  1017. break;
  1018. case EAdvMapMode::WORLD_VIEW:
  1019. on.x -= static_cast<si32>(CGI->mh->tilesW / 2 / worldViewScale);
  1020. on.y -= static_cast<si32>(CGI->mh->tilesH / 2 / worldViewScale);
  1021. break;
  1022. }
  1023. on = LOCPLINT->repairScreenPos(on);
  1024. position = on;
  1025. updateScreen=true;
  1026. underground->setIndex(on.z,true); //change underground switch button image
  1027. underground->redraw();
  1028. worldViewUnderground->setIndex(on.z, true);
  1029. worldViewUnderground->redraw();
  1030. if (switchedLevels)
  1031. minimap.setLevel(position.z);
  1032. minimap.redraw();
  1033. if (mode == EAdvMapMode::WORLD_VIEW)
  1034. terrain.redraw();
  1035. }
  1036. void CAdvMapInt::centerOn(const CGObjectInstance * obj, bool fade)
  1037. {
  1038. centerOn(obj->getSightCenter(), fade);
  1039. }
  1040. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1041. {
  1042. if (mode == EAdvMapMode::WORLD_VIEW)
  1043. return;
  1044. ui8 Dir = 0;
  1045. SDL_Keycode k = key.keysym.sym;
  1046. const CGHeroInstance *h = curHero(); //selected hero
  1047. const CGTownInstance *t = curTown(); //selected town
  1048. switch(k)
  1049. {
  1050. case SDLK_g:
  1051. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1052. return;
  1053. {
  1054. //find first town with tavern
  1055. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  1056. {
  1057. return town->hasBuilt(BuildingID::TAVERN);
  1058. });
  1059. if(itr != LOCPLINT->towns.end())
  1060. LOCPLINT->showThievesGuildWindow(*itr);
  1061. else
  1062. LOCPLINT->showInfoDialog(CGI->generaltexth->localizedTexts["adventureMap"]["noTownWithTavern"].String());
  1063. }
  1064. return;
  1065. case SDLK_i:
  1066. if(isActive())
  1067. CAdventureOptions::showScenarioInfo();
  1068. return;
  1069. case SDLK_l:
  1070. if(isActive())
  1071. LOCPLINT->proposeLoadingGame();
  1072. return;
  1073. case SDLK_s:
  1074. if(isActive() && key.type == SDL_KEYUP)
  1075. GH.pushIntT<CSavingScreen>();
  1076. return;
  1077. case SDLK_d:
  1078. {
  1079. if(h && isActive() && key.state == SDL_PRESSED)
  1080. LOCPLINT->tryDiggging(h);
  1081. return;
  1082. }
  1083. case SDLK_p:
  1084. if(isActive())
  1085. LOCPLINT->showPuzzleMap();
  1086. return;
  1087. case SDLK_v:
  1088. if(isActive())
  1089. LOCPLINT->viewWorldMap();
  1090. return;
  1091. case SDLK_r:
  1092. if(isActive() && LOCPLINT->ctrlPressed())
  1093. {
  1094. LOCPLINT->showYesNoDialog(CGI->generaltexth->localizedTexts["adventureMap"]["confirmRestartGame"].String(),
  1095. [](){ LOCPLINT->sendCustomEvent(EUserEvent::RESTART_GAME); }, nullptr);
  1096. }
  1097. return;
  1098. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1099. {
  1100. if(!isActive())
  1101. return;
  1102. if(h && key.state == SDL_PRESSED)
  1103. {
  1104. auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
  1105. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  1106. //this thread leaves scope and tries to lock pim while holding gs,
  1107. //network thread tries to lock gs (appluy cl) while holding pim
  1108. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  1109. LOCPLINT->cb->moveHero(h,h->pos);
  1110. }
  1111. }
  1112. return;
  1113. case SDLK_RETURN:
  1114. {
  1115. if(!isActive() || !selection || key.state != SDL_PRESSED)
  1116. return;
  1117. if(h)
  1118. LOCPLINT->openHeroWindow(h);
  1119. else if(t)
  1120. LOCPLINT->openTownWindow(t);
  1121. return;
  1122. }
  1123. case SDLK_ESCAPE:
  1124. {
  1125. if(isActive() || GH.topInt().get() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  1126. return;
  1127. leaveCastingMode();
  1128. return;
  1129. }
  1130. case SDLK_t:
  1131. {
  1132. //act on key down if marketplace windows is not already opened
  1133. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1134. return;
  1135. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  1136. {
  1137. //check if we have any marketplace
  1138. const CGTownInstance *townWithMarket = nullptr;
  1139. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  1140. {
  1141. if(t->hasBuilt(BuildingID::MARKETPLACE))
  1142. {
  1143. townWithMarket = t;
  1144. break;
  1145. }
  1146. }
  1147. if(townWithMarket) //if any town has marketplace, open window
  1148. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  1149. else //if not - complain
  1150. LOCPLINT->showInfoDialog(CGI->generaltexth->localizedTexts["adventureMap"]["noTownWithMarket"].String());
  1151. }
  1152. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  1153. {
  1154. townList.selectNext();
  1155. }
  1156. return;
  1157. }
  1158. default:
  1159. {
  1160. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1161. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1162. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1163. //numpad arrow
  1164. if(CGuiHandler::isArrowKey(k))
  1165. k = CGuiHandler::arrowToNum(k);
  1166. k -= SDLK_KP_1;
  1167. if(k < 0 || k > 8)
  1168. return;
  1169. if (!CGI->mh->canStartHeroMovement())
  1170. return;
  1171. int3 dir = directions[k];
  1172. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  1173. {
  1174. Dir = (dir.x<0 ? LEFT : 0) |
  1175. (dir.x>0 ? RIGHT : 0) |
  1176. (dir.y<0 ? UP : 0) |
  1177. (dir.y>0 ? DOWN : 0) ;
  1178. break;
  1179. }
  1180. if(!h || key.state != SDL_PRESSED)
  1181. break;
  1182. if(k == 4)
  1183. {
  1184. centerOn(h);
  1185. return;
  1186. }
  1187. CGPath &path = LOCPLINT->paths[h];
  1188. terrain.currentPath = &path;
  1189. int3 dst = h->getPosition(false) + dir;
  1190. if(dst != verifyPos(dst) || !LOCPLINT->cb->getPathsInfo(h)->getPath(path, dst))
  1191. {
  1192. terrain.currentPath = nullptr;
  1193. return;
  1194. }
  1195. if (path.nodes.size() > 2)
  1196. updateMoveHero(h);
  1197. else
  1198. if(!path.nodes[0].turns)
  1199. LOCPLINT->moveHero(h, path);
  1200. }
  1201. return;
  1202. }
  1203. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1204. && LOCPLINT->ctrlPressed()
  1205. )
  1206. scrollingDir |= Dir;
  1207. else
  1208. scrollingDir &= ~Dir;
  1209. }
  1210. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  1211. {
  1212. if(down)
  1213. {
  1214. CRClickPopup::createAndPush(text);
  1215. }
  1216. }
  1217. int3 CAdvMapInt::verifyPos(int3 ver)
  1218. {
  1219. if (ver.x<0)
  1220. ver.x=0;
  1221. if (ver.y<0)
  1222. ver.y=0;
  1223. if (ver.z<0)
  1224. ver.z=0;
  1225. if (ver.x>=CGI->mh->sizes.x)
  1226. ver.x=CGI->mh->sizes.x-1;
  1227. if (ver.y>=CGI->mh->sizes.y)
  1228. ver.y=CGI->mh->sizes.y-1;
  1229. if (ver.z>=CGI->mh->sizes.z)
  1230. ver.z=CGI->mh->sizes.z-1;
  1231. return ver;
  1232. }
  1233. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView)
  1234. {
  1235. assert(sel);
  1236. LOCPLINT->setSelection(sel);
  1237. selection = sel;
  1238. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  1239. {
  1240. auto pos = sel->visitablePos();
  1241. auto tile = LOCPLINT->cb->getTile(pos);
  1242. if(tile)
  1243. CCS->musich->playMusicFromSet("terrain", tile->terType, true);
  1244. }
  1245. if(centerView)
  1246. centerOn(sel);
  1247. terrain.currentPath = nullptr;
  1248. if(sel->ID==Obj::TOWN)
  1249. {
  1250. auto town = dynamic_cast<const CGTownInstance*>(sel);
  1251. infoBar.showTownSelection(town);
  1252. townList.select(town);
  1253. heroList.select(nullptr);
  1254. updateSleepWake(nullptr);
  1255. updateMoveHero(nullptr);
  1256. updateSpellbook(nullptr);
  1257. }
  1258. else //hero selected
  1259. {
  1260. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  1261. infoBar.showHeroSelection(hero);
  1262. heroList.select(hero);
  1263. townList.select(nullptr);
  1264. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  1265. updateSleepWake(hero);
  1266. updateMoveHero(hero);
  1267. updateSpellbook(hero);
  1268. }
  1269. townList.redraw();
  1270. heroList.redraw();
  1271. }
  1272. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1273. {
  1274. #ifdef VCMI_ANDROID
  1275. // todo ios
  1276. if(swipeEnabled)
  1277. return;
  1278. #endif
  1279. // adventure map scrolling with mouse
  1280. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  1281. // don't scroll if there is no window in focus - these events don't seem to correspond to the actual mouse movement
  1282. if(!isCtrlKeyDown() && isActive() && sEvent.windowID != 0 && mode == EAdvMapMode::NORMAL)
  1283. {
  1284. if(sEvent.x<15)
  1285. {
  1286. scrollingDir |= LEFT;
  1287. }
  1288. else
  1289. {
  1290. scrollingDir &= ~LEFT;
  1291. }
  1292. if(sEvent.x>screen->w-15)
  1293. {
  1294. scrollingDir |= RIGHT;
  1295. }
  1296. else
  1297. {
  1298. scrollingDir &= ~RIGHT;
  1299. }
  1300. if(sEvent.y<15)
  1301. {
  1302. scrollingDir |= UP;
  1303. }
  1304. else
  1305. {
  1306. scrollingDir &= ~UP;
  1307. }
  1308. if(sEvent.y>screen->h-15)
  1309. {
  1310. scrollingDir |= DOWN;
  1311. }
  1312. else
  1313. {
  1314. scrollingDir &= ~DOWN;
  1315. }
  1316. }
  1317. }
  1318. bool CAdvMapInt::isActive()
  1319. {
  1320. return active & ~CIntObject::KEYBOARD;
  1321. }
  1322. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  1323. {
  1324. state = WAITING;
  1325. }
  1326. void CAdvMapInt::setPlayer(PlayerColor Player)
  1327. {
  1328. player = Player;
  1329. graphics->blueToPlayersAdv(bg,player);
  1330. panelMain->setPlayerColor(player);
  1331. panelWorldView->setPlayerColor(player);
  1332. panelWorldView->recolorIcons(player, player.getNum() * 19);
  1333. resdatabar.background->colorize(player);
  1334. }
  1335. void CAdvMapInt::startTurn()
  1336. {
  1337. state = INGAME;
  1338. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  1339. || settings["session"]["spectate"].Bool())
  1340. {
  1341. adjustActiveness(false);
  1342. minimap.setAIRadar(false);
  1343. }
  1344. }
  1345. void CAdvMapInt::endingTurn()
  1346. {
  1347. if(settings["session"]["spectate"].Bool())
  1348. return;
  1349. if(LOCPLINT->cingconsole->active)
  1350. LOCPLINT->cingconsole->deactivate();
  1351. LOCPLINT->makingTurn = false;
  1352. LOCPLINT->cb->endTurn();
  1353. CCS->soundh->ambientStopAllChannels();
  1354. }
  1355. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1356. {
  1357. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1358. if (bobjs.empty())
  1359. return nullptr;
  1360. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1361. /*
  1362. if (bobjs.back()->ID == Obj::HERO)
  1363. return bobjs.back();
  1364. else
  1365. return bobjs.front();*/
  1366. }
  1367. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1368. {
  1369. if(mode != EAdvMapMode::NORMAL)
  1370. return;
  1371. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1372. return;
  1373. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1374. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1375. int3 selPos = selection->getSightCenter();
  1376. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1377. {
  1378. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1379. switch(spellBeingCasted->id)
  1380. {
  1381. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1382. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1383. leaveCastingMode(true, mapPos);
  1384. break;
  1385. case SpellID::DIMENSION_DOOR:
  1386. if(!tile || tile->isClear(heroTile))
  1387. leaveCastingMode(true, mapPos);
  1388. break;
  1389. }
  1390. return;
  1391. }
  1392. //check if we can select this object
  1393. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1394. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1395. if(selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1396. {
  1397. assert(!terrain.currentPath); //path can be active only when hero is selected
  1398. if(selection == topBlocking) //selected town clicked
  1399. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1400. else if(canSelect)
  1401. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1402. return;
  1403. }
  1404. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1405. {
  1406. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  1407. if(currentHero == topBlocking) //clicked selected hero
  1408. {
  1409. LOCPLINT->openHeroWindow(currentHero);
  1410. return;
  1411. }
  1412. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1413. {
  1414. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1415. return;
  1416. }
  1417. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1418. {
  1419. if(terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1420. {
  1421. if(CGI->mh->canStartHeroMovement())
  1422. LOCPLINT->moveHero(currentHero, *terrain.currentPath);
  1423. return;
  1424. }
  1425. else //remove old path and find a new one if we clicked on accessible tile
  1426. {
  1427. CGPath &path = LOCPLINT->paths[currentHero];
  1428. CGPath newpath;
  1429. bool gotPath = LOCPLINT->cb->getPathsInfo(currentHero)->getPath(newpath, mapPos); //try getting path, erase if failed
  1430. if(gotPath && newpath.nodes.size())
  1431. path = newpath;
  1432. if(path.nodes.size())
  1433. terrain.currentPath = &path;
  1434. else
  1435. LOCPLINT->eraseCurrentPathOf(currentHero);
  1436. updateMoveHero(currentHero);
  1437. }
  1438. }
  1439. } //end of hero is selected "case"
  1440. else
  1441. {
  1442. throw std::runtime_error("Nothing is selected...");
  1443. }
  1444. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1445. {
  1446. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1447. }
  1448. }
  1449. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1450. {
  1451. if(mode != EAdvMapMode::NORMAL //disable in world view
  1452. || !selection) //may occur just at the start of game (fake move before full intiialization)
  1453. return;
  1454. if(!LOCPLINT->cb->isVisible(mapPos))
  1455. {
  1456. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1457. statusbar->clear();
  1458. return;
  1459. }
  1460. auto objRelations = PlayerRelations::ALLIES;
  1461. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1462. if(objAtTile)
  1463. {
  1464. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  1465. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1466. boost::replace_all(text,"\n"," ");
  1467. statusbar->setText(text);
  1468. }
  1469. else
  1470. {
  1471. std::string hlp;
  1472. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1473. statusbar->setText(hlp);
  1474. }
  1475. if(spellBeingCasted)
  1476. {
  1477. switch(spellBeingCasted->id)
  1478. {
  1479. case SpellID::SCUTTLE_BOAT:
  1480. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1481. CCS->curh->changeGraphic(ECursor::ADVENTURE, 42);
  1482. else
  1483. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1484. return;
  1485. case SpellID::DIMENSION_DOOR:
  1486. {
  1487. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  1488. int3 hpos = selection->getSightCenter();
  1489. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1490. CCS->curh->changeGraphic(ECursor::ADVENTURE, 41);
  1491. else
  1492. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1493. return;
  1494. }
  1495. }
  1496. }
  1497. if(selection->ID == Obj::TOWN)
  1498. {
  1499. if(objAtTile)
  1500. {
  1501. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  1502. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1503. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1504. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1505. else
  1506. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1507. }
  1508. else
  1509. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1510. }
  1511. else if(const CGHeroInstance * h = curHero())
  1512. {
  1513. int3 mapPosCopy = mapPos;
  1514. const CGPathNode * pnode = LOCPLINT->cb->getPathsInfo(h)->getPathInfo(mapPosCopy);
  1515. assert(pnode);
  1516. int turns = pnode->turns;
  1517. vstd::amin(turns, 3);
  1518. switch(pnode->action)
  1519. {
  1520. case CGPathNode::NORMAL:
  1521. case CGPathNode::TELEPORT_NORMAL:
  1522. if(pnode->layer == EPathfindingLayer::LAND)
  1523. CCS->curh->changeGraphic(ECursor::ADVENTURE, 4 + turns*6);
  1524. else
  1525. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1526. break;
  1527. case CGPathNode::VISIT:
  1528. case CGPathNode::BLOCKING_VISIT:
  1529. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  1530. if(objAtTile && objAtTile->ID == Obj::HERO)
  1531. {
  1532. if(selection == objAtTile)
  1533. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1534. else
  1535. CCS->curh->changeGraphic(ECursor::ADVENTURE, 8 + turns*6);
  1536. }
  1537. else if(pnode->layer == EPathfindingLayer::LAND)
  1538. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1539. else
  1540. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1541. break;
  1542. case CGPathNode::BATTLE:
  1543. case CGPathNode::TELEPORT_BATTLE:
  1544. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1545. break;
  1546. case CGPathNode::EMBARK:
  1547. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1548. break;
  1549. case CGPathNode::DISEMBARK:
  1550. CCS->curh->changeGraphic(ECursor::ADVENTURE, 7 + turns*6);
  1551. break;
  1552. default:
  1553. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1554. {
  1555. if(objAtTile->ID == Obj::TOWN)
  1556. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1557. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1558. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1559. else
  1560. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1561. }
  1562. else
  1563. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1564. break;
  1565. }
  1566. }
  1567. if(ourInaccessibleShipyard(objAtTile))
  1568. {
  1569. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6);
  1570. }
  1571. }
  1572. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1573. {
  1574. if(mode != EAdvMapMode::NORMAL)
  1575. return;
  1576. if(spellBeingCasted)
  1577. {
  1578. leaveCastingMode();
  1579. return;
  1580. }
  1581. if(!LOCPLINT->cb->isVisible(mapPos))
  1582. {
  1583. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1584. return;
  1585. }
  1586. const CGObjectInstance * obj = getActiveObject(mapPos);
  1587. if(!obj)
  1588. {
  1589. // Bare or undiscovered terrain
  1590. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1591. if (tile)
  1592. {
  1593. std::string hlp;
  1594. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1595. CRClickPopup::createAndPush(hlp);
  1596. }
  1597. return;
  1598. }
  1599. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1600. }
  1601. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1602. {
  1603. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1604. spellBeingCasted = sp;
  1605. deactivate();
  1606. terrain.activate();
  1607. GH.fakeMouseMove();
  1608. }
  1609. void CAdvMapInt::leaveCastingMode(bool cast, int3 dest)
  1610. {
  1611. assert(spellBeingCasted);
  1612. SpellID id = spellBeingCasted->id;
  1613. spellBeingCasted = nullptr;
  1614. terrain.deactivate();
  1615. activate();
  1616. if(cast)
  1617. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1618. else
  1619. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1620. }
  1621. const CGHeroInstance * CAdvMapInt::curHero() const
  1622. {
  1623. if(selection && selection->ID == Obj::HERO)
  1624. return static_cast<const CGHeroInstance *>(selection);
  1625. else
  1626. return nullptr;
  1627. }
  1628. const CGTownInstance * CAdvMapInt::curTown() const
  1629. {
  1630. if(selection && selection->ID == Obj::TOWN)
  1631. return static_cast<const CGTownInstance *>(selection);
  1632. else
  1633. return nullptr;
  1634. }
  1635. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1636. {
  1637. const IShipyard *ret = IShipyard::castFrom(obj);
  1638. if(!ret || obj->tempOwner != player || CCS->curh->type || (CCS->curh->frame != 6 && CCS->curh->frame != 0))
  1639. return nullptr;
  1640. return ret;
  1641. }
  1642. void CAdvMapInt::aiTurnStarted()
  1643. {
  1644. if(settings["session"]["spectate"].Bool())
  1645. return;
  1646. adjustActiveness(true);
  1647. CCS->musich->playMusicFromSet("enemy-turn", true);
  1648. adventureInt->minimap.setAIRadar(true);
  1649. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1650. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1651. }
  1652. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1653. {
  1654. bool wasActive = isActive();
  1655. if(wasActive)
  1656. deactivate();
  1657. adventureInt->duringAITurn = aiTurnStart;
  1658. if(wasActive)
  1659. activate();
  1660. }
  1661. void CAdvMapInt::quickCombatLock()
  1662. {
  1663. if(!duringAITurn)
  1664. deactivate();
  1665. }
  1666. void CAdvMapInt::quickCombatUnlock()
  1667. {
  1668. if(!duringAITurn)
  1669. activate();
  1670. }
  1671. void CAdvMapInt::changeMode(EAdvMapMode newMode, float newScale)
  1672. {
  1673. if (mode != newMode)
  1674. {
  1675. mode = newMode;
  1676. switch (mode)
  1677. {
  1678. case EAdvMapMode::NORMAL:
  1679. panelMain->activate();
  1680. panelWorldView->deactivate();
  1681. activeMapPanel = panelMain;
  1682. townList.activate();
  1683. heroList.activate();
  1684. infoBar.activate();
  1685. worldViewOptions.clear();
  1686. break;
  1687. case EAdvMapMode::WORLD_VIEW:
  1688. panelMain->deactivate();
  1689. panelWorldView->activate();
  1690. activeMapPanel = panelWorldView;
  1691. townList.deactivate();
  1692. heroList.deactivate();
  1693. infoBar.showSelection(); // to prevent new day animation interfering world view mode
  1694. infoBar.deactivate();
  1695. break;
  1696. }
  1697. worldViewScale = newScale;
  1698. redraw();
  1699. }
  1700. else if (worldViewScale != newScale) // still in world view mode, but the scale changed
  1701. {
  1702. worldViewScale = newScale;
  1703. redraw();
  1704. }
  1705. }
  1706. CAdventureOptions::CAdventureOptions()
  1707. : CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1708. {
  1709. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1710. viewWorld = std::make_shared<CButton>(Point(24, 23), "ADVVIEW.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_v);
  1711. viewWorld->addCallback(std::bind(&CPlayerInterface::viewWorldMap, LOCPLINT));
  1712. exit = std::make_shared<CButton>(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), std::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1713. exit->assignedKeys.insert(SDLK_ESCAPE);
  1714. scenInfo = std::make_shared<CButton>(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_i);
  1715. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1716. puzzle = std::make_shared<CButton>(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_p);
  1717. puzzle->addCallback(std::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1718. dig = std::make_shared<CButton>(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_d);
  1719. if(const CGHeroInstance *h = adventureInt->curHero())
  1720. dig->addCallback(std::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1721. else
  1722. dig->block(true);
  1723. }
  1724. void CAdventureOptions::showScenarioInfo()
  1725. {
  1726. if(LOCPLINT->cb->getStartInfo()->campState)
  1727. {
  1728. GH.pushIntT<CCampaignInfoScreen>();
  1729. }
  1730. else
  1731. {
  1732. GH.pushIntT<CScenarioInfoScreen>();
  1733. }
  1734. }
  1735. CAdvMapInt::WorldViewOptions::WorldViewOptions()
  1736. {
  1737. clear();
  1738. }
  1739. void CAdvMapInt::WorldViewOptions::clear()
  1740. {
  1741. showAllTerrain = false;
  1742. iconPositions.clear();
  1743. }
  1744. void CAdvMapInt::WorldViewOptions::adjustDrawingInfo(MapDrawingInfo& info)
  1745. {
  1746. info.showAllTerrain = showAllTerrain;
  1747. info.additionalIcons = &iconPositions;
  1748. }