CAdvmapInterface.cpp 45 KB

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