mapHandler.cpp 43 KB

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