mapHandler.cpp 40 KB

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