CAdvmapInterface.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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)
  452. {
  453. if (currentPath->endPos() == mp) //we'll be moving
  454. {
  455. LOCPLINT->pim->unlock();
  456. LOCPLINT->moveHero(currentHero,*currentPath);
  457. LOCPLINT->pim->lock();
  458. }
  459. else //remove an old path
  460. {
  461. delete currentPath;
  462. currentPath=NULL;
  463. LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.getPosOfHero(LOCPLINT->adventureInt->selection)].second = NULL;
  464. }
  465. }
  466. else //remove old path and find a new one
  467. {
  468. int3 bufpos = currentHero->getPosition(false);
  469. CPath *& pathForCurhero = LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.selected].second;
  470. if(pathForCurhero)
  471. delete pathForCurhero;
  472. currentPath = pathForCurhero = LOCPLINT->cb->getPath(bufpos, mp, currentHero);;
  473. }
  474. } //end of hero is selected "case"
  475. }
  476. void CTerrainRect::clickRight(tribool down)
  477. {
  478. int3 mp = whichTileIsIt();
  479. if ((mp.x<0)
  480. || (mp.y<0)
  481. || down != true
  482. )
  483. {
  484. LOCPLINT->adventureInt->handleRightClick("",down,this);
  485. return;
  486. }
  487. std::vector < const CGObjectInstance * > objs = LOCPLINT->cb->getBlockingObjs(mp);
  488. if(!objs.size())
  489. return;
  490. const CGObjectInstance * obj = objs[objs.size()-1];
  491. switch(obj->ID)
  492. {
  493. case HEROI_TYPE:
  494. {
  495. if(!vstd::contains(graphics->heroWins,obj->subID))
  496. {
  497. tlog3 << "Warning - no infowin for hero " << obj->subID << std::endl;
  498. break;
  499. }
  500. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[obj->subID],
  501. LOCPLINT->current->motion.x-graphics->heroWins[obj->subID]->w,
  502. LOCPLINT->current->motion.y-graphics->heroWins[obj->subID]->h,false
  503. );
  504. LOCPLINT->pushInt(ip);
  505. break;
  506. }
  507. case TOWNI_TYPE:
  508. {
  509. if(!vstd::contains(graphics->townWins,obj->id))
  510. {
  511. tlog3 << "Warning - no infowin for town " << obj->id << std::endl;
  512. break;
  513. }
  514. CInfoPopup * ip = new CInfoPopup(graphics->townWins[obj->id],
  515. LOCPLINT->current->motion.x-graphics->townWins[obj->id]->w,
  516. LOCPLINT->current->motion.y-graphics->townWins[obj->id]->h,false
  517. );
  518. LOCPLINT->pushInt(ip);
  519. break;
  520. }
  521. default:
  522. {
  523. LOCPLINT->adventureInt->handleRightClick(obj->getHoverText(),down,this);
  524. break;
  525. }
  526. }
  527. }
  528. void CTerrainRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  529. {
  530. int3 pom=LOCPLINT->adventureInt->verifyPos(whichTileIsIt(sEvent.x,sEvent.y));
  531. if (pom!=curHoveredTile)
  532. curHoveredTile=pom;
  533. else
  534. return;
  535. std::vector<std::string> temp = LOCPLINT->cb->getObjDescriptions(pom);
  536. if (temp.size())
  537. {
  538. boost::replace_all(temp.back(),"\n"," ");
  539. LOCPLINT->adventureInt->statusbar.print(temp.back());
  540. }
  541. else
  542. {
  543. LOCPLINT->adventureInt->statusbar.clear();
  544. }
  545. std::vector<const CGObjectInstance *> objs = LOCPLINT->cb->getVisitableObjs(pom);
  546. for(int i=0; i<objs.size();i++)
  547. {
  548. if(objs[i]->ID == TOWNI_TYPE) //town
  549. {
  550. CGI->curh->changeGraphic(0,0);
  551. return;
  552. }
  553. }
  554. objs = LOCPLINT->cb->getBlockingObjs(pom);
  555. for(size_t i=0; i < objs.size(); ++i)
  556. {
  557. if(objs[i]->ID == TOWNI_TYPE && objs[i]->tempOwner == LOCPLINT->playerID) //town
  558. {
  559. CGI->curh->changeGraphic(0,3);
  560. return;
  561. }
  562. else if(objs[i]->ID == HEROI_TYPE //mouse over hero
  563. && (objs[i]==LOCPLINT->adventureInt->selection || LOCPLINT->adventureInt->selection->ID==TOWNI_TYPE)
  564. && objs[i]->tempOwner == LOCPLINT->playerID) //this hero is selected or we've selected a town
  565. {
  566. CGI->curh->changeGraphic(0,2);
  567. return;
  568. }
  569. }
  570. CGI->curh->changeGraphic(0,0);
  571. }
  572. void CTerrainRect::hover(bool on)
  573. {
  574. if (!on)
  575. LOCPLINT->adventureInt->statusbar.clear();
  576. }
  577. void CTerrainRect::showPath(const SDL_Rect * extRect)
  578. {
  579. for (size_t i=0; i < currentPath->nodes.size()-1; ++i)
  580. {
  581. int pn=-1;//number of picture
  582. if (i==0) //last tile
  583. {
  584. int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+7,
  585. y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+6;
  586. if (x<0 || y<0 || x>pos.w || y>pos.h)
  587. continue;
  588. pn=0;
  589. }
  590. else
  591. {
  592. /*
  593. * notation of arrow direction:
  594. * 1 2 3
  595. * 4 5 6
  596. * 7 8 9
  597. * ie. 157 means an arrow from left upper tile to left bottom tile through 5 (all arrows go through 5 in this notation)
  598. */
  599. std::vector<CPathNode> & cv = currentPath->nodes;
  600. if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y-1) //15x
  601. {
  602. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //156
  603. {
  604. pn = 3;
  605. }
  606. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //159
  607. {
  608. pn = 12;
  609. }
  610. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //158
  611. {
  612. pn = 21;
  613. }
  614. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //157
  615. {
  616. pn = 22;
  617. }
  618. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //153
  619. {
  620. pn = 2;
  621. }
  622. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //154
  623. {
  624. pn = 23;
  625. }
  626. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //152
  627. {
  628. pn = 1;
  629. }
  630. }
  631. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y-1) //25x
  632. {
  633. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //253
  634. {
  635. pn = 2;
  636. }
  637. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //256
  638. {
  639. pn = 3;
  640. }
  641. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //259
  642. {
  643. pn = 4;
  644. }
  645. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //258
  646. {
  647. pn = 13;
  648. }
  649. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //257
  650. {
  651. pn = 22;
  652. }
  653. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //254
  654. {
  655. pn = 23;
  656. }
  657. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //251
  658. {
  659. pn = 24;
  660. }
  661. }
  662. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y-1) //35x
  663. {
  664. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //358
  665. {
  666. pn = 5;
  667. }
  668. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //357
  669. {
  670. pn = 14;
  671. }
  672. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //354
  673. {
  674. pn = 23;
  675. }
  676. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //351
  677. {
  678. pn = 24;
  679. }
  680. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //359
  681. {
  682. pn = 4;
  683. }
  684. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //356
  685. {
  686. pn = 3;
  687. }
  688. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //352
  689. {
  690. pn = 17;
  691. }
  692. }
  693. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y) //65x
  694. {
  695. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //657
  696. {
  697. pn = 6;
  698. }
  699. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //654
  700. {
  701. pn = 15;
  702. }
  703. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //651
  704. {
  705. pn = 24;
  706. }
  707. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //652
  708. {
  709. pn = 17;
  710. }
  711. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //658
  712. {
  713. pn = 5;
  714. }
  715. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //653
  716. {
  717. pn = 18;
  718. }
  719. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //659
  720. {
  721. pn = 4;
  722. }
  723. }
  724. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y+1) //95x
  725. {
  726. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //954
  727. {
  728. pn = 7;
  729. }
  730. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //951
  731. {
  732. pn = 16;
  733. }
  734. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //952
  735. {
  736. pn = 17;
  737. }
  738. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //957
  739. {
  740. pn = 6;
  741. }
  742. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //953
  743. {
  744. pn = 18;
  745. }
  746. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //956
  747. {
  748. pn = 19;
  749. }
  750. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //958
  751. {
  752. pn = 5;
  753. }
  754. }
  755. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y+1) //85x
  756. {
  757. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //857
  758. {
  759. pn = 6;
  760. }
  761. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //854
  762. {
  763. pn = 7;
  764. }
  765. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //851
  766. {
  767. pn = 8;
  768. }
  769. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //852
  770. {
  771. pn = 9;
  772. }
  773. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //853
  774. {
  775. pn = 18;
  776. }
  777. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //856
  778. {
  779. pn = 19;
  780. }
  781. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //859
  782. {
  783. pn = 20;
  784. }
  785. }
  786. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y+1) //75x
  787. {
  788. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //752
  789. {
  790. pn = 1;
  791. }
  792. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //753
  793. {
  794. pn = 10;
  795. }
  796. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //756
  797. {
  798. pn = 19;
  799. }
  800. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //751
  801. {
  802. pn = 8;
  803. }
  804. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //759
  805. {
  806. pn = 20;
  807. }
  808. }
  809. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y) //45x
  810. {
  811. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //453
  812. {
  813. pn = 2;
  814. }
  815. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //456
  816. {
  817. pn = 11;
  818. }
  819. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //459
  820. {
  821. pn = 20;
  822. }
  823. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //452
  824. {
  825. pn = 1;
  826. }
  827. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //456
  828. {
  829. pn = 21;
  830. }
  831. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //451
  832. {
  833. pn = 8;
  834. }
  835. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //457
  836. {
  837. pn = 22;
  838. }
  839. }
  840. }
  841. if ( ((currentPath->nodes[i].dist)-(*(currentPath->nodes.end()-1)).dist) > (static_cast<const CGHeroInstance*>(LOCPLINT->adventureInt->selection))->movement)
  842. pn+=25;
  843. if (pn>=0)
  844. {
  845. int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+7,
  846. y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+6;
  847. if (x<0 || y<0 || x>pos.w || y>pos.h)
  848. continue;
  849. int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
  850. hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
  851. SDL_Rect prevClip;
  852. SDL_GetClipRect(screen, &prevClip);
  853. SDL_SetClipRect(screen, extRect); //preventing blitting outside of that rect
  854. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  855. {
  856. if (hvx<0 && hvy<0)
  857. {
  858. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, screen, &genRect(32, 32, x + moveX, y + moveY));
  859. }
  860. else if(hvx<0)
  861. {
  862. CSDL_Ext::blit8bppAlphaTo24bpp
  863. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  864. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY));
  865. }
  866. else if (hvy<0)
  867. {
  868. CSDL_Ext::blit8bppAlphaTo24bpp
  869. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  870. screen, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  871. }
  872. else
  873. {
  874. CSDL_Ext::blit8bppAlphaTo24bpp
  875. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  876. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  877. }
  878. }
  879. else //standard version
  880. {
  881. if (hvx<0 && hvy<0)
  882. {
  883. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, screen, &genRect(32, 32, x, y));
  884. }
  885. else if(hvx<0)
  886. {
  887. CSDL_Ext::blit8bppAlphaTo24bpp
  888. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  889. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y));
  890. }
  891. else if (hvy<0)
  892. {
  893. CSDL_Ext::blit8bppAlphaTo24bpp
  894. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  895. screen, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  896. }
  897. else
  898. {
  899. CSDL_Ext::blit8bppAlphaTo24bpp
  900. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  901. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  902. }
  903. }
  904. SDL_SetClipRect(screen, &prevClip);
  905. }
  906. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  907. }
  908. void CTerrainRect::show(SDL_Surface * to)
  909. {
  910. CGI->mh->terrainRect
  911. (LOCPLINT->adventureInt->position.x,LOCPLINT->adventureInt->position.y,
  912. tilesw,tilesh,LOCPLINT->adventureInt->position.z,LOCPLINT->adventureInt->anim,
  913. &LOCPLINT->cb->getVisibilityMap(), true, LOCPLINT->adventureInt->heroAnim,
  914. to, &genRect(pos.h, pos.w, pos.x, pos.y), moveX, moveY, ADVOPT.smoothMove && (moveX != 0 || moveY != 0)
  915. );
  916. //SDL_BlitSurface(teren,&genRect(pos.h,pos.w,0,0),screen,&genRect(547,594,7,6));
  917. //SDL_FreeSurface(teren);
  918. if (currentPath && LOCPLINT->adventureInt->position.z==currentPath->startPos().z) //drawing path
  919. {
  920. showPath(&genRect(pos.h, pos.w, pos.x, pos.y));
  921. }
  922. }
  923. int3 CTerrainRect::whichTileIsIt(const int & x, const int & y)
  924. {
  925. int3 ret;
  926. ret.x = LOCPLINT->adventureInt->position.x + ((LOCPLINT->current->motion.x-pos.x)/32);
  927. ret.y = LOCPLINT->adventureInt->position.y + ((LOCPLINT->current->motion.y-pos.y)/32);
  928. ret.z = LOCPLINT->adventureInt->position.z;
  929. return ret;
  930. }
  931. int3 CTerrainRect::whichTileIsIt()
  932. {
  933. return whichTileIsIt(LOCPLINT->current->motion.x,LOCPLINT->current->motion.y);
  934. }
  935. void CResDataBar::clickRight (tribool down)
  936. {
  937. }
  938. void CResDataBar::activate()
  939. {
  940. ClickableR::activate();
  941. }
  942. void CResDataBar::deactivate()
  943. {
  944. ClickableR::deactivate();
  945. }
  946. CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
  947. {
  948. bg = BitmapHandler::loadBitmap(defname);
  949. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  950. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  951. pos = genRect(bg->h,bg->w,x,y);
  952. txtpos.resize(8);
  953. for (int i = 0; i < 8 ; i++)
  954. {
  955. txtpos[i].first = pos.x + offx + resdist*i;
  956. txtpos[i].second = pos.y + offy;
  957. }
  958. txtpos[7].first = txtpos[6].first + datedist;
  959. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  960. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  961. }
  962. CResDataBar::CResDataBar()
  963. {
  964. bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
  965. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  966. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  967. pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
  968. txtpos.resize(8);
  969. for (int i = 0; i < 8 ; i++)
  970. {
  971. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  972. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  973. }
  974. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  975. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  976. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  977. }
  978. CResDataBar::~CResDataBar()
  979. {
  980. SDL_FreeSurface(bg);
  981. }
  982. void CResDataBar::draw(SDL_Surface * to)
  983. {
  984. blitAt(bg,pos.x,pos.y,to);
  985. char * buf = new char[15];
  986. for (int i=0;i<7;i++)
  987. {
  988. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  989. printAt(buf,txtpos[i].first,txtpos[i].second,GEOR13,zwykly,to);
  990. }
  991. std::vector<std::string> temp;
  992. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp+=std::string(buf);
  993. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp+=std::string(buf);
  994. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp+=std::string(buf);
  995. printAt(processStr(datetext,temp),txtpos[7].first,txtpos[7].second,GEOR13,zwykly,to);
  996. temp.clear();
  997. //updateRect(&pos,screen);
  998. delete[] buf;
  999. }
  1000. CInfoBar::CInfoBar()
  1001. {
  1002. toNextTick = mode = pom = -1;
  1003. pos.x=ADVOPT.infoboxX;
  1004. pos.y=ADVOPT.infoboxY;
  1005. pos.w=194;
  1006. pos.h=186;
  1007. day = CDefHandler::giveDef("NEWDAY.DEF");
  1008. week1 = CDefHandler::giveDef("NEWWEEK1.DEF");
  1009. week2 = CDefHandler::giveDef("NEWWEEK2.DEF");
  1010. week3 = CDefHandler::giveDef("NEWWEEK3.DEF");
  1011. week4 = CDefHandler::giveDef("NEWWEEK4.DEF");
  1012. }
  1013. CInfoBar::~CInfoBar()
  1014. {
  1015. delete day;
  1016. delete week1;
  1017. delete week2;
  1018. delete week3;
  1019. delete week4;
  1020. }
  1021. void CInfoBar::draw(SDL_Surface * to, const CGObjectInstance * specific)
  1022. {
  1023. if ((mode>=0) && mode<5)
  1024. {
  1025. blitAnim(mode);
  1026. return;
  1027. }
  1028. else if (mode==5)
  1029. {
  1030. mode = -1;
  1031. draw(to,LOCPLINT->adventureInt->selection);
  1032. }
  1033. if (!specific)
  1034. specific = LOCPLINT->adventureInt->selection;
  1035. if(!specific)
  1036. return;
  1037. if(specific->ID == HEROI_TYPE) //hero
  1038. {
  1039. if(graphics->heroWins.find(specific->subID)!=graphics->heroWins.end())
  1040. blitAt(graphics->heroWins[specific->subID],pos.x,pos.y,to);
  1041. }
  1042. else if (specific->ID == TOWNI_TYPE)
  1043. {
  1044. const CGTownInstance * t = static_cast<const CGTownInstance*>(specific);
  1045. if(graphics->townWins.find(t->id)!=graphics->townWins.end())
  1046. blitAt(graphics->townWins[t->id],pos.x,pos.y,to);
  1047. }
  1048. }
  1049. CDefHandler * CInfoBar::getAnim(int mode)
  1050. {
  1051. switch(mode)
  1052. {
  1053. case 0:
  1054. return day;
  1055. break;
  1056. case 1:
  1057. return week1;
  1058. break;
  1059. case 2:
  1060. return week2;
  1061. break;
  1062. case 3:
  1063. return week3;
  1064. break;
  1065. case 4:
  1066. return week4;
  1067. break;
  1068. default:
  1069. return NULL;
  1070. break;
  1071. }
  1072. }
  1073. void CInfoBar::blitAnim(int mode)//0 - day, 1 - week
  1074. {
  1075. CDefHandler * anim = NULL;
  1076. std::stringstream txt;
  1077. anim = getAnim(mode);
  1078. if(mode) //new week animation
  1079. {
  1080. txt << CGI->generaltexth->allTexts[63] << " " << LOCPLINT->cb->getDate(2);
  1081. }
  1082. else //new day
  1083. {
  1084. txt << CGI->generaltexth->allTexts[64] << " " << LOCPLINT->cb->getDate(1);
  1085. }
  1086. blitAt(anim->ourImages[pom].bitmap,pos.x+9,pos.y+10);
  1087. printAtMiddle(txt.str(),pos.x+95,pos.y+31,TNRB16,zwykly);
  1088. if (pom == anim->ourImages.size()-1)
  1089. toNextTick+=750;
  1090. }
  1091. void CInfoBar::newDay(int Day)
  1092. {
  1093. if(LOCPLINT->cb->getDate(1) != 1)
  1094. {
  1095. mode = 0; //showing day
  1096. }
  1097. else
  1098. {
  1099. switch(LOCPLINT->cb->getDate(2))
  1100. {
  1101. case 1:
  1102. mode = 1;
  1103. break;
  1104. case 2:
  1105. mode = 2;
  1106. break;
  1107. case 3:
  1108. mode = 3;
  1109. break;
  1110. case 4:
  1111. mode = 4;
  1112. break;
  1113. default:
  1114. mode = -1;
  1115. break;
  1116. }
  1117. }
  1118. pom = 0;
  1119. TimeInterested::activate();
  1120. toNextTick = 500;
  1121. blitAnim(mode);
  1122. //blitAt(day->ourImages[pom].bitmap,pos.x+10,pos.y+10);
  1123. }
  1124. void CInfoBar::showComp(SComponent * comp, int time)
  1125. {
  1126. SDL_Surface * b = BitmapHandler::loadBitmap("ADSTATOT.bmp");
  1127. blitAt(b,pos.x+8,pos.y+11);
  1128. blitAt(comp->getImg(),pos.x+52,pos.y+54);
  1129. printAtMiddle(comp->subtitle,pos.x+91,pos.y+158,GEOR13,zwykly);
  1130. printAtMiddleWB(comp->description,pos.x+94,pos.y+31,GEOR13,26,zwykly);
  1131. SDL_FreeSurface(b);
  1132. TimeInterested::activate();
  1133. mode = 6;
  1134. toNextTick = time;
  1135. }
  1136. void CInfoBar::tick()
  1137. {
  1138. if((mode >= 0) && (mode < 5))
  1139. {
  1140. pom++;
  1141. if (pom >= getAnim(mode)->ourImages.size())
  1142. {
  1143. TimeInterested::deactivate();
  1144. toNextTick = -1;
  1145. mode = 5;
  1146. draw(screen2);
  1147. return;
  1148. }
  1149. toNextTick = 150;
  1150. blitAnim(mode);
  1151. }
  1152. else if (mode == 6)
  1153. {
  1154. TimeInterested::deactivate();
  1155. toNextTick = -1;
  1156. mode = 5;
  1157. draw(screen2);
  1158. }
  1159. }
  1160. CAdvMapInt::CAdvMapInt(int Player)
  1161. :player(Player),
  1162. statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  1163. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  1164. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  1165. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  1166. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  1167. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  1168. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  1169. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  1170. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  1171. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  1172. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  1173. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  1174. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  1175. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  1176. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  1177. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  1178. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  1179. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  1180. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  1181. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  1182. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  1183. heroList(ADVOPT.hlistSize),
  1184. townList(ADVOPT.tlistSize,ADVOPT.tlistX,ADVOPT.tlistY,ADVOPT.tlistAU,ADVOPT.tlistAD)//(5,&genRect(192,48,747,196),747,196,747,372),
  1185. {
  1186. active = 0;
  1187. subInt = NULL;
  1188. selection = NULL;
  1189. townList.fun = boost::bind(&CAdvMapInt::selectionChanged,this);
  1190. LOCPLINT->adventureInt=this;
  1191. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  1192. graphics->blueToPlayersAdv(bg,player);
  1193. scrollingDir = 0;
  1194. updateScreen = false;
  1195. anim=0;
  1196. animValHitCount=0; //animation frame
  1197. heroAnim=0;
  1198. heroAnimValHitCount=0; // hero animation frame
  1199. heroList.init();
  1200. heroList.genList();
  1201. //townList.init();
  1202. townList.genList();
  1203. heroWindow = new CHeroWindow(this->player);
  1204. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[0]));
  1205. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[1]));
  1206. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[2]));
  1207. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[3]));
  1208. }
  1209. CAdvMapInt::~CAdvMapInt()
  1210. {
  1211. SDL_FreeSurface(bg);
  1212. delete heroWindow;
  1213. for(int i=0; i<gems.size(); i++)
  1214. delete gems[i];
  1215. }
  1216. void CAdvMapInt::fshowOverview()
  1217. {
  1218. }
  1219. void CAdvMapInt::fswitchLevel()
  1220. {
  1221. if(!CGI->mh->map->twoLevel)
  1222. return;
  1223. if (position.z)
  1224. {
  1225. position.z--;
  1226. underground.curimg=0;
  1227. underground.show(screen2);
  1228. }
  1229. else
  1230. {
  1231. underground.curimg=1;
  1232. position.z++;
  1233. underground.show(screen2);
  1234. }
  1235. updateScreen = true;
  1236. minimap.draw(screen2);
  1237. }
  1238. void CAdvMapInt::fshowQuestlog()
  1239. {
  1240. }
  1241. void CAdvMapInt::fsleepWake()
  1242. {
  1243. }
  1244. void CAdvMapInt::fmoveHero()
  1245. {
  1246. if (selection->ID!=HEROI_TYPE)
  1247. return;
  1248. if (!terrain.currentPath)
  1249. return;
  1250. LOCPLINT->pim->unlock();
  1251. LOCPLINT->moveHero(static_cast<const CGHeroInstance*>(LOCPLINT->adventureInt->selection),*terrain.currentPath);
  1252. LOCPLINT->pim->lock();
  1253. }
  1254. void CAdvMapInt::fshowSpellbok()
  1255. {
  1256. if (selection->ID!=HEROI_TYPE) //checking necessary values
  1257. return;
  1258. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), (static_cast<const CGHeroInstance*>(LOCPLINT->adventureInt->selection)));
  1259. LOCPLINT->pushInt(spellWindow);
  1260. }
  1261. void CAdvMapInt::fadventureOPtions()
  1262. {
  1263. }
  1264. void CAdvMapInt::fsystemOptions()
  1265. {
  1266. CSystemOptionsWindow * sysopWindow = new CSystemOptionsWindow(genRect(487, 481, 159, 57), LOCPLINT);
  1267. LOCPLINT->pushInt(sysopWindow);
  1268. }
  1269. void CAdvMapInt::fnextHero()
  1270. {
  1271. if(!heroList.items.size()) //no wandering heroes
  1272. return;
  1273. int start = heroList.selected;
  1274. int i = start;
  1275. do
  1276. {
  1277. i++;
  1278. if(i >= heroList.items.size())
  1279. i = 0;
  1280. } while (!heroList.items[i].first->movement && i!=start);
  1281. heroList.select(i);
  1282. }
  1283. void CAdvMapInt::fendTurn()
  1284. {
  1285. LOCPLINT->makingTurn = false;
  1286. }
  1287. void CAdvMapInt::activate()
  1288. {
  1289. if(subInt == heroWindow)
  1290. {
  1291. heroWindow->activate();
  1292. return;
  1293. }
  1294. if(active++)
  1295. {
  1296. tlog1 << "Error: advmapint already active...\n";
  1297. active--;
  1298. return;
  1299. }
  1300. LOCPLINT->statusbar = &statusbar;
  1301. kingOverview.activate();
  1302. underground.activate();
  1303. questlog.activate();
  1304. sleepWake.activate();
  1305. moveHero.activate();
  1306. spellbook.activate();
  1307. sysOptions.activate();
  1308. advOptions.activate();
  1309. nextHero.activate();
  1310. endTurn.activate();
  1311. minimap.activate();
  1312. heroList.activate();
  1313. townList.activate();
  1314. terrain.activate();
  1315. KeyInterested::activate();
  1316. LOCPLINT->cingconsole->activate();
  1317. }
  1318. void CAdvMapInt::deactivate()
  1319. {
  1320. if(subInt == heroWindow)
  1321. {
  1322. heroWindow->deactivate();
  1323. return;
  1324. }
  1325. KeyInterested::deactivate();
  1326. hide();
  1327. LOCPLINT->cingconsole->deactivate();
  1328. if(--active)
  1329. {
  1330. tlog1 << "Error: advmapint still active...\n";
  1331. deactivate();
  1332. }
  1333. }
  1334. void CAdvMapInt::showAll(SDL_Surface *to)
  1335. {
  1336. blitAt(bg,0,0,to);
  1337. kingOverview.show(to);
  1338. underground.show(to);
  1339. questlog.show(to);
  1340. sleepWake.show(to);
  1341. moveHero.show(to);
  1342. spellbook.show(to);
  1343. advOptions.show(to);
  1344. sysOptions.show(to);
  1345. nextHero.show(to);
  1346. endTurn.show(to);
  1347. minimap.draw(to);
  1348. heroList.draw(to);
  1349. townList.draw(to);
  1350. updateScreen = true;
  1351. show(to);
  1352. resdatabar.draw(to);
  1353. statusbar.show(to);
  1354. infoBar.draw(to);
  1355. LOCPLINT->cingconsole->show(to);
  1356. }
  1357. void CAdvMapInt::hide()
  1358. {
  1359. CGI->curh->changeGraphic(0,0);
  1360. kingOverview.deactivate();
  1361. underground.deactivate();
  1362. questlog.deactivate();
  1363. sleepWake.deactivate();
  1364. moveHero.deactivate();
  1365. spellbook.deactivate();
  1366. advOptions.deactivate();
  1367. sysOptions.deactivate();
  1368. nextHero.deactivate();
  1369. endTurn.deactivate();
  1370. minimap.deactivate();
  1371. heroList.deactivate();
  1372. townList.deactivate();
  1373. terrain.deactivate();
  1374. if(std::find(LOCPLINT->timeinterested.begin(),LOCPLINT->timeinterested.end(),&infoBar)!=LOCPLINT->timeinterested.end())
  1375. LOCPLINT->timeinterested.erase(std::find(LOCPLINT->timeinterested.begin(),LOCPLINT->timeinterested.end(),&infoBar));
  1376. infoBar.mode=-1;
  1377. }
  1378. void CAdvMapInt::show(SDL_Surface *to)
  1379. {
  1380. ++animValHitCount; //for animations
  1381. if(animValHitCount == 8)
  1382. {
  1383. CGI->mh->updateWater();
  1384. animValHitCount = 0;
  1385. ++anim;
  1386. updateScreen = true;
  1387. }
  1388. ++heroAnim;
  1389. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  1390. if((animValHitCount % (4/LOCPLINT->mapScrollingSpeed)) == 0
  1391. &&
  1392. (LOCPLINT->topInt() == this)
  1393. || SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1394. || SDL_GetKeyState(NULL)[SDLK_RCTRL]
  1395. )
  1396. {
  1397. if( (scrollingDir & LEFT) && (position.x>-Woff) )
  1398. position.x--;
  1399. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - terrain.tilesw + Woff) )
  1400. position.x++;
  1401. if( (scrollingDir & UP) && (position.y>-Hoff) )
  1402. position.y--;
  1403. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - terrain.tilesh + Hoff) )
  1404. position.y++;
  1405. if(scrollingDir)
  1406. {
  1407. updateScreen = true;
  1408. updateMinimap=true;
  1409. }
  1410. }
  1411. if(updateScreen)
  1412. {
  1413. terrain.show(to);
  1414. for(int i=0;i<4;i++)
  1415. blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  1416. updateScreen=false;
  1417. LOCPLINT->cingconsole->show(to);
  1418. }
  1419. if (updateMinimap)
  1420. {
  1421. minimap.draw(to);
  1422. updateMinimap=false;
  1423. }
  1424. }
  1425. void CAdvMapInt::selectionChanged()
  1426. {
  1427. const CGTownInstance *to = townList.items[townList.selected];
  1428. select(to);
  1429. }
  1430. void CAdvMapInt::centerOn(int3 on)
  1431. {
  1432. on.x -= (LOCPLINT->adventureInt->terrain.tilesw/2);
  1433. on.y -= (LOCPLINT->adventureInt->terrain.tilesh/2);
  1434. on = LOCPLINT->repairScreenPos(on);
  1435. LOCPLINT->adventureInt->position.x=on.x;
  1436. LOCPLINT->adventureInt->position.y=on.y;
  1437. LOCPLINT->adventureInt->position.z=on.z;
  1438. LOCPLINT->adventureInt->updateScreen=true;
  1439. updateMinimap=true;
  1440. }
  1441. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1442. {
  1443. ui8 Dir;
  1444. switch(key.keysym.sym)
  1445. {
  1446. case SDLK_UP:
  1447. Dir = UP;
  1448. break;
  1449. case SDLK_LEFT:
  1450. Dir = LEFT;
  1451. break;
  1452. case SDLK_RIGHT:
  1453. Dir = RIGHT;
  1454. break;
  1455. case SDLK_DOWN:
  1456. Dir = DOWN;
  1457. break;
  1458. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1459. {
  1460. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance*>(selection);
  1461. if(h && key.state == SDL_PRESSED)
  1462. {
  1463. LOCPLINT->pim->unlock();
  1464. LOCPLINT->cb->moveHero(h,h->pos);
  1465. LOCPLINT->pim->lock();
  1466. }
  1467. }
  1468. return;
  1469. default:
  1470. return;
  1471. }
  1472. if(key.state == SDL_PRESSED //arrow is pressed
  1473. && (SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1474. || SDL_GetKeyState(NULL)[SDLK_RCTRL])
  1475. )
  1476. scrollingDir |= Dir;
  1477. else
  1478. scrollingDir &= ~Dir;
  1479. }
  1480. void CAdvMapInt::handleRightClick(std::string text, tribool down, CIntObject * client)
  1481. {
  1482. if (down)
  1483. {
  1484. //boost::algorithm::erase_all(text,"\"");
  1485. CSimpleWindow * temp = CMessage::genWindow(text,LOCPLINT->playerID);
  1486. temp->pos.x=screen->w/2-(temp->pos.w/2);
  1487. temp->pos.y=screen->h/2-(temp->pos.h/2);
  1488. temp->owner = client;
  1489. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  1490. LOCPLINT->pushInt(rcpi);
  1491. }
  1492. }
  1493. int3 CAdvMapInt::verifyPos(int3 ver)
  1494. {
  1495. if (ver.x<0)
  1496. ver.x=0;
  1497. if (ver.y<0)
  1498. ver.y=0;
  1499. if (ver.z<0)
  1500. ver.z=0;
  1501. if (ver.x>=CGI->mh->sizes.x)
  1502. ver.x=CGI->mh->sizes.x-1;
  1503. if (ver.y>=CGI->mh->sizes.y)
  1504. ver.y=CGI->mh->sizes.y-1;
  1505. if (ver.z>=CGI->mh->sizes.z)
  1506. ver.z=CGI->mh->sizes.z-1;
  1507. return ver;
  1508. }
  1509. void CAdvMapInt::select(const CArmedInstance *sel )
  1510. {
  1511. LOCPLINT->cb->setSelection(sel);
  1512. centerOn(sel->pos);
  1513. selection = sel;
  1514. if(sel->ID==TOWNI_TYPE)
  1515. {
  1516. int pos = vstd::findPos(townList.items,sel);
  1517. townList.selected = pos;
  1518. terrain.currentPath = NULL;
  1519. }
  1520. else
  1521. {
  1522. int pos = heroList.getPosOfHero(sel);
  1523. heroList.selected = pos;
  1524. terrain.currentPath = heroList.items[pos].second;
  1525. }
  1526. townList.draw(screen);
  1527. heroList.draw(screen);
  1528. infoBar.draw(screen);
  1529. }