CAdvmapInterface.cpp 48 KB

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