CAdvmapInterface.cpp 55 KB

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