mapHandler.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. #include "stdafx.h"
  2. #include "mapHandler.h"
  3. #include "hch\CSemiDefHandler.h"
  4. #include "SDL_rotozoom.h"
  5. #include "SDL_Extensions.h"
  6. #include "CGameInfo.h"
  7. #include "stdlib.h"
  8. #include "hch\CLodHandler.h"
  9. #include "hch\CDefObjInfoHandler.h"
  10. #include <algorithm>
  11. #include "CGameState.h"
  12. #include "CLua.h"
  13. #include "hch\CCastleHandler.h"
  14. #include "hch\CHeroHandler.h"
  15. #include "hch\CTownHandler.h"
  16. #include <iomanip>
  17. #include <sstream>
  18. extern SDL_Surface * ekran;
  19. class OCM_HLP
  20. {
  21. public:
  22. bool operator ()(const std::pair<CGObjectInstance*,std::pair<SDL_Rect, std::vector<std::list<int3>>>> & a, const std::pair<CGObjectInstance*,std::pair<SDL_Rect, std::vector<std::list<int3>>>> & b)
  23. {
  24. return (*a.first)<(*b.first);
  25. }
  26. } ocmptwo ;
  27. void alphaTransformDef(CGDefInfo * defInfo)
  28. {
  29. SDL_Surface * alphaTransSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, 12, 12, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
  30. for(int yy=0;yy<defInfo->handler->ourImages.size();yy++)
  31. {
  32. defInfo->handler->ourImages[yy].bitmap = CSDL_Ext::alphaTransform(defInfo->handler->ourImages[yy].bitmap);
  33. SDL_Surface * bufs = CSDL_Ext::secondAlphaTransform(defInfo->handler->ourImages[yy].bitmap, alphaTransSurf);
  34. SDL_FreeSurface(defInfo->handler->ourImages[yy].bitmap);
  35. defInfo->handler->ourImages[yy].bitmap = bufs;
  36. defInfo->handler->alphaTransformed = true;
  37. }
  38. SDL_FreeSurface(alphaTransSurf);
  39. }
  40. std::pair<int,int> CMapHandler::pickObject(CGObjectInstance *obj)
  41. {
  42. switch(obj->ID)
  43. {
  44. case 65: //random artifact
  45. return std::pair<int,int>(5,(rand()%136)+7); //tylko sensowny zakres - na poczatku sa katapulty itp, na koncu specjalne i blanki
  46. case 66: //random treasure artifact
  47. return std::pair<int,int>(5,CGI->arth->treasures[rand()%CGI->arth->treasures.size()]->id);
  48. case 67: //random minor artifact
  49. return std::pair<int,int>(5,CGI->arth->minors[rand()%CGI->arth->minors.size()]->id);
  50. case 68: //random major artifact
  51. return std::pair<int,int>(5,CGI->arth->majors[rand()%CGI->arth->majors.size()]->id);
  52. case 69: //random relic artifact
  53. return std::pair<int,int>(5,CGI->arth->relics[rand()%CGI->arth->relics.size()]->id);
  54. case 70: //random hero
  55. return std::pair<int,int>(34,rand()%CGI->heroh->heroes.size());
  56. case 71: //random monster
  57. return std::pair<int,int>(54,rand()%(CGI->creh->creatures.size()));
  58. case 72: //random monster lvl1
  59. return std::pair<int,int>(54,CGI->creh->levelCreatures[1][rand()%CGI->creh->levelCreatures[1].size()]->idNumber);
  60. case 73: //random monster lvl2
  61. return std::pair<int,int>(54,CGI->creh->levelCreatures[2][rand()%CGI->creh->levelCreatures[2].size()]->idNumber);
  62. case 74: //random monster lvl3
  63. return std::pair<int,int>(54,CGI->creh->levelCreatures[3][rand()%CGI->creh->levelCreatures[3].size()]->idNumber);
  64. case 75: //random monster lvl4
  65. return std::pair<int,int>(54,CGI->creh->levelCreatures[4][rand()%CGI->creh->levelCreatures[4].size()]->idNumber);
  66. case 76: //random resource
  67. return std::pair<int,int>(79,rand()%7); //now it's OH3 style, use %8 for mithril
  68. case 77: //random town
  69. return std::pair<int,int>(98,rand()%CGI->townh->towns.size());
  70. case 162: //random monster lvl5
  71. return std::pair<int,int>(54,CGI->creh->levelCreatures[5][rand()%CGI->creh->levelCreatures[5].size()]->idNumber);
  72. case 163: //random monster lvl6
  73. return std::pair<int,int>(54,CGI->creh->levelCreatures[6][rand()%CGI->creh->levelCreatures[6].size()]->idNumber);
  74. case 164: //random monster lvl7
  75. return std::pair<int,int>(54,CGI->creh->levelCreatures[7][rand()%CGI->creh->levelCreatures[7].size()]->idNumber);
  76. case 216: //random dwelling
  77. {
  78. int faction = rand()%F_NUMBER;
  79. CCreGen2ObjInfo* info =(CCreGen2ObjInfo*)obj->info;
  80. if (info->asCastle)
  81. {
  82. for(int i=0;i<CGI->objh->objInstances.size();i++)
  83. {
  84. if(CGI->objh->objInstances[i]->ID==77 && dynamic_cast<CGTownInstance*>(CGI->objh->objInstances[i])->identifier == info->identifier)
  85. {
  86. randomizeObject(CGI->objh->objInstances[i]); //we have to randomize the castle first
  87. faction = CGI->objh->objInstances[i]->subID;
  88. break;
  89. }
  90. else if(CGI->objh->objInstances[i]->ID==98 && dynamic_cast<CGTownInstance*>(CGI->objh->objInstances[i])->identifier == info->identifier)
  91. {
  92. faction = CGI->objh->objInstances[i]->subID;
  93. break;
  94. }
  95. }
  96. }
  97. else
  98. {
  99. while((!(info->castles[0]&(1<<faction))))
  100. {
  101. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  102. break;
  103. faction = rand()%F_NUMBER;
  104. }
  105. }
  106. int level = ((info->maxLevel-info->minLevel) ? (rand()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  107. int cid = CGI->townh->towns[faction].basicCreatures[level];
  108. for(int i=0;i<CGI->objh->cregens.size();i++)
  109. if(CGI->objh->cregens[i]==cid)
  110. return std::pair<int,int>(17,i);
  111. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  112. return std::pair<int,int>(17,0);
  113. }
  114. case 217:
  115. {
  116. int faction = rand()%F_NUMBER;
  117. CCreGenObjInfo* info =(CCreGenObjInfo*)obj->info;
  118. if (info->asCastle)
  119. {
  120. for(int i=0;i<CGI->objh->objInstances.size();i++)
  121. {
  122. if(CGI->objh->objInstances[i]->ID==77 && dynamic_cast<CGTownInstance*>(CGI->objh->objInstances[i])->identifier == info->identifier)
  123. {
  124. randomizeObject(CGI->objh->objInstances[i]); //we have to randomize the castle first
  125. faction = CGI->objh->objInstances[i]->subID;
  126. break;
  127. }
  128. else if(CGI->objh->objInstances[i]->ID==98 && dynamic_cast<CGTownInstance*>(CGI->objh->objInstances[i])->identifier == info->identifier)
  129. {
  130. faction = CGI->objh->objInstances[i]->subID;
  131. break;
  132. }
  133. }
  134. }
  135. else
  136. {
  137. while((!(info->castles[0]&(1<<faction))))
  138. {
  139. if((faction>7) && (info->castles[1]&(1<<(faction-8))))
  140. break;
  141. faction = rand()%F_NUMBER;
  142. }
  143. }
  144. int cid = CGI->townh->towns[faction].basicCreatures[obj->subID];
  145. for(int i=0;i<CGI->objh->cregens.size();i++)
  146. if(CGI->objh->cregens[i]==cid)
  147. return std::pair<int,int>(17,i);
  148. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  149. return std::pair<int,int>(17,0);
  150. }
  151. case 218:
  152. {
  153. CCreGen3ObjInfo* info =(CCreGen3ObjInfo*)obj->info;
  154. int level = ((info->maxLevel-info->minLevel) ? (rand()%(info->maxLevel-info->minLevel)+info->minLevel) : (info->minLevel));
  155. int cid = CGI->townh->towns[obj->subID].basicCreatures[level];
  156. for(int i=0;i<CGI->objh->cregens.size();i++)
  157. if(CGI->objh->cregens[i]==cid)
  158. return std::pair<int,int>(17,i);
  159. std::cout << "Cannot find a dwelling for creature "<<cid <<std::endl;
  160. return std::pair<int,int>(17,0);
  161. }
  162. }
  163. return std::pair<int,int>(-1,-1);
  164. }
  165. void CMapHandler::randomizeObject(CGObjectInstance *cur)
  166. {
  167. std::pair<int,int> ran = pickObject(cur);
  168. if(ran.first<0 || ran.second<0) //this is not a random object, or we couldn't find anything
  169. return;
  170. else if(ran.first==34)//special code for hero
  171. {
  172. CGHeroInstance *h = dynamic_cast<CGHeroInstance *>(cur);
  173. if(!h) {std::cout<<"Wrong random hero at "<<cur->pos<<std::endl; return;}
  174. cur->ID = ran.first;
  175. cur->subID = ran.second;
  176. h->type = CGI->heroh->heroes[ran.second];
  177. CGI->heroh->heroInstances.push_back(h);
  178. CGI->objh->objInstances.erase(std::find(CGI->objh->objInstances.begin(),CGI->objh->objInstances.end(),h));
  179. return; //TODO: maybe we should do something with definfo?
  180. }
  181. else if(ran.first==98)//special code for town
  182. {
  183. CGTownInstance *t = dynamic_cast<CGTownInstance*>(cur);
  184. if(!t) {std::cout<<"Wrong random town at "<<cur->pos<<std::endl; return;}
  185. cur->ID = ran.first;
  186. cur->subID = ran.second;
  187. t->town = &CGI->townh->towns[ran.second];
  188. if(t->hasCapitol())
  189. t->defInfo = capitols[t->subID];
  190. else if(t->hasFort())
  191. t->defInfo = CGI->dobjinfo->castles[t->subID];
  192. else
  193. t->defInfo = villages[t->subID];
  194. if(!t->defInfo->handler)
  195. {
  196. t->defInfo->handler = CGI->spriteh->giveDef(t->defInfo->name);
  197. alphaTransformDef(t->defInfo);
  198. }
  199. CGI->townh->townInstances.push_back(t);
  200. return;
  201. }
  202. //we have to replace normal random object
  203. cur->ID = ran.first;
  204. cur->subID = ran.second;
  205. cur->defInfo = CGI->dobjinfo->gobjs[ran.first][ran.second];
  206. if(!cur->defInfo){std::cout<<"Missing def declaration for "<<cur->ID<<" "<<cur->subID<<std::endl;return;}
  207. if(!cur->defInfo->handler) //if we have to load def
  208. {
  209. cur->defInfo->handler = CGI->spriteh->giveDef(cur->defInfo->name);
  210. alphaTransformDef(cur->defInfo);
  211. }
  212. }
  213. void CMapHandler::randomizeObjects()
  214. {
  215. CGObjectInstance * cur;
  216. for(int no=0; no<CGI->objh->objInstances.size(); ++no)
  217. {
  218. randomizeObject(CGI->objh->objInstances[no]);
  219. }
  220. }
  221. void CMapHandler::prepareFOWDefs()
  222. {
  223. fullHide = CGameInfo::mainObj->spriteh->giveDef("TSHRC.DEF");
  224. partialHide = CGameInfo::mainObj->spriteh->giveDef("TSHRE.DEF");
  225. //adding necessary rotations
  226. Cimage nw = partialHide->ourImages[22]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  227. partialHide->ourImages.push_back(nw);
  228. nw = partialHide->ourImages[15]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  229. partialHide->ourImages.push_back(nw);
  230. nw = partialHide->ourImages[2]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  231. partialHide->ourImages.push_back(nw);
  232. nw = partialHide->ourImages[13]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  233. partialHide->ourImages.push_back(nw);
  234. nw = partialHide->ourImages[12]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  235. partialHide->ourImages.push_back(nw);
  236. nw = partialHide->ourImages[16]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  237. partialHide->ourImages.push_back(nw);
  238. nw = partialHide->ourImages[18]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  239. partialHide->ourImages.push_back(nw);
  240. nw = partialHide->ourImages[17]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  241. partialHide->ourImages.push_back(nw);
  242. nw = partialHide->ourImages[20]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  243. partialHide->ourImages.push_back(nw);
  244. nw = partialHide->ourImages[19]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  245. partialHide->ourImages.push_back(nw);
  246. nw = partialHide->ourImages[7]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  247. partialHide->ourImages.push_back(nw);
  248. nw = partialHide->ourImages[24]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  249. partialHide->ourImages.push_back(nw);
  250. nw = partialHide->ourImages[26]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  251. partialHide->ourImages.push_back(nw);
  252. nw = partialHide->ourImages[25]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  253. partialHide->ourImages.push_back(nw);
  254. nw = partialHide->ourImages[30]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  255. partialHide->ourImages.push_back(nw);
  256. nw = partialHide->ourImages[32]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  257. partialHide->ourImages.push_back(nw);
  258. nw = partialHide->ourImages[27]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  259. partialHide->ourImages.push_back(nw);
  260. nw = partialHide->ourImages[28]; nw.bitmap = CSDL_Ext::rotate01(nw.bitmap);
  261. partialHide->ourImages.push_back(nw);
  262. //necessaary rotations added
  263. for(int i=0; i<partialHide->ourImages.size(); ++i)
  264. {
  265. CSDL_Ext::fullAlphaTransform(partialHide->ourImages[i].bitmap);
  266. }
  267. //visibility.resize(reader->map.width+2*Woff);
  268. //for(int gg=0; gg<reader->map.width+2*Woff; ++gg)
  269. //{
  270. // visibility[gg].resize(reader->map.height+2*Hoff);
  271. // for(int jj=0; jj<reader->map.height+2*Hoff; ++jj)
  272. // visibility[gg][jj] = true;
  273. //}
  274. visibility.resize(CGI->ac->map.width, Woff);
  275. for (int i=0-Woff;i<visibility.size()-Woff;i++)
  276. {
  277. visibility[i].resize(CGI->ac->map.height,Hoff);
  278. }
  279. for (int i=0-Woff; i<visibility.size()-Woff; ++i)
  280. {
  281. for (int j=0-Hoff; j<CGI->ac->map.height+Hoff; ++j)
  282. {
  283. visibility[i][j].resize(CGI->ac->map.twoLevel+1,0);
  284. for(int k=0; k<CGI->ac->map.twoLevel+1; ++k)
  285. visibility[i][j][k]=true;
  286. }
  287. }
  288. hideBitmap.resize(CGI->ac->map.width, Woff);
  289. for (int i=0-Woff;i<visibility.size()-Woff;i++)
  290. {
  291. hideBitmap[i].resize(CGI->ac->map.height,Hoff);
  292. }
  293. for (int i=0-Woff; i<hideBitmap.size()-Woff; ++i)
  294. {
  295. for (int j=0-Hoff; j<CGI->ac->map.height+Hoff; ++j)
  296. {
  297. hideBitmap[i][j].resize(CGI->ac->map.twoLevel+1,0);
  298. for(int k=0; k<CGI->ac->map.twoLevel+1; ++k)
  299. hideBitmap[i][j][k] = rand()%fullHide->ourImages.size();
  300. }
  301. }
  302. //visibility[6][7][1] = false;
  303. //visibility[7][7][1] = false;
  304. //visibility[6][8][1] = false;
  305. //visibility[6][6][1] = false;
  306. //visibility[5][8][1] = false;
  307. //visibility[7][6][1] = false;
  308. //visibility[6][9][1] = false;
  309. }
  310. void CMapHandler::roadsRiverTerrainInit()
  311. {
  312. //initializing road's and river's DefHandlers
  313. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  314. int rmask = 0xff000000;
  315. int gmask = 0x00ff0000;
  316. int bmask = 0x0000ff00;
  317. int amask = 0x000000ff;
  318. #else
  319. int rmask = 0x000000ff;
  320. int gmask = 0x0000ff00;
  321. int bmask = 0x00ff0000;
  322. int amask = 0xff000000;
  323. #endif
  324. SDL_Surface * su = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32,
  325. rmask, gmask, bmask, amask);
  326. roadDefs.push_back(CGameInfo::mainObj->spriteh->giveDef("dirtrd.def"));
  327. roadDefs.push_back(CGameInfo::mainObj->spriteh->giveDef("gravrd.def"));
  328. roadDefs.push_back(CGameInfo::mainObj->spriteh->giveDef("cobbrd.def"));
  329. staticRiverDefs.push_back(CGameInfo::mainObj->spriteh->giveDef("clrrvr.def"));
  330. staticRiverDefs.push_back(CGameInfo::mainObj->spriteh->giveDef("icyrvr.def"));
  331. staticRiverDefs.push_back(CGameInfo::mainObj->spriteh->giveDef("mudrvr.def"));
  332. staticRiverDefs.push_back(CGameInfo::mainObj->spriteh->giveDef("lavrvr.def"));
  333. //roadBitmaps = new SDL_Surface** [reader->map.width+2*Woff];
  334. //for (int ii=0;ii<reader->map.width+2*Woff;ii++)
  335. // roadBitmaps[ii] = new SDL_Surface*[reader->map.height+2*Hoff]; // allocate memory
  336. sizes.x = CGI->ac->map.width;
  337. sizes.y = CGI->ac->map.height;
  338. sizes.z = CGI->ac->map.twoLevel+1;
  339. ttiles.resize(CGI->ac->map.width,Woff);
  340. for (int i=0-Woff;i<ttiles.size()-Woff;i++)
  341. {
  342. ttiles[i].resize(CGI->ac->map.height,Hoff);
  343. }
  344. for (int i=0-Woff;i<ttiles.size()-Woff;i++)
  345. {
  346. for (int j=0-Hoff;j<CGI->ac->map.height+Hoff;j++)
  347. ttiles[i][j].resize(CGI->ac->map.twoLevel+1,0);
  348. }
  349. for (int i=0; i<reader->map.width; i++) //jest po szerokoœci
  350. {
  351. for (int j=0; j<reader->map.height;j++) //po wysokoœci
  352. {
  353. for (int k=0; k<=reader->map.twoLevel; ++k)
  354. {
  355. TerrainTile** pomm = reader->map.terrain; ;
  356. if (k==0)
  357. pomm = reader->map.terrain;
  358. else
  359. pomm = reader->map.undergroungTerrain;
  360. if(pomm[i][j].malle)
  361. {
  362. int cDir;
  363. bool rotV, rotH;
  364. if(k==0)
  365. {
  366. int roadpom = reader->map.terrain[i][j].malle-1,
  367. impom = reader->map.terrain[i][j].roadDir;
  368. SDL_Surface *pom1 = roadDefs[roadpom]->ourImages[impom].bitmap;
  369. ttiles[i][j][k].roadbitmap.push_back(pom1);
  370. cDir = reader->map.terrain[i][j].roadDir;
  371. rotH = (reader->map.terrain[i][j].siodmyTajemniczyBajt >> 5) & 1;
  372. rotV = (reader->map.terrain[i][j].siodmyTajemniczyBajt >> 4) & 1;
  373. }
  374. else
  375. {
  376. int pom111 = reader->map.undergroungTerrain[i][j].malle-1,
  377. pom777 = reader->map.undergroungTerrain[i][j].roadDir;
  378. SDL_Surface *pom1 = roadDefs[pom111]->ourImages[pom777].bitmap;
  379. ttiles[i][j][k].roadbitmap.push_back(pom1);
  380. cDir = reader->map.undergroungTerrain[i][j].roadDir;
  381. rotH = (reader->map.undergroungTerrain[i][j].siodmyTajemniczyBajt >> 5) & 1;
  382. rotV = (reader->map.undergroungTerrain[i][j].siodmyTajemniczyBajt >> 4) & 1;
  383. }
  384. if(rotH)
  385. {
  386. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].roadbitmap[0]);
  387. }
  388. if(rotV)
  389. {
  390. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].roadbitmap[0]);
  391. }
  392. if(rotH || rotV)
  393. {
  394. ttiles[i][j][k].roadbitmap[0] = CSDL_Ext::alphaTransform(ttiles[i][j][k].roadbitmap[0]);
  395. SDL_Surface * buf = CSDL_Ext::secondAlphaTransform(ttiles[i][j][k].roadbitmap[0], su);
  396. SDL_FreeSurface(ttiles[i][j][k].roadbitmap[0]);
  397. ttiles[i][j][k].roadbitmap[0] = buf;
  398. }
  399. }
  400. }
  401. }
  402. }
  403. //initializing simple values
  404. for (int i=0; i<CGI->ac->map.width; i++) //jest po szerokoœci
  405. {
  406. for (int j=0; j<CGI->ac->map.height;j++) //po wysokoœci
  407. {
  408. for(int k=0; k<ttiles[0][0].size(); ++k)
  409. {
  410. ttiles[i][j][k].pos = int3(i, j, k);
  411. ttiles[i][j][k].blocked = false;
  412. ttiles[i][j][k].visitable = false;
  413. if(i<0 || j<0 || i>=CGI->ac->map.width || j>=CGI->ac->map.height)
  414. {
  415. ttiles[i][j][k].blocked = true;
  416. continue;
  417. }
  418. ttiles[i][j][k].terType = (k==0 ? CGI->ac->map.terrain[i][j].tertype : CGI->ac->map.undergroungTerrain[i][j].tertype);
  419. ttiles[i][j][k].malle = (k==0 ? CGI->ac->map.terrain[i][j].malle : CGI->ac->map.undergroungTerrain[i][j].malle);
  420. ttiles[i][j][k].nuine = (k==0 ? CGI->ac->map.terrain[i][j].nuine : CGI->ac->map.undergroungTerrain[i][j].nuine);
  421. ttiles[i][j][k].rivdir = (k==0 ? CGI->ac->map.terrain[i][j].rivDir : CGI->ac->map.undergroungTerrain[i][j].rivDir);
  422. ttiles[i][j][k].roaddir = (k==0 ? CGI->ac->map.terrain[i][j].roadDir : CGI->ac->map.undergroungTerrain[i][j].roadDir);
  423. }
  424. }
  425. }
  426. //simple values initialized
  427. for (int i=0; i<reader->map.width; i++) //jest po szerokoœci
  428. {
  429. for (int j=0; j<reader->map.height;j++) //po wysokoœci
  430. {
  431. for(int k=0; k<=reader->map.twoLevel; ++k)
  432. {
  433. TerrainTile** pomm = reader->map.terrain;
  434. if(k==0)
  435. {
  436. pomm = reader->map.terrain;
  437. }
  438. else
  439. {
  440. pomm = reader->map.undergroungTerrain;
  441. }
  442. if(pomm[i][j].nuine)
  443. {
  444. int cDir;
  445. bool rotH, rotV;
  446. if(k==0)
  447. {
  448. ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[reader->map.terrain[i][j].nuine-1]->ourImages[reader->map.terrain[i][j].rivDir].bitmap);
  449. cDir = reader->map.terrain[i][j].rivDir;
  450. rotH = (reader->map.terrain[i][j].siodmyTajemniczyBajt >> 3) & 1;
  451. rotV = (reader->map.terrain[i][j].siodmyTajemniczyBajt >> 2) & 1;
  452. }
  453. else
  454. {
  455. ttiles[i][j][k].rivbitmap.push_back(staticRiverDefs[reader->map.undergroungTerrain[i][j].nuine-1]->ourImages[reader->map.undergroungTerrain[i][j].rivDir].bitmap);
  456. cDir = reader->map.undergroungTerrain[i][j].rivDir;
  457. rotH = (reader->map.undergroungTerrain[i][j].siodmyTajemniczyBajt >> 3) & 1;
  458. rotV = (reader->map.undergroungTerrain[i][j].siodmyTajemniczyBajt >> 2) & 1;
  459. }
  460. if(rotH)
  461. {
  462. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].rivbitmap[0]);
  463. }
  464. if(rotV)
  465. {
  466. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].rivbitmap[0]);
  467. }
  468. if(rotH || rotV)
  469. {
  470. ttiles[i][j][k].rivbitmap[0] = CSDL_Ext::alphaTransform(ttiles[i][j][k].rivbitmap[0]);
  471. SDL_Surface * buf = CSDL_Ext::secondAlphaTransform(ttiles[i][j][k].rivbitmap[0], su);
  472. SDL_FreeSurface(ttiles[i][j][k].rivbitmap[0]);
  473. ttiles[i][j][k].rivbitmap[0] = buf;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. SDL_FreeSurface(su);
  480. }
  481. void CMapHandler::borderAndTerrainBitmapInit()
  482. {
  483. //terrainBitmap = new SDL_Surface **[reader->map.width+2*Woff];
  484. //for (int ii=0;ii<reader->map.width+2*Woff;ii++)
  485. // terrainBitmap[ii] = new SDL_Surface*[reader->map.height+2*Hoff]; // allocate memory
  486. CDefHandler * bord = CGameInfo::mainObj->spriteh->giveDef("EDG.DEF");
  487. for (int i=0-Woff; i<reader->map.width+Woff; i++) //jest po szerokoœci
  488. {
  489. for (int j=0-Hoff; j<reader->map.height+Hoff;j++) //po wysokoœci
  490. {
  491. for(int k=0; k<=reader->map.twoLevel; ++k)
  492. {
  493. if(i < 0 || i > (reader->map.width-1) || j < 0 || j > (reader->map.height-1))
  494. {
  495. if(i==-1 && j==-1)
  496. {
  497. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[16].bitmap);
  498. continue;
  499. }
  500. else if(i==-1 && j==(reader->map.height))
  501. {
  502. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[19].bitmap);
  503. continue;
  504. }
  505. else if(i==(reader->map.width) && j==-1)
  506. {
  507. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[17].bitmap);
  508. continue;
  509. }
  510. else if(i==(reader->map.width) && j==(reader->map.height))
  511. {
  512. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[18].bitmap);
  513. continue;
  514. }
  515. else if(j == -1 && i > -1 && i < reader->map.height)
  516. {
  517. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[22+rand()%2].bitmap);
  518. continue;
  519. }
  520. else if(i == -1 && j > -1 && j < reader->map.height)
  521. {
  522. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[33+rand()%2].bitmap);
  523. continue;
  524. }
  525. else if(j == reader->map.height && i >-1 && i < reader->map.width)
  526. {
  527. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[29+rand()%2].bitmap);
  528. continue;
  529. }
  530. else if(i == reader->map.width && j > -1 && j < reader->map.height)
  531. {
  532. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[25+rand()%2].bitmap);
  533. continue;
  534. }
  535. else
  536. {
  537. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[rand()%16].bitmap);
  538. continue;
  539. }
  540. }
  541. //TerrainTile zz = reader->map.terrain[i-Woff][j-Hoff];
  542. std::string name;
  543. if (k>0)
  544. name = CSemiDefHandler::nameFromType(reader->map.undergroungTerrain[i][j].tertype);
  545. else
  546. name = CSemiDefHandler::nameFromType(reader->map.terrain[i][j].tertype);
  547. for (unsigned int m=0; m<reader->defs.size(); m++)
  548. {
  549. try
  550. {
  551. if (reader->defs[m]->defName != name)
  552. continue;
  553. else
  554. {
  555. int ktora;
  556. if (k==0)
  557. ktora = reader->map.terrain[i][j].terview;
  558. else
  559. ktora = reader->map.undergroungTerrain[i][j].terview;
  560. ttiles[i][j][k].terbitmap.push_back(reader->defs[m]->ourImages[ktora].bitmap);
  561. int zz;
  562. if (k==0)
  563. zz = (reader->map.terrain[i][j].siodmyTajemniczyBajt)%4;
  564. else
  565. zz = (reader->map.undergroungTerrain[i][j].siodmyTajemniczyBajt)%4;
  566. switch (zz)
  567. {
  568. case 1:
  569. {
  570. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].terbitmap[0]);
  571. break;
  572. }
  573. case 2:
  574. {
  575. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].terbitmap[0]);
  576. break;
  577. }
  578. case 3:
  579. {
  580. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate03(ttiles[i][j][k].terbitmap[0]);
  581. break;
  582. }
  583. }
  584. break;
  585. }
  586. }
  587. catch (...)
  588. {
  589. continue;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. void CMapHandler::initObjectRects()
  597. {
  598. //initializing objects / rects
  599. for(int f=0; f<CGI->objh->objInstances.size(); ++f)
  600. {
  601. /*CGI->objh->objInstances[f]->pos.x+=1;
  602. CGI->objh->objInstances[f]->pos.y+=1;*/
  603. if(!CGI->objh->objInstances[f]->defInfo)
  604. {
  605. continue;
  606. }
  607. CDefHandler * curd = CGI->objh->objInstances[f]->defInfo->handler;
  608. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  609. {
  610. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  611. {
  612. SDL_Rect cr;
  613. cr.w = 32;
  614. cr.h = 32;
  615. cr.x = fx*32;
  616. cr.y = fy*32;
  617. std::pair<CGObjectInstance*,std::pair<SDL_Rect, std::vector<std::list<int3>>>> toAdd = std::make_pair(CGI->objh->objInstances[f], std::make_pair(cr, std::vector<std::list<int3>>()));
  618. ///initializing places that will be coloured by blitting (flag colour / player colour positions)
  619. if(toAdd.first->defInfo->isVisitable())
  620. {
  621. toAdd.second.second.resize(toAdd.first->defInfo->handler->ourImages.size());
  622. for(int no = 0; no<toAdd.first->defInfo->handler->ourImages.size(); ++no)
  623. {
  624. bool breakNow = true;
  625. for(int dx=0; dx<32; ++dx)
  626. {
  627. for(int dy=0; dy<32; ++dy)
  628. {
  629. SDL_Surface * curs = toAdd.first->defInfo->handler->ourImages[no].bitmap;
  630. Uint32* point = (Uint32*)( (Uint8*)curs->pixels + curs->pitch * (fy*32+dy) + curs->format->BytesPerPixel*(fx*32+dx));
  631. Uint8 r, g, b, a;
  632. SDL_GetRGBA(*point, curs->format, &r, &g, &b, &a);
  633. if(r==255 && g==255 && b==0)
  634. {
  635. toAdd.second.second[no].push_back(int3((fx*32+dx), (fy*32+dy), 0));
  636. breakNow = false;
  637. }
  638. }
  639. }
  640. if(breakNow)
  641. break;
  642. }
  643. }
  644. if((CGI->objh->objInstances[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1)>=0 && (CGI->objh->objInstances[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1)<ttiles.size()-Woff && (CGI->objh->objInstances[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)>=0 && (CGI->objh->objInstances[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1)<ttiles[0].size()-Hoff)
  645. {
  646. TerrainTile2 & curt =
  647. ttiles
  648. [CGI->objh->objInstances[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  649. [CGI->objh->objInstances[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  650. [CGI->objh->objInstances[f]->pos.z];
  651. ttiles[CGI->objh->objInstances[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32+1][CGI->objh->objInstances[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32+1][CGI->objh->objInstances[f]->pos.z].objects.push_back(toAdd);
  652. }
  653. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  654. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  655. } // for(int f=0; f<CGI->objh->objInstances.size(); ++f)
  656. for(int ix=0; ix<ttiles.size()-Woff; ++ix)
  657. {
  658. for(int iy=0; iy<ttiles[0].size()-Hoff; ++iy)
  659. {
  660. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  661. {
  662. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  663. }
  664. }
  665. }
  666. }
  667. void CMapHandler::calculateBlockedPos()
  668. {
  669. for(int f=0; f<CGI->objh->objInstances.size(); ++f) //calculationg blocked / visitable positions
  670. {
  671. if(!CGI->objh->objInstances[f]->defInfo)
  672. continue;
  673. CDefHandler * curd = CGI->objh->objInstances[f]->defInfo->handler;
  674. for(int fx=0; fx<8; ++fx)
  675. {
  676. for(int fy=0; fy<6; ++fy)
  677. {
  678. int xVal = CGI->objh->objInstances[f]->pos.x + fx - 7;
  679. int yVal = CGI->objh->objInstances[f]->pos.y + fy - 5;
  680. int zVal = CGI->objh->objInstances[f]->pos.z;
  681. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  682. {
  683. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  684. if(((CGI->objh->objInstances[f]->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  685. curt.visitable = true;
  686. if(!((CGI->objh->objInstances[f]->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  687. curt.blocked = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. void CMapHandler::init()
  694. {
  695. std::ifstream ifs("config/townsDefs.txt");
  696. int ccc;
  697. ifs>>ccc;
  698. for(int i=0;i<ccc*2;i++)
  699. {
  700. CGDefInfo * n = new CGDefInfo(*CGI->dobjinfo->castles[i%ccc]);
  701. ifs >> n->name;
  702. if (!(n->handler = CGI->spriteh->giveDef(n->name)))
  703. std::cout << "Cannot open "<<n->name<<std::endl;
  704. if(i<ccc)
  705. villages[i]=n;
  706. else
  707. capitols[i%ccc]=n;
  708. alphaTransformDef(n);
  709. }
  710. timeHandler th;
  711. th.getDif();
  712. randomizeObjects();//randomizing objects on map
  713. std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
  714. for(int h=0; h<reader->map.defy.size(); ++h) //initializing loaded def handler's info
  715. {
  716. //std::string hlp = reader->map.defy[h]->name;
  717. //std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  718. CGI->mh->loadedDefs.insert(std::make_pair(reader->map.defy[h]->name, reader->map.defy[h]->handler));
  719. }
  720. std::cout<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  721. prepareFOWDefs();
  722. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  723. borderAndTerrainBitmapInit();
  724. std::cout<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  725. initObjectRects();
  726. std::cout<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  727. calculateBlockedPos();
  728. std::cout<<"\tCalculating blockmap: "<<th.getDif()<<std::endl;
  729. }
  730. SDL_Surface * CMapHandler::terrainRect(int x, int y, int dx, int dy, int level, unsigned char anim, PseudoV< PseudoV< PseudoV<unsigned char> > > & visibilityMap, bool otherHeroAnim, unsigned char heroAnim)
  731. {
  732. if(!otherHeroAnim)
  733. heroAnim = anim; //the same, as it should be
  734. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  735. int rmask = 0xff000000;
  736. int gmask = 0x00ff0000;
  737. int bmask = 0x0000ff00;
  738. int amask = 0x000000ff;
  739. #else
  740. int rmask = 0x000000ff;
  741. int gmask = 0x0000ff00;
  742. int bmask = 0x00ff0000;
  743. int amask = 0xff000000;
  744. #endif
  745. SDL_Surface * su = SDL_CreateRGBSurface(SDL_SWSURFACE, dx*32, dy*32, 32,
  746. rmask, gmask, bmask, amask);
  747. if (((dx+x)>((reader->map.width+Woff)) || (dy+y)>((reader->map.height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  748. throw new std::string("terrainRect: out of range");
  749. ////printing terrain
  750. for (int bx=0; bx<dx; bx++)
  751. {
  752. for (int by=0; by<dy; by++)
  753. {
  754. SDL_Rect sr;
  755. sr.y=by*32;
  756. sr.x=bx*32;
  757. sr.h=sr.w=32;
  758. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap[anim%ttiles[x+bx][y+by][level].terbitmap.size()],NULL,su,&sr);
  759. }
  760. }
  761. ////terrain printed
  762. ////printing rivers
  763. for (int bx=0; bx<dx; bx++)
  764. {
  765. for (int by=0; by<dy; by++)
  766. {
  767. SDL_Rect sr;
  768. sr.y=by*32;
  769. sr.x=bx*32;
  770. sr.h=sr.w=32;
  771. if(ttiles[x+bx][y+by][level].rivbitmap.size())
  772. SDL_BlitSurface(ttiles[x+bx][y+by][level].rivbitmap[anim%ttiles[x+bx][y+by][level].rivbitmap.size()],NULL,su,&sr);
  773. }
  774. }
  775. ////rivers printed
  776. ////printing roads
  777. for (int bx=0; bx<dx; bx++)
  778. {
  779. for (int by=-1; by<dy; by++)
  780. {
  781. if(y+by<=-4)
  782. continue;
  783. SDL_Rect sr;
  784. sr.y=by*32+16;
  785. sr.x=bx*32;
  786. sr.h=sr.w=32;
  787. if(ttiles[x+bx][y+by][level].roadbitmap.size())
  788. SDL_BlitSurface(ttiles[x+bx][y+by][level].roadbitmap[anim%ttiles[x+bx][y+by][level].roadbitmap.size()],NULL,su,&sr);
  789. }
  790. }
  791. ////roads printed
  792. ////printing objects
  793. for (int bx=0; bx<dx; bx++)
  794. {
  795. for (int by=0; by<dy; by++)
  796. {
  797. for(int h=0; h<ttiles[x+bx][y+by][level].objects.size(); ++h)
  798. {
  799. SDL_Rect sr;
  800. sr.w = 32;
  801. sr.h = 32;
  802. sr.x = (bx)*32;
  803. sr.y = (by)*32;
  804. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second.first;
  805. CGHeroInstance * themp = (dynamic_cast<CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  806. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  807. {
  808. int imgVal = 8;
  809. SDL_Surface * tb;
  810. if(((CHeroObjInfo*)themp->info)->myInstance->type==NULL)
  811. continue;
  812. std::vector<Cimage> & iv = ((CHeroObjInfo*)themp->info)->myInstance->type->heroClass->moveAnim->ourImages;
  813. switch(themp->moveDir)
  814. {
  815. case 1:
  816. {
  817. int gg;
  818. for(gg=0; gg<iv.size(); ++gg)
  819. {
  820. if(iv[gg].groupNumber==10)
  821. {
  822. tb = iv[gg+heroAnim%imgVal].bitmap;
  823. break;
  824. }
  825. }
  826. SDL_BlitSurface(tb,&pp,su,&sr);
  827. pp.y+=imgVal*2-32;
  828. sr.y-=16;
  829. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  830. break;
  831. }
  832. case 2:
  833. {
  834. int gg;
  835. for(gg=0; gg<iv.size(); ++gg)
  836. {
  837. if(iv[gg].groupNumber==5)
  838. {
  839. tb = iv[gg+heroAnim%imgVal].bitmap;
  840. break;
  841. }
  842. }
  843. SDL_BlitSurface(tb,&pp,su,&sr);
  844. pp.y+=imgVal*2-32;
  845. sr.y-=16;
  846. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  847. break;
  848. }
  849. case 3:
  850. {
  851. int gg;
  852. for(gg=0; gg<iv.size(); ++gg)
  853. {
  854. if(iv[gg].groupNumber==6)
  855. {
  856. tb = iv[gg+heroAnim%imgVal].bitmap;
  857. break;
  858. }
  859. }
  860. SDL_BlitSurface(tb,&pp,su,&sr);
  861. pp.y+=imgVal*2-32;
  862. sr.y-=16;
  863. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  864. break;
  865. }
  866. case 4:
  867. {
  868. int gg;
  869. for(gg=0; gg<iv.size(); ++gg)
  870. {
  871. if(iv[gg].groupNumber==7)
  872. {
  873. tb = iv[gg+heroAnim%imgVal].bitmap;
  874. break;
  875. }
  876. }
  877. SDL_BlitSurface(tb,&pp,su,&sr);
  878. pp.y+=imgVal*2-32;
  879. sr.y-=16;
  880. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  881. break;
  882. }
  883. case 5:
  884. {
  885. int gg;
  886. for(gg=0; gg<iv.size(); ++gg)
  887. {
  888. if(iv[gg].groupNumber==8)
  889. {
  890. tb = iv[gg+heroAnim%imgVal].bitmap;
  891. break;
  892. }
  893. }
  894. SDL_BlitSurface(tb,&pp,su,&sr);
  895. pp.y+=imgVal*2-32;
  896. sr.y-=16;
  897. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  898. break;
  899. }
  900. case 6: //ok
  901. {
  902. int gg;
  903. for(gg=0; gg<iv.size(); ++gg)
  904. {
  905. if(iv[gg].groupNumber==9)
  906. {
  907. tb = iv[gg+heroAnim%imgVal].bitmap;
  908. break;
  909. }
  910. }
  911. SDL_BlitSurface(tb,&pp,su,&sr);
  912. pp.y+=imgVal*2-32;
  913. sr.y-=16;
  914. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  915. break;
  916. }
  917. case 7:
  918. {
  919. int gg;
  920. for(gg=0; gg<iv.size(); ++gg)
  921. {
  922. if(iv[gg].groupNumber==12)
  923. {
  924. tb = iv[gg+heroAnim%imgVal].bitmap;
  925. break;
  926. }
  927. }
  928. SDL_BlitSurface(tb,&pp,su,&sr);
  929. pp.y+=imgVal*2-32;
  930. sr.y-=16;
  931. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  932. break;
  933. }
  934. case 8:
  935. {
  936. int gg;
  937. for(gg=0; gg<iv.size(); ++gg)
  938. {
  939. if(iv[gg].groupNumber==11)
  940. {
  941. tb = iv[gg+heroAnim%imgVal].bitmap;
  942. break;
  943. }
  944. }
  945. SDL_BlitSurface(tb,&pp,su,&sr);
  946. pp.y+=imgVal*2-32;
  947. sr.y-=16;
  948. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  949. break;
  950. }
  951. }
  952. }
  953. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison
  954. {
  955. int imgVal = 8;
  956. SDL_Surface * tb;
  957. if(((CHeroObjInfo*)themp->info)->myInstance->type==NULL)
  958. continue;
  959. std::vector<Cimage> & iv = ((CHeroObjInfo*)themp->info)->myInstance->type->heroClass->moveAnim->ourImages;
  960. switch(themp->moveDir)
  961. {
  962. case 1:
  963. {
  964. int gg;
  965. for(gg=0; gg<iv.size(); ++gg)
  966. {
  967. if(iv[gg].groupNumber==13)
  968. {
  969. tb = iv[gg].bitmap;
  970. break;
  971. }
  972. }
  973. SDL_BlitSurface(tb,&pp,su,&sr);
  974. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  975. {
  976. SDL_Rect bufr = sr;
  977. bufr.x-=2*32;
  978. bufr.y-=1*32;
  979. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[13*8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  980. themp->flagPrinted = true;
  981. }
  982. break;
  983. }
  984. case 2:
  985. {
  986. int gg;
  987. for(gg=0; gg<iv.size(); ++gg)
  988. {
  989. if(iv[gg].groupNumber==0)
  990. {
  991. tb = iv[gg].bitmap;
  992. break;
  993. }
  994. }
  995. SDL_BlitSurface(tb,&pp,su,&sr);
  996. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  997. {
  998. SDL_Rect bufr = sr;
  999. bufr.x-=2*32;
  1000. bufr.y-=1*32;
  1001. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[heroAnim%imgVal].bitmap, NULL, su, &bufr);
  1002. themp->flagPrinted = true;
  1003. }
  1004. break;
  1005. }
  1006. case 3:
  1007. {
  1008. int gg;
  1009. for(gg=0; gg<iv.size(); ++gg)
  1010. {
  1011. if(iv[gg].groupNumber==1)
  1012. {
  1013. tb = iv[gg].bitmap;
  1014. break;
  1015. }
  1016. }
  1017. SDL_BlitSurface(tb,&pp,su,&sr);
  1018. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  1019. {
  1020. SDL_Rect bufr = sr;
  1021. bufr.x-=2*32;
  1022. bufr.y-=1*32;
  1023. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  1024. themp->flagPrinted = true;
  1025. }
  1026. break;
  1027. }
  1028. case 4:
  1029. {
  1030. int gg;
  1031. for(gg=0; gg<iv.size(); ++gg)
  1032. {
  1033. if(iv[gg].groupNumber==2)
  1034. {
  1035. tb = iv[gg].bitmap;
  1036. break;
  1037. }
  1038. }
  1039. SDL_BlitSurface(tb,&pp,su,&sr);
  1040. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  1041. {
  1042. SDL_Rect bufr = sr;
  1043. bufr.x-=2*32;
  1044. bufr.y-=1*32;
  1045. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[2*8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  1046. themp->flagPrinted = true;
  1047. }
  1048. break;
  1049. }
  1050. case 5:
  1051. {
  1052. int gg;
  1053. for(gg=0; gg<iv.size(); ++gg)
  1054. {
  1055. if(iv[gg].groupNumber==3)
  1056. {
  1057. tb = iv[gg].bitmap;
  1058. break;
  1059. }
  1060. }
  1061. SDL_BlitSurface(tb,&pp,su,&sr);
  1062. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  1063. {
  1064. SDL_Rect bufr = sr;
  1065. bufr.x-=2*32;
  1066. bufr.y-=1*32;
  1067. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[3*8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  1068. themp->flagPrinted = true;
  1069. }
  1070. break;
  1071. }
  1072. case 6:
  1073. {
  1074. int gg;
  1075. for(gg=0; gg<iv.size(); ++gg)
  1076. {
  1077. if(iv[gg].groupNumber==4)
  1078. {
  1079. tb = iv[gg].bitmap;
  1080. break;
  1081. }
  1082. }
  1083. SDL_BlitSurface(tb,&pp,su,&sr);
  1084. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  1085. {
  1086. SDL_Rect bufr = sr;
  1087. bufr.x-=2*32;
  1088. bufr.y-=1*32;
  1089. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[4*8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  1090. themp->flagPrinted = true;
  1091. }
  1092. break;
  1093. }
  1094. case 7:
  1095. {
  1096. int gg;
  1097. for(gg=0; gg<iv.size(); ++gg)
  1098. {
  1099. if(iv[gg].groupNumber==15)
  1100. {
  1101. tb = iv[gg].bitmap;
  1102. break;
  1103. }
  1104. }
  1105. SDL_BlitSurface(tb,&pp,su,&sr);
  1106. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  1107. {
  1108. SDL_Rect bufr = sr;
  1109. bufr.x-=2*32;
  1110. bufr.y-=1*32;
  1111. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[15*8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  1112. themp->flagPrinted = true;
  1113. }
  1114. break;
  1115. }
  1116. case 8:
  1117. {
  1118. int gg;
  1119. for(gg=0; gg<iv.size(); ++gg)
  1120. {
  1121. if(iv[gg].groupNumber==14)
  1122. {
  1123. tb = iv[gg].bitmap;
  1124. break;
  1125. }
  1126. }
  1127. SDL_BlitSurface(tb,&pp,su,&sr);
  1128. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  1129. {
  1130. SDL_Rect bufr = sr;
  1131. bufr.x-=2*32;
  1132. bufr.y-=1*32;
  1133. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[14*8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  1134. themp->flagPrinted = true;
  1135. }
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. else
  1141. {
  1142. int imgVal = ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages.size();
  1143. SDL_BlitSurface(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[anim%imgVal].bitmap,&pp,su,&sr);
  1144. }
  1145. //printing appropriate flag colour
  1146. if(ttiles[x+bx][y+by][level].objects[h].second.second.size())
  1147. {
  1148. std::list<int3> & curl = ttiles[x+bx][y+by][level].objects[h].second.second[anim%ttiles[x+bx][y+by][level].objects[h].second.second.size()];
  1149. for(std::list<int3>::iterator g=curl.begin(); g!=curl.end(); ++g)
  1150. {
  1151. SDL_Color ourC;
  1152. int own = ttiles[x+bx][y+by][level].objects[h].first->getOwner();
  1153. if(ttiles[x+bx][y+by][level].objects[h].first->getOwner()!=255 && ttiles[x+bx][y+by][level].objects[h].first->getOwner()!=254)
  1154. ourC = CGI->playerColors[ttiles[x+bx][y+by][level].objects[h].first->getOwner()];
  1155. else if(ttiles[x+bx][y+by][level].objects[h].first->getOwner()==255)
  1156. ourC = CGI->neutralColor;
  1157. else continue;
  1158. CSDL_Ext::SDL_PutPixelWithoutRefresh(su, bx*32 + g->x%32 , by*32 + g->y%32, ourC.r , ourC.g, ourC.b, 0);
  1159. }
  1160. }
  1161. }
  1162. }
  1163. }
  1164. ///enabling flags
  1165. //nie zauwazylem aby ustawianie tego cokolwiek zmienialo w wyswietlaniu, wiec komentuje (do dzialania wymaga jeszcze odkomentowania przyjazni w statcie)
  1166. /*for(std::map<int, PlayerState>::iterator k=CGI->state->players.begin(); k!=CGI->state->players.end(); ++k)
  1167. {
  1168. for (int l = 0; l<k->second.heroes.size(); l++)
  1169. k->second.heroes[l]->flagPrinted = false;
  1170. }
  1171. for(int qq=0; qq<CGI->heroh->heroInstances.size(); ++qq)
  1172. {
  1173. CGI->heroh->heroInstances[qq]->flagPrinted = false;
  1174. }*/
  1175. ///flags enabled
  1176. ////objects printed, printing shadow
  1177. for (int bx=0; bx<dx; bx++)
  1178. {
  1179. for (int by=0; by<dy; by++)
  1180. {
  1181. SDL_Rect sr;
  1182. sr.y=by*32;
  1183. sr.x=bx*32;
  1184. sr.h=sr.w=32;
  1185. if (!level)
  1186. {
  1187. //if( bx+x>-1 && by+y>-1 && bx+x<visibilityMap.size()-(-1) && by+y<visibilityMap[0].size()-(-1) && !visibilityMap[bx+x][by+y][0])
  1188. if(bx+x>=0 && by+y>=0 && bx+x<CGI->mh->reader->map.width && by+y<CGI->mh->reader->map.height && !visibilityMap[bx+x][by+y][0])
  1189. {
  1190. SDL_Surface * hide = getVisBitmap(bx+x, by+y, visibilityMap, 0);
  1191. //SDL_Surface * hide2 = CSDL_Ext::secondAlphaTransform(hide, su);
  1192. SDL_BlitSurface(hide, NULL, su, &sr);
  1193. //SDL_FreeSurface(hide2);
  1194. }
  1195. }
  1196. else
  1197. {
  1198. //if( bx+x>-1 && by+y>-1 && bx+x<visibilityMap.size()-(-1) && by+y<visibilityMap[0].size()-(-1) && !visibilityMap[bx+x][by+y][1])
  1199. if(bx+x>=0 && by+y>=0 && bx+x<CGI->mh->reader->map.width && by+y<CGI->mh->reader->map.height && !visibilityMap[bx+x][by+y][1])
  1200. {
  1201. SDL_Surface * hide = getVisBitmap(bx+x, by+y, visibilityMap, 1);
  1202. //SDL_Surface * hide2 = CSDL_Ext::secondAlphaTransform(hide, su);
  1203. SDL_BlitSurface(hide, NULL, su, &sr);
  1204. //SDL_FreeSurface(hide2);
  1205. }
  1206. }
  1207. }
  1208. }
  1209. ////shadow printed
  1210. //printing borders
  1211. for (int bx=0; bx<dx; bx++)
  1212. {
  1213. for (int by=0; by<dy; by++)
  1214. {
  1215. if(bx+x<0 || by+y<0 || bx+x>reader->map.width+(-1) || by+y>reader->map.height+(-1))
  1216. {
  1217. SDL_Rect sr;
  1218. sr.y=by*32;
  1219. sr.x=bx*32;
  1220. sr.h=sr.w=32;
  1221. SDL_BlitSurface(ttiles[x+bx][y+by][level].terbitmap[anim%ttiles[x+bx][y+by][level].terbitmap.size()],NULL,su,&sr);
  1222. }
  1223. else
  1224. {
  1225. if(MARK_BLOCKED_POSITIONS && ttiles[x+bx][y+by][level].blocked) //temporary hiding blocked positions
  1226. {
  1227. SDL_Rect sr;
  1228. sr.y=by*32;
  1229. sr.x=bx*32;
  1230. sr.h=sr.w=32;
  1231. SDL_Surface * ns = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32,
  1232. rmask, gmask, bmask, amask);
  1233. for(int f=0; f<ns->w*ns->h*4; ++f)
  1234. {
  1235. *((unsigned char*)(ns->pixels) + f) = 128;
  1236. }
  1237. SDL_BlitSurface(ns,NULL,su,&sr);
  1238. SDL_FreeSurface(ns);
  1239. }
  1240. if(MARK_VISITABLE_POSITIONS && ttiles[x+bx][y+by][level].visitable) //temporary hiding visitable positions
  1241. {
  1242. SDL_Rect sr;
  1243. sr.y=by*32;
  1244. sr.x=bx*32;
  1245. sr.h=sr.w=32;
  1246. SDL_Surface * ns = SDL_CreateRGBSurface(SDL_SWSURFACE, 32, 32, 32,
  1247. rmask, gmask, bmask, amask);
  1248. for(int f=0; f<ns->w*ns->h*4; ++f)
  1249. {
  1250. *((unsigned char*)(ns->pixels) + f) = 128;
  1251. }
  1252. SDL_BlitSurface(ns,NULL,su,&sr);
  1253. SDL_FreeSurface(ns);
  1254. }
  1255. }
  1256. }
  1257. }
  1258. CSDL_Ext::update(su);
  1259. //borders printed
  1260. return su;
  1261. }
  1262. SDL_Surface * CMapHandler::terrBitmap(int x, int y)
  1263. {
  1264. return ttiles[x+Woff][y+Hoff][0].terbitmap[0];
  1265. }
  1266. SDL_Surface * CMapHandler::undTerrBitmap(int x, int y)
  1267. {
  1268. return ttiles[x+Woff][y+Hoff][0].terbitmap[1];
  1269. }
  1270. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, PseudoV< PseudoV< PseudoV<unsigned char> > > & visibilityMap, int lvl)
  1271. {
  1272. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1273. {
  1274. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  1275. }
  1276. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1277. {
  1278. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  1279. }
  1280. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1281. {
  1282. return partialHide->ourImages[15].bitmap; //visible right top corner
  1283. }
  1284. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1285. {
  1286. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  1287. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  1288. }
  1289. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1290. {
  1291. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  1292. return partialHide->ourImages[35].bitmap;
  1293. }
  1294. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1295. {
  1296. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  1297. return partialHide->ourImages[0].bitmap; //visible top
  1298. }
  1299. else if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1300. {
  1301. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  1302. return partialHide->ourImages[4].bitmap; //visble bottom
  1303. }
  1304. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1305. {
  1306. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  1307. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  1308. return partialHide->ourImages[36].bitmap;
  1309. }
  1310. else if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1311. {
  1312. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  1313. return partialHide->ourImages[2].bitmap; //visible right
  1314. }
  1315. else if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl])
  1316. {
  1317. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  1318. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  1319. }
  1320. else if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1321. {
  1322. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  1323. }
  1324. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && !visibilityMap[x+1][y+1][lvl])
  1325. {
  1326. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  1327. return partialHide->ourImages[37].bitmap;
  1328. }
  1329. else if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x+1][y-1][lvl])
  1330. {
  1331. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1332. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1333. return partialHide->ourImages[38].bitmap;
  1334. }
  1335. else if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1336. {
  1337. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  1338. }
  1339. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1340. {
  1341. return partialHide->ourImages[16].bitmap; //visible right corners
  1342. }
  1343. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1344. {
  1345. return partialHide->ourImages[18].bitmap; //visible top corners
  1346. }
  1347. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1348. {
  1349. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  1350. return partialHide->ourImages[39].bitmap;
  1351. }
  1352. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1353. {
  1354. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  1355. return partialHide->ourImages[40].bitmap;
  1356. }
  1357. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1358. {
  1359. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  1360. }
  1361. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1362. {
  1363. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  1364. return partialHide->ourImages[41].bitmap;
  1365. }
  1366. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1367. {
  1368. return partialHide->ourImages[19].bitmap; //visible corners without left top
  1369. }
  1370. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1371. {
  1372. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  1373. }
  1374. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1375. {
  1376. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  1377. return partialHide->ourImages[42].bitmap;
  1378. }
  1379. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1380. {
  1381. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  1382. return partialHide->ourImages[43].bitmap;
  1383. }
  1384. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1385. {
  1386. return partialHide->ourImages[21].bitmap; //visible all corners only
  1387. }
  1388. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl])
  1389. {
  1390. return partialHide->ourImages[6].bitmap; //hidden top
  1391. }
  1392. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1393. {
  1394. return partialHide->ourImages[7].bitmap; //hidden right
  1395. }
  1396. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1397. {
  1398. return partialHide->ourImages[8].bitmap; //hidden bottom
  1399. }
  1400. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1401. {
  1402. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  1403. return partialHide->ourImages[44].bitmap;
  1404. }
  1405. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl])
  1406. {
  1407. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  1408. }
  1409. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1410. {
  1411. return partialHide->ourImages[29].bitmap; //hidden left and right
  1412. }
  1413. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1414. {
  1415. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  1416. }
  1417. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x-1][y+1][lvl])
  1418. {
  1419. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  1420. return partialHide->ourImages[45].bitmap;
  1421. }
  1422. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x-1][y+1][lvl])
  1423. {
  1424. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1425. }
  1426. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl])
  1427. {
  1428. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  1429. return partialHide->ourImages[46].bitmap;
  1430. }
  1431. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl])
  1432. {
  1433. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  1434. return partialHide->ourImages[47].bitmap;
  1435. }
  1436. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl])
  1437. {
  1438. return partialHide->ourImages[32].bitmap; //visible left and right corners
  1439. }
  1440. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y-1][lvl])
  1441. {
  1442. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  1443. return partialHide->ourImages[48].bitmap;
  1444. }
  1445. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y-1][lvl])
  1446. {
  1447. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  1448. }
  1449. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y-1][lvl])
  1450. {
  1451. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1452. }
  1453. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1454. {
  1455. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  1456. }
  1457. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1458. {
  1459. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  1460. }
  1461. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1462. {
  1463. //return CSDL_Ext::rotate01(partialHide->ourImages[32].bitmap); //visible right and left cornres
  1464. return partialHide->ourImages[49].bitmap;
  1465. }
  1466. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl])
  1467. {
  1468. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1469. }
  1470. else if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1471. {
  1472. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1473. }
  1474. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl])
  1475. {
  1476. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  1477. return partialHide->ourImages[50].bitmap;
  1478. }
  1479. else if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y-1][lvl])
  1480. {
  1481. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  1482. return partialHide->ourImages[51].bitmap;
  1483. }
  1484. //newly added
  1485. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl]) //visible t and tr
  1486. {
  1487. return partialHide->ourImages[0].bitmap;
  1488. }
  1489. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl]) //visible t and tl
  1490. {
  1491. return partialHide->ourImages[1].bitmap;
  1492. }
  1493. else if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl]) //visible b and br
  1494. {
  1495. return partialHide->ourImages[4].bitmap;
  1496. }
  1497. else if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl]) //visible b and bl
  1498. {
  1499. return partialHide->ourImages[5].bitmap;
  1500. }
  1501. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl]) //visible l and tl
  1502. {
  1503. return partialHide->ourImages[36].bitmap;
  1504. }
  1505. else if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl]) //visible l and bl
  1506. {
  1507. return partialHide->ourImages[36].bitmap;
  1508. }
  1509. else if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl]) //visible r and tr
  1510. {
  1511. return partialHide->ourImages[2].bitmap;
  1512. }
  1513. else if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl]) //visible r and br
  1514. {
  1515. return partialHide->ourImages[3].bitmap;
  1516. }
  1517. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1518. }
  1519. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1520. {
  1521. int ret=-1;
  1522. if(a.x>=CGI->mh->reader->map.width && a.y>=CGI->mh->reader->map.height)
  1523. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->reader->map.width-1][CGI->mh->reader->map.width-1][a.z].malle];
  1524. else if(a.x>=CGI->mh->reader->map.width && a.y<CGI->mh->reader->map.height)
  1525. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->reader->map.width-1][a.y][a.z].malle];
  1526. else if(a.x<CGI->mh->reader->map.width && a.y>=CGI->mh->reader->map.height)
  1527. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->reader->map.width-1][a.z].malle];
  1528. else
  1529. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].malle];
  1530. if(!(a.x==b.x || a.y==b.y))
  1531. ret*=1.41421;
  1532. //TODO: use hero's pathfinding skill during calculating cost
  1533. return ret;
  1534. }
  1535. std::vector < std::string > CMapHandler::getObjDescriptions(int3 pos)
  1536. {
  1537. std::vector < std::pair<CGObjectInstance*,std::pair<SDL_Rect, std::vector<std::list<int3>>>> > objs = ttiles[pos.x][pos.y][pos.z].objects;
  1538. std::vector<std::string> ret;
  1539. for(int g=0; g<objs.size(); ++g)
  1540. {
  1541. 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 &&
  1542. (((objs[g].first->defInfo->blockMap[5-(objs[g].first->pos.y-pos.y)])>>((objs[g].first->pos.x-pos.x)))&1)==0
  1543. ) //checking position blocking
  1544. {
  1545. //unsigned char * blm = objs[g].first->defInfo->blockMap;
  1546. if (objs[g].first->state)
  1547. ret.push_back(objs[g].first->state->hoverText(objs[g].first));
  1548. else
  1549. ret.push_back(CGI->objh->objects[objs[g].first->ID].name);
  1550. }
  1551. }
  1552. return ret;
  1553. }
  1554. std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1555. {
  1556. std::vector < CGObjectInstance * > ret;
  1557. for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1558. {
  1559. CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1560. if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1561. ret.push_back(curi);
  1562. }
  1563. return ret;
  1564. }
  1565. CGObjectInstance * CMapHandler::createObject(int id, int subid, int3 pos)
  1566. {
  1567. CGObjectInstance * nobj;
  1568. switch(id)
  1569. {
  1570. case 43: //hero
  1571. nobj = new CGHeroInstance;
  1572. break;
  1573. case 98: //town
  1574. nobj = new CGTownInstance;
  1575. break;
  1576. default: //rest of objects
  1577. nobj = new CGObjectInstance;
  1578. break;
  1579. }
  1580. nobj->ID = id;
  1581. nobj->subID = subid;
  1582. nobj->defInfo = CGI->dobjinfo->gobjs[id][subid];
  1583. if(!nobj->defInfo)
  1584. std::cout <<"No def declaration for " <<id <<" "<<subid<<std::endl;
  1585. /*new CGDefInfo;
  1586. int defObjInfoNumber = -1;
  1587. for(int f=0; f<CGI->dobjinfo->objs.size(); ++f)
  1588. {
  1589. if(CGI->dobjinfo->objs[f].type==id && CGI->dobjinfo->objs[f].subtype == subid)
  1590. {
  1591. defObjInfoNumber = f;
  1592. break;
  1593. }
  1594. }
  1595. nobj->defInfo->name = CGI->dobjinfo->objs[defObjInfoNumber].defName;
  1596. for(int g=0; g<6; ++g)
  1597. nobj->defInfo->blockMap[g] = CGI->dobjinfo->objs[defObjInfoNumber].blockMap[g];
  1598. for(int g=0; g<6; ++g)
  1599. nobj->defInfo->visitMap[g] = CGI->dobjinfo->objs[nobj->defObjInfoNumber].visitMap[g];
  1600. nobj->defInfo->printPriority = CGI->dobjinfo->objs[nobj->defObjInfoNumber].priority;*/
  1601. nobj->pos = pos;
  1602. //nobj->state = NULL;//new CLuaObjectScript();
  1603. nobj->tempOwner = 254;
  1604. nobj->info = NULL;
  1605. nobj->defInfo->id = id;
  1606. nobj->defInfo->subid = subid;
  1607. //assigning defhandler
  1608. std::string ourName = getDefName(id, subid);
  1609. std::transform(ourName.begin(), ourName.end(), ourName.begin(), (int(*)(int))toupper);
  1610. nobj->defInfo->name = ourName;
  1611. if(loadedDefs[ourName] == NULL)
  1612. {
  1613. nobj->defInfo->handler = CGI->spriteh->giveDef(ourName);
  1614. loadedDefs[ourName] = nobj->defInfo->handler;
  1615. }
  1616. else
  1617. {
  1618. nobj->defInfo->handler = loadedDefs[ourName];
  1619. }
  1620. return nobj;
  1621. }
  1622. std::string CMapHandler::getDefName(int id, int subid)
  1623. {
  1624. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1625. if(temp)
  1626. return temp->name;
  1627. throw new std::exception("Def not found.");
  1628. }
  1629. bool CMapHandler::printObject(CGObjectInstance *obj)
  1630. {
  1631. CDefHandler * curd = obj->defInfo->handler;
  1632. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1633. {
  1634. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1635. {
  1636. SDL_Rect cr;
  1637. cr.w = 32;
  1638. cr.h = 32;
  1639. cr.x = fx*32;
  1640. cr.y = fy*32;
  1641. std::pair<CGObjectInstance*,std::pair<SDL_Rect, std::vector<std::list<int3>>>> toAdd = std::make_pair(obj, std::make_pair(cr, std::vector<std::list<int3>>()));
  1642. ///initializing places that will be coloured by blitting (flag colour / player colour positions)
  1643. if(CGI->dobjinfo->gobjs[toAdd.first->ID][toAdd.first->subID]->isVisitable())
  1644. {
  1645. toAdd.second.second.resize(toAdd.first->defInfo->handler->ourImages.size());
  1646. for(int no = 0; no<toAdd.first->defInfo->handler->ourImages.size(); ++no)
  1647. {
  1648. bool breakNow = true;
  1649. for(int dx=0; dx<32; ++dx)
  1650. {
  1651. for(int dy=0; dy<32; ++dy)
  1652. {
  1653. SDL_Surface * curs = toAdd.first->defInfo->handler->ourImages[no].bitmap;
  1654. Uint32* point = (Uint32*)( (Uint8*)curs->pixels + curs->pitch * (fy*32+dy) + curs->format->BytesPerPixel*(fx*32+dx));
  1655. Uint8 r, g, b, a;
  1656. SDL_GetRGBA(*point, curs->format, &r, &g, &b, &a);
  1657. if(r==255 && g==255 && b==0)
  1658. {
  1659. toAdd.second.second[no].push_back(int3((fx*32+dx), (fy*32+dy), 0));
  1660. breakNow = false;
  1661. }
  1662. }
  1663. }
  1664. if(breakNow)
  1665. break;
  1666. }
  1667. }
  1668. 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)
  1669. {
  1670. TerrainTile2 & curt =
  1671. ttiles
  1672. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1673. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1674. [obj->pos.z];
  1675. 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);
  1676. }
  1677. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1678. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1679. return true;
  1680. }
  1681. bool CMapHandler::hideObject(CGObjectInstance *obj)
  1682. {
  1683. CDefHandler * curd = obj->defInfo->handler;
  1684. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1685. {
  1686. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1687. {
  1688. 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)
  1689. {
  1690. std::vector < std::pair<CGObjectInstance*,std::pair<SDL_Rect, std::vector<std::list<int3>>>> > & 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;
  1691. for(int dd=0; dd<ctile.size(); ++dd)
  1692. {
  1693. if(ctile[dd].first->id==obj->id)
  1694. ctile.erase(ctile.begin() + dd);
  1695. }
  1696. }
  1697. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1698. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1699. return true;
  1700. }
  1701. std::string CMapHandler::getRandomizedDefName(CGDefInfo *di, CGObjectInstance * obj)
  1702. {
  1703. return std::string();
  1704. }
  1705. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1706. {
  1707. hideObject(obj);
  1708. std::vector<CGObjectInstance *>::iterator db = std::find(CGI->objh->objInstances.begin(), CGI->objh->objInstances.end(), obj);
  1709. recalculateHideVisPosUnderObj(*db);
  1710. delete *db;
  1711. CGI->objh->objInstances.erase(db);
  1712. return true;
  1713. }
  1714. bool CMapHandler::recalculateHideVisPos(int3 &pos)
  1715. {
  1716. ttiles[pos.x][pos.y][pos.z].visitable = false;
  1717. ttiles[pos.x][pos.y][pos.z].blocked = false;
  1718. for(int i=0; i<ttiles[pos.x][pos.y][pos.z].objects.size(); ++i)
  1719. {
  1720. CDefHandler * curd = ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->handler;
  1721. for(int fx=0; fx<8; ++fx)
  1722. {
  1723. for(int fy=0; fy<6; ++fy)
  1724. {
  1725. int xVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.x + fx - 7;
  1726. int yVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.y + fy - 5;
  1727. int zVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.z;
  1728. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  1729. {
  1730. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  1731. if(((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  1732. curt.visitable = true;
  1733. if(!((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  1734. curt.blocked = true;
  1735. }
  1736. }
  1737. }
  1738. }
  1739. return true;
  1740. }
  1741. bool CMapHandler::recalculateHideVisPosUnderObj(CGObjectInstance *obj, bool withBorder)
  1742. {
  1743. if(withBorder)
  1744. {
  1745. for(int fx=-1; fx<=obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1746. {
  1747. for(int fy=-1; fy<=obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1748. {
  1749. 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)
  1750. {
  1751. 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));
  1752. }
  1753. }
  1754. }
  1755. }
  1756. else
  1757. {
  1758. for(int fx=0; fx<obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1759. {
  1760. for(int fy=0; fy<obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1761. {
  1762. 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)
  1763. {
  1764. 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));
  1765. }
  1766. }
  1767. }
  1768. }
  1769. return true;
  1770. }