mapHandler.cpp 44 KB

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