CAdvmapInterface.cpp 44 KB

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