CAdvmapInterface.cpp 54 KB

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