CAdvmapInterface.cpp 55 KB

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