mapHandler.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. #include "stdafx.h"
  2. #include "mapHandler.h"
  3. #include "SDL_Extensions.h"
  4. #include "CGameInfo.h"
  5. #include <cstdlib>
  6. #include "hch/CLodHandler.h"
  7. #include "hch/CDefObjInfoHandler.h"
  8. #include <algorithm>
  9. #include "CGameState.h"
  10. #include "CLua.h"
  11. #include "hch/CHeroHandler.h"
  12. #include "hch/CTownHandler.h"
  13. #include "client\Graphics.h"
  14. #include <iomanip>
  15. #include <sstream>
  16. #include "hch/CObjectHandler.h"
  17. #include "map.h"
  18. extern SDL_Surface * screen;
  19. std::string nameFromType (EterrainType typ)
  20. {
  21. switch(typ)
  22. {
  23. case dirt:
  24. {
  25. return std::string("DIRTTL.DEF");
  26. break;
  27. }
  28. case sand:
  29. {
  30. return std::string("SANDTL.DEF");
  31. break;
  32. }
  33. case grass:
  34. {
  35. return std::string("GRASTL.DEF");
  36. break;
  37. }
  38. case snow:
  39. {
  40. return std::string("SNOWTL.DEF");
  41. break;
  42. }
  43. case swamp:
  44. {
  45. return std::string("SWMPTL.DEF");
  46. break;
  47. }
  48. case rough:
  49. {
  50. return std::string("ROUGTL.DEF");
  51. break;
  52. }
  53. case subterranean:
  54. {
  55. return std::string("SUBBTL.DEF");
  56. break;
  57. }
  58. case lava:
  59. {
  60. return std::string("LAVATL.DEF");
  61. break;
  62. }
  63. case water:
  64. {
  65. return std::string("WATRTL.DEF");
  66. break;
  67. }
  68. case rock:
  69. {
  70. return std::string("ROCKTL.DEF");
  71. break;
  72. }
  73. }
  74. return std::string();
  75. }
  76. class OCM_HLP
  77. {
  78. public:
  79. bool operator ()(const std::pair<CGObjectInstance*, SDL_Rect> & a, const std::pair<CGObjectInstance*, SDL_Rect> & b)
  80. {
  81. return (*a.first)<(*b.first);
  82. }
  83. } ocmptwo ;
  84. void alphaTransformDef(CGDefInfo * defInfo)
  85. {
  86. SDL_Surface * alphaTransSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, 12, 12, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
  87. for(int yy=0;yy<defInfo->handler->ourImages.size();yy++)
  88. {
  89. defInfo->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(defInfo->handler->ourImages[yy].bitmap);
  90. defInfo->handler->alphaTransformed = true;
  91. }
  92. SDL_FreeSurface(alphaTransSurf);
  93. }
  94. void CMapHandler::prepareFOWDefs()
  95. {
  96. fullHide = CDefHandler::giveDef("TSHRC.DEF");
  97. partialHide = CDefHandler::giveDef("TSHRE.DEF");
  98. //adding necessary rotations
  99. Cimage nw = partialHide->ourImages[22]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  100. partialHide->ourImages.push_back(nw);
  101. nw = partialHide->ourImages[15]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  102. partialHide->ourImages.push_back(nw);
  103. nw = partialHide->ourImages[2]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  104. partialHide->ourImages.push_back(nw);
  105. nw = partialHide->ourImages[13]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  106. partialHide->ourImages.push_back(nw);
  107. nw = partialHide->ourImages[12]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  108. partialHide->ourImages.push_back(nw);
  109. nw = partialHide->ourImages[16]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  110. partialHide->ourImages.push_back(nw);
  111. nw = partialHide->ourImages[18]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  112. partialHide->ourImages.push_back(nw);
  113. nw = partialHide->ourImages[17]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  114. partialHide->ourImages.push_back(nw);
  115. nw = partialHide->ourImages[20]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  116. partialHide->ourImages.push_back(nw);
  117. nw = partialHide->ourImages[19]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  118. partialHide->ourImages.push_back(nw);
  119. nw = partialHide->ourImages[7]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  120. partialHide->ourImages.push_back(nw);
  121. nw = partialHide->ourImages[24]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  122. partialHide->ourImages.push_back(nw);
  123. nw = partialHide->ourImages[26]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  124. partialHide->ourImages.push_back(nw);
  125. nw = partialHide->ourImages[25]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  126. partialHide->ourImages.push_back(nw);
  127. nw = partialHide->ourImages[30]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  128. partialHide->ourImages.push_back(nw);
  129. nw = partialHide->ourImages[32]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  130. partialHide->ourImages.push_back(nw);
  131. nw = partialHide->ourImages[27]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  132. partialHide->ourImages.push_back(nw);
  133. nw = partialHide->ourImages[28]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  134. partialHide->ourImages.push_back(nw);
  135. //necessaary rotations added
  136. for(int i=0; i<partialHide->ourImages.size(); ++i)
  137. {
  138. CSDL_Ext::alphaTransform(partialHide->ourImages[i].bitmap);
  139. }
  140. //visibility.resize(map->width+2*Woff);
  141. //for(int gg=0; gg<map->width+2*Woff; ++gg)
  142. //{
  143. // visibility[gg].resize(map->height+2*Hoff);
  144. // for(int jj=0; jj<map->height+2*Hoff; ++jj)
  145. // visibility[gg][jj] = true;
  146. //}
  147. //visibility.resize(CGI->mh->map->width, Woff);
  148. //for (int i=0-Woff;i<visibility.size()-Woff;i++)
  149. //{
  150. // visibility[i].resize(CGI->mh->map->height,Hoff);
  151. //}
  152. //for (int i=0-Woff; i<visibility.size()-Woff; ++i)
  153. //{
  154. // for (int j=0-Hoff; j<CGI->mh->map->height+Hoff; ++j)
  155. // {
  156. // visibility[i][j].resize(CGI->mh->map->twoLevel+1,0);
  157. // for(int k=0; k<CGI->mh->map->twoLevel+1; ++k)
  158. // visibility[i][j][k]=true;
  159. // }
  160. //}
  161. hideBitmap.resize(CGI->mh->map->width);
  162. for (int i=0;i<hideBitmap.size();i++)
  163. {
  164. hideBitmap[i].resize(CGI->mh->map->height);
  165. }
  166. for (int i=0; i<hideBitmap.size(); ++i)
  167. {
  168. for (int j=0; j<CGI->mh->map->height; ++j)
  169. {
  170. hideBitmap[i][j].resize(CGI->mh->map->twoLevel+1);
  171. for(int k=0; k<CGI->mh->map->twoLevel+1; ++k)
  172. hideBitmap[i][j][k] = rand()%fullHide->ourImages.size();
  173. }
  174. }
  175. }
  176. void CMapHandler::roadsRiverTerrainInit()
  177. {
  178. //initializing road's and river's DefHandlers
  179. roadDefs.push_back(CDefHandler::giveDef("dirtrd.def"));
  180. roadDefs.push_back(CDefHandler::giveDef("gravrd.def"));
  181. roadDefs.push_back(CDefHandler::giveDef("cobbrd.def"));
  182. staticRiverDefs.push_back(CDefHandler::giveDef("clrrvr.def"));
  183. staticRiverDefs.push_back(CDefHandler::giveDef("icyrvr.def"));
  184. staticRiverDefs.push_back(CDefHandler::giveDef("mudrvr.def"));
  185. staticRiverDefs.push_back(CDefHandler::giveDef("lavrvr.def"));
  186. for(int g=0; g<staticRiverDefs.size(); ++g)
  187. {
  188. for(int h=0; h<staticRiverDefs[g]->ourImages.size(); ++h)
  189. {
  190. CSDL_Ext::alphaTransform(staticRiverDefs[g]->ourImages[h].bitmap);
  191. }
  192. }
  193. for(int g=0; g<roadDefs.size(); ++g)
  194. {
  195. for(int h=0; h<roadDefs[g]->ourImages.size(); ++h)
  196. {
  197. CSDL_Ext::alphaTransform(roadDefs[g]->ourImages[h].bitmap);
  198. }
  199. }
  200. sizes.x = CGI->mh->map->width;
  201. sizes.y = CGI->mh->map->height;
  202. sizes.z = CGI->mh->map->twoLevel+1;
  203. ttiles.resize(CGI->mh->map->width,Woff);
  204. for (int i=0-Woff;i<ttiles.size()-Woff;i++)
  205. {
  206. ttiles[i].resize(CGI->mh->map->height,Hoff);
  207. }
  208. for (int i=0-Woff;i<ttiles.size()-Woff;i++)
  209. {
  210. for (int j=0-Hoff;j<CGI->mh->map->height+Hoff;j++)
  211. ttiles[i][j].resize(CGI->mh->map->twoLevel+1,0);
  212. }
  213. for (int i=0; i<map->width; i++) //jest po szerokoœci
  214. {
  215. for (int j=0; j<map->height;j++) //po wysokoœci
  216. {
  217. for (int k=0; k<=map->twoLevel; ++k)
  218. {
  219. TerrainTile** pomm = map->terrain; ;
  220. if (k==0)
  221. pomm = map->terrain;
  222. else
  223. pomm = map->undergroungTerrain;
  224. if(pomm[i][j].malle)
  225. {
  226. int cDir;
  227. bool rotV, rotH;
  228. if(k==0)
  229. {
  230. int roadpom = map->terrain[i][j].malle-1,
  231. impom = map->terrain[i][j].roadDir;
  232. SDL_Surface *pom1 = roadDefs[roadpom]->ourImages[impom].bitmap;
  233. ttiles[i][j][k].roadbitmap.push_back(pom1);
  234. cDir = map->terrain[i][j].roadDir;
  235. rotH = (map->terrain[i][j].siodmyTajemniczyBajt >> 5) & 1;
  236. rotV = (map->terrain[i][j].siodmyTajemniczyBajt >> 4) & 1;
  237. }
  238. else
  239. {
  240. int pom111 = map->undergroungTerrain[i][j].malle-1,
  241. pom777 = map->undergroungTerrain[i][j].roadDir;
  242. SDL_Surface *pom1 = roadDefs[pom111]->ourImages[pom777].bitmap;
  243. ttiles[i][j][k].roadbitmap.push_back(pom1);
  244. cDir = map->undergroungTerrain[i][j].roadDir;
  245. rotH = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt >> 5) & 1;
  246. rotV = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt >> 4) & 1;
  247. }
  248. if(rotH)
  249. {
  250. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].roadbitmap[0]);
  251. }
  252. if(rotV)
  253. {
  254. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].roadbitmap[0]);
  255. }
  256. if(rotH || rotV)
  257. {
  258. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::alphaTransform(ttiles[i][j][k].roadbitmap[0]);
  259. }
  260. }
  261. }
  262. }
  263. }
  264. //initializing simple values
  265. for (int i=0; i<CGI->mh->map->width; i++) //jest po szerokoœci
  266. {
  267. for (int j=0; j<CGI->mh->map->height;j++) //po wysokoœci
  268. {
  269. for(int k=0; k<ttiles[0][0].size(); ++k)
  270. {
  271. ttiles[i][j][k].pos = int3(i, j, k);
  272. ttiles[i][j][k].blocked = false;
  273. ttiles[i][j][k].visitable = false;
  274. if(i<0 || j<0 || i>=CGI->mh->map->width || j>=CGI->mh->map->height)
  275. {
  276. ttiles[i][j][k].blocked = true;
  277. continue;
  278. }
  279. ttiles[i][j][k].terType = (k==0 ? CGI->mh->map->terrain[i][j].tertype : CGI->mh->map->undergroungTerrain[i][j].tertype);
  280. ttiles[i][j][k].malle = (k==0 ? CGI->mh->map->terrain[i][j].malle : CGI->mh->map->undergroungTerrain[i][j].malle);
  281. ttiles[i][j][k].nuine = (k==0 ? CGI->mh->map->terrain[i][j].nuine : CGI->mh->map->undergroungTerrain[i][j].nuine);
  282. ttiles[i][j][k].rivdir = (k==0 ? CGI->mh->map->terrain[i][j].rivDir : CGI->mh->map->undergroungTerrain[i][j].rivDir);
  283. ttiles[i][j][k].roaddir = (k==0 ? CGI->mh->map->terrain[i][j].roadDir : CGI->mh->map->undergroungTerrain[i][j].roadDir);
  284. }
  285. }
  286. }
  287. //simple values initialized
  288. for (int i=0; i<map->width; i++) //jest po szerokoœci
  289. {
  290. for (int j=0; j<map->height;j++) //po wysokoœci
  291. {
  292. for(int k=0; k<=map->twoLevel; ++k)
  293. {
  294. TerrainTile** pomm = map->terrain;
  295. if(k==0)
  296. {
  297. pomm = map->terrain;
  298. }
  299. else
  300. {
  301. pomm = map->undergroungTerrain;
  302. }
  303. if(pomm[i][j].nuine)
  304. {
  305. int cDir;
  306. bool rotH, rotV;
  307. if(k==0)
  308. {
  309. ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[map->terrain[i][j].nuine-1]->ourImages[map->terrain[i][j].rivDir].bitmap);
  310. cDir = map->terrain[i][j].rivDir;
  311. rotH = (map->terrain[i][j].siodmyTajemniczyBajt >> 3) & 1;
  312. rotV = (map->terrain[i][j].siodmyTajemniczyBajt >> 2) & 1;
  313. }
  314. else
  315. {
  316. ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[map->undergroungTerrain[i][j].nuine-1]->ourImages[map->undergroungTerrain[i][j].rivDir].bitmap);
  317. cDir = map->undergroungTerrain[i][j].rivDir;
  318. rotH = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt >> 3) & 1;
  319. rotV = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt >> 2) & 1;
  320. }
  321. if(rotH)
  322. {
  323. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].rivbitmap[0]);
  324. }
  325. if(rotV)
  326. {
  327. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].rivbitmap[0]);
  328. }
  329. if(rotH || rotV)
  330. {
  331. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::alphaTransform(ttiles[i][j][k].rivbitmap[0]);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. void CMapHandler::borderAndTerrainBitmapInit()
  339. {
  340. CDefHandler * bord = CDefHandler::giveDef("EDG.DEF");
  341. bord->notFreeImgs = true;
  342. for (int i=0-Woff; i<map->width+Woff; i++) //jest po szerokoœci
  343. {
  344. for (int j=0-Hoff; j<map->height+Hoff;j++) //po wysokoœci
  345. {
  346. for(int k=0; k<=map->twoLevel; ++k)
  347. {
  348. if(i < 0 || i > (map->width-1) || j < 0 || j > (map->height-1))
  349. {
  350. if(i==-1 && j==-1)
  351. {
  352. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[16].bitmap);
  353. continue;
  354. }
  355. else if(i==-1 && j==(map->height))
  356. {
  357. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[19].bitmap);
  358. continue;
  359. }
  360. else if(i==(map->width) && j==-1)
  361. {
  362. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[17].bitmap);
  363. continue;
  364. }
  365. else if(i==(map->width) && j==(map->height))
  366. {
  367. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[18].bitmap);
  368. continue;
  369. }
  370. else if(j == -1 && i > -1 && i < map->height)
  371. {
  372. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[22+rand()%2].bitmap);
  373. continue;
  374. }
  375. else if(i == -1 && j > -1 && j < map->height)
  376. {
  377. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[33+rand()%2].bitmap);
  378. continue;
  379. }
  380. else if(j == map->height && i >-1 && i < map->width)
  381. {
  382. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[29+rand()%2].bitmap);
  383. continue;
  384. }
  385. else if(i == map->width && j > -1 && j < map->height)
  386. {
  387. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[25+rand()%2].bitmap);
  388. continue;
  389. }
  390. else
  391. {
  392. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[rand()%16].bitmap);
  393. continue;
  394. }
  395. }
  396. //TerrainTile zz = map->terrain[i-Woff][j-Hoff];
  397. std::string name;
  398. if (k>0)
  399. name = nameFromType(map->undergroungTerrain[i][j].tertype);
  400. else
  401. name = nameFromType(map->terrain[i][j].tertype);
  402. for (unsigned int m=0; m<defs.size(); m++)
  403. {
  404. try
  405. {
  406. if (defs[m]->defName != name)
  407. continue;
  408. else
  409. {
  410. int ktora;
  411. if (k==0)
  412. ktora = map->terrain[i][j].terview;
  413. else
  414. ktora = map->undergroungTerrain[i][j].terview;
  415. ttiles[i][j][k].terbitmap.push_back(defs[m]->ourImages[ktora].bitmap);
  416. int zz;
  417. if (k==0)
  418. zz = (map->terrain[i][j].siodmyTajemniczyBajt)%4;
  419. else
  420. zz = (map->undergroungTerrain[i][j].siodmyTajemniczyBajt)%4;
  421. switch (zz)
  422. {
  423. case 1:
  424. {
  425. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].terbitmap[0]);
  426. break;
  427. }
  428. case 2:
  429. {
  430. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].terbitmap[0]);
  431. break;
  432. }
  433. case 3:
  434. {
  435. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate03(ttiles[i][j][k].terbitmap[0]);
  436. break;
  437. }
  438. }
  439. break;
  440. }
  441. }
  442. catch (...)
  443. {
  444. continue;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. delete bord;
  451. }
  452. void CMapHandler::initObjectRects()
  453. {
  454. //initializing objects / rects
  455. for(int f=0; f<map->objects.size(); ++f)
  456. {
  457. /*map->objects[f]->pos.x+=1;
  458. map->objects[f]->pos.y+=1;*/
  459. if(!map->objects[f]->defInfo)
  460. {
  461. continue;
  462. }
  463. CDefHandler * curd = map->objects[f]->defInfo->handler;
  464. if(curd)
  465. {
  466. for(int fx=0; fx<curd->ourImages[0].bitmap->w>>5; ++fx) //curd->ourImages[0].bitmap->w/32
  467. {
  468. for(int fy=0; fy<curd->ourImages[0].bitmap->h>>5; ++fy) //curd->ourImages[0].bitmap->h/32
  469. {
  470. SDL_Rect cr;
  471. cr.w = 32;
  472. cr.h = 32;
  473. cr.x = fx<<5; //fx*32
  474. cr.y = fy<<5; //fy*32
  475. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(map->objects[f],cr);
  476. if((map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1)>=0 && (map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)>=0 && (map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
  477. {
  478. //TerrainTile2 & curt =
  479. // ttiles
  480. // [map->objects[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  481. //[map->objects[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  482. //[map->objects[f]->pos.z];
  483. 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);
  484. }
  485. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  486. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  487. }//if curd
  488. } // for(int f=0; f<map->objects.size(); ++f)
  489. for(int ix=0; ix<ttiles.size()-Woff; ++ix)
  490. {
  491. for(int iy=0; iy<ttiles[0].size()-Hoff; ++iy)
  492. {
  493. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  494. {
  495. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  496. }
  497. }
  498. }
  499. }
  500. void CMapHandler::calculateBlockedPos()
  501. {
  502. for(int f=0; f<map->objects.size(); ++f) //calculationg blocked / visitable positions
  503. {
  504. if(!map->objects[f]->defInfo)
  505. continue;
  506. CDefHandler * curd = map->objects[f]->defInfo->handler;
  507. for(int fx=0; fx<8; ++fx)
  508. {
  509. for(int fy=0; fy<6; ++fy)
  510. {
  511. int xVal = map->objects[f]->pos.x + fx - 7;
  512. int yVal = map->objects[f]->pos.y + fy - 5;
  513. int zVal = map->objects[f]->pos.z;
  514. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  515. {
  516. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  517. if(((map->objects[f]->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  518. curt.visitable = true;
  519. if(!((map->objects[f]->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  520. curt.blocked = true;
  521. }
  522. }
  523. }
  524. }
  525. }
  526. void processDef (CGDefInfo* def)
  527. {
  528. def->handler=CDefHandler::giveDef(def->name);
  529. def->width = def->handler->ourImages[0].bitmap->w/32;
  530. def->height = def->handler->ourImages[0].bitmap->h/32;
  531. CGDefInfo* pom = CGI->dobjinfo->gobjs[def->id][def->subid];
  532. if(pom)
  533. {
  534. pom->handler = def->handler;
  535. pom->width = pom->handler->ourImages[0].bitmap->w/32;
  536. pom->height = pom->handler->ourImages[0].bitmap->h/32;
  537. }
  538. else
  539. std::cout << "\t\tMinor warning: lacking def info for " << def->id << " " << def->subid <<" " << def->name << std::endl;
  540. if(!def->handler->alphaTransformed)
  541. {
  542. for(int yy=0; yy<def->handler->ourImages.size(); ++yy)
  543. {
  544. def->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(def->handler->ourImages[yy].bitmap);
  545. def->handler->alphaTransformed = true;
  546. }
  547. }
  548. }
  549. void CMapHandler::init()
  550. {
  551. timeHandler th;
  552. th.getDif();
  553. std::for_each(map->defy.begin(),map->defy.end(),processDef); //load h3m defs
  554. std::for_each(map->defs.begin(),map->defs.end(),processDef); //and non-h3m defs
  555. THC std::cout<<"\tUnpacking and handling defs: "<<th.getDif()<<std::endl;
  556. //loading castles' defs
  557. //std::ifstream ifs("config/townsDefs.txt");
  558. //int ccc;
  559. //ifs>>ccc;
  560. //for(int i=0;i<ccc*2;i++)
  561. //{
  562. // //CGDefInfo * n = new CGDefInfo(*CGI->dobjinfo->castles[i%ccc]);
  563. // ifs >> n->name;
  564. // if (!(n->handler = CDefHandler::giveDef(n->name)))
  565. // std::cout << "Cannot open "<<n->name<<std::endl;
  566. // else
  567. // {
  568. // n->width = n->handler->ourImages[0].bitmap->w/32;
  569. // n->height = n->handler->ourImages[0].bitmap->h/32;
  570. // }
  571. // if(i<ccc)
  572. // villages[i]=n;
  573. // else
  574. // capitols[i%ccc]=n;
  575. // alphaTransformDef(n);
  576. //}
  577. for(int i=0;i<PLAYER_LIMIT;i++)
  578. {
  579. for(int j=0; j<map->players[i].heroesNames.size();j++)
  580. {
  581. usedHeroes.insert(map->players[i].heroesNames[j].heroID);
  582. }
  583. }
  584. std::cout<<"\tLoading town defs, picking random factions and heroes: "<<th.getDif()<<std::endl;
  585. for(int h=0; h<map->defy.size(); ++h) //initializing loaded def handler's info {
  586. CGI->mh->loadedDefs.insert(std::make_pair(map->defy[h]->name, map->defy[h]->handler));
  587. std::cout<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  588. prepareFOWDefs();
  589. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  590. borderAndTerrainBitmapInit();
  591. std::cout<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  592. //giving starting hero
  593. //for(int i=0;i<PLAYER_LIMIT;i++)
  594. //{
  595. // if((map->players[i].generateHeroAtMainTown && map->players[i].hasMainTown) || (map->players[i].hasMainTown && map->version==RoE))
  596. // {
  597. // int3 hpos = map->players[i].posOfMainTown;
  598. // hpos.x+=1;// hpos.y+=1;
  599. // int j;
  600. // for(j=0;j<CGI->state->scenarioOps->playerInfos.size();j++)
  601. // if(CGI->state->scenarioOps->playerInfos[j].color==i)
  602. // break;
  603. // if(j==CGI->state->scenarioOps->playerInfos.size())
  604. // continue;
  605. // int h=pickHero(i);
  606. // CGHeroInstance * nnn = (CGHeroInstance*)createObject(34,h,hpos,i);
  607. // nnn->defInfo->handler = graphics->flags1[0];
  608. // map->heroes.push_back(nnn);
  609. // map->objects.push_back(nnn);
  610. // }
  611. //}
  612. std::cout<<"\tGiving starting heroes: "<<th.getDif()<<std::endl;
  613. initObjectRects();
  614. std::cout<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  615. calculateBlockedPos();
  616. std::cout<<"\tCalculating blockmap: "<<th.getDif()<<std::endl;
  617. }
  618. 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)
  619. {
  620. if(!otherHeroAnim)
  621. heroAnim = anim; //the same, as it should be
  622. //setting surface to blit at
  623. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  624. if(extSurf)
  625. {
  626. su = extSurf;
  627. }
  628. else
  629. {
  630. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  631. }
  632. if (((dx+x)>((map->width+Woff)) || (dy+y)>((map->height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  633. throw new std::string("terrainRect: out of range");
  634. ////printing terrain
  635. for (int bx=0; bx<dx; bx++)
  636. {
  637. for (int by=0; by<dy; by++)
  638. {
  639. SDL_Rect sr;
  640. sr.y=by*32;
  641. sr.x=bx*32;
  642. sr.h=sr.w=32;
  643. validateRectTerr(&sr, extRect);
  644. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap[anim%ttiles[x+bx][y+by][level].terbitmap.size()],&genRect(sr.h, sr.w, 0, 0),su,&sr);
  645. }
  646. }
  647. ////terrain printed
  648. ////printing rivers
  649. for (int bx=0; bx<dx; bx++)
  650. {
  651. for (int by=0; by<dy; by++)
  652. {
  653. SDL_Rect sr;
  654. sr.y=by*32;
  655. sr.x=bx*32;
  656. sr.h=sr.w=32;
  657. validateRectTerr(&sr, extRect);
  658. if(ttiles[x+bx][y+by][level].rivbitmap.size())
  659. {
  660. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].rivbitmap[anim%ttiles[x+bx][y+by][level].rivbitmap.size()],&genRect(sr.h, sr.w, 0, 0),su,&sr);
  661. }
  662. }
  663. }
  664. ////rivers printed
  665. ////printing roads
  666. for (int bx=0; bx<dx; bx++)
  667. {
  668. for (int by=-1; by<dy; by++)
  669. {
  670. if(y+by<=-4)
  671. continue;
  672. SDL_Rect sr;
  673. sr.y=by*32+16;
  674. sr.x=bx*32;
  675. sr.h=sr.w=32;
  676. validateRectTerr(&sr, extRect);
  677. if(ttiles[x+bx][y+by][level].roadbitmap.size())
  678. {
  679. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].roadbitmap[anim%ttiles[x+bx][y+by][level].roadbitmap.size()], &genRect(sr.h, sr.w, 0, (by==-1 ? 16 : 0)),su,&sr);
  680. }
  681. }
  682. }
  683. ////roads printed
  684. ////printing objects
  685. for (int bx=0; bx<dx; bx++)
  686. {
  687. for (int by=0; by<dy; by++)
  688. {
  689. for(int h=0; h<ttiles[x+bx][y+by][level].objects.size(); ++h)
  690. {
  691. SDL_Rect sr;
  692. sr.w = 32;
  693. sr.h = 32;
  694. sr.x = (bx)*32;
  695. sr.y = (by)*32;
  696. validateRectTerr(&sr, extRect);
  697. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  698. pp.h = sr.h;
  699. pp.w = sr.w;
  700. CGHeroInstance * themp = (dynamic_cast<CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  701. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  702. {
  703. int imgVal = 8;
  704. SDL_Surface * tb;
  705. if(themp->type==NULL)
  706. continue;
  707. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  708. int gg;
  709. for(gg=0; gg<iv.size(); ++gg)
  710. {
  711. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  712. {
  713. tb = iv[gg+heroAnim%imgVal].bitmap;
  714. break;
  715. }
  716. }
  717. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  718. pp.y+=imgVal*2-32;
  719. sr.y-=16;
  720. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  721. }
  722. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  723. {
  724. int imgVal = 8;
  725. SDL_Surface * tb;
  726. if(themp->type==NULL)
  727. continue;
  728. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  729. int gg;
  730. for(gg=0; gg<iv.size(); ++gg)
  731. {
  732. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  733. {
  734. tb = iv[gg].bitmap;
  735. break;
  736. }
  737. }
  738. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  739. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  740. {
  741. SDL_Rect bufr = sr;
  742. bufr.x-=2*32;
  743. bufr.y-=1*32;
  744. bufr.h = 64;
  745. bufr.w = 96;
  746. if(bufr.x-extRect->x>-64)
  747. SDL_BlitSurface(graphics->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, su, &bufr);
  748. themp->flagPrinted = true;
  749. }
  750. }
  751. else
  752. {
  753. int imgVal = ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages.size();
  754. int phaseShift = ttiles[x+bx][y+by][level].objects[h].first->animPhaseShift;
  755. //setting appropriate flag color
  756. if((ttiles[x+bx][y+by][level].objects[h].first->tempOwner>=0 && ttiles[x+bx][y+by][level].objects[h].first->tempOwner<8) || ttiles[x+bx][y+by][level].objects[h].first->tempOwner==255)
  757. CSDL_Ext::setPlayerColor(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[(anim+phaseShift)%imgVal].bitmap, ttiles[x+bx][y+by][level].objects[h].first->tempOwner);
  758. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[(anim+phaseShift)%imgVal].bitmap,&pp,su,&sr);
  759. }
  760. }
  761. }
  762. }
  763. ////objects printed, printing shadow
  764. for (int bx=0; bx<dx; bx++)
  765. {
  766. for (int by=0; by<dy; by++)
  767. {
  768. SDL_Rect sr;
  769. sr.y=by*32;
  770. sr.x=bx*32;
  771. sr.h=sr.w=32;
  772. validateRectTerr(&sr, extRect);
  773. 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])
  774. {
  775. SDL_Surface * hide = getVisBitmap(bx+x, by+y, *visibilityMap, level);
  776. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  777. }
  778. }
  779. }
  780. ////shadow printed
  781. //printing borders
  782. for (int bx=0; bx<dx; bx++)
  783. {
  784. for (int by=0; by<dy; by++)
  785. {
  786. if(bx+x<0 || by+y<0 || bx+x>map->width+(-1) || by+y>map->height+(-1))
  787. {
  788. SDL_Rect sr;
  789. sr.y=by*32;
  790. sr.x=bx*32;
  791. sr.h=sr.w=32;
  792. validateRectTerr(&sr, extRect);
  793. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap[anim%ttiles[x+bx][y+by][level].terbitmap.size()],&genRect(sr.h, sr.w, 0, 0),su,&sr);
  794. }
  795. else
  796. {
  797. if(MARK_BLOCKED_POSITIONS && ttiles[x+bx][y+by][level].blocked) //temporary hiding blocked positions
  798. {
  799. SDL_Rect sr;
  800. sr.y=by*32;
  801. sr.x=bx*32;
  802. sr.h=sr.w=32;
  803. validateRectTerr(&sr, extRect);
  804. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  805. for(int f=0; f<ns->w*ns->h*4; ++f)
  806. {
  807. *((unsigned char*)(ns->pixels) + f) = 128;
  808. }
  809. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  810. SDL_FreeSurface(ns);
  811. }
  812. if(MARK_VISITABLE_POSITIONS && ttiles[x+bx][y+by][level].visitable) //temporary hiding visitable positions
  813. {
  814. SDL_Rect sr;
  815. sr.y=by*32;
  816. sr.x=bx*32;
  817. sr.h=sr.w=32;
  818. validateRectTerr(&sr, extRect);
  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. }
  828. }
  829. }
  830. //borders printed
  831. return su;
  832. }
  833. SDL_Surface * CMapHandler::terrBitmap(int x, int y)
  834. {
  835. return ttiles[x+Woff][y+Hoff][0].terbitmap[0];
  836. }
  837. SDL_Surface * CMapHandler::undTerrBitmap(int x, int y)
  838. {
  839. return ttiles[x+Woff][y+Hoff][0].terbitmap[1];
  840. }
  841. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, std::vector< std::vector< std::vector<unsigned char> > > & visibilityMap, int lvl)
  842. {
  843. int size = visibilityMap.size()-1; //is tile visible. arrangement: (like num keyboard)
  844. bool d7 = (x>0 && y>0) ? visibilityMap[x-1][y-1][lvl] : 0, //789
  845. d8 = (y>0) ? visibilityMap[x][y-1][lvl] : 0, //456
  846. d9 = (y>0 && x<size) ? visibilityMap[x+1][y-1][lvl] : 0,//123
  847. d4 = (x>0) ? visibilityMap[x-1][y][lvl] : 0,
  848. d5 = visibilityMap[x][y][lvl],
  849. d6 = (x<size) ? visibilityMap[x+1][y][lvl] : 0,
  850. d1 = (x>0 && y<size) ? visibilityMap[x-1][y+1][lvl] : 0,
  851. d2 = (y<size) ? visibilityMap[x][y+1][lvl] : 0,
  852. d3 = (x<size && y<size) ? visibilityMap[x+1][y+1][lvl] : 0;
  853. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && !d1)
  854. {
  855. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  856. }
  857. else if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1)
  858. {
  859. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  860. }
  861. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1)
  862. {
  863. return partialHide->ourImages[15].bitmap; //visible right top corner
  864. }
  865. else if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1)
  866. {
  867. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  868. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  869. }
  870. else if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && !d1)
  871. {
  872. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  873. return partialHide->ourImages[35].bitmap;
  874. }
  875. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && d9 && !d1)
  876. {
  877. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  878. return partialHide->ourImages[0].bitmap; //visible top
  879. }
  880. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  881. {
  882. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  883. return partialHide->ourImages[4].bitmap; //visble bottom
  884. }
  885. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && d1)
  886. {
  887. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  888. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  889. return partialHide->ourImages[36].bitmap;
  890. }
  891. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  892. {
  893. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  894. return partialHide->ourImages[2].bitmap; //visible right
  895. }
  896. else if(d2 && d6 && !d4 && !d8 && !d7)
  897. {
  898. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  899. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  900. }
  901. else if(!d2 && d6 && !d4 && d8 && !d1)
  902. {
  903. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  904. }
  905. else if(!d2 && !d6 && d4 && d8 && !d3)
  906. {
  907. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  908. return partialHide->ourImages[37].bitmap;
  909. }
  910. else if(d2 && !d6 && d4 && !d8 && !d9)
  911. {
  912. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  913. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  914. return partialHide->ourImages[38].bitmap;
  915. }
  916. else if(d2 && d6 && d4 && d8)
  917. {
  918. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  919. }
  920. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && !d1)
  921. {
  922. return partialHide->ourImages[16].bitmap; //visible right corners
  923. }
  924. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && !d1)
  925. {
  926. return partialHide->ourImages[18].bitmap; //visible top corners
  927. }
  928. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && !d9 && d1)
  929. {
  930. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  931. return partialHide->ourImages[39].bitmap;
  932. }
  933. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && d1)
  934. {
  935. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  936. return partialHide->ourImages[40].bitmap;
  937. }
  938. if(!d2 && !d6 && !d4 && !d8 && !d7 && !d3 && d9 && d1)
  939. {
  940. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  941. }
  942. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && !d1)
  943. {
  944. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  945. return partialHide->ourImages[41].bitmap;
  946. }
  947. if(!d2 && !d6 && !d4 && !d8 && !d7 && d3 && d9 && d1)
  948. {
  949. return partialHide->ourImages[19].bitmap; //visible corners without left top
  950. }
  951. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && !d1)
  952. {
  953. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  954. }
  955. if(!d2 && !d6 && !d4 && !d8 && d7 && !d3 && d9 && d1)
  956. {
  957. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  958. return partialHide->ourImages[42].bitmap;
  959. }
  960. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && !d9 && d1)
  961. {
  962. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  963. return partialHide->ourImages[43].bitmap;
  964. }
  965. if(!d2 && !d6 && !d4 && !d8 && d7 && d3 && d9 && d1)
  966. {
  967. return partialHide->ourImages[21].bitmap; //visible all corners only
  968. }
  969. if(d2 && d6 && d4 && !d8)
  970. {
  971. return partialHide->ourImages[6].bitmap; //hidden top
  972. }
  973. if(d2 && !d6 && d4 && d8)
  974. {
  975. return partialHide->ourImages[7].bitmap; //hidden right
  976. }
  977. if(!d2 && d6 && d4 && d8)
  978. {
  979. return partialHide->ourImages[8].bitmap; //hidden bottom
  980. }
  981. if(d2 && d6 && !d4 && d8)
  982. {
  983. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  984. return partialHide->ourImages[44].bitmap;
  985. }
  986. if(!d2 && d6 && d4 && !d8)
  987. {
  988. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  989. }
  990. if(d2 && !d6 && !d4 && d8)
  991. {
  992. return partialHide->ourImages[29].bitmap; //hidden left and right
  993. }
  994. if(!d2 && !d6 && !d4 && d8 && d3 && !d1)
  995. {
  996. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  997. }
  998. if(!d2 && !d6 && !d4 && d8 && !d3 && d1)
  999. {
  1000. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  1001. return partialHide->ourImages[45].bitmap;
  1002. }
  1003. if(!d2 && !d6 && !d4 && d8 && d3 && d1)
  1004. {
  1005. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1006. }
  1007. if(!d2 && !d6 && d4 && !d8 && !d3 && d9)
  1008. {
  1009. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  1010. return partialHide->ourImages[46].bitmap;
  1011. }
  1012. if(!d2 && !d6 && d4 && !d8 && d3 && !d9)
  1013. {
  1014. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  1015. return partialHide->ourImages[47].bitmap;
  1016. }
  1017. if(!d2 && !d6 && d4 && !d8 && d3 && d9)
  1018. {
  1019. return partialHide->ourImages[32].bitmap; //visible left and right corners
  1020. }
  1021. if(d2 && !d6 && !d4 && !d8 && d7 && !d9)
  1022. {
  1023. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  1024. return partialHide->ourImages[48].bitmap;
  1025. }
  1026. if(d2 && !d6 && !d4 && !d8 && !d7 && d9)
  1027. {
  1028. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  1029. }
  1030. if(d2 && !d6 && !d4 && !d8 && d7 && d9)
  1031. {
  1032. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1033. }
  1034. if(!d2 && d6 && !d4 && !d8 && !d7 && d1)
  1035. {
  1036. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  1037. }
  1038. if(!d2 && d6 && !d4 && !d8 && d7 && !d1)
  1039. {
  1040. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  1041. }
  1042. if(!d2 && d6 && !d4 && !d8 && d7 && d1)
  1043. {
  1044. //return CSDL_Ext::rotate01(partialHide->ourImages[32].bitmap); //visible right and left cornres
  1045. return partialHide->ourImages[49].bitmap;
  1046. }
  1047. if(d2 && d6 && !d4 && !d8 && d7)
  1048. {
  1049. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1050. }
  1051. else if(!d2 && d6 && !d4 && d8 && d1)
  1052. {
  1053. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1054. }
  1055. else if(!d2 && !d6 && d4 && d8 && d3)
  1056. {
  1057. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  1058. return partialHide->ourImages[50].bitmap;
  1059. }
  1060. else if(d2 && !d6 && d4 && !d8 && d9)
  1061. {
  1062. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  1063. return partialHide->ourImages[51].bitmap;
  1064. }
  1065. //newly added
  1066. else if(!d2 && !d6 && !d4 && d8 && !d7 && !d3 && d9 && !d1) //visible t and tr
  1067. {
  1068. return partialHide->ourImages[0].bitmap;
  1069. }
  1070. else if(!d2 && !d6 && !d4 && d8 && d7 && !d3 && !d9 && !d1) //visible t and tl
  1071. {
  1072. return partialHide->ourImages[1].bitmap;
  1073. }
  1074. else if(d2 && !d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible b and br
  1075. {
  1076. return partialHide->ourImages[4].bitmap;
  1077. }
  1078. else if(d2 && !d6 && !d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible b and bl
  1079. {
  1080. return partialHide->ourImages[5].bitmap;
  1081. }
  1082. else if(!d2 && !d6 && d4 && !d8 && d7 && !d3 && !d9 && !d1) //visible l and tl
  1083. {
  1084. return partialHide->ourImages[36].bitmap;
  1085. }
  1086. else if(!d2 && !d6 && d4 && !d8 && !d7 && !d3 && !d9 && d1) //visible l and bl
  1087. {
  1088. return partialHide->ourImages[36].bitmap;
  1089. }
  1090. else if(!d2 && d6 && !d4 && !d8 && !d7 && !d3 && d9 && !d1) //visible r and tr
  1091. {
  1092. return partialHide->ourImages[2].bitmap;
  1093. }
  1094. else if(!d2 && d6 && !d4 && !d8 && !d7 && d3 && !d9 && !d1) //visible r and br
  1095. {
  1096. return partialHide->ourImages[3].bitmap;
  1097. }
  1098. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1099. }
  1100. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1101. {
  1102. int ret=-1;
  1103. if(a.x>=CGI->mh->map->width && a.y>=CGI->mh->map->height)
  1104. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->map->width-1][CGI->mh->map->width-1][a.z].malle];
  1105. else 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][a.y][a.z].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[a.x][CGI->mh->map->width-1][a.z].malle];
  1109. else
  1110. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].malle];
  1111. if(!(a.x==b.x || a.y==b.y))
  1112. ret*=1.41421;
  1113. //TODO: use hero's pathfinding skill during calculating cost
  1114. return ret;
  1115. }
  1116. std::vector < std::string > CMapHandler::getObjDescriptions(int3 pos)
  1117. {
  1118. std::vector < std::pair<CGObjectInstance*,SDL_Rect > > objs = ttiles[pos.x][pos.y][pos.z].objects;
  1119. std::vector<std::string> ret;
  1120. for(int g=0; g<objs.size(); ++g)
  1121. {
  1122. if( (5-(objs[g].first->pos.y-pos.y)) >= 0 && (5-(objs[g].first->pos.y-pos.y)) < 6 && (objs[g].first->pos.x-pos.x) >= 0 && (objs[g].first->pos.x-pos.x)<7 && objs[g].first->defInfo &&
  1123. (((objs[g].first->defInfo->blockMap[5-(objs[g].first->pos.y-pos.y)])>>((objs[g].first->pos.x-pos.x)))&1)==0
  1124. ) //checking position blocking
  1125. {
  1126. //unsigned char * blm = objs[g].first->defInfo->blockMap;
  1127. if (objs[g].first->state)
  1128. ret.push_back(objs[g].first->state->hoverText(objs[g].first));
  1129. else
  1130. ret.push_back(CGI->objh->objects[objs[g].first->ID].name);
  1131. }
  1132. }
  1133. return ret;
  1134. }
  1135. std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1136. {
  1137. std::vector < CGObjectInstance * > ret;
  1138. for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1139. {
  1140. CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1141. if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1142. ret.push_back(curi);
  1143. }
  1144. return ret;
  1145. }
  1146. CGObjectInstance * CMapHandler::createObject(int id, int subid, int3 pos, int owner)
  1147. {
  1148. CGObjectInstance * nobj;
  1149. switch(id)
  1150. {
  1151. case 34: //hero
  1152. {
  1153. CGHeroInstance * nobj;
  1154. nobj = new CGHeroInstance();
  1155. nobj->pos = pos;
  1156. nobj->tempOwner = owner;
  1157. nobj->defInfo = new CGDefInfo();
  1158. nobj->defInfo->id = 34;
  1159. nobj->defInfo->subid = subid;
  1160. nobj->defInfo->printPriority = 0;
  1161. nobj->type = CGI->heroh->heroes[subid];
  1162. for(int i=0;i<6;i++)
  1163. {
  1164. nobj->defInfo->blockMap[i]=255;
  1165. nobj->defInfo->visitMap[i]=0;
  1166. }
  1167. nobj->ID = id;
  1168. nobj->subID = subid;
  1169. nobj->defInfo->handler=NULL;
  1170. nobj->defInfo->blockMap[5] = 253;
  1171. nobj->defInfo->visitMap[5] = 2;
  1172. nobj->artifacts.resize(20);
  1173. nobj->artifWorn[16] = 3;
  1174. nobj->primSkills.resize(4);
  1175. nobj->primSkills[0] = nobj->type->heroClass->initialAttack;
  1176. nobj->primSkills[1] = nobj->type->heroClass->initialDefence;
  1177. nobj->primSkills[2] = nobj->type->heroClass->initialPower;
  1178. nobj->primSkills[3] = nobj->type->heroClass->initialKnowledge;
  1179. nobj->mana = 10 * nobj->primSkills[3];
  1180. return nobj;
  1181. }
  1182. case 98: //town
  1183. nobj = new CGTownInstance;
  1184. break;
  1185. default: //rest of objects
  1186. nobj = new CGObjectInstance;
  1187. nobj->defInfo = CGI->dobjinfo->gobjs[id][subid];
  1188. break;
  1189. }
  1190. nobj->ID = id;
  1191. nobj->subID = subid;
  1192. if(!nobj->defInfo)
  1193. std::cout <<"No def declaration for " <<id <<" "<<subid<<std::endl;
  1194. nobj->pos = pos;
  1195. //nobj->state = NULL;//new CLuaObjectScript();
  1196. nobj->tempOwner = owner;
  1197. nobj->info = NULL;
  1198. nobj->defInfo->id = id;
  1199. nobj->defInfo->subid = subid;
  1200. //assigning defhandler
  1201. if(nobj->ID==34 || nobj->ID==98)
  1202. return nobj;
  1203. nobj->defInfo = CGI->dobjinfo->gobjs[id][subid];
  1204. if(!nobj->defInfo->handler)
  1205. {
  1206. nobj->defInfo->handler = CDefHandler::giveDef(nobj->defInfo->name);
  1207. nobj->defInfo->width = nobj->defInfo->handler->ourImages[0].bitmap->w/32;
  1208. nobj->defInfo->height = nobj->defInfo->handler->ourImages[0].bitmap->h/32;
  1209. }
  1210. return nobj;
  1211. }
  1212. std::string CMapHandler::getDefName(int id, int subid)
  1213. {
  1214. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1215. if(temp)
  1216. return temp->name;
  1217. throw new std::exception("Def not found.");
  1218. }
  1219. bool CMapHandler::printObject(CGObjectInstance *obj)
  1220. {
  1221. CDefHandler * curd = obj->defInfo->handler;
  1222. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1223. {
  1224. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1225. {
  1226. SDL_Rect cr;
  1227. cr.w = 32;
  1228. cr.h = 32;
  1229. cr.x = fx*32;
  1230. cr.y = fy*32;
  1231. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1232. 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)
  1233. {
  1234. TerrainTile2 & curt =
  1235. ttiles
  1236. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1237. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1238. [obj->pos.z];
  1239. 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);
  1240. }
  1241. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1242. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1243. return true;
  1244. }
  1245. bool CMapHandler::hideObject(CGObjectInstance *obj)
  1246. {
  1247. CDefHandler * curd = obj->defInfo->handler;
  1248. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1249. {
  1250. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1251. {
  1252. 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)
  1253. {
  1254. std::vector < std::pair<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;
  1255. for(int dd=0; dd<ctile.size(); ++dd)
  1256. {
  1257. if(ctile[dd].first->id==obj->id)
  1258. ctile.erase(ctile.begin() + dd);
  1259. }
  1260. }
  1261. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1262. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1263. return true;
  1264. }
  1265. std::string CMapHandler::getRandomizedDefName(CGDefInfo *di, CGObjectInstance * obj)
  1266. {
  1267. return std::string();
  1268. }
  1269. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1270. {
  1271. hideObject(obj);
  1272. std::vector<CGObjectInstance *>::iterator db = std::find(map->objects.begin(), map->objects.end(), obj);
  1273. recalculateHideVisPosUnderObj(*db);
  1274. delete *db;
  1275. map->objects.erase(db);
  1276. return true;
  1277. }
  1278. bool CMapHandler::recalculateHideVisPos(int3 &pos)
  1279. {
  1280. ttiles[pos.x][pos.y][pos.z].visitable = false;
  1281. ttiles[pos.x][pos.y][pos.z].blocked = false;
  1282. for(int i=0; i<ttiles[pos.x][pos.y][pos.z].objects.size(); ++i)
  1283. {
  1284. CDefHandler * curd = ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->handler;
  1285. for(int fx=0; fx<8; ++fx)
  1286. {
  1287. for(int fy=0; fy<6; ++fy)
  1288. {
  1289. int xVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.x + fx - 7;
  1290. int yVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.y + fy - 5;
  1291. int zVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.z;
  1292. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  1293. {
  1294. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  1295. if(((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  1296. curt.visitable = true;
  1297. if(!((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  1298. curt.blocked = true;
  1299. }
  1300. }
  1301. }
  1302. }
  1303. return true;
  1304. }
  1305. bool CMapHandler::recalculateHideVisPosUnderObj(CGObjectInstance *obj, bool withBorder)
  1306. {
  1307. if(withBorder)
  1308. {
  1309. for(int fx=-1; fx<=obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1310. {
  1311. for(int fy=-1; fy<=obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1312. {
  1313. if((obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)>=0 && (obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)>=0 && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
  1314. {
  1315. recalculateHideVisPos(int3(obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32 +1, obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32 + 1, obj->pos.z));
  1316. }
  1317. }
  1318. }
  1319. }
  1320. else
  1321. {
  1322. for(int fx=0; fx<obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1323. {
  1324. for(int fy=0; fy<obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1325. {
  1326. if((obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)>=0 && (obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)>=0 && (obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
  1327. {
  1328. recalculateHideVisPos(int3(obj->pos.x + fx - obj->defInfo->handler->ourImages[0].bitmap->w/32 +1, obj->pos.y + fy - obj->defInfo->handler->ourImages[0].bitmap->h/32 + 1, obj->pos.z));
  1329. }
  1330. }
  1331. }
  1332. }
  1333. return true;
  1334. }
  1335. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1336. {
  1337. if(isMoving)
  1338. {
  1339. switch(dir)
  1340. {
  1341. case 1:
  1342. return 10;
  1343. case 2:
  1344. return 5;
  1345. case 3:
  1346. return 6;
  1347. case 4:
  1348. return 7;
  1349. case 5:
  1350. return 8;
  1351. case 6:
  1352. return 9;
  1353. case 7:
  1354. return 12;
  1355. case 8:
  1356. return 11;
  1357. default:
  1358. return -1; //should never happen
  1359. }
  1360. }
  1361. else //if(isMoving)
  1362. {
  1363. switch(dir)
  1364. {
  1365. case 1:
  1366. return 13;
  1367. case 2:
  1368. return 0;
  1369. case 3:
  1370. return 1;
  1371. case 4:
  1372. return 2;
  1373. case 5:
  1374. return 3;
  1375. case 6:
  1376. return 4;
  1377. case 7:
  1378. return 15;
  1379. case 8:
  1380. return 14;
  1381. default:
  1382. return -1; //should never happen
  1383. }
  1384. }
  1385. }
  1386. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1387. {
  1388. if(ext)
  1389. {
  1390. if(val->x<0)
  1391. {
  1392. val->w += val->x;
  1393. val->x = ext->x;
  1394. }
  1395. else
  1396. {
  1397. val->x += ext->x;
  1398. }
  1399. if(val->y<0)
  1400. {
  1401. val->h += val->y;
  1402. val->y = ext->y;
  1403. }
  1404. else
  1405. {
  1406. val->y += ext->y;
  1407. }
  1408. if(val->x+val->w > ext->x+ext->w)
  1409. {
  1410. val->w = ext->x+ext->w-val->x;
  1411. }
  1412. if(val->y+val->h > ext->y+ext->h)
  1413. {
  1414. val->h = ext->y+ext->h-val->y;
  1415. }
  1416. //for sign problems
  1417. if(val->h > 20000 || val->w > 20000)
  1418. {
  1419. val->h = val->w = 0;
  1420. }
  1421. }
  1422. }
  1423. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1424. {
  1425. if(a.z!=b.z)
  1426. return -1; //error!
  1427. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1428. {
  1429. return 0;
  1430. }
  1431. else if(a.x==b.x && a.y==b.y+1) //t
  1432. {
  1433. return 1;
  1434. }
  1435. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1436. {
  1437. return 2;
  1438. }
  1439. else if(a.x==b.x-1 && a.y==b.y) //r
  1440. {
  1441. return 3;
  1442. }
  1443. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1444. {
  1445. return 4;
  1446. }
  1447. else if(a.x==b.x && a.y==b.y-1) //b
  1448. {
  1449. return 5;
  1450. }
  1451. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1452. {
  1453. return 6;
  1454. }
  1455. else if(a.x==b.x+1 && a.y==b.y) //l
  1456. {
  1457. return 7;
  1458. }
  1459. return -2; //shouldn't happen
  1460. }
  1461. void CMapHandler::loadDefs()
  1462. {
  1463. std::set<int> loadedTypes;
  1464. for (int i=0; i<map->width; i++)
  1465. {
  1466. for (int j=0; j<map->width; j++)
  1467. {
  1468. if (loadedTypes.find(map->terrain[i][j].tertype)==loadedTypes.end())
  1469. {
  1470. CDefHandler *sdh = CDefHandler::giveDef(nameFromType(map->terrain[i][j].tertype).c_str());
  1471. loadedTypes.insert(map->terrain[i][j].tertype);
  1472. defs.push_back(sdh);
  1473. }
  1474. if (map->twoLevel && loadedTypes.find(map->undergroungTerrain[i][j].tertype)==loadedTypes.end())
  1475. {
  1476. CDefHandler *sdh = CDefHandler::giveDef(nameFromType(map->undergroungTerrain[i][j].tertype).c_str());
  1477. loadedTypes.insert(map->undergroungTerrain[i][j].tertype);
  1478. defs.push_back(sdh);
  1479. }
  1480. }
  1481. }
  1482. }