CAdvmapInterface.cpp 42 KB

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