CAdvmapInterface.cpp 51 KB

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