CAdvmapInterface.cpp 56 KB

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