CAdvmapInterface.cpp 51 KB

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