CAdvmapInterface.cpp 50 KB

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