CAdvmapInterface.cpp 55 KB

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