CAdvmapInterface.cpp 55 KB

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