CAdvmapInterface.cpp 58 KB

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