CAdvmapInterface.cpp 54 KB

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