mapHandler.cpp 65 KB

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