CAdvmapInterface.cpp 55 KB

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