CAdvmapInterface.cpp 53 KB

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