CAdvmapInterface.cpp 57 KB

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