CAdvmapInterface.cpp 50 KB

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