CAdvmapInterface.cpp 55 KB

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