CAdvmapInterface.cpp 44 KB

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