CAdvmapInterface.cpp 50 KB

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