CAdvmapInterface.cpp 56 KB

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