2
0

CAdvmapInterface.cpp 54 KB

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