CAdvmapInterface.cpp 58 KB

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