mapHandler.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. #include "stdafx.h"
  2. #include "mapHandler.h"
  3. #include "client/SDL_Extensions.h"
  4. #include "client/CGameInfo.h"
  5. #include <cstdlib>
  6. #include "hch/CLodHandler.h"
  7. #include "hch/CDefObjInfoHandler.h"
  8. #include <algorithm>
  9. #include "lib/CGameState.h"
  10. #include "hch/CHeroHandler.h"
  11. #include "hch/CTownHandler.h"
  12. #include "client/Graphics.h"
  13. #include <iomanip>
  14. #include <sstream>
  15. #include "hch/CObjectHandler.h"
  16. #include "lib/map.h"
  17. #include "hch/CDefHandler.h"
  18. #include "client/CConfigHandler.h"
  19. #include <boost/assign/list_of.hpp>
  20. /*
  21. * mapHandler.cpp, part of VCMI engine
  22. *
  23. * Authors: listed in file AUTHORS in main folder
  24. *
  25. * License: GNU General Public License v2.0 or later
  26. * Full text of license available in license.txt file, in main folder
  27. *
  28. */
  29. extern SDL_Surface * screen;
  30. #define ADVOPT (conf.go()->ac)
  31. std::string nameFromType (int typ)
  32. {
  33. switch(static_cast<TerrainTile::EterrainType>(typ))
  34. {
  35. case TerrainTile::dirt:
  36. return std::string("DIRTTL.DEF");
  37. case TerrainTile::sand:
  38. return std::string("SANDTL.DEF");
  39. case TerrainTile::grass:
  40. return std::string("GRASTL.DEF");
  41. case TerrainTile::snow:
  42. return std::string("SNOWTL.DEF");
  43. case TerrainTile::swamp:
  44. return std::string("SWMPTL.DEF");
  45. case TerrainTile::rough:
  46. return std::string("ROUGTL.DEF");
  47. case TerrainTile::subterranean:
  48. return std::string("SUBBTL.DEF");
  49. case TerrainTile::lava:
  50. return std::string("LAVATL.DEF");
  51. case TerrainTile::water:
  52. return std::string("WATRTL.DEF");
  53. case TerrainTile::rock:
  54. return std::string("ROCKTL.DEF");
  55. case TerrainTile::border:
  56. //TODO use me
  57. break;
  58. default:
  59. //TODO do something here
  60. break;
  61. }
  62. return std::string();
  63. }
  64. struct OCM_HLP
  65. {
  66. bool operator ()(const std::pair<const CGObjectInstance*, SDL_Rect> & a, const std::pair<const CGObjectInstance*, SDL_Rect> & b)
  67. {
  68. return (*a.first)<(*b.first);
  69. }
  70. } ocmptwo ;
  71. void alphaTransformDef(CGDefInfo * defInfo)
  72. {
  73. for(int yy=0; yy<defInfo->handler->ourImages.size(); ++yy)
  74. {
  75. CSDL_Ext::alphaTransform(defInfo->handler->ourImages[yy].bitmap);
  76. }
  77. }
  78. void CMapHandler::prepareFOWDefs()
  79. {
  80. fullHide = CDefHandler::giveDef("TSHRC.DEF");
  81. partialHide = CDefHandler::giveDef("TSHRE.DEF");
  82. //adding necessary rotations
  83. int missRot [] = {22, 15, 2, 13, 12, 16, 18, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27, 28};
  84. Cimage nw;
  85. for(int g=0; g<ARRAY_COUNT(missRot); ++g)
  86. {
  87. nw = partialHide->ourImages[missRot[g]];
  88. nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  89. partialHide->ourImages.push_back(nw);
  90. }
  91. //necessaary rotations added
  92. //alpha - transformation
  93. for(size_t i=0; i<partialHide->ourImages.size(); ++i)
  94. {
  95. CSDL_Ext::alphaTransform(partialHide->ourImages[i].bitmap);
  96. }
  97. //initialization of type of full-hide image
  98. hideBitmap.resize(CGI->mh->map->width);
  99. for (size_t i=0;i<hideBitmap.size();i++)
  100. {
  101. hideBitmap[i].resize(CGI->mh->map->height);
  102. }
  103. for (size_t i=0; i<hideBitmap.size(); ++i)
  104. {
  105. for (int j=0; j < CGI->mh->map->height; ++j)
  106. {
  107. hideBitmap[i][j].resize(CGI->mh->map->twoLevel+1);
  108. for(int k=0; k<CGI->mh->map->twoLevel+1; ++k)
  109. {
  110. hideBitmap[i][j][k] = rand()%fullHide->ourImages.size();
  111. }
  112. }
  113. }
  114. }
  115. void CMapHandler::roadsRiverTerrainInit()
  116. {
  117. //initializing road's and river's DefHandlers
  118. roadDefs.push_back(CDefHandler::giveDefEss("dirtrd.def"));
  119. roadDefs.push_back(CDefHandler::giveDefEss("gravrd.def"));
  120. roadDefs.push_back(CDefHandler::giveDefEss("cobbrd.def"));
  121. staticRiverDefs.push_back(CDefHandler::giveDefEss("clrrvr.def"));
  122. staticRiverDefs.push_back(CDefHandler::giveDefEss("icyrvr.def"));
  123. staticRiverDefs.push_back(CDefHandler::giveDefEss("mudrvr.def"));
  124. staticRiverDefs.push_back(CDefHandler::giveDefEss("lavrvr.def"));
  125. for(size_t g=0; g<staticRiverDefs.size(); ++g)
  126. {
  127. for(size_t h=0; h < staticRiverDefs[g]->ourImages.size(); ++h)
  128. {
  129. CSDL_Ext::alphaTransform(staticRiverDefs[g]->ourImages[h].bitmap);
  130. }
  131. }
  132. for(size_t g=0; g<roadDefs.size(); ++g)
  133. {
  134. for(size_t h=0; h < roadDefs[g]->ourImages.size(); ++h)
  135. {
  136. CSDL_Ext::alphaTransform(roadDefs[g]->ourImages[h].bitmap);
  137. }
  138. }
  139. sizes.x = CGI->mh->map->width;
  140. sizes.y = CGI->mh->map->height;
  141. sizes.z = CGI->mh->map->twoLevel+1;
  142. // Create enough room for the whole map and its frame
  143. ttiles.resize(CGI->mh->map->width, frameW, frameW);
  144. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  145. {
  146. ttiles[i].resize(CGI->mh->map->height, frameH, frameH);
  147. }
  148. for (int i=0-frameW;i<ttiles.size()-frameW;i++)
  149. {
  150. for (int j=0-frameH;j<(int)CGI->mh->map->height+frameH;j++)
  151. ttiles[i][j].resize(CGI->mh->map->twoLevel+1, 0, 0);
  152. }
  153. // Draw the map
  154. for (int i=0; i<map->width; i++) //by width
  155. {
  156. for (int j=0; j<map->height;j++) //by height
  157. {
  158. for (int k=0; k<=map->twoLevel; ++k) //by levels
  159. {
  160. TerrainTile2 &pom(ttiles[i][j][k]);
  161. pom.pos = int3(i, j, k);
  162. pom.tileInfo = &(map->terrain[i][j][k]);
  163. if(pom.tileInfo->malle)
  164. {
  165. bool rotV, rotH;
  166. int roadpom = pom.tileInfo->malle-1,
  167. impom = pom.tileInfo->roadDir;
  168. SDL_Surface *bitmap = roadDefs[roadpom]->ourImages[impom].bitmap;
  169. rotH = (pom.tileInfo->siodmyTajemniczyBajt >> 5) & 1;
  170. rotV = (pom.tileInfo->siodmyTajemniczyBajt >> 4) & 1;
  171. if(rotH || rotV)
  172. {
  173. if(rotH)
  174. bitmap = CSDL_Ext::hFlip(bitmap);
  175. if(rotV)
  176. {
  177. SDL_Surface *bitmap2 = CSDL_Ext::rotate01(bitmap);
  178. if (rotH)
  179. // bitmap is already a duplicated surface
  180. SDL_FreeSurface(bitmap);
  181. bitmap = bitmap2;
  182. }
  183. CSDL_Ext::alphaTransform(bitmap);
  184. }
  185. ttiles[i][j][k].roadbitmap.push_back(bitmap);
  186. }
  187. }
  188. }
  189. }
  190. for (int i=0; i<map->width; i++) //by width
  191. {
  192. for (int j=0; j<map->height;j++) //by height
  193. {
  194. for(int k=0; k<=map->twoLevel; ++k) //by levels
  195. {
  196. if(map->terrain[i][j][k].nuine)
  197. {
  198. bool rotH, rotV;
  199. SDL_Surface *bitmap = staticRiverDefs[map->terrain[i][j][k].nuine-1]->ourImages[map->terrain[i][j][k].rivDir].bitmap;
  200. rotH = (map->terrain[i][j][k].siodmyTajemniczyBajt >> 3) & 1;
  201. rotV = (map->terrain[i][j][k].siodmyTajemniczyBajt >> 2) & 1;
  202. if(rotH || rotV)
  203. {
  204. if(rotH)
  205. bitmap = CSDL_Ext::hFlip(bitmap);
  206. if(rotV)
  207. {
  208. SDL_Surface *bitmap2 = CSDL_Ext::rotate01(bitmap);
  209. if (rotH)
  210. // bitmap is already a duplicated surface
  211. SDL_FreeSurface(bitmap);
  212. bitmap = bitmap2;
  213. }
  214. CSDL_Ext::alphaTransform(bitmap);
  215. }
  216. ttiles[i][j][k].rivbitmap.push_back(bitmap);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. void CMapHandler::borderAndTerrainBitmapInit()
  223. {
  224. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  225. bord->notFreeImgs = true;
  226. terrainGraphics.resize(10);
  227. for (int i = 0; i < 10 ; i++)
  228. {
  229. CDefHandler *hlp = CDefHandler::giveDef(nameFromType(i));
  230. terrainGraphics[i].resize(hlp->ourImages.size());
  231. hlp->notFreeImgs = true;
  232. for(size_t j=0; j < hlp->ourImages.size(); ++j)
  233. terrainGraphics[i][j] = hlp->ourImages[j].bitmap;
  234. delete hlp;
  235. }
  236. for (int i=0-frameW; i<map->width+frameW; i++) //by width
  237. {
  238. for (int j=0-frameH; j<map->height+frameH;j++) //by height
  239. {
  240. for(int k=0; k<=map->twoLevel; ++k) //by levles
  241. {
  242. if(i < 0 || i > (map->width-1) || j < 0 || j > (map->height-1))
  243. {
  244. int terBitmapNum = -1;
  245. if(i==-1 && j==-1)
  246. terBitmapNum = 16;
  247. else if(i==-1 && j==(map->height))
  248. terBitmapNum = 19;
  249. else if(i==(map->width) && j==-1)
  250. terBitmapNum = 17;
  251. else if(i==(map->width) && j==(map->height))
  252. terBitmapNum = 18;
  253. else if(j == -1 && i > -1 && i < map->height)
  254. terBitmapNum = 22+rand()%2;
  255. else if(i == -1 && j > -1 && j < map->height)
  256. terBitmapNum = 33+rand()%2;
  257. else if(j == map->height && i >-1 && i < map->width)
  258. terBitmapNum = 29+rand()%2;
  259. else if(i == map->width && j > -1 && j < map->height)
  260. terBitmapNum = 25+rand()%2;
  261. else
  262. terBitmapNum = rand()%16;
  263. if(terBitmapNum != -1)
  264. {
  265. ttiles[i][j][k].terbitmap = bord->ourImages[terBitmapNum].bitmap;
  266. continue;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. delete bord;
  273. }
  274. void CMapHandler::initObjectRects()
  275. {
  276. //initializing objects / rects
  277. for(size_t f=0; f < map->objects.size(); ++f)
  278. {
  279. const CGObjectInstance *obj = map->objects[f];
  280. if( !obj
  281. || obj->ID==HEROI_TYPE && static_cast<const CGHeroInstance*>(obj)->inTownGarrison //garrisoned hero
  282. || obj->ID==8 && static_cast<const CGBoat*>(obj)->hero //boat wih hero (hero graphics is used)
  283. || !obj->defInfo
  284. || !obj->defInfo->handler) //no graphic...
  285. {
  286. continue;
  287. }
  288. const SDL_Surface *bitmap = obj->defInfo->handler->ourImages[0].bitmap;
  289. for(int fx=0; fx<bitmap->w>>5; ++fx) //bitmap->w/32
  290. {
  291. for(int fy=0; fy<bitmap->h>>5; ++fy) //bitmap->h/32
  292. {
  293. SDL_Rect cr;
  294. cr.w = 32;
  295. cr.h = 32;
  296. cr.x = fx<<5; //fx*32
  297. cr.y = fy<<5; //fy*32
  298. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj,cr);
  299. if( (obj->pos.x + fx - bitmap->w/32+1) >= 0
  300. && (obj->pos.x + fx - bitmap->w/32+1) < ttiles.size() - frameW
  301. && (obj->pos.y + fy - bitmap->h/32+1) >= 0
  302. && (obj->pos.y + fy - bitmap->h/32+1) < ttiles[0].size() - frameH
  303. )
  304. {
  305. //TerrainTile2 & curt =
  306. // ttiles
  307. // [obj->pos.x + fx - bitmap->w/32]
  308. //[obj->pos.y + fy - bitmap->h/32]
  309. //[obj->pos.z];
  310. ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd);
  311. }
  312. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  313. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  314. } // for(int f=0; f<map->objects.size(); ++f)
  315. for(int ix=0; ix<ttiles.size()-frameW; ++ix)
  316. {
  317. for(int iy=0; iy<ttiles[0].size()-frameH; ++iy)
  318. {
  319. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  320. {
  321. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  322. }
  323. }
  324. }
  325. }
  326. static void processDef (CGDefInfo* def)
  327. {
  328. if(def->id == 26) //if it's event, return from function
  329. return;
  330. if(!def->handler) //if object has already set handler (eg. heroes) it should not be overwritten
  331. {
  332. if(def->name.size())
  333. {
  334. def->handler = CDefHandler::giveDefEss(def->name);
  335. }
  336. else
  337. {
  338. tlog2 << "No def name for " << def->id << " " << def->subid << std::endl;
  339. def->handler = NULL;
  340. return;
  341. }
  342. def->width = def->handler->ourImages[0].bitmap->w/32;
  343. def->height = def->handler->ourImages[0].bitmap->h/32;
  344. }
  345. CGDefInfo* pom = CGI->dobjinfo->gobjs[def->id][def->subid];
  346. if(pom && def->id!=TOWNI_TYPE)
  347. {
  348. pom->handler = def->handler;
  349. pom->width = pom->handler->ourImages[0].bitmap->w/32;
  350. pom->height = pom->handler->ourImages[0].bitmap->h/32;
  351. }
  352. else if(def->id != HEROI_TYPE && def->id != TOWNI_TYPE)
  353. tlog3 << "\t\tMinor warning: lacking def info for " << def->id << " " << def->subid <<" " << def->name << std::endl;
  354. //alpha transformation
  355. for(size_t yy=0; yy < def->handler->ourImages.size(); ++yy)
  356. {
  357. CSDL_Ext::alphaTransform(def->handler->ourImages[yy].bitmap);
  358. }
  359. }
  360. void CMapHandler::initHeroDef(CGHeroInstance * h)
  361. {
  362. h->defInfo->handler = graphics->flags1[0];
  363. h->defInfo->width = h->defInfo->handler->ourImages[0].bitmap->w/32;
  364. h->defInfo->height = h->defInfo->handler->ourImages[0].bitmap->h/32;
  365. }
  366. void CMapHandler::init()
  367. {
  368. timeHandler th;
  369. th.getDif();
  370. CGI->dobjinfo->gobjs[8][0]->handler = graphics->boatAnims[0];
  371. CGI->dobjinfo->gobjs[8][1]->handler = graphics->boatAnims[1];
  372. CGI->dobjinfo->gobjs[8][2]->handler = graphics->boatAnims[2];
  373. // Size of visible terrain.
  374. mapW = conf.go()->ac.advmapW;
  375. mapH = conf.go()->ac.advmapH;
  376. // Total number of visible tiles. Substract the center tile, then
  377. // compute the number of tiles on each side, and reassemble.
  378. int t1, t2;
  379. t1 = (mapW-32)/2;
  380. t2 = mapW - 32 - t1;
  381. tilesW = 1 + (t1+31)/32 + (t2+31)/32;
  382. t1 = (mapH-32)/2;
  383. t2 = mapH - 32 - t1;
  384. tilesH = 1 + (t1+31)/32 + (t2+31)/32;
  385. // Size of the frame around the map. In extremes positions, the
  386. // frame must not be on the center of the map, but right on the
  387. // edge of the center tile.
  388. frameW = (mapW+31) /32 / 2;
  389. frameH = (mapH+31) /32 / 2;
  390. offsetX = (mapW - (2*frameW+1)*32)/2;
  391. offsetY = (mapH - (2*frameH+1)*32)/2;
  392. std::ifstream ifs("config/townsDefs.txt");
  393. int ccc;
  394. ifs>>ccc;
  395. for(int i=0;i<ccc*2;i++)
  396. {
  397. CGDefInfo *n;
  398. if(i<ccc)
  399. {
  400. n = CGI->state->villages[i];
  401. map->defy.push_back(CGI->state->forts[i]);
  402. }
  403. else
  404. n = CGI->state->capitols[i%ccc];
  405. ifs >> n->name;
  406. if(!n)
  407. tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  408. else
  409. map->defy.push_back(n);
  410. }
  411. tlog0<<"\tLoading town def info: "<<th.getDif()<<std::endl;
  412. for(int i=0;i<map->heroes.size();i++)
  413. {
  414. if(!map->heroes[i]->defInfo->handler)
  415. {
  416. initHeroDef(map->heroes[i]);
  417. }
  418. }
  419. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  420. tlog0<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  421. for(int i=0;i<PLAYER_LIMIT;i++)
  422. {
  423. for(size_t j=0; j < map->players[i].heroesNames.size(); ++j)
  424. {
  425. usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  426. }
  427. }
  428. tlog0<<"\tChecking used heroes: "<<th.getDif()<<std::endl;
  429. for(size_t h=0; h<map->defy.size(); ++h) //initializing loaded def handler's info {
  430. CGI->mh->loadedDefs.insert(std::make_pair(map->defy[h]->name, map->defy[h]->handler));
  431. tlog0<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  432. prepareFOWDefs();
  433. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  434. borderAndTerrainBitmapInit();
  435. tlog0<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  436. initObjectRects();
  437. tlog0<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  438. }
  439. // Update map window screen
  440. // top_tile top left tile to draw. Not necessarily visible.
  441. // extRect, extRect = map window on screen
  442. // moveX, moveY: when a hero is in movement indicates how to shift the map. Range is -31 to + 31.
  443. void CMapHandler::terrainRect(int3 top_tile, unsigned char anim, const std::vector< std::vector< std::vector<unsigned char> > > * visibilityMap, bool otherHeroAnim, unsigned char heroAnim, SDL_Surface * extSurf, const SDL_Rect * extRect, int moveX, int moveY, bool puzzleMode)
  444. {
  445. // Width and height of the portion of the map to process. Units in tiles.
  446. unsigned int dx = tilesW;
  447. unsigned int dy = tilesH;
  448. // Basic rectangle for a tile. Should be a const but conflicts with SDL headers
  449. SDL_Rect rtile = { 0, 0, 32, 32 };
  450. // Absolute coords of the first pixel in the top left corner
  451. int srx_init = offsetX + extRect->x;
  452. int sry_init = offsetY + extRect->y;
  453. int srx, sry; // absolute screen coordinates in pixels
  454. // If moving, we need to add an extra column/line
  455. if (moveX != 0) {
  456. dx++;
  457. srx_init += moveX;
  458. if (moveX > 0) {
  459. // Moving right. We still need to draw the old tile on the
  460. // left, so adjust our referential
  461. top_tile.x --;
  462. srx_init -= 32;
  463. }
  464. }
  465. if (moveY != 0) {
  466. dy++;
  467. sry_init += moveY;
  468. if (moveY > 0) {
  469. // Moving down. We still need to draw the tile on the top,
  470. // so adjust our referential.
  471. top_tile.y --;
  472. sry_init -= 32;
  473. }
  474. }
  475. // Reduce sizes if we go out of the full map.
  476. if (top_tile.x < -frameW)
  477. top_tile.x = -frameW;
  478. if (top_tile.y < -frameH)
  479. top_tile.y = -frameH;
  480. if (top_tile.x + dx > map->width + frameW)
  481. dx = map->width + frameW - top_tile.x;
  482. if (top_tile.y + dy > map->height + frameH)
  483. dy = map->height + frameH - top_tile.y;
  484. if(!otherHeroAnim)
  485. heroAnim = anim; //the same, as it should be
  486. SDL_Rect prevClip;
  487. SDL_GetClipRect(extSurf, &prevClip);
  488. SDL_SetClipRect(extSurf, extRect); //preventing blitting outside of that rect
  489. // Temp surface for rotating tile and then properly blit. The
  490. // problem is that blitWithRotate1clip & co do not respect the
  491. // previous SDL_SetClipRect.
  492. // TODO: optimize by pre-rotating those surfaces
  493. SDL_Surface *rSurf = CSDL_Ext::newSurface(32, 32, extSurf);
  494. // printing terrain
  495. srx = srx_init;
  496. for (int bx = 0; bx < dx; bx++, srx+=32)
  497. {
  498. // Skip column if not in map
  499. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  500. continue;
  501. sry = sry_init;
  502. for (int by=0; by < dy; by++, sry+=32)
  503. {
  504. // Skip tile if not in map
  505. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  506. continue;
  507. const TerrainTile2 & tile = ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z];
  508. SDL_Rect sr;
  509. sr.x=srx;
  510. sr.y=sry;
  511. sr.h=sr.w=32;
  512. if(tile.terbitmap)
  513. {
  514. SDL_BlitSurface(tile.terbitmap, &genRect(sr.h, sr.w, 0, 0), extSurf, &sr);
  515. }
  516. else
  517. {
  518. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  519. {
  520. case 0:
  521. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  522. &rtile, extSurf, &sr);
  523. break;
  524. case 1:
  525. CSDL_Ext::blitWithRotate1clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  526. &rtile, rSurf, &rtile);
  527. SDL_BlitSurface(rSurf, &rtile, extSurf, &sr);
  528. break;
  529. case 2:
  530. CSDL_Ext::blitWithRotate2clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  531. &rtile, rSurf, &rtile);
  532. SDL_BlitSurface(rSurf, &rtile, extSurf, &sr);
  533. break;
  534. default:
  535. CSDL_Ext::blitWithRotate3clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  536. &rtile, rSurf, &rtile);
  537. SDL_BlitSurface(rSurf, &rtile, extSurf, &sr);
  538. break;
  539. }
  540. }
  541. }
  542. }
  543. // terrain printed
  544. // printing rivers
  545. srx = srx_init;
  546. for (int bx = 0; bx<dx; bx++, srx+=32)
  547. {
  548. // Skip column if not in map
  549. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  550. continue;
  551. sry = sry_init;
  552. for (int by= 0; by<dy; by++, sry+=32)
  553. {
  554. // Skip tile if not in map
  555. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  556. continue;
  557. SDL_Rect sr;
  558. sr.x=srx;
  559. sr.y=sry;
  560. sr.h=sr.w=32;
  561. const std::vector<SDL_Surface *> &rivbitmap = ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].rivbitmap;
  562. if(rivbitmap.size())
  563. {
  564. CSDL_Ext::blit8bppAlphaTo24bpp(rivbitmap[anim%rivbitmap.size()],&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  565. }
  566. }
  567. }
  568. // rivers printed
  569. // printing roads
  570. srx = srx_init;
  571. for (int bx = 0; bx < dx; bx++, srx+=32)
  572. {
  573. // Skip column if not in map
  574. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  575. continue;
  576. sry = sry_init + 16;
  577. for (int by = 0; by < dy; by++, sry+=32)
  578. {
  579. // Skip tile if not in map
  580. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  581. continue;
  582. SDL_Rect sr;
  583. sr.x = srx;
  584. sr.y = sry;
  585. sr.h=sr.w=32;
  586. const std::vector<SDL_Surface *> &roadbitmap = ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].roadbitmap;
  587. if(roadbitmap.size())
  588. {
  589. CSDL_Ext::blit8bppAlphaTo24bpp(roadbitmap[anim%roadbitmap.size()], &genRect(sr.h, sr.w, 0, (by==-1 && moveY == 0 ? 16 : 0)),extSurf,&sr);
  590. }
  591. }
  592. }
  593. // roads printed
  594. // printing objects
  595. srx = srx_init;
  596. for (int bx = 0; bx<dx; bx++, srx+=32)
  597. {
  598. // Skip column if not in map
  599. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  600. continue;
  601. sry = sry_init;
  602. for (int by = 0; by<dy; by++, sry+=32)
  603. {
  604. // Skip tile if not in map
  605. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  606. continue;
  607. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > &objects =
  608. ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].objects;
  609. for(int h=0; h < objects.size(); ++h)
  610. {
  611. const CGObjectInstance *obj = objects[h].first;
  612. ui8 color = obj->tempOwner;
  613. //checking if object has non-empty graphic on this tile
  614. if(obj->ID != HEROI_TYPE && !obj->coveringAt(obj->pos.x - (top_tile.x + bx), top_tile.y + by - obj->pos.y + 5))
  615. continue;
  616. //don't print flaggable objects in puzzle mode
  617. if(puzzleMode && obj->tempOwner != 254)
  618. continue;
  619. SDL_Rect sr;
  620. sr.x = srx;
  621. sr.y = sry;
  622. sr.w = sr.h = 32;
  623. SDL_Rect pp = objects[h].second;
  624. pp.h = sr.h;
  625. pp.w = sr.w;
  626. const CGHeroInstance * themp = (obj->ID != HEROI_TYPE
  627. ? NULL
  628. : static_cast<const CGHeroInstance*>(obj));
  629. //print hero / boat and flag
  630. if(themp && themp->moveDir && themp->type || obj->ID == 8) //it's hero or boat
  631. {
  632. const int IMGVAL = 8; //frames per group of movement animation
  633. ui8 dir;
  634. std::vector<Cimage> * iv = NULL;
  635. std::vector<CDefEssential *> Graphics::*flg = NULL;
  636. SDL_Surface * tb; //surface to blitted
  637. if(themp) //hero
  638. {
  639. dir = themp->moveDir;
  640. //pick graphics of hero (or boat if hero is sailing)
  641. iv = (themp->boat)
  642. ? &graphics->boatAnims[themp->boat->subID]->ourImages
  643. : &graphics->heroAnims[themp->type->heroType]->ourImages;
  644. //pick appropriate flag set
  645. if(themp->boat)
  646. {
  647. switch (themp->boat->subID)
  648. {
  649. case 0: flg = &Graphics::flags1; break;
  650. case 1: flg = &Graphics::flags2; break;
  651. case 2: flg = &Graphics::flags3; break;
  652. default: tlog1 << "Not supported boat subtype: " << themp->boat->subID << std::endl;
  653. }
  654. }
  655. else
  656. {
  657. flg = &Graphics::flags4;
  658. }
  659. }
  660. else //boat
  661. {
  662. const CGBoat *boat = static_cast<const CGBoat*>(obj);
  663. dir = boat->direction;
  664. iv = &graphics->boatAnims[boat->subID]->ourImages;
  665. }
  666. if(themp && !themp->isStanding) //hero is moving
  667. {
  668. size_t gg;
  669. for(gg=0; gg<iv->size(); ++gg)
  670. {
  671. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, true))
  672. {
  673. tb = (*iv)[gg+heroAnim%IMGVAL].bitmap;
  674. break;
  675. }
  676. }
  677. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr);
  678. //printing flag
  679. pp.y+=IMGVAL*2-32;
  680. sr.y-=16;
  681. SDL_BlitSurface((graphics->*flg)[color]->ourImages[gg+heroAnim%IMGVAL+35].bitmap, &pp, extSurf, &sr);
  682. }
  683. else //hero / boat stands still
  684. {
  685. size_t gg;
  686. for(gg=0; gg < iv->size(); ++gg)
  687. {
  688. if((*iv)[gg].groupNumber==getHeroFrameNum(dir, false))
  689. {
  690. tb = (*iv)[gg].bitmap;
  691. break;
  692. }
  693. }
  694. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,extSurf,&sr);
  695. //printing flag
  696. if(flg
  697. && obj->pos.x == top_tile.x + bx
  698. && obj->pos.y == top_tile.y + by)
  699. {
  700. SDL_Rect bufr = sr;
  701. bufr.x-=2*32;
  702. bufr.y-=1*32;
  703. bufr.h = 64;
  704. bufr.w = 96;
  705. if(bufr.x-extRect->x>-64)
  706. SDL_BlitSurface((graphics->*flg)[color]->ourImages[getHeroFrameNum(dir, false) *8+(heroAnim/4)%IMGVAL].bitmap, NULL, extSurf, &bufr);
  707. }
  708. }
  709. }
  710. else //blit normal object
  711. {
  712. const std::vector<Cimage> &ourImages = obj->defInfo->handler->ourImages;
  713. SDL_Surface *bitmap = ourImages[(anim+obj->animPhaseShift)%ourImages.size()].bitmap;
  714. //setting appropriate flag color
  715. if(color < 8 || color==255)
  716. CSDL_Ext::setPlayerColor(bitmap, color);
  717. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,extSurf,&sr);
  718. }
  719. }
  720. }
  721. }
  722. // objects printed
  723. // printing shadow
  724. if(!puzzleMode)
  725. {
  726. srx = srx_init;
  727. for (int bx = 0; bx<dx; bx++, srx+=32)
  728. {
  729. // Skip column if not in map
  730. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width)
  731. continue;
  732. sry = sry_init;
  733. for (int by = 0; by<dy; by++, sry+=32)
  734. {
  735. // Skip tile if not in map
  736. if (top_tile.y+by < 0 || top_tile.y+by >= map->height)
  737. continue;
  738. SDL_Rect sr;
  739. sr.x = srx;
  740. sr.y = sry;
  741. sr.h = sr.w = 32;
  742. if (top_tile.x+bx >= 0 &&
  743. top_tile.y+by >= 0 &&
  744. top_tile.x+bx < CGI->mh->map->width &&
  745. top_tile.y+by < CGI->mh->map->height &&
  746. !(*visibilityMap)[top_tile.x+bx][top_tile.y+by][top_tile.z])
  747. {
  748. SDL_Surface * hide = getVisBitmap(top_tile.x+bx, top_tile.y+by, *visibilityMap, top_tile.z);
  749. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &rtile, extSurf, &sr);
  750. }
  751. }
  752. }
  753. }
  754. // shadow printed
  755. // printing borders
  756. srx = srx_init;
  757. for (int bx = 0; bx < dx; bx++, srx+=32)
  758. {
  759. sry = sry_init;
  760. for (int by = 0; by<dy; by++, sry+=32)
  761. {
  762. if (top_tile.x+bx < 0 || top_tile.x+bx >= map->width ||
  763. top_tile.y+by < 0 || top_tile.y+by >= map->height) {
  764. SDL_Rect sr;
  765. sr.x=srx;
  766. sr.y=sry;
  767. sr.h=sr.w=32;
  768. SDL_BlitSurface(ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].terbitmap,
  769. &genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  770. } else {
  771. // TODO: these should be activable by the console
  772. #ifdef MARK_BLOCKED_POSITIONS
  773. if(ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].tileInfo->blocked) //temporary hiding blocked positions
  774. {
  775. SDL_Rect sr;
  776. sr.x=srx;
  777. sr.y=sry;
  778. sr.h=sr.w=32;
  779. memset(rSurf->pixels, 128, rSurf->pitch * rSurf->h);
  780. SDL_BlitSurface(rSurf,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  781. }
  782. #endif
  783. #ifdef MARK_VISITABLE_POSITIONS
  784. if(ttiles[top_tile.x+bx][top_tile.y+by][top_tile.z].tileInfo->visitable) //temporary hiding visitable positions
  785. {
  786. SDL_Rect sr;
  787. sr.x=srx;
  788. sr.y=sry;
  789. sr.h=sr.w=32;
  790. memset(rSurf->pixels, 128, rSurf->pitch * rSurf->h);
  791. SDL_BlitSurface(rSurf,&genRect(sr.h, sr.w, 0, 0),extSurf,&sr);
  792. }
  793. #endif
  794. }
  795. }
  796. }
  797. // borders printed
  798. #ifdef MARK_GRID_POSITIONS
  799. // print grid
  800. // TODO: This option should be activated by the console.
  801. srx = srx_init;
  802. for (int bx = 0; bx < dx; bx++, srx+=32)
  803. {
  804. sry = sry_init;
  805. for (int by = 0; by<dy; by++, sry+=32)
  806. {
  807. SDL_Rect sr;
  808. sr.x=srx;
  809. sr.y=sry;
  810. sr.h=sr.w=32;
  811. const int3 color(0x555555, 0x555555, 0x555555);
  812. if (sr.y >= extRect->y &&
  813. sr.y < extRect->y+extRect->h)
  814. for(int i=0;i<sr.w;i++)
  815. if (sr.x+i >= extRect->x &&
  816. sr.x+i < extRect->x+extRect->w)
  817. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x+i,sr.y,color.x,color.y,color.z);
  818. if (sr.x >= extRect->x &&
  819. sr.x < extRect->x+extRect->w)
  820. for(int i=0; i<sr.h;i++)
  821. if (sr.y+i >= extRect->y &&
  822. sr.y+i < extRect->y+extRect->h)
  823. CSDL_Ext::SDL_PutPixelWithoutRefresh(extSurf,sr.x,sr.y+i,color.x,color.y,color.z);
  824. }
  825. }
  826. // grid
  827. #endif
  828. //applying sepia / gray effect
  829. if(puzzleMode)
  830. {
  831. if(ADVOPT.puzzleSepia)
  832. {
  833. const int sepiaDepth = 20;
  834. const int sepiaIntensity = 30;
  835. for(int xp = extRect->x; xp < extRect->x + extRect->w; ++xp)
  836. {
  837. for(int yp = extRect->y; yp < extRect->y + extRect->h; ++yp)
  838. {
  839. unsigned char * pixels = (unsigned char*)extSurf->pixels + yp * extSurf->pitch + xp * extSurf->format->BytesPerPixel;
  840. int b = pixels[0];
  841. int g = pixels[1];
  842. int r = pixels[2];
  843. int gry = (r + g + b) / 3;
  844. r = g = b = gry;
  845. r = r + (sepiaDepth * 2);
  846. g = g + sepiaDepth;
  847. if (r>255) r=255;
  848. if (g>255) g=255;
  849. if (b>255) b=255;
  850. // Darken blue color to increase sepia effect
  851. b -= sepiaIntensity;
  852. // normalize if out of bounds
  853. if (b<0) b=0;
  854. if (b>255) b=255;
  855. pixels[0] = b;
  856. pixels[1] = g;
  857. pixels[2] = r;
  858. }
  859. }
  860. }
  861. else
  862. {
  863. for(int xp = extRect->x; xp < extRect->x + extRect->w; ++xp)
  864. {
  865. for(int yp = extRect->y; yp < extRect->y + extRect->h; ++yp)
  866. {
  867. unsigned char * pixels = (unsigned char*)extSurf->pixels + yp * extSurf->pitch + xp * extSurf->format->BytesPerPixel;
  868. int b = pixels[0];
  869. int g = pixels[1];
  870. int r = pixels[2];
  871. int gry = (r + g + b) / 3;
  872. pixels[0] = pixels[1] = pixels[2] = gry;
  873. }
  874. }
  875. }
  876. }
  877. //sepia / gray effect applied
  878. SDL_SetClipRect(extSurf, &prevClip); //restoring clip_rect
  879. SDL_FreeSurface(rSurf);
  880. }
  881. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, const std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)
  882. {
  883. int size = visibilityMap.size()-1; //is tile visible. arrangement: (like num keyboard)
  884. bool d7 = (x>0 && y>0) ? visibilityMap[x-1][y-1][lvl] : 0, //789
  885. d8 = (y>0) ? visibilityMap[x][y-1][lvl] : 0, //456
  886. d9 = (y>0 && x<size) ? visibilityMap[x+1][y-1][lvl] : 0,//123
  887. d4 = (x>0) ? visibilityMap[x-1][y][lvl] : 0,
  888. d5 = visibilityMap[x][y][lvl], //TODO use me - OMFG
  889. d6 = (x<size) ? visibilityMap[x+1][y][lvl] : 0,
  890. d1 = (x>0 && y<size) ? visibilityMap[x-1][y+1][lvl] : 0,
  891. d2 = (y<size) ? visibilityMap[x][y+1][lvl] : 0,
  892. d3 = (x<size && y<size) ? visibilityMap[x+1][y+1][lvl] : 0;
  893. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && !d1)
  894. {
  895. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  896. }
  897. else if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1)
  898. {
  899. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  900. }
  901. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1)
  902. {
  903. return partialHide->ourImages[15].bitmap; //visible right top corner
  904. }
  905. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1)
  906. {
  907. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  908. }
  909. else if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && !d1)
  910. {
  911. return partialHide->ourImages[35].bitmap; //visible left top corner
  912. }
  913. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && d9 && !d1)
  914. {
  915. return partialHide->ourImages[0].bitmap; //visible top
  916. }
  917. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  918. {
  919. return partialHide->ourImages[4].bitmap; //visble bottom
  920. }
  921. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && d1)
  922. {
  923. return partialHide->ourImages[36].bitmap; //visible left
  924. }
  925. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  926. {
  927. return partialHide->ourImages[2].bitmap; //visible right
  928. }
  929. else if(d2 && d6 && !d4 && !d8 && !d7)
  930. {
  931. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  932. }
  933. else if(!d2 && d6 && !d4 && d8 && !d1)
  934. {
  935. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  936. }
  937. else if(!d2 && !d6 && d4 && d8 && !d3)
  938. {
  939. return partialHide->ourImages[37].bitmap; //visible top, top - left, left; right bottom corner hidden
  940. }
  941. else if(d2 && !d6 && d4 && !d8 && !d9)
  942. {
  943. return partialHide->ourImages[38].bitmap; //visible left, left - bottom, bottom; right top corner hidden
  944. }
  945. else if(d2 && d6 && d4 && d8)
  946. {
  947. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  948. }
  949. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  950. {
  951. return partialHide->ourImages[16].bitmap; //visible right corners
  952. }
  953. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && !d1)
  954. {
  955. return partialHide->ourImages[18].bitmap; //visible top corners
  956. }
  957. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && d1)
  958. {
  959. return partialHide->ourImages[39].bitmap; //visible left corners
  960. }
  961. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  962. {
  963. return partialHide->ourImages[40].bitmap; //visible bottom corners
  964. }
  965. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && d1)
  966. {
  967. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  968. }
  969. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && !d1)
  970. {
  971. return partialHide->ourImages[41].bitmap; //visible top - left and bottom - right corners
  972. }
  973. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && d1)
  974. {
  975. return partialHide->ourImages[19].bitmap; //visible corners without left top
  976. }
  977. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && !d1)
  978. {
  979. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  980. }
  981. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && d1)
  982. {
  983. return partialHide->ourImages[42].bitmap; //visible corners without right bottom
  984. }
  985. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && d1)
  986. {
  987. return partialHide->ourImages[43].bitmap; //visible corners without right top
  988. }
  989. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && d1)
  990. {
  991. return partialHide->ourImages[21].bitmap; //visible all corners only
  992. }
  993. if(d2 && d6 && d4 && !d8)
  994. {
  995. return partialHide->ourImages[6].bitmap; //hidden top
  996. }
  997. if(d2 && !d6 && d4 && d8)
  998. {
  999. return partialHide->ourImages[7].bitmap; //hidden right
  1000. }
  1001. if(!d2 && d6 && d4 && d8)
  1002. {
  1003. return partialHide->ourImages[8].bitmap; //hidden bottom
  1004. }
  1005. if(d2 && d6 && !d4 && d8)
  1006. {
  1007. return partialHide->ourImages[44].bitmap; //hidden left
  1008. }
  1009. if(!d2 && d6 && d4 && !d8)
  1010. {
  1011. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  1012. }
  1013. if(d2 && !d6 && !d4 && d8)
  1014. {
  1015. return partialHide->ourImages[29].bitmap; //hidden left and right
  1016. }
  1017. if(!d2 && !d6 && !d4 && d8 && d3 && !d1)
  1018. {
  1019. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  1020. }
  1021. if(!d2 && !d6 && !d4 && d8 && !d3 && d1)
  1022. {
  1023. return partialHide->ourImages[45].bitmap; //visible top and left bottom corner
  1024. }
  1025. if(!d2 && !d6 && !d4 && d8 && d3 && d1)
  1026. {
  1027. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1028. }
  1029. if(!d2 && !d6 && d4 && !d8 && !d3 && d9)
  1030. {
  1031. return partialHide->ourImages[46].bitmap; //visible left and right top corner
  1032. }
  1033. if(!d2 && !d6 && d4 && !d8 && d3 && !d9)
  1034. {
  1035. return partialHide->ourImages[47].bitmap; //visible left and right bottom corner
  1036. }
  1037. if(!d2 && !d6 && d4 && !d8 && d3 && d9)
  1038. {
  1039. return partialHide->ourImages[32].bitmap; //visible left and right corners
  1040. }
  1041. if(d2 && !d6 && !d4 && !d8 && d7 && !d9)
  1042. {
  1043. return partialHide->ourImages[48].bitmap; //visible bottom and left top corner
  1044. }
  1045. if(d2 && !d6 && !d4 && !d8 && !d7 && d9)
  1046. {
  1047. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  1048. }
  1049. if(d2 && !d6 && !d4 && !d8 && d7 && d9)
  1050. {
  1051. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1052. }
  1053. if(!d2 && d6 && !d4 && !d8 && !d7 && d1)
  1054. {
  1055. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  1056. }
  1057. if(!d2 && d6 && !d4 && !d8 && d7 && !d1)
  1058. {
  1059. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  1060. }
  1061. if(!d2 && d6 && !d4 && !d8 && d7 && d1)
  1062. {
  1063. return partialHide->ourImages[49].bitmap; //visible right and left cornres
  1064. }
  1065. if(d2 && d6 && !d4 && !d8 && d7)
  1066. {
  1067. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1068. }
  1069. else if(!d2 && d6 && !d4 && d8 && d1)
  1070. {
  1071. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1072. }
  1073. else if(!d2 && !d6 && d4 && d8 && d3)
  1074. {
  1075. return partialHide->ourImages[50].bitmap; //visible top, top - left, left; right bottom corner visible
  1076. }
  1077. else if(d2 && !d6 && d4 && !d8 && d9)
  1078. {
  1079. return partialHide->ourImages[51].bitmap; //visible left, left - bottom, bottom; right top corner visible
  1080. }
  1081. //newly added
  1082. else if(!d2 && !d6 && !d4 && d8 && !d7 && !d3 && d9 && !d1) //visible t and tr
  1083. {
  1084. return partialHide->ourImages[0].bitmap;
  1085. }
  1086. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && !d9 && !d1) //visible t and tl
  1087. {
  1088. return partialHide->ourImages[1].bitmap;
  1089. }
  1090. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible b and br
  1091. {
  1092. return partialHide->ourImages[4].bitmap;
  1093. }
  1094. else if(d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible b and bl
  1095. {
  1096. return partialHide->ourImages[5].bitmap;
  1097. }
  1098. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && !d1) //visible l and tl
  1099. {
  1100. return partialHide->ourImages[36].bitmap;
  1101. }
  1102. else if(!d2 && !d6 && d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible l and bl
  1103. {
  1104. return partialHide->ourImages[36].bitmap;
  1105. }
  1106. else if(!d2 && d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1) //visible r and tr
  1107. {
  1108. return partialHide->ourImages[2].bitmap;
  1109. }
  1110. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible r and br
  1111. {
  1112. return partialHide->ourImages[3].bitmap;
  1113. }
  1114. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1115. }
  1116. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1117. {
  1118. int ret=-1;
  1119. if(a.x>=CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1120. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1121. else if(a.x>=CGI->mh->map->width && a.y<CGI->mh->map->height)
  1122. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][a.y][a.z].tileInfo->malle];
  1123. else if(a.x<CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1124. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1125. else
  1126. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].tileInfo->malle];
  1127. if(!(a.x==b.x || a.y==b.y))
  1128. ret*=1.41421;
  1129. //TODO: use hero's pathfinding skill during calculating cost
  1130. return ret;
  1131. }
  1132. //std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1133. //{
  1134. // std::vector < CGObjectInstance * > ret;
  1135. // for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1136. // {
  1137. // CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1138. // if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1139. // ret.push_back(curi);
  1140. // }
  1141. // return ret;
  1142. //}
  1143. std::string CMapHandler::getDefName(int id, int subid)
  1144. {
  1145. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1146. if(temp)
  1147. return temp->name;
  1148. throw std::string("Def not found.");
  1149. }
  1150. bool CMapHandler::printObject(const CGObjectInstance *obj)
  1151. {
  1152. const SDL_Surface *bitmap = obj->defInfo->handler->ourImages[0].bitmap;
  1153. for(int fx=0; fx<bitmap->w/32; ++fx)
  1154. {
  1155. for(int fy=0; fy<bitmap->h/32; ++fy)
  1156. {
  1157. SDL_Rect cr;
  1158. cr.w = 32;
  1159. cr.h = 32;
  1160. cr.x = fx*32;
  1161. cr.y = fy*32;
  1162. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1163. if((obj->pos.x + fx - bitmap->w/32+1)>=0 && (obj->pos.x + fx - bitmap->w/32+1)<ttiles.size()-frameW && (obj->pos.y + fy - bitmap->h/32+1)>=0 && (obj->pos.y + fy - bitmap->h/32+1)<ttiles[0].size()-frameH)
  1164. {
  1165. TerrainTile2 & curt = //TODO use me
  1166. ttiles
  1167. [obj->pos.x + fx - bitmap->w/32]
  1168. [obj->pos.y + fy - bitmap->h/32]
  1169. [obj->pos.z];
  1170. ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects.push_back(toAdd);
  1171. }
  1172. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  1173. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  1174. return true;
  1175. }
  1176. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1177. {
  1178. CDefEssential * curd = obj->defInfo->handler;
  1179. if(!curd) return false;
  1180. const SDL_Surface *bitmap = curd->ourImages[0].bitmap;
  1181. for(int fx=0; fx<bitmap->w/32; ++fx)
  1182. {
  1183. for(int fy=0; fy<bitmap->h/32; ++fy)
  1184. {
  1185. if((obj->pos.x + fx - bitmap->w/32+1)>=0 && (obj->pos.x + fx - bitmap->w/32+1)<ttiles.size()-frameW && (obj->pos.y + fy - bitmap->h/32+1)>=0 && (obj->pos.y + fy - bitmap->h/32+1)<ttiles[0].size()-frameH)
  1186. {
  1187. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & ctile = ttiles[obj->pos.x + fx - bitmap->w/32+1][obj->pos.y + fy - bitmap->h/32+1][obj->pos.z].objects;
  1188. for(size_t dd=0; dd < ctile.size(); ++dd)
  1189. {
  1190. if(ctile[dd].first->id==obj->id)
  1191. ctile.erase(ctile.begin() + dd);
  1192. }
  1193. }
  1194. } // for(int fy=0; fy<bitmap->h/32; ++fy)
  1195. } //for(int fx=0; fx<bitmap->w/32; ++fx)
  1196. return true;
  1197. }
  1198. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1199. {
  1200. hideObject(obj);
  1201. return true;
  1202. }
  1203. unsigned char CMapHandler::getHeroFrameNum(unsigned char dir, bool isMoving) const
  1204. {
  1205. if(isMoving)
  1206. {
  1207. switch(dir)
  1208. {
  1209. case 1:
  1210. return 10;
  1211. case 2:
  1212. return 5;
  1213. case 3:
  1214. return 6;
  1215. case 4:
  1216. return 7;
  1217. case 5:
  1218. return 8;
  1219. case 6:
  1220. return 9;
  1221. case 7:
  1222. return 12;
  1223. case 8:
  1224. return 11;
  1225. default:
  1226. throw std::string("Something very wrong1.");
  1227. }
  1228. }
  1229. else //if(isMoving)
  1230. {
  1231. switch(dir)
  1232. {
  1233. case 1:
  1234. return 13;
  1235. case 2:
  1236. return 0;
  1237. case 3:
  1238. return 1;
  1239. case 4:
  1240. return 2;
  1241. case 5:
  1242. return 3;
  1243. case 6:
  1244. return 4;
  1245. case 7:
  1246. return 15;
  1247. case 8:
  1248. return 14;
  1249. default:
  1250. throw std::string("Something very wrong2.");
  1251. }
  1252. }
  1253. }
  1254. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1255. {
  1256. if(ext)
  1257. {
  1258. if(val->x<0)
  1259. {
  1260. val->w += val->x;
  1261. val->x = ext->x;
  1262. }
  1263. else
  1264. {
  1265. val->x += ext->x;
  1266. }
  1267. if(val->y<0)
  1268. {
  1269. val->h += val->y;
  1270. val->y = ext->y;
  1271. }
  1272. else
  1273. {
  1274. val->y += ext->y;
  1275. }
  1276. if(val->x+val->w > ext->x+ext->w)
  1277. {
  1278. val->w = ext->x+ext->w-val->x;
  1279. }
  1280. if(val->y+val->h > ext->y+ext->h)
  1281. {
  1282. val->h = ext->y+ext->h-val->y;
  1283. }
  1284. //for sign problems
  1285. if(val->h > 20000 || val->w > 20000)
  1286. {
  1287. val->h = val->w = 0;
  1288. }
  1289. }
  1290. }
  1291. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1292. {
  1293. if(a.z!=b.z)
  1294. return -1; //error!
  1295. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1296. return 0;
  1297. else if(a.x==b.x && a.y==b.y+1) //t
  1298. return 1;
  1299. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1300. return 2;
  1301. else if(a.x==b.x-1 && a.y==b.y) //r
  1302. return 3;
  1303. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1304. return 4;
  1305. else if(a.x==b.x && a.y==b.y-1) //b
  1306. return 5;
  1307. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1308. return 6;
  1309. else if(a.x==b.x+1 && a.y==b.y) //l
  1310. return 7;
  1311. return -2; //shouldn't happen
  1312. }
  1313. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1314. {
  1315. SDL_Color palette[14];
  1316. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  1317. {
  1318. for(int i=0; i<12; ++i)
  1319. {
  1320. palette[(i+1)%12] = terrainGraphics[8][j]->format->palette->colors[229 + i];
  1321. }
  1322. SDL_SetColors(terrainGraphics[8][j],palette,229,12);
  1323. for(int i=0; i<14; ++i)
  1324. {
  1325. palette[(i+1)%14] = terrainGraphics[8][j]->format->palette->colors[242 + i];
  1326. }
  1327. SDL_SetColors(terrainGraphics[8][j],palette,242,14);
  1328. }
  1329. }
  1330. CMapHandler::~CMapHandler()
  1331. {
  1332. delete fullHide;
  1333. delete partialHide;
  1334. for(int i=0; i < roadDefs.size(); i++)
  1335. delete roadDefs[i];
  1336. for(int i=0; i < staticRiverDefs.size(); i++)
  1337. delete staticRiverDefs[i];
  1338. //TODO: delete border graphics
  1339. }
  1340. CMapHandler::CMapHandler()
  1341. {
  1342. mapW = mapH = 0;
  1343. frameW = frameH = 0;
  1344. fullHide = NULL;
  1345. partialHide = NULL;
  1346. }
  1347. TerrainTile2::TerrainTile2()
  1348. :tileInfo(0),terbitmap(0)
  1349. {}