CAdvmapInterface.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. /*
  2. * CAdvmapInterface.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CAdvmapInterface.h"
  12. #include "CCastleInterface.h"
  13. #include "CHeroWindow.h"
  14. #include "CKingdomInterface.h"
  15. #include "CSpellWindow.h"
  16. #include "CTradeWindow.h"
  17. #include "GUIClasses.h"
  18. #include "InfoWindows.h"
  19. #include "../CBitmapHandler.h"
  20. #include "../CGameInfo.h"
  21. #include "../CMessage.h"
  22. #include "../CMusicHandler.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../mainmenu/CMainMenu.h"
  25. #include "../lobby/CSelectionBase.h"
  26. #include "../lobby/CCampaignInfoScreen.h"
  27. #include "../lobby/CSavingScreen.h"
  28. #include "../lobby/CScenarioInfoScreen.h"
  29. #include "../Graphics.h"
  30. #include "../mapHandler.h"
  31. #include "../gui/CAnimation.h"
  32. #include "../gui/CursorHandler.h"
  33. #include "../gui/CGuiHandler.h"
  34. #include "../gui/SDL_Extensions.h"
  35. #include "../widgets/MiscWidgets.h"
  36. #include "../../CCallback.h"
  37. #include "../../lib/CConfigHandler.h"
  38. #include "../../lib/CGameState.h"
  39. #include "../../lib/CGeneralTextHandler.h"
  40. #include "../../lib/CHeroHandler.h"
  41. #include "../../lib/CSoundBase.h"
  42. #include "../../lib/spells/CSpellHandler.h"
  43. #include "../../lib/CTownHandler.h"
  44. #include "../../lib/JsonNode.h"
  45. #include "../../lib/mapObjects/CGHeroInstance.h"
  46. #include "../../lib/mapping/CMap.h"
  47. #include "../../lib/UnlockGuard.h"
  48. #include "../../lib/VCMI_Lib.h"
  49. #include "../../lib/StartInfo.h"
  50. #include "../../lib/mapping/CMapInfo.h"
  51. #include "../../lib/TerrainHandler.h"
  52. #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->set(Cursor::Map::SCROLL_NORTHEAST);
  61. else if(direction & CAdvMapInt::DOWN)
  62. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  63. else
  64. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  65. }
  66. else if(direction & CAdvMapInt::LEFT)
  67. {
  68. if(direction & CAdvMapInt::UP)
  69. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  70. else if(direction & CAdvMapInt::DOWN)
  71. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  72. else
  73. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  74. }
  75. else if(direction & CAdvMapInt::UP)
  76. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  77. else if(direction & CAdvMapInt::DOWN)
  78. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  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. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  113. if(adventureInt->swipeEnabled)
  114. {
  115. if(handleSwipeStateChange((bool)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. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  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. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  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((bool)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. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  159. if(sEvent.state == 0 || GH.multifinger) // any "button" is enough on mobile
  160. #else
  161. if((sEvent.state & SDL_BUTTON_MMASK) == 0) // swipe only works with middle mouse on other platforms
  162. #endif
  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.getCursorPosition().x, GH.getCursorPosition().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->set(Cursor::Map::POINTER);
  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->set(Cursor::Map::POINTER);
  220. }
  221. //Hoverable::hover(on);
  222. }
  223. void CTerrainRect::showPath(const 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 < -1 + (int)currentPath->nodes.size(); ++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. Rect prevClip;
  290. CSDL_Ext::getClipRect(to, prevClip);
  291. CSDL_Ext::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. CSDL_Ext::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, r.topLeft());
  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. return whichTileIsIt(GH.getCursorPosition().x, GH.getCursorPosition().y);
  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((si32)(tilesw / adventureInt->worldViewScale), (si32)(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. CSDL_Ext::blitSurface(screen, fadeSurface, Point(0,0));
  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. pos.x += x;
  428. pos.y += y;
  429. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  430. background = std::make_shared<CPicture>(defname, 0, 0);
  431. background->colorize(LOCPLINT->playerID);
  432. pos.w = background->bg->w;
  433. pos.h = background->bg->h;
  434. txtpos.resize(8);
  435. for (int i = 0; i < 8 ; i++)
  436. {
  437. txtpos[i].first = pos.x + offx + resdist*i;
  438. txtpos[i].second = pos.y + offy;
  439. }
  440. txtpos[7].first = txtpos[6].first + datedist;
  441. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  442. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  443. addUsedEvents(RCLICK);
  444. }
  445. CResDataBar::CResDataBar()
  446. {
  447. pos.x += ADVOPT.resdatabarX;
  448. pos.y += ADVOPT.resdatabarY;
  449. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  450. background = std::make_shared<CPicture>(ADVOPT.resdatabarG, 0, 0);
  451. background->colorize(LOCPLINT->playerID);
  452. pos.w = background->bg->w;
  453. pos.h = background->bg->h;
  454. txtpos.resize(8);
  455. for (int i = 0; i < 8 ; i++)
  456. {
  457. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  458. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  459. }
  460. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  461. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  462. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  463. }
  464. CResDataBar::~CResDataBar() = default;
  465. void CResDataBar::draw(SDL_Surface * to)
  466. {
  467. //TODO: all this should be labels, but they require proper text update on change
  468. for (auto i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  469. {
  470. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  471. graphics->fonts[FONT_SMALL]->renderTextLeft(to, text, Colors::WHITE, Point(txtpos[i].first,txtpos[i].second));
  472. }
  473. std::vector<std::string> temp;
  474. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  475. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  476. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  477. graphics->fonts[FONT_SMALL]->renderTextLeft(to, processStr(datetext,temp), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second));
  478. }
  479. void CResDataBar::show(SDL_Surface * to)
  480. {
  481. }
  482. void CResDataBar::showAll(SDL_Surface * to)
  483. {
  484. CIntObject::showAll(to);
  485. draw(to);
  486. }
  487. CAdvMapInt::CAdvMapInt():
  488. mode(EAdvMapMode::NORMAL),
  489. worldViewScale(0.0f), //actual init later in changeMode
  490. minimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH)),
  491. statusbar(CGStatusBar::create(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG)),
  492. heroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD),
  493. townList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD),
  494. infoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192)), state(NA),
  495. spellBeingCasted(nullptr), position(int3(0, 0, 0)), selection(nullptr),
  496. updateScreen(false), anim(0), animValHitCount(0), heroAnim(0), heroAnimValHitCount(0),
  497. activeMapPanel(nullptr), duringAITurn(false), scrollingDir(0), scrollingState(false),
  498. swipeEnabled(settings["general"]["swipe"].Bool()), swipeMovementRequested(false),
  499. swipeTargetPosition(int3(-1, -1, -1))
  500. {
  501. pos.x = pos.y = 0;
  502. pos.w = screen->w;
  503. pos.h = screen->h;
  504. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  505. townList.onSelect = std::bind(&CAdvMapInt::selectionChanged,this);
  506. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  507. if(!ADVOPT.worldViewGraphic.empty())
  508. {
  509. bgWorldView = BitmapHandler::loadBitmap(ADVOPT.worldViewGraphic);
  510. }
  511. else
  512. {
  513. bgWorldView = nullptr;
  514. logGlobal->warn("ADVOPT.worldViewGraphic is empty => bitmap not loaded");
  515. }
  516. if (!bgWorldView)
  517. {
  518. logGlobal->warn("bgWorldView not defined in resolution config; fallback to VWorld.bmp");
  519. bgWorldView = BitmapHandler::loadBitmap("VWorld.bmp");
  520. }
  521. worldViewIcons = std::make_shared<CAnimation>("VwSymbol");//todo: customize with ADVOPT
  522. worldViewIcons->preload();
  523. for(int g = 0; g < ADVOPT.gemG.size(); ++g)
  524. {
  525. gems.push_back(std::make_shared<CAnimImage>(ADVOPT.gemG[g], 0, 0, ADVOPT.gemX[g], ADVOPT.gemY[g]));
  526. }
  527. auto makeButton = [&](int textID, std::function<void()> callback, config::ButtonInfo info, int key) -> std::shared_ptr<CButton>
  528. {
  529. auto button = std::make_shared<CButton>(Point(info.x, info.y), info.defName, CGI->generaltexth->zelp[textID], callback, key, info.playerColoured);
  530. for(auto image : info.additionalDefs)
  531. button->addImage(image);
  532. return button;
  533. };
  534. kingOverview = makeButton(293, std::bind(&CAdvMapInt::fshowOverview,this), ADVOPT.kingOverview, SDLK_k);
  535. underground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), ADVOPT.underground, SDLK_u);
  536. questlog = makeButton(295, std::bind(&CAdvMapInt::fshowQuestlog,this), ADVOPT.questlog, SDLK_q);
  537. sleepWake = makeButton(296, std::bind(&CAdvMapInt::fsleepWake,this), ADVOPT.sleepWake, SDLK_w);
  538. moveHero = makeButton(297, std::bind(&CAdvMapInt::fmoveHero,this), ADVOPT.moveHero, SDLK_m);
  539. spellbook = makeButton(298, std::bind(&CAdvMapInt::fshowSpellbok,this), ADVOPT.spellbook, SDLK_c);
  540. advOptions = makeButton(299, std::bind(&CAdvMapInt::fadventureOPtions,this), ADVOPT.advOptions, SDLK_a);
  541. sysOptions = makeButton(300, std::bind(&CAdvMapInt::fsystemOptions,this), ADVOPT.sysOptions, SDLK_o);
  542. nextHero = makeButton(301, std::bind(&CAdvMapInt::fnextHero,this), ADVOPT.nextHero, SDLK_h);
  543. endTurn = makeButton(302, std::bind(&CAdvMapInt::fendTurn,this), ADVOPT.endTurn, SDLK_e);
  544. int panelSpaceBottom = screen->h - resdatabar.pos.h - 4;
  545. panelMain = std::make_shared<CAdvMapPanel>(nullptr, Point(0, 0));
  546. // TODO correct drawing position
  547. panelWorldView = std::make_shared<CAdvMapWorldViewPanel>(worldViewIcons, bgWorldView, Point(heroList.pos.x - 2, 195), panelSpaceBottom, LOCPLINT->playerID);
  548. panelMain->addChildColorableButton(kingOverview);
  549. panelMain->addChildColorableButton(underground);
  550. panelMain->addChildColorableButton(questlog);
  551. panelMain->addChildColorableButton(sleepWake);
  552. panelMain->addChildColorableButton(moveHero);
  553. panelMain->addChildColorableButton(spellbook);
  554. panelMain->addChildColorableButton(advOptions);
  555. panelMain->addChildColorableButton(sysOptions);
  556. panelMain->addChildColorableButton(nextHero);
  557. panelMain->addChildColorableButton(endTurn);
  558. // TODO move configs to resolutions.json, similarly to previous buttons
  559. config::ButtonInfo worldViewBackConfig = config::ButtonInfo();
  560. worldViewBackConfig.defName = "IOK6432.DEF";
  561. worldViewBackConfig.x = screen->w - 73;
  562. worldViewBackConfig.y = 343 + 195;
  563. worldViewBackConfig.playerColoured = false;
  564. panelWorldView->addChildToPanel(
  565. makeButton(288, std::bind(&CAdvMapInt::fworldViewBack,this), worldViewBackConfig, SDLK_ESCAPE), ACTIVATE | DEACTIVATE);
  566. config::ButtonInfo worldViewPuzzleConfig = config::ButtonInfo();
  567. worldViewPuzzleConfig.defName = "VWPUZ.DEF";
  568. worldViewPuzzleConfig.x = screen->w - 188;
  569. worldViewPuzzleConfig.y = 343 + 195;
  570. worldViewPuzzleConfig.playerColoured = false;
  571. panelWorldView->addChildToPanel( // no help text for this one
  572. std::make_shared<CButton>(Point(worldViewPuzzleConfig.x, worldViewPuzzleConfig.y), worldViewPuzzleConfig.defName, std::pair<std::string, std::string>(),
  573. std::bind(&CPlayerInterface::showPuzzleMap,LOCPLINT), SDLK_p, worldViewPuzzleConfig.playerColoured), ACTIVATE | DEACTIVATE);
  574. config::ButtonInfo worldViewScale1xConfig = config::ButtonInfo();
  575. worldViewScale1xConfig.defName = "VWMAG1.DEF";
  576. worldViewScale1xConfig.x = screen->w - 191;
  577. worldViewScale1xConfig.y = 23 + 195;
  578. worldViewScale1xConfig.playerColoured = false;
  579. panelWorldView->addChildToPanel( // help text is wrong for this button
  580. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale1x,this), worldViewScale1xConfig, SDLK_1), ACTIVATE | DEACTIVATE);
  581. config::ButtonInfo worldViewScale2xConfig = config::ButtonInfo();
  582. worldViewScale2xConfig.defName = "VWMAG2.DEF";
  583. worldViewScale2xConfig.x = screen->w - 191 + 63;
  584. worldViewScale2xConfig.y = 23 + 195;
  585. worldViewScale2xConfig.playerColoured = false;
  586. panelWorldView->addChildToPanel( // help text is wrong for this button
  587. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale2x,this), worldViewScale2xConfig, SDLK_2), ACTIVATE | DEACTIVATE);
  588. config::ButtonInfo worldViewScale4xConfig = config::ButtonInfo();
  589. worldViewScale4xConfig.defName = "VWMAG4.DEF";
  590. worldViewScale4xConfig.x = screen->w - 191 + 126;
  591. worldViewScale4xConfig.y = 23 + 195;
  592. worldViewScale4xConfig.playerColoured = false;
  593. panelWorldView->addChildToPanel( // help text is wrong for this button
  594. makeButton(291, std::bind(&CAdvMapInt::fworldViewScale4x,this), worldViewScale4xConfig, SDLK_4), ACTIVATE | DEACTIVATE);
  595. config::ButtonInfo worldViewUndergroundConfig = config::ButtonInfo();
  596. worldViewUndergroundConfig.defName = "IAM010.DEF";
  597. worldViewUndergroundConfig.additionalDefs.push_back("IAM003.DEF");
  598. worldViewUndergroundConfig.x = screen->w - 115;
  599. worldViewUndergroundConfig.y = 343 + 195;
  600. worldViewUndergroundConfig.playerColoured = true;
  601. worldViewUnderground = makeButton(294, std::bind(&CAdvMapInt::fswitchLevel,this), worldViewUndergroundConfig, SDLK_u);
  602. panelWorldView->addChildColorableButton(worldViewUnderground);
  603. setPlayer(LOCPLINT->playerID);
  604. int iconColorMultiplier = player.getNum() * 19;
  605. int wvLeft = heroList.pos.x - 2; // TODO correct drawing position
  606. //int wvTop = 195;
  607. for (int i = 0; i < 5; ++i)
  608. {
  609. panelWorldView->addChildIcon(std::pair<int, Point>(i, Point(5, 58 + i * 20)), iconColorMultiplier);
  610. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 263 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  611. Colors::WHITE, CGI->generaltexth->allTexts[612 + i]));
  612. }
  613. for (int i = 0; i < 7; ++i)
  614. {
  615. panelWorldView->addChildIcon(std::pair<int, Point>(i + 5, Point(5, 182 + i * 20)), iconColorMultiplier);
  616. panelWorldView->addChildIcon(std::pair<int, Point>(i + 12, Point(160, 182 + i * 20)), iconColorMultiplier);
  617. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 387 + i * 20, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  618. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  619. }
  620. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 5, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  621. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  622. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  623. Colors::WHITE, CGI->generaltexth->allTexts[618]));
  624. activeMapPanel = panelMain;
  625. changeMode(EAdvMapMode::NORMAL);
  626. underground->block(!CGI->mh->map->twoLevel);
  627. questlog->block(!CGI->mh->map->quests.size());
  628. worldViewUnderground->block(!CGI->mh->map->twoLevel);
  629. addUsedEvents(MOVE);
  630. }
  631. CAdvMapInt::~CAdvMapInt()
  632. {
  633. SDL_FreeSurface(bg);
  634. }
  635. void CAdvMapInt::fshowOverview()
  636. {
  637. GH.pushIntT<CKingdomInterface>();
  638. }
  639. void CAdvMapInt::fworldViewBack()
  640. {
  641. changeMode(EAdvMapMode::NORMAL);
  642. CGI->mh->discardWorldViewCache();
  643. auto hero = curHero();
  644. if (hero)
  645. centerOn(hero);
  646. }
  647. void CAdvMapInt::fworldViewScale1x()
  648. {
  649. // TODO set corresponding scale button to "selected" mode
  650. changeMode(EAdvMapMode::WORLD_VIEW, 0.22f);
  651. }
  652. void CAdvMapInt::fworldViewScale2x()
  653. {
  654. changeMode(EAdvMapMode::WORLD_VIEW, 0.36f);
  655. }
  656. void CAdvMapInt::fworldViewScale4x()
  657. {
  658. changeMode(EAdvMapMode::WORLD_VIEW, 0.5f);
  659. }
  660. void CAdvMapInt::fswitchLevel()
  661. {
  662. // with support for future multi-level maps :)
  663. int maxLevels = CGI->mh->map->levels();
  664. if (maxLevels < 2)
  665. return;
  666. position.z = (position.z + 1) % maxLevels;
  667. underground->setIndex(position.z, true);
  668. underground->redraw();
  669. worldViewUnderground->setIndex(position.z, true);
  670. worldViewUnderground->redraw();
  671. updateScreen = true;
  672. minimap.setLevel(position.z);
  673. if (mode == EAdvMapMode::WORLD_VIEW)
  674. terrain.redraw();
  675. }
  676. void CAdvMapInt::fshowQuestlog()
  677. {
  678. LOCPLINT->showQuestLog();
  679. }
  680. void CAdvMapInt::fsleepWake()
  681. {
  682. const CGHeroInstance *h = curHero();
  683. if (!h)
  684. return;
  685. bool newSleep = !isHeroSleeping(h);
  686. setHeroSleeping(h, newSleep);
  687. updateSleepWake(h);
  688. if (newSleep)
  689. {
  690. fnextHero();
  691. //moveHero.block(true);
  692. //uncomment to enable original HoMM3 behaviour:
  693. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  694. }
  695. }
  696. void CAdvMapInt::fmoveHero()
  697. {
  698. const CGHeroInstance *h = curHero();
  699. if (!h || !terrain.currentPath || !CGI->mh->canStartHeroMovement())
  700. return;
  701. LOCPLINT->moveHero(h, *terrain.currentPath);
  702. }
  703. void CAdvMapInt::fshowSpellbok()
  704. {
  705. if (!curHero()) //checking necessary values
  706. return;
  707. centerOn(selection);
  708. GH.pushIntT<CSpellWindow>(curHero(), LOCPLINT, false);
  709. }
  710. void CAdvMapInt::fadventureOPtions()
  711. {
  712. GH.pushIntT<CAdventureOptions>();
  713. }
  714. void CAdvMapInt::fsystemOptions()
  715. {
  716. GH.pushIntT<CSystemOptionsWindow>();
  717. }
  718. void CAdvMapInt::fnextHero()
  719. {
  720. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  721. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  722. if (next < 0)
  723. return;
  724. select(LOCPLINT->wanderingHeroes[next], true);
  725. }
  726. void CAdvMapInt::fendTurn()
  727. {
  728. if(!LOCPLINT->makingTurn)
  729. return;
  730. if(settings["adventure"]["heroReminder"].Bool())
  731. {
  732. for(auto hero : LOCPLINT->wanderingHeroes)
  733. {
  734. if(!isHeroSleeping(hero) && hero->movement > 0)
  735. {
  736. // Only show hero reminder if conditions met:
  737. // - There still movement points
  738. // - Hero don't have a path or there not points for first step on path
  739. auto path = LOCPLINT->getAndVerifyPath(hero);
  740. if(!path || path->nodes.size() < 2 || !path->nodes[path->nodes.size()-2].turns)
  741. {
  742. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], std::bind(&CAdvMapInt::endingTurn, this), nullptr);
  743. return;
  744. }
  745. }
  746. }
  747. }
  748. endingTurn();
  749. }
  750. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  751. {
  752. sleepWake->block(!h);
  753. if (!h)
  754. return;
  755. bool state = isHeroSleeping(h);
  756. sleepWake->setIndex(state ? 1 : 0, true);
  757. sleepWake->assignedKeys.clear();
  758. sleepWake->assignedKeys.insert(state ? SDLK_w : SDLK_z);
  759. }
  760. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  761. {
  762. if(!h)
  763. {
  764. moveHero->block(true);
  765. return;
  766. }
  767. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  768. if(boost::logic::indeterminate(hasPath))
  769. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  770. moveHero->block(!(bool)hasPath || (h->movement == 0));
  771. }
  772. void CAdvMapInt::updateSpellbook(const CGHeroInstance *h)
  773. {
  774. spellbook->block(!h);
  775. }
  776. int CAdvMapInt::getNextHeroIndex(int startIndex)
  777. {
  778. if (LOCPLINT->wanderingHeroes.size() == 0)
  779. return -1;
  780. if (startIndex < 0)
  781. startIndex = 0;
  782. int i = startIndex;
  783. do
  784. {
  785. i++;
  786. if (i >= LOCPLINT->wanderingHeroes.size())
  787. i = 0;
  788. }
  789. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  790. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  791. return i;
  792. else
  793. return -1;
  794. }
  795. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  796. {
  797. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  798. int next = getNextHeroIndex(start);
  799. if (next < 0)
  800. {
  801. nextHero->block(true);
  802. return;
  803. }
  804. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  805. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  806. nextHero->block(noActiveHeroes);
  807. }
  808. void CAdvMapInt::activate()
  809. {
  810. CIntObject::activate();
  811. if (!(active & KEYBOARD))
  812. CIntObject::activate(KEYBOARD);
  813. screenBuf = screen;
  814. GH.statusbar = statusbar;
  815. if(LOCPLINT)
  816. {
  817. LOCPLINT->cingconsole->activate();
  818. LOCPLINT->cingconsole->pos = this->pos;
  819. }
  820. if(!duringAITurn)
  821. {
  822. activeMapPanel->activate();
  823. if (mode == EAdvMapMode::NORMAL)
  824. {
  825. heroList.activate();
  826. townList.activate();
  827. infoBar.activate();
  828. }
  829. minimap.activate();
  830. terrain.activate();
  831. statusbar->activate();
  832. GH.fakeMouseMove(); //to restore the cursor
  833. }
  834. }
  835. void CAdvMapInt::deactivate()
  836. {
  837. CIntObject::deactivate();
  838. if(!duringAITurn)
  839. {
  840. scrollingDir = 0;
  841. CCS->curh->set(Cursor::Map::POINTER);
  842. activeMapPanel->deactivate();
  843. if (mode == EAdvMapMode::NORMAL)
  844. {
  845. heroList.deactivate();
  846. townList.deactivate();
  847. infoBar.deactivate();
  848. }
  849. minimap.deactivate();
  850. terrain.deactivate();
  851. statusbar->deactivate();
  852. }
  853. }
  854. void CAdvMapInt::showAll(SDL_Surface * to)
  855. {
  856. blitAt(bg,0,0,to);
  857. if(state != INGAME)
  858. return;
  859. switch (mode)
  860. {
  861. case EAdvMapMode::NORMAL:
  862. heroList.showAll(to);
  863. townList.showAll(to);
  864. infoBar.showAll(to);
  865. break;
  866. case EAdvMapMode::WORLD_VIEW:
  867. terrain.showAll(to);
  868. break;
  869. }
  870. activeMapPanel->showAll(to);
  871. updateScreen = true;
  872. minimap.showAll(to);
  873. show(to);
  874. resdatabar.showAll(to);
  875. statusbar->show(to);
  876. LOCPLINT->cingconsole->show(to);
  877. }
  878. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  879. {
  880. if (!hero)
  881. return false;
  882. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  883. }
  884. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  885. {
  886. if (sleep)
  887. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  888. else
  889. LOCPLINT->sleepingHeroes -= hero;
  890. updateNextHero(nullptr);
  891. }
  892. void CAdvMapInt::show(SDL_Surface * to)
  893. {
  894. if(state != INGAME)
  895. return;
  896. ++animValHitCount; //for animations
  897. if(animValHitCount % 2 == 0)
  898. {
  899. ++heroAnim;
  900. }
  901. if(animValHitCount >= 8)
  902. {
  903. CGI->mh->updateWater();
  904. animValHitCount = 0;
  905. ++anim;
  906. updateScreen = true;
  907. }
  908. if(swipeEnabled)
  909. {
  910. handleSwipeUpdate();
  911. }
  912. #if defined(VCMI_ANDROID) || defined(VCMI_IOS) // on mobile, map-moving mode is exclusive (TODO technically it might work with both enabled; to be checked)
  913. else
  914. #endif
  915. {
  916. handleMapScrollingUpdate();
  917. }
  918. for(int i = 0; i < 4; i++)
  919. {
  920. if(settings["session"]["spectate"].Bool())
  921. gems[i]->setFrame(PlayerColor(1).getNum());
  922. else
  923. gems[i]->setFrame(LOCPLINT->playerID.getNum());
  924. }
  925. if(updateScreen)
  926. {
  927. int3 betterPos = LOCPLINT->repairScreenPos(position);
  928. if (betterPos != position)
  929. {
  930. logGlobal->warn("Incorrect position for adventure map!");
  931. position = betterPos;
  932. }
  933. terrain.show(to);
  934. for(int i = 0; i < 4; i++)
  935. gems[i]->showAll(to);
  936. updateScreen=false;
  937. LOCPLINT->cingconsole->show(to);
  938. }
  939. else if (terrain.needsAnimUpdate())
  940. {
  941. terrain.showAnim(to);
  942. for(int i = 0; i < 4; i++)
  943. gems[i]->showAll(to);
  944. }
  945. infoBar.show(to);
  946. statusbar->showAll(to);
  947. }
  948. void CAdvMapInt::handleMapScrollingUpdate()
  949. {
  950. int scrollSpeed = static_cast<int>(settings["adventure"]["scrollSpeed"].Float());
  951. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  952. if((animValHitCount % (4 / scrollSpeed)) == 0
  953. && ((GH.topInt().get() == this) || isCtrlKeyDown()))
  954. {
  955. if((scrollingDir & LEFT) && (position.x > -CGI->mh->frameW))
  956. position.x--;
  957. if((scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW))
  958. position.x++;
  959. if((scrollingDir & UP) && (position.y > -CGI->mh->frameH))
  960. position.y--;
  961. if((scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH))
  962. position.y++;
  963. if(scrollingDir)
  964. {
  965. setScrollingCursor(scrollingDir);
  966. scrollingState = true;
  967. updateScreen = true;
  968. minimap.redraw();
  969. if(mode == EAdvMapMode::WORLD_VIEW)
  970. terrain.redraw();
  971. }
  972. else if(scrollingState)
  973. {
  974. CCS->curh->set(Cursor::Map::POINTER);
  975. scrollingState = false;
  976. }
  977. }
  978. }
  979. void CAdvMapInt::handleSwipeUpdate()
  980. {
  981. if(swipeMovementRequested)
  982. {
  983. auto fixedPos = LOCPLINT->repairScreenPos(swipeTargetPosition);
  984. position.x = fixedPos.x;
  985. position.y = fixedPos.y;
  986. CCS->curh->set(Cursor::Map::POINTER);
  987. updateScreen = true;
  988. minimap.redraw();
  989. swipeMovementRequested = false;
  990. }
  991. }
  992. void CAdvMapInt::selectionChanged()
  993. {
  994. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  995. if (selection != to)
  996. select(to);
  997. }
  998. void CAdvMapInt::centerOn(int3 on, bool fade)
  999. {
  1000. bool switchedLevels = on.z != position.z;
  1001. if (fade)
  1002. {
  1003. terrain.fadeFromCurrentView();
  1004. }
  1005. switch (mode)
  1006. {
  1007. default:
  1008. case EAdvMapMode::NORMAL:
  1009. on.x -= CGI->mh->frameW; // is this intentional? frame size doesn't really have to correspond to camera size...
  1010. on.y -= CGI->mh->frameH;
  1011. break;
  1012. case EAdvMapMode::WORLD_VIEW:
  1013. on.x -= static_cast<si32>(CGI->mh->tilesW / 2 / worldViewScale);
  1014. on.y -= static_cast<si32>(CGI->mh->tilesH / 2 / worldViewScale);
  1015. break;
  1016. }
  1017. on = LOCPLINT->repairScreenPos(on);
  1018. position = on;
  1019. updateScreen=true;
  1020. underground->setIndex(on.z,true); //change underground switch button image
  1021. underground->redraw();
  1022. worldViewUnderground->setIndex(on.z, true);
  1023. worldViewUnderground->redraw();
  1024. if (switchedLevels)
  1025. minimap.setLevel(position.z);
  1026. minimap.redraw();
  1027. if (mode == EAdvMapMode::WORLD_VIEW)
  1028. terrain.redraw();
  1029. }
  1030. void CAdvMapInt::centerOn(const CGObjectInstance * obj, bool fade)
  1031. {
  1032. centerOn(obj->getSightCenter(), fade);
  1033. }
  1034. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1035. {
  1036. if (mode == EAdvMapMode::WORLD_VIEW)
  1037. return;
  1038. ui8 Dir = 0;
  1039. SDL_Keycode k = key.keysym.sym;
  1040. const CGHeroInstance *h = curHero(); //selected hero
  1041. const CGTownInstance *t = curTown(); //selected town
  1042. switch(k)
  1043. {
  1044. case SDLK_g:
  1045. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1046. return;
  1047. {
  1048. //find first town with tavern
  1049. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  1050. {
  1051. return town->hasBuilt(BuildingID::TAVERN);
  1052. });
  1053. if(itr != LOCPLINT->towns.end())
  1054. LOCPLINT->showThievesGuildWindow(*itr);
  1055. else
  1056. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  1057. }
  1058. return;
  1059. case SDLK_i:
  1060. if(isActive())
  1061. CAdventureOptions::showScenarioInfo();
  1062. return;
  1063. case SDLK_l:
  1064. if(isActive())
  1065. LOCPLINT->proposeLoadingGame();
  1066. return;
  1067. case SDLK_s:
  1068. if(isActive() && key.type == SDL_KEYUP)
  1069. GH.pushIntT<CSavingScreen>();
  1070. return;
  1071. case SDLK_d:
  1072. {
  1073. if(h && isActive() && LOCPLINT->makingTurn && key.state == SDL_PRESSED)
  1074. LOCPLINT->tryDiggging(h);
  1075. return;
  1076. }
  1077. case SDLK_p:
  1078. if(isActive())
  1079. LOCPLINT->showPuzzleMap();
  1080. return;
  1081. case SDLK_v:
  1082. if(isActive())
  1083. LOCPLINT->viewWorldMap();
  1084. return;
  1085. case SDLK_r:
  1086. if(isActive() && LOCPLINT->ctrlPressed())
  1087. {
  1088. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  1089. [](){ LOCPLINT->sendCustomEvent(EUserEvent::RESTART_GAME); }, nullptr);
  1090. }
  1091. return;
  1092. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1093. {
  1094. if(!isActive())
  1095. return;
  1096. if(h && key.state == SDL_PRESSED)
  1097. {
  1098. auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
  1099. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  1100. //this thread leaves scope and tries to lock pim while holding gs,
  1101. //network thread tries to lock gs (appluy cl) while holding pim
  1102. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  1103. LOCPLINT->cb->moveHero(h,h->pos);
  1104. }
  1105. }
  1106. return;
  1107. case SDLK_RETURN:
  1108. {
  1109. if(!isActive() || !selection || key.state != SDL_PRESSED)
  1110. return;
  1111. if(h)
  1112. LOCPLINT->openHeroWindow(h);
  1113. else if(t)
  1114. LOCPLINT->openTownWindow(t);
  1115. return;
  1116. }
  1117. case SDLK_ESCAPE:
  1118. {
  1119. if(isActive() || GH.topInt().get() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  1120. return;
  1121. leaveCastingMode();
  1122. return;
  1123. }
  1124. case SDLK_t:
  1125. {
  1126. //act on key down if marketplace windows is not already opened
  1127. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1128. return;
  1129. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  1130. {
  1131. //check if we have any marketplace
  1132. const CGTownInstance *townWithMarket = nullptr;
  1133. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  1134. {
  1135. if(t->hasBuilt(BuildingID::MARKETPLACE))
  1136. {
  1137. townWithMarket = t;
  1138. break;
  1139. }
  1140. }
  1141. if(townWithMarket) //if any town has marketplace, open window
  1142. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  1143. else //if not - complain
  1144. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  1145. }
  1146. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  1147. {
  1148. townList.selectNext();
  1149. }
  1150. return;
  1151. }
  1152. default:
  1153. {
  1154. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1155. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1156. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1157. //numpad arrow
  1158. if(CGuiHandler::isArrowKey(k))
  1159. k = CGuiHandler::arrowToNum(k);
  1160. k -= SDLK_KP_1;
  1161. if(k < 0 || k > 8)
  1162. return;
  1163. if (!CGI->mh->canStartHeroMovement())
  1164. return;
  1165. int3 dir = directions[k];
  1166. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  1167. {
  1168. Dir = (dir.x<0 ? LEFT : 0) |
  1169. (dir.x>0 ? RIGHT : 0) |
  1170. (dir.y<0 ? UP : 0) |
  1171. (dir.y>0 ? DOWN : 0) ;
  1172. break;
  1173. }
  1174. if(!h || key.state != SDL_PRESSED)
  1175. break;
  1176. if(k == 4)
  1177. {
  1178. centerOn(h);
  1179. return;
  1180. }
  1181. CGPath &path = LOCPLINT->paths[h];
  1182. terrain.currentPath = &path;
  1183. int3 dst = h->visitablePos() + dir;
  1184. if(dst != verifyPos(dst) || !LOCPLINT->cb->getPathsInfo(h)->getPath(path, dst))
  1185. {
  1186. terrain.currentPath = nullptr;
  1187. return;
  1188. }
  1189. if (path.nodes.size() > 2)
  1190. updateMoveHero(h);
  1191. else
  1192. if(!path.nodes[0].turns)
  1193. LOCPLINT->moveHero(h, path);
  1194. }
  1195. return;
  1196. }
  1197. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1198. && LOCPLINT->ctrlPressed()
  1199. )
  1200. scrollingDir |= Dir;
  1201. else
  1202. scrollingDir &= ~Dir;
  1203. }
  1204. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  1205. {
  1206. if(down)
  1207. {
  1208. CRClickPopup::createAndPush(text);
  1209. }
  1210. }
  1211. int3 CAdvMapInt::verifyPos(int3 ver)
  1212. {
  1213. if (ver.x<0)
  1214. ver.x=0;
  1215. if (ver.y<0)
  1216. ver.y=0;
  1217. if (ver.z<0)
  1218. ver.z=0;
  1219. if (ver.x>=CGI->mh->sizes.x)
  1220. ver.x=CGI->mh->sizes.x-1;
  1221. if (ver.y>=CGI->mh->sizes.y)
  1222. ver.y=CGI->mh->sizes.y-1;
  1223. if (ver.z>=CGI->mh->sizes.z)
  1224. ver.z=CGI->mh->sizes.z-1;
  1225. return ver;
  1226. }
  1227. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView)
  1228. {
  1229. assert(sel);
  1230. LOCPLINT->setSelection(sel);
  1231. selection = sel;
  1232. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  1233. {
  1234. auto pos = sel->visitablePos();
  1235. auto tile = LOCPLINT->cb->getTile(pos);
  1236. if(tile)
  1237. CCS->musich->playMusicFromSet("terrain", tile->terType->getJsonKey(), true, false);
  1238. }
  1239. if(centerView)
  1240. centerOn(sel);
  1241. terrain.currentPath = nullptr;
  1242. if(sel->ID==Obj::TOWN)
  1243. {
  1244. auto town = dynamic_cast<const CGTownInstance*>(sel);
  1245. infoBar.showTownSelection(town);
  1246. townList.select(town);
  1247. heroList.select(nullptr);
  1248. updateSleepWake(nullptr);
  1249. updateMoveHero(nullptr);
  1250. updateSpellbook(nullptr);
  1251. }
  1252. else //hero selected
  1253. {
  1254. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  1255. infoBar.showHeroSelection(hero);
  1256. heroList.select(hero);
  1257. townList.select(nullptr);
  1258. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  1259. updateSleepWake(hero);
  1260. updateMoveHero(hero);
  1261. updateSpellbook(hero);
  1262. }
  1263. townList.redraw();
  1264. heroList.redraw();
  1265. }
  1266. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1267. {
  1268. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  1269. if(swipeEnabled)
  1270. return;
  1271. #endif
  1272. // adventure map scrolling with mouse
  1273. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  1274. // don't scroll if there is no window in focus - these events don't seem to correspond to the actual mouse movement
  1275. if(!isCtrlKeyDown() && isActive() && sEvent.windowID != 0 && mode == EAdvMapMode::NORMAL)
  1276. {
  1277. if(sEvent.x<15)
  1278. {
  1279. scrollingDir |= LEFT;
  1280. }
  1281. else
  1282. {
  1283. scrollingDir &= ~LEFT;
  1284. }
  1285. if(sEvent.x>screen->w-15)
  1286. {
  1287. scrollingDir |= RIGHT;
  1288. }
  1289. else
  1290. {
  1291. scrollingDir &= ~RIGHT;
  1292. }
  1293. if(sEvent.y<15)
  1294. {
  1295. scrollingDir |= UP;
  1296. }
  1297. else
  1298. {
  1299. scrollingDir &= ~UP;
  1300. }
  1301. if(sEvent.y>screen->h-15)
  1302. {
  1303. scrollingDir |= DOWN;
  1304. }
  1305. else
  1306. {
  1307. scrollingDir &= ~DOWN;
  1308. }
  1309. }
  1310. }
  1311. bool CAdvMapInt::isActive()
  1312. {
  1313. return active & ~CIntObject::KEYBOARD;
  1314. }
  1315. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  1316. {
  1317. state = WAITING;
  1318. }
  1319. void CAdvMapInt::setPlayer(PlayerColor Player)
  1320. {
  1321. player = Player;
  1322. graphics->blueToPlayersAdv(bg,player);
  1323. panelMain->setPlayerColor(player);
  1324. panelWorldView->setPlayerColor(player);
  1325. panelWorldView->recolorIcons(player, player.getNum() * 19);
  1326. resdatabar.background->colorize(player);
  1327. }
  1328. void CAdvMapInt::startTurn()
  1329. {
  1330. state = INGAME;
  1331. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  1332. || settings["session"]["spectate"].Bool())
  1333. {
  1334. adjustActiveness(false);
  1335. minimap.setAIRadar(false);
  1336. }
  1337. }
  1338. void CAdvMapInt::endingTurn()
  1339. {
  1340. if(settings["session"]["spectate"].Bool())
  1341. return;
  1342. LOCPLINT->makingTurn = false;
  1343. LOCPLINT->cb->endTurn();
  1344. CCS->soundh->ambientStopAllChannels();
  1345. }
  1346. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1347. {
  1348. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1349. if (bobjs.empty())
  1350. return nullptr;
  1351. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1352. /*
  1353. if (bobjs.back()->ID == Obj::HERO)
  1354. return bobjs.back();
  1355. else
  1356. return bobjs.front();*/
  1357. }
  1358. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1359. {
  1360. if(mode != EAdvMapMode::NORMAL)
  1361. return;
  1362. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1363. return;
  1364. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1365. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1366. int3 selPos = selection->getSightCenter();
  1367. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1368. {
  1369. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1370. switch(spellBeingCasted->id)
  1371. {
  1372. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1373. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1374. leaveCastingMode(true, mapPos);
  1375. break;
  1376. case SpellID::DIMENSION_DOOR:
  1377. if(!tile || tile->isClear(heroTile))
  1378. leaveCastingMode(true, mapPos);
  1379. break;
  1380. }
  1381. return;
  1382. }
  1383. //check if we can select this object
  1384. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1385. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1386. bool isHero = false;
  1387. if(selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1388. {
  1389. assert(!terrain.currentPath); //path can be active only when hero is selected
  1390. if(selection == topBlocking) //selected town clicked
  1391. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1392. else if(canSelect)
  1393. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1394. }
  1395. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1396. {
  1397. isHero = true;
  1398. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  1399. if(currentHero == topBlocking) //clicked selected hero
  1400. {
  1401. LOCPLINT->openHeroWindow(currentHero);
  1402. return;
  1403. }
  1404. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1405. {
  1406. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1407. return;
  1408. }
  1409. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1410. {
  1411. if(terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1412. {
  1413. if(CGI->mh->canStartHeroMovement())
  1414. LOCPLINT->moveHero(currentHero, *terrain.currentPath);
  1415. return;
  1416. }
  1417. else //remove old path and find a new one if we clicked on accessible tile
  1418. {
  1419. CGPath &path = LOCPLINT->paths[currentHero];
  1420. CGPath newpath;
  1421. bool gotPath = LOCPLINT->cb->getPathsInfo(currentHero)->getPath(newpath, mapPos); //try getting path, erase if failed
  1422. if(gotPath && newpath.nodes.size())
  1423. path = newpath;
  1424. if(path.nodes.size())
  1425. terrain.currentPath = &path;
  1426. else
  1427. LOCPLINT->eraseCurrentPathOf(currentHero);
  1428. updateMoveHero(currentHero);
  1429. }
  1430. }
  1431. } //end of hero is selected "case"
  1432. else
  1433. {
  1434. throw std::runtime_error("Nothing is selected...");
  1435. }
  1436. const auto shipyard = ourInaccessibleShipyard(topBlocking);
  1437. if(isHero && shipyard != nullptr)
  1438. {
  1439. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1440. }
  1441. }
  1442. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1443. {
  1444. if(mode != EAdvMapMode::NORMAL //disable in world view
  1445. || !selection) //may occur just at the start of game (fake move before full intiialization)
  1446. return;
  1447. if(!LOCPLINT->cb->isVisible(mapPos))
  1448. {
  1449. CCS->curh->set(Cursor::Map::POINTER);
  1450. statusbar->clear();
  1451. return;
  1452. }
  1453. auto objRelations = PlayerRelations::ALLIES;
  1454. const CGObjectInstance *objAtTile = getActiveObject(mapPos);
  1455. if(objAtTile)
  1456. {
  1457. objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
  1458. std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
  1459. boost::replace_all(text,"\n"," ");
  1460. statusbar->write(text);
  1461. }
  1462. else
  1463. {
  1464. std::string hlp;
  1465. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1466. statusbar->write(hlp);
  1467. }
  1468. if(spellBeingCasted)
  1469. {
  1470. switch(spellBeingCasted->id)
  1471. {
  1472. case SpellID::SCUTTLE_BOAT:
  1473. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1474. CCS->curh->set(Cursor::Map::SCUTTLE_BOAT);
  1475. else
  1476. CCS->curh->set(Cursor::Map::POINTER);
  1477. return;
  1478. case SpellID::DIMENSION_DOOR:
  1479. {
  1480. const TerrainTile * t = LOCPLINT->cb->getTile(mapPos, false);
  1481. int3 hpos = selection->getSightCenter();
  1482. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1483. CCS->curh->set(Cursor::Map::TELEPORT);
  1484. else
  1485. CCS->curh->set(Cursor::Map::POINTER);
  1486. return;
  1487. }
  1488. }
  1489. }
  1490. if(selection->ID == Obj::TOWN)
  1491. {
  1492. if(objAtTile)
  1493. {
  1494. if(objAtTile->ID == Obj::TOWN && objRelations != PlayerRelations::ENEMIES)
  1495. CCS->curh->set(Cursor::Map::TOWN);
  1496. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1497. CCS->curh->set(Cursor::Map::HERO);
  1498. else
  1499. CCS->curh->set(Cursor::Map::POINTER);
  1500. }
  1501. else
  1502. CCS->curh->set(Cursor::Map::POINTER);
  1503. }
  1504. else if(const CGHeroInstance * hero = curHero())
  1505. {
  1506. std::array<Cursor::Map, 4> cursorMove = { Cursor::Map::T1_MOVE, Cursor::Map::T2_MOVE, Cursor::Map::T3_MOVE, Cursor::Map::T4_MOVE, };
  1507. std::array<Cursor::Map, 4> cursorAttack = { Cursor::Map::T1_ATTACK, Cursor::Map::T2_ATTACK, Cursor::Map::T3_ATTACK, Cursor::Map::T4_ATTACK, };
  1508. std::array<Cursor::Map, 4> cursorSail = { Cursor::Map::T1_SAIL, Cursor::Map::T2_SAIL, Cursor::Map::T3_SAIL, Cursor::Map::T4_SAIL, };
  1509. std::array<Cursor::Map, 4> cursorDisembark = { Cursor::Map::T1_DISEMBARK, Cursor::Map::T2_DISEMBARK, Cursor::Map::T3_DISEMBARK, Cursor::Map::T4_DISEMBARK, };
  1510. std::array<Cursor::Map, 4> cursorExchange = { Cursor::Map::T1_EXCHANGE, Cursor::Map::T2_EXCHANGE, Cursor::Map::T3_EXCHANGE, Cursor::Map::T4_EXCHANGE, };
  1511. std::array<Cursor::Map, 4> cursorVisit = { Cursor::Map::T1_VISIT, Cursor::Map::T2_VISIT, Cursor::Map::T3_VISIT, Cursor::Map::T4_VISIT, };
  1512. std::array<Cursor::Map, 4> cursorSailVisit = { Cursor::Map::T1_SAIL_VISIT, Cursor::Map::T2_SAIL_VISIT, Cursor::Map::T3_SAIL_VISIT, Cursor::Map::T4_SAIL_VISIT, };
  1513. const CGPathNode * pathNode = LOCPLINT->cb->getPathsInfo(hero)->getPathInfo(mapPos);
  1514. assert(pathNode);
  1515. if(LOCPLINT->altPressed() && pathNode->reachable()) //overwrite status bar text with movement info
  1516. {
  1517. ShowMoveDetailsInStatusbar(*hero, *pathNode);
  1518. }
  1519. int turns = pathNode->turns;
  1520. vstd::amin(turns, 3);
  1521. switch(pathNode->action)
  1522. {
  1523. case CGPathNode::NORMAL:
  1524. case CGPathNode::TELEPORT_NORMAL:
  1525. if(pathNode->layer == EPathfindingLayer::LAND)
  1526. CCS->curh->set(cursorMove[turns]);
  1527. else
  1528. CCS->curh->set(cursorSailVisit[turns]);
  1529. break;
  1530. case CGPathNode::VISIT:
  1531. case CGPathNode::BLOCKING_VISIT:
  1532. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  1533. if(objAtTile && objAtTile->ID == Obj::HERO)
  1534. {
  1535. if(selection == objAtTile)
  1536. CCS->curh->set(Cursor::Map::HERO);
  1537. else
  1538. CCS->curh->set(cursorExchange[turns]);
  1539. }
  1540. else if(pathNode->layer == EPathfindingLayer::LAND)
  1541. CCS->curh->set(cursorVisit[turns]);
  1542. else
  1543. CCS->curh->set(cursorSailVisit[turns]);
  1544. break;
  1545. case CGPathNode::BATTLE:
  1546. case CGPathNode::TELEPORT_BATTLE:
  1547. CCS->curh->set(cursorAttack[turns]);
  1548. break;
  1549. case CGPathNode::EMBARK:
  1550. CCS->curh->set(cursorSail[turns]);
  1551. break;
  1552. case CGPathNode::DISEMBARK:
  1553. CCS->curh->set(cursorDisembark[turns]);
  1554. break;
  1555. default:
  1556. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1557. {
  1558. if(objAtTile->ID == Obj::TOWN)
  1559. CCS->curh->set(Cursor::Map::TOWN);
  1560. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1561. CCS->curh->set(Cursor::Map::HERO);
  1562. else
  1563. CCS->curh->set(Cursor::Map::POINTER);
  1564. }
  1565. else
  1566. CCS->curh->set(Cursor::Map::POINTER);
  1567. break;
  1568. }
  1569. }
  1570. if(ourInaccessibleShipyard(objAtTile))
  1571. {
  1572. CCS->curh->set(Cursor::Map::T1_SAIL);
  1573. }
  1574. }
  1575. void CAdvMapInt::ShowMoveDetailsInStatusbar(const CGHeroInstance & hero, const CGPathNode & pathNode)
  1576. {
  1577. const int maxMovementPointsAtStartOfLastTurn = pathNode.turns > 0 ? hero.maxMovePoints(pathNode.layer == EPathfindingLayer::LAND) : hero.movement;
  1578. const int movementPointsLastTurnCost = maxMovementPointsAtStartOfLastTurn - pathNode.moveRemains;
  1579. const int remainingPointsAfterMove = pathNode.turns == 0 ? pathNode.moveRemains : 0;
  1580. std::string result = VLC->generaltexth->translate("vcmi.adventureMap", pathNode.turns > 0 ? "moveCostDetails" : "moveCostDetailsNoTurns");
  1581. boost::replace_first(result, "%TURNS", std::to_string(pathNode.turns));
  1582. boost::replace_first(result, "%POINTS", std::to_string(movementPointsLastTurnCost));
  1583. boost::replace_first(result, "%REMAINING", std::to_string(remainingPointsAfterMove));
  1584. statusbar->write(result);
  1585. }
  1586. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1587. {
  1588. if(mode != EAdvMapMode::NORMAL)
  1589. return;
  1590. if(spellBeingCasted)
  1591. {
  1592. leaveCastingMode();
  1593. return;
  1594. }
  1595. if(!LOCPLINT->cb->isVisible(mapPos))
  1596. {
  1597. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1598. return;
  1599. }
  1600. const CGObjectInstance * obj = getActiveObject(mapPos);
  1601. if(!obj)
  1602. {
  1603. // Bare or undiscovered terrain
  1604. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1605. if (tile)
  1606. {
  1607. std::string hlp;
  1608. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1609. CRClickPopup::createAndPush(hlp);
  1610. }
  1611. return;
  1612. }
  1613. CRClickPopup::createAndPush(obj, GH.getCursorPosition(), ETextAlignment::CENTER);
  1614. }
  1615. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1616. {
  1617. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1618. spellBeingCasted = sp;
  1619. deactivate();
  1620. terrain.activate();
  1621. GH.fakeMouseMove();
  1622. }
  1623. void CAdvMapInt::leaveCastingMode(bool cast, int3 dest)
  1624. {
  1625. assert(spellBeingCasted);
  1626. SpellID id = spellBeingCasted->id;
  1627. spellBeingCasted = nullptr;
  1628. terrain.deactivate();
  1629. activate();
  1630. if(cast)
  1631. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1632. else
  1633. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1634. }
  1635. const CGHeroInstance * CAdvMapInt::curHero() const
  1636. {
  1637. if(selection && selection->ID == Obj::HERO)
  1638. return static_cast<const CGHeroInstance *>(selection);
  1639. else
  1640. return nullptr;
  1641. }
  1642. const CGTownInstance * CAdvMapInt::curTown() const
  1643. {
  1644. if(selection && selection->ID == Obj::TOWN)
  1645. return static_cast<const CGTownInstance *>(selection);
  1646. else
  1647. return nullptr;
  1648. }
  1649. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1650. {
  1651. const IShipyard *ret = IShipyard::castFrom(obj);
  1652. if(!ret ||
  1653. obj->tempOwner != player ||
  1654. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  1655. return nullptr;
  1656. return ret;
  1657. }
  1658. void CAdvMapInt::aiTurnStarted()
  1659. {
  1660. if(settings["session"]["spectate"].Bool())
  1661. return;
  1662. adjustActiveness(true);
  1663. CCS->musich->playMusicFromSet("enemy-turn", true, false);
  1664. adventureInt->minimap.setAIRadar(true);
  1665. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1666. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1667. }
  1668. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1669. {
  1670. bool wasActive = isActive();
  1671. if(wasActive)
  1672. deactivate();
  1673. adventureInt->duringAITurn = aiTurnStart;
  1674. if(wasActive)
  1675. activate();
  1676. }
  1677. void CAdvMapInt::quickCombatLock()
  1678. {
  1679. if(!duringAITurn)
  1680. deactivate();
  1681. }
  1682. void CAdvMapInt::quickCombatUnlock()
  1683. {
  1684. if(!duringAITurn)
  1685. activate();
  1686. }
  1687. void CAdvMapInt::changeMode(EAdvMapMode newMode, float newScale)
  1688. {
  1689. if (mode != newMode)
  1690. {
  1691. mode = newMode;
  1692. switch (mode)
  1693. {
  1694. case EAdvMapMode::NORMAL:
  1695. panelMain->activate();
  1696. panelWorldView->deactivate();
  1697. activeMapPanel = panelMain;
  1698. townList.activate();
  1699. heroList.activate();
  1700. infoBar.activate();
  1701. worldViewOptions.clear();
  1702. break;
  1703. case EAdvMapMode::WORLD_VIEW:
  1704. panelMain->deactivate();
  1705. panelWorldView->activate();
  1706. activeMapPanel = panelWorldView;
  1707. townList.deactivate();
  1708. heroList.deactivate();
  1709. infoBar.showSelection(); // to prevent new day animation interfering world view mode
  1710. infoBar.deactivate();
  1711. break;
  1712. }
  1713. worldViewScale = newScale;
  1714. redraw();
  1715. }
  1716. else if (worldViewScale != newScale) // still in world view mode, but the scale changed
  1717. {
  1718. worldViewScale = newScale;
  1719. redraw();
  1720. }
  1721. }
  1722. CAdventureOptions::CAdventureOptions()
  1723. : CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1724. {
  1725. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1726. viewWorld = std::make_shared<CButton>(Point(24, 23), "ADVVIEW.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_v);
  1727. viewWorld->addCallback(std::bind(&CPlayerInterface::viewWorldMap, LOCPLINT));
  1728. exit = std::make_shared<CButton>(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), std::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1729. exit->assignedKeys.insert(SDLK_ESCAPE);
  1730. scenInfo = std::make_shared<CButton>(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_i);
  1731. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1732. puzzle = std::make_shared<CButton>(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_p);
  1733. puzzle->addCallback(std::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1734. dig = std::make_shared<CButton>(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_d);
  1735. if(const CGHeroInstance *h = adventureInt->curHero())
  1736. dig->addCallback(std::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1737. else
  1738. dig->block(true);
  1739. }
  1740. void CAdventureOptions::showScenarioInfo()
  1741. {
  1742. if(LOCPLINT->cb->getStartInfo()->campState)
  1743. {
  1744. GH.pushIntT<CCampaignInfoScreen>();
  1745. }
  1746. else
  1747. {
  1748. GH.pushIntT<CScenarioInfoScreen>();
  1749. }
  1750. }
  1751. CAdvMapInt::WorldViewOptions::WorldViewOptions()
  1752. {
  1753. clear();
  1754. }
  1755. void CAdvMapInt::WorldViewOptions::clear()
  1756. {
  1757. showAllTerrain = false;
  1758. iconPositions.clear();
  1759. }
  1760. void CAdvMapInt::WorldViewOptions::adjustDrawingInfo(MapDrawingInfo& info)
  1761. {
  1762. info.showAllTerrain = showAllTerrain;
  1763. info.additionalIcons = &iconPositions;
  1764. }