CAdvmapInterface.cpp 54 KB

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