CAdvmapInterface.cpp 54 KB

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