CAdvmapInterface.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. #include "StdInc.h"
  2. #include "CAdvmapInterface.h"
  3. #include "../CCallback.h"
  4. #include "CCastleInterface.h"
  5. #include "UIFramework/CCursorHandler.h"
  6. #include "CGameInfo.h"
  7. #include "CHeroWindow.h"
  8. #include "CKingdomInterface.h"
  9. #include "CMessage.h"
  10. #include "CPlayerInterface.h"
  11. #include "UIFramework/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 "../lib/JsonNode.h"
  23. #include "mapHandler.h"
  24. #include "CPreGame.h"
  25. #include "../lib/VCMI_Lib.h"
  26. #include "../lib/CSpellHandler.h"
  27. #include "CSoundBase.h"
  28. #include "../lib/CGameState.h"
  29. #include "CMusicHandler.h"
  30. #include "UIFramework/CGuiHandler.h"
  31. #include "UIFramework/CIntObjectClasses.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, Rect(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 Rect &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 Rect &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 Rect &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. Rect 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. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  573. Rect 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. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  579. Rect 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. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  585. Rect 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. Rect 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. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  599. Rect 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. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  605. Rect 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. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  611. Rect 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,Colors::Cornsilk,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,Colors::Cornsilk,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 = pom = -1;
  725. mode = NOTHING;
  726. pos.x=ADVOPT.infoboxX;
  727. pos.y=ADVOPT.infoboxY;
  728. pos.w=194;
  729. pos.h=186;
  730. day = CDefHandler::giveDef("NEWDAY.DEF");
  731. week1 = CDefHandler::giveDef("NEWWEEK1.DEF");
  732. week2 = CDefHandler::giveDef("NEWWEEK2.DEF");
  733. week3 = CDefHandler::giveDef("NEWWEEK3.DEF");
  734. week4 = CDefHandler::giveDef("NEWWEEK4.DEF");
  735. hourglass = CDefHandler::giveDef("HOURGLAS.DEF");
  736. hourglassSand = CDefHandler::giveDef("HOURSAND.DEF");
  737. selInfoWin = NULL;
  738. }
  739. CInfoBar::~CInfoBar()
  740. {
  741. delete day;
  742. delete week1;
  743. delete week2;
  744. delete week3;
  745. delete week4;
  746. if(selInfoWin)
  747. SDL_FreeSurface(selInfoWin);
  748. }
  749. void CInfoBar::showAll(SDL_Surface * to)
  750. {
  751. if (mode >= NEW_DAY && mode <= NEW_WEEK4)
  752. {
  753. blitAnim(mode);
  754. }
  755. else if(mode == ENEMY_TURN)
  756. {
  757. CPicture bg("ADSTATOT.bmp");
  758. bg.convertToScreenBPP();
  759. blitAt(graphics->flags->ourImages[enemyTurnInfo.color].bitmap, 20, 51, bg);
  760. int hourglassFrame = enemyTurnInfo.progress * hourglass->ourImages.size();
  761. static int sandFrame = 0;
  762. vstd::amin(hourglassFrame, hourglass->ourImages.size()-1);
  763. blitAt(hourglassSand->ourImages[sandFrame++ % hourglassSand->ourImages.size()].bitmap, 99, 51, bg);
  764. blitAt(hourglass->ourImages[hourglassFrame].bitmap, 99, 51, bg);
  765. blitAtLoc(bg, 8, 11, to);
  766. }
  767. else if(selInfoWin)
  768. {
  769. blitAt(selInfoWin, pos.x, pos.y, to);
  770. }
  771. }
  772. CDefHandler * CInfoBar::getAnim(EMode mode)
  773. {
  774. switch(mode)
  775. {
  776. case NEW_DAY:
  777. return day;
  778. case NEW_WEEK1:
  779. return week1;
  780. case NEW_WEEK2:
  781. return week2;
  782. case NEW_WEEK3:
  783. return week3;
  784. case NEW_WEEK4:
  785. return week4;
  786. default:
  787. return NULL;
  788. }
  789. }
  790. void CInfoBar::blitAnim(EMode mode)//0 - day, 1 - week
  791. {
  792. CDefHandler * anim = NULL;
  793. std::ostringstream txt;
  794. anim = getAnim(mode);
  795. if(mode > NEW_DAY) //new week animation
  796. {
  797. txt << CGI->generaltexth->allTexts[63] << " " << LOCPLINT->cb->getDate(2);
  798. }
  799. else if(mode == NEW_DAY) //new day
  800. {
  801. txt << CGI->generaltexth->allTexts[64] << " " << LOCPLINT->cb->getDate(1);
  802. }
  803. blitAt(anim->ourImages[pom].bitmap,pos.x+9,pos.y+10);
  804. printAtMiddle(txt.str(),pos.x+95,pos.y+31,FONT_MEDIUM,Colors::Cornsilk);
  805. if (pom == anim->ourImages.size()-1)
  806. toNextTick+=750;
  807. }
  808. void CInfoBar::newDay(int Day)
  809. {
  810. if(LOCPLINT->cb->getDate(1) != 1)
  811. {
  812. mode = NEW_DAY; //showing day
  813. }
  814. else
  815. {
  816. switch(LOCPLINT->cb->getDate(2))
  817. {
  818. case 1:
  819. mode = NEW_WEEK1;
  820. break;
  821. case 2:
  822. mode = NEW_WEEK2;
  823. break;
  824. case 3:
  825. mode = NEW_WEEK3;
  826. break;
  827. case 4:
  828. mode = NEW_WEEK4;
  829. break;
  830. default:
  831. mode = NOTHING;
  832. break;
  833. }
  834. }
  835. pom = 0;
  836. if(!(active & TIME))
  837. activateTimer();
  838. toNextTick = 500;
  839. blitAnim(mode);
  840. }
  841. void CInfoBar::showComp(CComponent * comp, int time)
  842. {
  843. if(comp->type != CComponent::hero)
  844. {
  845. curSel = NULL;
  846. }
  847. SDL_Surface * b = BitmapHandler::loadBitmap("ADSTATOT.bmp");
  848. blitAt(b,pos.x+8,pos.y+11);
  849. blitAt(comp->getImg(),pos.x+52,pos.y+54);
  850. printAtMiddle(comp->subtitle,pos.x+91,pos.y+158,FONT_SMALL,Colors::Cornsilk);
  851. printAtMiddleWB(comp->description,pos.x+94,pos.y+31,FONT_SMALL,26,Colors::Cornsilk);
  852. SDL_FreeSurface(b);
  853. if(!(active & TIME))
  854. activateTimer();
  855. mode = SHOW_COMPONENT;
  856. toNextTick = time;
  857. }
  858. void CInfoBar::tick()
  859. {
  860. if(mode >= NEW_DAY && mode <= NEW_WEEK4) //animation
  861. {
  862. pom++;
  863. if (pom >= getAnim(mode)->ourImages.size())
  864. {
  865. deactivateTimer();
  866. toNextTick = -1;
  867. mode = NOTHING;
  868. }
  869. toNextTick = 150;
  870. }
  871. else if(mode == SHOW_COMPONENT)
  872. {
  873. deactivateTimer();
  874. toNextTick = -1;
  875. mode = NOTHING;
  876. }
  877. redraw();
  878. }
  879. void CInfoBar::show(SDL_Surface * to)
  880. {
  881. }
  882. void CInfoBar::activate()
  883. {
  884. //CIntObject::activate();
  885. }
  886. void CInfoBar::deactivate()
  887. {
  888. //CIntObject::deactivate();
  889. if(active & TIME)
  890. deactivateTimer();
  891. toNextTick = -1;
  892. mode = NOTHING;
  893. }
  894. void CInfoBar::updateSelection(const CGObjectInstance *obj)
  895. {
  896. if(obj->ID == GameConstants::HEROI_TYPE)
  897. curSel = static_cast<const CGHeroInstance*>(obj);
  898. else
  899. curSel = NULL;
  900. if(selInfoWin)
  901. SDL_FreeSurface(selInfoWin);
  902. selInfoWin = LOCPLINT->infoWin(obj);
  903. }
  904. void CInfoBar::enemyTurn(ui8 color, double progress)
  905. {
  906. mode = ENEMY_TURN;
  907. enemyTurnInfo.color = color;
  908. enemyTurnInfo.progress = progress;
  909. redraw();
  910. if(!(active & TIME))
  911. activateTimer();
  912. toNextTick = 250;
  913. }
  914. CAdvMapInt::CAdvMapInt()
  915. :statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  916. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  917. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  918. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  919. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  920. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  921. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  922. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  923. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  924. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  925. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  926. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  927. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  928. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  929. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  930. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  931. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  932. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  933. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  934. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  935. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  936. heroList(ADVOPT.hlistSize),
  937. townList(ADVOPT.tlistSize,ADVOPT.tlistX,ADVOPT.tlistY,ADVOPT.tlistAU,ADVOPT.tlistAD)//(5,&genRect(192,48,747,196),747,196,747,372),
  938. {
  939. state = NA;
  940. spellBeingCasted = NULL;
  941. pos.x = pos.y = 0;
  942. pos.w = screen->w;
  943. pos.h = screen->h;
  944. selection = NULL;
  945. townList.fun = boost::bind(&CAdvMapInt::selectionChanged,this);
  946. adventureInt=this;
  947. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  948. scrollingDir = 0;
  949. updateScreen = false;
  950. anim=0;
  951. animValHitCount=0; //animation frame
  952. heroAnim=0;
  953. heroAnimValHitCount=0; // hero animation frame
  954. heroList.init();
  955. heroList.genList();
  956. //townList.init();
  957. //townList.genList();
  958. for (int g=0; g<ADVOPT.gemG.size(); ++g)
  959. {
  960. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[g]));
  961. }
  962. setPlayer(LOCPLINT->playerID);
  963. }
  964. CAdvMapInt::~CAdvMapInt()
  965. {
  966. SDL_FreeSurface(bg);
  967. for(int i=0; i<gems.size(); i++)
  968. delete gems[i];
  969. }
  970. void CAdvMapInt::fshowOverview()
  971. {
  972. GH.pushInt(new CKingdomInterface);
  973. }
  974. void CAdvMapInt::fswitchLevel()
  975. {
  976. if(!CGI->mh->map->twoLevel)
  977. return;
  978. if (position.z)
  979. {
  980. position.z--;
  981. underground.setIndex(0,true);
  982. underground.showAll(screenBuf);
  983. }
  984. else
  985. {
  986. underground.setIndex(1,true);
  987. position.z++;
  988. underground.showAll(screenBuf);
  989. }
  990. updateScreen = true;
  991. minimap.draw(screenBuf);
  992. }
  993. void CAdvMapInt::fshowQuestlog()
  994. {
  995. }
  996. void CAdvMapInt::fsleepWake()
  997. {
  998. const CGHeroInstance *h = curHero();
  999. if (!h)
  1000. return;
  1001. bool newSleep = !isHeroSleeping(h);
  1002. setHeroSleeping(h, newSleep);
  1003. updateSleepWake(h);
  1004. if (newSleep)
  1005. {
  1006. fnextHero();
  1007. //moveHero.block(true);
  1008. //uncomment to enable original HoMM3 behaviour:
  1009. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  1010. }
  1011. }
  1012. void CAdvMapInt::fmoveHero()
  1013. {
  1014. const CGHeroInstance *h = curHero();
  1015. if (!h || !terrain.currentPath)
  1016. return;
  1017. LOCPLINT->moveHero(h, *terrain.currentPath);
  1018. }
  1019. void CAdvMapInt::fshowSpellbok()
  1020. {
  1021. if (!curHero()) //checking necessary values
  1022. return;
  1023. centerOn(selection);
  1024. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), curHero(), LOCPLINT, false);
  1025. GH.pushInt(spellWindow);
  1026. }
  1027. void CAdvMapInt::fadventureOPtions()
  1028. {
  1029. GH.pushInt(new CAdventureOptions);
  1030. }
  1031. void CAdvMapInt::fsystemOptions()
  1032. {
  1033. CSystemOptionsWindow * sysopWindow = new CSystemOptionsWindow(Rect::createCentered(487, 481), LOCPLINT);
  1034. GH.pushInt(sysopWindow);
  1035. }
  1036. void CAdvMapInt::fnextHero()
  1037. {
  1038. int next = getNextHeroIndex(heroList.selected);
  1039. if (next < 0)
  1040. return;
  1041. heroList.select(next);
  1042. }
  1043. void CAdvMapInt::fendTurn()
  1044. {
  1045. if(!LOCPLINT->makingTurn)
  1046. return;
  1047. if ( settings["adventure"]["heroReminder"].Bool())
  1048. {
  1049. for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)
  1050. if (!isHeroSleeping(LOCPLINT->wanderingHeroes[i]) && (LOCPLINT->wanderingHeroes[i]->movement > 0))
  1051. {
  1052. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], std::vector<CComponent*>(), boost::bind(&CAdvMapInt::endingTurn, this), 0, false);
  1053. return;
  1054. }
  1055. }
  1056. endingTurn();
  1057. }
  1058. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  1059. {
  1060. sleepWake.block(!h);
  1061. if (!h)
  1062. return;
  1063. bool state = isHeroSleeping(h);
  1064. sleepWake.setIndex(state ? 1 : 0, true);
  1065. sleepWake.assignedKeys.clear();
  1066. sleepWake.assignedKeys.insert(state ? SDLK_w : SDLK_z);
  1067. sleepWake.update();
  1068. }
  1069. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  1070. {
  1071. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  1072. if (hasPath == tribool::indeterminate_value)
  1073. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  1074. if (!h)
  1075. {
  1076. moveHero.block(true);
  1077. return;
  1078. }
  1079. moveHero.block(!hasPath || (h->movement == 0));
  1080. }
  1081. int CAdvMapInt::getNextHeroIndex(int startIndex)
  1082. {
  1083. if (LOCPLINT->wanderingHeroes.size() == 0)
  1084. return -1;
  1085. if (startIndex < 0)
  1086. startIndex = 0;
  1087. int i = startIndex;
  1088. do
  1089. {
  1090. i++;
  1091. if (i >= LOCPLINT->wanderingHeroes.size())
  1092. i = 0;
  1093. }
  1094. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  1095. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  1096. return i;
  1097. else
  1098. return -1;
  1099. }
  1100. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  1101. {
  1102. int start = heroList.getPosOfHero(h);
  1103. int next = getNextHeroIndex(start);
  1104. if (next < 0)
  1105. {
  1106. nextHero.block(true);
  1107. return;
  1108. }
  1109. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  1110. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  1111. nextHero.block(noActiveHeroes);
  1112. }
  1113. void CAdvMapInt::activate()
  1114. {
  1115. if(isActive())
  1116. {
  1117. tlog1 << "Error: advmapint already active...\n";
  1118. return;
  1119. }
  1120. screenBuf = screen;
  1121. GH.statusbar = &statusbar;
  1122. activateMouseMove();
  1123. kingOverview.activate();
  1124. underground.activate();
  1125. questlog.activate();
  1126. sleepWake.activate();
  1127. moveHero.activate();
  1128. spellbook.activate();
  1129. sysOptions.activate();
  1130. advOptions.activate();
  1131. nextHero.activate();
  1132. endTurn.activate();
  1133. minimap.activate();
  1134. heroList.activate();
  1135. townList.activate();
  1136. terrain.activate();
  1137. infoBar.activate();
  1138. if(!LOCPLINT->cingconsole->active)
  1139. LOCPLINT->cingconsole->activate();
  1140. GH.fakeMouseMove(); //to restore the cursor
  1141. }
  1142. void CAdvMapInt::deactivate()
  1143. {
  1144. deactivateMouseMove();
  1145. scrollingDir = 0;
  1146. CCS->curh->changeGraphic(0,0);
  1147. kingOverview.deactivate();
  1148. underground.deactivate();
  1149. questlog.deactivate();
  1150. sleepWake.deactivate();
  1151. moveHero.deactivate();
  1152. spellbook.deactivate();
  1153. advOptions.deactivate();
  1154. sysOptions.deactivate();
  1155. nextHero.deactivate();
  1156. endTurn.deactivate();
  1157. minimap.deactivate();
  1158. heroList.deactivate();
  1159. townList.deactivate();
  1160. terrain.deactivate();
  1161. infoBar.deactivate();
  1162. if(LOCPLINT->cingconsole->active) //TODO
  1163. LOCPLINT->cingconsole->deactivate();
  1164. }
  1165. void CAdvMapInt::showAll(SDL_Surface * to)
  1166. {
  1167. blitAt(bg,0,0,to);
  1168. if(state != INGAME)
  1169. return;
  1170. kingOverview.showAll(to);
  1171. underground.showAll(to);
  1172. questlog.showAll(to);
  1173. sleepWake.showAll(to);
  1174. moveHero.showAll(to);
  1175. spellbook.showAll(to);
  1176. advOptions.showAll(to);
  1177. sysOptions.showAll(to);
  1178. nextHero.showAll(to);
  1179. endTurn.showAll(to);
  1180. minimap.draw(to);
  1181. heroList.draw(to);
  1182. townList.draw(to);
  1183. updateScreen = true;
  1184. show(to);
  1185. resdatabar.draw(to);
  1186. statusbar.show(to);
  1187. infoBar.showAll(to);
  1188. LOCPLINT->cingconsole->show(to);
  1189. }
  1190. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  1191. {
  1192. if (!hero)
  1193. return false;
  1194. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  1195. }
  1196. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  1197. {
  1198. if (sleep)
  1199. LOCPLINT->sleepingHeroes += hero;
  1200. else
  1201. LOCPLINT->sleepingHeroes -= hero;
  1202. updateNextHero(NULL);
  1203. }
  1204. void CAdvMapInt::show(SDL_Surface * to)
  1205. {
  1206. if(state != INGAME)
  1207. return;
  1208. ++animValHitCount; //for animations
  1209. if(animValHitCount == 8)
  1210. {
  1211. CGI->mh->updateWater();
  1212. animValHitCount = 0;
  1213. ++anim;
  1214. updateScreen = true;
  1215. }
  1216. ++heroAnim;
  1217. int scrollSpeed = settings["adventure"]["scrollSpeed"].Float();
  1218. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  1219. if((animValHitCount % (4/scrollSpeed)) == 0
  1220. && (
  1221. (GH.topInt() == this)
  1222. || SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1223. || SDL_GetKeyState(NULL)[SDLK_RCTRL]
  1224. )
  1225. )
  1226. {
  1227. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  1228. position.x--;
  1229. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  1230. position.x++;
  1231. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  1232. position.y--;
  1233. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  1234. position.y++;
  1235. if(scrollingDir)
  1236. {
  1237. updateScreen = true;
  1238. updateMinimap=true;
  1239. }
  1240. }
  1241. if(updateScreen)
  1242. {
  1243. terrain.show(to);
  1244. for(int i=0;i<4;i++)
  1245. blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  1246. updateScreen=false;
  1247. LOCPLINT->cingconsole->show(to);
  1248. }
  1249. if (updateMinimap)
  1250. {
  1251. minimap.draw(to);
  1252. updateMinimap=false;
  1253. }
  1254. }
  1255. void CAdvMapInt::selectionChanged()
  1256. {
  1257. const CGTownInstance *to = LOCPLINT->towns[townList.selected];
  1258. select(to);
  1259. }
  1260. void CAdvMapInt::centerOn(int3 on)
  1261. {
  1262. on.x -= CGI->mh->frameW;
  1263. on.y -= CGI->mh->frameH;
  1264. on = LOCPLINT->repairScreenPos(on);
  1265. adventureInt->position = on;
  1266. adventureInt->updateScreen=true;
  1267. updateMinimap=true;
  1268. underground.setIndex(on.z,true); //change underground switch button image
  1269. if(GH.topInt() == this)
  1270. underground.redraw();
  1271. }
  1272. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  1273. {
  1274. centerOn(obj->getSightCenter());
  1275. }
  1276. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1277. {
  1278. ui8 Dir = 0;
  1279. int k = key.keysym.sym;
  1280. const CGHeroInstance *h = curHero(); //selected hero
  1281. const CGTownInstance *t = curTown(); //selected town
  1282. switch(k)
  1283. {
  1284. case SDLK_i:
  1285. if(isActive())
  1286. CAdventureOptions::showScenarioInfo();
  1287. return;
  1288. case SDLK_s:
  1289. if(isActive())
  1290. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  1291. return;
  1292. case SDLK_d:
  1293. {
  1294. if(h && isActive() && key.state == SDL_PRESSED)
  1295. LOCPLINT->tryDiggging(h);
  1296. return;
  1297. }
  1298. case SDLK_p:
  1299. if(isActive())
  1300. LOCPLINT->showPuzzleMap();
  1301. return;
  1302. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1303. {
  1304. if(!isActive())
  1305. return;
  1306. if(h && key.state == SDL_PRESSED)
  1307. {
  1308. LOCPLINT->pim->unlock();
  1309. LOCPLINT->cb->moveHero(h,h->pos);
  1310. LOCPLINT->pim->lock();
  1311. }
  1312. }
  1313. return;
  1314. case SDLK_RETURN:
  1315. {
  1316. if(!isActive() || !selection || key.state != SDL_PRESSED)
  1317. return;
  1318. if(h)
  1319. LOCPLINT->openHeroWindow(h);
  1320. else if(t)
  1321. LOCPLINT->openTownWindow(t);
  1322. return;
  1323. }
  1324. case SDLK_ESCAPE:
  1325. {
  1326. if(isActive() || GH.topInt() != this || !spellBeingCasted || key.state != SDL_PRESSED)
  1327. return;
  1328. leaveCastingMode();
  1329. return;
  1330. }
  1331. case SDLK_t:
  1332. {
  1333. //act on key down if marketplace windows is not already opened
  1334. if(key.state != SDL_PRESSED || GH.topInt()->type & BLOCK_ADV_HOTKEYS) return;
  1335. //check if we have any marketplace
  1336. const CGTownInstance *townWithMarket = NULL;
  1337. BOOST_FOREACH(const CGTownInstance *t, LOCPLINT->cb->getTownsInfo())
  1338. {
  1339. if(vstd::contains(t->builtBuildings, 14))
  1340. {
  1341. townWithMarket = t;
  1342. break;
  1343. }
  1344. }
  1345. if(townWithMarket) //if any town has marketplace, open window
  1346. GH.pushInt(new CMarketplaceWindow(townWithMarket));
  1347. else //if not - complain
  1348. LOCPLINT->showInfoDialog("No available marketplace!", std::vector<CComponent*>(), soundBase::sound_todo);
  1349. return;
  1350. }
  1351. default:
  1352. {
  1353. static const int3 directions[] = { int3(-1, +1, 0), int3(0, +1, 0), int3(+1, +1, 0),
  1354. int3(-1, 0, 0), int3(0, 0, 0), int3(+1, 0, 0),
  1355. int3(-1, -1, 0), int3(0, -1, 0), int3(+1, -1, 0) };
  1356. //numpad arrow
  1357. if(CGuiHandler::isArrowKey(SDLKey(k)))
  1358. {
  1359. switch(k)
  1360. {
  1361. case SDLK_UP:
  1362. Dir = UP;
  1363. break;
  1364. case SDLK_LEFT:
  1365. Dir = LEFT;
  1366. break;
  1367. case SDLK_RIGHT:
  1368. Dir = RIGHT;
  1369. break;
  1370. case SDLK_DOWN:
  1371. Dir = DOWN;
  1372. break;
  1373. }
  1374. k = CGuiHandler::arrowToNum(SDLKey(k));
  1375. }
  1376. if(!isActive() || LOCPLINT->ctrlPressed())//ctrl makes arrow move screen, not hero
  1377. break;
  1378. k -= SDLK_KP0 + 1;
  1379. if(k < 0 || k > 8 || key.state != SDL_PRESSED)
  1380. return;
  1381. if(!h)
  1382. break;
  1383. if(k == 4)
  1384. {
  1385. centerOn(h);
  1386. return;
  1387. }
  1388. int3 dir = directions[k];
  1389. CGPath &path = LOCPLINT->paths[h];
  1390. terrain.currentPath = &path;
  1391. if(!LOCPLINT->cb->getPath2(h->getPosition(false) + dir, path))
  1392. {
  1393. terrain.currentPath = NULL;
  1394. return;
  1395. }
  1396. if(!path.nodes[0].turns)
  1397. {
  1398. LOCPLINT->moveHero(h, path);
  1399. }
  1400. }
  1401. return;
  1402. }
  1403. if(Dir && key.state == SDL_PRESSED //arrow is pressed
  1404. && LOCPLINT->ctrlPressed()
  1405. )
  1406. scrollingDir |= Dir;
  1407. else
  1408. scrollingDir &= ~Dir;
  1409. }
  1410. void CAdvMapInt::handleRightClick(std::string text, tribool down)
  1411. {
  1412. if(down)
  1413. {
  1414. CRClickPopup::createAndPush(text);
  1415. }
  1416. }
  1417. int3 CAdvMapInt::verifyPos(int3 ver)
  1418. {
  1419. if (ver.x<0)
  1420. ver.x=0;
  1421. if (ver.y<0)
  1422. ver.y=0;
  1423. if (ver.z<0)
  1424. ver.z=0;
  1425. if (ver.x>=CGI->mh->sizes.x)
  1426. ver.x=CGI->mh->sizes.x-1;
  1427. if (ver.y>=CGI->mh->sizes.y)
  1428. ver.y=CGI->mh->sizes.y-1;
  1429. if (ver.z>=CGI->mh->sizes.z)
  1430. ver.z=CGI->mh->sizes.z-1;
  1431. return ver;
  1432. }
  1433. void CAdvMapInt::select(const CArmedInstance *sel, bool centerView /*= true*/)
  1434. {
  1435. assert(sel);
  1436. LOCPLINT->cb->setSelection(sel);
  1437. selection = sel;
  1438. if (LOCPLINT->battleInt == NULL)
  1439. CCS->musich->playMusic(CCS->musich->terrainMusics[LOCPLINT->cb->getTile(sel->visitablePos())->tertype]);
  1440. if(centerView)
  1441. centerOn(sel);
  1442. terrain.currentPath = NULL;
  1443. if(sel->ID==GameConstants::TOWNI_TYPE)
  1444. {
  1445. int pos = vstd::find_pos(LOCPLINT->towns,sel);
  1446. townList.selected = pos;
  1447. townList.fixPos();
  1448. updateSleepWake(NULL);
  1449. updateMoveHero(NULL);
  1450. }
  1451. else //hero selected
  1452. {
  1453. const CGHeroInstance *h = static_cast<const CGHeroInstance*>(sel);
  1454. if(LOCPLINT->getWHero(heroList.selected) != h)
  1455. {
  1456. heroList.selected = heroList.getPosOfHero(h);
  1457. heroList.fixPos();
  1458. }
  1459. terrain.currentPath = LOCPLINT->getAndVerifyPath(h);
  1460. updateSleepWake(h);
  1461. updateMoveHero(h);
  1462. }
  1463. townList.draw(screen);
  1464. heroList.draw(screen);
  1465. infoBar.updateSelection(sel);
  1466. infoBar.showAll(screen);
  1467. }
  1468. void CAdvMapInt::mouseMoved( const SDL_MouseMotionEvent & sEvent )
  1469. {
  1470. //adventure map scrolling with mouse
  1471. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL] && isActive())
  1472. {
  1473. if(sEvent.x<15)
  1474. {
  1475. scrollingDir |= LEFT;
  1476. }
  1477. else
  1478. {
  1479. scrollingDir &= ~LEFT;
  1480. }
  1481. if(sEvent.x>screen->w-15)
  1482. {
  1483. scrollingDir |= RIGHT;
  1484. }
  1485. else
  1486. {
  1487. scrollingDir &= ~RIGHT;
  1488. }
  1489. if(sEvent.y<15)
  1490. {
  1491. scrollingDir |= UP;
  1492. }
  1493. else
  1494. {
  1495. scrollingDir &= ~UP;
  1496. }
  1497. if(sEvent.y>screen->h-15)
  1498. {
  1499. scrollingDir |= DOWN;
  1500. }
  1501. else
  1502. {
  1503. scrollingDir &= ~DOWN;
  1504. }
  1505. }
  1506. }
  1507. bool CAdvMapInt::isActive()
  1508. {
  1509. return active & ~CIntObject::KEYBOARD;
  1510. }
  1511. void CAdvMapInt::startHotSeatWait(int Player)
  1512. {
  1513. state = WAITING;
  1514. }
  1515. void CAdvMapInt::setPlayer(int Player)
  1516. {
  1517. player = Player;
  1518. graphics->blueToPlayersAdv(bg,player);
  1519. kingOverview.setPlayerColor(player);
  1520. underground.setPlayerColor(player);
  1521. questlog.setPlayerColor(player);
  1522. sleepWake.setPlayerColor(player);
  1523. moveHero.setPlayerColor(player);
  1524. spellbook.setPlayerColor(player);
  1525. sysOptions.setPlayerColor(player);
  1526. advOptions.setPlayerColor(player);
  1527. nextHero.setPlayerColor(player);
  1528. endTurn.setPlayerColor(player);
  1529. graphics->blueToPlayersAdv(resdatabar.bg,player);
  1530. //heroList.updateHList();
  1531. //townList.genList();
  1532. }
  1533. void CAdvMapInt::startTurn()
  1534. {
  1535. state = INGAME;
  1536. }
  1537. void CAdvMapInt::endingTurn()
  1538. {
  1539. if(LOCPLINT->cingconsole->active)
  1540. LOCPLINT->cingconsole->deactivate();
  1541. LOCPLINT->makingTurn = false;
  1542. LOCPLINT->cb->endTurn();
  1543. }
  1544. void CAdvMapInt::tileLClicked(const int3 &mp)
  1545. {
  1546. if(!LOCPLINT->cb->isVisible(mp) || !LOCPLINT->makingTurn)
  1547. return;
  1548. std::vector < const CGObjectInstance * > bobjs = LOCPLINT->cb->getBlockingObjs(mp), //blocking objects at tile
  1549. vobjs = LOCPLINT->cb->getVisitableObjs(mp); //visitable objects
  1550. const TerrainTile *tile = LOCPLINT->cb->getTile(mp);
  1551. const CGObjectInstance *topBlocking = bobjs.size() ? bobjs.back() : NULL;
  1552. int3 selPos = selection->getSightCenter();
  1553. if(spellBeingCasted && isInScreenRange(selPos, mp))
  1554. {
  1555. const TerrainTile *heroTile = LOCPLINT->cb->getTile(selPos);
  1556. switch(spellBeingCasted->id)
  1557. {
  1558. case Spells::SCUTTLE_BOAT: //Scuttle Boat
  1559. if(topBlocking && topBlocking->ID == 8)
  1560. leaveCastingMode(true, mp);
  1561. break;
  1562. case Spells::DIMENSION_DOOR:
  1563. if(!tile || tile->isClear(heroTile))
  1564. leaveCastingMode(true, mp);
  1565. break;
  1566. }
  1567. return;
  1568. }
  1569. //check if we can select this object
  1570. bool canSelect = topBlocking && topBlocking->ID == GameConstants::HEROI_TYPE && topBlocking->tempOwner == LOCPLINT->playerID;
  1571. canSelect |= topBlocking && topBlocking->ID == GameConstants::TOWNI_TYPE && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, topBlocking->tempOwner);
  1572. if (selection->ID != GameConstants::HEROI_TYPE) //hero is not selected (presumably town)
  1573. {
  1574. assert(!terrain.currentPath); //path can be active only when hero is selected
  1575. if(selection == topBlocking) //selected town clicked
  1576. LOCPLINT->openTownWindow(static_cast<const CGTownInstance*>(topBlocking));
  1577. else if ( canSelect )
  1578. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1579. return;
  1580. }
  1581. else if(const CGHeroInstance * currentHero = curHero()) //hero is selected
  1582. {
  1583. const CGPathNode *pn = LOCPLINT->cb->getPathInfo(mp);
  1584. if(currentHero == topBlocking) //clicked selected hero
  1585. {
  1586. LOCPLINT->openHeroWindow(currentHero);
  1587. return;
  1588. }
  1589. else if(canSelect && pn->turns == 255 ) //selectable object at inaccessible tile
  1590. {
  1591. select(static_cast<const CArmedInstance*>(topBlocking), false);
  1592. return;
  1593. }
  1594. else //still here? we need to move hero if we clicked end of already selected path or calculate a new path otherwise
  1595. {
  1596. if (terrain.currentPath && terrain.currentPath->endPos() == mp)//we'll be moving
  1597. {
  1598. LOCPLINT->moveHero(currentHero,*terrain.currentPath);
  1599. return;
  1600. }
  1601. 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
  1602. {
  1603. CGPath &path = LOCPLINT->paths[currentHero];
  1604. terrain.currentPath = &path;
  1605. bool gotPath = LOCPLINT->cb->getPath2(mp, path); //try getting path, erase if failed
  1606. updateMoveHero(currentHero);
  1607. if (!gotPath)
  1608. LOCPLINT->eraseCurrentPathOf(currentHero);
  1609. else
  1610. return;
  1611. }
  1612. }
  1613. } //end of hero is selected "case"
  1614. else
  1615. {
  1616. throw std::string("Nothing is selected...");
  1617. }
  1618. if(const IShipyard *shipyard = ourInaccessibleShipyard(topBlocking))
  1619. {
  1620. LOCPLINT->showShipyardDialogOrProblemPopup(shipyard);
  1621. }
  1622. }
  1623. void CAdvMapInt::tileHovered(const int3 &tile)
  1624. {
  1625. if(!LOCPLINT->cb->isVisible(tile))
  1626. {
  1627. CCS->curh->changeGraphic(0, 0);
  1628. statusbar.clear();
  1629. return;
  1630. }
  1631. std::vector<std::string> temp = LOCPLINT->cb->getObjDescriptions(tile);
  1632. if (temp.size())
  1633. {
  1634. boost::replace_all(temp.back(),"\n"," ");
  1635. statusbar.print(temp.back());
  1636. }
  1637. else
  1638. {
  1639. std::string hlp;
  1640. CGI->mh->getTerrainDescr(tile, hlp, false);
  1641. statusbar.print(hlp);
  1642. }
  1643. const CGPathNode *pnode = LOCPLINT->cb->getPathInfo(tile);
  1644. std::vector<const CGObjectInstance *> objs = LOCPLINT->cb->getBlockingObjs(tile);
  1645. const CGObjectInstance *objAtTile = objs.size() ? objs.back() : NULL;
  1646. bool accessible = pnode->turns < 255;
  1647. int turns = pnode->turns;
  1648. vstd::amin(turns, 3);
  1649. if(!selection) //may occur just at the start of game (fake move before full intiialization)
  1650. return;
  1651. if(spellBeingCasted)
  1652. {
  1653. switch(spellBeingCasted->id)
  1654. {
  1655. case Spells::SCUTTLE_BOAT:
  1656. if(objAtTile && objAtTile->ID == 8)
  1657. CCS->curh->changeGraphic(0, 42);
  1658. else
  1659. CCS->curh->changeGraphic(0, 0);
  1660. return;
  1661. case Spells::DIMENSION_DOOR:
  1662. {
  1663. const TerrainTile *t = LOCPLINT->cb->getTile(tile, false);
  1664. int3 hpos = selection->getSightCenter();
  1665. if((!t || t->isClear(LOCPLINT->cb->getTile(hpos))) && isInScreenRange(hpos, tile))
  1666. CCS->curh->changeGraphic(0, 41);
  1667. else
  1668. CCS->curh->changeGraphic(0, 0);
  1669. return;
  1670. }
  1671. }
  1672. }
  1673. const bool guardingCreature = CGI->mh->map->isInTheMap(LOCPLINT->cb->guardingCreaturePosition(tile));
  1674. if(selection->ID == GameConstants::TOWNI_TYPE)
  1675. {
  1676. if(objAtTile)
  1677. {
  1678. if(objAtTile->ID == GameConstants::TOWNI_TYPE && LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner))
  1679. CCS->curh->changeGraphic(0, 3);
  1680. else if(objAtTile->ID == GameConstants::HEROI_TYPE && objAtTile->tempOwner == LOCPLINT->playerID)
  1681. CCS->curh->changeGraphic(0, 2);
  1682. }
  1683. else
  1684. CCS->curh->changeGraphic(0, 0);
  1685. }
  1686. else if(const CGHeroInstance *h = curHero())
  1687. {
  1688. if(objAtTile)
  1689. {
  1690. if(objAtTile->ID == GameConstants::HEROI_TYPE)
  1691. {
  1692. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy hero
  1693. {
  1694. if(accessible)
  1695. CCS->curh->changeGraphic(0, 5 + turns*6);
  1696. else
  1697. CCS->curh->changeGraphic(0, 0);
  1698. }
  1699. else //our or ally hero
  1700. {
  1701. if(selection == objAtTile)
  1702. CCS->curh->changeGraphic(0, 2);
  1703. else if(accessible)
  1704. CCS->curh->changeGraphic(0, 8 + turns*6);
  1705. else
  1706. CCS->curh->changeGraphic(0, 2);
  1707. }
  1708. }
  1709. else if(objAtTile->ID == GameConstants::TOWNI_TYPE)
  1710. {
  1711. if(!LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, objAtTile->tempOwner)) //enemy town
  1712. {
  1713. if(accessible)
  1714. {
  1715. const CGTownInstance* townObj = dynamic_cast<const CGTownInstance*>(objAtTile);
  1716. // Show movement cursor for unguarded enemy towns, otherwise attack cursor.
  1717. if (townObj && !townObj->armedGarrison())
  1718. CCS->curh->changeGraphic(0, 9 + turns*6);
  1719. else
  1720. CCS->curh->changeGraphic(0, 5 + turns*6);
  1721. }
  1722. else
  1723. {
  1724. CCS->curh->changeGraphic(0, 0);
  1725. }
  1726. }
  1727. else //our or ally town
  1728. {
  1729. if(accessible)
  1730. CCS->curh->changeGraphic(0, 9 + turns*6);
  1731. else
  1732. CCS->curh->changeGraphic(0, 3);
  1733. }
  1734. }
  1735. else if(objAtTile->ID == 8) //boat
  1736. {
  1737. if(accessible)
  1738. CCS->curh->changeGraphic(0, 6 + turns*6);
  1739. else
  1740. CCS->curh->changeGraphic(0, 0);
  1741. }
  1742. else if (objAtTile->ID == 33 || objAtTile->ID == 219) // Garrison
  1743. {
  1744. if (accessible)
  1745. {
  1746. const CGGarrison* garrObj = dynamic_cast<const CGGarrison*>(objAtTile); //TODO evil evil cast!
  1747. // Show battle cursor for guarded enemy garrisons, otherwise movement cursor.
  1748. if (garrObj && garrObj->stacksCount()
  1749. && !LOCPLINT->cb->getPlayerRelations( LOCPLINT->playerID, garrObj->tempOwner) )
  1750. CCS->curh->changeGraphic(0, 5 + turns*6);
  1751. else
  1752. CCS->curh->changeGraphic(0, 9 + turns*6);
  1753. }
  1754. else
  1755. CCS->curh->changeGraphic(0, 0);
  1756. }
  1757. else if (guardingCreature && accessible) //(objAtTile->ID == 54) //monster
  1758. {
  1759. CCS->curh->changeGraphic(0, 5 + turns*6);
  1760. }
  1761. else
  1762. {
  1763. if(accessible)
  1764. {
  1765. if(pnode->land)
  1766. CCS->curh->changeGraphic(0, 9 + turns*6);
  1767. else
  1768. CCS->curh->changeGraphic(0, 28 + turns);
  1769. }
  1770. else
  1771. CCS->curh->changeGraphic(0, 0);
  1772. }
  1773. }
  1774. else //no objs
  1775. {
  1776. if(accessible/* && pnode->accessible != CGPathNode::FLYABLE*/)
  1777. {
  1778. if (guardingCreature)
  1779. {
  1780. CCS->curh->changeGraphic(0, 5 + turns*6);
  1781. } else
  1782. {
  1783. if(pnode->land)
  1784. {
  1785. if(LOCPLINT->cb->getTile(h->getPosition(false))->tertype != TerrainTile::water)
  1786. CCS->curh->changeGraphic(0, 4 + turns*6);
  1787. else
  1788. CCS->curh->changeGraphic(0, 7 + turns*6); //anchor
  1789. }
  1790. else
  1791. CCS->curh->changeGraphic(0, 6 + turns*6);
  1792. }
  1793. }
  1794. else
  1795. CCS->curh->changeGraphic(0, 0);
  1796. }
  1797. }
  1798. if(ourInaccessibleShipyard(objAtTile))
  1799. {
  1800. CCS->curh->changeGraphic(0, 6);
  1801. }
  1802. }
  1803. void CAdvMapInt::tileRClicked(const int3 &mp)
  1804. {
  1805. if(spellBeingCasted)
  1806. {
  1807. leaveCastingMode();
  1808. return;
  1809. }
  1810. if(!LOCPLINT->cb->isVisible(mp))
  1811. {
  1812. CRClickPopup::createAndPush(VLC->generaltexth->allTexts[61]); //Uncharted Territory
  1813. return;
  1814. }
  1815. std::vector < const CGObjectInstance * > objs = LOCPLINT->cb->getBlockingObjs(mp);
  1816. if(!objs.size())
  1817. {
  1818. // Bare or undiscovered terrain
  1819. const TerrainTile * tile = LOCPLINT->cb->getTile(mp);
  1820. if (tile)
  1821. {
  1822. std::string hlp;
  1823. CGI->mh->getTerrainDescr(mp, hlp, true);
  1824. CRClickPopup::createAndPush(hlp);
  1825. }
  1826. return;
  1827. }
  1828. const CGObjectInstance * obj = objs.back();
  1829. CRClickPopup::createAndPush(obj, GH.current->motion, CENTER);
  1830. }
  1831. void CAdvMapInt::enterCastingMode(const CSpell * sp)
  1832. {
  1833. using namespace Spells;
  1834. assert(sp->id == SCUTTLE_BOAT || sp->id == DIMENSION_DOOR);
  1835. spellBeingCasted = sp;
  1836. deactivate();
  1837. terrain.activate();
  1838. GH.fakeMouseMove();
  1839. }
  1840. void CAdvMapInt::leaveCastingMode(bool cast /*= false*/, int3 dest /*= int3(-1, -1, -1)*/)
  1841. {
  1842. assert(spellBeingCasted);
  1843. int id = spellBeingCasted->id;
  1844. spellBeingCasted = NULL;
  1845. terrain.deactivate();
  1846. activate();
  1847. if(cast)
  1848. LOCPLINT->cb->castSpell(curHero(), id, dest);
  1849. else
  1850. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[731]); //Spell cancelled
  1851. }
  1852. const CGHeroInstance * CAdvMapInt::curHero() const
  1853. {
  1854. if(selection && selection->ID == GameConstants::HEROI_TYPE)
  1855. return static_cast<const CGHeroInstance *>(selection);
  1856. else
  1857. return NULL;
  1858. }
  1859. const CGTownInstance * CAdvMapInt::curTown() const
  1860. {
  1861. if(selection && selection->ID == GameConstants::TOWNI_TYPE)
  1862. return static_cast<const CGTownInstance *>(selection);
  1863. else
  1864. return NULL;
  1865. }
  1866. const IShipyard * CAdvMapInt::ourInaccessibleShipyard(const CGObjectInstance *obj) const
  1867. {
  1868. const IShipyard *ret = IShipyard::castFrom(obj);
  1869. if(!ret || obj->tempOwner != player || CCS->curh->mode || (CCS->curh->number != 6 && CCS->curh->number != 0))
  1870. return NULL;
  1871. return ret;
  1872. }
  1873. CAdventureOptions::CAdventureOptions()
  1874. {
  1875. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1876. bg = new CPicture("ADVOPTS.bmp");
  1877. graphics->blueToPlayersAdv(bg->bg, LOCPLINT->playerID);
  1878. pos = bg->center();
  1879. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1880. exit->assignedKeys.insert(SDLK_ESCAPE);
  1881. //scenInfo = new CAdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 24, "ADVINFO.DEF",SDLK_i);
  1882. scenInfo = new CAdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 198, "ADVINFO.DEF",SDLK_i);
  1883. scenInfo->callback += CAdventureOptions::showScenarioInfo;
  1884. //viewWorld = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 204, 313, "IOK6432.DEF",SDLK_RETURN);
  1885. puzzle = new CAdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 81, "ADVPUZ.DEF");
  1886. puzzle->callback += boost::bind(&CPlayerInterface::showPuzzleMap, LOCPLINT);
  1887. dig = new CAdventureMapButton("","", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 24, 139, "ADVDIG.DEF");
  1888. if(const CGHeroInstance *h = adventureInt->curHero())
  1889. dig->callback += boost::bind(&CPlayerInterface::tryDiggging, LOCPLINT, h);
  1890. else
  1891. dig->block(true);
  1892. }
  1893. CAdventureOptions::~CAdventureOptions()
  1894. {
  1895. }
  1896. void CAdventureOptions::showScenarioInfo()
  1897. {
  1898. GH.pushInt(new CScenarioInfo(LOCPLINT->cb->getMapHeader(), LOCPLINT->cb->getStartInfo()));
  1899. }