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 "SDL_Extensions.h"
  11. #include "CBitmapHandler.h"
  12. #include "CConfigHandler.h"
  13. #include "CSpellWindow.h"
  14. #include "Graphics.h"
  15. #include "../hch/CDefHandler.h"
  16. #include "../hch/CGeneralTextHandler.h"
  17. #include "../hch/CHeroHandler.h"
  18. #include "../hch/CObjectHandler.h"
  19. #include "../hch/CTownHandler.h"
  20. #include "../lib/map.h"
  21. #include "mapHandler.h"
  22. #include "../stdafx.h"
  23. #include <boost/algorithm/string.hpp>
  24. #include <boost/algorithm/string/replace.hpp>
  25. #include <boost/assign/std/vector.hpp>
  26. #include <boost/thread.hpp>
  27. #include <sstream>
  28. #include "CPreGame.h"
  29. #include "../lib/VCMI_Lib.h"
  30. #include "../hch/CSpellHandler.h"
  31. #include <boost/foreach.hpp>
  32. #ifdef _MSC_VER
  33. #pragma warning (disable : 4355)
  34. #endif
  35. /*
  36. * CAdvMapInterface.cpp, part of VCMI engine
  37. *
  38. * Authors: listed in file AUTHORS in main folder
  39. *
  40. * License: GNU General Public License v2.0 or later
  41. * Full text of license available in license.txt file, in main folder
  42. *
  43. */
  44. #define ADVOPT (conf.go()->ac)
  45. using namespace boost::logic;
  46. using namespace boost::assign;
  47. using namespace CSDL_Ext;
  48. CAdvMapInt *adventureInt;
  49. CMinimap::CMinimap(bool draw)
  50. {
  51. int3 mapSizes = LOCPLINT->cb->getMapSize();
  52. statusbarTxt = CGI->generaltexth->zelp[291].first;
  53. rcText = CGI->generaltexth->zelp[291].second;
  54. pos.x=ADVOPT.minimapX;//630
  55. pos.y=ADVOPT.minimapY;//26
  56. pos.h=ADVOPT.minimapW;//144
  57. pos.w=ADVOPT.minimapH;//144
  58. temps = newSurface(pos.w,pos.h);
  59. std::ifstream is(DATA_DIR "/config/minimap.txt",std::ifstream::in);
  60. for (int i=0;i<TERRAIN_TYPES;i++)
  61. {
  62. std::pair<int,SDL_Color> vinya;
  63. std::pair<int,SDL_Color> vinya2;
  64. int pom;
  65. is >> pom;
  66. vinya2.first=vinya.first=pom;
  67. is >> pom;
  68. vinya.second.r=pom;
  69. is >> pom;
  70. vinya.second.g=pom;
  71. is >> pom;
  72. vinya.second.b=pom;
  73. is >> pom;
  74. vinya2.second.r=pom;
  75. is >> pom;
  76. vinya2.second.g=pom;
  77. is >> pom;
  78. vinya2.second.b=pom;
  79. vinya.second.unused=vinya2.second.unused=255;
  80. colors.insert(vinya);
  81. colorsBlocked.insert(vinya2);
  82. }
  83. is.close();
  84. if (draw)
  85. redraw();
  86. }
  87. CMinimap::~CMinimap()
  88. {
  89. SDL_FreeSurface(temps);
  90. }
  91. void CMinimap::draw(SDL_Surface * to)
  92. {
  93. int3 mapSizes = LOCPLINT->cb->getMapSize();
  94. //draw terrain
  95. blitAt(map[adventureInt->position.z],0,0,temps);
  96. //draw heroes
  97. std::vector <const CGHeroInstance *> hh = LOCPLINT->cb->getHeroesInfo(false);
  98. int mw = map[0]->w, mh = map[0]->h,
  99. wo = mw/mapSizes.x, ho = mh/mapSizes.y;
  100. for (size_t i=0; i < hh.size(); ++i)
  101. {
  102. int3 hpos = hh[i]->getPosition(false);
  103. if(hpos.z!=adventureInt->position.z)
  104. continue;
  105. //float zawx = ((float)hpos.x/CGI->mh->sizes.x), zawy = ((float)hpos.y/CGI->mh->sizes.y);
  106. int3 maplgp ( (hpos.x*mw)/mapSizes.x, (hpos.y*mh)/mapSizes.y, hpos.z );
  107. for (int ii=0; ii<wo; ii++)
  108. {
  109. for (int jj=0; jj<ho; jj++)
  110. {
  111. SDL_PutPixelWithoutRefresh(temps,maplgp.x+ii,maplgp.y+jj,graphics->playerColors[hh[i]->getOwner()].r,
  112. graphics->playerColors[hh[i]->getOwner()].g,graphics->playerColors[hh[i]->getOwner()].b);
  113. }
  114. }
  115. }
  116. blitAt(flObjs[adventureInt->position.z],0,0,temps);
  117. blitAt(FoW[adventureInt->position.z],0,0,temps);
  118. //draw radar
  119. const int tilesw=(ADVOPT.advmapW+31)/32;
  120. const int tilesh=(ADVOPT.advmapH+31)/32;
  121. int bx = (((float)adventureInt->position.x)/(((float)mapSizes.x)))*pos.w,
  122. by = (((float)adventureInt->position.y)/(((float)mapSizes.y)))*pos.h,
  123. rx = (((float)tilesw)/(mapSizes.x))*((float)pos.w), //width
  124. ry = (((float)tilesh)/(mapSizes.y))*((float)pos.h); //height
  125. CSDL_Ext::drawDashedBorder(temps, Rect(bx, by, rx, ry), int3(255,75,125));
  126. //blitAt(radar,bx,by,temps);
  127. blitAt(temps,pos.x,pos.y,to);
  128. }
  129. void CMinimap::redraw(int level)// (level==-1) => redraw all levels
  130. {
  131. initMap(level);
  132. //FoW
  133. initFoW(level);
  134. //flaggable objects
  135. initFlaggableObjs(level);
  136. //showing tiles
  137. showVisibleTiles();
  138. }
  139. void CMinimap::initMap(int level)
  140. {
  141. /*for(int g=0; g<map.size(); ++g)
  142. {
  143. SDL_FreeSurface(map[g]);
  144. }
  145. map.clear();*/
  146. int3 mapSizes = LOCPLINT->cb->getMapSize();
  147. for (size_t i=0; i<CGI->mh->sizes.z; i++)
  148. {
  149. SDL_Surface * pom ;
  150. if ((level>=0) && (i!=level))
  151. continue;
  152. if (map.size()<i+1)
  153. pom = CSDL_Ext::newSurface(pos.w,pos.h,screen);
  154. else pom = map[i];
  155. for (int x=0;x<pos.w;x++)
  156. {
  157. for (int y=0;y<pos.h;y++)
  158. {
  159. int mx=(mapSizes.x*x)/pos.w;
  160. int my=(mapSizes.y*y)/pos.h;
  161. const TerrainTile * tile = LOCPLINT->cb->getTileInfo(int3(mx, my, i));
  162. if(tile)
  163. {
  164. if (tile->blocked && (!tile->visitable))
  165. SDL_PutPixelWithoutRefresh(pom, x, y, colorsBlocked[tile->tertype].r, colorsBlocked[tile->tertype].g, colorsBlocked[tile->tertype].b);
  166. else SDL_PutPixelWithoutRefresh(pom, x, y, colors[tile->tertype].r, colors[tile->tertype].g, colors[tile->tertype].b);
  167. }
  168. }
  169. }
  170. map.push_back(pom);
  171. }
  172. }
  173. void CMinimap::initFoW(int level)
  174. {
  175. /*for(int g=0; g<FoW.size(); ++g)
  176. {
  177. SDL_FreeSurface(FoW[g]);
  178. }
  179. FoW.clear();*/
  180. int3 mapSizes = LOCPLINT->cb->getMapSize();
  181. int mw = map[0]->w, mh = map[0]->h;//,
  182. //wo = mw/mapSizes.x, ho = mh/mapSizes.y; //TODO use me
  183. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  184. {
  185. if(level>=0 && d!=level)
  186. continue;
  187. SDL_Surface * pt = CSDL_Ext::newSurface(pos.w, pos.h, CSDL_Ext::std32bppSurface);
  188. for (int i=0; i<mw; i++)
  189. {
  190. for (int j=0; j<mh; j++)
  191. {
  192. int3 pp( ((i*mapSizes.x)/mw), ((j*mapSizes.y)/mh), d );
  193. if ( !LOCPLINT->cb->isVisible(pp) )
  194. {
  195. CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0);
  196. }
  197. }
  198. }
  199. FoW.push_back(pt);
  200. }
  201. }
  202. void CMinimap::initFlaggableObjs(int level)
  203. {
  204. /*for(int g=0; g<flObjs.size(); ++g)
  205. {
  206. SDL_FreeSurface(flObjs[g]);
  207. }
  208. flObjs.clear();*/
  209. int3 mapSizes = LOCPLINT->cb->getMapSize();
  210. int mw = map[0]->w, mh = map[0]->h;
  211. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  212. {
  213. if(level>=0 && d!=level)
  214. continue;
  215. SDL_Surface * pt = CSDL_Ext::newSurface(pos.w, pos.h, CSDL_Ext::std32bppSurface);
  216. for (int i=0; i<mw; i++)
  217. {
  218. for (int j=0; j<mh; j++)
  219. {
  220. CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0,0);
  221. }
  222. }
  223. flObjs.push_back(pt);
  224. }
  225. }
  226. void CMinimap::updateRadar()
  227. {}
  228. void CMinimap::clickRight(tribool down, bool previousState)
  229. {
  230. adventureInt->handleRightClick(rcText,down);
  231. }
  232. void CMinimap::clickLeft(tribool down, bool previousState)
  233. {
  234. if (down && (!previousState))
  235. activateMouseMove();
  236. else if (!down)
  237. {
  238. if (std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this)!=GH.motioninterested.end())
  239. deactivateMouseMove();
  240. }
  241. //ClickableL::clickLeft(down);
  242. if (!((bool)down))
  243. return;
  244. float dx=((float)(GH.current->motion.x-pos.x))/((float)pos.w),
  245. dy=((float)(GH.current->motion.y-pos.y))/((float)pos.h);
  246. int3 newCPos;
  247. newCPos.x = (CGI->mh->sizes.x*dx);
  248. newCPos.y = (CGI->mh->sizes.y*dy);
  249. newCPos.z = adventureInt->position.z;
  250. adventureInt->centerOn(newCPos);
  251. }
  252. void CMinimap::hover (bool on)
  253. {
  254. //Hoverable::hover(on);
  255. if (on)
  256. adventureInt->statusbar.print(statusbarTxt);
  257. else if (adventureInt->statusbar.current==statusbarTxt)
  258. adventureInt->statusbar.clear();
  259. }
  260. void CMinimap::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  261. {
  262. if (pressedL)
  263. {
  264. clickLeft(true, true);
  265. }
  266. }
  267. void CMinimap::activate()
  268. {
  269. activateLClick();
  270. activateRClick();
  271. activateHover();
  272. if (pressedL)
  273. activateMouseMove();
  274. }
  275. void CMinimap::deactivate()
  276. {
  277. if (pressedL)
  278. deactivateMouseMove();
  279. deactivateLClick();
  280. deactivateRClick();
  281. deactivateHover();
  282. }
  283. void CMinimap::showTile(const int3 &pos)
  284. {
  285. int3 mapSizes = LOCPLINT->cb->getMapSize();
  286. //drawing terrain
  287. int mw = map[0]->w, mh = map[0]->h;
  288. double wo = ((double)mw)/mapSizes.x, ho = ((double)mh)/mapSizes.y;
  289. for (int ii=0; ii<wo; ii++)
  290. {
  291. for (int jj=0; jj<ho; jj++)
  292. {
  293. if ((pos.x*wo+ii<this->pos.w) && (pos.y*ho+jj<this->pos.h))
  294. CSDL_Ext::SDL_PutPixelWithoutRefresh(FoW[pos.z],pos.x*wo+ii,pos.y*ho+jj,0,0,0,0);
  295. const TerrainTile * tile = LOCPLINT->cb->getTileInfo(pos);
  296. if(tile)
  297. {
  298. if (tile->blocked && (!tile->visitable))
  299. 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);
  300. 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);
  301. }
  302. }
  303. }
  304. //drawing flaggable objects
  305. int woShifted = wo, hoShifted = ho; //for better minimap rendering on L-sized maps
  306. std::vector < const CGObjectInstance * > oo = LOCPLINT->cb->getFlaggableObjects(pos);
  307. for(size_t v=0; v<oo.size(); ++v)
  308. {
  309. if(!dynamic_cast< const CGHeroInstance * >(oo[v])) //heroes have been printed
  310. {
  311. int3 maplgp ( (pos.x*mw)/mapSizes.x, (pos.y*mh)/mapSizes.y, pos.z );
  312. if(((int)wo) * mapSizes.x != mw && pos.x+1 < mapSizes.x)//minimap size in X is not multiple of map size in X
  313. {
  314. std::vector < const CGObjectInstance * > op1x = LOCPLINT->cb->getFlaggableObjects(int3(pos.x+1, pos.y, pos.z));
  315. if(op1x.size()!=0)
  316. {
  317. woShifted = wo + 1;
  318. }
  319. else
  320. {
  321. woShifted = wo;
  322. }
  323. }
  324. if(((int)ho) * mapSizes.y != mh && pos.y+1 < mapSizes.y) //minimap size in Y is not multiple of map size in Y
  325. {
  326. std::vector < const CGObjectInstance * > op1y = LOCPLINT->cb->getFlaggableObjects(int3(pos.x, pos.y+1, pos.z));
  327. if(op1y.size()!=0)
  328. {
  329. hoShifted = ho + 1;
  330. }
  331. else
  332. {
  333. hoShifted = ho;
  334. }
  335. }
  336. for (int ii=0; ii<woShifted; ii++) //rendering flaggable objects
  337. {
  338. for (int jj=0; jj<hoShifted; jj++)
  339. {
  340. if(oo[v]->tempOwner == 255)
  341. SDL_PutPixelWithoutRefresh(flObjs[pos.z],maplgp.x+ii,maplgp.y+jj,graphics->neutralColor->b,
  342. graphics->neutralColor->g,graphics->neutralColor->r);
  343. else
  344. SDL_PutPixelWithoutRefresh(flObjs[pos.z],maplgp.x+ii,maplgp.y+jj,graphics->playerColors[oo[v]->getOwner()].b,
  345. graphics->playerColors[oo[v]->getOwner()].g,graphics->playerColors[oo[v]->getOwner()].r);
  346. }
  347. }
  348. }
  349. }
  350. //flaggable objects drawn
  351. }
  352. void CMinimap::showVisibleTiles(int level)
  353. {
  354. int3 mapSizes = LOCPLINT->cb->getMapSize();
  355. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  356. {
  357. if(level>=0 && d!=level)
  358. continue;
  359. for(int x=0; x<mapSizes.x; ++x)
  360. {
  361. for(int y=0; y<mapSizes.y; ++y)
  362. {
  363. if(LOCPLINT->cb->isVisible(int3(x, y, d)))
  364. {
  365. showTile(int3(x, y, d));
  366. }
  367. }
  368. }
  369. }
  370. }
  371. void CMinimap::hideTile(const int3 &pos)
  372. {
  373. int3 mapSizes = LOCPLINT->cb->getMapSize();
  374. //drawing terrain
  375. int mw = map[0]->w, mh = map[0]->h;
  376. double wo = ((double)mw)/mapSizes.x, ho = ((double)mh)/mapSizes.y;
  377. for (int ii=0; ii<wo; ii++)
  378. {
  379. for (int jj=0; jj<ho; jj++)
  380. {
  381. if ((pos.x*wo+ii<this->pos.w) && (pos.y*ho+jj<this->pos.h))
  382. CSDL_Ext::SDL_PutPixelWithoutRefresh(FoW[pos.z],pos.x*wo+ii,pos.y*ho+jj,0,0,0,0);
  383. }
  384. }
  385. }
  386. void CMinimap::show( SDL_Surface * to )
  387. {
  388. }
  389. CTerrainRect::CTerrainRect()
  390. :curHoveredTile(-1,-1,-1), currentPath(NULL)
  391. {
  392. tilesw=(ADVOPT.advmapW+31)/32;
  393. tilesh=(ADVOPT.advmapH+31)/32;
  394. pos.x=ADVOPT.advmapX;
  395. pos.y=ADVOPT.advmapY;
  396. pos.w=ADVOPT.advmapW;
  397. pos.h=ADVOPT.advmapH;
  398. moveX = moveY = 0;
  399. arrows = CDefHandler::giveDef("ADAG.DEF");
  400. for(size_t y=0; y < arrows->ourImages.size(); ++y)
  401. {
  402. CSDL_Ext::alphaTransform(arrows->ourImages[y].bitmap);
  403. }
  404. }
  405. CTerrainRect::~CTerrainRect()
  406. {
  407. delete arrows;
  408. }
  409. void CTerrainRect::activate()
  410. {
  411. activateLClick();
  412. activateRClick();
  413. activateHover();
  414. activateMouseMove();
  415. };
  416. void CTerrainRect::deactivate()
  417. {
  418. deactivateLClick();
  419. deactivateRClick();
  420. deactivateHover();
  421. deactivateMouseMove();
  422. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  423. };
  424. void CTerrainRect::clickLeft(tribool down, bool previousState)
  425. {
  426. if ((down==false) || indeterminate(down))
  427. return;
  428. int3 mp = whichTileIsIt();
  429. if (mp.x<0 || mp.y<0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  430. return;
  431. adventureInt->tileLClicked(mp);
  432. }
  433. void CTerrainRect::clickRight(tribool down, bool previousState)
  434. {
  435. int3 mp = whichTileIsIt();
  436. if (!CGI->mh->map->isInTheMap(mp) || down != true)
  437. return;
  438. adventureInt->tileRClicked(mp);
  439. }
  440. void CTerrainRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  441. {
  442. int3 tHovered = whichTileIsIt(sEvent.x,sEvent.y);
  443. int3 pom = adventureInt->verifyPos(tHovered);
  444. if(tHovered != pom) //tile outside the map
  445. {
  446. CGI->curh->changeGraphic(0, 0);
  447. return;
  448. }
  449. if (pom != curHoveredTile)
  450. curHoveredTile=pom;
  451. else
  452. return;
  453. adventureInt->tileHovered(curHoveredTile);
  454. }
  455. void CTerrainRect::hover(bool on)
  456. {
  457. if (!on)
  458. {
  459. adventureInt->statusbar.clear();
  460. CGI->curh->changeGraphic(0,0);
  461. }
  462. //Hoverable::hover(on);
  463. }
  464. void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
  465. {
  466. for (size_t i=0; i < currentPath->nodes.size()-1; ++i)
  467. {
  468. int pn=-1;//number of picture
  469. if (i==0) //last tile
  470. {
  471. int x = 32*(currentPath->nodes[i].coord.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  472. y = 32*(currentPath->nodes[i].coord.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  473. if (x<0 || y<0 || x>pos.w || y>pos.h)
  474. continue;
  475. pn=0;
  476. }
  477. else
  478. {
  479. /*
  480. * notation of arrow direction:
  481. * 1 2 3
  482. * 4 5 6
  483. * 7 8 9
  484. * ie. 157 means an arrow from left upper tile to left bottom tile through 5 (all arrows go through 5 in this notation)
  485. */
  486. std::vector<CGPathNode> & cv = currentPath->nodes;
  487. if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y-1) //15x
  488. {
  489. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //156
  490. {
  491. pn = 3;
  492. }
  493. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //159
  494. {
  495. pn = 12;
  496. }
  497. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //158
  498. {
  499. pn = 21;
  500. }
  501. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //157
  502. {
  503. pn = 22;
  504. }
  505. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //153
  506. {
  507. pn = 2;
  508. }
  509. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //154
  510. {
  511. pn = 23;
  512. }
  513. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //152
  514. {
  515. pn = 1;
  516. }
  517. }
  518. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y-1) //25x
  519. {
  520. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //253
  521. {
  522. pn = 2;
  523. }
  524. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //256
  525. {
  526. pn = 3;
  527. }
  528. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //259
  529. {
  530. pn = 4;
  531. }
  532. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //258
  533. {
  534. pn = 13;
  535. }
  536. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //257
  537. {
  538. pn = 22;
  539. }
  540. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //254
  541. {
  542. pn = 23;
  543. }
  544. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //251
  545. {
  546. pn = 24;
  547. }
  548. }
  549. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y-1) //35x
  550. {
  551. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //358
  552. {
  553. pn = 5;
  554. }
  555. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //357
  556. {
  557. pn = 14;
  558. }
  559. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //354
  560. {
  561. pn = 23;
  562. }
  563. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //351
  564. {
  565. pn = 24;
  566. }
  567. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //359
  568. {
  569. pn = 4;
  570. }
  571. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //356
  572. {
  573. pn = 3;
  574. }
  575. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //352
  576. {
  577. pn = 17;
  578. }
  579. }
  580. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y) //65x
  581. {
  582. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //657
  583. {
  584. pn = 6;
  585. }
  586. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //654
  587. {
  588. pn = 15;
  589. }
  590. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //651
  591. {
  592. pn = 24;
  593. }
  594. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //652
  595. {
  596. pn = 17;
  597. }
  598. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //658
  599. {
  600. pn = 5;
  601. }
  602. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //653
  603. {
  604. pn = 18;
  605. }
  606. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //659
  607. {
  608. pn = 4;
  609. }
  610. }
  611. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y+1) //95x
  612. {
  613. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //954
  614. {
  615. pn = 7;
  616. }
  617. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //951
  618. {
  619. pn = 16;
  620. }
  621. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //952
  622. {
  623. pn = 17;
  624. }
  625. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //957
  626. {
  627. pn = 6;
  628. }
  629. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //953
  630. {
  631. pn = 18;
  632. }
  633. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //956
  634. {
  635. pn = 19;
  636. }
  637. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //958
  638. {
  639. pn = 5;
  640. }
  641. }
  642. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y+1) //85x
  643. {
  644. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //857
  645. {
  646. pn = 6;
  647. }
  648. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //854
  649. {
  650. pn = 7;
  651. }
  652. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //851
  653. {
  654. pn = 8;
  655. }
  656. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //852
  657. {
  658. pn = 9;
  659. }
  660. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //853
  661. {
  662. pn = 18;
  663. }
  664. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //856
  665. {
  666. pn = 19;
  667. }
  668. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //859
  669. {
  670. pn = 20;
  671. }
  672. }
  673. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y+1) //75x
  674. {
  675. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //752
  676. {
  677. pn = 1;
  678. }
  679. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //753
  680. {
  681. pn = 10;
  682. }
  683. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //756
  684. {
  685. pn = 19;
  686. }
  687. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //751
  688. {
  689. pn = 8;
  690. }
  691. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //759
  692. {
  693. pn = 20;
  694. }
  695. }
  696. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y) //45x
  697. {
  698. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //453
  699. {
  700. pn = 2;
  701. }
  702. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //456
  703. {
  704. pn = 11;
  705. }
  706. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //459
  707. {
  708. pn = 20;
  709. }
  710. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //452
  711. {
  712. pn = 1;
  713. }
  714. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //456
  715. {
  716. pn = 21;
  717. }
  718. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //451
  719. {
  720. pn = 8;
  721. }
  722. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //457
  723. {
  724. pn = 22;
  725. }
  726. }
  727. }
  728. if (currentPath->nodes[i].turns)
  729. pn+=25;
  730. if (pn>=0)
  731. {
  732. int x = 32*(currentPath->nodes[i].coord.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  733. y = 32*(currentPath->nodes[i].coord.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  734. if (x<0 || y<0 || x>pos.w || y>pos.h)
  735. continue;
  736. int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
  737. hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
  738. SDL_Rect prevClip;
  739. SDL_GetClipRect(to, &prevClip);
  740. SDL_SetClipRect(to, extRect); //preventing blitting outside of that rect
  741. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  742. {
  743. if (hvx<0 && hvy<0)
  744. {
  745. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, to, &genRect(32, 32, x + moveX, y + moveY));
  746. }
  747. else if(hvx<0)
  748. {
  749. CSDL_Ext::blit8bppAlphaTo24bpp
  750. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  751. to, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY));
  752. }
  753. else if (hvy<0)
  754. {
  755. CSDL_Ext::blit8bppAlphaTo24bpp
  756. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  757. to, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  758. }
  759. else
  760. {
  761. CSDL_Ext::blit8bppAlphaTo24bpp
  762. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  763. to, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  764. }
  765. }
  766. else //standard version
  767. {
  768. if (hvx<0 && hvy<0)
  769. {
  770. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, to, &genRect(32, 32, x, y));
  771. }
  772. else if(hvx<0)
  773. {
  774. CSDL_Ext::blit8bppAlphaTo24bpp
  775. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  776. to, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y));
  777. }
  778. else if (hvy<0)
  779. {
  780. CSDL_Ext::blit8bppAlphaTo24bpp
  781. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  782. to, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  783. }
  784. else
  785. {
  786. CSDL_Ext::blit8bppAlphaTo24bpp
  787. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  788. to, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  789. }
  790. }
  791. SDL_SetClipRect(to, &prevClip);
  792. }
  793. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  794. }
  795. void CTerrainRect::show(SDL_Surface * to)
  796. {
  797. if(ADVOPT.smoothMove)
  798. CGI->mh->terrainRect
  799. (adventureInt->position, adventureInt->anim,
  800. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  801. to, &pos, moveX, moveY, false);
  802. else
  803. CGI->mh->terrainRect
  804. (adventureInt->position, adventureInt->anim,
  805. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  806. to, &pos, 0, 0, false);
  807. //SDL_BlitSurface(teren,&genRect(pos.h,pos.w,0,0),screen,&genRect(547,594,7,6));
  808. //SDL_FreeSurface(teren);
  809. if (currentPath && adventureInt->position.z==currentPath->startPos().z) //drawing path
  810. {
  811. showPath(&pos, to);
  812. }
  813. }
  814. int3 CTerrainRect::whichTileIsIt(const int & x, const int & y)
  815. {
  816. int3 ret;
  817. ret.x = adventureInt->position.x + ((GH.current->motion.x-CGI->mh->offsetX-pos.x)/32);
  818. ret.y = adventureInt->position.y + ((GH.current->motion.y-CGI->mh->offsetY-pos.y)/32);
  819. ret.z = adventureInt->position.z;
  820. return ret;
  821. }
  822. int3 CTerrainRect::whichTileIsIt()
  823. {
  824. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  825. }
  826. void CResDataBar::clickRight(tribool down, bool previousState)
  827. {
  828. }
  829. void CResDataBar::activate()
  830. {
  831. activateRClick();
  832. }
  833. void CResDataBar::deactivate()
  834. {
  835. deactivateRClick();
  836. }
  837. CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
  838. {
  839. bg = BitmapHandler::loadBitmap(defname);
  840. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  841. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  842. pos = genRect(bg->h,bg->w,x,y);
  843. txtpos.resize(8);
  844. for (int i = 0; i < 8 ; i++)
  845. {
  846. txtpos[i].first = pos.x + offx + resdist*i;
  847. txtpos[i].second = pos.y + offy;
  848. }
  849. txtpos[7].first = txtpos[6].first + datedist;
  850. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  851. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  852. }
  853. CResDataBar::CResDataBar()
  854. {
  855. bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
  856. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  857. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  858. pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
  859. txtpos.resize(8);
  860. for (int i = 0; i < 8 ; i++)
  861. {
  862. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  863. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  864. }
  865. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  866. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  867. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  868. }
  869. CResDataBar::~CResDataBar()
  870. {
  871. SDL_FreeSurface(bg);
  872. }
  873. void CResDataBar::draw(SDL_Surface * to)
  874. {
  875. blitAt(bg,pos.x,pos.y,to);
  876. char * buf = new char[15];
  877. for (int i=0;i<7;i++)
  878. {
  879. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  880. printAt(buf,txtpos[i].first,txtpos[i].second,FONT_SMALL,zwykly,to);
  881. }
  882. std::vector<std::string> temp;
  883. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp+=std::string(buf);
  884. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp+=std::string(buf);
  885. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp+=std::string(buf);
  886. printAt(processStr(datetext,temp),txtpos[7].first,txtpos[7].second,FONT_SMALL,zwykly,to);
  887. temp.clear();
  888. //updateRect(&pos,screen);
  889. delete[] buf;
  890. }
  891. void CResDataBar::show( SDL_Surface * to )
  892. {
  893. }
  894. CInfoBar::CInfoBar()
  895. {
  896. toNextTick = mode = pom = -1;
  897. pos.x=ADVOPT.infoboxX;
  898. pos.y=ADVOPT.infoboxY;
  899. pos.w=194;
  900. pos.h=186;
  901. day = CDefHandler::giveDef("NEWDAY.DEF");
  902. week1 = CDefHandler::giveDef("NEWWEEK1.DEF");
  903. week2 = CDefHandler::giveDef("NEWWEEK2.DEF");
  904. week3 = CDefHandler::giveDef("NEWWEEK3.DEF");
  905. week4 = CDefHandler::giveDef("NEWWEEK4.DEF");
  906. selInfoWin = NULL;
  907. }
  908. CInfoBar::~CInfoBar()
  909. {
  910. delete day;
  911. delete week1;
  912. delete week2;
  913. delete week3;
  914. delete week4;
  915. if(selInfoWin)
  916. SDL_FreeSurface(selInfoWin);
  917. }
  918. void CInfoBar::showAll(SDL_Surface * to)
  919. {
  920. if ((mode>=0) && mode<5)
  921. {
  922. blitAnim(mode);
  923. return;
  924. }
  925. else if (mode==5)
  926. {
  927. mode = -1;
  928. }
  929. if(selInfoWin)
  930. {
  931. blitAt(selInfoWin, pos.x, pos.y, to);
  932. }
  933. }
  934. CDefHandler * CInfoBar::getAnim(int mode)
  935. {
  936. switch(mode)
  937. {
  938. case 0:
  939. return day;
  940. case 1:
  941. return week1;
  942. case 2:
  943. return week2;
  944. case 3:
  945. return week3;
  946. case 4:
  947. return week4;
  948. default:
  949. return NULL;
  950. }
  951. }
  952. void CInfoBar::blitAnim(int mode)//0 - day, 1 - week
  953. {
  954. CDefHandler * anim = NULL;
  955. std::ostringstream txt;
  956. anim = getAnim(mode);
  957. if(mode) //new week animation
  958. {
  959. txt << CGI->generaltexth->allTexts[63] << " " << LOCPLINT->cb->getDate(2);
  960. }
  961. else //new day
  962. {
  963. txt << CGI->generaltexth->allTexts[64] << " " << LOCPLINT->cb->getDate(1);
  964. }
  965. blitAt(anim->ourImages[pom].bitmap,pos.x+9,pos.y+10);
  966. printAtMiddle(txt.str(),pos.x+95,pos.y+31,FONT_MEDIUM,zwykly);
  967. if (pom == anim->ourImages.size()-1)
  968. toNextTick+=750;
  969. }
  970. void CInfoBar::newDay(int Day)
  971. {
  972. if(LOCPLINT->cb->getDate(1) != 1)
  973. {
  974. mode = 0; //showing day
  975. }
  976. else
  977. {
  978. switch(LOCPLINT->cb->getDate(2))
  979. {
  980. case 1:
  981. mode = 1;
  982. break;
  983. case 2:
  984. mode = 2;
  985. break;
  986. case 3:
  987. mode = 3;
  988. break;
  989. case 4:
  990. mode = 4;
  991. break;
  992. default:
  993. mode = -1;
  994. break;
  995. }
  996. }
  997. pom = 0;
  998. if(!(active & TIME))
  999. activateTimer();
  1000. toNextTick = 500;
  1001. blitAnim(mode);
  1002. }
  1003. void CInfoBar::showComp(SComponent * comp, int time)
  1004. {
  1005. SDL_Surface * b = BitmapHandler::loadBitmap("ADSTATOT.bmp");
  1006. blitAt(b,pos.x+8,pos.y+11);
  1007. blitAt(comp->getImg(),pos.x+52,pos.y+54);
  1008. printAtMiddle(comp->subtitle,pos.x+91,pos.y+158,FONT_SMALL,zwykly);
  1009. printAtMiddleWB(comp->description,pos.x+94,pos.y+31,FONT_SMALL,26,zwykly);
  1010. SDL_FreeSurface(b);
  1011. if(!(active & TIME))
  1012. activateTimer();
  1013. mode = 6;
  1014. toNextTick = time;
  1015. }
  1016. void CInfoBar::tick()
  1017. {
  1018. if(mode >= 0 && mode < 5)
  1019. {
  1020. pom++;
  1021. if (pom >= getAnim(mode)->ourImages.size())
  1022. {
  1023. deactivateTimer();
  1024. toNextTick = -1;
  1025. mode = 5;
  1026. showAll(screen2);
  1027. return;
  1028. }
  1029. toNextTick = 150;
  1030. blitAnim(mode);
  1031. }
  1032. else if(mode == 6)
  1033. {
  1034. deactivateTimer();
  1035. toNextTick = -1;
  1036. mode = 5;
  1037. showAll(screen2);
  1038. }
  1039. }
  1040. void CInfoBar::show( SDL_Surface * to )
  1041. {
  1042. }
  1043. void CInfoBar::activate()
  1044. {
  1045. //CIntObject::activate();
  1046. }
  1047. void CInfoBar::deactivate()
  1048. {
  1049. //CIntObject::deactivate();
  1050. if(active & TIME)
  1051. deactivateTimer();
  1052. }
  1053. void CInfoBar::updateSelection(const CGObjectInstance *obj)
  1054. {
  1055. if(selInfoWin)
  1056. SDL_FreeSurface(selInfoWin);
  1057. selInfoWin = LOCPLINT->infoWin(obj);
  1058. }
  1059. CAdvMapInt::CAdvMapInt()
  1060. :statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  1061. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  1062. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  1063. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  1064. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  1065. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  1066. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  1067. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  1068. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  1069. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  1070. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  1071. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  1072. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  1073. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  1074. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  1075. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  1076. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  1077. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  1078. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  1079. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  1080. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  1081. heroList(ADVOPT.hlistSize),
  1082. townList(ADVOPT.tlistSize,ADVOPT.tlistX,ADVOPT.tlistY,ADVOPT.tlistAU,ADVOPT.tlistAD)//(5,&genRect(192,48,747,196),747,196,747,372),
  1083. {
  1084. spellBeingCasted = NULL;
  1085. pos.x = pos.y = 0;
  1086. pos.w = screen->w;
  1087. pos.h = screen->h;
  1088. selection = NULL;
  1089. townList.fun = boost::bind(&CAdvMapInt::selectionChanged,this);
  1090. adventureInt=this;
  1091. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  1092. scrollingDir = 0;
  1093. updateScreen = false;
  1094. anim=0;
  1095. animValHitCount=0; //animation frame
  1096. heroAnim=0;
  1097. heroAnimValHitCount=0; // hero animation frame
  1098. heroList.init();
  1099. heroList.genList();
  1100. //townList.init();
  1101. //townList.genList();
  1102. heroWindow = NULL;
  1103. for (int g=0; g<ADVOPT.gemG.size(); ++g)
  1104. {
  1105. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[g]));
  1106. }
  1107. setPlayer(LOCPLINT->playerID);
  1108. }
  1109. CAdvMapInt::~CAdvMapInt()
  1110. {
  1111. SDL_FreeSurface(bg);
  1112. delete heroWindow;
  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. CGI->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. CGI->curh->changeGraphic(0, 42);
  1697. else
  1698. CGI->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. CGI->curh->changeGraphic(0, 41);
  1706. else
  1707. CGI->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. CGI->curh->changeGraphic(0, 3);
  1719. else if(objAtTile->ID == HEROI_TYPE && objAtTile->tempOwner == LOCPLINT->playerID)
  1720. CGI->curh->changeGraphic(0, 2);
  1721. }
  1722. else
  1723. CGI->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. CGI->curh->changeGraphic(0, 5 + turns*6);
  1735. else
  1736. CGI->curh->changeGraphic(0, 0);
  1737. }
  1738. else //our or ally hero
  1739. {
  1740. if(selection == objAtTile)
  1741. CGI->curh->changeGraphic(0, 2);
  1742. else if(accessible)
  1743. CGI->curh->changeGraphic(0, 8 + turns*6);
  1744. else
  1745. CGI->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. CGI->curh->changeGraphic(0, 9 + turns*6);
  1758. else
  1759. CGI->curh->changeGraphic(0, 5 + turns*6);
  1760. }
  1761. else
  1762. {
  1763. CGI->curh->changeGraphic(0, 0);
  1764. }
  1765. }
  1766. else //our or ally town
  1767. {
  1768. if(accessible)
  1769. CGI->curh->changeGraphic(0, 9 + turns*6);
  1770. else
  1771. CGI->curh->changeGraphic(0, 3);
  1772. }
  1773. }
  1774. else if(objAtTile->ID == 8) //boat
  1775. {
  1776. if(accessible)
  1777. CGI->curh->changeGraphic(0, 6 + turns*6);
  1778. else
  1779. CGI->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. CGI->curh->changeGraphic(0, 5 + turns*6);
  1790. else
  1791. CGI->curh->changeGraphic(0, 9 + turns*6);
  1792. }
  1793. else
  1794. CGI->curh->changeGraphic(0, 0);
  1795. }
  1796. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1797. {
  1798. CGI->curh->changeGraphic(0, 5 + turns*6);
  1799. }
  1800. else
  1801. {
  1802. if(accessible)
  1803. {
  1804. if(pnode->land)
  1805. CGI->curh->changeGraphic(0, 9 + turns*6);
  1806. else
  1807. CGI->curh->changeGraphic(0, 28 + turns);
  1808. }
  1809. else
  1810. CGI->curh->changeGraphic(0, 0);
  1811. }
  1812. }
  1813. else //no objs
  1814. {
  1815. if(accessible && pnode->accessible != CGPathNode::FLYABLE)
  1816. {
  1817. if (guardingCreature) {
  1818. CGI->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. CGI->curh->changeGraphic(0, 4 + turns*6);
  1824. else
  1825. CGI->curh->changeGraphic(0, 7 + turns*6); //anchor
  1826. }
  1827. else
  1828. CGI->curh->changeGraphic(0, 6 + turns*6);
  1829. }
  1830. }
  1831. else
  1832. CGI->curh->changeGraphic(0, 0);
  1833. }
  1834. }
  1835. if(const IShipyard *shipyard = ourInaccessibleShipyard(objAtTile))
  1836. {
  1837. CGI->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 || CGI->curh->mode || (CGI->curh->number != 6 && CGI->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. }