CAdvmapInterface.cpp 55 KB

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