CAdvmapInterface.cpp 50 KB

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