CAdvmapInterface.cpp 53 KB

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