CAdvmapInterface.cpp 48 KB

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