mapHandler.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. if(!otherHeroAnim)
  497. heroAnim = anim; //the same, as it should be
  498. //setting surface to blit at
  499. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  500. if(extSurf)
  501. {
  502. su = extSurf;
  503. }
  504. else
  505. {
  506. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  507. }
  508. SDL_Rect prevClip;
  509. SDL_GetClipRect(su, &prevClip);
  510. if(extRect) SDL_SetClipRect(su, extRect); //preventing blitting outside of that rect
  511. if (((dx+x)>((map->width+Woff)) || (dy+y)>((map->height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  512. throw std::string("terrainRect: out of range");
  513. ////printing terrain
  514. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx+(smooth?1:0); bx++)
  515. {
  516. for (int by=(moveY <= 0 ? 0 : -1); by<dy+(smooth?1:0); by++)
  517. {
  518. const TerrainTile2 & tile = ttiles[x+bx][y+by][level];
  519. SDL_Rect sr;
  520. if(smooth)
  521. {
  522. sr.y=by*32 + moveY + extRect->y;
  523. sr.x=bx*32 + moveX + extRect->x;
  524. sr.h=sr.w=32;
  525. }
  526. else
  527. {
  528. sr.y=by*32;
  529. sr.x=bx*32;
  530. sr.h=sr.w=32;
  531. validateRectTerr(&sr, extRect);
  532. }
  533. if(tile.terbitmap)
  534. {
  535. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  536. }
  537. else
  538. {
  539. if(smooth)
  540. {
  541. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  542. {
  543. case 0:
  544. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  545. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  546. break;
  547. case 1:
  548. CSDL_Ext::blitWithRotate1clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  549. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  550. break;
  551. case 2:
  552. CSDL_Ext::blitWithRotate2clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  553. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  554. break;
  555. default:
  556. CSDL_Ext::blitWithRotate3clip(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  557. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  558. break;
  559. }
  560. }
  561. else
  562. {
  563. switch(tile.tileInfo->siodmyTajemniczyBajt%4)
  564. {
  565. case 0:
  566. SDL_BlitSurface(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  567. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  568. break;
  569. case 1:
  570. CSDL_Ext::blitWithRotate1(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  571. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  572. break;
  573. case 2:
  574. CSDL_Ext::blitWithRotate2(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  575. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  576. break;
  577. default:
  578. CSDL_Ext::blitWithRotate3(terrainGraphics[tile.tileInfo->tertype][tile.tileInfo->terview],
  579. &genRect(sr.h, sr.w, 0, 0),su,&sr);
  580. break;
  581. }
  582. }
  583. }
  584. }
  585. }
  586. ////terrain printed
  587. ////printing rivers
  588. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx+(smooth?1:0); bx++)
  589. {
  590. for (int by=(moveY <= 0 ? 0 : -1); by<dy+(smooth?1:0); by++)
  591. {
  592. SDL_Rect sr;
  593. if(smooth)
  594. {
  595. sr.y=by*32 + moveY + extRect->y;
  596. sr.x=bx*32 + moveX + extRect->x;
  597. sr.h=sr.w=32;
  598. }
  599. else
  600. {
  601. sr.y=by*32;
  602. sr.x=bx*32;
  603. sr.h=sr.w=32;
  604. validateRectTerr(&sr, extRect);
  605. }
  606. const std::vector<SDL_Surface *> &rivbitmap = ttiles[x+bx][y+by][level].rivbitmap;
  607. if(rivbitmap.size())
  608. {
  609. CSDL_Ext::blit8bppAlphaTo24bpp(rivbitmap[anim%rivbitmap.size()],&genRect(sr.h, sr.w, 0, 0),su,&sr);
  610. }
  611. }
  612. }
  613. ////rivers printed
  614. ////printing roads
  615. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx+(smooth?1:0); bx++)
  616. {
  617. for (int by=(moveY <= 0 ? 0 : -1) - 1; by<dy+(smooth?1:0); by++)
  618. {
  619. if(y+by<=-4)
  620. continue;
  621. SDL_Rect sr;
  622. if(smooth)
  623. {
  624. sr.y=by*32+16 + moveY + extRect->y;
  625. sr.x=bx*32 + moveX + extRect->x;
  626. sr.h=sr.w=32;
  627. }
  628. else
  629. {
  630. sr.y=by*32+16;
  631. sr.x=bx*32;
  632. sr.h=sr.w=32;
  633. validateRectTerr(&sr, extRect);
  634. }
  635. const std::vector<SDL_Surface *> &roadbitmap = ttiles[x+bx][y+by][level].roadbitmap;
  636. if(roadbitmap.size())
  637. {
  638. CSDL_Ext::blit8bppAlphaTo24bpp(roadbitmap[anim%roadbitmap.size()], &genRect(sr.h, sr.w, 0, (by==-1 && moveY == 0 ? 16 : 0)),su,&sr);
  639. }
  640. }
  641. }
  642. ////roads printed
  643. ////printing objects
  644. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx+(smooth?1:0); bx++)
  645. {
  646. for (int by=(moveY <= 0 ? 0 : -1); by<dy+(smooth?1:0); by++)
  647. {
  648. for(int h=0; h < ttiles[x+bx][y+by][level].objects.size(); ++h)
  649. {
  650. SDL_Rect sr;
  651. if(smooth)
  652. {
  653. sr.w = 32;
  654. sr.h = 32;
  655. sr.x = (bx)*32 + moveX + extRect->x;
  656. sr.y = (by)*32 + moveY + extRect->y;
  657. }
  658. else
  659. {
  660. sr.w = 32;
  661. sr.h = 32;
  662. sr.x = (bx)*32;
  663. sr.y = (by)*32;
  664. validateRectTerr(&sr, extRect);
  665. }
  666. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  667. pp.h = sr.h;
  668. pp.w = sr.w;
  669. const CGHeroInstance * themp = (dynamic_cast<const CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  670. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  671. {
  672. int imgVal = 8;
  673. SDL_Surface * tb;
  674. if(themp->type==NULL)
  675. continue;
  676. std::vector<Cimage> & iv = graphics->heroAnims[themp->type->heroType]->ourImages;
  677. size_t gg;
  678. for(gg=0; gg<iv.size(); ++gg)
  679. {
  680. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  681. {
  682. tb = iv[gg+heroAnim%imgVal].bitmap;
  683. break;
  684. }
  685. }
  686. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  687. pp.y+=imgVal*2-32;
  688. sr.y-=16;
  689. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  690. }
  691. else 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].bitmap;
  704. break;
  705. }
  706. }
  707. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  708. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  709. {
  710. SDL_Rect bufr = sr;
  711. bufr.x-=2*32;
  712. bufr.y-=1*32;
  713. bufr.h = 64;
  714. bufr.w = 96;
  715. if(bufr.x-extRect->x>-64)
  716. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, su, &bufr);
  717. }
  718. }
  719. else
  720. {
  721. const CGObjectInstance *obj = ttiles[x+bx][y+by][level].objects[h].first;
  722. const std::vector<Cimage> &ourImages = obj->defInfo->handler->ourImages;
  723. SDL_Surface *bitmap = ourImages[(anim+obj->animPhaseShift)%ourImages.size()].bitmap;
  724. //setting appropriate flag color
  725. if(obj->tempOwner<8 || obj->tempOwner==255)
  726. CSDL_Ext::setPlayerColor(bitmap, obj->tempOwner);
  727. CSDL_Ext::blit8bppAlphaTo24bpp(bitmap,&pp,su,&sr);
  728. }
  729. }
  730. }
  731. }
  732. ////objects printed, printing shadow
  733. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx+(smooth?1:0); bx++)
  734. {
  735. for (int by=(moveY <= 0 ? 0 : -1); by<dy+(smooth?1:0); by++)
  736. {
  737. SDL_Rect sr;
  738. if(smooth)
  739. {
  740. sr.y=by*32 + moveY + extRect->y;
  741. sr.x=bx*32 + moveX + extRect->x;
  742. sr.h=sr.w=32;
  743. }
  744. else
  745. {
  746. sr.y=by*32;
  747. sr.x=bx*32;
  748. sr.h=sr.w=32;
  749. validateRectTerr(&sr, extRect);
  750. }
  751. 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])
  752. {
  753. SDL_Surface * hide = getVisBitmap(bx+x, by+y, *visibilityMap, level);
  754. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  755. }
  756. }
  757. }
  758. ////shadow printed
  759. //printing borders
  760. for (int bx= (moveX <= 0 ? 0 : -1); bx<dx+(smooth?1:0); bx++)
  761. {
  762. for (int by=(moveY <= 0 ? 0 : -1); by<dy+(smooth?1:0); by++)
  763. {
  764. if(bx+x<0 || by+y<0 || bx+x>map->width+(-1) || by+y>map->height+(-1))
  765. {
  766. SDL_Rect sr;
  767. if(smooth)
  768. {
  769. sr.y=by*32 + moveY + extRect->y;
  770. sr.x=bx*32 + moveX + extRect->x;
  771. sr.h=sr.w=32;
  772. }
  773. else
  774. {
  775. sr.y=by*32;
  776. sr.x=bx*32;
  777. sr.h=sr.w=32;
  778. validateRectTerr(&sr, extRect);
  779. }
  780. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  781. }
  782. else
  783. {
  784. #ifdef MARK_BLOCKED_POSITIONS
  785. if(ttiles[x+bx][y+by][level].tileInfo->blocked) //temporary hiding blocked positions
  786. {
  787. SDL_Rect sr;
  788. if(smooth)
  789. {
  790. sr.y=by*32 + moveY;
  791. sr.x=bx*32 + moveX;
  792. sr.h=sr.w=32;
  793. }
  794. else
  795. {
  796. sr.y=by*32;
  797. sr.x=bx*32;
  798. sr.h=sr.w=32;
  799. validateRectTerr(&sr, extRect);
  800. }
  801. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  802. for(int f=0; f<ns->w*ns->h*4; ++f)
  803. {
  804. *((unsigned char*)(ns->pixels) + f) = 128;
  805. }
  806. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  807. SDL_FreeSurface(ns);
  808. }
  809. #endif
  810. #ifdef MARK_VISITABLE_POSITIONS
  811. if(ttiles[x+bx][y+by][level].tileInfo->visitable) //temporary hiding visitable positions
  812. {
  813. SDL_Rect sr;
  814. if(smooth)
  815. {
  816. sr.y=by*32 + moveY;
  817. sr.x=bx*32 + moveX;
  818. sr.h=sr.w=32;
  819. }
  820. else
  821. {
  822. sr.y=by*32;
  823. sr.x=bx*32;
  824. sr.h=sr.w=32;
  825. validateRectTerr(&sr, extRect);
  826. }
  827. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  828. for(int f=0; f<ns->w*ns->h*4; ++f)
  829. {
  830. *((unsigned char*)(ns->pixels) + f) = 128;
  831. }
  832. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  833. SDL_FreeSurface(ns);
  834. }
  835. #endif
  836. }
  837. }
  838. }
  839. SDL_SetClipRect(su, &prevClip); //restoring clip_rect
  840. //borders printed
  841. return su;
  842. }
  843. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, const std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)
  844. {
  845. int size = visibilityMap.size()-1; //is tile visible. arrangement: (like num keyboard)
  846. bool d7 = (x>0 && y>0) ? visibilityMap[x-1][y-1][lvl] : 0, //789
  847. d8 = (y>0) ? visibilityMap[x][y-1][lvl] : 0, //456
  848. d9 = (y>0 && x<size) ? visibilityMap[x+1][y-1][lvl] : 0,//123
  849. d4 = (x>0) ? visibilityMap[x-1][y][lvl] : 0,
  850. d5 = visibilityMap[x][y][lvl], //TODO use me - OMFG
  851. d6 = (x<size) ? visibilityMap[x+1][y][lvl] : 0,
  852. d1 = (x>0 && y<size) ? visibilityMap[x-1][y+1][lvl] : 0,
  853. d2 = (y<size) ? visibilityMap[x][y+1][lvl] : 0,
  854. d3 = (x<size && y<size) ? visibilityMap[x+1][y+1][lvl] : 0;
  855. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && !d1)
  856. {
  857. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  858. }
  859. else if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1)
  860. {
  861. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  862. }
  863. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1)
  864. {
  865. return partialHide->ourImages[15].bitmap; //visible right top corner
  866. }
  867. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1)
  868. {
  869. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  870. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  871. }
  872. else if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && !d1)
  873. {
  874. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  875. return partialHide->ourImages[35].bitmap;
  876. }
  877. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && d9 && !d1)
  878. {
  879. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  880. return partialHide->ourImages[0].bitmap; //visible top
  881. }
  882. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  883. {
  884. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  885. return partialHide->ourImages[4].bitmap; //visble bottom
  886. }
  887. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && d1)
  888. {
  889. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  890. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  891. return partialHide->ourImages[36].bitmap;
  892. }
  893. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  894. {
  895. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  896. return partialHide->ourImages[2].bitmap; //visible right
  897. }
  898. else if(d2 && d6 && !d4 && !d8 && !d7)
  899. {
  900. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  901. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  902. }
  903. else if(!d2 && d6 && !d4 && d8 && !d1)
  904. {
  905. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  906. }
  907. else if(!d2 && !d6 && d4 && d8 && !d3)
  908. {
  909. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  910. return partialHide->ourImages[37].bitmap;
  911. }
  912. else if(d2 && !d6 && d4 && !d8 && !d9)
  913. {
  914. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  915. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  916. return partialHide->ourImages[38].bitmap;
  917. }
  918. else if(d2 && d6 && d4 && d8)
  919. {
  920. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  921. }
  922. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  923. {
  924. return partialHide->ourImages[16].bitmap; //visible right corners
  925. }
  926. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && !d1)
  927. {
  928. return partialHide->ourImages[18].bitmap; //visible top corners
  929. }
  930. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && d1)
  931. {
  932. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  933. return partialHide->ourImages[39].bitmap;
  934. }
  935. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  936. {
  937. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  938. return partialHide->ourImages[40].bitmap;
  939. }
  940. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && d1)
  941. {
  942. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  943. }
  944. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && !d1)
  945. {
  946. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  947. return partialHide->ourImages[41].bitmap;
  948. }
  949. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && d1)
  950. {
  951. return partialHide->ourImages[19].bitmap; //visible corners without left top
  952. }
  953. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && !d1)
  954. {
  955. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  956. }
  957. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && d1)
  958. {
  959. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  960. return partialHide->ourImages[42].bitmap;
  961. }
  962. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && d1)
  963. {
  964. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  965. return partialHide->ourImages[43].bitmap;
  966. }
  967. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && d1)
  968. {
  969. return partialHide->ourImages[21].bitmap; //visible all corners only
  970. }
  971. if(d2 && d6 && d4 && !d8)
  972. {
  973. return partialHide->ourImages[6].bitmap; //hidden top
  974. }
  975. if(d2 && !d6 && d4 && d8)
  976. {
  977. return partialHide->ourImages[7].bitmap; //hidden right
  978. }
  979. if(!d2 && d6 && d4 && d8)
  980. {
  981. return partialHide->ourImages[8].bitmap; //hidden bottom
  982. }
  983. if(d2 && d6 && !d4 && d8)
  984. {
  985. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  986. return partialHide->ourImages[44].bitmap;
  987. }
  988. if(!d2 && d6 && d4 && !d8)
  989. {
  990. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  991. }
  992. if(d2 && !d6 && !d4 && d8)
  993. {
  994. return partialHide->ourImages[29].bitmap; //hidden left and right
  995. }
  996. if(!d2 && !d6 && !d4 && d8 && d3 && !d1)
  997. {
  998. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  999. }
  1000. if(!d2 && !d6 && !d4 && d8 && !d3 && d1)
  1001. {
  1002. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  1003. return partialHide->ourImages[45].bitmap;
  1004. }
  1005. if(!d2 && !d6 && !d4 && d8 && d3 && d1)
  1006. {
  1007. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1008. }
  1009. if(!d2 && !d6 && d4 && !d8 && !d3 && d9)
  1010. {
  1011. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  1012. return partialHide->ourImages[46].bitmap;
  1013. }
  1014. if(!d2 && !d6 && d4 && !d8 && d3 && !d9)
  1015. {
  1016. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  1017. return partialHide->ourImages[47].bitmap;
  1018. }
  1019. if(!d2 && !d6 && d4 && !d8 && d3 && d9)
  1020. {
  1021. return partialHide->ourImages[32].bitmap; //visible left and right corners
  1022. }
  1023. if(d2 && !d6 && !d4 && !d8 && d7 && !d9)
  1024. {
  1025. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  1026. return partialHide->ourImages[48].bitmap;
  1027. }
  1028. if(d2 && !d6 && !d4 && !d8 && !d7 && d9)
  1029. {
  1030. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  1031. }
  1032. if(d2 && !d6 && !d4 && !d8 && d7 && d9)
  1033. {
  1034. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1035. }
  1036. if(!d2 && d6 && !d4 && !d8 && !d7 && d1)
  1037. {
  1038. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  1039. }
  1040. if(!d2 && d6 && !d4 && !d8 && d7 && !d1)
  1041. {
  1042. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  1043. }
  1044. if(!d2 && d6 && !d4 && !d8 && d7 && d1)
  1045. {
  1046. //return CSDL_Ext::rotate01(partialHide->ourImages[32].bitmap); //visible right and left cornres
  1047. return partialHide->ourImages[49].bitmap;
  1048. }
  1049. if(d2 && d6 && !d4 && !d8 && d7)
  1050. {
  1051. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1052. }
  1053. else if(!d2 && d6 && !d4 && d8 && d1)
  1054. {
  1055. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1056. }
  1057. else if(!d2 && !d6 && d4 && d8 && d3)
  1058. {
  1059. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  1060. return partialHide->ourImages[50].bitmap;
  1061. }
  1062. else if(d2 && !d6 && d4 && !d8 && d9)
  1063. {
  1064. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  1065. return partialHide->ourImages[51].bitmap;
  1066. }
  1067. //newly added
  1068. else if(!d2 && !d6 && !d4 && d8 && !d7 && !d3 && d9 && !d1) //visible t and tr
  1069. {
  1070. return partialHide->ourImages[0].bitmap;
  1071. }
  1072. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && !d9 && !d1) //visible t and tl
  1073. {
  1074. return partialHide->ourImages[1].bitmap;
  1075. }
  1076. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible b and br
  1077. {
  1078. return partialHide->ourImages[4].bitmap;
  1079. }
  1080. else if(d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible b and bl
  1081. {
  1082. return partialHide->ourImages[5].bitmap;
  1083. }
  1084. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && !d1) //visible l and tl
  1085. {
  1086. return partialHide->ourImages[36].bitmap;
  1087. }
  1088. else if(!d2 && !d6 && d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible l and bl
  1089. {
  1090. return partialHide->ourImages[36].bitmap;
  1091. }
  1092. else if(!d2 && d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1) //visible r and tr
  1093. {
  1094. return partialHide->ourImages[2].bitmap;
  1095. }
  1096. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible r and br
  1097. {
  1098. return partialHide->ourImages[3].bitmap;
  1099. }
  1100. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1101. }
  1102. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1103. {
  1104. int ret=-1;
  1105. if(a.x>=CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1106. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1107. else if(a.x>=CGI->mh->map->width && a.y<CGI->mh->map->height)
  1108. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][a.y][a.z].tileInfo->malle];
  1109. else if(a.x<CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1110. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->map->width-1][a.z].tileInfo->malle];
  1111. else
  1112. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].tileInfo->malle];
  1113. if(!(a.x==b.x || a.y==b.y))
  1114. ret*=1.41421;
  1115. //TODO: use hero's pathfinding skill during calculating cost
  1116. return ret;
  1117. }
  1118. //std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1119. //{
  1120. // std::vector < CGObjectInstance * > ret;
  1121. // for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1122. // {
  1123. // CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1124. // if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1125. // ret.push_back(curi);
  1126. // }
  1127. // return ret;
  1128. //}
  1129. std::string CMapHandler::getDefName(int id, int subid)
  1130. {
  1131. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1132. if(temp)
  1133. return temp->name;
  1134. throw std::string("Def not found.");
  1135. }
  1136. bool CMapHandler::printObject(const CGObjectInstance *obj)
  1137. {
  1138. CDefHandler * curd = obj->defInfo->handler;
  1139. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1140. {
  1141. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1142. {
  1143. SDL_Rect cr;
  1144. cr.w = 32;
  1145. cr.h = 32;
  1146. cr.x = fx*32;
  1147. cr.y = fy*32;
  1148. std::pair<const CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1149. 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)
  1150. {
  1151. TerrainTile2 & curt = //TODO use me
  1152. ttiles
  1153. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1154. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1155. [obj->pos.z];
  1156. 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);
  1157. }
  1158. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1159. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1160. return true;
  1161. }
  1162. bool CMapHandler::hideObject(const CGObjectInstance *obj)
  1163. {
  1164. CDefHandler * curd = obj->defInfo->handler;
  1165. if(!curd) return false;
  1166. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1167. {
  1168. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1169. {
  1170. 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)
  1171. {
  1172. 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;
  1173. for(size_t dd=0; dd < ctile.size(); ++dd)
  1174. {
  1175. if(ctile[dd].first->id==obj->id)
  1176. ctile.erase(ctile.begin() + dd);
  1177. }
  1178. }
  1179. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1180. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1181. return true;
  1182. }
  1183. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1184. {
  1185. hideObject(obj);
  1186. return true;
  1187. }
  1188. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1189. {
  1190. if(isMoving)
  1191. {
  1192. switch(dir)
  1193. {
  1194. case 1:
  1195. return 10;
  1196. case 2:
  1197. return 5;
  1198. case 3:
  1199. return 6;
  1200. case 4:
  1201. return 7;
  1202. case 5:
  1203. return 8;
  1204. case 6:
  1205. return 9;
  1206. case 7:
  1207. return 12;
  1208. case 8:
  1209. return 11;
  1210. default:
  1211. throw std::string("Something very wrong1.");
  1212. }
  1213. }
  1214. else //if(isMoving)
  1215. {
  1216. switch(dir)
  1217. {
  1218. case 1:
  1219. return 13;
  1220. case 2:
  1221. return 0;
  1222. case 3:
  1223. return 1;
  1224. case 4:
  1225. return 2;
  1226. case 5:
  1227. return 3;
  1228. case 6:
  1229. return 4;
  1230. case 7:
  1231. return 15;
  1232. case 8:
  1233. return 14;
  1234. default:
  1235. throw std::string("Something very wrong2.");
  1236. }
  1237. }
  1238. }
  1239. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1240. {
  1241. if(ext)
  1242. {
  1243. if(val->x<0)
  1244. {
  1245. val->w += val->x;
  1246. val->x = ext->x;
  1247. }
  1248. else
  1249. {
  1250. val->x += ext->x;
  1251. }
  1252. if(val->y<0)
  1253. {
  1254. val->h += val->y;
  1255. val->y = ext->y;
  1256. }
  1257. else
  1258. {
  1259. val->y += ext->y;
  1260. }
  1261. if(val->x+val->w > ext->x+ext->w)
  1262. {
  1263. val->w = ext->x+ext->w-val->x;
  1264. }
  1265. if(val->y+val->h > ext->y+ext->h)
  1266. {
  1267. val->h = ext->y+ext->h-val->y;
  1268. }
  1269. //for sign problems
  1270. if(val->h > 20000 || val->w > 20000)
  1271. {
  1272. val->h = val->w = 0;
  1273. }
  1274. }
  1275. }
  1276. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1277. {
  1278. if(a.z!=b.z)
  1279. return -1; //error!
  1280. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1281. {
  1282. return 0;
  1283. }
  1284. else if(a.x==b.x && a.y==b.y+1) //t
  1285. {
  1286. return 1;
  1287. }
  1288. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1289. {
  1290. return 2;
  1291. }
  1292. else if(a.x==b.x-1 && a.y==b.y) //r
  1293. {
  1294. return 3;
  1295. }
  1296. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1297. {
  1298. return 4;
  1299. }
  1300. else if(a.x==b.x && a.y==b.y-1) //b
  1301. {
  1302. return 5;
  1303. }
  1304. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1305. {
  1306. return 6;
  1307. }
  1308. else if(a.x==b.x+1 && a.y==b.y) //l
  1309. {
  1310. return 7;
  1311. }
  1312. return -2; //shouldn't happen
  1313. }
  1314. void CMapHandler::updateWater() //shift colors in palettes of water tiles
  1315. {
  1316. SDL_Color palette[14];
  1317. for(size_t j=0; j < terrainGraphics[8].size(); ++j)
  1318. {
  1319. for(int i=0; i<12; ++i)
  1320. {
  1321. palette[(i+1)%12] = terrainGraphics[8][j]->format->palette->colors[229 + i];
  1322. }
  1323. SDL_SetColors(terrainGraphics[8][j],palette,229,12);
  1324. for(int i=0; i<14; ++i)
  1325. {
  1326. palette[(i+1)%14] = terrainGraphics[8][j]->format->palette->colors[242 + i];
  1327. }
  1328. SDL_SetColors(terrainGraphics[8][j],palette,242,14);
  1329. }
  1330. }
  1331. CMapHandler::~CMapHandler()
  1332. {
  1333. delete fullHide;
  1334. delete partialHide;
  1335. for(int i=0; i < map->defy.size(); i++)
  1336. delete map->defy[i];
  1337. for(int i=0; i < roadDefs.size(); i++)
  1338. delete roadDefs[i];
  1339. for(int i=0; i < staticRiverDefs.size(); i++)
  1340. delete staticRiverDefs[i];
  1341. //TODO: delete border graphics
  1342. }
  1343. CMapHandler::CMapHandler()
  1344. {
  1345. fullHide = NULL;
  1346. partialHide = NULL;
  1347. }
  1348. TerrainTile2::TerrainTile2()
  1349. :tileInfo(0),terbitmap(0)
  1350. {}