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