CAdvmapInterface.cpp 57 KB

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