CAdvmapInterface.cpp 55 KB

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