CAdvmapInterface.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. #include "StdInc.h"
  2. #include "CAdvmapInterface.h"
  3. #include "../CCallback.h"
  4. #include "CCastleInterface.h"
  5. #include "UIFramework/CCursorHandler.h"
  6. #include "CGameInfo.h"
  7. #include "CHeroWindow.h"
  8. #include "CKingdomInterface.h"
  9. #include "CMessage.h"
  10. #include "CPlayerInterface.h"
  11. #include "UIFramework/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/Map/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 "UIFramework/CGuiHandler.h"
  31. #include "UIFramework/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(NULL)
  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, NULL, 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, NULL, 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. char * buf = new char[15];
  306. for (int i=0;i<7;i++)
  307. {
  308. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  309. printAt(buf,txtpos[i].first,txtpos[i].second,FONT_SMALL,Colors::WHITE,to);
  310. }
  311. std::vector<std::string> temp;
  312. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp+=std::string(buf);
  313. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp+=std::string(buf);
  314. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp+=std::string(buf);
  315. printAt(processStr(datetext,temp),txtpos[7].first,txtpos[7].second,FONT_SMALL,Colors::WHITE,to);
  316. temp.clear();
  317. //updateRect(&pos,screen);
  318. delete[] buf;
  319. }
  320. void CResDataBar::show(SDL_Surface * to)
  321. {
  322. }
  323. void CResDataBar::showAll(SDL_Surface * to)
  324. {
  325. draw(to);
  326. }
  327. CAdvMapInt::CAdvMapInt():
  328. minimap(Rect(ADVOPT.minimapX, ADVOPT.minimapY, ADVOPT.minimapW, ADVOPT.minimapH)),
  329. statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  330. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  331. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  332. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  333. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  334. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  335. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  336. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  337. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  338. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  339. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  340. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  341. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  342. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  343. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  344. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  345. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  346. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  347. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  348. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  349. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  350. heroList(ADVOPT.hlistSize, Point(ADVOPT.hlistX, ADVOPT.hlistY), ADVOPT.hlistAU, ADVOPT.hlistAD),
  351. townList(ADVOPT.tlistSize, Point(ADVOPT.tlistX, ADVOPT.tlistY), ADVOPT.tlistAU, ADVOPT.tlistAD),
  352. infoBar(Rect(ADVOPT.infoboxX, ADVOPT.infoboxY, 192, 192) )
  353. {
  354. duringAITurn = false;
  355. state = NA;
  356. spellBeingCasted = NULL;
  357. pos.x = pos.y = 0;
  358. pos.w = screen->w;
  359. pos.h = screen->h;
  360. selection = NULL;
  361. townList.onSelect = boost::bind(&CAdvMapInt::selectionChanged,this);
  362. adventureInt=this;
  363. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  364. scrollingDir = 0;
  365. updateScreen = false;
  366. anim=0;
  367. animValHitCount=0; //animation frame
  368. heroAnim=0;
  369. heroAnimValHitCount=0; // hero animation frame
  370. for (int g=0; g<ADVOPT.gemG.size(); ++g)
  371. {
  372. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[g]));
  373. }
  374. setPlayer(LOCPLINT->playerID);
  375. underground.block(!CGI->mh->map->twoLevel);
  376. addUsedEvents(MOVE);
  377. }
  378. CAdvMapInt::~CAdvMapInt()
  379. {
  380. SDL_FreeSurface(bg);
  381. for(int i=0; i<gems.size(); i++)
  382. delete gems[i];
  383. }
  384. void CAdvMapInt::fshowOverview()
  385. {
  386. GH.pushInt(new CKingdomInterface);
  387. }
  388. void CAdvMapInt::fswitchLevel()
  389. {
  390. if(!CGI->mh->map->twoLevel)
  391. return;
  392. if (position.z)
  393. {
  394. position.z--;
  395. underground.setIndex(0,true);
  396. underground.showAll(screenBuf);
  397. }
  398. else
  399. {
  400. underground.setIndex(1,true);
  401. position.z++;
  402. underground.showAll(screenBuf);
  403. }
  404. updateScreen = true;
  405. minimap.setLevel(position.z);
  406. }
  407. void CAdvMapInt::fshowQuestlog()
  408. {
  409. LOCPLINT->showQuestLog();
  410. }
  411. void CAdvMapInt::fsleepWake()
  412. {
  413. const CGHeroInstance *h = curHero();
  414. if (!h)
  415. return;
  416. bool newSleep = !isHeroSleeping(h);
  417. setHeroSleeping(h, newSleep);
  418. updateSleepWake(h);
  419. if (newSleep)
  420. {
  421. fnextHero();
  422. //moveHero.block(true);
  423. //uncomment to enable original HoMM3 behaviour:
  424. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  425. }
  426. }
  427. void CAdvMapInt::fmoveHero()
  428. {
  429. const CGHeroInstance *h = curHero();
  430. if (!h || !terrain.currentPath)
  431. return;
  432. LOCPLINT->moveHero(h, *terrain.currentPath);
  433. }
  434. void CAdvMapInt::fshowSpellbok()
  435. {
  436. if (!curHero()) //checking necessary values
  437. return;
  438. centerOn(selection);
  439. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), curHero(), LOCPLINT, false);
  440. GH.pushInt(spellWindow);
  441. }
  442. void CAdvMapInt::fadventureOPtions()
  443. {
  444. GH.pushInt(new CAdventureOptions);
  445. }
  446. void CAdvMapInt::fsystemOptions()
  447. {
  448. GH.pushInt(new CSystemOptionsWindow());
  449. }
  450. void CAdvMapInt::fnextHero()
  451. {
  452. auto hero = dynamic_cast<const CGHeroInstance*>(selection);
  453. int next = getNextHeroIndex(vstd::find_pos(LOCPLINT->wanderingHeroes, hero));
  454. if (next < 0)
  455. return;
  456. select(LOCPLINT->wanderingHeroes[next], true);
  457. }
  458. void CAdvMapInt::fendTurn()
  459. {
  460. if(!LOCPLINT->makingTurn)
  461. return;
  462. if ( settings["adventure"]["heroReminder"].Bool())
  463. {
  464. for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)
  465. if (!isHeroSleeping(LOCPLINT->wanderingHeroes[i]) && (LOCPLINT->wanderingHeroes[i]->movement > 0))
  466. {
  467. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], boost::bind(&CAdvMapInt::endingTurn, this), 0, false);
  468. return;
  469. }
  470. }
  471. endingTurn();
  472. }
  473. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  474. {
  475. sleepWake.block(!h);
  476. if (!h)
  477. return;
  478. bool state = isHeroSleeping(h);
  479. sleepWake.setIndex(state ? 1 : 0, true);
  480. sleepWake.assignedKeys.clear();
  481. sleepWake.assignedKeys.insert(state ? SDLK_w : SDLK_z);
  482. sleepWake.update();
  483. }
  484. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  485. {
  486. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  487. if (hasPath == tribool::indeterminate_value)
  488. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  489. if (!h)
  490. {
  491. moveHero.block(true);
  492. return;
  493. }
  494. moveHero.block(!hasPath || (h->movement == 0));
  495. }
  496. int CAdvMapInt::getNextHeroIndex(int startIndex)
  497. {
  498. if (LOCPLINT->wanderingHeroes.size() == 0)
  499. return -1;
  500. if (startIndex < 0)
  501. startIndex = 0;
  502. int i = startIndex;
  503. do
  504. {
  505. i++;
  506. if (i >= LOCPLINT->wanderingHeroes.size())
  507. i = 0;
  508. }
  509. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  510. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  511. return i;
  512. else
  513. return -1;
  514. }
  515. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  516. {
  517. int start = vstd::find_pos(LOCPLINT->wanderingHeroes, h);
  518. int next = getNextHeroIndex(start);
  519. if (next < 0)
  520. {
  521. nextHero.block(true);
  522. return;
  523. }
  524. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  525. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  526. nextHero.block(noActiveHeroes);
  527. }
  528. void CAdvMapInt::activate()
  529. {
  530. CIntObject::activate();
  531. if (!(active & KEYBOARD))
  532. CIntObject::activate(KEYBOARD);
  533. screenBuf = screen;
  534. GH.statusbar = &statusbar;
  535. if(!duringAITurn)
  536. {
  537. kingOverview.activate();
  538. underground.activate();
  539. questlog.activate();
  540. sleepWake.activate();
  541. moveHero.activate();
  542. spellbook.activate();
  543. sysOptions.activate();
  544. advOptions.activate();
  545. nextHero.activate();
  546. endTurn.activate();
  547. minimap.activate();
  548. heroList.activate();
  549. townList.activate();
  550. terrain.activate();
  551. infoBar.activate();
  552. LOCPLINT->cingconsole->activate();
  553. GH.fakeMouseMove(); //to restore the cursor
  554. }
  555. }
  556. void CAdvMapInt::deactivate()
  557. {
  558. CIntObject::deactivate();
  559. if(!duringAITurn)
  560. {
  561. scrollingDir = 0;
  562. CCS->curh->changeGraphic(ECursor::ADVENTURE,0);
  563. kingOverview.deactivate();
  564. underground.deactivate();
  565. questlog.deactivate();
  566. sleepWake.deactivate();
  567. moveHero.deactivate();
  568. spellbook.deactivate();
  569. advOptions.deactivate();
  570. sysOptions.deactivate();
  571. nextHero.deactivate();
  572. endTurn.deactivate();
  573. minimap.deactivate();
  574. heroList.deactivate();
  575. townList.deactivate();
  576. terrain.deactivate();
  577. infoBar.deactivate();
  578. LOCPLINT->cingconsole->deactivate();
  579. }
  580. }
  581. void CAdvMapInt::showAll(SDL_Surface * to)
  582. {
  583. blitAt(bg,0,0,to);
  584. if(state != INGAME)
  585. return;
  586. kingOverview.showAll(to);
  587. underground.showAll(to);
  588. questlog.showAll(to);
  589. sleepWake.showAll(to);
  590. moveHero.showAll(to);
  591. spellbook.showAll(to);
  592. advOptions.showAll(to);
  593. sysOptions.showAll(to);
  594. nextHero.showAll(to);
  595. endTurn.showAll(to);
  596. minimap.showAll(to);
  597. heroList.showAll(to);
  598. townList.showAll(to);
  599. updateScreen = true;
  600. show(to);
  601. resdatabar.draw(to);
  602. statusbar.show(to);
  603. infoBar.showAll(to);
  604. LOCPLINT->cingconsole->showAll(to);
  605. }
  606. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  607. {
  608. if (!hero)
  609. return false;
  610. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  611. }
  612. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  613. {
  614. if (sleep)
  615. LOCPLINT->sleepingHeroes += hero;
  616. else
  617. LOCPLINT->sleepingHeroes -= hero;
  618. updateNextHero(NULL);
  619. }
  620. void CAdvMapInt::show(SDL_Surface * to)
  621. {
  622. if(state != INGAME)
  623. return;
  624. ++animValHitCount; //for animations
  625. if(animValHitCount == 8)
  626. {
  627. CGI->mh->updateWater();
  628. animValHitCount = 0;
  629. ++anim;
  630. updateScreen = true;
  631. }
  632. ++heroAnim;
  633. int scrollSpeed = settings["adventure"]["scrollSpeed"].Float();
  634. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  635. if((animValHitCount % (4/scrollSpeed)) == 0
  636. && (
  637. (GH.topInt() == this)
  638. || SDL_GetKeyState(NULL)[SDLK_LCTRL]
  639. || SDL_GetKeyState(NULL)[SDLK_RCTRL]
  640. )
  641. )
  642. {
  643. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  644. position.x--;
  645. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  646. position.x++;
  647. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  648. position.y--;
  649. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  650. position.y++;
  651. if(scrollingDir)
  652. {
  653. updateScreen = true;
  654. minimap.redraw();
  655. }
  656. }
  657. if(updateScreen)
  658. {
  659. terrain.show(to);
  660. for(int i=0;i<4;i++)
  661. blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  662. updateScreen=false;
  663. LOCPLINT->cingconsole->showAll(to);
  664. }
  665. infoBar.show(to);
  666. statusbar.showAll(to);
  667. }
  668. void CAdvMapInt::selectionChanged()
  669. {
  670. const CGTownInstance *to = LOCPLINT->towns[townList.getSelectedIndex()];
  671. if (selection != to)
  672. select(to);
  673. }
  674. void CAdvMapInt::centerOn(int3 on)
  675. {
  676. bool switchedLevels = on.z != position.z;
  677. on.x -= CGI->mh->frameW;
  678. on.y -= CGI->mh->frameH;
  679. on = LOCPLINT->repairScreenPos(on);
  680. position = on;
  681. updateScreen=true;
  682. underground.setIndex(on.z,true); //change underground switch button image
  683. underground.redraw();
  684. if (switchedLevels)
  685. minimap.setLevel(position.z);
  686. }
  687. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  688. {
  689. centerOn(obj->getSightCenter());
  690. }
  691. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  692. {
  693. ui8 Dir = 0;
  694. int k = key.keysym.sym;
  695. const CGHeroInstance *h = curHero(); //selected hero
  696. const CGTownInstance *t = curTown(); //selected town
  697. switch(k)
  698. {
  699. case SDLK_g:
  700. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  701. return;
  702. {
  703. //find first town with tavern
  704. auto itr = range::find_if(LOCPLINT->towns, boost::bind(&CGTownInstance::hasBuilt, _1, EBuilding::TAVERN));
  705. if(itr != LOCPLINT->towns.end())
  706. LOCPLINT->showThievesGuildWindow(*itr);
  707. else
  708. LOCPLINT->showInfoDialog("No available town with tavern!");
  709. }
  710. return;
  711. case SDLK_i:
  712. if(isActive())
  713. CAdventureOptions::showScenarioInfo();
  714. return;
  715. case SDLK_l:
  716. if(isActive())
  717. LOCPLINT->proposeLoadingGame();
  718. return;
  719. case SDLK_s:
  720. if(isActive())
  721. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  722. return;
  723. case SDLK_d:
  724. {
  725. if(h && isActive() && key.state == SDL_PRESSED)
  726. LOCPLINT->tryDiggging(h);
  727. return;
  728. }
  729. case SDLK_p:
  730. if(isActive())
  731. LOCPLINT->showPuzzleMap();
  732. return;
  733. case SDLK_r:
  734. if(isActive() && LOCPLINT->ctrlPressed())
  735. {
  736. LOCPLINT->showYesNoDialog("Are you sure you want to restart game?",
  737. []{ LOCPLINT->sendCustomEvent(RESTART_GAME); },
  738. []{}, true);
  739. }
  740. return;
  741. case SDLK_SPACE: //space - try to revisit current object with selected hero
  742. {
  743. if(!isActive())
  744. return;
  745. if(h && key.state == SDL_PRESSED)
  746. {
  747. auto unlockPim = vstd::makeUnlockGuard(*LOCPLINT->pim);
  748. LOCPLINT->cb->moveHero(h,h->pos);
  749. }
  750. }
  751. return;
  752. case SDLK_RETURN:
  753. {
  754. if(!isActive() || !selection || key.state != SDL_PRESSED)
  755. return;
  756. if(h)
  757. LOCPLINT->openHeroWindow(h);
  758. else if(t)
  759. LOCPLINT->openTownWindow(t);
  760. return;
  761. }
  762. case SDLK_ESCAPE:
  763. {
  764. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  765. return;
  766. leaveCastingMode();
  767. return;
  768. }
  769. case SDLK_t:
  770. {
  771. //act on key down if marketplace windows is not already opened
  772. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  773. return;
  774. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  775. {
  776. //check if we have any marketplace
  777. const CGTownInstance *townWithMarket = NULL;
  778. BOOST_FOREACH(const CGTownInstance *t, LOCPLINT->cb->getTownsInfo())
  779. {
  780. if(t->hasBuilt(EBuilding::MARKETPLACE))
  781. {
  782. townWithMarket = t;
  783. break;
  784. }
  785. }
  786. if(townWithMarket) //if any town has marketplace, open window
  787. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  788. else //if not - complain
  789. LOCPLINT->showInfoDialog("No available marketplace!");
  790. }
  791. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  792. {
  793. townList.selectNext();
  794. }
  795. return;
  796. }
  797. default:
  798. {
  799. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  800. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  801. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  802. //numpad arrow
  803. if(CGuiHandler::isArrowKey(SDLKey(k)))
  804. k = CGuiHandler::arrowToNum(SDLKey(k));
  805. k -= SDLK_KP0 + 1;
  806. if(k < 0 || k > 8)
  807. return;
  808. int3 dir = directions[k];
  809. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  810. {
  811. Dir = (dir.x<0 ? LEFT : 0) |
  812. (dir.x>0 ? RIGHT : 0) |
  813. (dir.y<0 ? UP : 0) |
  814. (dir.y>0 ? DOWN : 0) ;
  815. break;
  816. }
  817. if(!h || key.state != SDL_PRESSED)
  818. break;
  819. if(k == 4)
  820. {
  821. centerOn(h);
  822. return;
  823. }
  824. CGPath &path = LOCPLINT->paths[h];
  825. terrain.currentPath = &path;
  826. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  827. {
  828. terrain.currentPath = NULL;
  829. return;
  830. }
  831. if (path.nodes.size() > 2)
  832. updateMoveHero(h);
  833. else
  834. if(!path.nodes[0].turns)
  835. LOCPLINT->moveHero(h, path);
  836. }
  837. return;
  838. }
  839. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  840. && LOCPLINT->ctrlPressed()
  841. )
  842. scrollingDir |= Dir;
  843. else
  844. scrollingDir &= ~Dir;
  845. }
  846. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  847. {
  848. if(down)
  849. {
  850. CRClickPopup::createAndPush(text);
  851. }
  852. }
  853. int3 CAdvMapInt::verifyPos(int3 ver)
  854. {
  855. if (ver.x<0)
  856. ver.x=0;
  857. if (ver.y<0)
  858. ver.y=0;
  859. if (ver.z<0)
  860. ver.z=0;
  861. if (ver.x>=CGI->mh->sizes.x)
  862. ver.x=CGI->mh->sizes.x-1;
  863. if (ver.y>=CGI->mh->sizes.y)
  864. ver.y=CGI->mh->sizes.y-1;
  865. if (ver.z>=CGI->mh->sizes.z)
  866. ver.z=CGI->mh->sizes.z-1;
  867. return ver;
  868. }
  869. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  870. {
  871. assert(sel);
  872. LOCPLINT->cb->setSelection(sel);
  873. selection = sel;
  874. if (LOCPLINT->battleInt == NULL && LOCPLINT->makingTurn)
  875. {
  876. auto pos = sel->visitablePos();
  877. auto tile = LOCPLINT->cb->getTile(pos);
  878. if(tile)
  879. CCS->musich->playMusicFromSet("terrain", tile->terType, true);
  880. }
  881. if(centerView)
  882. centerOn(sel);
  883. terrain.currentPath = NULL;
  884. if(sel->ID==Obj::TOWN)
  885. {
  886. auto town = dynamic_cast<const CGTownInstance*>(sel);
  887. infoBar.showTownSelection(town);
  888. townList.select(town);
  889. heroList.select(nullptr);
  890. updateSleepWake(NULL);
  891. updateMoveHero(NULL);
  892. }
  893. else //hero selected
  894. {
  895. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  896. infoBar.showHeroSelection(hero);
  897. heroList.select(hero);
  898. townList.select(nullptr);
  899. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  900. updateSleepWake(hero);
  901. updateMoveHero(hero);
  902. }
  903. townList.redraw();
  904. heroList.redraw();
  905. }
  906. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  907. {
  908. //adventure map scrolling with mouse
  909. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && isActive())
  910. {
  911. if(sEvent.x<15)
  912. {
  913. scrollingDir |= LEFT;
  914. }
  915. else
  916. {
  917. scrollingDir &= ~LEFT;
  918. }
  919. if(sEvent.x>screen->w-15)
  920. {
  921. scrollingDir |= RIGHT;
  922. }
  923. else
  924. {
  925. scrollingDir &= ~RIGHT;
  926. }
  927. if(sEvent.y<15)
  928. {
  929. scrollingDir |= UP;
  930. }
  931. else
  932. {
  933. scrollingDir &= ~UP;
  934. }
  935. if(sEvent.y>screen->h-15)
  936. {
  937. scrollingDir |= DOWN;
  938. }
  939. else
  940. {
  941. scrollingDir &= ~DOWN;
  942. }
  943. }
  944. }
  945. bool CAdvMapInt::isActive()
  946. {
  947. return active & ~CIntObject::KEYBOARD;
  948. }
  949. void CAdvMapInt::startHotSeatWait(int Player)
  950. {
  951. state = WAITING;
  952. }
  953. void CAdvMapInt::setPlayer(int Player)
  954. {
  955. player = Player;
  956. graphics->blueToPlayersAdv(bg,player);
  957. kingOverview.setPlayerColor(player);
  958. underground.setPlayerColor(player);
  959. questlog.setPlayerColor(player);
  960. sleepWake.setPlayerColor(player);
  961. moveHero.setPlayerColor(player);
  962. spellbook.setPlayerColor(player);
  963. sysOptions.setPlayerColor(player);
  964. advOptions.setPlayerColor(player);
  965. nextHero.setPlayerColor(player);
  966. endTurn.setPlayerColor(player);
  967. graphics->blueToPlayersAdv(resdatabar.bg,player);
  968. //heroList.updateHList();
  969. //townList.genList();
  970. }
  971. void CAdvMapInt::startTurn()
  972. {
  973. state = INGAME;
  974. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID)
  975. {
  976. adjustActiveness(false);
  977. minimap.setAIRadar(false);
  978. }
  979. }
  980. void CAdvMapInt::endingTurn()
  981. {
  982. if(LOCPLINT->cingconsole->active)
  983. LOCPLINT->cingconsole->deactivate();
  984. LOCPLINT->makingTurn = false;
  985. LOCPLINT->cb->endTurn();
  986. }
  987. const CGObjectInstance* CAdvMapInt::getBlockingObject(const int3 &mapPos)
  988. {
  989. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  990. if (bobjs.empty())
  991. return nullptr;
  992. if (bobjs.back()->ID == Obj::HERO)
  993. return bobjs.back();
  994. else
  995. return bobjs.front();
  996. }
  997. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  998. {
  999. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  1000. return;
  1001. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos);//blocking objects at tile
  1002. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  1003. const CGObjectInstance *topBlocking = getBlockingObject(mapPos);
  1004. int3 selPos = selection->getSightCenter();
  1005. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1006. {
  1007. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1008. switch(spellBeingCasted->id)
  1009. {
  1010. case Spells::SCUTTLE_BOAT: //Scuttle Boat
  1011. if(topBlocking && topBlocking->ID == 8)
  1012. leaveCastingMode(true, mapPos);
  1013. break;
  1014. case Spells::DIMENSION_DOOR:
  1015. if(!tile || tile->isClear(heroTile))
  1016. leaveCastingMode(true, mapPos);
  1017. break;
  1018. }
  1019. return;
  1020. }
  1021. //check if we can select this object
  1022. bool canSelect = topBlocking && topBlocking->ID == Obj::HERO && topBlocking->tempOwner == LOCPLINT->playerID;
  1023. canSelect |= topBlocking && topBlocking->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1024. if (selection->ID != Obj::HERO) //hero is not selected (presumably town)
  1025. {
  1026. assert(!terrain.currentPath); //path can be active only when hero is selected
  1027. if(selection == topBlocking) //selected town clicked
  1028. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1029. else if ( canSelect )
  1030. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1031. return;
  1032. }
  1033. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1034. {
  1035. const CGPathNode *pn = LOCPLINT->cb->getPathInfo(mapPos);
  1036. if(currentHero == topBlocking) //clicked selected hero
  1037. {
  1038. LOCPLINT->openHeroWindow(currentHero);
  1039. return;
  1040. }
  1041. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1042. {
  1043. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1044. return;
  1045. }
  1046. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1047. {
  1048. if (terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1049. {
  1050. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1051. return;
  1052. }
  1053. 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
  1054. {
  1055. CGPath &path = LOCPLINT->paths[currentHero];
  1056. terrain.currentPath = &path;
  1057. bool gotPath = LOCPLINT->cb->getPath2(mapPos, path); //try getting path, erase if failed
  1058. updateMoveHero(currentHero);
  1059. if (!gotPath)
  1060. LOCPLINT->eraseCurrentPathOf(currentHero);
  1061. else
  1062. return;
  1063. }
  1064. }
  1065. } //end of hero is selected "case"
  1066. else
  1067. {
  1068. throw std::runtime_error("Nothing is selected...");
  1069. }
  1070. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1071. {
  1072. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1073. }
  1074. }
  1075. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1076. {
  1077. if(!LOCPLINT->cb->isVisible(mapPos))
  1078. {
  1079. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1080. statusbar.clear();
  1081. return;
  1082. }
  1083. const CGObjectInstance *objAtTile = getBlockingObject(mapPos);
  1084. //std::vector<std::string> temp = LOCPLINT->cb->getObjDescriptions(mapPos);
  1085. if (objAtTile)
  1086. {
  1087. std::string text = objAtTile->getHoverText();
  1088. boost::replace_all(text,"\n"," ");
  1089. statusbar.print(text);
  1090. }
  1091. else
  1092. {
  1093. std::string hlp;
  1094. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1095. statusbar.print(hlp);
  1096. }
  1097. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(mapPos);
  1098. int turns = pnode->turns;
  1099. vstd::amin(turns, 3);
  1100. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1101. return;
  1102. if(spellBeingCasted)
  1103. {
  1104. switch(spellBeingCasted->id)
  1105. {
  1106. case Spells::SCUTTLE_BOAT:
  1107. if(objAtTile && objAtTile->ID == Obj::BOAT)
  1108. CCS->curh->changeGraphic(ECursor::ADVENTURE, 42);
  1109. else
  1110. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1111. return;
  1112. case Spells::DIMENSION_DOOR:
  1113. {
  1114. const TerrainTile *t = LOCPLINT->cb->getTile(mapPos, false);
  1115. int3 hpos = selection->getSightCenter();
  1116. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1117. CCS->curh->changeGraphic(ECursor::ADVENTURE, 41);
  1118. else
  1119. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1120. return;
  1121. }
  1122. }
  1123. }
  1124. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->guardingCreaturePosition(mapPos));
  1125. if(selection->ID == Obj::TOWN)
  1126. {
  1127. if(objAtTile)
  1128. {
  1129. if(objAtTile->ID == Obj::TOWN && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner))
  1130. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1131. else if(objAtTile->ID == Obj::HERO && objAtTile->tempOwner == LOCPLINT->playerID)
  1132. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1133. else
  1134. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1135. }
  1136. else
  1137. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1138. }
  1139. else if(const CGHeroInstance *h = curHero())
  1140. {
  1141. bool accessible = pnode->turns < 255;
  1142. if(objAtTile)
  1143. {
  1144. if(objAtTile->ID == Obj::HERO)
  1145. {
  1146. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1147. {
  1148. if(accessible)
  1149. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1150. else
  1151. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1152. }
  1153. else //our or ally hero
  1154. {
  1155. if(selection == objAtTile)
  1156. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1157. else if(accessible)
  1158. CCS->curh->changeGraphic(ECursor::ADVENTURE, 8 + turns*6);
  1159. else
  1160. CCS->curh->changeGraphic(ECursor::ADVENTURE, 2);
  1161. }
  1162. }
  1163. else if(objAtTile->ID == Obj::TOWN)
  1164. {
  1165. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1166. {
  1167. if(accessible)
  1168. {
  1169. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1170. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1171. if (townObj && !townObj->armedGarrison())
  1172. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1173. else
  1174. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1175. }
  1176. else
  1177. {
  1178. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1179. }
  1180. }
  1181. else //our or ally town
  1182. {
  1183. if(accessible)
  1184. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1185. else
  1186. CCS->curh->changeGraphic(ECursor::ADVENTURE, 3);
  1187. }
  1188. }
  1189. else if(objAtTile->ID == Obj::BOAT)
  1190. {
  1191. if(accessible)
  1192. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1193. else
  1194. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1195. }
  1196. else if (objAtTile->ID == Obj::GARRISON || objAtTile->ID == Obj::GARRISON2)
  1197. {
  1198. if (accessible)
  1199. {
  1200. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1201. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1202. if (garrObj && garrObj->stacksCount()
  1203. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1204. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1205. else
  1206. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1207. }
  1208. else
  1209. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1210. }
  1211. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1212. {
  1213. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1214. }
  1215. else
  1216. {
  1217. if(accessible)
  1218. {
  1219. if(pnode->land)
  1220. CCS->curh->changeGraphic(ECursor::ADVENTURE, 9 + turns*6);
  1221. else
  1222. CCS->curh->changeGraphic(ECursor::ADVENTURE, 28 + turns);
  1223. }
  1224. else
  1225. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1226. }
  1227. }
  1228. else //no objs
  1229. {
  1230. if(accessible/* && pnode->accessible != CGPathNode::FLYABLE*/)
  1231. {
  1232. if (guardingCreature)
  1233. {
  1234. CCS->curh->changeGraphic(ECursor::ADVENTURE, 5 + turns*6);
  1235. }
  1236. else
  1237. {
  1238. if(pnode->land)
  1239. {
  1240. if(LOCPLINT->cb->getTile(h->getPosition(false))->terType != ETerrainType::WATER)
  1241. CCS->curh->changeGraphic(ECursor::ADVENTURE, 4 + turns*6);
  1242. else
  1243. CCS->curh->changeGraphic(ECursor::ADVENTURE, 7 + turns*6); //anchor
  1244. }
  1245. else
  1246. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6 + turns*6);
  1247. }
  1248. }
  1249. else
  1250. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  1251. }
  1252. }
  1253. if(ourInaccessibleShipyard(objAtTile))
  1254. {
  1255. CCS->curh->changeGraphic(ECursor::ADVENTURE, 6);
  1256. }
  1257. }
  1258. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1259. {
  1260. if(spellBeingCasted)
  1261. {
  1262. leaveCastingMode();
  1263. return;
  1264. }
  1265. if(!LOCPLINT->cb->isVisible(mapPos))
  1266. {
  1267. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1268. return;
  1269. }
  1270. const CGObjectInstance * obj = getBlockingObject(mapPos);
  1271. if(!obj)
  1272. {
  1273. // Bare or undiscovered terrain
  1274. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1275. if (tile)
  1276. {
  1277. std::string hlp;
  1278. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1279. CRClickPopup::createAndPush(hlp);
  1280. }
  1281. return;
  1282. }
  1283. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1284. }
  1285. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1286. {
  1287. using namespace Spells;
  1288. assert(sp->id == SCUTTLE_BOAT || sp->id == DIMENSION_DOOR);
  1289. spellBeingCasted = sp;
  1290. deactivate();
  1291. terrain.activate();
  1292. GH.fakeMouseMove();
  1293. }
  1294. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1295. {
  1296. assert(spellBeingCasted);
  1297. int id = spellBeingCasted->id;
  1298. spellBeingCasted = NULL;
  1299. terrain.deactivate();
  1300. activate();
  1301. if(cast)
  1302. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1303. else
  1304. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1305. }
  1306. const CGHeroInstance * CAdvMapInt::curHero() const
  1307. {
  1308. if(selection && selection->ID == Obj::HERO)
  1309. return static_cast<const CGHeroInstance *>(selection);
  1310. else
  1311. return NULL;
  1312. }
  1313. const CGTownInstance * CAdvMapInt::curTown() const
  1314. {
  1315. if(selection && selection->ID == Obj::TOWN)
  1316. return static_cast<const CGTownInstance *>(selection);
  1317. else
  1318. return NULL;
  1319. }
  1320. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1321. {
  1322. const IShipyard *ret = IShipyard::castFrom(obj);
  1323. if(!ret || obj->tempOwner != player || CCS->curh->type || (CCS->curh->frame != 6 && CCS->curh->frame != 0))
  1324. return NULL;
  1325. return ret;
  1326. }
  1327. void CAdvMapInt::aiTurnStarted()
  1328. {
  1329. adjustActiveness(true);
  1330. CCS->musich->playMusicFromSet("enemy-turn", true);
  1331. adventureInt->minimap.setAIRadar(true);
  1332. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1333. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1334. }
  1335. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1336. {
  1337. bool wasActive = isActive();
  1338. if(wasActive)
  1339. deactivate();
  1340. adventureInt->duringAITurn = aiTurnStart;
  1341. if(wasActive)
  1342. activate();
  1343. }
  1344. CAdventureOptions::CAdventureOptions():
  1345. CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1346. {
  1347. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1348. exit = new CAdventureMapButton("","",boost::bind(&CAdventureOptions::close, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1349. exit->assignedKeys.insert(SDLK_ESCAPE);
  1350. scenInfo = new CAdventureMapButton("","", boost::bind(&CAdventureOptions::close, this), 24, 198, "ADVINFO.DEF",SDLK_i);
  1351. scenInfo->callback += CAdventureOptions::showScenarioInfo;
  1352. //viewWorld = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1353. puzzle = new CAdventureMapButton("","", boost::bind(&CAdventureOptions::close, this), 24, 81, "ADVPUZ.DEF");
  1354. puzzle->callback += boost::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT);
  1355. dig = new CAdventureMapButton("","", boost::bind(&CAdventureOptions::close, this), 24, 139, "ADVDIG.DEF");
  1356. if(const CGHeroInstance *h = adventureInt->curHero())
  1357. dig->callback += boost::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h);
  1358. else
  1359. dig->block(true);
  1360. }
  1361. void CAdventureOptions::showScenarioInfo()
  1362. {
  1363. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1364. }