CAdvmapInterface.cpp 55 KB

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