CAdvmapInterface.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  1. /*
  2. * CAdvmapInterface.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CAdvmapInterface.h"
  12. #include "CCastleInterface.h"
  13. #include "CHeroWindow.h"
  14. #include "CKingdomInterface.h"
  15. #include "CSpellWindow.h"
  16. #include "CTradeWindow.h"
  17. #include "GUIClasses.h"
  18. #include "InfoWindows.h"
  19. #include "../CBitmapHandler.h"
  20. #include "../CGameInfo.h"
  21. #include "../CMessage.h"
  22. #include "../CMusicHandler.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../mainmenu/CMainMenu.h"
  25. #include "../lobby/CSelectionBase.h"
  26. #include "../lobby/CCampaignInfoScreen.h"
  27. #include "../lobby/CSavingScreen.h"
  28. #include "../lobby/CScenarioInfoScreen.h"
  29. #include "../Graphics.h"
  30. #include "../mapHandler.h"
  31. #include "../gui/CAnimation.h"
  32. #include "../gui/CCursorHandler.h"
  33. #include "../gui/CGuiHandler.h"
  34. #include "../gui/SDL_Extensions.h"
  35. #include "../widgets/MiscWidgets.h"
  36. #include "../../CCallback.h"
  37. #include "../../lib/CConfigHandler.h"
  38. #include "../../lib/CGameState.h"
  39. #include "../../lib/CGeneralTextHandler.h"
  40. #include "../../lib/CHeroHandler.h"
  41. #include "../../lib/CSoundBase.h"
  42. #include "../../lib/spells/CSpellHandler.h"
  43. #include "../../lib/CTownHandler.h"
  44. #include "../../lib/JsonNode.h"
  45. #include "../../lib/mapObjects/CGHeroInstance.h"
  46. #include "../../lib/mapping/CMap.h"
  47. #include "../../lib/UnlockGuard.h"
  48. #include "../../lib/VCMI_Lib.h"
  49. #include "../../lib/StartInfo.h"
  50. #include "../../lib/mapping/CMapInfo.h"
  51. #define ADVOPT (conf.go()->ac)
  52. using namespace CSDL_Ext;
  53. std::shared_ptr<CAdvMapInt> adventureInt;
  54. static void setScrollingCursor(ui8 direction)
  55. {
  56. if(direction & CAdvMapInt::RIGHT)
  57. {
  58. if(direction & CAdvMapInt::UP)
  59. CCS->curh->set(Cursor::Map::SCROLL_NORTHEAST);
  60. else if(direction & CAdvMapInt::DOWN)
  61. CCS->curh->set(Cursor::Map::SCROLL_SOUTHEAST);
  62. else
  63. CCS->curh->set(Cursor::Map::SCROLL_EAST);
  64. }
  65. else if(direction & CAdvMapInt::LEFT)
  66. {
  67. if(direction & CAdvMapInt::UP)
  68. CCS->curh->set(Cursor::Map::SCROLL_NORTHWEST);
  69. else if(direction & CAdvMapInt::DOWN)
  70. CCS->curh->set(Cursor::Map::SCROLL_SOUTHWEST);
  71. else
  72. CCS->curh->set(Cursor::Map::SCROLL_WEST);
  73. }
  74. else if(direction & CAdvMapInt::UP)
  75. CCS->curh->set(Cursor::Map::SCROLL_NORTH);
  76. else if(direction & CAdvMapInt::DOWN)
  77. CCS->curh->set(Cursor::Map::SCROLL_SOUTH);
  78. }
  79. CTerrainRect::CTerrainRect()
  80. : fadeSurface(nullptr),
  81. lastRedrawStatus(EMapAnimRedrawStatus::OK),
  82. fadeAnim(std::make_shared<CFadeAnimation>()),
  83. curHoveredTile(-1,-1,-1),
  84. currentPath(nullptr)
  85. {
  86. tilesw=(ADVOPT.advmapW+31)/32;
  87. tilesh=(ADVOPT.advmapH+31)/32;
  88. pos.x=ADVOPT.advmapX;
  89. pos.y=ADVOPT.advmapY;
  90. pos.w=ADVOPT.advmapW;
  91. pos.h=ADVOPT.advmapH;
  92. moveX = moveY = 0;
  93. addUsedEvents(LCLICK | RCLICK | MCLICK | HOVER | MOVE);
  94. }
  95. CTerrainRect::~CTerrainRect()
  96. {
  97. if(fadeSurface)
  98. SDL_FreeSurface(fadeSurface);
  99. }
  100. void CTerrainRect::deactivate()
  101. {
  102. CIntObject::deactivate();
  103. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  104. }
  105. void CTerrainRect::clickLeft(tribool down, bool previousState)
  106. {
  107. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  108. return;
  109. if(indeterminate(down))
  110. return;
  111. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  112. if(adventureInt->swipeEnabled)
  113. {
  114. if(handleSwipeStateChange((bool)down == true))
  115. {
  116. return; // if swipe is enabled, we don't process "down" events and wait for "up" (to make sure this wasn't a swiping gesture)
  117. }
  118. }
  119. else
  120. {
  121. #endif
  122. if(down == false)
  123. return;
  124. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  125. }
  126. #endif
  127. int3 mp = whichTileIsIt();
  128. if(mp.x < 0 || mp.y < 0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  129. return;
  130. adventureInt->tileLClicked(mp);
  131. }
  132. void CTerrainRect::clickRight(tribool down, bool previousState)
  133. {
  134. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  135. if(adventureInt->swipeEnabled && isSwiping)
  136. return;
  137. #endif
  138. if(adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  139. return;
  140. int3 mp = whichTileIsIt();
  141. if(CGI->mh->map->isInTheMap(mp) && down)
  142. adventureInt->tileRClicked(mp);
  143. }
  144. void CTerrainRect::clickMiddle(tribool down, bool previousState)
  145. {
  146. handleSwipeStateChange((bool)down == true);
  147. }
  148. void CTerrainRect::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  149. {
  150. handleHover(sEvent);
  151. if(!adventureInt->swipeEnabled)
  152. return;
  153. handleSwipeMove(sEvent);
  154. }
  155. void CTerrainRect::handleSwipeMove(const SDL_MouseMotionEvent & sEvent)
  156. {
  157. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  158. if(sEvent.state == 0) // any "button" is enough on mobile
  159. #else
  160. if((sEvent.state & SDL_BUTTON_MMASK) == 0) // swipe only works with middle mouse on other platforms
  161. #endif
  162. {
  163. return;
  164. }
  165. if(!isSwiping)
  166. {
  167. // try to distinguish if this touch was meant to be a swipe or just fat-fingering press
  168. if(abs(sEvent.x - swipeInitialRealPos.x) > SwipeTouchSlop ||
  169. abs(sEvent.y - swipeInitialRealPos.y) > SwipeTouchSlop)
  170. {
  171. isSwiping = true;
  172. }
  173. }
  174. if(isSwiping)
  175. {
  176. adventureInt->swipeTargetPosition.x =
  177. swipeInitialMapPos.x + static_cast<si32>(swipeInitialRealPos.x - sEvent.x) / 32;
  178. adventureInt->swipeTargetPosition.y =
  179. swipeInitialMapPos.y + static_cast<si32>(swipeInitialRealPos.y - sEvent.y) / 32;
  180. adventureInt->swipeMovementRequested = true;
  181. }
  182. }
  183. bool CTerrainRect::handleSwipeStateChange(bool btnPressed)
  184. {
  185. if(btnPressed)
  186. {
  187. swipeInitialRealPos = int3(GH.current->motion.x, GH.current->motion.y, 0);
  188. swipeInitialMapPos = int3(adventureInt->position);
  189. return true;
  190. }
  191. else if(isSwiping) // only accept this touch if it wasn't a swipe
  192. {
  193. isSwiping = false;
  194. return true;
  195. }
  196. return false;
  197. }
  198. void CTerrainRect::handleHover(const SDL_MouseMotionEvent &sEvent)
  199. {
  200. int3 tHovered = whichTileIsIt(sEvent.x, sEvent.y);
  201. int3 pom = adventureInt->verifyPos(tHovered);
  202. if(tHovered != pom) //tile outside the map
  203. {
  204. CCS->curh->set(Cursor::Map::POINTER);
  205. return;
  206. }
  207. if (pom != curHoveredTile)
  208. {
  209. curHoveredTile = pom;
  210. adventureInt->tileHovered(pom);
  211. }
  212. }
  213. void CTerrainRect::hover(bool on)
  214. {
  215. if (!on)
  216. {
  217. adventureInt->statusbar->clear();
  218. CCS->curh->set(Cursor::Map::POINTER);
  219. }
  220. //Hoverable::hover(on);
  221. }
  222. void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
  223. {
  224. const static int pns[9][9] = {
  225. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  226. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  227. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  228. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  229. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  230. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  231. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  232. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  233. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  234. }; //table of magic values TODO meaning, change variable name
  235. for (int i = 0; i < -1 + (int)currentPath->nodes.size(); ++i)
  236. {
  237. const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
  238. if(curPos.z != adventureInt->position.z)
  239. continue;
  240. int pn=-1;//number of picture
  241. if (i==0) //last tile
  242. {
  243. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  244. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  245. if (x<0 || y<0 || x>pos.w || y>pos.h)
  246. continue;
  247. pn=0;
  248. }
  249. else
  250. {
  251. const int3 &prevPos = currentPath->nodes[i-1].coord;
  252. std::vector<CGPathNode> & cv = currentPath->nodes;
  253. /* Vector directions
  254. * 0 1 2
  255. * \ | /
  256. * 3 - 4 - 5
  257. * / | \
  258. * 6 7 8
  259. *For example:
  260. * |
  261. * |__\
  262. * /
  263. * is id1=7, id2=5 (pns[7][5])
  264. */
  265. bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
  266. if(pathContinuous && cv[i].action != CGPathNode::EMBARK && cv[i].action != CGPathNode::DISEMBARK)
  267. {
  268. int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
  269. int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
  270. pn=pns[id1][id2];
  271. }
  272. else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
  273. {
  274. pn = 0;
  275. }
  276. }
  277. if (currentPath->nodes[i].turns)
  278. pn+=25;
  279. if (pn>=0)
  280. {
  281. const auto arrow = graphics->heroMoveArrows->getImage(pn);
  282. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  283. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  284. if (x< -32 || y< -32 || x>pos.w || y>pos.h)
  285. continue;
  286. int hvx = (x + arrow->width()) - (pos.x + pos.w),
  287. hvy = (y + arrow->height()) - (pos.y + pos.h);
  288. SDL_Rect prevClip;
  289. SDL_GetClipRect(to, &prevClip);
  290. SDL_SetClipRect(to, extRect); //preventing blitting outside of that rect
  291. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  292. {
  293. if (hvx<0 && hvy<0)
  294. {
  295. arrow->draw(to, x + moveX, y + moveY);
  296. }
  297. else if(hvx<0)
  298. {
  299. Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  300. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  301. }
  302. else if (hvy<0)
  303. {
  304. Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  305. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  306. }
  307. else
  308. {
  309. Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  310. arrow->draw(to, x + moveX, y + moveY, &srcRect);
  311. }
  312. }
  313. else //standard version
  314. {
  315. if (hvx<0 && hvy<0)
  316. {
  317. arrow->draw(to, x, y);
  318. }
  319. else if(hvx<0)
  320. {
  321. Rect srcRect = genRect(arrow->height() - hvy, arrow->width(), 0, 0);
  322. arrow->draw(to, x, y, &srcRect);
  323. }
  324. else if (hvy<0)
  325. {
  326. Rect srcRect = genRect(arrow->height(), arrow->width() - hvx, 0, 0);
  327. arrow->draw(to, x, y, &srcRect);
  328. }
  329. else
  330. {
  331. Rect srcRect = genRect(arrow->height() - hvy, arrow->width() - hvx, 0, 0);
  332. arrow->draw(to, x, y, &srcRect);
  333. }
  334. }
  335. SDL_SetClipRect(to, &prevClip);
  336. }
  337. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  338. }
  339. void CTerrainRect::show(SDL_Surface * to)
  340. {
  341. if (adventureInt->mode == EAdvMapMode::NORMAL)
  342. {
  343. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), &pos);
  344. info.otherheroAnim = true;
  345. info.anim = adventureInt->anim;
  346. info.heroAnim = adventureInt->heroAnim;
  347. if (ADVOPT.smoothMove)
  348. info.movement = int3(moveX, moveY, 0);
  349. lastRedrawStatus = CGI->mh->drawTerrainRectNew(to, &info);
  350. if (fadeAnim->isFading())
  351. {
  352. Rect r(pos);
  353. fadeAnim->update();
  354. fadeAnim->draw(to, nullptr, &r);
  355. }
  356. if (currentPath/* && adventureInt->position.z==currentPath->startPos().z*/) //drawing path
  357. {
  358. showPath(&pos, to);
  359. }
  360. }
  361. }
  362. void CTerrainRect::showAll(SDL_Surface * to)
  363. {
  364. // world view map is static and doesn't need redraw every frame
  365. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  366. {
  367. MapDrawingInfo info(adventureInt->position, LOCPLINT->cb->getVisibilityMap(), &pos, adventureInt->worldViewIcons);
  368. info.scaled = true;
  369. info.scale = adventureInt->worldViewScale;
  370. adventureInt->worldViewOptions.adjustDrawingInfo(info);
  371. CGI->mh->drawTerrainRectNew(to, &info);
  372. }
  373. }
  374. void CTerrainRect::showAnim(SDL_Surface * to)
  375. {
  376. if (fadeAnim->isFading())
  377. show(to);
  378. else if (lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED)
  379. 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
  380. }
  381. int3 CTerrainRect::whichTileIsIt(const int x, const int y)
  382. {
  383. int3 ret;
  384. ret.x = adventureInt->position.x + ((x-CGI->mh->offsetX-pos.x)/32);
  385. ret.y = adventureInt->position.y + ((y-CGI->mh->offsetY-pos.y)/32);
  386. ret.z = adventureInt->position.z;
  387. return ret;
  388. }
  389. int3 CTerrainRect::whichTileIsIt()
  390. {
  391. if(GH.current)
  392. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  393. else
  394. return int3(-1);
  395. }
  396. int3 CTerrainRect::tileCountOnScreen()
  397. {
  398. switch (adventureInt->mode)
  399. {
  400. default:
  401. logGlobal->error("Unknown map mode %d", (int)adventureInt->mode);
  402. return int3();
  403. case EAdvMapMode::NORMAL:
  404. return int3(tilesw, tilesh, 1);
  405. case EAdvMapMode::WORLD_VIEW:
  406. return int3((si32)(tilesw / adventureInt->worldViewScale), (si32)(tilesh / adventureInt->worldViewScale), 1);
  407. }
  408. }
  409. void CTerrainRect::fadeFromCurrentView()
  410. {
  411. if (!ADVOPT.screenFading)
  412. return;
  413. if (adventureInt->mode == EAdvMapMode::WORLD_VIEW)
  414. return;
  415. if (!fadeSurface)
  416. fadeSurface = CSDL_Ext::newSurface(pos.w, pos.h);
  417. SDL_BlitSurface(screen, &pos, fadeSurface, nullptr);
  418. fadeAnim->init(CFadeAnimation::EMode::OUT, fadeSurface);
  419. }
  420. bool CTerrainRect::needsAnimUpdate()
  421. {
  422. return fadeAnim->isFading() || lastRedrawStatus == EMapAnimRedrawStatus::REDRAW_REQUESTED;
  423. }
  424. void CResDataBar::clickRight(tribool down, bool previousState)
  425. {
  426. }
  427. CResDataBar::CResDataBar(const std::string & defname, int x, int y, int offx, int offy, int resdist, int datedist)
  428. {
  429. pos.x += x;
  430. pos.y += y;
  431. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  432. background = std::make_shared<CPicture>(defname, 0, 0);
  433. background->colorize(LOCPLINT->playerID);
  434. pos.w = background->bg->w;
  435. pos.h = background->bg->h;
  436. txtpos.resize(8);
  437. for (int i = 0; i < 8 ; i++)
  438. {
  439. txtpos[i].first = pos.x + offx + resdist*i;
  440. txtpos[i].second = pos.y + offy;
  441. }
  442. txtpos[7].first = txtpos[6].first + datedist;
  443. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  444. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  445. addUsedEvents(RCLICK);
  446. }
  447. CResDataBar::CResDataBar()
  448. {
  449. pos.x += ADVOPT.resdatabarX;
  450. pos.y += ADVOPT.resdatabarY;
  451. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  452. background = std::make_shared<CPicture>(ADVOPT.resdatabarG, 0, 0);
  453. background->colorize(LOCPLINT->playerID);
  454. pos.w = background->bg->w;
  455. pos.h = background->bg->h;
  456. txtpos.resize(8);
  457. for (int i = 0; i < 8 ; i++)
  458. {
  459. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  460. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  461. }
  462. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  463. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  464. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  465. }
  466. CResDataBar::~CResDataBar() = default;
  467. void CResDataBar::draw(SDL_Surface * to)
  468. {
  469. //TODO: all this should be labels, but they require proper text update on change
  470. for (auto i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  471. {
  472. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  473. graphics->fonts[FONT_SMALL]->renderTextLeft(to, text, Colors::WHITE, Point(txtpos[i].first,txtpos[i].second));
  474. }
  475. std::vector<std::string> temp;
  476. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  477. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  478. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  479. graphics->fonts[FONT_SMALL]->renderTextLeft(to, processStr(datetext,temp), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second));
  480. }
  481. void CResDataBar::show(SDL_Surface * to)
  482. {
  483. }
  484. void CResDataBar::showAll(SDL_Surface * to)
  485. {
  486. CIntObject::showAll(to);
  487. draw(to);
  488. }
  489. CAdvMapInt::CAdvMapInt():
  490. mode(EAdvMapMode::NORMAL),
  491. worldViewScale(0.0f), //actual init later in changeMode
  492. minimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH)),
  493. statusbar(CGStatusBar::create(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG)),
  494. heroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD),
  495. townList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD),
  496. infoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192)), state(NA),
  497. spellBeingCasted(nullptr), position(int3(0, 0, 0)), selection(nullptr),
  498. updateScreen(false), anim(0), animValHitCount(0), heroAnim(0), heroAnimValHitCount(0),
  499. activeMapPanel(nullptr), duringAITurn(false), scrollingDir(0), scrollingState(false),
  500. swipeEnabled(settings["general"]["swipe"].Bool()), swipeMovementRequested(false),
  501. swipeTargetPosition(int3(-1, -1, -1))
  502. {
  503. pos.x = pos.y = 0;
  504. pos.w = screen->w;
  505. pos.h = screen->h;
  506. strongInterest = true; // handle all mouse move events to prevent dead mouse move space in fullscreen mode
  507. townList.onSelect = std::bind(&CAdvMapInt::selectionChanged,this);
  508. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  509. if(!ADVOPT.worldViewGraphic.empty())
  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, ETextAlignment::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, ETextAlignment::TOPLEFT,
  620. Colors::WHITE, CGI->generaltexth->allTexts[619 + i]));
  621. }
  622. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 5, 367, EFonts::FONT_SMALL, ETextAlignment::TOPLEFT,
  623. Colors::WHITE, CGI->generaltexth->allTexts[617]));
  624. panelWorldView->addChildToPanel(std::make_shared<CLabel>(wvLeft + 45, 367, EFonts::FONT_SMALL, ETextAlignment::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->levels();
  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(!(bool)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(LOCPLINT)
  818. {
  819. LOCPLINT->cingconsole->activate();
  820. LOCPLINT->cingconsole->pos = this->pos;
  821. }
  822. if(!duringAITurn)
  823. {
  824. activeMapPanel->activate();
  825. if (mode == EAdvMapMode::NORMAL)
  826. {
  827. heroList.activate();
  828. townList.activate();
  829. infoBar.activate();
  830. }
  831. minimap.activate();
  832. terrain.activate();
  833. statusbar->activate();
  834. GH.fakeMouseMove(); //to restore the cursor
  835. }
  836. }
  837. void CAdvMapInt::deactivate()
  838. {
  839. CIntObject::deactivate();
  840. if(!duringAITurn)
  841. {
  842. scrollingDir = 0;
  843. CCS->curh->set(Cursor::Map::POINTER);
  844. activeMapPanel->deactivate();
  845. if (mode == EAdvMapMode::NORMAL)
  846. {
  847. heroList.deactivate();
  848. townList.deactivate();
  849. infoBar.deactivate();
  850. }
  851. minimap.deactivate();
  852. terrain.deactivate();
  853. statusbar->deactivate();
  854. }
  855. }
  856. void CAdvMapInt::showAll(SDL_Surface * to)
  857. {
  858. blitAt(bg,0,0,to);
  859. if(state != INGAME)
  860. return;
  861. switch (mode)
  862. {
  863. case EAdvMapMode::NORMAL:
  864. heroList.showAll(to);
  865. townList.showAll(to);
  866. infoBar.showAll(to);
  867. break;
  868. case EAdvMapMode::WORLD_VIEW:
  869. terrain.showAll(to);
  870. break;
  871. }
  872. activeMapPanel->showAll(to);
  873. updateScreen = true;
  874. minimap.showAll(to);
  875. show(to);
  876. resdatabar.showAll(to);
  877. statusbar->show(to);
  878. LOCPLINT->cingconsole->show(to);
  879. }
  880. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  881. {
  882. if (!hero)
  883. return false;
  884. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  885. }
  886. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  887. {
  888. if (sleep)
  889. LOCPLINT->sleepingHeroes.push_back(hero); //FIXME: should we check for existence?
  890. else
  891. LOCPLINT->sleepingHeroes -= hero;
  892. updateNextHero(nullptr);
  893. }
  894. void CAdvMapInt::show(SDL_Surface * to)
  895. {
  896. if(state != INGAME)
  897. return;
  898. ++animValHitCount; //for animations
  899. if(animValHitCount % 2 == 0)
  900. {
  901. ++heroAnim;
  902. }
  903. if(animValHitCount == 8)
  904. {
  905. CGI->mh->updateWater();
  906. animValHitCount = 0;
  907. ++anim;
  908. updateScreen = true;
  909. }
  910. if(swipeEnabled)
  911. {
  912. handleSwipeUpdate();
  913. }
  914. #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)
  915. else
  916. #endif
  917. {
  918. handleMapScrollingUpdate();
  919. }
  920. for(int i = 0; i < 4; i++)
  921. {
  922. if(settings["session"]["spectate"].Bool())
  923. gems[i]->setFrame(PlayerColor(1).getNum());
  924. else
  925. gems[i]->setFrame(LOCPLINT->playerID.getNum());
  926. }
  927. if(updateScreen)
  928. {
  929. int3 betterPos = LOCPLINT->repairScreenPos(position);
  930. if (betterPos != position)
  931. {
  932. logGlobal->warn("Incorrect position for adventure map!");
  933. position = betterPos;
  934. }
  935. terrain.show(to);
  936. for(int i = 0; i < 4; i++)
  937. gems[i]->showAll(to);
  938. updateScreen=false;
  939. LOCPLINT->cingconsole->show(to);
  940. }
  941. else if (terrain.needsAnimUpdate())
  942. {
  943. terrain.showAnim(to);
  944. for(int i = 0; i < 4; i++)
  945. gems[i]->showAll(to);
  946. }
  947. infoBar.show(to);
  948. statusbar->showAll(to);
  949. }
  950. void CAdvMapInt::handleMapScrollingUpdate()
  951. {
  952. int scrollSpeed = static_cast<int>(settings["adventure"]["scrollSpeed"].Float());
  953. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  954. if((animValHitCount % (4 / scrollSpeed)) == 0
  955. && ((GH.topInt().get() == this) || isCtrlKeyDown()))
  956. {
  957. if((scrollingDir & LEFT) && (position.x > -CGI->mh->frameW))
  958. position.x--;
  959. if((scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW))
  960. position.x++;
  961. if((scrollingDir & UP) && (position.y > -CGI->mh->frameH))
  962. position.y--;
  963. if((scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH))
  964. position.y++;
  965. if(scrollingDir)
  966. {
  967. setScrollingCursor(scrollingDir);
  968. scrollingState = true;
  969. updateScreen = true;
  970. minimap.redraw();
  971. if(mode == EAdvMapMode::WORLD_VIEW)
  972. terrain.redraw();
  973. }
  974. else if(scrollingState)
  975. {
  976. CCS->curh->set(Cursor::Map::POINTER);
  977. scrollingState = false;
  978. }
  979. }
  980. }
  981. void CAdvMapInt::handleSwipeUpdate()
  982. {
  983. if(swipeMovementRequested)
  984. {
  985. auto fixedPos = LOCPLINT->repairScreenPos(swipeTargetPosition);
  986. position.x = fixedPos.x;
  987. position.y = fixedPos.y;
  988. CCS->curh->set(Cursor::Map::POINTER);
  989. updateScreen = true;
  990. minimap.redraw();
  991. swipeMovementRequested = false;
  992. }
  993. }
  994. void CAdvMapInt::selectionChanged()
  995. {
  996. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  997. if (selection != to)
  998. select(to);
  999. }
  1000. void CAdvMapInt::centerOn(int3 on, bool fade)
  1001. {
  1002. bool switchedLevels = on.z != position.z;
  1003. if (fade)
  1004. {
  1005. terrain.fadeFromCurrentView();
  1006. }
  1007. switch (mode)
  1008. {
  1009. default:
  1010. case EAdvMapMode::NORMAL:
  1011. on.x -= CGI->mh->frameW; // is this intentional? frame size doesn't really have to correspond to camera size...
  1012. on.y -= CGI->mh->frameH;
  1013. break;
  1014. case EAdvMapMode::WORLD_VIEW:
  1015. on.x -= static_cast<si32>(CGI->mh->tilesW / 2 / worldViewScale);
  1016. on.y -= static_cast<si32>(CGI->mh->tilesH / 2 / worldViewScale);
  1017. break;
  1018. }
  1019. on = LOCPLINT->repairScreenPos(on);
  1020. position = on;
  1021. updateScreen=true;
  1022. underground->setIndex(on.z,true); //change underground switch button image
  1023. underground->redraw();
  1024. worldViewUnderground->setIndex(on.z, true);
  1025. worldViewUnderground->redraw();
  1026. if (switchedLevels)
  1027. minimap.setLevel(position.z);
  1028. minimap.redraw();
  1029. if (mode == EAdvMapMode::WORLD_VIEW)
  1030. terrain.redraw();
  1031. }
  1032. void CAdvMapInt::centerOn(const CGObjectInstance * obj, bool fade)
  1033. {
  1034. centerOn(obj->getSightCenter(), fade);
  1035. }
  1036. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1037. {
  1038. if (mode == EAdvMapMode::WORLD_VIEW)
  1039. return;
  1040. ui8 Dir = 0;
  1041. SDL_Keycode k = key.keysym.sym;
  1042. const CGHeroInstance *h = curHero(); //selected hero
  1043. const CGTownInstance *t = curTown(); //selected town
  1044. switch(k)
  1045. {
  1046. case SDLK_g:
  1047. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1048. return;
  1049. {
  1050. //find first town with tavern
  1051. auto itr = range::find_if(LOCPLINT->towns, [](const CGTownInstance * town)
  1052. {
  1053. return town->hasBuilt(BuildingID::TAVERN);
  1054. });
  1055. if(itr != LOCPLINT->towns.end())
  1056. LOCPLINT->showThievesGuildWindow(*itr);
  1057. else
  1058. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithTavern"));
  1059. }
  1060. return;
  1061. case SDLK_i:
  1062. if(isActive())
  1063. CAdventureOptions::showScenarioInfo();
  1064. return;
  1065. case SDLK_l:
  1066. if(isActive())
  1067. LOCPLINT->proposeLoadingGame();
  1068. return;
  1069. case SDLK_s:
  1070. if(isActive() && key.type == SDL_KEYUP)
  1071. GH.pushIntT<CSavingScreen>();
  1072. return;
  1073. case SDLK_d:
  1074. {
  1075. if(h && isActive() && LOCPLINT->makingTurn && key.state == SDL_PRESSED)
  1076. LOCPLINT->tryDiggging(h);
  1077. return;
  1078. }
  1079. case SDLK_p:
  1080. if(isActive())
  1081. LOCPLINT->showPuzzleMap();
  1082. return;
  1083. case SDLK_v:
  1084. if(isActive())
  1085. LOCPLINT->viewWorldMap();
  1086. return;
  1087. case SDLK_r:
  1088. if(isActive() && LOCPLINT->ctrlPressed())
  1089. {
  1090. LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
  1091. [](){ LOCPLINT->sendCustomEvent(EUserEvent::RESTART_GAME); }, nullptr);
  1092. }
  1093. return;
  1094. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1095. {
  1096. if(!isActive())
  1097. return;
  1098. if(h && key.state == SDL_PRESSED)
  1099. {
  1100. auto unlockPim = vstd::makeUnlockGuard(*CPlayerInterface::pim);
  1101. //TODO!!!!!!! possible freeze, when GS mutex is locked and network thread can't apply package
  1102. //this thread leaves scope and tries to lock pim while holding gs,
  1103. //network thread tries to lock gs (appluy cl) while holding pim
  1104. //this thread should first lock pim, however gs locking/unlocking is done inside cb
  1105. LOCPLINT->cb->moveHero(h,h->pos);
  1106. }
  1107. }
  1108. return;
  1109. case SDLK_RETURN:
  1110. {
  1111. if(!isActive() || !selection || key.state != SDL_PRESSED)
  1112. return;
  1113. if(h)
  1114. LOCPLINT->openHeroWindow(h);
  1115. else if(t)
  1116. LOCPLINT->openTownWindow(t);
  1117. return;
  1118. }
  1119. case SDLK_ESCAPE:
  1120. {
  1121. if(isActive() || GH.topInt().get() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  1122. return;
  1123. leaveCastingMode();
  1124. return;
  1125. }
  1126. case SDLK_t:
  1127. {
  1128. //act on key down if marketplace windows is not already opened
  1129. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  1130. return;
  1131. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  1132. {
  1133. //check if we have any marketplace
  1134. const CGTownInstance *townWithMarket = nullptr;
  1135. for(const CGTownInstance *t : LOCPLINT->cb->getTownsInfo())
  1136. {
  1137. if(t->hasBuilt(BuildingID::MARKETPLACE))
  1138. {
  1139. townWithMarket = t;
  1140. break;
  1141. }
  1142. }
  1143. if(townWithMarket) //if any town has marketplace, open window
  1144. GH.pushIntT<CMarketplaceWindow>(townWithMarket);
  1145. else //if not - complain
  1146. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  1147. }
  1148. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  1149. {
  1150. townList.selectNext();
  1151. }
  1152. return;
  1153. }
  1154. default:
  1155. {
  1156. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1157. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1158. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1159. //numpad arrow
  1160. if(CGuiHandler::isArrowKey(k))
  1161. k = CGuiHandler::arrowToNum(k);
  1162. k -= SDLK_KP_1;
  1163. if(k < 0 || k > 8)
  1164. return;
  1165. if (!CGI->mh->canStartHeroMovement())
  1166. return;
  1167. int3 dir = directions[k];
  1168. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  1169. {
  1170. Dir = (dir.x<0 ? LEFT : 0) |
  1171. (dir.x>0 ? RIGHT : 0) |
  1172. (dir.y<0 ? UP : 0) |
  1173. (dir.y>0 ? DOWN : 0) ;
  1174. break;
  1175. }
  1176. if(!h || key.state != SDL_PRESSED)
  1177. break;
  1178. if(k == 4)
  1179. {
  1180. centerOn(h);
  1181. return;
  1182. }
  1183. CGPath &path = LOCPLINT->paths[h];
  1184. terrain.currentPath = &path;
  1185. int3 dst = h->visitablePos() + dir;
  1186. if(dst != verifyPos(dst) || !LOCPLINT->cb->getPathsInfo(h)->getPath(path, dst))
  1187. {
  1188. terrain.currentPath = nullptr;
  1189. return;
  1190. }
  1191. if (path.nodes.size() > 2)
  1192. updateMoveHero(h);
  1193. else
  1194. if(!path.nodes[0].turns)
  1195. LOCPLINT->moveHero(h, path);
  1196. }
  1197. return;
  1198. }
  1199. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1200. && LOCPLINT->ctrlPressed()
  1201. )
  1202. scrollingDir |= Dir;
  1203. else
  1204. scrollingDir &= ~Dir;
  1205. }
  1206. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  1207. {
  1208. if(down)
  1209. {
  1210. CRClickPopup::createAndPush(text);
  1211. }
  1212. }
  1213. int3 CAdvMapInt::verifyPos(int3 ver)
  1214. {
  1215. if (ver.x<0)
  1216. ver.x=0;
  1217. if (ver.y<0)
  1218. ver.y=0;
  1219. if (ver.z<0)
  1220. ver.z=0;
  1221. if (ver.x>=CGI->mh->sizes.x)
  1222. ver.x=CGI->mh->sizes.x-1;
  1223. if (ver.y>=CGI->mh->sizes.y)
  1224. ver.y=CGI->mh->sizes.y-1;
  1225. if (ver.z>=CGI->mh->sizes.z)
  1226. ver.z=CGI->mh->sizes.z-1;
  1227. return ver;
  1228. }
  1229. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView)
  1230. {
  1231. assert(sel);
  1232. LOCPLINT->setSelection(sel);
  1233. selection = sel;
  1234. if (LOCPLINT->battleInt == nullptr && LOCPLINT->makingTurn)
  1235. {
  1236. auto pos = sel->visitablePos();
  1237. auto tile = LOCPLINT->cb->getTile(pos);
  1238. if(tile)
  1239. CCS->musich->playMusicFromSet("terrain", tile->terType->getName(), true, false);
  1240. }
  1241. if(centerView)
  1242. centerOn(sel);
  1243. terrain.currentPath = nullptr;
  1244. if(sel->ID==Obj::TOWN)
  1245. {
  1246. auto town = dynamic_cast<const CGTownInstance*>(sel);
  1247. infoBar.showTownSelection(town);
  1248. townList.select(town);
  1249. heroList.select(nullptr);
  1250. updateSleepWake(nullptr);
  1251. updateMoveHero(nullptr);
  1252. updateSpellbook(nullptr);
  1253. }
  1254. else //hero selected
  1255. {
  1256. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  1257. infoBar.showHeroSelection(hero);
  1258. heroList.select(hero);
  1259. townList.select(nullptr);
  1260. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  1261. updateSleepWake(hero);
  1262. updateMoveHero(hero);
  1263. updateSpellbook(hero);
  1264. }
  1265. townList.redraw();
  1266. heroList.redraw();
  1267. }
  1268. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1269. {
  1270. #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
  1271. if(swipeEnabled)
  1272. return;
  1273. #endif
  1274. // adventure map scrolling with mouse
  1275. // currently disabled in world view mode (as it is in OH3), but should work correctly if mode check is removed
  1276. // don't scroll if there is no window in focus - these events don't seem to correspond to the actual mouse movement
  1277. if(!isCtrlKeyDown() && isActive() && sEvent.windowID != 0 && mode == EAdvMapMode::NORMAL)
  1278. {
  1279. if(sEvent.x<15)
  1280. {
  1281. scrollingDir |= LEFT;
  1282. }
  1283. else
  1284. {
  1285. scrollingDir &= ~LEFT;
  1286. }
  1287. if(sEvent.x>screen->w-15)
  1288. {
  1289. scrollingDir |= RIGHT;
  1290. }
  1291. else
  1292. {
  1293. scrollingDir &= ~RIGHT;
  1294. }
  1295. if(sEvent.y<15)
  1296. {
  1297. scrollingDir |= UP;
  1298. }
  1299. else
  1300. {
  1301. scrollingDir &= ~UP;
  1302. }
  1303. if(sEvent.y>screen->h-15)
  1304. {
  1305. scrollingDir |= DOWN;
  1306. }
  1307. else
  1308. {
  1309. scrollingDir &= ~DOWN;
  1310. }
  1311. }
  1312. }
  1313. bool CAdvMapInt::isActive()
  1314. {
  1315. return active & ~CIntObject::KEYBOARD;
  1316. }
  1317. void CAdvMapInt::startHotSeatWait(PlayerColor Player)
  1318. {
  1319. state = WAITING;
  1320. }
  1321. void CAdvMapInt::setPlayer(PlayerColor Player)
  1322. {
  1323. player = Player;
  1324. graphics->blueToPlayersAdv(bg,player);
  1325. panelMain->setPlayerColor(player);
  1326. panelWorldView->setPlayerColor(player);
  1327. panelWorldView->recolorIcons(player, player.getNum() * 19);
  1328. resdatabar.background->colorize(player);
  1329. }
  1330. void CAdvMapInt::startTurn()
  1331. {
  1332. state = INGAME;
  1333. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID
  1334. || settings["session"]["spectate"].Bool())
  1335. {
  1336. adjustActiveness(false);
  1337. minimap.setAIRadar(false);
  1338. }
  1339. }
  1340. void CAdvMapInt::endingTurn()
  1341. {
  1342. if(settings["session"]["spectate"].Bool())
  1343. return;
  1344. LOCPLINT->makingTurn = false;
  1345. LOCPLINT->cb->endTurn();
  1346. CCS->soundh->ambientStopAllChannels();
  1347. }
  1348. const CGObjectInstance* CAdvMapInt::getActiveObject(const int3 &mapPos)
  1349. {
  1350. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  1351. if (bobjs.empty())
  1352. return nullptr;
  1353. return *boost::range::max_element(bobjs, &CMapHandler::compareObjectBlitOrder);
  1354. /*
  1355. if (bobjs.back()->ID == Obj::HERO)
  1356. return bobjs.back();
  1357. else
  1358. return bobjs.front();*/
  1359. }
  1360. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  1361. {
  1362. if(mode != EAdvMapMode::NORMAL)
  1363. return;
  1364. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1365. return;
  1366. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1367. const CGObjectInstance *topBlocking = getActiveObject(mapPos);
  1368. int3 selPos = selection->getSightCenter();
  1369. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1370. {
  1371. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1372. switch(spellBeingCasted->id)
  1373. {
  1374. case SpellID::SCUTTLE_BOAT: //Scuttle Boat
  1375. if(topBlocking && topBlocking->ID == Obj::BOAT)
  1376. leaveCastingMode(true, mapPos);
  1377. break;
  1378. case SpellID::DIMENSION_DOOR:
  1379. if(!tile || tile->isClear(heroTile))
  1380. leaveCastingMode(true, mapPos);
  1381. break;
  1382. }
  1383. return;
  1384. }
  1385. //check if we can select this object
  1386. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1387. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1388. if(selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1389. {
  1390. assert(!terrain.currentPath); //path can be active only when hero is selected
  1391. if(selection == topBlocking) //selected town clicked
  1392. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1393. else if(canSelect)
  1394. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1395. return;
  1396. }
  1397. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1398. {
  1399. const CGPathNode *pn = LOCPLINT->cb->getPathsInfo(currentHero)->getPathInfo(mapPos);
  1400. if(currentHero == topBlocking) //clicked selected hero
  1401. {
  1402. LOCPLINT->openHeroWindow(currentHero);
  1403. return;
  1404. }
  1405. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1406. {
  1407. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1408. return;
  1409. }
  1410. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1411. {
  1412. if(terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1413. {
  1414. if(CGI->mh->canStartHeroMovement())
  1415. LOCPLINT->moveHero(currentHero, *terrain.currentPath);
  1416. return;
  1417. }
  1418. else //remove old path and find a new one if we clicked on accessible tile
  1419. {
  1420. CGPath &path = LOCPLINT->paths[currentHero];
  1421. CGPath newpath;
  1422. bool gotPath = LOCPLINT->cb->getPathsInfo(currentHero)->getPath(newpath, mapPos); //try getting path, erase if failed
  1423. if(gotPath && newpath.nodes.size())
  1424. path = newpath;
  1425. if(path.nodes.size())
  1426. terrain.currentPath = &path;
  1427. else
  1428. LOCPLINT->eraseCurrentPathOf(currentHero);
  1429. updateMoveHero(currentHero);
  1430. }
  1431. }
  1432. } //end of hero is selected "case"
  1433. else
  1434. {
  1435. throw std::runtime_error("Nothing is selected...");
  1436. }
  1437. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  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 * h = 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 * pnode = LOCPLINT->cb->getPathsInfo(h)->getPathInfo(mapPos);
  1514. assert(pnode);
  1515. int turns = pnode->turns;
  1516. vstd::amin(turns, 3);
  1517. switch(pnode->action)
  1518. {
  1519. case CGPathNode::NORMAL:
  1520. case CGPathNode::TELEPORT_NORMAL:
  1521. if(pnode->layer == EPathfindingLayer::LAND)
  1522. CCS->curh->set(cursorMove[turns]);
  1523. else
  1524. CCS->curh->set(cursorSailVisit[turns]);
  1525. break;
  1526. case CGPathNode::VISIT:
  1527. case CGPathNode::BLOCKING_VISIT:
  1528. case CGPathNode::TELEPORT_BLOCKING_VISIT:
  1529. if(objAtTile && objAtTile->ID == Obj::HERO)
  1530. {
  1531. if(selection == objAtTile)
  1532. CCS->curh->set(Cursor::Map::HERO);
  1533. else
  1534. CCS->curh->set(cursorExchange[turns]);
  1535. }
  1536. else if(pnode->layer == EPathfindingLayer::LAND)
  1537. CCS->curh->set(cursorVisit[turns]);
  1538. else
  1539. CCS->curh->set(cursorSailVisit[turns]);
  1540. break;
  1541. case CGPathNode::BATTLE:
  1542. case CGPathNode::TELEPORT_BATTLE:
  1543. CCS->curh->set(cursorAttack[turns]);
  1544. break;
  1545. case CGPathNode::EMBARK:
  1546. CCS->curh->set(cursorSail[turns]);
  1547. break;
  1548. case CGPathNode::DISEMBARK:
  1549. CCS->curh->set(cursorDisembark[turns]);
  1550. break;
  1551. default:
  1552. if(objAtTile && objRelations != PlayerRelations::ENEMIES)
  1553. {
  1554. if(objAtTile->ID == Obj::TOWN)
  1555. CCS->curh->set(Cursor::Map::TOWN);
  1556. else if(objAtTile->ID == Obj::HERO && objRelations == PlayerRelations::SAME_PLAYER)
  1557. CCS->curh->set(Cursor::Map::HERO);
  1558. else
  1559. CCS->curh->set(Cursor::Map::POINTER);
  1560. }
  1561. else
  1562. CCS->curh->set(Cursor::Map::POINTER);
  1563. break;
  1564. }
  1565. }
  1566. if(ourInaccessibleShipyard(objAtTile))
  1567. {
  1568. CCS->curh->set(Cursor::Map::T1_SAIL);
  1569. }
  1570. }
  1571. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1572. {
  1573. if(mode != EAdvMapMode::NORMAL)
  1574. return;
  1575. if(spellBeingCasted)
  1576. {
  1577. leaveCastingMode();
  1578. return;
  1579. }
  1580. if(!LOCPLINT->cb->isVisible(mapPos))
  1581. {
  1582. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1583. return;
  1584. }
  1585. const CGObjectInstance * obj = getActiveObject(mapPos);
  1586. if(!obj)
  1587. {
  1588. // Bare or undiscovered terrain
  1589. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1590. if (tile)
  1591. {
  1592. std::string hlp;
  1593. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1594. CRClickPopup::createAndPush(hlp);
  1595. }
  1596. return;
  1597. }
  1598. CRClickPopup::createAndPush(obj, Point(GH.current->motion), ETextAlignment::CENTER);
  1599. }
  1600. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1601. {
  1602. assert(sp->id == SpellID::SCUTTLE_BOAT || sp->id == SpellID::DIMENSION_DOOR);
  1603. spellBeingCasted = sp;
  1604. deactivate();
  1605. terrain.activate();
  1606. GH.fakeMouseMove();
  1607. }
  1608. void CAdvMapInt::leaveCastingMode(bool cast, int3 dest)
  1609. {
  1610. assert(spellBeingCasted);
  1611. SpellID id = spellBeingCasted->id;
  1612. spellBeingCasted = nullptr;
  1613. terrain.deactivate();
  1614. activate();
  1615. if(cast)
  1616. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1617. else
  1618. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1619. }
  1620. const CGHeroInstance * CAdvMapInt::curHero() const
  1621. {
  1622. if(selection && selection->ID == Obj::HERO)
  1623. return static_cast<const CGHeroInstance *>(selection);
  1624. else
  1625. return nullptr;
  1626. }
  1627. const CGTownInstance * CAdvMapInt::curTown() const
  1628. {
  1629. if(selection && selection->ID == Obj::TOWN)
  1630. return static_cast<const CGTownInstance *>(selection);
  1631. else
  1632. return nullptr;
  1633. }
  1634. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1635. {
  1636. const IShipyard *ret = IShipyard::castFrom(obj);
  1637. if(!ret ||
  1638. obj->tempOwner != player ||
  1639. (CCS->curh->get<Cursor::Map>() != Cursor::Map::T1_SAIL && CCS->curh->get<Cursor::Map>() != Cursor::Map::POINTER))
  1640. return nullptr;
  1641. return ret;
  1642. }
  1643. void CAdvMapInt::aiTurnStarted()
  1644. {
  1645. if(settings["session"]["spectate"].Bool())
  1646. return;
  1647. adjustActiveness(true);
  1648. CCS->musich->playMusicFromSet("enemy-turn", true, false);
  1649. adventureInt->minimap.setAIRadar(true);
  1650. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1651. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1652. }
  1653. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1654. {
  1655. bool wasActive = isActive();
  1656. if(wasActive)
  1657. deactivate();
  1658. adventureInt->duringAITurn = aiTurnStart;
  1659. if(wasActive)
  1660. activate();
  1661. }
  1662. void CAdvMapInt::quickCombatLock()
  1663. {
  1664. if(!duringAITurn)
  1665. deactivate();
  1666. }
  1667. void CAdvMapInt::quickCombatUnlock()
  1668. {
  1669. if(!duringAITurn)
  1670. activate();
  1671. }
  1672. void CAdvMapInt::changeMode(EAdvMapMode newMode, float newScale)
  1673. {
  1674. if (mode != newMode)
  1675. {
  1676. mode = newMode;
  1677. switch (mode)
  1678. {
  1679. case EAdvMapMode::NORMAL:
  1680. panelMain->activate();
  1681. panelWorldView->deactivate();
  1682. activeMapPanel = panelMain;
  1683. townList.activate();
  1684. heroList.activate();
  1685. infoBar.activate();
  1686. worldViewOptions.clear();
  1687. break;
  1688. case EAdvMapMode::WORLD_VIEW:
  1689. panelMain->deactivate();
  1690. panelWorldView->activate();
  1691. activeMapPanel = panelWorldView;
  1692. townList.deactivate();
  1693. heroList.deactivate();
  1694. infoBar.showSelection(); // to prevent new day animation interfering world view mode
  1695. infoBar.deactivate();
  1696. break;
  1697. }
  1698. worldViewScale = newScale;
  1699. redraw();
  1700. }
  1701. else if (worldViewScale != newScale) // still in world view mode, but the scale changed
  1702. {
  1703. worldViewScale = newScale;
  1704. redraw();
  1705. }
  1706. }
  1707. CAdventureOptions::CAdventureOptions()
  1708. : CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1709. {
  1710. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1711. viewWorld = std::make_shared<CButton>(Point(24, 23), "ADVVIEW.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_v);
  1712. viewWorld->addCallback(std::bind(&CPlayerInterface::viewWorldMap, LOCPLINT));
  1713. exit = std::make_shared<CButton>(Point(204, 313), "IOK6432.DEF", CButton::tooltip(), std::bind(&CAdventureOptions::close, this), SDLK_RETURN);
  1714. exit->assignedKeys.insert(SDLK_ESCAPE);
  1715. scenInfo = std::make_shared<CButton>(Point(24, 198), "ADVINFO.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_i);
  1716. scenInfo->addCallback(CAdventureOptions::showScenarioInfo);
  1717. puzzle = std::make_shared<CButton>(Point(24, 81), "ADVPUZ.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_p);
  1718. puzzle->addCallback(std::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT));
  1719. dig = std::make_shared<CButton>(Point(24, 139), "ADVDIG.DEF", CButton::tooltip(), [&](){ close(); }, SDLK_d);
  1720. if(const CGHeroInstance *h = adventureInt->curHero())
  1721. dig->addCallback(std::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h));
  1722. else
  1723. dig->block(true);
  1724. }
  1725. void CAdventureOptions::showScenarioInfo()
  1726. {
  1727. if(LOCPLINT->cb->getStartInfo()->campState)
  1728. {
  1729. GH.pushIntT<CCampaignInfoScreen>();
  1730. }
  1731. else
  1732. {
  1733. GH.pushIntT<CScenarioInfoScreen>();
  1734. }
  1735. }
  1736. CAdvMapInt::WorldViewOptions::WorldViewOptions()
  1737. {
  1738. clear();
  1739. }
  1740. void CAdvMapInt::WorldViewOptions::clear()
  1741. {
  1742. showAllTerrain = false;
  1743. iconPositions.clear();
  1744. }
  1745. void CAdvMapInt::WorldViewOptions::adjustDrawingInfo(MapDrawingInfo& info)
  1746. {
  1747. info.showAllTerrain = showAllTerrain;
  1748. info.additionalIcons = &iconPositions;
  1749. }