CAdvmapInterface.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  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 (!CGI->mh->map->isInTheMap(mp) || down != true)
  512. return;
  513. std::vector < const CGObjectInstance * > objs = LOCPLINT->cb->getBlockingObjs(mp);
  514. if(!objs.size()) {
  515. // Bare or undiscovered terrain
  516. const TerrainTile * tile = LOCPLINT->cb->getTileInfo(mp);
  517. if (tile) {
  518. CSimpleWindow * temp = CMessage::genWindow(VLC->generaltexth->terrainNames[tile->tertype],LOCPLINT->playerID,true);
  519. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  520. GH.pushInt(rcpi);
  521. }
  522. return;
  523. }
  524. const CGObjectInstance * obj = objs.back();
  525. switch(obj->ID)
  526. {
  527. case HEROI_TYPE:
  528. {
  529. if(!vstd::contains(graphics->heroWins,obj->subID) || obj->tempOwner != LOCPLINT->playerID)
  530. {
  531. InfoAboutHero iah;
  532. if(LOCPLINT->cb->getHeroInfo(obj, iah))
  533. {
  534. SDL_Surface *iwin = graphics->drawHeroInfoWin(iah);
  535. CInfoPopup * ip = new CInfoPopup(iwin, GH.current->motion.x-iwin->w,
  536. GH.current->motion.y-iwin->h, true);
  537. GH.pushInt(ip);
  538. }
  539. else
  540. {
  541. tlog3 << "Warning - no infowin for hero " << obj->id << std::endl;
  542. }
  543. }
  544. else
  545. {
  546. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[obj->subID],
  547. GH.current->motion.x-graphics->heroWins[obj->subID]->w,
  548. GH.current->motion.y-graphics->heroWins[obj->subID]->h,false
  549. );
  550. GH.pushInt(ip);
  551. }
  552. break;
  553. }
  554. case TOWNI_TYPE:
  555. {
  556. if(!vstd::contains(graphics->townWins,obj->id) || obj->tempOwner != LOCPLINT->playerID)
  557. {
  558. InfoAboutTown iah;
  559. if(LOCPLINT->cb->getTownInfo(obj, iah))
  560. {
  561. SDL_Surface *iwin = graphics->drawTownInfoWin(iah);
  562. CInfoPopup * ip = new CInfoPopup(iwin, GH.current->motion.x - iwin->w/2,
  563. GH.current->motion.y - iwin->h/2, true);
  564. GH.pushInt(ip);
  565. }
  566. else
  567. {
  568. tlog3 << "Warning - no infowin for town " << obj->id << std::endl;
  569. }
  570. }
  571. else
  572. {
  573. CInfoPopup * ip = new CInfoPopup(graphics->townWins[obj->id],
  574. GH.current->motion.x - graphics->townWins[obj->id]->w/2,
  575. GH.current->motion.y - graphics->townWins[obj->id]->h/2,false
  576. );
  577. GH.pushInt(ip);
  578. }
  579. break;
  580. }
  581. case 33: // Garrison
  582. case 219:
  583. {
  584. const CGGarrison *garr = dynamic_cast<const CGGarrison *>(obj);
  585. if (garr != NULL) {
  586. InfoAboutTown iah;
  587. iah.fortLevel = 0;
  588. iah.army = garr->army;
  589. iah.name = std::string("Garrison");
  590. iah.owner = garr->tempOwner;
  591. iah.built = false;
  592. iah.details = NULL; // TODO: Find a suitable way to show detailed info.
  593. iah.tType = NULL;
  594. SDL_Surface *iwin = graphics->drawTownInfoWin(iah);
  595. CInfoPopup * ip = new CInfoPopup(iwin,
  596. GH.current->motion.x - iwin->w/2,
  597. GH.current->motion.y - iwin->h/2, true);
  598. GH.pushInt(ip);
  599. }
  600. break;
  601. }
  602. default:
  603. {
  604. LOCPLINT->adventureInt->handleRightClick(obj->getHoverText(),down,this);
  605. break;
  606. }
  607. }
  608. }
  609. void CTerrainRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  610. {
  611. int3 tHovered = whichTileIsIt(sEvent.x,sEvent.y);
  612. int3 pom = LOCPLINT->adventureInt->verifyPos(tHovered);
  613. if(tHovered != pom) //tile outside the map
  614. {
  615. CGI->curh->changeGraphic(0, 0);
  616. return;
  617. }
  618. if (pom != curHoveredTile)
  619. curHoveredTile=pom;
  620. else
  621. return;
  622. std::vector<std::string> temp = LOCPLINT->cb->getObjDescriptions(pom);
  623. if (temp.size())
  624. {
  625. boost::replace_all(temp.back(),"\n"," ");
  626. LOCPLINT->adventureInt->statusbar.print(temp.back());
  627. }
  628. else
  629. {
  630. LOCPLINT->adventureInt->statusbar.clear();
  631. }
  632. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(pom);
  633. std::vector<const CGObjectInstance *> objs = LOCPLINT->cb->getBlockingObjs(pom);
  634. const CGObjectInstance *obj = objs.size() ? objs.back() : NULL;
  635. bool accessible = pnode->turns < 255;
  636. int turns = pnode->turns;
  637. amin(turns, 4);
  638. if(LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE)
  639. {
  640. if(obj)
  641. {
  642. if(obj->ID == TOWNI_TYPE)
  643. {
  644. CGI->curh->changeGraphic(0, 3);
  645. }
  646. else if(obj->ID == HEROI_TYPE)
  647. {
  648. CGI->curh->changeGraphic(0, 2);
  649. }
  650. }
  651. else
  652. {
  653. CGI->curh->changeGraphic(0, 0);
  654. }
  655. }
  656. else if(LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
  657. {
  658. const CGHeroInstance *h = static_cast<const CGHeroInstance *>(LOCPLINT->adventureInt->selection);
  659. if(obj)
  660. {
  661. if(obj->ID == HEROI_TYPE)
  662. {
  663. if(obj->tempOwner != LOCPLINT->playerID) //enemy hero TODO: allies
  664. {
  665. if(accessible)
  666. CGI->curh->changeGraphic(0, 5 + turns*6);
  667. else
  668. CGI->curh->changeGraphic(0, 0);
  669. }
  670. else //our hero
  671. {
  672. if(LOCPLINT->adventureInt->selection == obj)
  673. CGI->curh->changeGraphic(0, 2);
  674. else if(accessible)
  675. CGI->curh->changeGraphic(0, 8 + turns*6);
  676. else
  677. CGI->curh->changeGraphic(0, 2);
  678. }
  679. }
  680. else if(obj->ID == TOWNI_TYPE)
  681. {
  682. if(obj->tempOwner != LOCPLINT->playerID) //enemy town TODO: allies
  683. {
  684. if(accessible) {
  685. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(obj);
  686. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  687. if (townObj && townObj->army.slots.empty())
  688. CGI->curh->changeGraphic(0, 9 + turns*6);
  689. else
  690. CGI->curh->changeGraphic(0, 5 + turns*6);
  691. } else {
  692. CGI->curh->changeGraphic(0, 0);
  693. }
  694. }
  695. else //our town
  696. {
  697. if(accessible)
  698. CGI->curh->changeGraphic(0, 9 + turns*6);
  699. else
  700. CGI->curh->changeGraphic(0, 3);
  701. }
  702. }
  703. else if(obj->ID == 54) //monster
  704. {
  705. if(accessible)
  706. CGI->curh->changeGraphic(0, 5 + turns*6);
  707. else
  708. CGI->curh->changeGraphic(0, 0);
  709. }
  710. else if(obj->ID == 8) //boat
  711. {
  712. if(accessible)
  713. CGI->curh->changeGraphic(0, 6 + turns*6);
  714. else
  715. CGI->curh->changeGraphic(0, 0);
  716. }
  717. else if (obj->ID == 33 || obj->ID == 219) // Garrison
  718. {
  719. if (accessible) {
  720. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(obj);
  721. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  722. if (garrObj && garrObj->tempOwner != LOCPLINT->playerID
  723. && !garrObj->army.slots.empty())
  724. {
  725. CGI->curh->changeGraphic(0, 5 + turns*6);
  726. }
  727. else
  728. {
  729. CGI->curh->changeGraphic(0, 9 + turns*6);
  730. }
  731. } else {
  732. CGI->curh->changeGraphic(0, 0);
  733. }
  734. }
  735. else
  736. {
  737. if(accessible)
  738. {
  739. if(pnode->land)
  740. CGI->curh->changeGraphic(0, 9 + turns*6);
  741. else
  742. CGI->curh->changeGraphic(0, 28 + turns);
  743. }
  744. else
  745. CGI->curh->changeGraphic(0, 0);
  746. }
  747. }
  748. else //no objs
  749. {
  750. if(accessible)
  751. {
  752. if(pnode->land)
  753. {
  754. if(LOCPLINT->cb->getTileInfo(h->getPosition(false))->tertype != TerrainTile::water)
  755. CGI->curh->changeGraphic(0, 4 + turns*6);
  756. else
  757. CGI->curh->changeGraphic(0, 7 + turns*6); //anchor
  758. }
  759. else
  760. CGI->curh->changeGraphic(0, 6 + turns*6);
  761. }
  762. else
  763. CGI->curh->changeGraphic(0, 0);
  764. }
  765. }
  766. //tlog1 << "Tile " << pom << ": Turns=" << (int)pnode->turns <<" Move:=" << pnode->moveRemains <</* " (from " << ")" << */std::endl;
  767. }
  768. void CTerrainRect::hover(bool on)
  769. {
  770. if (!on)
  771. {
  772. LOCPLINT->adventureInt->statusbar.clear();
  773. CGI->curh->changeGraphic(0,0);
  774. }
  775. //Hoverable::hover(on);
  776. }
  777. void CTerrainRect::showPath(const SDL_Rect * extRect)
  778. {
  779. for (size_t i=0; i < currentPath->nodes.size()-1; ++i)
  780. {
  781. int pn=-1;//number of picture
  782. if (i==0) //last tile
  783. {
  784. int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  785. y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  786. if (x<0 || y<0 || x>pos.w || y>pos.h)
  787. continue;
  788. pn=0;
  789. }
  790. else
  791. {
  792. /*
  793. * notation of arrow direction:
  794. * 1 2 3
  795. * 4 5 6
  796. * 7 8 9
  797. * ie. 157 means an arrow from left upper tile to left bottom tile through 5 (all arrows go through 5 in this notation)
  798. */
  799. std::vector<CGPathNode> & cv = currentPath->nodes;
  800. if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y-1) //15x
  801. {
  802. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //156
  803. {
  804. pn = 3;
  805. }
  806. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //159
  807. {
  808. pn = 12;
  809. }
  810. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //158
  811. {
  812. pn = 21;
  813. }
  814. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //157
  815. {
  816. pn = 22;
  817. }
  818. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //153
  819. {
  820. pn = 2;
  821. }
  822. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //154
  823. {
  824. pn = 23;
  825. }
  826. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //152
  827. {
  828. pn = 1;
  829. }
  830. }
  831. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y-1) //25x
  832. {
  833. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //253
  834. {
  835. pn = 2;
  836. }
  837. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //256
  838. {
  839. pn = 3;
  840. }
  841. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //259
  842. {
  843. pn = 4;
  844. }
  845. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //258
  846. {
  847. pn = 13;
  848. }
  849. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //257
  850. {
  851. pn = 22;
  852. }
  853. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //254
  854. {
  855. pn = 23;
  856. }
  857. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //251
  858. {
  859. pn = 24;
  860. }
  861. }
  862. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y-1) //35x
  863. {
  864. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //358
  865. {
  866. pn = 5;
  867. }
  868. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //357
  869. {
  870. pn = 14;
  871. }
  872. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //354
  873. {
  874. pn = 23;
  875. }
  876. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //351
  877. {
  878. pn = 24;
  879. }
  880. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //359
  881. {
  882. pn = 4;
  883. }
  884. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //356
  885. {
  886. pn = 3;
  887. }
  888. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //352
  889. {
  890. pn = 17;
  891. }
  892. }
  893. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y) //65x
  894. {
  895. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //657
  896. {
  897. pn = 6;
  898. }
  899. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //654
  900. {
  901. pn = 15;
  902. }
  903. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //651
  904. {
  905. pn = 24;
  906. }
  907. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //652
  908. {
  909. pn = 17;
  910. }
  911. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //658
  912. {
  913. pn = 5;
  914. }
  915. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //653
  916. {
  917. pn = 18;
  918. }
  919. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //659
  920. {
  921. pn = 4;
  922. }
  923. }
  924. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y+1) //95x
  925. {
  926. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //954
  927. {
  928. pn = 7;
  929. }
  930. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //951
  931. {
  932. pn = 16;
  933. }
  934. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //952
  935. {
  936. pn = 17;
  937. }
  938. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //957
  939. {
  940. pn = 6;
  941. }
  942. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //953
  943. {
  944. pn = 18;
  945. }
  946. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //956
  947. {
  948. pn = 19;
  949. }
  950. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //958
  951. {
  952. pn = 5;
  953. }
  954. }
  955. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y+1) //85x
  956. {
  957. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //857
  958. {
  959. pn = 6;
  960. }
  961. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //854
  962. {
  963. pn = 7;
  964. }
  965. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //851
  966. {
  967. pn = 8;
  968. }
  969. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //852
  970. {
  971. pn = 9;
  972. }
  973. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //853
  974. {
  975. pn = 18;
  976. }
  977. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //856
  978. {
  979. pn = 19;
  980. }
  981. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //859
  982. {
  983. pn = 20;
  984. }
  985. }
  986. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y+1) //75x
  987. {
  988. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //752
  989. {
  990. pn = 1;
  991. }
  992. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //753
  993. {
  994. pn = 10;
  995. }
  996. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //756
  997. {
  998. pn = 19;
  999. }
  1000. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //751
  1001. {
  1002. pn = 8;
  1003. }
  1004. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //759
  1005. {
  1006. pn = 20;
  1007. }
  1008. }
  1009. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y) //45x
  1010. {
  1011. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //453
  1012. {
  1013. pn = 2;
  1014. }
  1015. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //456
  1016. {
  1017. pn = 11;
  1018. }
  1019. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //459
  1020. {
  1021. pn = 20;
  1022. }
  1023. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //452
  1024. {
  1025. pn = 1;
  1026. }
  1027. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //456
  1028. {
  1029. pn = 21;
  1030. }
  1031. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //451
  1032. {
  1033. pn = 8;
  1034. }
  1035. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //457
  1036. {
  1037. pn = 22;
  1038. }
  1039. }
  1040. }
  1041. if (currentPath->nodes[i].turns)
  1042. pn+=25;
  1043. if (pn>=0)
  1044. {
  1045. int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  1046. y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  1047. if (x<0 || y<0 || x>pos.w || y>pos.h)
  1048. continue;
  1049. int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
  1050. hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
  1051. SDL_Rect prevClip;
  1052. SDL_GetClipRect(screen, &prevClip);
  1053. SDL_SetClipRect(screen, extRect); //preventing blitting outside of that rect
  1054. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  1055. {
  1056. if (hvx<0 && hvy<0)
  1057. {
  1058. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, screen, &genRect(32, 32, x + moveX, y + moveY));
  1059. }
  1060. else if(hvx<0)
  1061. {
  1062. CSDL_Ext::blit8bppAlphaTo24bpp
  1063. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  1064. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY));
  1065. }
  1066. else if (hvy<0)
  1067. {
  1068. CSDL_Ext::blit8bppAlphaTo24bpp
  1069. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1070. screen, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  1071. }
  1072. else
  1073. {
  1074. CSDL_Ext::blit8bppAlphaTo24bpp
  1075. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1076. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  1077. }
  1078. }
  1079. else //standard version
  1080. {
  1081. if (hvx<0 && hvy<0)
  1082. {
  1083. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, screen, &genRect(32, 32, x, y));
  1084. }
  1085. else if(hvx<0)
  1086. {
  1087. CSDL_Ext::blit8bppAlphaTo24bpp
  1088. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  1089. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y));
  1090. }
  1091. else if (hvy<0)
  1092. {
  1093. CSDL_Ext::blit8bppAlphaTo24bpp
  1094. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1095. screen, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  1096. }
  1097. else
  1098. {
  1099. CSDL_Ext::blit8bppAlphaTo24bpp
  1100. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1101. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  1102. }
  1103. }
  1104. SDL_SetClipRect(screen, &prevClip);
  1105. }
  1106. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  1107. }
  1108. void CTerrainRect::show(SDL_Surface * to)
  1109. {
  1110. if(ADVOPT.smoothMove)
  1111. CGI->mh->terrainRect
  1112. (LOCPLINT->adventureInt->position, LOCPLINT->adventureInt->anim,
  1113. &LOCPLINT->cb->getVisibilityMap(), true, LOCPLINT->adventureInt->heroAnim,
  1114. to, &pos, moveX, moveY, false);
  1115. else
  1116. CGI->mh->terrainRect
  1117. (LOCPLINT->adventureInt->position, LOCPLINT->adventureInt->anim,
  1118. &LOCPLINT->cb->getVisibilityMap(), true, LOCPLINT->adventureInt->heroAnim,
  1119. to, &pos, 0, 0, false);
  1120. //SDL_BlitSurface(teren,&genRect(pos.h,pos.w,0,0),screen,&genRect(547,594,7,6));
  1121. //SDL_FreeSurface(teren);
  1122. if (currentPath && LOCPLINT->adventureInt->position.z==currentPath->startPos().z) //drawing path
  1123. {
  1124. showPath(&pos);
  1125. }
  1126. }
  1127. int3 CTerrainRect::whichTileIsIt(const int & x, const int & y)
  1128. {
  1129. int3 ret;
  1130. ret.x = LOCPLINT->adventureInt->position.x + ((GH.current->motion.x-CGI->mh->offsetX-pos.x)/32);
  1131. ret.y = LOCPLINT->adventureInt->position.y + ((GH.current->motion.y-CGI->mh->offsetY-pos.y)/32);
  1132. ret.z = LOCPLINT->adventureInt->position.z;
  1133. return ret;
  1134. }
  1135. int3 CTerrainRect::whichTileIsIt()
  1136. {
  1137. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  1138. }
  1139. void CResDataBar::clickRight(tribool down, bool previousState)
  1140. {
  1141. }
  1142. void CResDataBar::activate()
  1143. {
  1144. activateRClick();
  1145. }
  1146. void CResDataBar::deactivate()
  1147. {
  1148. deactivateRClick();
  1149. }
  1150. CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
  1151. {
  1152. bg = BitmapHandler::loadBitmap(defname);
  1153. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1154. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1155. pos = genRect(bg->h,bg->w,x,y);
  1156. txtpos.resize(8);
  1157. for (int i = 0; i < 8 ; i++)
  1158. {
  1159. txtpos[i].first = pos.x + offx + resdist*i;
  1160. txtpos[i].second = pos.y + offy;
  1161. }
  1162. txtpos[7].first = txtpos[6].first + datedist;
  1163. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  1164. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  1165. }
  1166. CResDataBar::CResDataBar()
  1167. {
  1168. bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
  1169. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1170. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1171. pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
  1172. txtpos.resize(8);
  1173. for (int i = 0; i < 8 ; i++)
  1174. {
  1175. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  1176. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  1177. }
  1178. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  1179. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  1180. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  1181. }
  1182. CResDataBar::~CResDataBar()
  1183. {
  1184. SDL_FreeSurface(bg);
  1185. }
  1186. void CResDataBar::draw(SDL_Surface * to)
  1187. {
  1188. blitAt(bg,pos.x,pos.y,to);
  1189. char * buf = new char[15];
  1190. for (int i=0;i<7;i++)
  1191. {
  1192. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  1193. printAt(buf,txtpos[i].first,txtpos[i].second,GEOR13,zwykly,to);
  1194. }
  1195. std::vector<std::string> temp;
  1196. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp+=std::string(buf);
  1197. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp+=std::string(buf);
  1198. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp+=std::string(buf);
  1199. printAt(processStr(datetext,temp),txtpos[7].first,txtpos[7].second,GEOR13,zwykly,to);
  1200. temp.clear();
  1201. //updateRect(&pos,screen);
  1202. delete[] buf;
  1203. }
  1204. void CResDataBar::show( SDL_Surface * to )
  1205. {
  1206. }
  1207. CInfoBar::CInfoBar()
  1208. {
  1209. toNextTick = mode = pom = -1;
  1210. pos.x=ADVOPT.infoboxX;
  1211. pos.y=ADVOPT.infoboxY;
  1212. pos.w=194;
  1213. pos.h=186;
  1214. day = CDefHandler::giveDef("NEWDAY.DEF");
  1215. week1 = CDefHandler::giveDef("NEWWEEK1.DEF");
  1216. week2 = CDefHandler::giveDef("NEWWEEK2.DEF");
  1217. week3 = CDefHandler::giveDef("NEWWEEK3.DEF");
  1218. week4 = CDefHandler::giveDef("NEWWEEK4.DEF");
  1219. }
  1220. CInfoBar::~CInfoBar()
  1221. {
  1222. delete day;
  1223. delete week1;
  1224. delete week2;
  1225. delete week3;
  1226. delete week4;
  1227. }
  1228. void CInfoBar::draw(SDL_Surface * to, const CGObjectInstance * specific)
  1229. {
  1230. if ((mode>=0) && mode<5)
  1231. {
  1232. blitAnim(mode);
  1233. return;
  1234. }
  1235. else if (mode==5)
  1236. {
  1237. mode = -1;
  1238. draw(to,LOCPLINT->adventureInt->selection);
  1239. }
  1240. if (!specific)
  1241. specific = LOCPLINT->adventureInt->selection;
  1242. if(!specific)
  1243. return;
  1244. if(specific->ID == HEROI_TYPE) //hero
  1245. {
  1246. if(graphics->heroWins.find(specific->subID)!=graphics->heroWins.end())
  1247. blitAt(graphics->heroWins[specific->subID],pos.x,pos.y,to);
  1248. }
  1249. else if (specific->ID == TOWNI_TYPE)
  1250. {
  1251. const CGTownInstance * t = static_cast<const CGTownInstance*>(specific);
  1252. if(graphics->townWins.find(t->id)!=graphics->townWins.end())
  1253. blitAt(graphics->townWins[t->id],pos.x,pos.y,to);
  1254. }
  1255. }
  1256. CDefHandler * CInfoBar::getAnim(int mode)
  1257. {
  1258. switch(mode)
  1259. {
  1260. case 0:
  1261. return day;
  1262. case 1:
  1263. return week1;
  1264. case 2:
  1265. return week2;
  1266. case 3:
  1267. return week3;
  1268. case 4:
  1269. return week4;
  1270. default:
  1271. return NULL;
  1272. }
  1273. }
  1274. void CInfoBar::blitAnim(int mode)//0 - day, 1 - week
  1275. {
  1276. CDefHandler * anim = NULL;
  1277. std::ostringstream txt;
  1278. anim = getAnim(mode);
  1279. if(mode) //new week animation
  1280. {
  1281. txt << CGI->generaltexth->allTexts[63] << " " << LOCPLINT->cb->getDate(2);
  1282. }
  1283. else //new day
  1284. {
  1285. txt << CGI->generaltexth->allTexts[64] << " " << LOCPLINT->cb->getDate(1);
  1286. }
  1287. blitAt(anim->ourImages[pom].bitmap,pos.x+9,pos.y+10);
  1288. printAtMiddle(txt.str(),pos.x+95,pos.y+31,TNRB16,zwykly);
  1289. if (pom == anim->ourImages.size()-1)
  1290. toNextTick+=750;
  1291. }
  1292. void CInfoBar::newDay(int Day)
  1293. {
  1294. if(LOCPLINT->cb->getDate(1) != 1)
  1295. {
  1296. mode = 0; //showing day
  1297. }
  1298. else
  1299. {
  1300. switch(LOCPLINT->cb->getDate(2))
  1301. {
  1302. case 1:
  1303. mode = 1;
  1304. break;
  1305. case 2:
  1306. mode = 2;
  1307. break;
  1308. case 3:
  1309. mode = 3;
  1310. break;
  1311. case 4:
  1312. mode = 4;
  1313. break;
  1314. default:
  1315. mode = -1;
  1316. break;
  1317. }
  1318. }
  1319. pom = 0;
  1320. activateTimer();
  1321. toNextTick = 500;
  1322. blitAnim(mode);
  1323. }
  1324. void CInfoBar::showComp(SComponent * comp, int time)
  1325. {
  1326. SDL_Surface * b = BitmapHandler::loadBitmap("ADSTATOT.bmp");
  1327. blitAt(b,pos.x+8,pos.y+11);
  1328. blitAt(comp->getImg(),pos.x+52,pos.y+54);
  1329. printAtMiddle(comp->subtitle,pos.x+91,pos.y+158,GEOR13,zwykly);
  1330. printAtMiddleWB(comp->description,pos.x+94,pos.y+31,GEOR13,26,zwykly);
  1331. SDL_FreeSurface(b);
  1332. activateTimer();
  1333. mode = 6;
  1334. toNextTick = time;
  1335. }
  1336. void CInfoBar::tick()
  1337. {
  1338. if((mode >= 0) && (mode < 5))
  1339. {
  1340. pom++;
  1341. if (pom >= getAnim(mode)->ourImages.size())
  1342. {
  1343. deactivateTimer();
  1344. toNextTick = -1;
  1345. mode = 5;
  1346. draw(screen2);
  1347. return;
  1348. }
  1349. toNextTick = 150;
  1350. blitAnim(mode);
  1351. }
  1352. else if (mode == 6)
  1353. {
  1354. deactivateTimer();
  1355. toNextTick = -1;
  1356. mode = 5;
  1357. draw(screen2);
  1358. }
  1359. }
  1360. void CInfoBar::show( SDL_Surface * to )
  1361. {
  1362. }
  1363. CAdvMapInt::CAdvMapInt(int Player)
  1364. :player(Player),
  1365. statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  1366. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  1367. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  1368. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  1369. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  1370. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  1371. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  1372. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  1373. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  1374. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  1375. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  1376. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  1377. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  1378. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  1379. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  1380. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  1381. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  1382. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  1383. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  1384. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  1385. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  1386. heroList(ADVOPT.hlistSize),
  1387. townList(ADVOPT.tlistSize,ADVOPT.tlistX,ADVOPT.tlistY,ADVOPT.tlistAU,ADVOPT.tlistAD)//(5,&genRect(192,48,747,196),747,196,747,372),
  1388. {
  1389. pos.x = pos.y = 0;
  1390. pos.w = screen->w;
  1391. pos.h = screen->h;
  1392. active = 0;
  1393. selection = NULL;
  1394. townList.fun = boost::bind(&CAdvMapInt::selectionChanged,this);
  1395. LOCPLINT->adventureInt=this;
  1396. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  1397. graphics->blueToPlayersAdv(bg,player);
  1398. scrollingDir = 0;
  1399. updateScreen = false;
  1400. anim=0;
  1401. animValHitCount=0; //animation frame
  1402. heroAnim=0;
  1403. heroAnimValHitCount=0; // hero animation frame
  1404. heroList.init();
  1405. heroList.genList();
  1406. //townList.init();
  1407. townList.genList();
  1408. heroWindow = new CHeroWindow(this->player);
  1409. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[0]));
  1410. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[1]));
  1411. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[2]));
  1412. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[3]));
  1413. }
  1414. CAdvMapInt::~CAdvMapInt()
  1415. {
  1416. SDL_FreeSurface(bg);
  1417. delete heroWindow;
  1418. for(int i=0; i<gems.size(); i++)
  1419. delete gems[i];
  1420. }
  1421. void CAdvMapInt::fshowOverview()
  1422. {
  1423. }
  1424. void CAdvMapInt::fswitchLevel()
  1425. {
  1426. if(!CGI->mh->map->twoLevel)
  1427. return;
  1428. if (position.z)
  1429. {
  1430. position.z--;
  1431. underground.curimg=0;
  1432. underground.show(screenBuf);
  1433. }
  1434. else
  1435. {
  1436. underground.curimg=1;
  1437. position.z++;
  1438. underground.show(screenBuf);
  1439. }
  1440. updateScreen = true;
  1441. minimap.draw(screenBuf);
  1442. }
  1443. void CAdvMapInt::fshowQuestlog()
  1444. {
  1445. }
  1446. void CAdvMapInt::fsleepWake()
  1447. {
  1448. }
  1449. void CAdvMapInt::fmoveHero()
  1450. {
  1451. if (selection->ID!=HEROI_TYPE)
  1452. return;
  1453. if (!terrain.currentPath)
  1454. return;
  1455. LOCPLINT->pim->unlock();
  1456. LOCPLINT->moveHero(static_cast<const CGHeroInstance*>(LOCPLINT->adventureInt->selection),*terrain.currentPath);
  1457. LOCPLINT->pim->lock();
  1458. }
  1459. void CAdvMapInt::fshowSpellbok()
  1460. {
  1461. if (selection->ID!=HEROI_TYPE) //checking necessary values
  1462. return;
  1463. 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);
  1464. GH.pushInt(spellWindow);
  1465. }
  1466. void CAdvMapInt::fadventureOPtions()
  1467. {
  1468. GH.pushInt(new CAdventureOptions);
  1469. }
  1470. void CAdvMapInt::fsystemOptions()
  1471. {
  1472. CSystemOptionsWindow * sysopWindow = new CSystemOptionsWindow(genRect(487, 481, 159, 57), LOCPLINT);
  1473. GH.pushInt(sysopWindow);
  1474. }
  1475. void CAdvMapInt::fnextHero()
  1476. {
  1477. if(!LOCPLINT->wanderingHeroes.size()) //no wandering heroes
  1478. return;
  1479. int start = heroList.selected;
  1480. int i = start;
  1481. do
  1482. {
  1483. i++;
  1484. if(i >= LOCPLINT->wanderingHeroes.size())
  1485. i = 0;
  1486. } while (!LOCPLINT->wanderingHeroes[i]->movement && i!=start);
  1487. heroList.select(i);
  1488. }
  1489. void CAdvMapInt::fendTurn()
  1490. {
  1491. LOCPLINT->makingTurn = false;
  1492. }
  1493. void CAdvMapInt::activate()
  1494. {
  1495. if(active++)
  1496. {
  1497. tlog1 << "Error: advmapint already active...\n";
  1498. active--;
  1499. return;
  1500. }
  1501. screenBuf = screen;
  1502. LOCPLINT->statusbar = &statusbar;
  1503. activateMouseMove();
  1504. kingOverview.activate();
  1505. underground.activate();
  1506. questlog.activate();
  1507. sleepWake.activate();
  1508. moveHero.activate();
  1509. spellbook.activate();
  1510. sysOptions.activate();
  1511. advOptions.activate();
  1512. nextHero.activate();
  1513. endTurn.activate();
  1514. minimap.activate();
  1515. heroList.activate();
  1516. townList.activate();
  1517. terrain.activate();
  1518. LOCPLINT->cingconsole->activate();
  1519. }
  1520. void CAdvMapInt::deactivate()
  1521. {
  1522. deactivateMouseMove();
  1523. scrollingDir = 0;
  1524. CGI->curh->changeGraphic(0,0);
  1525. kingOverview.deactivate();
  1526. underground.deactivate();
  1527. questlog.deactivate();
  1528. sleepWake.deactivate();
  1529. moveHero.deactivate();
  1530. spellbook.deactivate();
  1531. advOptions.deactivate();
  1532. sysOptions.deactivate();
  1533. nextHero.deactivate();
  1534. endTurn.deactivate();
  1535. minimap.deactivate();
  1536. heroList.deactivate();
  1537. townList.deactivate();
  1538. terrain.deactivate();
  1539. if(std::find(GH.timeinterested.begin(),GH.timeinterested.end(),&infoBar)!=GH.timeinterested.end())
  1540. GH.timeinterested.erase(std::find(GH.timeinterested.begin(),GH.timeinterested.end(),&infoBar));
  1541. infoBar.mode=-1;
  1542. LOCPLINT->cingconsole->deactivate();
  1543. if(--active)
  1544. {
  1545. tlog1 << "Error: advmapint still active...\n";
  1546. deactivate();
  1547. }
  1548. }
  1549. void CAdvMapInt::showAll(SDL_Surface *to)
  1550. {
  1551. blitAt(bg,0,0,to);
  1552. kingOverview.show(to);
  1553. underground.show(to);
  1554. questlog.show(to);
  1555. sleepWake.show(to);
  1556. moveHero.show(to);
  1557. spellbook.show(to);
  1558. advOptions.show(to);
  1559. sysOptions.show(to);
  1560. nextHero.show(to);
  1561. endTurn.show(to);
  1562. minimap.draw(to);
  1563. heroList.draw(to);
  1564. townList.draw(to);
  1565. updateScreen = true;
  1566. show(to);
  1567. resdatabar.draw(to);
  1568. statusbar.show(to);
  1569. infoBar.draw(to);
  1570. LOCPLINT->cingconsole->show(to);
  1571. }
  1572. void CAdvMapInt::show(SDL_Surface *to)
  1573. {
  1574. ++animValHitCount; //for animations
  1575. if(animValHitCount == 8)
  1576. {
  1577. CGI->mh->updateWater();
  1578. animValHitCount = 0;
  1579. ++anim;
  1580. updateScreen = true;
  1581. }
  1582. ++heroAnim;
  1583. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  1584. if((animValHitCount % (4/LOCPLINT->sysOpts.mapScrollingSpeed)) == 0
  1585. &&
  1586. (GH.topInt() == this)
  1587. || SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1588. || SDL_GetKeyState(NULL)[SDLK_RCTRL]
  1589. )
  1590. {
  1591. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  1592. position.x--;
  1593. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  1594. position.x++;
  1595. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  1596. position.y--;
  1597. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  1598. position.y++;
  1599. if(scrollingDir)
  1600. {
  1601. updateScreen = true;
  1602. updateMinimap=true;
  1603. }
  1604. }
  1605. if(updateScreen)
  1606. {
  1607. terrain.show(to);
  1608. for(int i=0;i<4;i++)
  1609. blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  1610. updateScreen=false;
  1611. LOCPLINT->cingconsole->show(to);
  1612. }
  1613. if (updateMinimap)
  1614. {
  1615. minimap.draw(to);
  1616. updateMinimap=false;
  1617. }
  1618. }
  1619. void CAdvMapInt::selectionChanged()
  1620. {
  1621. const CGTownInstance *to = townList.items[townList.selected];
  1622. select(to);
  1623. }
  1624. void CAdvMapInt::centerOn(int3 on)
  1625. {
  1626. // TODO:convertPosition should not belong to CGHeroInstance, and it
  1627. // should be split in 2 methods.
  1628. on = CGHeroInstance::convertPosition(on, false);
  1629. on.x -= CGI->mh->frameW;
  1630. on.y -= CGI->mh->frameH;
  1631. on = LOCPLINT->repairScreenPos(on);
  1632. LOCPLINT->adventureInt->position = on;
  1633. LOCPLINT->adventureInt->updateScreen=true;
  1634. updateMinimap=true;
  1635. underground.curimg = on.z; //change underground switch button image
  1636. if(GH.topInt() == this)
  1637. underground.redraw();
  1638. }
  1639. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1640. {
  1641. ui8 Dir = 0;
  1642. int k = key.keysym.sym;
  1643. switch(k)
  1644. {
  1645. case SDLK_i:
  1646. if(active)
  1647. CAdventureOptions::showScenarioInfo();
  1648. return;
  1649. case SDLK_s:
  1650. if(active)
  1651. GH.pushInt(new CSelectionScreen(saveGame));
  1652. return;
  1653. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1654. {
  1655. if(!active)
  1656. return;
  1657. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance*>(selection);
  1658. if(h && key.state == SDL_PRESSED)
  1659. {
  1660. LOCPLINT->pim->unlock();
  1661. LOCPLINT->cb->moveHero(h,h->pos);
  1662. LOCPLINT->pim->lock();
  1663. }
  1664. }
  1665. return;
  1666. case SDLK_RETURN:
  1667. {
  1668. if(!active || !selection || key.state != SDL_PRESSED)
  1669. return;
  1670. if(selection->ID == HEROI_TYPE)
  1671. LOCPLINT->openHeroWindow(static_cast<const CGHeroInstance*>(selection));
  1672. else if(selection->ID == TOWNI_TYPE)
  1673. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(selection));
  1674. return;
  1675. }
  1676. case SDLK_t:
  1677. {
  1678. //act on key down if marketplace windows is not already opened
  1679. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS) return;
  1680. //check if we have aby marketplace
  1681. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo();
  1682. size_t i = 0;
  1683. for(; i<towns.size(); i++)
  1684. if(vstd::contains(towns[i]->builtBuildings, 14))
  1685. break;
  1686. if(i != towns.size()) //if any town has marketplace, open window
  1687. GH.pushInt(new CMarketplaceWindow);
  1688. else //if not - complain
  1689. LOCPLINT->showInfoDialog("No available marketplace!", std::vector<SComponent*>(), soundBase::sound_todo);
  1690. return;
  1691. }
  1692. default:
  1693. {
  1694. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1695. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1696. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1697. //numpad arrow
  1698. if(isArrowKey(SDLKey(k)))
  1699. {
  1700. switch(k)
  1701. {
  1702. case SDLK_UP:
  1703. Dir = UP;
  1704. break;
  1705. case SDLK_LEFT:
  1706. Dir = LEFT;
  1707. break;
  1708. case SDLK_RIGHT:
  1709. Dir = RIGHT;
  1710. break;
  1711. case SDLK_DOWN:
  1712. Dir = DOWN;
  1713. break;
  1714. }
  1715. k = arrowToNum(SDLKey(k));
  1716. }
  1717. if(!active)
  1718. break;
  1719. k -= SDLK_KP0 + 1;
  1720. if(k < 0 || k > 8 || key.state != SDL_PRESSED)
  1721. return;
  1722. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(selection);
  1723. if(!h) break;
  1724. if(k == 4)
  1725. {
  1726. centerOn(h->getPosition(false));
  1727. return;
  1728. }
  1729. int3 dir = directions[k];
  1730. CGPath &path = paths[h];
  1731. terrain.currentPath = &path;
  1732. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  1733. {
  1734. terrain.currentPath = NULL;
  1735. return;
  1736. }
  1737. if(!path.nodes[0].turns)
  1738. {
  1739. LOCPLINT->pim->unlock();
  1740. LOCPLINT->moveHero(h, path);
  1741. LOCPLINT->pim->lock();
  1742. }
  1743. }
  1744. return;
  1745. }
  1746. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1747. && (SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1748. || SDL_GetKeyState(NULL)[SDLK_RCTRL])
  1749. )
  1750. scrollingDir |= Dir;
  1751. else
  1752. scrollingDir &= ~Dir;
  1753. }
  1754. void CAdvMapInt::handleRightClick(std::string text, tribool down, CIntObject * client)
  1755. {
  1756. if (down)
  1757. {
  1758. CSimpleWindow * temp = CMessage::genWindow(text,LOCPLINT->playerID,true);
  1759. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  1760. GH.pushInt(rcpi);
  1761. }
  1762. }
  1763. int3 CAdvMapInt::verifyPos(int3 ver)
  1764. {
  1765. if (ver.x<0)
  1766. ver.x=0;
  1767. if (ver.y<0)
  1768. ver.y=0;
  1769. if (ver.z<0)
  1770. ver.z=0;
  1771. if (ver.x>=CGI->mh->sizes.x)
  1772. ver.x=CGI->mh->sizes.x-1;
  1773. if (ver.y>=CGI->mh->sizes.y)
  1774. ver.y=CGI->mh->sizes.y-1;
  1775. if (ver.z>=CGI->mh->sizes.z)
  1776. ver.z=CGI->mh->sizes.z-1;
  1777. return ver;
  1778. }
  1779. void CAdvMapInt::select(const CArmedInstance *sel )
  1780. {
  1781. LOCPLINT->cb->setSelection(sel);
  1782. centerOn(sel->pos);
  1783. selection = sel;
  1784. terrain.currentPath = NULL;
  1785. if(sel->ID==TOWNI_TYPE)
  1786. {
  1787. int pos = vstd::findPos(townList.items,sel);
  1788. townList.selected = pos;
  1789. townList.fixPos();
  1790. }
  1791. else //hero selected
  1792. {
  1793. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(sel);
  1794. if(LOCPLINT->getWHero(heroList.selected) != h)
  1795. {
  1796. heroList.selected = heroList.getPosOfHero(h);
  1797. heroList.fixPos();
  1798. }
  1799. if(vstd::contains(paths,h)) //hero has assigned path
  1800. {
  1801. CGPath &path = paths[h];
  1802. assert(h->getPosition(false) == path.startPos());
  1803. //update the hero path in case of something has changed on map
  1804. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1805. terrain.currentPath = &path;
  1806. else
  1807. paths.erase(h);
  1808. }
  1809. }
  1810. townList.draw(screen);
  1811. heroList.draw(screen);
  1812. infoBar.draw(screen);
  1813. }
  1814. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1815. {
  1816. //adventure map scrolling with mouse
  1817. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && active)
  1818. {
  1819. if(sEvent.x<15)
  1820. {
  1821. scrollingDir |= LEFT;
  1822. }
  1823. else
  1824. {
  1825. scrollingDir &= ~LEFT;
  1826. }
  1827. if(sEvent.x>screen->w-15)
  1828. {
  1829. scrollingDir |= RIGHT;
  1830. }
  1831. else
  1832. {
  1833. scrollingDir &= ~RIGHT;
  1834. }
  1835. if(sEvent.y<15)
  1836. {
  1837. scrollingDir |= UP;
  1838. }
  1839. else
  1840. {
  1841. scrollingDir &= ~UP;
  1842. }
  1843. if(sEvent.y>screen->h-15)
  1844. {
  1845. scrollingDir |= DOWN;
  1846. }
  1847. else
  1848. {
  1849. scrollingDir &= ~DOWN;
  1850. }
  1851. }
  1852. }
  1853. CAdventureOptions::CAdventureOptions()
  1854. {
  1855. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1856. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTS.bmp"), 0, 0);
  1857. graphics->blueToPlayersAdv(bg->bg, LOCPLINT->playerID);
  1858. pos = bg->center();
  1859. exit = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1860. //scenInfo = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 24, "ADVINFO.DEF",SDLK_i);
  1861. scenInfo = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 198, "ADVINFO.DEF",SDLK_i);
  1862. scenInfo->callback += CAdventureOptions::showScenarioInfo;
  1863. //viewWorld = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1864. puzzle = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 81, "ADVPUZ.DEF");;
  1865. puzzle->callback += CAdventureOptions::showPuzzleMap;
  1866. }
  1867. CAdventureOptions::~CAdventureOptions()
  1868. {
  1869. }
  1870. void CAdventureOptions::showScenarioInfo()
  1871. {
  1872. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1873. }
  1874. void CAdventureOptions::showPuzzleMap()
  1875. {
  1876. GH.pushInt(new CPuzzleWindow());
  1877. }