CAdvmapInterface.cpp 55 KB

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