CAdvmapInterface.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  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.fortLevel = 0;
  589. iah.army = garr->army;
  590. iah.name = std::string("Garrison");
  591. iah.owner = garr->tempOwner;
  592. iah.built = false;
  593. iah.details = NULL; // TODO: Find a suitable way to show detailed info.
  594. iah.tType = NULL;
  595. SDL_Surface *iwin = graphics->drawTownInfoWin(iah);
  596. CInfoPopup * ip = new CInfoPopup(iwin,
  597. GH.current->motion.x - iwin->w/2,
  598. GH.current->motion.y - iwin->h/2, true);
  599. GH.pushInt(ip);
  600. }
  601. break;
  602. }
  603. default:
  604. {
  605. LOCPLINT->adventureInt->handleRightClick(obj->getHoverText(),down,this);
  606. break;
  607. }
  608. }
  609. }
  610. void CTerrainRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  611. {
  612. int3 tHovered = whichTileIsIt(sEvent.x,sEvent.y);
  613. int3 pom = LOCPLINT->adventureInt->verifyPos(tHovered);
  614. if(tHovered != pom) //tile outside the map
  615. {
  616. CGI->curh->changeGraphic(0, 0);
  617. return;
  618. }
  619. if (pom != curHoveredTile)
  620. curHoveredTile=pom;
  621. else
  622. return;
  623. std::vector<std::string> temp = LOCPLINT->cb->getObjDescriptions(pom);
  624. if (temp.size())
  625. {
  626. boost::replace_all(temp.back(),"\n"," ");
  627. LOCPLINT->adventureInt->statusbar.print(temp.back());
  628. }
  629. else
  630. {
  631. LOCPLINT->adventureInt->statusbar.clear();
  632. }
  633. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(pom);
  634. std::vector<const CGObjectInstance *> objs = LOCPLINT->cb->getBlockingObjs(pom);
  635. const CGObjectInstance *obj = objs.size() ? objs.back() : NULL;
  636. bool accessible = pnode->turns < 255;
  637. int turns = pnode->turns;
  638. amin(turns, 4);
  639. if(LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE)
  640. {
  641. if(obj)
  642. {
  643. if(obj->ID == TOWNI_TYPE)
  644. {
  645. CGI->curh->changeGraphic(0, 3);
  646. }
  647. else if(obj->ID == HEROI_TYPE)
  648. {
  649. CGI->curh->changeGraphic(0, 2);
  650. }
  651. }
  652. else
  653. {
  654. CGI->curh->changeGraphic(0, 0);
  655. }
  656. }
  657. else if(LOCPLINT->adventureInt->selection->ID == HEROI_TYPE)
  658. {
  659. const CGHeroInstance *h = static_cast<const CGHeroInstance *>(LOCPLINT->adventureInt->selection);
  660. if(obj)
  661. {
  662. if(obj->ID == HEROI_TYPE)
  663. {
  664. if(obj->tempOwner != LOCPLINT->playerID) //enemy hero TODO: allies
  665. {
  666. if(accessible)
  667. CGI->curh->changeGraphic(0, 5 + turns*6);
  668. else
  669. CGI->curh->changeGraphic(0, 0);
  670. }
  671. else //our hero
  672. {
  673. if(LOCPLINT->adventureInt->selection == obj)
  674. CGI->curh->changeGraphic(0, 2);
  675. else if(accessible)
  676. CGI->curh->changeGraphic(0, 8 + turns*6);
  677. else
  678. CGI->curh->changeGraphic(0, 2);
  679. }
  680. }
  681. else if(obj->ID == TOWNI_TYPE)
  682. {
  683. if(obj->tempOwner != LOCPLINT->playerID) //enemy town TODO: allies
  684. {
  685. if(accessible) {
  686. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(obj);
  687. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  688. if (townObj && townObj->army.slots.empty())
  689. CGI->curh->changeGraphic(0, 9 + turns*6);
  690. else
  691. CGI->curh->changeGraphic(0, 5 + turns*6);
  692. } else {
  693. CGI->curh->changeGraphic(0, 0);
  694. }
  695. }
  696. else //our town
  697. {
  698. if(accessible)
  699. CGI->curh->changeGraphic(0, 9 + turns*6);
  700. else
  701. CGI->curh->changeGraphic(0, 3);
  702. }
  703. }
  704. else if(obj->ID == 54) //monster
  705. {
  706. if(accessible)
  707. CGI->curh->changeGraphic(0, 5 + turns*6);
  708. else
  709. CGI->curh->changeGraphic(0, 0);
  710. }
  711. else if(obj->ID == 8) //boat
  712. {
  713. if(accessible)
  714. CGI->curh->changeGraphic(0, 6 + turns*6);
  715. else
  716. CGI->curh->changeGraphic(0, 0);
  717. }
  718. else if (obj->ID == 33 || obj->ID == 219) // Garrison
  719. {
  720. if (accessible) {
  721. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(obj);
  722. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  723. if (garrObj && garrObj->tempOwner != LOCPLINT->playerID
  724. && !garrObj->army.slots.empty())
  725. {
  726. CGI->curh->changeGraphic(0, 5 + turns*6);
  727. }
  728. else
  729. {
  730. CGI->curh->changeGraphic(0, 9 + turns*6);
  731. }
  732. } else {
  733. CGI->curh->changeGraphic(0, 0);
  734. }
  735. }
  736. else
  737. {
  738. if(accessible)
  739. {
  740. if(pnode->land)
  741. CGI->curh->changeGraphic(0, 9 + turns*6);
  742. else
  743. CGI->curh->changeGraphic(0, 28 + turns);
  744. }
  745. else
  746. CGI->curh->changeGraphic(0, 0);
  747. }
  748. }
  749. else //no objs
  750. {
  751. if(accessible)
  752. {
  753. if(pnode->land)
  754. {
  755. if(LOCPLINT->cb->getTileInfo(h->getPosition(false))->tertype != TerrainTile::water)
  756. CGI->curh->changeGraphic(0, 4 + turns*6);
  757. else
  758. CGI->curh->changeGraphic(0, 7 + turns*6); //anchor
  759. }
  760. else
  761. CGI->curh->changeGraphic(0, 6 + turns*6);
  762. }
  763. else
  764. CGI->curh->changeGraphic(0, 0);
  765. }
  766. }
  767. //tlog1 << "Tile " << pom << ": Turns=" << (int)pnode->turns <<" Move:=" << pnode->moveRemains <</* " (from " << ")" << */std::endl;
  768. }
  769. void CTerrainRect::hover(bool on)
  770. {
  771. if (!on)
  772. {
  773. LOCPLINT->adventureInt->statusbar.clear();
  774. CGI->curh->changeGraphic(0,0);
  775. }
  776. //Hoverable::hover(on);
  777. }
  778. void CTerrainRect::showPath(const SDL_Rect * extRect)
  779. {
  780. for (size_t i=0; i < currentPath->nodes.size()-1; ++i)
  781. {
  782. int pn=-1;//number of picture
  783. if (i==0) //last tile
  784. {
  785. int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  786. y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  787. if (x<0 || y<0 || x>pos.w || y>pos.h)
  788. continue;
  789. pn=0;
  790. }
  791. else
  792. {
  793. /*
  794. * notation of arrow direction:
  795. * 1 2 3
  796. * 4 5 6
  797. * 7 8 9
  798. * ie. 157 means an arrow from left upper tile to left bottom tile through 5 (all arrows go through 5 in this notation)
  799. */
  800. std::vector<CGPathNode> & cv = currentPath->nodes;
  801. if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y-1) //15x
  802. {
  803. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //156
  804. {
  805. pn = 3;
  806. }
  807. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //159
  808. {
  809. pn = 12;
  810. }
  811. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //158
  812. {
  813. pn = 21;
  814. }
  815. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //157
  816. {
  817. pn = 22;
  818. }
  819. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //153
  820. {
  821. pn = 2;
  822. }
  823. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //154
  824. {
  825. pn = 23;
  826. }
  827. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //152
  828. {
  829. pn = 1;
  830. }
  831. }
  832. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y-1) //25x
  833. {
  834. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //253
  835. {
  836. pn = 2;
  837. }
  838. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //256
  839. {
  840. pn = 3;
  841. }
  842. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //259
  843. {
  844. pn = 4;
  845. }
  846. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //258
  847. {
  848. pn = 13;
  849. }
  850. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //257
  851. {
  852. pn = 22;
  853. }
  854. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //254
  855. {
  856. pn = 23;
  857. }
  858. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //251
  859. {
  860. pn = 24;
  861. }
  862. }
  863. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y-1) //35x
  864. {
  865. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //358
  866. {
  867. pn = 5;
  868. }
  869. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //357
  870. {
  871. pn = 14;
  872. }
  873. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //354
  874. {
  875. pn = 23;
  876. }
  877. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //351
  878. {
  879. pn = 24;
  880. }
  881. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //359
  882. {
  883. pn = 4;
  884. }
  885. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //356
  886. {
  887. pn = 3;
  888. }
  889. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //352
  890. {
  891. pn = 17;
  892. }
  893. }
  894. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y) //65x
  895. {
  896. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //657
  897. {
  898. pn = 6;
  899. }
  900. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //654
  901. {
  902. pn = 15;
  903. }
  904. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //651
  905. {
  906. pn = 24;
  907. }
  908. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //652
  909. {
  910. pn = 17;
  911. }
  912. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //658
  913. {
  914. pn = 5;
  915. }
  916. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //653
  917. {
  918. pn = 18;
  919. }
  920. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //659
  921. {
  922. pn = 4;
  923. }
  924. }
  925. else if (cv[i+1].coord.x == cv[i].coord.x+1 && cv[i+1].coord.y == cv[i].coord.y+1) //95x
  926. {
  927. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //954
  928. {
  929. pn = 7;
  930. }
  931. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //951
  932. {
  933. pn = 16;
  934. }
  935. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //952
  936. {
  937. pn = 17;
  938. }
  939. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //957
  940. {
  941. pn = 6;
  942. }
  943. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //953
  944. {
  945. pn = 18;
  946. }
  947. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //956
  948. {
  949. pn = 19;
  950. }
  951. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //958
  952. {
  953. pn = 5;
  954. }
  955. }
  956. else if (cv[i+1].coord.x == cv[i].coord.x && cv[i+1].coord.y == cv[i].coord.y+1) //85x
  957. {
  958. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //857
  959. {
  960. pn = 6;
  961. }
  962. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y) //854
  963. {
  964. pn = 7;
  965. }
  966. if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //851
  967. {
  968. pn = 8;
  969. }
  970. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //852
  971. {
  972. pn = 9;
  973. }
  974. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //853
  975. {
  976. pn = 18;
  977. }
  978. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //856
  979. {
  980. pn = 19;
  981. }
  982. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //859
  983. {
  984. pn = 20;
  985. }
  986. }
  987. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y+1) //75x
  988. {
  989. if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //752
  990. {
  991. pn = 1;
  992. }
  993. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //753
  994. {
  995. pn = 10;
  996. }
  997. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //756
  998. {
  999. pn = 19;
  1000. }
  1001. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //751
  1002. {
  1003. pn = 8;
  1004. }
  1005. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //759
  1006. {
  1007. pn = 20;
  1008. }
  1009. }
  1010. else if (cv[i+1].coord.x == cv[i].coord.x-1 && cv[i+1].coord.y == cv[i].coord.y) //45x
  1011. {
  1012. if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y-1) //453
  1013. {
  1014. pn = 2;
  1015. }
  1016. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y) //456
  1017. {
  1018. pn = 11;
  1019. }
  1020. else if(cv[i-1].coord.x == cv[i].coord.x+1 && cv[i-1].coord.y == cv[i].coord.y+1) //459
  1021. {
  1022. pn = 20;
  1023. }
  1024. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y-1) //452
  1025. {
  1026. pn = 1;
  1027. }
  1028. else if(cv[i-1].coord.x == cv[i].coord.x && cv[i-1].coord.y == cv[i].coord.y+1) //456
  1029. {
  1030. pn = 21;
  1031. }
  1032. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y-1) //451
  1033. {
  1034. pn = 8;
  1035. }
  1036. else if(cv[i-1].coord.x == cv[i].coord.x-1 && cv[i-1].coord.y == cv[i].coord.y+1) //457
  1037. {
  1038. pn = 22;
  1039. }
  1040. }
  1041. }
  1042. if (currentPath->nodes[i].turns)
  1043. pn+=25;
  1044. if (pn>=0)
  1045. {
  1046. int x = 32*(currentPath->nodes[i].coord.x-LOCPLINT->adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  1047. y = 32*(currentPath->nodes[i].coord.y-LOCPLINT->adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  1048. if (x<0 || y<0 || x>pos.w || y>pos.h)
  1049. continue;
  1050. int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
  1051. hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
  1052. SDL_Rect prevClip;
  1053. SDL_GetClipRect(screen, &prevClip);
  1054. SDL_SetClipRect(screen, extRect); //preventing blitting outside of that rect
  1055. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  1056. {
  1057. if (hvx<0 && hvy<0)
  1058. {
  1059. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, screen, &genRect(32, 32, x + moveX, y + moveY));
  1060. }
  1061. else if(hvx<0)
  1062. {
  1063. CSDL_Ext::blit8bppAlphaTo24bpp
  1064. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  1065. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY));
  1066. }
  1067. else if (hvy<0)
  1068. {
  1069. CSDL_Ext::blit8bppAlphaTo24bpp
  1070. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1071. screen, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  1072. }
  1073. else
  1074. {
  1075. CSDL_Ext::blit8bppAlphaTo24bpp
  1076. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1077. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY));
  1078. }
  1079. }
  1080. else //standard version
  1081. {
  1082. if (hvx<0 && hvy<0)
  1083. {
  1084. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, screen, &genRect(32, 32, x, y));
  1085. }
  1086. else if(hvx<0)
  1087. {
  1088. CSDL_Ext::blit8bppAlphaTo24bpp
  1089. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0),
  1090. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y));
  1091. }
  1092. else if (hvy<0)
  1093. {
  1094. CSDL_Ext::blit8bppAlphaTo24bpp
  1095. (arrows->ourImages[pn].bitmap,&genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1096. screen, &genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  1097. }
  1098. else
  1099. {
  1100. CSDL_Ext::blit8bppAlphaTo24bpp
  1101. (arrows->ourImages[pn].bitmap, &genRect(arrows->ourImages[pn].bitmap->h-hvy,arrows->ourImages[pn].bitmap->w-hvx, 0, 0),
  1102. screen, &genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y));
  1103. }
  1104. }
  1105. SDL_SetClipRect(screen, &prevClip);
  1106. }
  1107. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  1108. }
  1109. void CTerrainRect::show(SDL_Surface * to)
  1110. {
  1111. if(ADVOPT.smoothMove)
  1112. CGI->mh->terrainRect
  1113. (LOCPLINT->adventureInt->position, LOCPLINT->adventureInt->anim,
  1114. &LOCPLINT->cb->getVisibilityMap(), true, LOCPLINT->adventureInt->heroAnim,
  1115. to, &pos, moveX, moveY, false);
  1116. else
  1117. CGI->mh->terrainRect
  1118. (LOCPLINT->adventureInt->position, LOCPLINT->adventureInt->anim,
  1119. &LOCPLINT->cb->getVisibilityMap(), true, LOCPLINT->adventureInt->heroAnim,
  1120. to, &pos, 0, 0, false);
  1121. //SDL_BlitSurface(teren,&genRect(pos.h,pos.w,0,0),screen,&genRect(547,594,7,6));
  1122. //SDL_FreeSurface(teren);
  1123. if (currentPath && LOCPLINT->adventureInt->position.z==currentPath->startPos().z) //drawing path
  1124. {
  1125. showPath(&pos);
  1126. }
  1127. }
  1128. int3 CTerrainRect::whichTileIsIt(const int & x, const int & y)
  1129. {
  1130. int3 ret;
  1131. ret.x = LOCPLINT->adventureInt->position.x + ((GH.current->motion.x-CGI->mh->offsetX-pos.x)/32);
  1132. ret.y = LOCPLINT->adventureInt->position.y + ((GH.current->motion.y-CGI->mh->offsetY-pos.y)/32);
  1133. ret.z = LOCPLINT->adventureInt->position.z;
  1134. return ret;
  1135. }
  1136. int3 CTerrainRect::whichTileIsIt()
  1137. {
  1138. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  1139. }
  1140. void CResDataBar::clickRight(tribool down, bool previousState)
  1141. {
  1142. }
  1143. void CResDataBar::activate()
  1144. {
  1145. activateRClick();
  1146. }
  1147. void CResDataBar::deactivate()
  1148. {
  1149. deactivateRClick();
  1150. }
  1151. CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
  1152. {
  1153. bg = BitmapHandler::loadBitmap(defname);
  1154. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1155. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1156. pos = genRect(bg->h,bg->w,x,y);
  1157. txtpos.resize(8);
  1158. for (int i = 0; i < 8 ; i++)
  1159. {
  1160. txtpos[i].first = pos.x + offx + resdist*i;
  1161. txtpos[i].second = pos.y + offy;
  1162. }
  1163. txtpos[7].first = txtpos[6].first + datedist;
  1164. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  1165. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  1166. }
  1167. CResDataBar::CResDataBar()
  1168. {
  1169. bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
  1170. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1171. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1172. pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
  1173. txtpos.resize(8);
  1174. for (int i = 0; i < 8 ; i++)
  1175. {
  1176. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  1177. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  1178. }
  1179. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  1180. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  1181. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  1182. }
  1183. CResDataBar::~CResDataBar()
  1184. {
  1185. SDL_FreeSurface(bg);
  1186. }
  1187. void CResDataBar::draw(SDL_Surface * to)
  1188. {
  1189. blitAt(bg,pos.x,pos.y,to);
  1190. char * buf = new char[15];
  1191. for (int i=0;i<7;i++)
  1192. {
  1193. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  1194. printAt(buf,txtpos[i].first,txtpos[i].second,GEOR13,zwykly,to);
  1195. }
  1196. std::vector<std::string> temp;
  1197. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp+=std::string(buf);
  1198. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp+=std::string(buf);
  1199. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp+=std::string(buf);
  1200. printAt(processStr(datetext,temp),txtpos[7].first,txtpos[7].second,GEOR13,zwykly,to);
  1201. temp.clear();
  1202. //updateRect(&pos,screen);
  1203. delete[] buf;
  1204. }
  1205. void CResDataBar::show( SDL_Surface * to )
  1206. {
  1207. }
  1208. CInfoBar::CInfoBar()
  1209. {
  1210. toNextTick = mode = pom = -1;
  1211. pos.x=ADVOPT.infoboxX;
  1212. pos.y=ADVOPT.infoboxY;
  1213. pos.w=194;
  1214. pos.h=186;
  1215. day = CDefHandler::giveDef("NEWDAY.DEF");
  1216. week1 = CDefHandler::giveDef("NEWWEEK1.DEF");
  1217. week2 = CDefHandler::giveDef("NEWWEEK2.DEF");
  1218. week3 = CDefHandler::giveDef("NEWWEEK3.DEF");
  1219. week4 = CDefHandler::giveDef("NEWWEEK4.DEF");
  1220. }
  1221. CInfoBar::~CInfoBar()
  1222. {
  1223. delete day;
  1224. delete week1;
  1225. delete week2;
  1226. delete week3;
  1227. delete week4;
  1228. }
  1229. void CInfoBar::draw(SDL_Surface * to, const CGObjectInstance * specific)
  1230. {
  1231. if ((mode>=0) && mode<5)
  1232. {
  1233. blitAnim(mode);
  1234. return;
  1235. }
  1236. else if (mode==5)
  1237. {
  1238. mode = -1;
  1239. draw(to,LOCPLINT->adventureInt->selection);
  1240. }
  1241. if (!specific)
  1242. specific = LOCPLINT->adventureInt->selection;
  1243. if(!specific)
  1244. return;
  1245. if(specific->ID == HEROI_TYPE) //hero
  1246. {
  1247. if(graphics->heroWins.find(specific->subID)!=graphics->heroWins.end())
  1248. blitAt(graphics->heroWins[specific->subID],pos.x,pos.y,to);
  1249. }
  1250. else if (specific->ID == TOWNI_TYPE)
  1251. {
  1252. const CGTownInstance * t = static_cast<const CGTownInstance*>(specific);
  1253. if(graphics->townWins.find(t->id)!=graphics->townWins.end())
  1254. blitAt(graphics->townWins[t->id],pos.x,pos.y,to);
  1255. }
  1256. }
  1257. CDefHandler * CInfoBar::getAnim(int mode)
  1258. {
  1259. switch(mode)
  1260. {
  1261. case 0:
  1262. return day;
  1263. case 1:
  1264. return week1;
  1265. case 2:
  1266. return week2;
  1267. case 3:
  1268. return week3;
  1269. case 4:
  1270. return week4;
  1271. default:
  1272. return NULL;
  1273. }
  1274. }
  1275. void CInfoBar::blitAnim(int mode)//0 - day, 1 - week
  1276. {
  1277. CDefHandler * anim = NULL;
  1278. std::ostringstream txt;
  1279. anim = getAnim(mode);
  1280. if(mode) //new week animation
  1281. {
  1282. txt << CGI->generaltexth->allTexts[63] << " " << LOCPLINT->cb->getDate(2);
  1283. }
  1284. else //new day
  1285. {
  1286. txt << CGI->generaltexth->allTexts[64] << " " << LOCPLINT->cb->getDate(1);
  1287. }
  1288. blitAt(anim->ourImages[pom].bitmap,pos.x+9,pos.y+10);
  1289. printAtMiddle(txt.str(),pos.x+95,pos.y+31,TNRB16,zwykly);
  1290. if (pom == anim->ourImages.size()-1)
  1291. toNextTick+=750;
  1292. }
  1293. void CInfoBar::newDay(int Day)
  1294. {
  1295. if(LOCPLINT->cb->getDate(1) != 1)
  1296. {
  1297. mode = 0; //showing day
  1298. }
  1299. else
  1300. {
  1301. switch(LOCPLINT->cb->getDate(2))
  1302. {
  1303. case 1:
  1304. mode = 1;
  1305. break;
  1306. case 2:
  1307. mode = 2;
  1308. break;
  1309. case 3:
  1310. mode = 3;
  1311. break;
  1312. case 4:
  1313. mode = 4;
  1314. break;
  1315. default:
  1316. mode = -1;
  1317. break;
  1318. }
  1319. }
  1320. pom = 0;
  1321. activateTimer();
  1322. toNextTick = 500;
  1323. blitAnim(mode);
  1324. }
  1325. void CInfoBar::showComp(SComponent * comp, int time)
  1326. {
  1327. SDL_Surface * b = BitmapHandler::loadBitmap("ADSTATOT.bmp");
  1328. blitAt(b,pos.x+8,pos.y+11);
  1329. blitAt(comp->getImg(),pos.x+52,pos.y+54);
  1330. printAtMiddle(comp->subtitle,pos.x+91,pos.y+158,GEOR13,zwykly);
  1331. printAtMiddleWB(comp->description,pos.x+94,pos.y+31,GEOR13,26,zwykly);
  1332. SDL_FreeSurface(b);
  1333. activateTimer();
  1334. mode = 6;
  1335. toNextTick = time;
  1336. }
  1337. void CInfoBar::tick()
  1338. {
  1339. if((mode >= 0) && (mode < 5))
  1340. {
  1341. pom++;
  1342. if (pom >= getAnim(mode)->ourImages.size())
  1343. {
  1344. deactivateTimer();
  1345. toNextTick = -1;
  1346. mode = 5;
  1347. draw(screen2);
  1348. return;
  1349. }
  1350. toNextTick = 150;
  1351. blitAnim(mode);
  1352. }
  1353. else if (mode == 6)
  1354. {
  1355. deactivateTimer();
  1356. toNextTick = -1;
  1357. mode = 5;
  1358. draw(screen2);
  1359. }
  1360. }
  1361. void CInfoBar::show( SDL_Surface * to )
  1362. {
  1363. }
  1364. CAdvMapInt::CAdvMapInt(int Player)
  1365. :player(Player),
  1366. statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  1367. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  1368. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  1369. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  1370. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  1371. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  1372. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  1373. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  1374. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  1375. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  1376. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  1377. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  1378. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  1379. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  1380. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  1381. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  1382. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  1383. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  1384. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  1385. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  1386. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  1387. heroList(ADVOPT.hlistSize),
  1388. townList(ADVOPT.tlistSize,ADVOPT.tlistX,ADVOPT.tlistY,ADVOPT.tlistAU,ADVOPT.tlistAD)//(5,&genRect(192,48,747,196),747,196,747,372),
  1389. {
  1390. pos.x = pos.y = 0;
  1391. pos.w = screen->w;
  1392. pos.h = screen->h;
  1393. active = 0;
  1394. selection = NULL;
  1395. townList.fun = boost::bind(&CAdvMapInt::selectionChanged,this);
  1396. LOCPLINT->adventureInt=this;
  1397. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  1398. graphics->blueToPlayersAdv(bg,player);
  1399. scrollingDir = 0;
  1400. updateScreen = false;
  1401. anim=0;
  1402. animValHitCount=0; //animation frame
  1403. heroAnim=0;
  1404. heroAnimValHitCount=0; // hero animation frame
  1405. heroList.init();
  1406. heroList.genList();
  1407. //townList.init();
  1408. townList.genList();
  1409. heroWindow = new CHeroWindow(this->player);
  1410. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[0]));
  1411. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[1]));
  1412. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[2]));
  1413. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[3]));
  1414. }
  1415. CAdvMapInt::~CAdvMapInt()
  1416. {
  1417. SDL_FreeSurface(bg);
  1418. delete heroWindow;
  1419. for(int i=0; i<gems.size(); i++)
  1420. delete gems[i];
  1421. }
  1422. void CAdvMapInt::fshowOverview()
  1423. {
  1424. }
  1425. void CAdvMapInt::fswitchLevel()
  1426. {
  1427. if(!CGI->mh->map->twoLevel)
  1428. return;
  1429. if (position.z)
  1430. {
  1431. position.z--;
  1432. underground.curimg=0;
  1433. underground.show(screenBuf);
  1434. }
  1435. else
  1436. {
  1437. underground.curimg=1;
  1438. position.z++;
  1439. underground.show(screenBuf);
  1440. }
  1441. updateScreen = true;
  1442. minimap.draw(screenBuf);
  1443. }
  1444. void CAdvMapInt::fshowQuestlog()
  1445. {
  1446. }
  1447. void CAdvMapInt::fsleepWake()
  1448. {
  1449. }
  1450. void CAdvMapInt::fmoveHero()
  1451. {
  1452. if (selection->ID!=HEROI_TYPE)
  1453. return;
  1454. if (!terrain.currentPath)
  1455. return;
  1456. LOCPLINT->pim->unlock();
  1457. LOCPLINT->moveHero(static_cast<const CGHeroInstance*>(LOCPLINT->adventureInt->selection),*terrain.currentPath);
  1458. LOCPLINT->pim->lock();
  1459. }
  1460. void CAdvMapInt::fshowSpellbok()
  1461. {
  1462. if (selection->ID!=HEROI_TYPE) //checking necessary values
  1463. return;
  1464. 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);
  1465. GH.pushInt(spellWindow);
  1466. }
  1467. void CAdvMapInt::fadventureOPtions()
  1468. {
  1469. GH.pushInt(new CAdventureOptions);
  1470. }
  1471. void CAdvMapInt::fsystemOptions()
  1472. {
  1473. CSystemOptionsWindow * sysopWindow = new CSystemOptionsWindow(center(genRect(487, 481, 0, 0)), LOCPLINT);
  1474. GH.pushInt(sysopWindow);
  1475. }
  1476. void CAdvMapInt::fnextHero()
  1477. {
  1478. if(!LOCPLINT->wanderingHeroes.size()) //no wandering heroes
  1479. return;
  1480. int start = heroList.selected;
  1481. int i = start;
  1482. do
  1483. {
  1484. i++;
  1485. if(i >= LOCPLINT->wanderingHeroes.size())
  1486. i = 0;
  1487. } while (!LOCPLINT->wanderingHeroes[i]->movement && i!=start);
  1488. heroList.select(i);
  1489. }
  1490. void CAdvMapInt::fendTurn()
  1491. {
  1492. LOCPLINT->makingTurn = false;
  1493. }
  1494. void CAdvMapInt::activate()
  1495. {
  1496. if(active++)
  1497. {
  1498. tlog1 << "Error: advmapint already active...\n";
  1499. active--;
  1500. return;
  1501. }
  1502. screenBuf = screen;
  1503. LOCPLINT->statusbar = &statusbar;
  1504. activateMouseMove();
  1505. kingOverview.activate();
  1506. underground.activate();
  1507. questlog.activate();
  1508. sleepWake.activate();
  1509. moveHero.activate();
  1510. spellbook.activate();
  1511. sysOptions.activate();
  1512. advOptions.activate();
  1513. nextHero.activate();
  1514. endTurn.activate();
  1515. minimap.activate();
  1516. heroList.activate();
  1517. townList.activate();
  1518. terrain.activate();
  1519. LOCPLINT->cingconsole->activate();
  1520. }
  1521. void CAdvMapInt::deactivate()
  1522. {
  1523. deactivateMouseMove();
  1524. scrollingDir = 0;
  1525. CGI->curh->changeGraphic(0,0);
  1526. kingOverview.deactivate();
  1527. underground.deactivate();
  1528. questlog.deactivate();
  1529. sleepWake.deactivate();
  1530. moveHero.deactivate();
  1531. spellbook.deactivate();
  1532. advOptions.deactivate();
  1533. sysOptions.deactivate();
  1534. nextHero.deactivate();
  1535. endTurn.deactivate();
  1536. minimap.deactivate();
  1537. heroList.deactivate();
  1538. townList.deactivate();
  1539. terrain.deactivate();
  1540. if(std::find(GH.timeinterested.begin(),GH.timeinterested.end(),&infoBar)!=GH.timeinterested.end())
  1541. GH.timeinterested.erase(std::find(GH.timeinterested.begin(),GH.timeinterested.end(),&infoBar));
  1542. infoBar.mode=-1;
  1543. LOCPLINT->cingconsole->deactivate();
  1544. if(--active)
  1545. {
  1546. tlog1 << "Error: advmapint still active...\n";
  1547. deactivate();
  1548. }
  1549. }
  1550. void CAdvMapInt::showAll(SDL_Surface *to)
  1551. {
  1552. blitAt(bg,0,0,to);
  1553. kingOverview.show(to);
  1554. underground.show(to);
  1555. questlog.show(to);
  1556. sleepWake.show(to);
  1557. moveHero.show(to);
  1558. spellbook.show(to);
  1559. advOptions.show(to);
  1560. sysOptions.show(to);
  1561. nextHero.show(to);
  1562. endTurn.show(to);
  1563. minimap.draw(to);
  1564. heroList.draw(to);
  1565. townList.draw(to);
  1566. updateScreen = true;
  1567. show(to);
  1568. resdatabar.draw(to);
  1569. statusbar.show(to);
  1570. infoBar.draw(to);
  1571. LOCPLINT->cingconsole->show(to);
  1572. }
  1573. void CAdvMapInt::show(SDL_Surface *to)
  1574. {
  1575. ++animValHitCount; //for animations
  1576. if(animValHitCount == 8)
  1577. {
  1578. CGI->mh->updateWater();
  1579. animValHitCount = 0;
  1580. ++anim;
  1581. updateScreen = true;
  1582. }
  1583. ++heroAnim;
  1584. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  1585. if((animValHitCount % (4/LOCPLINT->sysOpts.mapScrollingSpeed)) == 0
  1586. &&
  1587. (GH.topInt() == this)
  1588. || SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1589. || SDL_GetKeyState(NULL)[SDLK_RCTRL]
  1590. )
  1591. {
  1592. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  1593. position.x--;
  1594. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  1595. position.x++;
  1596. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  1597. position.y--;
  1598. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  1599. position.y++;
  1600. if(scrollingDir)
  1601. {
  1602. updateScreen = true;
  1603. updateMinimap=true;
  1604. }
  1605. }
  1606. if(updateScreen)
  1607. {
  1608. terrain.show(to);
  1609. for(int i=0;i<4;i++)
  1610. blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  1611. updateScreen=false;
  1612. LOCPLINT->cingconsole->show(to);
  1613. }
  1614. if (updateMinimap)
  1615. {
  1616. minimap.draw(to);
  1617. updateMinimap=false;
  1618. }
  1619. }
  1620. void CAdvMapInt::selectionChanged()
  1621. {
  1622. const CGTownInstance *to = townList.items[townList.selected];
  1623. select(to);
  1624. }
  1625. void CAdvMapInt::centerOn(int3 on)
  1626. {
  1627. // TODO:convertPosition should not belong to CGHeroInstance, and it
  1628. // should be split in 2 methods.
  1629. on = CGHeroInstance::convertPosition(on, false);
  1630. on.x -= CGI->mh->frameW;
  1631. on.y -= CGI->mh->frameH;
  1632. on = LOCPLINT->repairScreenPos(on);
  1633. LOCPLINT->adventureInt->position = on;
  1634. LOCPLINT->adventureInt->updateScreen=true;
  1635. updateMinimap=true;
  1636. underground.curimg = on.z; //change underground switch button image
  1637. if(GH.topInt() == this)
  1638. underground.redraw();
  1639. }
  1640. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1641. {
  1642. ui8 Dir = 0;
  1643. int k = key.keysym.sym;
  1644. switch(k)
  1645. {
  1646. case SDLK_i:
  1647. if(active)
  1648. CAdventureOptions::showScenarioInfo();
  1649. return;
  1650. case SDLK_s:
  1651. if(active)
  1652. GH.pushInt(new CSelectionScreen(saveGame));
  1653. return;
  1654. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1655. {
  1656. if(!active)
  1657. return;
  1658. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance*>(selection);
  1659. if(h && key.state == SDL_PRESSED)
  1660. {
  1661. LOCPLINT->pim->unlock();
  1662. LOCPLINT->cb->moveHero(h,h->pos);
  1663. LOCPLINT->pim->lock();
  1664. }
  1665. }
  1666. return;
  1667. case SDLK_RETURN:
  1668. {
  1669. if(!active || !selection || key.state != SDL_PRESSED)
  1670. return;
  1671. if(selection->ID == HEROI_TYPE)
  1672. LOCPLINT->openHeroWindow(static_cast<const CGHeroInstance*>(selection));
  1673. else if(selection->ID == TOWNI_TYPE)
  1674. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(selection));
  1675. return;
  1676. }
  1677. case SDLK_t:
  1678. {
  1679. //act on key down if marketplace windows is not already opened
  1680. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS) return;
  1681. //check if we have aby marketplace
  1682. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo();
  1683. size_t i = 0;
  1684. for(; i<towns.size(); i++)
  1685. if(vstd::contains(towns[i]->builtBuildings, 14))
  1686. break;
  1687. if(i != towns.size()) //if any town has marketplace, open window
  1688. GH.pushInt(new CMarketplaceWindow);
  1689. else //if not - complain
  1690. LOCPLINT->showInfoDialog("No available marketplace!", std::vector<SComponent*>(), soundBase::sound_todo);
  1691. return;
  1692. }
  1693. default:
  1694. {
  1695. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1696. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1697. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1698. //numpad arrow
  1699. if(isArrowKey(SDLKey(k)))
  1700. {
  1701. switch(k)
  1702. {
  1703. case SDLK_UP:
  1704. Dir = UP;
  1705. break;
  1706. case SDLK_LEFT:
  1707. Dir = LEFT;
  1708. break;
  1709. case SDLK_RIGHT:
  1710. Dir = RIGHT;
  1711. break;
  1712. case SDLK_DOWN:
  1713. Dir = DOWN;
  1714. break;
  1715. }
  1716. k = arrowToNum(SDLKey(k));
  1717. }
  1718. if(!active)
  1719. break;
  1720. k -= SDLK_KP0 + 1;
  1721. if(k < 0 || k > 8 || key.state != SDL_PRESSED)
  1722. return;
  1723. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(selection);
  1724. if(!h) break;
  1725. if(k == 4)
  1726. {
  1727. centerOn(h->getPosition(false));
  1728. return;
  1729. }
  1730. int3 dir = directions[k];
  1731. CGPath &path = paths[h];
  1732. terrain.currentPath = &path;
  1733. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  1734. {
  1735. terrain.currentPath = NULL;
  1736. return;
  1737. }
  1738. if(!path.nodes[0].turns)
  1739. {
  1740. LOCPLINT->pim->unlock();
  1741. LOCPLINT->moveHero(h, path);
  1742. LOCPLINT->pim->lock();
  1743. }
  1744. }
  1745. return;
  1746. }
  1747. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1748. && (SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1749. || SDL_GetKeyState(NULL)[SDLK_RCTRL])
  1750. )
  1751. scrollingDir |= Dir;
  1752. else
  1753. scrollingDir &= ~Dir;
  1754. }
  1755. void CAdvMapInt::handleRightClick(std::string text, tribool down, CIntObject * client)
  1756. {
  1757. if (down)
  1758. {
  1759. CSimpleWindow * temp = CMessage::genWindow(text,LOCPLINT->playerID,true);
  1760. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  1761. GH.pushInt(rcpi);
  1762. }
  1763. }
  1764. int3 CAdvMapInt::verifyPos(int3 ver)
  1765. {
  1766. if (ver.x<0)
  1767. ver.x=0;
  1768. if (ver.y<0)
  1769. ver.y=0;
  1770. if (ver.z<0)
  1771. ver.z=0;
  1772. if (ver.x>=CGI->mh->sizes.x)
  1773. ver.x=CGI->mh->sizes.x-1;
  1774. if (ver.y>=CGI->mh->sizes.y)
  1775. ver.y=CGI->mh->sizes.y-1;
  1776. if (ver.z>=CGI->mh->sizes.z)
  1777. ver.z=CGI->mh->sizes.z-1;
  1778. return ver;
  1779. }
  1780. void CAdvMapInt::select(const CArmedInstance *sel )
  1781. {
  1782. LOCPLINT->cb->setSelection(sel);
  1783. centerOn(sel->pos);
  1784. selection = sel;
  1785. terrain.currentPath = NULL;
  1786. if(sel->ID==TOWNI_TYPE)
  1787. {
  1788. int pos = vstd::findPos(townList.items,sel);
  1789. townList.selected = pos;
  1790. townList.fixPos();
  1791. }
  1792. else //hero selected
  1793. {
  1794. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(sel);
  1795. if(LOCPLINT->getWHero(heroList.selected) != h)
  1796. {
  1797. heroList.selected = heroList.getPosOfHero(h);
  1798. heroList.fixPos();
  1799. }
  1800. if(vstd::contains(paths,h)) //hero has assigned path
  1801. {
  1802. CGPath &path = paths[h];
  1803. assert(h->getPosition(false) == path.startPos());
  1804. //update the hero path in case of something has changed on map
  1805. if(LOCPLINT->cb->getPath2(path.endPos(), path))
  1806. terrain.currentPath = &path;
  1807. else
  1808. paths.erase(h);
  1809. }
  1810. }
  1811. townList.draw(screen);
  1812. heroList.draw(screen);
  1813. infoBar.draw(screen);
  1814. }
  1815. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1816. {
  1817. //adventure map scrolling with mouse
  1818. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && active)
  1819. {
  1820. if(sEvent.x<15)
  1821. {
  1822. scrollingDir |= LEFT;
  1823. }
  1824. else
  1825. {
  1826. scrollingDir &= ~LEFT;
  1827. }
  1828. if(sEvent.x>screen->w-15)
  1829. {
  1830. scrollingDir |= RIGHT;
  1831. }
  1832. else
  1833. {
  1834. scrollingDir &= ~RIGHT;
  1835. }
  1836. if(sEvent.y<15)
  1837. {
  1838. scrollingDir |= UP;
  1839. }
  1840. else
  1841. {
  1842. scrollingDir &= ~UP;
  1843. }
  1844. if(sEvent.y>screen->h-15)
  1845. {
  1846. scrollingDir |= DOWN;
  1847. }
  1848. else
  1849. {
  1850. scrollingDir &= ~DOWN;
  1851. }
  1852. }
  1853. }
  1854. CAdventureOptions::CAdventureOptions()
  1855. {
  1856. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1857. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTS.bmp"), 0, 0);
  1858. graphics->blueToPlayersAdv(bg->bg, LOCPLINT->playerID);
  1859. pos = bg->center();
  1860. exit = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1861. //scenInfo = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 24, "ADVINFO.DEF",SDLK_i);
  1862. scenInfo = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 198, "ADVINFO.DEF",SDLK_i);
  1863. scenInfo->callback += CAdventureOptions::showScenarioInfo;
  1864. //viewWorld = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1865. puzzle = new AdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 81, "ADVPUZ.DEF");;
  1866. puzzle->callback += CAdventureOptions::showPuzzleMap;
  1867. }
  1868. CAdventureOptions::~CAdventureOptions()
  1869. {
  1870. }
  1871. void CAdventureOptions::showScenarioInfo()
  1872. {
  1873. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1874. }
  1875. void CAdventureOptions::showPuzzleMap()
  1876. {
  1877. GH.pushInt(new CPuzzleWindow());
  1878. }