CAdvmapInterface.cpp 55 KB

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