CAdvmapInterface.cpp 54 KB

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