CAdvmapInterface.cpp 54 KB

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