CAdvmapInterface.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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 "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.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(0, 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(0,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::Cornsilk,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::Cornsilk,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. activateKeys();
  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(0,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. select(to);
  672. }
  673. void CAdvMapInt::centerOn(int3 on)
  674. {
  675. bool switchedLevels = on.z != position.z;
  676. on.x -= CGI->mh->frameW;
  677. on.y -= CGI->mh->frameH;
  678. on = LOCPLINT->repairScreenPos(on);
  679. position = on;
  680. updateScreen=true;
  681. underground.setIndex(on.z,true); //change underground switch button image
  682. underground.redraw();
  683. if (switchedLevels)
  684. minimap.setLevel(position.z);
  685. }
  686. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  687. {
  688. centerOn(obj->getSightCenter());
  689. }
  690. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  691. {
  692. ui8 Dir = 0;
  693. int k = key.keysym.sym;
  694. const CGHeroInstance *h = curHero(); //selected hero
  695. const CGTownInstance *t = curTown(); //selected town
  696. switch(k)
  697. {
  698. case SDLK_g:
  699. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  700. return;
  701. {
  702. //find first town with tavern
  703. auto itr = range::find_if(LOCPLINT->towns, boost::bind(&CGTownInstance::hasBuilt, _1, EBuilding::TAVERN));
  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. LOCPLINT->cb->moveHero(h,h->pos);
  748. }
  749. }
  750. return;
  751. case SDLK_RETURN:
  752. {
  753. if(!isActive() || !selection || key.state != SDL_PRESSED)
  754. return;
  755. if(h)
  756. LOCPLINT->openHeroWindow(h);
  757. else if(t)
  758. LOCPLINT->openTownWindow(t);
  759. return;
  760. }
  761. case SDLK_ESCAPE:
  762. {
  763. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  764. return;
  765. leaveCastingMode();
  766. return;
  767. }
  768. case SDLK_t:
  769. {
  770. //act on key down if marketplace windows is not already opened
  771. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS)
  772. return;
  773. if(LOCPLINT->ctrlPressed()) //CTRL + T => open marketplace
  774. {
  775. //check if we have any marketplace
  776. const CGTownInstance *townWithMarket = NULL;
  777. BOOST_FOREACH(const CGTownInstance *t, LOCPLINT->cb->getTownsInfo())
  778. {
  779. if(vstd::contains(t->builtBuildings, 14))
  780. {
  781. townWithMarket = t;
  782. break;
  783. }
  784. }
  785. if(townWithMarket) //if any town has marketplace, open window
  786. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  787. else //if not - complain
  788. LOCPLINT->showInfoDialog("No available marketplace!");
  789. }
  790. else if(isActive()) //no ctrl, advmapint is on the top => switch to town
  791. {
  792. townList.selectNext();
  793. }
  794. return;
  795. }
  796. default:
  797. {
  798. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  799. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  800. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  801. //numpad arrow
  802. if(CGuiHandler::isArrowKey(SDLKey(k)))
  803. k = CGuiHandler::arrowToNum(SDLKey(k));
  804. k -= SDLK_KP0 + 1;
  805. if(k < 0 || k > 8)
  806. return;
  807. int3 dir = directions[k];
  808. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  809. {
  810. Dir = (dir.x<0 ? LEFT : 0) |
  811. (dir.x>0 ? RIGHT : 0) |
  812. (dir.y<0 ? UP : 0) |
  813. (dir.y>0 ? DOWN : 0) ;
  814. break;
  815. }
  816. if(!h || key.state != SDL_PRESSED)
  817. break;
  818. if(k == 4)
  819. {
  820. centerOn(h);
  821. return;
  822. }
  823. CGPath &path = LOCPLINT->paths[h];
  824. terrain.currentPath = &path;
  825. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  826. {
  827. terrain.currentPath = NULL;
  828. return;
  829. }
  830. if (path.nodes.size() > 2)
  831. updateMoveHero(h);
  832. else
  833. if(!path.nodes[0].turns)
  834. LOCPLINT->moveHero(h, path);
  835. }
  836. return;
  837. }
  838. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  839. && LOCPLINT->ctrlPressed()
  840. )
  841. scrollingDir |= Dir;
  842. else
  843. scrollingDir &= ~Dir;
  844. }
  845. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  846. {
  847. if(down)
  848. {
  849. CRClickPopup::createAndPush(text);
  850. }
  851. }
  852. int3 CAdvMapInt::verifyPos(int3 ver)
  853. {
  854. if (ver.x<0)
  855. ver.x=0;
  856. if (ver.y<0)
  857. ver.y=0;
  858. if (ver.z<0)
  859. ver.z=0;
  860. if (ver.x>=CGI->mh->sizes.x)
  861. ver.x=CGI->mh->sizes.x-1;
  862. if (ver.y>=CGI->mh->sizes.y)
  863. ver.y=CGI->mh->sizes.y-1;
  864. if (ver.z>=CGI->mh->sizes.z)
  865. ver.z=CGI->mh->sizes.z-1;
  866. return ver;
  867. }
  868. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  869. {
  870. assert(sel);
  871. LOCPLINT->cb->setSelection(sel);
  872. selection = sel;
  873. if (LOCPLINT->battleInt == NULL && active & GENERAL)
  874. CCS->musich->playMusic(CCS->musich->terrainMusics[LOCPLINT->cb->getTile(sel->visitablePos())->tertype], -1);
  875. if(centerView)
  876. centerOn(sel);
  877. terrain.currentPath = NULL;
  878. if(sel->ID==GameConstants::TOWNI_TYPE)
  879. {
  880. auto town = dynamic_cast<const CGTownInstance*>(sel);
  881. infoBar.showTownSelection(town);
  882. townList.select(town);
  883. heroList.select(nullptr);
  884. updateSleepWake(NULL);
  885. updateMoveHero(NULL);
  886. }
  887. else //hero selected
  888. {
  889. auto hero = dynamic_cast<const CGHeroInstance*>(sel);
  890. infoBar.showHeroSelection(hero);
  891. heroList.select(hero);
  892. townList.select(nullptr);
  893. terrain.currentPath = LOCPLINT->getAndVerifyPath(hero);
  894. updateSleepWake(hero);
  895. updateMoveHero(hero);
  896. }
  897. townList.redraw();
  898. heroList.redraw();
  899. }
  900. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  901. {
  902. //adventure map scrolling with mouse
  903. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && isActive())
  904. {
  905. if(sEvent.x<15)
  906. {
  907. scrollingDir |= LEFT;
  908. }
  909. else
  910. {
  911. scrollingDir &= ~LEFT;
  912. }
  913. if(sEvent.x>screen->w-15)
  914. {
  915. scrollingDir |= RIGHT;
  916. }
  917. else
  918. {
  919. scrollingDir &= ~RIGHT;
  920. }
  921. if(sEvent.y<15)
  922. {
  923. scrollingDir |= UP;
  924. }
  925. else
  926. {
  927. scrollingDir &= ~UP;
  928. }
  929. if(sEvent.y>screen->h-15)
  930. {
  931. scrollingDir |= DOWN;
  932. }
  933. else
  934. {
  935. scrollingDir &= ~DOWN;
  936. }
  937. }
  938. }
  939. bool CAdvMapInt::isActive()
  940. {
  941. return active & ~CIntObject::KEYBOARD;
  942. }
  943. void CAdvMapInt::startHotSeatWait(int Player)
  944. {
  945. state = WAITING;
  946. }
  947. void CAdvMapInt::setPlayer(int Player)
  948. {
  949. player = Player;
  950. graphics->blueToPlayersAdv(bg,player);
  951. kingOverview.setPlayerColor(player);
  952. underground.setPlayerColor(player);
  953. questlog.setPlayerColor(player);
  954. sleepWake.setPlayerColor(player);
  955. moveHero.setPlayerColor(player);
  956. spellbook.setPlayerColor(player);
  957. sysOptions.setPlayerColor(player);
  958. advOptions.setPlayerColor(player);
  959. nextHero.setPlayerColor(player);
  960. endTurn.setPlayerColor(player);
  961. graphics->blueToPlayersAdv(resdatabar.bg,player);
  962. //heroList.updateHList();
  963. //townList.genList();
  964. }
  965. void CAdvMapInt::startTurn()
  966. {
  967. state = INGAME;
  968. if(LOCPLINT->cb->getCurrentPlayer() == LOCPLINT->playerID)
  969. {
  970. adjustActiveness(false);
  971. minimap.setAIRadar(false);
  972. }
  973. }
  974. void CAdvMapInt::endingTurn()
  975. {
  976. if(LOCPLINT->cingconsole->active)
  977. LOCPLINT->cingconsole->deactivate();
  978. LOCPLINT->makingTurn = false;
  979. LOCPLINT->cb->endTurn();
  980. }
  981. const CGObjectInstance* CAdvMapInt::getBlockingObject(const int3 &mapPos)
  982. {
  983. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos); //blocking objects at tile
  984. if (bobjs.empty())
  985. return nullptr;
  986. if (bobjs.back()->ID == GameConstants::HEROI_TYPE)
  987. return bobjs.back();
  988. else
  989. return bobjs.front();
  990. }
  991. void CAdvMapInt::tileLClicked(const int3 &mapPos)
  992. {
  993. if(!LOCPLINT->cb->isVisible(mapPos) || !LOCPLINT->makingTurn)
  994. return;
  995. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mapPos);//blocking objects at tile
  996. const TerrainTile *tile = LOCPLINT->cb->getTile(mapPos);
  997. const CGObjectInstance *topBlocking = getBlockingObject(mapPos);
  998. int3 selPos = selection->getSightCenter();
  999. if(spellBeingCasted && isInScreenRange(selPos, mapPos))
  1000. {
  1001. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1002. switch(spellBeingCasted->id)
  1003. {
  1004. case Spells::SCUTTLE_BOAT: //Scuttle Boat
  1005. if(topBlocking && topBlocking->ID == 8)
  1006. leaveCastingMode(true, mapPos);
  1007. break;
  1008. case Spells::DIMENSION_DOOR:
  1009. if(!tile || tile->isClear(heroTile))
  1010. leaveCastingMode(true, mapPos);
  1011. break;
  1012. }
  1013. return;
  1014. }
  1015. //check if we can select this object
  1016. bool canSelect = topBlocking && topBlocking->ID == GameConstants::HEROI_TYPE && topBlocking->tempOwner == LOCPLINT->playerID;
  1017. canSelect |= topBlocking && topBlocking->ID == GameConstants::TOWNI_TYPE && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1018. if (selection->ID != GameConstants::HEROI_TYPE) //hero is not selected (presumably town)
  1019. {
  1020. assert(!terrain.currentPath); //path can be active only when hero is selected
  1021. if(selection == topBlocking) //selected town clicked
  1022. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1023. else if ( canSelect )
  1024. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1025. return;
  1026. }
  1027. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1028. {
  1029. const CGPathNode *pn = LOCPLINT->cb->getPathInfo(mapPos);
  1030. if(currentHero == topBlocking) //clicked selected hero
  1031. {
  1032. LOCPLINT->openHeroWindow(currentHero);
  1033. return;
  1034. }
  1035. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1036. {
  1037. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1038. return;
  1039. }
  1040. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1041. {
  1042. if (terrain.currentPath && terrain.currentPath->endPos() == mapPos)//we'll be moving
  1043. {
  1044. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1045. return;
  1046. }
  1047. 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
  1048. {
  1049. CGPath &path = LOCPLINT->paths[currentHero];
  1050. terrain.currentPath = &path;
  1051. bool gotPath = LOCPLINT->cb->getPath2(mapPos, path); //try getting path, erase if failed
  1052. updateMoveHero(currentHero);
  1053. if (!gotPath)
  1054. LOCPLINT->eraseCurrentPathOf(currentHero);
  1055. else
  1056. return;
  1057. }
  1058. }
  1059. } //end of hero is selected "case"
  1060. else
  1061. {
  1062. throw std::runtime_error("Nothing is selected...");
  1063. }
  1064. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1065. {
  1066. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1067. }
  1068. }
  1069. void CAdvMapInt::tileHovered(const int3 &mapPos)
  1070. {
  1071. if(!LOCPLINT->cb->isVisible(mapPos))
  1072. {
  1073. CCS->curh->changeGraphic(0, 0);
  1074. statusbar.clear();
  1075. return;
  1076. }
  1077. const CGObjectInstance *objAtTile = getBlockingObject(mapPos);
  1078. //std::vector<std::string> temp = LOCPLINT->cb->getObjDescriptions(mapPos);
  1079. if (objAtTile)
  1080. {
  1081. std::string text = objAtTile->getHoverText();
  1082. boost::replace_all(text,"\n"," ");
  1083. statusbar.print(text);
  1084. }
  1085. else
  1086. {
  1087. std::string hlp;
  1088. CGI->mh->getTerrainDescr(mapPos, hlp, false);
  1089. statusbar.print(hlp);
  1090. }
  1091. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(mapPos);
  1092. int turns = pnode->turns;
  1093. vstd::amin(turns, 3);
  1094. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1095. return;
  1096. if(spellBeingCasted)
  1097. {
  1098. switch(spellBeingCasted->id)
  1099. {
  1100. case Spells::SCUTTLE_BOAT:
  1101. if(objAtTile && objAtTile->ID == 8)
  1102. CCS->curh->changeGraphic(0, 42);
  1103. else
  1104. CCS->curh->changeGraphic(0, 0);
  1105. return;
  1106. case Spells::DIMENSION_DOOR:
  1107. {
  1108. const TerrainTile *t = LOCPLINT->cb->getTile(mapPos, false);
  1109. int3 hpos = selection->getSightCenter();
  1110. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, mapPos))
  1111. CCS->curh->changeGraphic(0, 41);
  1112. else
  1113. CCS->curh->changeGraphic(0, 0);
  1114. return;
  1115. }
  1116. }
  1117. }
  1118. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->guardingCreaturePosition(mapPos));
  1119. if(selection->ID == GameConstants::TOWNI_TYPE)
  1120. {
  1121. if(objAtTile)
  1122. {
  1123. if(objAtTile->ID == GameConstants::TOWNI_TYPE && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner))
  1124. CCS->curh->changeGraphic(0, 3);
  1125. else if(objAtTile->ID == GameConstants::HEROI_TYPE && objAtTile->tempOwner == LOCPLINT->playerID)
  1126. CCS->curh->changeGraphic(0, 2);
  1127. else
  1128. CCS->curh->changeGraphic(0, 0);
  1129. }
  1130. else
  1131. CCS->curh->changeGraphic(0, 0);
  1132. }
  1133. else if(const CGHeroInstance *h = curHero())
  1134. {
  1135. bool accessible = pnode->turns < 255;
  1136. if(objAtTile)
  1137. {
  1138. if(objAtTile->ID == GameConstants::HEROI_TYPE)
  1139. {
  1140. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1141. {
  1142. if(accessible)
  1143. CCS->curh->changeGraphic(0, 5 + turns*6);
  1144. else
  1145. CCS->curh->changeGraphic(0, 0);
  1146. }
  1147. else //our or ally hero
  1148. {
  1149. if(selection == objAtTile)
  1150. CCS->curh->changeGraphic(0, 2);
  1151. else if(accessible)
  1152. CCS->curh->changeGraphic(0, 8 + turns*6);
  1153. else
  1154. CCS->curh->changeGraphic(0, 2);
  1155. }
  1156. }
  1157. else if(objAtTile->ID == GameConstants::TOWNI_TYPE)
  1158. {
  1159. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1160. {
  1161. if(accessible)
  1162. {
  1163. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1164. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1165. if (townObj && !townObj->armedGarrison())
  1166. CCS->curh->changeGraphic(0, 9 + turns*6);
  1167. else
  1168. CCS->curh->changeGraphic(0, 5 + turns*6);
  1169. }
  1170. else
  1171. {
  1172. CCS->curh->changeGraphic(0, 0);
  1173. }
  1174. }
  1175. else //our or ally town
  1176. {
  1177. if(accessible)
  1178. CCS->curh->changeGraphic(0, 9 + turns*6);
  1179. else
  1180. CCS->curh->changeGraphic(0, 3);
  1181. }
  1182. }
  1183. else if(objAtTile->ID == 8) //boat
  1184. {
  1185. if(accessible)
  1186. CCS->curh->changeGraphic(0, 6 + turns*6);
  1187. else
  1188. CCS->curh->changeGraphic(0, 0);
  1189. }
  1190. else if (objAtTile->ID == 33 || objAtTile->ID == 219) // Garrison
  1191. {
  1192. if (accessible)
  1193. {
  1194. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1195. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1196. if (garrObj && garrObj->stacksCount()
  1197. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1198. CCS->curh->changeGraphic(0, 5 + turns*6);
  1199. else
  1200. CCS->curh->changeGraphic(0, 9 + turns*6);
  1201. }
  1202. else
  1203. CCS->curh->changeGraphic(0, 0);
  1204. }
  1205. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1206. {
  1207. CCS->curh->changeGraphic(0, 5 + turns*6);
  1208. }
  1209. else
  1210. {
  1211. if(accessible)
  1212. {
  1213. if(pnode->land)
  1214. CCS->curh->changeGraphic(0, 9 + turns*6);
  1215. else
  1216. CCS->curh->changeGraphic(0, 28 + turns);
  1217. }
  1218. else
  1219. CCS->curh->changeGraphic(0, 0);
  1220. }
  1221. }
  1222. else //no objs
  1223. {
  1224. if(accessible/* && pnode->accessible != CGPathNode::FLYABLE*/)
  1225. {
  1226. if (guardingCreature)
  1227. {
  1228. CCS->curh->changeGraphic(0, 5 + turns*6);
  1229. } else
  1230. {
  1231. if(pnode->land)
  1232. {
  1233. if(LOCPLINT->cb->getTile(h->getPosition(false))->tertype != TerrainTile::water)
  1234. CCS->curh->changeGraphic(0, 4 + turns*6);
  1235. else
  1236. CCS->curh->changeGraphic(0, 7 + turns*6); //anchor
  1237. }
  1238. else
  1239. CCS->curh->changeGraphic(0, 6 + turns*6);
  1240. }
  1241. }
  1242. else
  1243. CCS->curh->changeGraphic(0, 0);
  1244. }
  1245. }
  1246. if(ourInaccessibleShipyard(objAtTile))
  1247. {
  1248. CCS->curh->changeGraphic(0, 6);
  1249. }
  1250. }
  1251. void CAdvMapInt::tileRClicked(const int3 &mapPos)
  1252. {
  1253. if(spellBeingCasted)
  1254. {
  1255. leaveCastingMode();
  1256. return;
  1257. }
  1258. if(!LOCPLINT->cb->isVisible(mapPos))
  1259. {
  1260. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1261. return;
  1262. }
  1263. const CGObjectInstance * obj = getBlockingObject(mapPos);
  1264. if(!obj)
  1265. {
  1266. // Bare or undiscovered terrain
  1267. const TerrainTile * tile = LOCPLINT->cb->getTile(mapPos);
  1268. if (tile)
  1269. {
  1270. std::string hlp;
  1271. CGI->mh->getTerrainDescr(mapPos, hlp, true);
  1272. CRClickPopup::createAndPush(hlp);
  1273. }
  1274. return;
  1275. }
  1276. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1277. }
  1278. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1279. {
  1280. using namespace Spells;
  1281. assert(sp->id == SCUTTLE_BOAT || sp->id == DIMENSION_DOOR);
  1282. spellBeingCasted = sp;
  1283. deactivate();
  1284. terrain.activate();
  1285. GH.fakeMouseMove();
  1286. }
  1287. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1288. {
  1289. assert(spellBeingCasted);
  1290. int id = spellBeingCasted->id;
  1291. spellBeingCasted = NULL;
  1292. terrain.deactivate();
  1293. activate();
  1294. if(cast)
  1295. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1296. else
  1297. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1298. }
  1299. const CGHeroInstance * CAdvMapInt::curHero() const
  1300. {
  1301. if(selection && selection->ID == GameConstants::HEROI_TYPE)
  1302. return static_cast<const CGHeroInstance *>(selection);
  1303. else
  1304. return NULL;
  1305. }
  1306. const CGTownInstance * CAdvMapInt::curTown() const
  1307. {
  1308. if(selection && selection->ID == GameConstants::TOWNI_TYPE)
  1309. return static_cast<const CGTownInstance *>(selection);
  1310. else
  1311. return NULL;
  1312. }
  1313. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1314. {
  1315. const IShipyard *ret = IShipyard::castFrom(obj);
  1316. if(!ret || obj->tempOwner != player || CCS->curh->mode || (CCS->curh->number != 6 && CCS->curh->number != 0))
  1317. return NULL;
  1318. return ret;
  1319. }
  1320. void CAdvMapInt::aiTurnStarted()
  1321. {
  1322. adjustActiveness(true);
  1323. CCS->musich->playMusicFromSet(CCS->musich->aiMusics);
  1324. adventureInt->minimap.setAIRadar(true);
  1325. adventureInt->infoBar.startEnemyTurn(LOCPLINT->cb->getCurrentPlayer());
  1326. adventureInt->infoBar.showAll(screen);//force refresh on inactive object
  1327. }
  1328. void CAdvMapInt::adjustActiveness(bool aiTurnStart)
  1329. {
  1330. bool wasActive = isActive();
  1331. if(wasActive)
  1332. deactivate();
  1333. adventureInt->duringAITurn = aiTurnStart;
  1334. if(wasActive)
  1335. activate();
  1336. }
  1337. CAdventureOptions::CAdventureOptions():
  1338. CWindowObject(PLAYER_COLORED, "ADVOPTS")
  1339. {
  1340. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1341. exit = new CAdventureMapButton("","",boost::bind(&CAdventureOptions::close, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1342. exit->assignedKeys.insert(SDLK_ESCAPE);
  1343. scenInfo = new CAdventureMapButton("","", boost::bind(&CAdventureOptions::close, this), 24, 198, "ADVINFO.DEF",SDLK_i);
  1344. scenInfo->callback += CAdventureOptions::showScenarioInfo;
  1345. //viewWorld = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1346. puzzle = new CAdventureMapButton("","", boost::bind(&CAdventureOptions::close, this), 24, 81, "ADVPUZ.DEF");
  1347. puzzle->callback += boost::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT);
  1348. dig = new CAdventureMapButton("","", boost::bind(&CAdventureOptions::close, this), 24, 139, "ADVDIG.DEF");
  1349. if(const CGHeroInstance *h = adventureInt->curHero())
  1350. dig->callback += boost::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h);
  1351. else
  1352. dig->block(true);
  1353. }
  1354. void CAdventureOptions::showScenarioInfo()
  1355. {
  1356. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1357. }