CAdvmapInterface.cpp 50 KB

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