CAdvmapInterface.cpp 55 KB

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