CAdvmapInterface.cpp 55 KB

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