CAdvmapInterface.cpp 51 KB

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