CAdvmapInterface.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. #include "AdventureMapButton.h"
  2. #include "CAdvmapInterface.h"
  3. #include "../CCallback.h"
  4. #include "CCastleInterface.h"
  5. #include "CCursorHandler.h"
  6. #include "CGameInfo.h"
  7. #include "CHeroWindow.h"
  8. #include "CKingdomInterface.h"
  9. #include "CMessage.h"
  10. #include "CPlayerInterface.h"
  11. #include "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 "mapHandler.h"
  23. #include "../stdafx.h"
  24. #include <boost/algorithm/string.hpp>
  25. #include <boost/algorithm/string/replace.hpp>
  26. #include <boost/assign/std/vector.hpp>
  27. #include <boost/thread.hpp>
  28. #include <sstream>
  29. #include "CPreGame.h"
  30. #include "../lib/VCMI_Lib.h"
  31. #include "../lib/CSpellHandler.h"
  32. #include <boost/foreach.hpp>
  33. #include "CSoundBase.h"
  34. #include "../lib/CGameState.h"
  35. #ifdef _MSC_VER
  36. #pragma warning (disable : 4355)
  37. #endif
  38. /*
  39. * CAdvMapInterface.cpp, part of VCMI engine
  40. *
  41. * Authors: listed in file AUTHORS in main folder
  42. *
  43. * License: GNU General Public License v2.0 or later
  44. * Full text of license available in license.txt file, in main folder
  45. *
  46. */
  47. #define ADVOPT (conf.go()->ac)
  48. using namespace boost::logic;
  49. using namespace boost::assign;
  50. using namespace CSDL_Ext;
  51. CAdvMapInt *adventureInt;
  52. CMinimap::CMinimap(bool draw)
  53. {
  54. int3 mapSizes = LOCPLINT->cb->getMapSize();
  55. statusbarTxt = CGI->generaltexth->zelp[291].first;
  56. rcText = CGI->generaltexth->zelp[291].second;
  57. pos.x=ADVOPT.minimapX;//630
  58. pos.y=ADVOPT.minimapY;//26
  59. pos.h=ADVOPT.minimapW;//144
  60. pos.w=ADVOPT.minimapH;//144
  61. temps = newSurface(pos.w,pos.h);
  62. std::ifstream is(DATA_DIR "/config/minimap.txt",std::ifstream::in);
  63. for (int i=0;i<TERRAIN_TYPES;i++)
  64. {
  65. std::pair<int,SDL_Color> vinya;
  66. std::pair<int,SDL_Color> vinya2;
  67. int pom;
  68. is >> pom;
  69. vinya2.first=vinya.first=pom;
  70. is >> pom;
  71. vinya.second.r=pom;
  72. is >> pom;
  73. vinya.second.g=pom;
  74. is >> pom;
  75. vinya.second.b=pom;
  76. is >> pom;
  77. vinya2.second.r=pom;
  78. is >> pom;
  79. vinya2.second.g=pom;
  80. is >> pom;
  81. vinya2.second.b=pom;
  82. vinya.second.unused=vinya2.second.unused=255;
  83. colors.insert(vinya);
  84. colorsBlocked.insert(vinya2);
  85. }
  86. is.close();
  87. if (draw)
  88. redraw();
  89. }
  90. CMinimap::~CMinimap()
  91. {
  92. SDL_FreeSurface(temps);
  93. for(int g=0; g<map.size(); ++g)
  94. SDL_FreeSurface(map[g]);
  95. map.clear();
  96. for(int g=0; g<FoW.size(); ++g)
  97. SDL_FreeSurface(FoW[g]);
  98. FoW.clear();
  99. for(int g=0; g<flObjs.size(); ++g)
  100. SDL_FreeSurface(flObjs[g]);
  101. flObjs.clear();
  102. }
  103. void CMinimap::draw(SDL_Surface * to)
  104. {
  105. int3 mapSizes = LOCPLINT->cb->getMapSize();
  106. //draw terrain
  107. blitAt(map[adventureInt->position.z],0,0,temps);
  108. //draw heroes
  109. std::vector <const CGHeroInstance *> hh = LOCPLINT->cb->getHeroesInfo(false);
  110. int mw = map[0]->w, mh = map[0]->h,
  111. wo = mw/mapSizes.x, ho = mh/mapSizes.y;
  112. for (size_t i=0; i < hh.size(); ++i)
  113. {
  114. int3 hpos = hh[i]->getPosition(false);
  115. if(hpos.z!=adventureInt->position.z)
  116. continue;
  117. //float zawx = ((float)hpos.x/CGI->mh->sizes.x), zawy = ((float)hpos.y/CGI->mh->sizes.y);
  118. int3 maplgp ( (hpos.x*mw)/mapSizes.x, (hpos.y*mh)/mapSizes.y, hpos.z );
  119. for (int ii=0; ii<wo; ii++)
  120. {
  121. for (int jj=0; jj<ho; jj++)
  122. {
  123. SDL_PutPixelWithoutRefresh(temps,maplgp.x+ii,maplgp.y+jj,graphics->playerColors[hh[i]->getOwner()].r,
  124. graphics->playerColors[hh[i]->getOwner()].g,graphics->playerColors[hh[i]->getOwner()].b);
  125. }
  126. }
  127. }
  128. blitAt(flObjs[adventureInt->position.z],0,0,temps);
  129. blitAt(FoW[adventureInt->position.z],0,0,temps);
  130. //draw radar
  131. const int tilesw=(ADVOPT.advmapW+31)/32;
  132. const int tilesh=(ADVOPT.advmapH+31)/32;
  133. int bx = (((float)adventureInt->position.x)/(((float)mapSizes.x)))*pos.w,
  134. by = (((float)adventureInt->position.y)/(((float)mapSizes.y)))*pos.h,
  135. rx = (((float)tilesw)/(mapSizes.x))*((float)pos.w), //width
  136. ry = (((float)tilesh)/(mapSizes.y))*((float)pos.h); //height
  137. CSDL_Ext::drawDashedBorder(temps, Rect(bx, by, rx, ry), int3(255,75,125));
  138. //blitAt(radar,bx,by,temps);
  139. blitAt(temps,pos.x,pos.y,to);
  140. }
  141. void CMinimap::redraw(int level)// (level==-1) => redraw all levels
  142. {
  143. initMap(level);
  144. //FoW
  145. initFoW(level);
  146. //flaggable objects
  147. initFlaggableObjs(level);
  148. //showing tiles
  149. showVisibleTiles();
  150. }
  151. void CMinimap::initMap(int level)
  152. {
  153. /*for(int g=0; g<map.size(); ++g)
  154. {
  155. SDL_FreeSurface(map[g]);
  156. }
  157. map.clear();*/
  158. int3 mapSizes = LOCPLINT->cb->getMapSize();
  159. for (size_t i=0; i<CGI->mh->sizes.z; i++)
  160. {
  161. SDL_Surface * pom ;
  162. if ((level>=0) && (i!=level))
  163. continue;
  164. if (map.size()<i+1)
  165. pom = CSDL_Ext::newSurface(pos.w,pos.h,screen);
  166. else pom = map[i];
  167. for (int x=0;x<pos.w;x++)
  168. {
  169. for (int y=0;y<pos.h;y++)
  170. {
  171. int mx=(mapSizes.x*x)/pos.w;
  172. int my=(mapSizes.y*y)/pos.h;
  173. const TerrainTile * tile = LOCPLINT->cb->getTile(int3(mx, my, i), false);
  174. if(tile)
  175. {
  176. if (tile->blocked && (!tile->visitable))
  177. SDL_PutPixelWithoutRefresh(pom, x, y, colorsBlocked[tile->tertype].r, colorsBlocked[tile->tertype].g, colorsBlocked[tile->tertype].b);
  178. else SDL_PutPixelWithoutRefresh(pom, x, y, colors[tile->tertype].r, colors[tile->tertype].g, colors[tile->tertype].b);
  179. }
  180. }
  181. }
  182. map.push_back(pom);
  183. }
  184. }
  185. void CMinimap::initFoW(int level)
  186. {
  187. /*for(int g=0; g<FoW.size(); ++g)
  188. {
  189. SDL_FreeSurface(FoW[g]);
  190. }
  191. FoW.clear();*/
  192. int3 mapSizes = LOCPLINT->cb->getMapSize();
  193. int mw = map[0]->w, mh = map[0]->h;//,
  194. //wo = mw/mapSizes.x, ho = mh/mapSizes.y; //TODO use me
  195. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  196. {
  197. if(level>=0 && d!=level)
  198. continue;
  199. SDL_Surface * pt = CSDL_Ext::newSurface(pos.w, pos.h, CSDL_Ext::std32bppSurface);
  200. for (int i=0; i<mw; i++)
  201. {
  202. for (int j=0; j<mh; j++)
  203. {
  204. int3 pp( ((i*mapSizes.x)/mw), ((j*mapSizes.y)/mh), d );
  205. if ( !LOCPLINT->cb->isVisible(pp) )
  206. {
  207. CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0);
  208. }
  209. }
  210. }
  211. FoW.push_back(pt);
  212. }
  213. }
  214. void CMinimap::initFlaggableObjs(int level)
  215. {
  216. /*for(int g=0; g<flObjs.size(); ++g)
  217. {
  218. SDL_FreeSurface(flObjs[g]);
  219. }
  220. flObjs.clear();*/
  221. int3 mapSizes = LOCPLINT->cb->getMapSize();
  222. int mw = map[0]->w, mh = map[0]->h;
  223. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  224. {
  225. if(level>=0 && d!=level)
  226. continue;
  227. SDL_Surface * pt = CSDL_Ext::newSurface(pos.w, pos.h, CSDL_Ext::std32bppSurface);
  228. for (int i=0; i<mw; i++)
  229. {
  230. for (int j=0; j<mh; j++)
  231. {
  232. CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0,0);
  233. }
  234. }
  235. flObjs.push_back(pt);
  236. }
  237. }
  238. void CMinimap::updateRadar()
  239. {}
  240. void CMinimap::clickRight(tribool down, bool previousState)
  241. {
  242. adventureInt->handleRightClick(rcText,down);
  243. }
  244. void CMinimap::clickLeft(tribool down, bool previousState)
  245. {
  246. if (down && (!previousState))
  247. activateMouseMove();
  248. else if (!down)
  249. {
  250. if (std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this)!=GH.motioninterested.end())
  251. deactivateMouseMove();
  252. }
  253. //ClickableL::clickLeft(down);
  254. if (!((bool)down))
  255. return;
  256. float dx=((float)(GH.current->motion.x-pos.x))/((float)pos.w),
  257. dy=((float)(GH.current->motion.y-pos.y))/((float)pos.h);
  258. int3 newCPos;
  259. newCPos.x = (CGI->mh->sizes.x*dx);
  260. newCPos.y = (CGI->mh->sizes.y*dy);
  261. newCPos.z = adventureInt->position.z;
  262. adventureInt->centerOn(newCPos);
  263. }
  264. void CMinimap::hover (bool on)
  265. {
  266. //Hoverable::hover(on);
  267. if (on)
  268. adventureInt->statusbar.print(statusbarTxt);
  269. else if (adventureInt->statusbar.current==statusbarTxt)
  270. adventureInt->statusbar.clear();
  271. }
  272. void CMinimap::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  273. {
  274. if (pressedL)
  275. {
  276. clickLeft(true, true);
  277. }
  278. }
  279. void CMinimap::activate()
  280. {
  281. activateLClick();
  282. activateRClick();
  283. activateHover();
  284. if (pressedL)
  285. activateMouseMove();
  286. }
  287. void CMinimap::deactivate()
  288. {
  289. if (pressedL)
  290. deactivateMouseMove();
  291. deactivateLClick();
  292. deactivateRClick();
  293. deactivateHover();
  294. }
  295. void CMinimap::showTile(const int3 &pos)
  296. {
  297. int3 mapSizes = LOCPLINT->cb->getMapSize();
  298. //drawing terrain
  299. int mw = map[0]->w, mh = map[0]->h;
  300. double wo = ((double)mw)/mapSizes.x, ho = ((double)mh)/mapSizes.y;
  301. for (int ii=0; ii<wo; ii++)
  302. {
  303. for (int jj=0; jj<ho; jj++)
  304. {
  305. if ((pos.x*wo+ii<this->pos.w) && (pos.y*ho+jj<this->pos.h))
  306. CSDL_Ext::SDL_PutPixelWithoutRefresh(FoW[pos.z],pos.x*wo+ii,pos.y*ho+jj,0,0,0,0);
  307. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  308. if(tile)
  309. {
  310. if (tile->blocked && (!tile->visitable))
  311. SDL_PutPixelWithoutRefresh(map[pos.z], pos.x*wo+ii, pos.y*ho+jj, colorsBlocked[tile->tertype].r, colorsBlocked[tile->tertype].g, colorsBlocked[tile->tertype].b);
  312. else SDL_PutPixelWithoutRefresh(map[pos.z], pos.x*wo+ii, pos.y*ho+jj, colors[tile->tertype].r, colors[tile->tertype].g, colors[tile->tertype].b);
  313. }
  314. }
  315. }
  316. //drawing flaggable objects
  317. int woShifted = wo, hoShifted = ho; //for better minimap rendering on L-sized maps
  318. std::vector < const CGObjectInstance * > oo = LOCPLINT->cb->getFlaggableObjects(pos);
  319. for(size_t v=0; v<oo.size(); ++v)
  320. {
  321. if(!dynamic_cast< const CGHeroInstance * >(oo[v])) //heroes have been printed
  322. {
  323. int3 maplgp ( (pos.x*mw)/mapSizes.x, (pos.y*mh)/mapSizes.y, pos.z );
  324. if(((int)wo) * mapSizes.x != mw && pos.x+1 < mapSizes.x)//minimap size in X is not multiple of map size in X
  325. {
  326. std::vector < const CGObjectInstance * > op1x = LOCPLINT->cb->getFlaggableObjects(int3(pos.x+1, pos.y, pos.z));
  327. if(op1x.size()!=0)
  328. {
  329. woShifted = wo + 1;
  330. }
  331. else
  332. {
  333. woShifted = wo;
  334. }
  335. }
  336. if(((int)ho) * mapSizes.y != mh && pos.y+1 < mapSizes.y) //minimap size in Y is not multiple of map size in Y
  337. {
  338. std::vector < const CGObjectInstance * > op1y = LOCPLINT->cb->getFlaggableObjects(int3(pos.x, pos.y+1, pos.z));
  339. if(op1y.size()!=0)
  340. {
  341. hoShifted = ho + 1;
  342. }
  343. else
  344. {
  345. hoShifted = ho;
  346. }
  347. }
  348. for (int ii=0; ii<woShifted; ii++) //rendering flaggable objects
  349. {
  350. for (int jj=0; jj<hoShifted; jj++)
  351. {
  352. if(oo[v]->tempOwner == 255)
  353. SDL_PutPixelWithoutRefresh(flObjs[pos.z],maplgp.x+ii,maplgp.y+jj,graphics->neutralColor->b,
  354. graphics->neutralColor->g,graphics->neutralColor->r);
  355. else
  356. SDL_PutPixelWithoutRefresh(flObjs[pos.z],maplgp.x+ii,maplgp.y+jj,graphics->playerColors[oo[v]->getOwner()].b,
  357. graphics->playerColors[oo[v]->getOwner()].g,graphics->playerColors[oo[v]->getOwner()].r);
  358. }
  359. }
  360. }
  361. }
  362. //flaggable objects drawn
  363. }
  364. void CMinimap::showVisibleTiles(int level)
  365. {
  366. int3 mapSizes = LOCPLINT->cb->getMapSize();
  367. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  368. {
  369. if(level>=0 && d!=level)
  370. continue;
  371. for(int x=0; x<mapSizes.x; ++x)
  372. {
  373. for(int y=0; y<mapSizes.y; ++y)
  374. {
  375. if(LOCPLINT->cb->isVisible(int3(x, y, d)))
  376. {
  377. showTile(int3(x, y, d));
  378. }
  379. }
  380. }
  381. }
  382. }
  383. void CMinimap::hideTile(const int3 &pos)
  384. {
  385. int3 mapSizes = LOCPLINT->cb->getMapSize();
  386. //drawing terrain
  387. int mw = map[0]->w, mh = map[0]->h;
  388. double wo = ((double)mw)/mapSizes.x, ho = ((double)mh)/mapSizes.y;
  389. for (int ii=0; ii<wo; ii++)
  390. {
  391. for (int jj=0; jj<ho; jj++)
  392. {
  393. if ((pos.x*wo+ii<this->pos.w) && (pos.y*ho+jj<this->pos.h))
  394. CSDL_Ext::SDL_PutPixelWithoutRefresh(FoW[pos.z],pos.x*wo+ii,pos.y*ho+jj,0,0,0,0);
  395. }
  396. }
  397. }
  398. void CMinimap::show( SDL_Surface * to )
  399. {
  400. }
  401. CTerrainRect::CTerrainRect()
  402. :curHoveredTile(-1,-1,-1), currentPath(NULL)
  403. {
  404. tilesw=(ADVOPT.advmapW+31)/32;
  405. tilesh=(ADVOPT.advmapH+31)/32;
  406. pos.x=ADVOPT.advmapX;
  407. pos.y=ADVOPT.advmapY;
  408. pos.w=ADVOPT.advmapW;
  409. pos.h=ADVOPT.advmapH;
  410. moveX = moveY = 0;
  411. }
  412. CTerrainRect::~CTerrainRect()
  413. {
  414. }
  415. void CTerrainRect::activate()
  416. {
  417. activateLClick();
  418. activateRClick();
  419. activateHover();
  420. activateMouseMove();
  421. }
  422. void CTerrainRect::deactivate()
  423. {
  424. deactivateLClick();
  425. deactivateRClick();
  426. deactivateHover();
  427. deactivateMouseMove();
  428. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  429. }
  430. void CTerrainRect::clickLeft(tribool down, bool previousState)
  431. {
  432. if ((down==false) || indeterminate(down))
  433. return;
  434. int3 mp = whichTileIsIt();
  435. if (mp.x<0 || mp.y<0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  436. return;
  437. adventureInt->tileLClicked(mp);
  438. }
  439. void CTerrainRect::clickRight(tribool down, bool previousState)
  440. {
  441. int3 mp = whichTileIsIt();
  442. if (!CGI->mh->map->isInTheMap(mp) || down != true)
  443. return;
  444. adventureInt->tileRClicked(mp);
  445. }
  446. void CTerrainRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  447. {
  448. int3 tHovered = whichTileIsIt(sEvent.x,sEvent.y);
  449. int3 pom = adventureInt->verifyPos(tHovered);
  450. if(tHovered != pom) //tile outside the map
  451. {
  452. CCS->curh->changeGraphic(0, 0);
  453. return;
  454. }
  455. if (pom != curHoveredTile)
  456. curHoveredTile=pom;
  457. else
  458. return;
  459. adventureInt->tileHovered(curHoveredTile);
  460. }
  461. void CTerrainRect::hover(bool on)
  462. {
  463. if (!on)
  464. {
  465. adventureInt->statusbar.clear();
  466. CCS->curh->changeGraphic(0,0);
  467. }
  468. //Hoverable::hover(on);
  469. }
  470. void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
  471. {
  472. for (size_t i=0; i < currentPath->nodes.size()-1; ++i)
  473. {
  474. int pn=-1;//number of picture
  475. if (i==0) //last tile
  476. {
  477. int x = 32*(currentPath->nodes[i].coord.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  478. y = 32*(currentPath->nodes[i].coord.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  479. if (x<0 || y<0 || x>pos.w || y>pos.h)
  480. continue;
  481. pn=0;
  482. }
  483. else
  484. {
  485. /* Vector directions
  486. * 0 1 2
  487. * \ | /
  488. * 3 - 4 - 5
  489. * / | \
  490. * 6 7 8
  491. *For example:
  492. * |
  493. * |__\
  494. * /
  495. * is id1=7, id2=5 (pns[7][5])
  496. */
  497. std::vector<CGPathNode> & cv = currentPath->nodes;
  498. int id1=(cv[i].coord.x-cv[i+1].coord.x+1)+3*(cv[i].coord.y-cv[i+1].coord.y+1); //Direction of entering vector
  499. int id2=(cv[i-1].coord.x-cv[i].coord.x+1)+3*(cv[i-1].coord.y-cv[i].coord.y+1); //Direction of exiting vector
  500. const static int pns[9][9] = {
  501. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  502. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  503. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  504. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  505. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  506. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  507. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  508. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  509. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  510. }; //table of magic values TODO meaning, change variable name
  511. pn=pns[id1][id2];
  512. }
  513. if (currentPath->nodes[i].turns)
  514. pn+=25;
  515. if (pn>=0)
  516. {
  517. CDefEssential * arrows = graphics->heroMoveArrows;
  518. int x = 32*(currentPath->nodes[i].coord.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  519. y = 32*(currentPath->nodes[i].coord.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  520. if (x<0 || y<0 || x>pos.w || y>pos.h)
  521. continue;
  522. int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
  523. hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
  524. SDL_Rect prevClip;
  525. SDL_GetClipRect(to, &prevClip);
  526. SDL_SetClipRect(to, extRect); //preventing blitting outside of that rect
  527. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  528. {
  529. if (hvx<0 && hvy<0)
  530. {
  531. Rect dstRect = genRect(32, 32, x + moveX, y + moveY);
  532. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, to, &dstRect);
  533. }
  534. else if(hvx<0)
  535. {
  536. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  537. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY);
  538. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  539. }
  540. else if (hvy<0)
  541. {
  542. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  543. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
  544. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  545. }
  546. else
  547. {
  548. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  549. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
  550. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  551. }
  552. }
  553. else //standard version
  554. {
  555. if (hvx<0 && hvy<0)
  556. {
  557. Rect dstRect = genRect(32, 32, x, y);
  558. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, to, &dstRect);
  559. }
  560. else if(hvx<0)
  561. {
  562. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  563. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y);
  564. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  565. }
  566. else if (hvy<0)
  567. {
  568. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  569. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y);
  570. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  571. }
  572. else
  573. {
  574. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  575. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y);
  576. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  577. }
  578. }
  579. SDL_SetClipRect(to, &prevClip);
  580. }
  581. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  582. }
  583. void CTerrainRect::show(SDL_Surface * to)
  584. {
  585. if(ADVOPT.smoothMove)
  586. CGI->mh->terrainRect
  587. (adventureInt->position, adventureInt->anim,
  588. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  589. to, &pos, moveX, moveY, false, int3());
  590. else
  591. CGI->mh->terrainRect
  592. (adventureInt->position, adventureInt->anim,
  593. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  594. to, &pos, 0, 0, false, int3());
  595. //SDL_BlitSurface(teren,&genRect(pos.h,pos.w,0,0),screen,&genRect(547,594,7,6));
  596. //SDL_FreeSurface(teren);
  597. if (currentPath && adventureInt->position.z==currentPath->startPos().z) //drawing path
  598. {
  599. showPath(&pos, to);
  600. }
  601. }
  602. int3 CTerrainRect::whichTileIsIt(const int & x, const int & y)
  603. {
  604. int3 ret;
  605. ret.x = adventureInt->position.x + ((GH.current->motion.x-CGI->mh->offsetX-pos.x)/32);
  606. ret.y = adventureInt->position.y + ((GH.current->motion.y-CGI->mh->offsetY-pos.y)/32);
  607. ret.z = adventureInt->position.z;
  608. return ret;
  609. }
  610. int3 CTerrainRect::whichTileIsIt()
  611. {
  612. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  613. }
  614. void CResDataBar::clickRight(tribool down, bool previousState)
  615. {
  616. }
  617. void CResDataBar::activate()
  618. {
  619. activateRClick();
  620. }
  621. void CResDataBar::deactivate()
  622. {
  623. deactivateRClick();
  624. }
  625. CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
  626. {
  627. bg = BitmapHandler::loadBitmap(defname);
  628. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  629. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  630. pos = genRect(bg->h, bg->w, pos.x+x, pos.y+y);
  631. txtpos.resize(8);
  632. for (int i = 0; i < 8 ; i++)
  633. {
  634. txtpos[i].first = pos.x + offx + resdist*i;
  635. txtpos[i].second = pos.y + offy;
  636. }
  637. txtpos[7].first = txtpos[6].first + datedist;
  638. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  639. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  640. }
  641. CResDataBar::CResDataBar()
  642. {
  643. bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
  644. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  645. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  646. pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
  647. txtpos.resize(8);
  648. for (int i = 0; i < 8 ; i++)
  649. {
  650. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  651. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  652. }
  653. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  654. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  655. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  656. }
  657. CResDataBar::~CResDataBar()
  658. {
  659. SDL_FreeSurface(bg);
  660. }
  661. void CResDataBar::draw(SDL_Surface * to)
  662. {
  663. blitAt(bg,pos.x,pos.y,to);
  664. char * buf = new char[15];
  665. for (int i=0;i<7;i++)
  666. {
  667. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  668. printAt(buf,txtpos[i].first,txtpos[i].second,FONT_SMALL,zwykly,to);
  669. }
  670. std::vector<std::string> temp;
  671. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp+=std::string(buf);
  672. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp+=std::string(buf);
  673. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp+=std::string(buf);
  674. printAt(processStr(datetext,temp),txtpos[7].first,txtpos[7].second,FONT_SMALL,zwykly,to);
  675. temp.clear();
  676. //updateRect(&pos,screen);
  677. delete[] buf;
  678. }
  679. void CResDataBar::show( SDL_Surface * to )
  680. {
  681. }
  682. void CResDataBar::showAll( SDL_Surface * to )
  683. {
  684. draw(to);
  685. }
  686. CInfoBar::CInfoBar()
  687. {
  688. toNextTick = mode = pom = -1;
  689. pos.x=ADVOPT.infoboxX;
  690. pos.y=ADVOPT.infoboxY;
  691. pos.w=194;
  692. pos.h=186;
  693. day = CDefHandler::giveDef("NEWDAY.DEF");
  694. week1 = CDefHandler::giveDef("NEWWEEK1.DEF");
  695. week2 = CDefHandler::giveDef("NEWWEEK2.DEF");
  696. week3 = CDefHandler::giveDef("NEWWEEK3.DEF");
  697. week4 = CDefHandler::giveDef("NEWWEEK4.DEF");
  698. selInfoWin = NULL;
  699. }
  700. CInfoBar::~CInfoBar()
  701. {
  702. delete day;
  703. delete week1;
  704. delete week2;
  705. delete week3;
  706. delete week4;
  707. if(selInfoWin)
  708. SDL_FreeSurface(selInfoWin);
  709. }
  710. void CInfoBar::showAll(SDL_Surface * to)
  711. {
  712. if ((mode>=0) && mode<5)
  713. {
  714. blitAnim(mode);
  715. return;
  716. }
  717. else if (mode==5)
  718. {
  719. mode = -1;
  720. }
  721. if(selInfoWin)
  722. {
  723. blitAt(selInfoWin, pos.x, pos.y, to);
  724. }
  725. }
  726. CDefHandler * CInfoBar::getAnim(int mode)
  727. {
  728. switch(mode)
  729. {
  730. case 0:
  731. return day;
  732. case 1:
  733. return week1;
  734. case 2:
  735. return week2;
  736. case 3:
  737. return week3;
  738. case 4:
  739. return week4;
  740. default:
  741. return NULL;
  742. }
  743. }
  744. void CInfoBar::blitAnim(int mode)//0 - day, 1 - week
  745. {
  746. CDefHandler * anim = NULL;
  747. std::ostringstream txt;
  748. anim = getAnim(mode);
  749. if(mode) //new week animation
  750. {
  751. txt << CGI->generaltexth->allTexts[63] << " " << LOCPLINT->cb->getDate(2);
  752. }
  753. else //new day
  754. {
  755. txt << CGI->generaltexth->allTexts[64] << " " << LOCPLINT->cb->getDate(1);
  756. }
  757. blitAt(anim->ourImages[pom].bitmap,pos.x+9,pos.y+10);
  758. printAtMiddle(txt.str(),pos.x+95,pos.y+31,FONT_MEDIUM,zwykly);
  759. if (pom == anim->ourImages.size()-1)
  760. toNextTick+=750;
  761. }
  762. void CInfoBar::newDay(int Day)
  763. {
  764. if(LOCPLINT->cb->getDate(1) != 1)
  765. {
  766. mode = 0; //showing day
  767. }
  768. else
  769. {
  770. switch(LOCPLINT->cb->getDate(2))
  771. {
  772. case 1:
  773. mode = 1;
  774. break;
  775. case 2:
  776. mode = 2;
  777. break;
  778. case 3:
  779. mode = 3;
  780. break;
  781. case 4:
  782. mode = 4;
  783. break;
  784. default:
  785. mode = -1;
  786. break;
  787. }
  788. }
  789. pom = 0;
  790. if(!(active & TIME))
  791. activateTimer();
  792. toNextTick = 500;
  793. blitAnim(mode);
  794. }
  795. void CInfoBar::showComp(SComponent * comp, int time)
  796. {
  797. if(comp->type != SComponent::hero)
  798. {
  799. curSel = NULL;
  800. }
  801. SDL_Surface * b = BitmapHandler::loadBitmap("ADSTATOT.bmp");
  802. blitAt(b,pos.x+8,pos.y+11);
  803. blitAt(comp->getImg(),pos.x+52,pos.y+54);
  804. printAtMiddle(comp->subtitle,pos.x+91,pos.y+158,FONT_SMALL,zwykly);
  805. printAtMiddleWB(comp->description,pos.x+94,pos.y+31,FONT_SMALL,26,zwykly);
  806. SDL_FreeSurface(b);
  807. if(!(active & TIME))
  808. activateTimer();
  809. mode = 6;
  810. toNextTick = time;
  811. }
  812. void CInfoBar::tick()
  813. {
  814. if(mode >= 0 && mode < 5)
  815. {
  816. pom++;
  817. if (pom >= getAnim(mode)->ourImages.size())
  818. {
  819. deactivateTimer();
  820. toNextTick = -1;
  821. mode = 5;
  822. showAll(screen2);
  823. return;
  824. }
  825. toNextTick = 150;
  826. blitAnim(mode);
  827. }
  828. else if(mode == 6)
  829. {
  830. deactivateTimer();
  831. toNextTick = -1;
  832. mode = 5;
  833. showAll(screen2);
  834. }
  835. }
  836. void CInfoBar::show( SDL_Surface * to )
  837. {
  838. }
  839. void CInfoBar::activate()
  840. {
  841. //CIntObject::activate();
  842. }
  843. void CInfoBar::deactivate()
  844. {
  845. //CIntObject::deactivate();
  846. if(active & TIME)
  847. deactivateTimer();
  848. }
  849. void CInfoBar::updateSelection(const CGObjectInstance *obj)
  850. {
  851. if(obj->ID == HEROI_TYPE)
  852. curSel = static_cast<const CGHeroInstance*>(obj);
  853. else
  854. curSel = NULL;
  855. if(selInfoWin)
  856. SDL_FreeSurface(selInfoWin);
  857. selInfoWin = LOCPLINT->infoWin(obj);
  858. }
  859. CAdvMapInt::CAdvMapInt()
  860. :statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  861. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  862. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  863. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  864. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  865. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  866. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  867. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  868. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  869. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  870. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  871. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  872. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  873. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  874. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  875. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  876. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  877. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  878. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  879. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  880. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  881. heroList(ADVOPT.hlistSize),
  882. townList(ADVOPT.tlistSize,ADVOPT.tlistX,ADVOPT.tlistY,ADVOPT.tlistAU,ADVOPT.tlistAD)//(5,&genRect(192,48,747,196),747,196,747,372),
  883. {
  884. spellBeingCasted = NULL;
  885. pos.x = pos.y = 0;
  886. pos.w = screen->w;
  887. pos.h = screen->h;
  888. selection = NULL;
  889. townList.fun = boost::bind(&CAdvMapInt::selectionChanged,this);
  890. adventureInt=this;
  891. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  892. scrollingDir = 0;
  893. updateScreen = false;
  894. anim=0;
  895. animValHitCount=0; //animation frame
  896. heroAnim=0;
  897. heroAnimValHitCount=0; // hero animation frame
  898. heroList.init();
  899. heroList.genList();
  900. //townList.init();
  901. //townList.genList();
  902. for (int g=0; g<ADVOPT.gemG.size(); ++g)
  903. {
  904. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[g]));
  905. }
  906. setPlayer(LOCPLINT->playerID);
  907. }
  908. CAdvMapInt::~CAdvMapInt()
  909. {
  910. SDL_FreeSurface(bg);
  911. for(int i=0; i<gems.size(); i++)
  912. delete gems[i];
  913. }
  914. void CAdvMapInt::fshowOverview()
  915. {
  916. GH.pushInt(new CKingdomInterface);
  917. }
  918. void CAdvMapInt::fswitchLevel()
  919. {
  920. if(!CGI->mh->map->twoLevel)
  921. return;
  922. if (position.z)
  923. {
  924. position.z--;
  925. underground.setIndex(0,true);
  926. underground.showAll(screenBuf);
  927. }
  928. else
  929. {
  930. underground.setIndex(1,true);
  931. position.z++;
  932. underground.showAll(screenBuf);
  933. }
  934. updateScreen = true;
  935. minimap.draw(screenBuf);
  936. }
  937. void CAdvMapInt::fshowQuestlog()
  938. {
  939. }
  940. void CAdvMapInt::fsleepWake()
  941. {
  942. }
  943. void CAdvMapInt::fmoveHero()
  944. {
  945. const CGHeroInstance *h = curHero();
  946. if (!h || !terrain.currentPath)
  947. return;
  948. LOCPLINT->moveHero(h, *terrain.currentPath);
  949. }
  950. void CAdvMapInt::fshowSpellbok()
  951. {
  952. if (!curHero()) //checking necessary values
  953. return;
  954. centerOn(selection);
  955. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), curHero(), LOCPLINT, false);
  956. GH.pushInt(spellWindow);
  957. }
  958. void CAdvMapInt::fadventureOPtions()
  959. {
  960. GH.pushInt(new CAdventureOptions);
  961. }
  962. void CAdvMapInt::fsystemOptions()
  963. {
  964. CSystemOptionsWindow * sysopWindow = new CSystemOptionsWindow(Rect::createCentered(487, 481), LOCPLINT);
  965. GH.pushInt(sysopWindow);
  966. }
  967. void CAdvMapInt::fnextHero()
  968. {
  969. if(!LOCPLINT->wanderingHeroes.size()) //no wandering heroes
  970. return;
  971. int start = heroList.selected;
  972. int i = start;
  973. do
  974. {
  975. i++;
  976. if(i >= LOCPLINT->wanderingHeroes.size())
  977. i = 0;
  978. } while (!LOCPLINT->wanderingHeroes[i]->movement && i!=start);
  979. heroList.select(i);
  980. }
  981. void CAdvMapInt::fendTurn()
  982. {
  983. if(LOCPLINT->cingconsole->active)
  984. LOCPLINT->cingconsole->deactivate();
  985. LOCPLINT->makingTurn = false;
  986. LOCPLINT->cb->endTurn();
  987. }
  988. void CAdvMapInt::activate()
  989. {
  990. if(isActive())
  991. {
  992. tlog1 << "Error: advmapint already active...\n";
  993. return;
  994. }
  995. screenBuf = screen;
  996. GH.statusbar = &statusbar;
  997. activateMouseMove();
  998. kingOverview.activate();
  999. underground.activate();
  1000. questlog.activate();
  1001. sleepWake.activate();
  1002. moveHero.activate();
  1003. spellbook.activate();
  1004. sysOptions.activate();
  1005. advOptions.activate();
  1006. nextHero.activate();
  1007. endTurn.activate();
  1008. minimap.activate();
  1009. heroList.activate();
  1010. townList.activate();
  1011. terrain.activate();
  1012. infoBar.activate();
  1013. if(!LOCPLINT->cingconsole->active)
  1014. LOCPLINT->cingconsole->activate();
  1015. GH.fakeMouseMove(); //to restore the cursor
  1016. }
  1017. void CAdvMapInt::deactivate()
  1018. {
  1019. deactivateMouseMove();
  1020. scrollingDir = 0;
  1021. CCS->curh->changeGraphic(0,0);
  1022. kingOverview.deactivate();
  1023. underground.deactivate();
  1024. questlog.deactivate();
  1025. sleepWake.deactivate();
  1026. moveHero.deactivate();
  1027. spellbook.deactivate();
  1028. advOptions.deactivate();
  1029. sysOptions.deactivate();
  1030. nextHero.deactivate();
  1031. endTurn.deactivate();
  1032. minimap.deactivate();
  1033. heroList.deactivate();
  1034. townList.deactivate();
  1035. terrain.deactivate();
  1036. infoBar.deactivate();
  1037. infoBar.mode=-1;
  1038. if(LOCPLINT->cingconsole->active) //TODO
  1039. LOCPLINT->cingconsole->deactivate();
  1040. }
  1041. void CAdvMapInt::showAll(SDL_Surface *to)
  1042. {
  1043. blitAt(bg,0,0,to);
  1044. if(state != INGAME)
  1045. return;
  1046. kingOverview.showAll(to);
  1047. underground.showAll(to);
  1048. questlog.showAll(to);
  1049. sleepWake.showAll(to);
  1050. moveHero.showAll(to);
  1051. spellbook.showAll(to);
  1052. advOptions.showAll(to);
  1053. sysOptions.showAll(to);
  1054. nextHero.showAll(to);
  1055. endTurn.showAll(to);
  1056. minimap.draw(to);
  1057. heroList.draw(to);
  1058. townList.draw(to);
  1059. updateScreen = true;
  1060. show(to);
  1061. resdatabar.draw(to);
  1062. statusbar.show(to);
  1063. infoBar.showAll(to);
  1064. LOCPLINT->cingconsole->show(to);
  1065. }
  1066. void CAdvMapInt::show(SDL_Surface *to)
  1067. {
  1068. if(state != INGAME)
  1069. return;
  1070. ++animValHitCount; //for animations
  1071. if(animValHitCount == 8)
  1072. {
  1073. CGI->mh->updateWater();
  1074. animValHitCount = 0;
  1075. ++anim;
  1076. updateScreen = true;
  1077. }
  1078. ++heroAnim;
  1079. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  1080. if((animValHitCount % (4/LOCPLINT->sysOpts.mapScrollingSpeed)) == 0
  1081. && (
  1082. (GH.topInt() == this)
  1083. || SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1084. || SDL_GetKeyState(NULL)[SDLK_RCTRL])
  1085. )
  1086. {
  1087. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  1088. position.x--;
  1089. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  1090. position.x++;
  1091. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  1092. position.y--;
  1093. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  1094. position.y++;
  1095. if(scrollingDir)
  1096. {
  1097. updateScreen = true;
  1098. updateMinimap=true;
  1099. }
  1100. }
  1101. if(updateScreen)
  1102. {
  1103. terrain.show(to);
  1104. for(int i=0;i<4;i++)
  1105. blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  1106. updateScreen=false;
  1107. LOCPLINT->cingconsole->show(to);
  1108. }
  1109. if (updateMinimap)
  1110. {
  1111. minimap.draw(to);
  1112. updateMinimap=false;
  1113. }
  1114. }
  1115. void CAdvMapInt::selectionChanged()
  1116. {
  1117. const CGTownInstance *to = LOCPLINT->towns[townList.selected];
  1118. select(to);
  1119. }
  1120. void CAdvMapInt::centerOn(int3 on)
  1121. {
  1122. on.x -= CGI->mh->frameW;
  1123. on.y -= CGI->mh->frameH;
  1124. on = LOCPLINT->repairScreenPos(on);
  1125. adventureInt->position = on;
  1126. adventureInt->updateScreen=true;
  1127. updateMinimap=true;
  1128. underground.setIndex(on.z,true); //change underground switch button image
  1129. if(GH.topInt() == this)
  1130. underground.redraw();
  1131. }
  1132. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  1133. {
  1134. centerOn(obj->getSightCenter());
  1135. }
  1136. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1137. {
  1138. ui8 Dir = 0;
  1139. int k = key.keysym.sym;
  1140. const CGHeroInstance *h = curHero(); //selected hero
  1141. const CGTownInstance *t = curTown(); //selected town
  1142. switch(k)
  1143. {
  1144. case SDLK_i:
  1145. if(isActive())
  1146. CAdventureOptions::showScenarioInfo();
  1147. return;
  1148. case SDLK_s:
  1149. if(isActive())
  1150. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  1151. return;
  1152. case SDLK_d:
  1153. {
  1154. if(h && isActive() && key.state == SDL_PRESSED)
  1155. LOCPLINT->tryDiggging(h);
  1156. return;
  1157. }
  1158. case SDLK_p:
  1159. if(isActive())
  1160. LOCPLINT->showPuzzleMap();
  1161. return;
  1162. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1163. {
  1164. if(!isActive())
  1165. return;
  1166. if(h && key.state == SDL_PRESSED)
  1167. {
  1168. LOCPLINT->pim->unlock();
  1169. LOCPLINT->cb->moveHero(h,h->pos);
  1170. LOCPLINT->pim->lock();
  1171. }
  1172. }
  1173. return;
  1174. case SDLK_RETURN:
  1175. {
  1176. if(!isActive() || !selection || key.state != SDL_PRESSED)
  1177. return;
  1178. if(h)
  1179. LOCPLINT->openHeroWindow(h);
  1180. else if(t)
  1181. LOCPLINT->openTownWindow(t);
  1182. return;
  1183. }
  1184. case SDLK_ESCAPE:
  1185. {
  1186. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  1187. return;
  1188. leaveCastingMode();
  1189. return;
  1190. }
  1191. case SDLK_t:
  1192. {
  1193. //act on key down if marketplace windows is not already opened
  1194. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS) return;
  1195. //check if we have any marketplace
  1196. const CGTownInstance *townWithMarket = NULL;
  1197. BOOST_FOREACH(const CGTownInstance *t, LOCPLINT->cb->getTownsInfo())
  1198. {
  1199. if(vstd::contains(t->builtBuildings, 14))
  1200. {
  1201. townWithMarket = t;
  1202. break;
  1203. }
  1204. }
  1205. if(townWithMarket) //if any town has marketplace, open window
  1206. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  1207. else //if not - complain
  1208. LOCPLINT->showInfoDialog("No available marketplace!", std::vector<SComponent*>(), soundBase::sound_todo);
  1209. return;
  1210. }
  1211. default:
  1212. {
  1213. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1214. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1215. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1216. //numpad arrow
  1217. if(isArrowKey(SDLKey(k)))
  1218. {
  1219. switch(k)
  1220. {
  1221. case SDLK_UP:
  1222. Dir = UP;
  1223. break;
  1224. case SDLK_LEFT:
  1225. Dir = LEFT;
  1226. break;
  1227. case SDLK_RIGHT:
  1228. Dir = RIGHT;
  1229. break;
  1230. case SDLK_DOWN:
  1231. Dir = DOWN;
  1232. break;
  1233. }
  1234. k = arrowToNum(SDLKey(k));
  1235. }
  1236. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  1237. break;
  1238. k -= SDLK_KP0 + 1;
  1239. if(k < 0 || k > 8 || key.state != SDL_PRESSED)
  1240. return;
  1241. if(!h)
  1242. break;
  1243. if(k == 4)
  1244. {
  1245. centerOn(h);
  1246. return;
  1247. }
  1248. int3 dir = directions[k];
  1249. CGPath &path = LOCPLINT->paths[h];
  1250. terrain.currentPath = &path;
  1251. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  1252. {
  1253. terrain.currentPath = NULL;
  1254. return;
  1255. }
  1256. if(!path.nodes[0].turns)
  1257. {
  1258. LOCPLINT->moveHero(h, path);
  1259. }
  1260. }
  1261. return;
  1262. }
  1263. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1264. && LOCPLINT->ctrlPressed()
  1265. )
  1266. scrollingDir |= Dir;
  1267. else
  1268. scrollingDir &= ~Dir;
  1269. }
  1270. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  1271. {
  1272. if(down)
  1273. {
  1274. CRClickPopup::createAndPush(text);
  1275. }
  1276. }
  1277. int3 CAdvMapInt::verifyPos(int3 ver)
  1278. {
  1279. if (ver.x<0)
  1280. ver.x=0;
  1281. if (ver.y<0)
  1282. ver.y=0;
  1283. if (ver.z<0)
  1284. ver.z=0;
  1285. if (ver.x>=CGI->mh->sizes.x)
  1286. ver.x=CGI->mh->sizes.x-1;
  1287. if (ver.y>=CGI->mh->sizes.y)
  1288. ver.y=CGI->mh->sizes.y-1;
  1289. if (ver.z>=CGI->mh->sizes.z)
  1290. ver.z=CGI->mh->sizes.z-1;
  1291. return ver;
  1292. }
  1293. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  1294. {
  1295. assert(sel);
  1296. LOCPLINT->cb->setSelection(sel);
  1297. selection = sel;
  1298. if(centerView)
  1299. centerOn(sel);
  1300. terrain.currentPath = NULL;
  1301. if(sel->ID==TOWNI_TYPE)
  1302. {
  1303. int pos = vstd::findPos(LOCPLINT->towns,sel);
  1304. townList.selected = pos;
  1305. townList.fixPos();
  1306. }
  1307. else //hero selected
  1308. {
  1309. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(sel);
  1310. if(LOCPLINT->getWHero(heroList.selected) != h)
  1311. {
  1312. heroList.selected = heroList.getPosOfHero(h);
  1313. heroList.fixPos();
  1314. }
  1315. terrain.currentPath = LOCPLINT->getAndVerifyPath(h);
  1316. }
  1317. townList.draw(screen);
  1318. heroList.draw(screen);
  1319. infoBar.updateSelection(sel);
  1320. infoBar.showAll(screen);
  1321. }
  1322. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1323. {
  1324. //adventure map scrolling with mouse
  1325. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && isActive())
  1326. {
  1327. if(sEvent.x<15)
  1328. {
  1329. scrollingDir |= LEFT;
  1330. }
  1331. else
  1332. {
  1333. scrollingDir &= ~LEFT;
  1334. }
  1335. if(sEvent.x>screen->w-15)
  1336. {
  1337. scrollingDir |= RIGHT;
  1338. }
  1339. else
  1340. {
  1341. scrollingDir &= ~RIGHT;
  1342. }
  1343. if(sEvent.y<15)
  1344. {
  1345. scrollingDir |= UP;
  1346. }
  1347. else
  1348. {
  1349. scrollingDir &= ~UP;
  1350. }
  1351. if(sEvent.y>screen->h-15)
  1352. {
  1353. scrollingDir |= DOWN;
  1354. }
  1355. else
  1356. {
  1357. scrollingDir &= ~DOWN;
  1358. }
  1359. }
  1360. }
  1361. bool CAdvMapInt::isActive()
  1362. {
  1363. return active & ~CIntObject::KEYBOARD;
  1364. }
  1365. void CAdvMapInt::startHotSeatWait(int Player)
  1366. {
  1367. state = WAITING;
  1368. }
  1369. void CAdvMapInt::setPlayer(int Player)
  1370. {
  1371. player = Player;
  1372. graphics->blueToPlayersAdv(bg,player);
  1373. kingOverview.setPlayerColor(player);
  1374. underground.setPlayerColor(player);
  1375. questlog.setPlayerColor(player);
  1376. sleepWake.setPlayerColor(player);
  1377. moveHero.setPlayerColor(player);
  1378. spellbook.setPlayerColor(player);
  1379. sysOptions.setPlayerColor(player);
  1380. advOptions.setPlayerColor(player);
  1381. nextHero.setPlayerColor(player);
  1382. endTurn.setPlayerColor(player);
  1383. graphics->blueToPlayersAdv(resdatabar.bg,player);
  1384. //heroList.updateHList();
  1385. //townList.genList();
  1386. }
  1387. void CAdvMapInt::startTurn()
  1388. {
  1389. state = INGAME;
  1390. }
  1391. void CAdvMapInt::tileLClicked(const int3 &mp)
  1392. {
  1393. if(!LOCPLINT->cb->isVisible(mp))
  1394. return;
  1395. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mp), //blocking objects at tile
  1396. vobjs = LOCPLINT->cb->getVisitableObjs(mp); //visitable objects
  1397. const TerrainTile *tile = LOCPLINT->cb->getTile(mp);
  1398. const CGObjectInstance *topBlocking = bobjs.size() ? bobjs.back() : NULL;
  1399. int3 selPos = selection->getSightCenter();
  1400. if(spellBeingCasted && isInScreenRange(selPos, mp))
  1401. {
  1402. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1403. switch(spellBeingCasted->id)
  1404. {
  1405. case Spells::SCUTTLE_BOAT: //Scuttle Boat
  1406. if(topBlocking && topBlocking->ID == 8)
  1407. leaveCastingMode(true, mp);
  1408. break;
  1409. case Spells::DIMENSION_DOOR:
  1410. if(!tile || tile->isClear(heroTile))
  1411. leaveCastingMode(true, mp);
  1412. break;
  1413. }
  1414. return;
  1415. }
  1416. //check if we can select this object
  1417. bool canSelect = topBlocking && topBlocking->ID == HEROI_TYPE && topBlocking->tempOwner == LOCPLINT->playerID;
  1418. canSelect |= topBlocking && topBlocking->ID == TOWNI_TYPE && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1419. if (selection->ID != HEROI_TYPE) //hero is not selected (presumably town)
  1420. {
  1421. assert(!terrain.currentPath); //path can be active only when hero is selected
  1422. if(selection == topBlocking) //selected town clicked
  1423. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1424. else if ( canSelect )
  1425. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1426. return;
  1427. }
  1428. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1429. {
  1430. const CGPathNode *pn = LOCPLINT->cb->getPathInfo(mp);
  1431. if(currentHero == topBlocking) //clicked selected hero
  1432. {
  1433. LOCPLINT->openHeroWindow(currentHero);
  1434. return;
  1435. }
  1436. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1437. {
  1438. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1439. return;
  1440. }
  1441. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1442. {
  1443. if (terrain.currentPath && terrain.currentPath->endPos() == mp)//we'll be moving
  1444. {
  1445. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1446. return;
  1447. }
  1448. 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
  1449. {
  1450. CGPath &path = LOCPLINT->paths[currentHero];
  1451. terrain.currentPath = &path;
  1452. if(!LOCPLINT->cb->getPath2(mp, path)) //try getting path, erase if failed
  1453. LOCPLINT->eraseCurrentPathOf(currentHero);
  1454. else
  1455. return;
  1456. }
  1457. }
  1458. } //end of hero is selected "case"
  1459. else
  1460. {
  1461. throw std::string("Nothing is selected...");
  1462. }
  1463. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1464. {
  1465. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1466. }
  1467. }
  1468. void CAdvMapInt::tileHovered(const int3 &tile)
  1469. {
  1470. if(!LOCPLINT->cb->isVisible(tile))
  1471. {
  1472. CCS->curh->changeGraphic(0, 0);
  1473. statusbar.clear();
  1474. return;
  1475. }
  1476. std::vector<std::string> temp = LOCPLINT->cb->getObjDescriptions(tile);
  1477. if (temp.size())
  1478. {
  1479. boost::replace_all(temp.back(),"\n"," ");
  1480. statusbar.print(temp.back());
  1481. }
  1482. else
  1483. {
  1484. std::string hlp;
  1485. CGI->mh->getTerrainDescr(tile, hlp, false);
  1486. statusbar.print(hlp);
  1487. }
  1488. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(tile);
  1489. std::vector<const CGObjectInstance *> objs = LOCPLINT->cb->getBlockingObjs(tile);
  1490. const CGObjectInstance *objAtTile = objs.size() ? objs.back() : NULL;
  1491. bool accessible = pnode->turns < 255;
  1492. int turns = pnode->turns;
  1493. amin(turns, 3);
  1494. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1495. return;
  1496. if(spellBeingCasted)
  1497. {
  1498. switch(spellBeingCasted->id)
  1499. {
  1500. case Spells::SCUTTLE_BOAT:
  1501. if(objAtTile && objAtTile->ID == 8)
  1502. CCS->curh->changeGraphic(0, 42);
  1503. else
  1504. CCS->curh->changeGraphic(0, 0);
  1505. return;
  1506. case Spells::DIMENSION_DOOR:
  1507. {
  1508. const TerrainTile *t = LOCPLINT->cb->getTile(tile, false);
  1509. int3 hpos = selection->getSightCenter();
  1510. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, tile))
  1511. CCS->curh->changeGraphic(0, 41);
  1512. else
  1513. CCS->curh->changeGraphic(0, 0);
  1514. return;
  1515. }
  1516. }
  1517. }
  1518. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->guardingCreaturePosition(tile));
  1519. if(selection->ID == TOWNI_TYPE)
  1520. {
  1521. if(objAtTile)
  1522. {
  1523. if(objAtTile->ID == TOWNI_TYPE && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner))
  1524. CCS->curh->changeGraphic(0, 3);
  1525. else if(objAtTile->ID == HEROI_TYPE && objAtTile->tempOwner == LOCPLINT->playerID)
  1526. CCS->curh->changeGraphic(0, 2);
  1527. }
  1528. else
  1529. CCS->curh->changeGraphic(0, 0);
  1530. }
  1531. else if(const CGHeroInstance *h = curHero())
  1532. {
  1533. if(objAtTile)
  1534. {
  1535. if(objAtTile->ID == HEROI_TYPE)
  1536. {
  1537. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1538. {
  1539. if(accessible)
  1540. CCS->curh->changeGraphic(0, 5 + turns*6);
  1541. else
  1542. CCS->curh->changeGraphic(0, 0);
  1543. }
  1544. else //our or ally hero
  1545. {
  1546. if(selection == objAtTile)
  1547. CCS->curh->changeGraphic(0, 2);
  1548. else if(accessible)
  1549. CCS->curh->changeGraphic(0, 8 + turns*6);
  1550. else
  1551. CCS->curh->changeGraphic(0, 2);
  1552. }
  1553. }
  1554. else if(objAtTile->ID == TOWNI_TYPE)
  1555. {
  1556. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1557. {
  1558. if(accessible)
  1559. {
  1560. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1561. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1562. if (townObj && !townObj->armedGarrison())
  1563. CCS->curh->changeGraphic(0, 9 + turns*6);
  1564. else
  1565. CCS->curh->changeGraphic(0, 5 + turns*6);
  1566. }
  1567. else
  1568. {
  1569. CCS->curh->changeGraphic(0, 0);
  1570. }
  1571. }
  1572. else //our or ally town
  1573. {
  1574. if(accessible)
  1575. CCS->curh->changeGraphic(0, 9 + turns*6);
  1576. else
  1577. CCS->curh->changeGraphic(0, 3);
  1578. }
  1579. }
  1580. else if(objAtTile->ID == 8) //boat
  1581. {
  1582. if(accessible)
  1583. CCS->curh->changeGraphic(0, 6 + turns*6);
  1584. else
  1585. CCS->curh->changeGraphic(0, 0);
  1586. }
  1587. else if (objAtTile->ID == 33 || objAtTile->ID == 219) // Garrison
  1588. {
  1589. if (accessible)
  1590. {
  1591. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1592. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1593. if (garrObj && garrObj->stacksCount()
  1594. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1595. CCS->curh->changeGraphic(0, 5 + turns*6);
  1596. else
  1597. CCS->curh->changeGraphic(0, 9 + turns*6);
  1598. }
  1599. else
  1600. CCS->curh->changeGraphic(0, 0);
  1601. }
  1602. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1603. {
  1604. CCS->curh->changeGraphic(0, 5 + turns*6);
  1605. }
  1606. else
  1607. {
  1608. if(accessible)
  1609. {
  1610. if(pnode->land)
  1611. CCS->curh->changeGraphic(0, 9 + turns*6);
  1612. else
  1613. CCS->curh->changeGraphic(0, 28 + turns);
  1614. }
  1615. else
  1616. CCS->curh->changeGraphic(0, 0);
  1617. }
  1618. }
  1619. else //no objs
  1620. {
  1621. if(accessible && pnode->accessible != CGPathNode::FLYABLE)
  1622. {
  1623. if (guardingCreature) {
  1624. CCS->curh->changeGraphic(0, 5 + turns*6);
  1625. } else {
  1626. if(pnode->land)
  1627. {
  1628. if(LOCPLINT->cb->getTile(h->getPosition(false))->tertype != TerrainTile::water)
  1629. CCS->curh->changeGraphic(0, 4 + turns*6);
  1630. else
  1631. CCS->curh->changeGraphic(0, 7 + turns*6); //anchor
  1632. }
  1633. else
  1634. CCS->curh->changeGraphic(0, 6 + turns*6);
  1635. }
  1636. }
  1637. else
  1638. CCS->curh->changeGraphic(0, 0);
  1639. }
  1640. }
  1641. if(ourInaccessibleShipyard(objAtTile))
  1642. {
  1643. CCS->curh->changeGraphic(0, 6);
  1644. }
  1645. }
  1646. void CAdvMapInt::tileRClicked(const int3 &mp)
  1647. {
  1648. if(spellBeingCasted)
  1649. {
  1650. leaveCastingMode();
  1651. return;
  1652. }
  1653. if(!LOCPLINT->cb->isVisible(mp))
  1654. {
  1655. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1656. return;
  1657. }
  1658. std::vector < const CGObjectInstance * > objs = LOCPLINT->cb->getBlockingObjs(mp);
  1659. if(!objs.size())
  1660. {
  1661. // Bare or undiscovered terrain
  1662. const TerrainTile * tile = LOCPLINT->cb->getTile(mp);
  1663. if (tile)
  1664. {
  1665. std::string hlp;
  1666. CGI->mh->getTerrainDescr(mp, hlp, true);
  1667. CRClickPopup::createAndPush(hlp);
  1668. }
  1669. return;
  1670. }
  1671. const CGObjectInstance * obj = objs.back();
  1672. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1673. }
  1674. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1675. {
  1676. using namespace Spells;
  1677. assert(sp->id == SCUTTLE_BOAT || sp->id == DIMENSION_DOOR);
  1678. spellBeingCasted = sp;
  1679. deactivate();
  1680. terrain.activate();
  1681. GH.fakeMouseMove();
  1682. }
  1683. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1684. {
  1685. assert(spellBeingCasted);
  1686. int id = spellBeingCasted->id;
  1687. spellBeingCasted = NULL;
  1688. terrain.deactivate();
  1689. activate();
  1690. if(cast)
  1691. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1692. else
  1693. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1694. }
  1695. const CGHeroInstance * CAdvMapInt::curHero() const
  1696. {
  1697. if(selection && selection->ID == HEROI_TYPE)
  1698. return static_cast<const CGHeroInstance *>(selection);
  1699. else
  1700. return NULL;
  1701. }
  1702. const CGTownInstance * CAdvMapInt::curTown() const
  1703. {
  1704. if(selection && selection->ID == TOWNI_TYPE)
  1705. return static_cast<const CGTownInstance *>(selection);
  1706. else
  1707. return NULL;
  1708. }
  1709. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1710. {
  1711. const IShipyard *ret = IShipyard::castFrom(obj);
  1712. if(!ret || obj->tempOwner != player || CCS->curh->mode || (CCS->curh->number != 6 && CCS->curh->number != 0))
  1713. return NULL;
  1714. return ret;
  1715. }
  1716. CAdventureOptions::CAdventureOptions()
  1717. {
  1718. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1719. bg = new CPicture("ADVOPTS.bmp");
  1720. graphics->blueToPlayersAdv(bg->bg, LOCPLINT->playerID);
  1721. pos = bg->center();
  1722. exit = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1723. exit->assignedKeys.insert(SDLK_ESCAPE);
  1724. //scenInfo = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 24, "ADVINFO.DEF",SDLK_i);
  1725. scenInfo = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 198, "ADVINFO.DEF",SDLK_i);
  1726. scenInfo->callback += CAdventureOptions::showScenarioInfo;
  1727. //viewWorld = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1728. puzzle = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 81, "ADVPUZ.DEF");
  1729. puzzle->callback += boost::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT);
  1730. dig = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 139, "ADVDIG.DEF");
  1731. if(const CGHeroInstance *h = adventureInt->curHero())
  1732. dig->callback += boost::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h);
  1733. else
  1734. dig->block(true);
  1735. }
  1736. CAdventureOptions::~CAdventureOptions()
  1737. {
  1738. }
  1739. void CAdventureOptions::showScenarioInfo()
  1740. {
  1741. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1742. }