CAdvmapInterface.cpp 53 KB

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