mapHandler.cpp 66 KB

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