CAdvmapInterface.cpp 52 KB

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