CAdvmapInterface.cpp 57 KB

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