CAdvmapInterface.cpp 56 KB

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