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. #include "../lib/UnlockGuard.h"
  33. #ifdef _MSC_VER
  34. #pragma warning (disable : 4355)
  35. #endif
  36. /*
  37. * CAdvMapInterface.cpp, part of VCMI engine
  38. *
  39. * Authors: listed in file AUTHORS in main folder
  40. *
  41. * License: GNU General Public License v2.0 or later
  42. * Full text of license available in license.txt file, in main folder
  43. *
  44. */
  45. #define ADVOPT (conf.go()->ac)
  46. using namespace boost::logic;
  47. using namespace boost::assign;
  48. using namespace CSDL_Ext;
  49. CAdvMapInt *adventureInt;
  50. CMinimap::CMinimap()
  51. {
  52. OBJ_CONSTRUCTION_CAPTURING_ALL;
  53. used = LCLICK | RCLICK | HOVER;
  54. int3 mapSizes = LOCPLINT->cb->getMapSize();
  55. statusbarTxt = CGI->generaltexth->zelp[291].first;
  56. rcText = CGI->generaltexth->zelp[291].second;
  57. pos.x=ADVOPT.minimapX;
  58. pos.y=ADVOPT.minimapY;
  59. pos.h=ADVOPT.minimapW;
  60. pos.w=ADVOPT.minimapH;
  61. temps = newSurface(pos.w,pos.h);
  62. aiShield = new CPicture("AISHIELD.bmp");
  63. const JsonNode config(GameConstants::DATA_DIR + "/config/minimap.json");
  64. const JsonVector &minimap_vec = config["MinimapColors"].Vector();
  65. BOOST_FOREACH(const JsonNode &m, minimap_vec) {
  66. std::pair<int,SDL_Color> vinya;
  67. vinya.first = m["terrain_id"].Float();
  68. const JsonVector &unblocked_vec = m["unblocked"].Vector();
  69. vinya.second.r = unblocked_vec[0].Float();
  70. vinya.second.g = unblocked_vec[1].Float();
  71. vinya.second.b = unblocked_vec[2].Float();
  72. vinya.second.unused = 255;
  73. colors.insert(vinya);
  74. const JsonVector &blocked_vec = m["blocked"].Vector();
  75. vinya.second.r = blocked_vec[0].Float();
  76. vinya.second.g = blocked_vec[1].Float();
  77. vinya.second.b = blocked_vec[2].Float();
  78. vinya.second.unused = 255;
  79. colorsBlocked.insert(vinya);
  80. }
  81. }
  82. CMinimap::~CMinimap()
  83. {
  84. SDL_FreeSurface(temps);
  85. for (std::map<int, CMinimapSurfacesRef>::iterator it = surfs.begin(); it != surfs.end(); ++it)
  86. {
  87. it->second.free();
  88. }
  89. }
  90. void CMinimap::draw(SDL_Surface * to)
  91. {
  92. int player = adventureInt->player;
  93. if(LOCPLINT->makingTurn)
  94. {
  95. int3 mapSizes = LOCPLINT->cb->getMapSize();
  96. //draw terrain
  97. blitAt(surfs[player].map()[adventureInt->position.z],0,0,temps);
  98. //draw heroes
  99. std::vector <const CGHeroInstance *> hh = LOCPLINT->cb->getHeroesInfo(false);
  100. int mw = surfs[player].map()[0]->w, mh = surfs[player].map()[0]->h,
  101. wo = mw/mapSizes.x, ho = mh/mapSizes.y;
  102. for (size_t i=0; i < hh.size(); ++i)
  103. {
  104. int3 hpos = hh[i]->getPosition(false);
  105. if(hpos.z!=adventureInt->position.z)
  106. continue;
  107. int3 maplgp ( (hpos.x*mw)/mapSizes.x, (hpos.y*mh)/mapSizes.y, hpos.z );
  108. for (int ii=0; ii<wo; ii++)
  109. {
  110. for (int jj=0; jj<ho; jj++)
  111. {
  112. SDL_PutPixelWithoutRefresh(temps,maplgp.x+ii,maplgp.y+jj,graphics->playerColors[hh[i]->getOwner()].r,
  113. graphics->playerColors[hh[i]->getOwner()].g,graphics->playerColors[hh[i]->getOwner()].b);
  114. }
  115. }
  116. }
  117. blitAt(surfs[player].flObjs()[adventureInt->position.z],0,0,temps);
  118. blitAt(surfs[player].FoW()[adventureInt->position.z],0,0,temps);
  119. //draw radar
  120. const int tilesw=(ADVOPT.advmapW+31)/32;
  121. const int tilesh=(ADVOPT.advmapH+31)/32;
  122. int bx = static_cast<int>((adventureInt->position.x / static_cast<double>(mapSizes.x)) * pos.w),
  123. by = static_cast<int>((adventureInt->position.y / static_cast<double>(mapSizes.y)) * pos.h),
  124. rx = static_cast<int>((tilesw / static_cast<double>(mapSizes.x)) * pos.w), //width
  125. ry = static_cast<int>((tilesh / static_cast<double>(mapSizes.y)) * pos.h); //height
  126. CSDL_Ext::drawDashedBorder(temps, Rect(bx, by, rx, ry), int3(255,75,125));
  127. //blitAt(radar,bx,by,temps);
  128. blitAt(temps,pos.x,pos.y,to);
  129. }
  130. else
  131. {
  132. aiShield->showAll(to);
  133. }
  134. }
  135. CMinimapSurfacesRef::CMinimapSurfacesRef() : ready(false)
  136. {
  137. }
  138. void CMinimapSurfacesRef::redraw(int level)
  139. {
  140. ready = true;
  141. initMap(level);
  142. //FoW
  143. initFoW(level);
  144. //flaggable objects
  145. initFlaggableObjs(level);
  146. //showing tiles
  147. showVisibleTiles();
  148. }
  149. void CMinimapSurfacesRef::initMap(int level)
  150. {
  151. /*for(int g=0; g<map.size(); ++g)
  152. {
  153. SDL_FreeSurface(map[g]);
  154. }
  155. map.clear();*/
  156. const Rect &minimap_pos = adventureInt->minimap.pos;
  157. std::map<int,SDL_Color> &colors = adventureInt->minimap.colors;
  158. std::map<int,SDL_Color> &colorsBlocked = adventureInt->minimap.colorsBlocked;
  159. int3 mapSizes = LOCPLINT->cb->getMapSize();
  160. for (size_t i=0; i<CGI->mh->sizes.z; i++)
  161. {
  162. SDL_Surface *pom;
  163. if ((level>=0) && (i!=level))
  164. continue;
  165. if (map_.size()<i+1)
  166. pom = CSDL_Ext::newSurface(minimap_pos.w,minimap_pos.h,screen);
  167. else pom = map_[i];
  168. for (int x=0;x<minimap_pos.w;x++)
  169. {
  170. for (int y=0;y<minimap_pos.h;y++)
  171. {
  172. int mx=(mapSizes.x*x)/minimap_pos.w;
  173. int my=(mapSizes.y*y)/minimap_pos.h;
  174. const TerrainTile * tile = LOCPLINT->cb->getTile(int3(mx, my, i), false);
  175. if(tile)
  176. {
  177. if (tile->blocked && (!tile->visitable))
  178. SDL_PutPixelWithoutRefresh(pom, x, y, colorsBlocked[tile->tertype].r, colorsBlocked[tile->tertype].g, colorsBlocked[tile->tertype].b);
  179. else SDL_PutPixelWithoutRefresh(pom, x, y, colors[tile->tertype].r, colors[tile->tertype].g, colors[tile->tertype].b);
  180. }
  181. }
  182. }
  183. map_.push_back(pom);
  184. }
  185. }
  186. void CMinimapSurfacesRef::initFoW(int level)
  187. {
  188. /*for(int g=0; g<FoW.size(); ++g)
  189. {
  190. SDL_FreeSurface(FoW[g]);
  191. }
  192. FoW.clear();*/
  193. const Rect &minimap_pos = adventureInt->minimap.pos;
  194. int3 mapSizes = LOCPLINT->cb->getMapSize();
  195. int mw = map_[0]->w, mh = map_[0]->h;//,
  196. //wo = mw/mapSizes.x, ho = mh/mapSizes.y; //TODO use me
  197. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  198. {
  199. if(level>=0 && d!=level)
  200. continue;
  201. SDL_Surface * pt = CSDL_Ext::newSurface(minimap_pos.w, minimap_pos.h, CSDL_Ext::std32bppSurface);
  202. for (int i=0; i<mw; i++)
  203. {
  204. for (int j=0; j<mh; j++)
  205. {
  206. int3 pp( ((i*mapSizes.x)/mw), ((j*mapSizes.y)/mh), d );
  207. if ( !LOCPLINT->cb->isVisible(pp) )
  208. {
  209. CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0);
  210. }
  211. }
  212. }
  213. FoW_.push_back(pt);
  214. }
  215. }
  216. void CMinimapSurfacesRef::initFlaggableObjs(int level)
  217. {
  218. /*for(int g=0; g<flObjs.size(); ++g)
  219. {
  220. SDL_FreeSurface(flObjs[g]);
  221. }
  222. flObjs.clear();*/
  223. const Rect &minimap_pos = adventureInt->minimap.pos;
  224. int3 mapSizes = LOCPLINT->cb->getMapSize();
  225. int mw = map_[0]->w, mh = map_[0]->h;
  226. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  227. {
  228. if(level>=0 && d!=level)
  229. continue;
  230. SDL_Surface * pt = CSDL_Ext::newSurface(minimap_pos.w, minimap_pos.h, CSDL_Ext::std32bppSurface);
  231. for (int i=0; i<mw; i++)
  232. {
  233. for (int j=0; j<mh; j++)
  234. {
  235. CSDL_Ext::SDL_PutPixelWithoutRefresh(pt,i,j,0,0,0,0);
  236. }
  237. }
  238. flObjs_.push_back(pt);
  239. }
  240. }
  241. void CMinimap::updateRadar()
  242. {}
  243. void CMinimap::clickRight(tribool down, bool previousState)
  244. {
  245. adventureInt->handleRightClick(rcText,down);
  246. }
  247. void CMinimap::clickLeft(tribool down, bool previousState)
  248. {
  249. if (down && !(used & MOVE))
  250. changeUsedEvents(MOVE, true);
  251. else if (!down && used & MOVE)
  252. changeUsedEvents(MOVE, false);
  253. //ClickableL::clickLeft(down);
  254. if (!((bool)down))
  255. return;
  256. double dx = (GH.current->motion.x - pos.x) / static_cast<double>(pos.w),
  257. dy = (GH.current->motion.y - pos.y) / static_cast<double>(pos.h);
  258. int3 newCPos;
  259. newCPos.x = (CGI->mh->sizes.x*dx);
  260. newCPos.y = (CGI->mh->sizes.y*dy);
  261. newCPos.z = adventureInt->position.z;
  262. adventureInt->centerOn(newCPos);
  263. }
  264. void CMinimap::hover (bool on)
  265. {
  266. //Hoverable::hover(on);
  267. if (on)
  268. adventureInt->statusbar.print(statusbarTxt);
  269. else if (adventureInt->statusbar.current==statusbarTxt)
  270. adventureInt->statusbar.clear();
  271. }
  272. void CMinimap::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  273. {
  274. if (pressedL)
  275. {
  276. clickLeft(true, true);
  277. }
  278. }
  279. void CMinimap::activate()
  280. {
  281. CIntObject::activate();
  282. }
  283. void CMinimap::deactivate()
  284. {
  285. CIntObject::deactivate();
  286. }
  287. std::vector<SDL_Surface*> & CMinimapSurfacesRef::map()
  288. {
  289. if (!ready) redraw();
  290. return map_;
  291. }
  292. std::vector<SDL_Surface*> & CMinimapSurfacesRef::FoW()
  293. {
  294. if (!ready) redraw();
  295. return FoW_;
  296. }
  297. std::vector<SDL_Surface*> & CMinimapSurfacesRef::flObjs()
  298. {
  299. if (!ready) redraw();
  300. return flObjs_;
  301. }
  302. void CMinimapSurfacesRef::free()
  303. {
  304. if (ready)
  305. {
  306. for (int g = 0; g < map_.size(); ++g)
  307. SDL_FreeSurface(map_[g]);
  308. map_.clear();
  309. for (int g = 0; g < FoW_.size(); ++g)
  310. SDL_FreeSurface(FoW_[g]);
  311. FoW_.clear();
  312. for (int g = 0; g < flObjs_.size(); ++g)
  313. SDL_FreeSurface(flObjs_[g]);
  314. flObjs_.clear();
  315. }
  316. }
  317. void CMinimap::showTile(const int3 &pos)
  318. {
  319. const int player = adventureInt->player;
  320. std::vector<SDL_Surface*> &map = surfs[player].map();
  321. std::vector<SDL_Surface*> &FoW = surfs[player].FoW();
  322. std::vector<SDL_Surface*> &flObjs = surfs[player].flObjs();
  323. int3 mapSizes = LOCPLINT->cb->getMapSize();
  324. //drawing terrain
  325. int mw = map[0]->w, mh = map[0]->h;
  326. double wo = ((double)mw)/mapSizes.x, ho = ((double)mh)/mapSizes.y;
  327. for (int ii=0; ii<wo; ii++)
  328. {
  329. for (int jj=0; jj<ho; jj++)
  330. {
  331. if ((pos.x*wo+ii<this->pos.w) && (pos.y*ho+jj<this->pos.h))
  332. CSDL_Ext::SDL_PutPixelWithoutRefresh(FoW[pos.z],pos.x*wo+ii,pos.y*ho+jj,0,0,0,0);
  333. const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
  334. if(tile)
  335. {
  336. if (tile->blocked && (!tile->visitable))
  337. 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);
  338. 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);
  339. }
  340. }
  341. }
  342. //drawing flaggable objects
  343. int woShifted = wo, hoShifted = ho; //for better minimap rendering on L-sized maps
  344. std::vector < const CGObjectInstance * > oo = LOCPLINT->cb->getFlaggableObjects(pos);
  345. for(size_t v=0; v<oo.size(); ++v)
  346. {
  347. if(!dynamic_cast< const CGHeroInstance * >(oo[v])) //heroes have been printed
  348. {
  349. int3 maplgp ( (pos.x*mw)/mapSizes.x, (pos.y*mh)/mapSizes.y, pos.z );
  350. if(((int)wo) * mapSizes.x != mw && pos.x+1 < mapSizes.x)//minimap size in X is not multiple of map size in X
  351. {
  352. std::vector < const CGObjectInstance * > op1x = LOCPLINT->cb->getFlaggableObjects(int3(pos.x+1, pos.y, pos.z));
  353. if(op1x.size()!=0)
  354. {
  355. woShifted = wo + 1;
  356. }
  357. else
  358. {
  359. woShifted = wo;
  360. }
  361. }
  362. if(((int)ho) * mapSizes.y != mh && pos.y+1 < mapSizes.y) //minimap size in Y is not multiple of map size in Y
  363. {
  364. std::vector < const CGObjectInstance * > op1y = LOCPLINT->cb->getFlaggableObjects(int3(pos.x, pos.y+1, pos.z));
  365. if(op1y.size()!=0)
  366. {
  367. hoShifted = ho + 1;
  368. }
  369. else
  370. {
  371. hoShifted = ho;
  372. }
  373. }
  374. for (int ii=0; ii<woShifted; ii++) //rendering flaggable objects
  375. {
  376. for (int jj=0; jj<hoShifted; jj++)
  377. {
  378. if(oo[v]->tempOwner == 255)
  379. SDL_PutPixelWithoutRefresh(flObjs[pos.z],maplgp.x+ii,maplgp.y+jj,graphics->neutralColor->b,
  380. graphics->neutralColor->g,graphics->neutralColor->r);
  381. else
  382. SDL_PutPixelWithoutRefresh(flObjs[pos.z],maplgp.x+ii,maplgp.y+jj,graphics->playerColors[oo[v]->getOwner()].b,
  383. graphics->playerColors[oo[v]->getOwner()].g,graphics->playerColors[oo[v]->getOwner()].r);
  384. }
  385. }
  386. }
  387. }
  388. //flaggable objects drawn
  389. }
  390. void CMinimapSurfacesRef::showVisibleTiles(int level)
  391. {
  392. int3 mapSizes = LOCPLINT->cb->getMapSize();
  393. for(int d=0; d<CGI->mh->map->twoLevel+1; ++d)
  394. {
  395. if(level>=0 && d!=level)
  396. continue;
  397. for(int x=0; x<mapSizes.x; ++x)
  398. {
  399. for(int y=0; y<mapSizes.y; ++y)
  400. {
  401. if(LOCPLINT->cb->isVisible(int3(x, y, d)))
  402. {
  403. adventureInt->minimap.showTile(int3(x, y, d));
  404. }
  405. }
  406. }
  407. }
  408. }
  409. void CMinimap::hideTile(const int3 &pos)
  410. {
  411. const int player = adventureInt->player;
  412. std::vector<SDL_Surface*> &map = surfs[player].map();
  413. std::vector<SDL_Surface*> &FoW = surfs[player].FoW();
  414. int3 mapSizes = LOCPLINT->cb->getMapSize();
  415. //drawing terrain
  416. int mw = map[0]->w, mh = map[0]->h;
  417. double wo = ((double)mw)/mapSizes.x, ho = ((double)mh)/mapSizes.y;
  418. for (int ii=0; ii<wo; ii++)
  419. {
  420. for (int jj=0; jj<ho; jj++)
  421. {
  422. if ((pos.x*wo+ii<this->pos.w) && (pos.y*ho+jj<this->pos.h))
  423. CSDL_Ext::SDL_PutPixelWithoutRefresh(FoW[pos.z],pos.x*wo+ii,pos.y*ho+jj,0,0,0,0);
  424. }
  425. }
  426. }
  427. CTerrainRect::CTerrainRect()
  428. :curHoveredTile(-1,-1,-1), currentPath(NULL)
  429. {
  430. tilesw=(ADVOPT.advmapW+31)/32;
  431. tilesh=(ADVOPT.advmapH+31)/32;
  432. pos.x=ADVOPT.advmapX;
  433. pos.y=ADVOPT.advmapY;
  434. pos.w=ADVOPT.advmapW;
  435. pos.h=ADVOPT.advmapH;
  436. moveX = moveY = 0;
  437. }
  438. CTerrainRect::~CTerrainRect()
  439. {
  440. }
  441. void CTerrainRect::activate()
  442. {
  443. activateLClick();
  444. activateRClick();
  445. activateHover();
  446. activateMouseMove();
  447. }
  448. void CTerrainRect::deactivate()
  449. {
  450. deactivateLClick();
  451. deactivateRClick();
  452. deactivateHover();
  453. deactivateMouseMove();
  454. curHoveredTile = int3(-1,-1,-1); //we lost info about hovered tile when disabling
  455. }
  456. void CTerrainRect::clickLeft(tribool down, bool previousState)
  457. {
  458. if ((down==false) || indeterminate(down))
  459. return;
  460. int3 mp = whichTileIsIt();
  461. if (mp.x<0 || mp.y<0 || mp.x >= LOCPLINT->cb->getMapSize().x || mp.y >= LOCPLINT->cb->getMapSize().y)
  462. return;
  463. adventureInt->tileLClicked(mp);
  464. }
  465. void CTerrainRect::clickRight(tribool down, bool previousState)
  466. {
  467. int3 mp = whichTileIsIt();
  468. if (CGI->mh->map->isInTheMap(mp) && down)
  469. adventureInt->tileRClicked(mp);
  470. }
  471. void CTerrainRect::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  472. {
  473. int3 tHovered = whichTileIsIt(sEvent.x,sEvent.y);
  474. int3 pom = adventureInt->verifyPos(tHovered);
  475. if(tHovered != pom) //tile outside the map
  476. {
  477. CCS->curh->changeGraphic(0, 0);
  478. return;
  479. }
  480. if (pom != curHoveredTile)
  481. curHoveredTile=pom;
  482. else
  483. return;
  484. adventureInt->tileHovered(curHoveredTile);
  485. }
  486. void CTerrainRect::hover(bool on)
  487. {
  488. if (!on)
  489. {
  490. adventureInt->statusbar.clear();
  491. CCS->curh->changeGraphic(0,0);
  492. }
  493. //Hoverable::hover(on);
  494. }
  495. void CTerrainRect::showPath(const SDL_Rect * extRect, SDL_Surface * to)
  496. {
  497. const static int pns[9][9] = {
  498. {16, 17, 18, 7, -1, 19, 6, 5, -1},
  499. { 8, 9, 18, 7, -1, 19, 6, -1, 20},
  500. { 8, 1, 10, 7, -1, 19, -1, 21, 20},
  501. {24, 17, 18, 15, -1, -1, 6, 5, 4},
  502. {-1, -1, -1, -1, -1, -1, -1, -1, -1},
  503. { 8, 1, 2, -1, -1, 11, 22, 21, 20},
  504. {24, 17, -1, 23, -1, 3, 14, 5, 4},
  505. {24, -1, 2, 23, -1, 3, 22, 13, 4},
  506. {-1, 1, 2, 23, -1, 3, 22, 21, 12}
  507. }; //table of magic values TODO meaning, change variable name
  508. for (size_t i=0; i < currentPath->nodes.size()-1; ++i)
  509. {
  510. const int3 &curPos = currentPath->nodes[i].coord, &nextPos = currentPath->nodes[i+1].coord;
  511. if(curPos.z != adventureInt->position.z)
  512. continue;
  513. int pn=-1;//number of picture
  514. if (i==0) //last tile
  515. {
  516. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  517. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  518. if (x<0 || y<0 || x>pos.w || y>pos.h)
  519. continue;
  520. pn=0;
  521. }
  522. else
  523. {
  524. const int3 &prevPos = currentPath->nodes[i-1].coord;
  525. std::vector<CGPathNode> & cv = currentPath->nodes;
  526. /* Vector directions
  527. * 0 1 2
  528. * \ | /
  529. * 3 - 4 - 5
  530. * / | \
  531. * 6 7 8
  532. *For example:
  533. * |
  534. * |__\
  535. * /
  536. * is id1=7, id2=5 (pns[7][5])
  537. */
  538. bool pathContinuous = curPos.areNeighbours(nextPos) && curPos.areNeighbours(prevPos);
  539. if(pathContinuous && cv[i].land == cv[i+1].land)
  540. {
  541. int id1=(curPos.x-nextPos.x+1)+3*(curPos.y-nextPos.y+1); //Direction of entering vector
  542. int id2=(cv[i-1].coord.x-curPos.x+1)+3*(cv[i-1].coord.y-curPos.y+1); //Direction of exiting vector
  543. pn=pns[id1][id2];
  544. }
  545. else //path discontinuity or sea/land transition (eg. when moving through Subterranean Gate or Boat)
  546. {
  547. pn = 0;
  548. }
  549. }
  550. if (currentPath->nodes[i].turns)
  551. pn+=25;
  552. if (pn>=0)
  553. {
  554. CDefEssential * arrows = graphics->heroMoveArrows;
  555. int x = 32*(curPos.x-adventureInt->position.x)+CGI->mh->offsetX + pos.x,
  556. y = 32*(curPos.y-adventureInt->position.y)+CGI->mh->offsetY + pos.y;
  557. if (x<0 || y<0 || x>pos.w || y>pos.h)
  558. continue;
  559. int hvx = (x+arrows->ourImages[pn].bitmap->w)-(pos.x+pos.w),
  560. hvy = (y+arrows->ourImages[pn].bitmap->h)-(pos.y+pos.h);
  561. SDL_Rect prevClip;
  562. SDL_GetClipRect(to, &prevClip);
  563. SDL_SetClipRect(to, extRect); //preventing blitting outside of that rect
  564. if(ADVOPT.smoothMove) //version for smooth hero move, with pos shifts
  565. {
  566. if (hvx<0 && hvy<0)
  567. {
  568. Rect dstRect = genRect(32, 32, x + moveX, y + moveY);
  569. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, to, &dstRect);
  570. }
  571. else if(hvx<0)
  572. {
  573. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  574. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x + moveX, y + moveY);
  575. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  576. }
  577. else if (hvy<0)
  578. {
  579. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  580. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
  581. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  582. }
  583. else
  584. {
  585. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  586. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x + moveX, y + moveY);
  587. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  588. }
  589. }
  590. else //standard version
  591. {
  592. if (hvx<0 && hvy<0)
  593. {
  594. Rect dstRect = genRect(32, 32, x, y);
  595. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, NULL, to, &dstRect);
  596. }
  597. else if(hvx<0)
  598. {
  599. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, 0, 0);
  600. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w, x, y);
  601. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  602. }
  603. else if (hvy<0)
  604. {
  605. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  606. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h, arrows->ourImages[pn].bitmap->w-hvx, x, y);
  607. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  608. }
  609. else
  610. {
  611. Rect srcRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, 0, 0);
  612. Rect dstRect = genRect(arrows->ourImages[pn].bitmap->h-hvy, arrows->ourImages[pn].bitmap->w-hvx, x, y);
  613. CSDL_Ext::blit8bppAlphaTo24bpp(arrows->ourImages[pn].bitmap, &srcRect, to, &dstRect);
  614. }
  615. }
  616. SDL_SetClipRect(to, &prevClip);
  617. }
  618. } //for (int i=0;i<currentPath->nodes.size()-1;i++)
  619. }
  620. void CTerrainRect::show(SDL_Surface * to)
  621. {
  622. if(ADVOPT.smoothMove)
  623. CGI->mh->terrainRect
  624. (adventureInt->position, adventureInt->anim,
  625. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  626. to, &pos, moveX, moveY, false, int3());
  627. else
  628. CGI->mh->terrainRect
  629. (adventureInt->position, adventureInt->anim,
  630. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  631. to, &pos, 0, 0, false, int3());
  632. //SDL_BlitSurface(teren,&genRect(pos.h,pos.w,0,0),screen,&genRect(547,594,7,6));
  633. //SDL_FreeSurface(teren);
  634. if (currentPath/* && adventureInt->position.z==currentPath->startPos().z*/) //drawing path
  635. {
  636. showPath(&pos, to);
  637. }
  638. }
  639. int3 CTerrainRect::whichTileIsIt(const int & x, const int & y)
  640. {
  641. int3 ret;
  642. ret.x = adventureInt->position.x + ((GH.current->motion.x-CGI->mh->offsetX-pos.x)/32);
  643. ret.y = adventureInt->position.y + ((GH.current->motion.y-CGI->mh->offsetY-pos.y)/32);
  644. ret.z = adventureInt->position.z;
  645. return ret;
  646. }
  647. int3 CTerrainRect::whichTileIsIt()
  648. {
  649. return whichTileIsIt(GH.current->motion.x,GH.current->motion.y);
  650. }
  651. void CResDataBar::clickRight(tribool down, bool previousState)
  652. {
  653. }
  654. void CResDataBar::activate()
  655. {
  656. activateRClick();
  657. }
  658. void CResDataBar::deactivate()
  659. {
  660. deactivateRClick();
  661. }
  662. CResDataBar::CResDataBar(const std::string &defname, int x, int y, int offx, int offy, int resdist, int datedist)
  663. {
  664. bg = BitmapHandler::loadBitmap(defname);
  665. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  666. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  667. pos = genRect(bg->h, bg->w, pos.x+x, pos.y+y);
  668. txtpos.resize(8);
  669. for (int i = 0; i < 8 ; i++)
  670. {
  671. txtpos[i].first = pos.x + offx + resdist*i;
  672. txtpos[i].second = pos.y + offy;
  673. }
  674. txtpos[7].first = txtpos[6].first + datedist;
  675. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  676. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  677. }
  678. CResDataBar::CResDataBar()
  679. {
  680. bg = BitmapHandler::loadBitmap(ADVOPT.resdatabarG);
  681. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  682. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  683. pos = genRect(bg->h,bg->w,ADVOPT.resdatabarX,ADVOPT.resdatabarY);
  684. txtpos.resize(8);
  685. for (int i = 0; i < 8 ; i++)
  686. {
  687. txtpos[i].first = pos.x + ADVOPT.resOffsetX + ADVOPT.resDist*i;
  688. txtpos[i].second = pos.y + ADVOPT.resOffsetY;
  689. }
  690. txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist;
  691. datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  692. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  693. }
  694. CResDataBar::~CResDataBar()
  695. {
  696. SDL_FreeSurface(bg);
  697. }
  698. void CResDataBar::draw(SDL_Surface * to)
  699. {
  700. blitAt(bg,pos.x,pos.y,to);
  701. char * buf = new char[15];
  702. for (int i=0;i<7;i++)
  703. {
  704. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  705. printAt(buf,txtpos[i].first,txtpos[i].second,FONT_SMALL,Colors::Cornsilk,to);
  706. }
  707. std::vector<std::string> temp;
  708. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp+=std::string(buf);
  709. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp+=std::string(buf);
  710. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp+=std::string(buf);
  711. printAt(processStr(datetext,temp),txtpos[7].first,txtpos[7].second,FONT_SMALL,Colors::Cornsilk,to);
  712. temp.clear();
  713. //updateRect(&pos,screen);
  714. delete[] buf;
  715. }
  716. void CResDataBar::show(SDL_Surface * to)
  717. {
  718. }
  719. void CResDataBar::showAll(SDL_Surface * to)
  720. {
  721. draw(to);
  722. }
  723. CInfoBar::CInfoBar()
  724. {
  725. toNextTick = pom = -1;
  726. mode = NOTHING;
  727. pos.x=ADVOPT.infoboxX;
  728. pos.y=ADVOPT.infoboxY;
  729. pos.w=194;
  730. pos.h=186;
  731. day = CDefHandler::giveDef("NEWDAY.DEF");
  732. week1 = CDefHandler::giveDef("NEWWEEK1.DEF");
  733. week2 = CDefHandler::giveDef("NEWWEEK2.DEF");
  734. week3 = CDefHandler::giveDef("NEWWEEK3.DEF");
  735. week4 = CDefHandler::giveDef("NEWWEEK4.DEF");
  736. hourglass = CDefHandler::giveDef("HOURGLAS.DEF");
  737. hourglassSand = CDefHandler::giveDef("HOURSAND.DEF");
  738. selInfoWin = NULL;
  739. }
  740. CInfoBar::~CInfoBar()
  741. {
  742. delete day;
  743. delete week1;
  744. delete week2;
  745. delete week3;
  746. delete week4;
  747. if(selInfoWin)
  748. SDL_FreeSurface(selInfoWin);
  749. }
  750. void CInfoBar::showAll(SDL_Surface * to)
  751. {
  752. if (mode >= NEW_DAY && mode <= NEW_WEEK4)
  753. {
  754. blitAnim(mode);
  755. }
  756. else if(mode == ENEMY_TURN)
  757. {
  758. CPicture bg("ADSTATOT.bmp");
  759. bg.convertToScreenBPP();
  760. blitAt(graphics->flags->ourImages[enemyTurnInfo.color].bitmap, 20, 51, bg);
  761. int hourglassFrame = enemyTurnInfo.progress * hourglass->ourImages.size();
  762. static int sandFrame = 0;
  763. vstd::amin(hourglassFrame, hourglass->ourImages.size()-1);
  764. blitAt(hourglassSand->ourImages[sandFrame++ % hourglassSand->ourImages.size()].bitmap, 99, 51, bg);
  765. blitAt(hourglass->ourImages[hourglassFrame].bitmap, 99, 51, bg);
  766. blitAtLoc(bg, 8, 11, to);
  767. }
  768. else if(selInfoWin)
  769. {
  770. blitAt(selInfoWin, pos.x, pos.y, to);
  771. }
  772. }
  773. CDefHandler * CInfoBar::getAnim(EMode mode)
  774. {
  775. switch(mode)
  776. {
  777. case NEW_DAY:
  778. return day;
  779. case NEW_WEEK1:
  780. return week1;
  781. case NEW_WEEK2:
  782. return week2;
  783. case NEW_WEEK3:
  784. return week3;
  785. case NEW_WEEK4:
  786. return week4;
  787. default:
  788. return NULL;
  789. }
  790. }
  791. void CInfoBar::blitAnim(EMode mode)//0 - day, 1 - week
  792. {
  793. CDefHandler * anim = NULL;
  794. std::ostringstream txt;
  795. anim = getAnim(mode);
  796. if(mode > NEW_DAY) //new week animation
  797. {
  798. txt << CGI->generaltexth->allTexts[63] << " " << LOCPLINT->cb->getDate(2);
  799. }
  800. else if(mode == NEW_DAY) //new day
  801. {
  802. txt << CGI->generaltexth->allTexts[64] << " " << LOCPLINT->cb->getDate(1);
  803. }
  804. blitAt(anim->ourImages[pom].bitmap,pos.x+9,pos.y+10);
  805. printAtMiddle(txt.str(),pos.x+95,pos.y+31,FONT_MEDIUM,Colors::Cornsilk);
  806. if (pom == anim->ourImages.size()-1)
  807. toNextTick+=750;
  808. }
  809. void CInfoBar::newDay(int Day)
  810. {
  811. if(LOCPLINT->cb->getDate(1) != 1)
  812. {
  813. mode = NEW_DAY; //showing day
  814. }
  815. else
  816. {
  817. switch(LOCPLINT->cb->getDate(2))
  818. {
  819. case 1:
  820. mode = NEW_WEEK1;
  821. break;
  822. case 2:
  823. mode = NEW_WEEK2;
  824. break;
  825. case 3:
  826. mode = NEW_WEEK3;
  827. break;
  828. case 4:
  829. mode = NEW_WEEK4;
  830. break;
  831. default:
  832. mode = NOTHING;
  833. break;
  834. }
  835. }
  836. pom = 0;
  837. if(!(active & TIME))
  838. activateTimer();
  839. toNextTick = 500;
  840. blitAnim(mode);
  841. }
  842. void CInfoBar::showComp(CComponent * comp, int time)
  843. {
  844. if(comp->type != CComponent::hero)
  845. {
  846. curSel = NULL;
  847. }
  848. SDL_Surface * b = BitmapHandler::loadBitmap("ADSTATOT.bmp");
  849. blitAt(b,pos.x+8,pos.y+11);
  850. blitAt(comp->getImg(),pos.x+52,pos.y+54);
  851. printAtMiddle(comp->subtitle,pos.x+91,pos.y+158,FONT_SMALL,Colors::Cornsilk);
  852. printAtMiddleWB(comp->description,pos.x+94,pos.y+31,FONT_SMALL,26,Colors::Cornsilk);
  853. SDL_FreeSurface(b);
  854. if(!(active & TIME))
  855. activateTimer();
  856. mode = SHOW_COMPONENT;
  857. toNextTick = time;
  858. }
  859. void CInfoBar::tick()
  860. {
  861. if(mode >= NEW_DAY && mode <= NEW_WEEK4) //animation
  862. {
  863. pom++;
  864. if (pom >= getAnim(mode)->ourImages.size())
  865. {
  866. deactivateTimer();
  867. toNextTick = -1;
  868. mode = NOTHING;
  869. }
  870. toNextTick = 150;
  871. }
  872. else if(mode == SHOW_COMPONENT)
  873. {
  874. deactivateTimer();
  875. toNextTick = -1;
  876. mode = NOTHING;
  877. }
  878. redraw();
  879. }
  880. void CInfoBar::show(SDL_Surface * to)
  881. {
  882. }
  883. void CInfoBar::activate()
  884. {
  885. //CIntObject::activate();
  886. }
  887. void CInfoBar::deactivate()
  888. {
  889. //CIntObject::deactivate();
  890. if(active & TIME)
  891. deactivateTimer();
  892. toNextTick = -1;
  893. mode = NOTHING;
  894. }
  895. void CInfoBar::updateSelection(const CGObjectInstance *obj)
  896. {
  897. if(obj->ID == GameConstants::HEROI_TYPE)
  898. curSel = static_cast<const CGHeroInstance*>(obj);
  899. else
  900. curSel = NULL;
  901. if(selInfoWin)
  902. SDL_FreeSurface(selInfoWin);
  903. selInfoWin = LOCPLINT->infoWin(obj);
  904. }
  905. void CInfoBar::enemyTurn(ui8 color, double progress)
  906. {
  907. mode = ENEMY_TURN;
  908. enemyTurnInfo.color = color;
  909. enemyTurnInfo.progress = progress;
  910. redraw();
  911. if(!(active & TIME))
  912. activateTimer();
  913. toNextTick = 250;
  914. }
  915. CAdvMapInt::CAdvMapInt()
  916. :statusbar(ADVOPT.statusbarX,ADVOPT.statusbarY,ADVOPT.statusbarG),
  917. kingOverview(CGI->generaltexth->zelp[293].first,CGI->generaltexth->zelp[293].second,
  918. boost::bind(&CAdvMapInt::fshowOverview,this),&ADVOPT.kingOverview, SDLK_k),
  919. underground(CGI->generaltexth->zelp[294].first,CGI->generaltexth->zelp[294].second,
  920. boost::bind(&CAdvMapInt::fswitchLevel,this),&ADVOPT.underground, SDLK_u),
  921. questlog(CGI->generaltexth->zelp[295].first,CGI->generaltexth->zelp[295].second,
  922. boost::bind(&CAdvMapInt::fshowQuestlog,this),&ADVOPT.questlog, SDLK_q),
  923. sleepWake(CGI->generaltexth->zelp[296].first,CGI->generaltexth->zelp[296].second,
  924. boost::bind(&CAdvMapInt::fsleepWake,this), &ADVOPT.sleepWake, SDLK_w),
  925. moveHero(CGI->generaltexth->zelp[297].first,CGI->generaltexth->zelp[297].second,
  926. boost::bind(&CAdvMapInt::fmoveHero,this), &ADVOPT.moveHero, SDLK_m),
  927. spellbook(CGI->generaltexth->zelp[298].first,CGI->generaltexth->zelp[298].second,
  928. boost::bind(&CAdvMapInt::fshowSpellbok,this), &ADVOPT.spellbook, SDLK_c),
  929. advOptions(CGI->generaltexth->zelp[299].first,CGI->generaltexth->zelp[299].second,
  930. boost::bind(&CAdvMapInt::fadventureOPtions,this), &ADVOPT.advOptions, SDLK_a),
  931. sysOptions(CGI->generaltexth->zelp[300].first,CGI->generaltexth->zelp[300].second,
  932. boost::bind(&CAdvMapInt::fsystemOptions,this), &ADVOPT.sysOptions, SDLK_o),
  933. nextHero(CGI->generaltexth->zelp[301].first,CGI->generaltexth->zelp[301].second,
  934. boost::bind(&CAdvMapInt::fnextHero,this), &ADVOPT.nextHero, SDLK_h),
  935. endTurn(CGI->generaltexth->zelp[302].first,CGI->generaltexth->zelp[302].second,
  936. boost::bind(&CAdvMapInt::fendTurn,this), &ADVOPT.endTurn, SDLK_e),
  937. heroList(ADVOPT.hlistSize),
  938. townList(ADVOPT.tlistSize,ADVOPT.tlistX,ADVOPT.tlistY,ADVOPT.tlistAU,ADVOPT.tlistAD)//(5,&genRect(192,48,747,196),747,196,747,372),
  939. {
  940. state = NA;
  941. spellBeingCasted = NULL;
  942. pos.x = pos.y = 0;
  943. pos.w = screen->w;
  944. pos.h = screen->h;
  945. selection = NULL;
  946. townList.fun = boost::bind(&CAdvMapInt::selectionChanged,this);
  947. adventureInt=this;
  948. bg = BitmapHandler::loadBitmap(ADVOPT.mainGraphic);
  949. scrollingDir = 0;
  950. updateScreen = false;
  951. anim=0;
  952. animValHitCount=0; //animation frame
  953. heroAnim=0;
  954. heroAnimValHitCount=0; // hero animation frame
  955. heroList.init();
  956. heroList.genList();
  957. //townList.init();
  958. //townList.genList();
  959. for (int g=0; g<ADVOPT.gemG.size(); ++g)
  960. {
  961. gems.push_back(CDefHandler::giveDef(ADVOPT.gemG[g]));
  962. }
  963. setPlayer(LOCPLINT->playerID);
  964. }
  965. CAdvMapInt::~CAdvMapInt()
  966. {
  967. SDL_FreeSurface(bg);
  968. for(int i=0; i<gems.size(); i++)
  969. delete gems[i];
  970. }
  971. void CAdvMapInt::fshowOverview()
  972. {
  973. GH.pushInt(new CKingdomInterface);
  974. }
  975. void CAdvMapInt::fswitchLevel()
  976. {
  977. if(!CGI->mh->map->twoLevel)
  978. return;
  979. if (position.z)
  980. {
  981. position.z--;
  982. underground.setIndex(0,true);
  983. underground.showAll(screenBuf);
  984. }
  985. else
  986. {
  987. underground.setIndex(1,true);
  988. position.z++;
  989. underground.showAll(screenBuf);
  990. }
  991. updateScreen = true;
  992. minimap.draw(screenBuf);
  993. }
  994. void CAdvMapInt::fshowQuestlog()
  995. {
  996. }
  997. void CAdvMapInt::fsleepWake()
  998. {
  999. const CGHeroInstance *h = curHero();
  1000. if (!h)
  1001. return;
  1002. bool newSleep = !isHeroSleeping(h);
  1003. setHeroSleeping(h, newSleep);
  1004. updateSleepWake(h);
  1005. if (newSleep)
  1006. {
  1007. fnextHero();
  1008. //moveHero.block(true);
  1009. //uncomment to enable original HoMM3 behaviour:
  1010. //move button is disabled for hero going to sleep, even though it's enabled when you reselect him
  1011. }
  1012. }
  1013. void CAdvMapInt::fmoveHero()
  1014. {
  1015. const CGHeroInstance *h = curHero();
  1016. if (!h || !terrain.currentPath)
  1017. return;
  1018. LOCPLINT->moveHero(h, *terrain.currentPath);
  1019. }
  1020. void CAdvMapInt::fshowSpellbok()
  1021. {
  1022. if (!curHero()) //checking necessary values
  1023. return;
  1024. centerOn(selection);
  1025. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), curHero(), LOCPLINT, false);
  1026. GH.pushInt(spellWindow);
  1027. }
  1028. void CAdvMapInt::fadventureOPtions()
  1029. {
  1030. GH.pushInt(new CAdventureOptions);
  1031. }
  1032. void CAdvMapInt::fsystemOptions()
  1033. {
  1034. CSystemOptionsWindow * sysopWindow = new CSystemOptionsWindow(Rect::createCentered(487, 481), LOCPLINT);
  1035. GH.pushInt(sysopWindow);
  1036. }
  1037. void CAdvMapInt::fnextHero()
  1038. {
  1039. int next = getNextHeroIndex(heroList.selected);
  1040. if (next < 0)
  1041. return;
  1042. heroList.select(next);
  1043. }
  1044. void CAdvMapInt::fendTurn()
  1045. {
  1046. if(!LOCPLINT->makingTurn)
  1047. return;
  1048. if ( settings["adventure"]["heroReminder"].Bool())
  1049. {
  1050. for (int i = 0; i < LOCPLINT->wanderingHeroes.size(); i++)
  1051. if (!isHeroSleeping(LOCPLINT->wanderingHeroes[i]) && (LOCPLINT->wanderingHeroes[i]->movement > 0))
  1052. {
  1053. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[55], std::vector<CComponent*>(), boost::bind(&CAdvMapInt::endingTurn, this), 0, false);
  1054. return;
  1055. }
  1056. }
  1057. endingTurn();
  1058. }
  1059. void CAdvMapInt::updateSleepWake(const CGHeroInstance *h)
  1060. {
  1061. sleepWake.block(!h);
  1062. if (!h)
  1063. return;
  1064. bool state = isHeroSleeping(h);
  1065. sleepWake.setIndex(state ? 1 : 0, true);
  1066. sleepWake.assignedKeys.clear();
  1067. sleepWake.assignedKeys.insert(state ? SDLK_w : SDLK_z);
  1068. sleepWake.update();
  1069. }
  1070. void CAdvMapInt::updateMoveHero(const CGHeroInstance *h, tribool hasPath)
  1071. {
  1072. //default value is for everywhere but CPlayerInterface::moveHero, because paths are not updated from there immediately
  1073. if (hasPath == tribool::indeterminate_value)
  1074. hasPath = LOCPLINT->paths[h].nodes.size() ? true : false;
  1075. if (!h)
  1076. {
  1077. moveHero.block(true);
  1078. return;
  1079. }
  1080. moveHero.block(!hasPath || (h->movement == 0));
  1081. }
  1082. int CAdvMapInt::getNextHeroIndex(int startIndex)
  1083. {
  1084. if (LOCPLINT->wanderingHeroes.size() == 0)
  1085. return -1;
  1086. if (startIndex < 0)
  1087. startIndex = 0;
  1088. int i = startIndex;
  1089. do
  1090. {
  1091. i++;
  1092. if (i >= LOCPLINT->wanderingHeroes.size())
  1093. i = 0;
  1094. }
  1095. while (((LOCPLINT->wanderingHeroes[i]->movement == 0) || isHeroSleeping(LOCPLINT->wanderingHeroes[i])) && (i != startIndex));
  1096. if ((LOCPLINT->wanderingHeroes[i]->movement != 0) && !isHeroSleeping(LOCPLINT->wanderingHeroes[i]))
  1097. return i;
  1098. else
  1099. return -1;
  1100. }
  1101. void CAdvMapInt::updateNextHero(const CGHeroInstance *h)
  1102. {
  1103. int start = heroList.getPosOfHero(h);
  1104. int next = getNextHeroIndex(start);
  1105. if (next < 0)
  1106. {
  1107. nextHero.block(true);
  1108. return;
  1109. }
  1110. const CGHeroInstance *nextH = LOCPLINT->wanderingHeroes[next];
  1111. bool noActiveHeroes = (next == start) && ((nextH->movement == 0) || isHeroSleeping(nextH));
  1112. nextHero.block(noActiveHeroes);
  1113. }
  1114. void CAdvMapInt::activate()
  1115. {
  1116. if(isActive())
  1117. {
  1118. tlog1 << "Error: advmapint already active...\n";
  1119. return;
  1120. }
  1121. screenBuf = screen;
  1122. GH.statusbar = &statusbar;
  1123. activateMouseMove();
  1124. kingOverview.activate();
  1125. underground.activate();
  1126. questlog.activate();
  1127. sleepWake.activate();
  1128. moveHero.activate();
  1129. spellbook.activate();
  1130. sysOptions.activate();
  1131. advOptions.activate();
  1132. nextHero.activate();
  1133. endTurn.activate();
  1134. minimap.activate();
  1135. heroList.activate();
  1136. townList.activate();
  1137. terrain.activate();
  1138. infoBar.activate();
  1139. if(!LOCPLINT->cingconsole->active)
  1140. LOCPLINT->cingconsole->activate();
  1141. GH.fakeMouseMove(); //to restore the cursor
  1142. }
  1143. void CAdvMapInt::deactivate()
  1144. {
  1145. deactivateMouseMove();
  1146. scrollingDir = 0;
  1147. CCS->curh->changeGraphic(0,0);
  1148. kingOverview.deactivate();
  1149. underground.deactivate();
  1150. questlog.deactivate();
  1151. sleepWake.deactivate();
  1152. moveHero.deactivate();
  1153. spellbook.deactivate();
  1154. advOptions.deactivate();
  1155. sysOptions.deactivate();
  1156. nextHero.deactivate();
  1157. endTurn.deactivate();
  1158. minimap.deactivate();
  1159. heroList.deactivate();
  1160. townList.deactivate();
  1161. terrain.deactivate();
  1162. infoBar.deactivate();
  1163. if(LOCPLINT->cingconsole->active) //TODO
  1164. LOCPLINT->cingconsole->deactivate();
  1165. }
  1166. void CAdvMapInt::showAll(SDL_Surface * to)
  1167. {
  1168. blitAt(bg,0,0,to);
  1169. if(state != INGAME)
  1170. return;
  1171. kingOverview.showAll(to);
  1172. underground.showAll(to);
  1173. questlog.showAll(to);
  1174. sleepWake.showAll(to);
  1175. moveHero.showAll(to);
  1176. spellbook.showAll(to);
  1177. advOptions.showAll(to);
  1178. sysOptions.showAll(to);
  1179. nextHero.showAll(to);
  1180. endTurn.showAll(to);
  1181. minimap.draw(to);
  1182. heroList.draw(to);
  1183. townList.draw(to);
  1184. updateScreen = true;
  1185. show(to);
  1186. resdatabar.draw(to);
  1187. statusbar.show(to);
  1188. infoBar.showAll(to);
  1189. LOCPLINT->cingconsole->show(to);
  1190. }
  1191. bool CAdvMapInt::isHeroSleeping(const CGHeroInstance *hero)
  1192. {
  1193. if (!hero)
  1194. return false;
  1195. return vstd::contains(LOCPLINT->sleepingHeroes, hero);
  1196. }
  1197. void CAdvMapInt::setHeroSleeping(const CGHeroInstance *hero, bool sleep)
  1198. {
  1199. if (sleep)
  1200. LOCPLINT->sleepingHeroes += hero;
  1201. else
  1202. LOCPLINT->sleepingHeroes -= hero;
  1203. updateNextHero(NULL);
  1204. }
  1205. void CAdvMapInt::show(SDL_Surface * to)
  1206. {
  1207. if(state != INGAME)
  1208. return;
  1209. ++animValHitCount; //for animations
  1210. if(animValHitCount == 8)
  1211. {
  1212. CGI->mh->updateWater();
  1213. animValHitCount = 0;
  1214. ++anim;
  1215. updateScreen = true;
  1216. }
  1217. ++heroAnim;
  1218. int scrollSpeed = settings["adventure"]["scrollSpeed"].Float();
  1219. //if advmap needs updating AND (no dialog is shown OR ctrl is pressed)
  1220. if((animValHitCount % (4/scrollSpeed)) == 0
  1221. && (
  1222. (GH.topInt() == this)
  1223. || SDL_GetKeyState(NULL)[SDLK_LCTRL]
  1224. || SDL_GetKeyState(NULL)[SDLK_RCTRL]
  1225. )
  1226. )
  1227. {
  1228. if( (scrollingDir & LEFT) && (position.x>-CGI->mh->frameW) )
  1229. position.x--;
  1230. if( (scrollingDir & RIGHT) && (position.x < CGI->mh->map->width - CGI->mh->tilesW + CGI->mh->frameW) )
  1231. position.x++;
  1232. if( (scrollingDir & UP) && (position.y>-CGI->mh->frameH) )
  1233. position.y--;
  1234. if( (scrollingDir & DOWN) && (position.y < CGI->mh->map->height - CGI->mh->tilesH + CGI->mh->frameH) )
  1235. position.y++;
  1236. if(scrollingDir)
  1237. {
  1238. updateScreen = true;
  1239. updateMinimap=true;
  1240. }
  1241. }
  1242. if(updateScreen)
  1243. {
  1244. terrain.show(to);
  1245. for(int i=0;i<4;i++)
  1246. blitAt(gems[i]->ourImages[LOCPLINT->playerID].bitmap,ADVOPT.gemX[i],ADVOPT.gemY[i],to);
  1247. updateScreen=false;
  1248. LOCPLINT->cingconsole->show(to);
  1249. }
  1250. if (updateMinimap)
  1251. {
  1252. minimap.draw(to);
  1253. updateMinimap=false;
  1254. }
  1255. }
  1256. void CAdvMapInt::selectionChanged()
  1257. {
  1258. const CGTownInstance *to = LOCPLINT->towns[townList.selected];
  1259. select(to);
  1260. }
  1261. void CAdvMapInt::centerOn(int3 on)
  1262. {
  1263. on.x -= CGI->mh->frameW;
  1264. on.y -= CGI->mh->frameH;
  1265. on = LOCPLINT->repairScreenPos(on);
  1266. adventureInt->position = on;
  1267. adventureInt->updateScreen=true;
  1268. updateMinimap=true;
  1269. underground.setIndex(on.z,true); //change underground switch button image
  1270. if(GH.topInt() == this)
  1271. underground.redraw();
  1272. }
  1273. void CAdvMapInt::centerOn(const CGObjectInstance *obj)
  1274. {
  1275. centerOn(obj->getSightCenter());
  1276. }
  1277. void CAdvMapInt::keyPressed(const SDL_KeyboardEvent & key)
  1278. {
  1279. ui8 Dir = 0;
  1280. int k = key.keysym.sym;
  1281. const CGHeroInstance *h = curHero(); //selected hero
  1282. const CGTownInstance *t = curTown(); //selected town
  1283. switch(k)
  1284. {
  1285. case SDLK_i:
  1286. if(isActive())
  1287. CAdventureOptions::showScenarioInfo();
  1288. return;
  1289. case SDLK_s:
  1290. if(isActive())
  1291. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  1292. return;
  1293. case SDLK_d:
  1294. {
  1295. if(h && isActive() && key.state == SDL_PRESSED)
  1296. LOCPLINT->tryDiggging(h);
  1297. return;
  1298. }
  1299. case SDLK_p:
  1300. if(isActive())
  1301. LOCPLINT->showPuzzleMap();
  1302. return;
  1303. case SDLK_SPACE: //space - try to revisit current object with selected hero
  1304. {
  1305. if(!isActive())
  1306. return;
  1307. if(h && key.state == SDL_PRESSED)
  1308. {
  1309. auto unlockPim = vstd::makeUnlockGuard(*LOCPLINT->pim);
  1310. LOCPLINT->cb->moveHero(h,h->pos);
  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. }