CAdvmapInterface.cpp 55 KB

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