CAdvmapInterface.cpp 55 KB

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