mapHandler.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. bord->notFreeImgs = true;
  526. for (int i=0-Woff; i<reader->map.width+Woff; i++) //jest po szerokoœci
  527. {
  528. for (int j=0-Hoff; j<reader->map.height+Hoff;j++) //po wysokoœci
  529. {
  530. for(int k=0; k<=reader->map.twoLevel; ++k)
  531. {
  532. if(i < 0 || i > (reader->map.width-1) || j < 0 || j > (reader->map.height-1))
  533. {
  534. if(i==-1 && j==-1)
  535. {
  536. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[16].bitmap);
  537. continue;
  538. }
  539. else if(i==-1 && j==(reader->map.height))
  540. {
  541. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[19].bitmap);
  542. continue;
  543. }
  544. else if(i==(reader->map.width) && j==-1)
  545. {
  546. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[17].bitmap);
  547. continue;
  548. }
  549. else if(i==(reader->map.width) && j==(reader->map.height))
  550. {
  551. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[18].bitmap);
  552. continue;
  553. }
  554. else if(j == -1 && i > -1 && i < reader->map.height)
  555. {
  556. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[22+rand()%2].bitmap);
  557. continue;
  558. }
  559. else if(i == -1 && j > -1 && j < reader->map.height)
  560. {
  561. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[33+rand()%2].bitmap);
  562. continue;
  563. }
  564. else if(j == reader->map.height && i >-1 && i < reader->map.width)
  565. {
  566. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[29+rand()%2].bitmap);
  567. continue;
  568. }
  569. else if(i == reader->map.width && j > -1 && j < reader->map.height)
  570. {
  571. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[25+rand()%2].bitmap);
  572. continue;
  573. }
  574. else
  575. {
  576. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[rand()%16].bitmap);
  577. continue;
  578. }
  579. }
  580. //TerrainTile zz = reader->map.terrain[i-Woff][j-Hoff];
  581. std::string name;
  582. if (k>0)
  583. name = CSemiDefHandler::nameFromType(reader->map.undergroungTerrain[i][j].tertype);
  584. else
  585. name = CSemiDefHandler::nameFromType(reader->map.terrain[i][j].tertype);
  586. for (unsigned int m=0; m<reader->defs.size(); m++)
  587. {
  588. try
  589. {
  590. if (reader->defs[m]->defName != name)
  591. continue;
  592. else
  593. {
  594. int ktora;
  595. if (k==0)
  596. ktora = reader->map.terrain[i][j].terview;
  597. else
  598. ktora = reader->map.undergroungTerrain[i][j].terview;
  599. ttiles[i][j][k].terbitmap.push_back(reader->defs[m]->ourImages[ktora].bitmap);
  600. int zz;
  601. if (k==0)
  602. zz = (reader->map.terrain[i][j].siodmyTajemniczyBajt)%4;
  603. else
  604. zz = (reader->map.undergroungTerrain[i][j].siodmyTajemniczyBajt)%4;
  605. switch (zz)
  606. {
  607. case 1:
  608. {
  609. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].terbitmap[0]);
  610. break;
  611. }
  612. case 2:
  613. {
  614. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].terbitmap[0]);
  615. break;
  616. }
  617. case 3:
  618. {
  619. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate03(ttiles[i][j][k].terbitmap[0]);
  620. break;
  621. }
  622. }
  623. break;
  624. }
  625. }
  626. catch (...)
  627. {
  628. continue;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. delete bord;
  635. }
  636. void CMapHandler::initObjectRects()
  637. {
  638. //initializing objects / rects
  639. for(int f=0; f<CGI->objh->objInstances.size(); ++f)
  640. {
  641. /*CGI->objh->objInstances[f]->pos.x+=1;
  642. CGI->objh->objInstances[f]->pos.y+=1;*/
  643. if(!CGI->objh->objInstances[f]->defInfo)
  644. {
  645. continue;
  646. }
  647. CDefHandler * curd = CGI->objh->objInstances[f]->defInfo->handler;
  648. if(curd)
  649. {
  650. for(int fx=0; fx<curd->ourImages[0].bitmap->w>>5; ++fx) //curd->ourImages[0].bitmap->w/32
  651. {
  652. for(int fy=0; fy<curd->ourImages[0].bitmap->h>>5; ++fy) //curd->ourImages[0].bitmap->h/32
  653. {
  654. SDL_Rect cr;
  655. cr.w = 32;
  656. cr.h = 32;
  657. cr.x = fx<<5; //fx*32
  658. cr.y = fy<<5; //fy*32
  659. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(CGI->objh->objInstances[f],cr);
  660. 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)
  661. {
  662. //TerrainTile2 & curt =
  663. // ttiles
  664. // [CGI->objh->objInstances[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  665. //[CGI->objh->objInstances[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  666. //[CGI->objh->objInstances[f]->pos.z];
  667. 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);
  668. }
  669. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  670. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  671. }//if curd
  672. } // for(int f=0; f<CGI->objh->objInstances.size(); ++f)
  673. for(int ix=0; ix<ttiles.size()-Woff; ++ix)
  674. {
  675. for(int iy=0; iy<ttiles[0].size()-Hoff; ++iy)
  676. {
  677. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  678. {
  679. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  680. }
  681. }
  682. }
  683. }
  684. void CMapHandler::calculateBlockedPos()
  685. {
  686. for(int f=0; f<CGI->objh->objInstances.size(); ++f) //calculationg blocked / visitable positions
  687. {
  688. if(!CGI->objh->objInstances[f]->defInfo)
  689. continue;
  690. CDefHandler * curd = CGI->objh->objInstances[f]->defInfo->handler;
  691. for(int fx=0; fx<8; ++fx)
  692. {
  693. for(int fy=0; fy<6; ++fy)
  694. {
  695. int xVal = CGI->objh->objInstances[f]->pos.x + fx - 7;
  696. int yVal = CGI->objh->objInstances[f]->pos.y + fy - 5;
  697. int zVal = CGI->objh->objInstances[f]->pos.z;
  698. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  699. {
  700. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  701. if(((CGI->objh->objInstances[f]->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  702. curt.visitable = true;
  703. if(!((CGI->objh->objInstances[f]->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  704. curt.blocked = true;
  705. }
  706. }
  707. }
  708. }
  709. }
  710. void CMapHandler::init()
  711. {
  712. //loading castles' defs
  713. std::ifstream ifs("config/townsDefs.txt");
  714. int ccc;
  715. ifs>>ccc;
  716. for(int i=0;i<ccc*2;i++)
  717. {
  718. CGDefInfo * n = new CGDefInfo(*CGI->dobjinfo->castles[i%ccc]);
  719. ifs >> n->name;
  720. if (!(n->handler = CGI->spriteh->giveDef(n->name)))
  721. std::cout << "Cannot open "<<n->name<<std::endl;
  722. if(i<ccc)
  723. villages[i]=n;
  724. else
  725. capitols[i%ccc]=n;
  726. alphaTransformDef(n);
  727. }
  728. for(int i=0;i<CGI->scenarioOps.playerInfos.size();i++)
  729. {
  730. if(CGI->scenarioOps.playerInfos[i].castle==-1)
  731. {
  732. int f;
  733. do
  734. {
  735. f = rand()%F_NUMBER;
  736. }while(!(reader->map.players[CGI->scenarioOps.playerInfos[i].color].allowedFactions & 1<<f));
  737. CGI->scenarioOps.playerInfos[i].castle = f;
  738. }
  739. }
  740. for(int i=0;i<PLAYER_LIMIT;i++)
  741. {
  742. for(int j=0; j<reader->map.players[i].heroesNames.size();j++)
  743. {
  744. usedHeroes.insert(reader->map.players[i].heroesNames[j].heroID);
  745. }
  746. }
  747. timeHandler th;
  748. th.getDif();
  749. randomizeObjects();//randomizing objects on map
  750. std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
  751. for(int h=0; h<reader->map.defy.size(); ++h) //initializing loaded def handler's info
  752. {
  753. //std::string hlp = reader->map.defy[h]->name;
  754. //std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  755. CGI->mh->loadedDefs.insert(std::make_pair(reader->map.defy[h]->name, reader->map.defy[h]->handler));
  756. }
  757. std::cout<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  758. prepareFOWDefs();
  759. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  760. borderAndTerrainBitmapInit();
  761. std::cout<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  762. for(int i=0;i<PLAYER_LIMIT;i++)
  763. {
  764. if(reader->map.players[i].generateHeroAtMainTown && reader->map.players[i].hasMainTown)
  765. {
  766. int3 hpos = reader->map.players[i].posOfMainTown;
  767. hpos.x+=1; hpos.y+=1;
  768. int j;
  769. for(j=0;j<CGI->scenarioOps.playerInfos.size();j++)
  770. if(CGI->scenarioOps.playerInfos[j].color==i)
  771. break;
  772. if(j==CGI->scenarioOps.playerInfos.size())
  773. continue;
  774. int h = CGI->scenarioOps.playerInfos[j].hero;
  775. if(h<0)
  776. h=pickHero(i);
  777. CGHeroInstance * nnn = (CGHeroInstance*)createObject(34,h,hpos,i);
  778. nnn->defInfo->handler = CGI->heroh->flags1[0];
  779. CGI->heroh->heroInstances.push_back(nnn);
  780. CGI->objh->objInstances.push_back(nnn);
  781. }
  782. }
  783. initObjectRects();
  784. std::cout<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  785. calculateBlockedPos();
  786. std::cout<<"\tCalculating blockmap: "<<th.getDif()<<std::endl;
  787. }
  788. 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)
  789. {
  790. if(!otherHeroAnim)
  791. heroAnim = anim; //the same, as it should be
  792. //setting surface to blit at
  793. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  794. if(extSurf)
  795. {
  796. su = extSurf;
  797. }
  798. else
  799. {
  800. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  801. }
  802. if (((dx+x)>((reader->map.width+Woff)) || (dy+y)>((reader->map.height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  803. throw new std::string("terrainRect: out of range");
  804. ////printing terrain
  805. for (int bx=0; bx<dx; bx++)
  806. {
  807. for (int by=0; by<dy; by++)
  808. {
  809. SDL_Rect sr;
  810. sr.y=by*32;
  811. sr.x=bx*32;
  812. sr.h=sr.w=32;
  813. validateRectTerr(&sr, extRect);
  814. 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);
  815. }
  816. }
  817. ////terrain printed
  818. ////printing rivers
  819. for (int bx=0; bx<dx; bx++)
  820. {
  821. for (int by=0; by<dy; by++)
  822. {
  823. SDL_Rect sr;
  824. sr.y=by*32;
  825. sr.x=bx*32;
  826. sr.h=sr.w=32;
  827. validateRectTerr(&sr, extRect);
  828. if(ttiles[x+bx][y+by][level].rivbitmap.size())
  829. {
  830. 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);
  831. }
  832. }
  833. }
  834. ////rivers printed
  835. ////printing roads
  836. for (int bx=0; bx<dx; bx++)
  837. {
  838. for (int by=-1; by<dy; by++)
  839. {
  840. if(y+by<=-4)
  841. continue;
  842. SDL_Rect sr;
  843. sr.y=by*32+16;
  844. sr.x=bx*32;
  845. sr.h=sr.w=32;
  846. validateRectTerr(&sr, extRect);
  847. if(ttiles[x+bx][y+by][level].roadbitmap.size())
  848. {
  849. 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);
  850. }
  851. }
  852. }
  853. ////roads printed
  854. ////printing objects
  855. for (int bx=0; bx<dx; bx++)
  856. {
  857. for (int by=0; by<dy; by++)
  858. {
  859. for(int h=0; h<ttiles[x+bx][y+by][level].objects.size(); ++h)
  860. {
  861. SDL_Rect sr;
  862. sr.w = 32;
  863. sr.h = 32;
  864. sr.x = (bx)*32;
  865. sr.y = (by)*32;
  866. validateRectTerr(&sr, extRect);
  867. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  868. pp.h = sr.h;
  869. pp.w = sr.w;
  870. CGHeroInstance * themp = (dynamic_cast<CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  871. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  872. {
  873. int imgVal = 8;
  874. SDL_Surface * tb;
  875. if(themp->type==NULL)
  876. continue;
  877. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  878. int gg;
  879. for(gg=0; gg<iv.size(); ++gg)
  880. {
  881. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  882. {
  883. tb = iv[gg+heroAnim%imgVal].bitmap;
  884. break;
  885. }
  886. }
  887. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  888. pp.y+=imgVal*2-32;
  889. sr.y-=16;
  890. CSDL_Ext::blit8bppAlphaTo24bpp(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  891. }
  892. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  893. {
  894. int imgVal = 8;
  895. SDL_Surface * tb;
  896. if(themp->type==NULL)
  897. continue;
  898. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  899. int gg;
  900. for(gg=0; gg<iv.size(); ++gg)
  901. {
  902. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  903. {
  904. tb = iv[gg].bitmap;
  905. break;
  906. }
  907. }
  908. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  909. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  910. {
  911. SDL_Rect bufr = sr;
  912. bufr.x-=2*32;
  913. bufr.y-=1*32;
  914. CSDL_Ext::blit8bppAlphaTo24bpp(CGI->heroh->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+heroAnim%imgVal].bitmap, NULL, su, &bufr);
  915. themp->flagPrinted = true;
  916. }
  917. }
  918. else
  919. {
  920. int imgVal = ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages.size();
  921. //setting appropriate flag color
  922. 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)
  923. 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);
  924. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[anim%imgVal].bitmap,&pp,su,&sr);
  925. }
  926. }
  927. }
  928. }
  929. ////objects printed, printing shadow
  930. for (int bx=0; bx<dx; bx++)
  931. {
  932. for (int by=0; by<dy; by++)
  933. {
  934. SDL_Rect sr;
  935. sr.y=by*32;
  936. sr.x=bx*32;
  937. sr.h=sr.w=32;
  938. validateRectTerr(&sr, extRect);
  939. 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])
  940. {
  941. SDL_Surface * hide = getVisBitmap(bx+x, by+y, visibilityMap, level);
  942. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  943. }
  944. }
  945. }
  946. ////shadow printed
  947. //printing borders
  948. for (int bx=0; bx<dx; bx++)
  949. {
  950. for (int by=0; by<dy; by++)
  951. {
  952. if(bx+x<0 || by+y<0 || bx+x>reader->map.width+(-1) || by+y>reader->map.height+(-1))
  953. {
  954. SDL_Rect sr;
  955. sr.y=by*32;
  956. sr.x=bx*32;
  957. sr.h=sr.w=32;
  958. validateRectTerr(&sr, extRect);
  959. 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);
  960. }
  961. else
  962. {
  963. if(MARK_BLOCKED_POSITIONS && ttiles[x+bx][y+by][level].blocked) //temporary hiding blocked positions
  964. {
  965. SDL_Rect sr;
  966. sr.y=by*32;
  967. sr.x=bx*32;
  968. sr.h=sr.w=32;
  969. validateRectTerr(&sr, extRect);
  970. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  971. for(int f=0; f<ns->w*ns->h*4; ++f)
  972. {
  973. *((unsigned char*)(ns->pixels) + f) = 128;
  974. }
  975. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  976. SDL_FreeSurface(ns);
  977. }
  978. if(MARK_VISITABLE_POSITIONS && ttiles[x+bx][y+by][level].visitable) //temporary hiding visitable positions
  979. {
  980. SDL_Rect sr;
  981. sr.y=by*32;
  982. sr.x=bx*32;
  983. sr.h=sr.w=32;
  984. validateRectTerr(&sr, extRect);
  985. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  986. for(int f=0; f<ns->w*ns->h*4; ++f)
  987. {
  988. *((unsigned char*)(ns->pixels) + f) = 128;
  989. }
  990. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  991. SDL_FreeSurface(ns);
  992. }
  993. }
  994. }
  995. }
  996. //borders printed
  997. return su;
  998. }
  999. SDL_Surface * CMapHandler::terrBitmap(int x, int y)
  1000. {
  1001. return ttiles[x+Woff][y+Hoff][0].terbitmap[0];
  1002. }
  1003. SDL_Surface * CMapHandler::undTerrBitmap(int x, int y)
  1004. {
  1005. return ttiles[x+Woff][y+Hoff][0].terbitmap[1];
  1006. }
  1007. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, PseudoV< PseudoV< PseudoV<unsigned char> > > & visibilityMap, int lvl)
  1008. {
  1009. 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])
  1010. {
  1011. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  1012. }
  1013. 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])
  1014. {
  1015. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  1016. }
  1017. 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])
  1018. {
  1019. return partialHide->ourImages[15].bitmap; //visible right top corner
  1020. }
  1021. 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])
  1022. {
  1023. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  1024. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  1025. }
  1026. 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])
  1027. {
  1028. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  1029. return partialHide->ourImages[35].bitmap;
  1030. }
  1031. 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])
  1032. {
  1033. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  1034. return partialHide->ourImages[0].bitmap; //visible top
  1035. }
  1036. 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])
  1037. {
  1038. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  1039. return partialHide->ourImages[4].bitmap; //visble bottom
  1040. }
  1041. 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])
  1042. {
  1043. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  1044. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  1045. return partialHide->ourImages[36].bitmap;
  1046. }
  1047. 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])
  1048. {
  1049. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  1050. return partialHide->ourImages[2].bitmap; //visible right
  1051. }
  1052. 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])
  1053. {
  1054. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  1055. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner 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])
  1058. {
  1059. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  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])
  1062. {
  1063. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  1064. return partialHide->ourImages[37].bitmap;
  1065. }
  1066. 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])
  1067. {
  1068. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1069. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1070. return partialHide->ourImages[38].bitmap;
  1071. }
  1072. 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])
  1073. {
  1074. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  1075. }
  1076. 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])
  1077. {
  1078. return partialHide->ourImages[16].bitmap; //visible right corners
  1079. }
  1080. 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[18].bitmap; //visible top corners
  1083. }
  1084. 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])
  1085. {
  1086. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  1087. return partialHide->ourImages[39].bitmap;
  1088. }
  1089. 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])
  1090. {
  1091. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  1092. return partialHide->ourImages[40].bitmap;
  1093. }
  1094. 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])
  1095. {
  1096. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  1097. }
  1098. 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])
  1099. {
  1100. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  1101. return partialHide->ourImages[41].bitmap;
  1102. }
  1103. 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])
  1104. {
  1105. return partialHide->ourImages[19].bitmap; //visible corners without left top
  1106. }
  1107. 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])
  1108. {
  1109. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  1110. }
  1111. 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])
  1112. {
  1113. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  1114. return partialHide->ourImages[42].bitmap;
  1115. }
  1116. 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])
  1117. {
  1118. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  1119. return partialHide->ourImages[43].bitmap;
  1120. }
  1121. 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])
  1122. {
  1123. return partialHide->ourImages[21].bitmap; //visible all corners only
  1124. }
  1125. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl])
  1126. {
  1127. return partialHide->ourImages[6].bitmap; //hidden top
  1128. }
  1129. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1130. {
  1131. return partialHide->ourImages[7].bitmap; //hidden right
  1132. }
  1133. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1134. {
  1135. return partialHide->ourImages[8].bitmap; //hidden bottom
  1136. }
  1137. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1138. {
  1139. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  1140. return partialHide->ourImages[44].bitmap;
  1141. }
  1142. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl])
  1143. {
  1144. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  1145. }
  1146. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1147. {
  1148. return partialHide->ourImages[29].bitmap; //hidden left and right
  1149. }
  1150. 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])
  1151. {
  1152. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  1153. }
  1154. 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])
  1155. {
  1156. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  1157. return partialHide->ourImages[45].bitmap;
  1158. }
  1159. 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])
  1160. {
  1161. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1162. }
  1163. 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])
  1164. {
  1165. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  1166. return partialHide->ourImages[46].bitmap;
  1167. }
  1168. 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])
  1169. {
  1170. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  1171. return partialHide->ourImages[47].bitmap;
  1172. }
  1173. 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])
  1174. {
  1175. return partialHide->ourImages[32].bitmap; //visible left and right corners
  1176. }
  1177. 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])
  1178. {
  1179. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  1180. return partialHide->ourImages[48].bitmap;
  1181. }
  1182. 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])
  1183. {
  1184. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  1185. }
  1186. 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])
  1187. {
  1188. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1189. }
  1190. 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])
  1191. {
  1192. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  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[26].bitmap; //visible right and left top 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[32].bitmap); //visible right and left cornres
  1201. return partialHide->ourImages[49].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])
  1204. {
  1205. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1206. }
  1207. 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])
  1208. {
  1209. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1210. }
  1211. 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])
  1212. {
  1213. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  1214. return partialHide->ourImages[50].bitmap;
  1215. }
  1216. 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])
  1217. {
  1218. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  1219. return partialHide->ourImages[51].bitmap;
  1220. }
  1221. //newly added
  1222. 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
  1223. {
  1224. return partialHide->ourImages[0].bitmap;
  1225. }
  1226. 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
  1227. {
  1228. return partialHide->ourImages[1].bitmap;
  1229. }
  1230. 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
  1231. {
  1232. return partialHide->ourImages[4].bitmap;
  1233. }
  1234. 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
  1235. {
  1236. return partialHide->ourImages[5].bitmap;
  1237. }
  1238. 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
  1239. {
  1240. return partialHide->ourImages[36].bitmap;
  1241. }
  1242. 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
  1243. {
  1244. return partialHide->ourImages[36].bitmap;
  1245. }
  1246. 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
  1247. {
  1248. return partialHide->ourImages[2].bitmap;
  1249. }
  1250. 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
  1251. {
  1252. return partialHide->ourImages[3].bitmap;
  1253. }
  1254. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1255. }
  1256. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1257. {
  1258. int ret=-1;
  1259. 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][CGI->mh->reader->map.width-1][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[CGI->mh->reader->map.width-1][a.y][a.z].malle];
  1263. else if(a.x<CGI->mh->reader->map.width && a.y>=CGI->mh->reader->map.height)
  1264. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->reader->map.width-1][a.z].malle];
  1265. else
  1266. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].malle];
  1267. if(!(a.x==b.x || a.y==b.y))
  1268. ret*=1.41421;
  1269. //TODO: use hero's pathfinding skill during calculating cost
  1270. return ret;
  1271. }
  1272. std::vector < std::string > CMapHandler::getObjDescriptions(int3 pos)
  1273. {
  1274. std::vector < std::pair<CGObjectInstance*,SDL_Rect > > objs = ttiles[pos.x][pos.y][pos.z].objects;
  1275. std::vector<std::string> ret;
  1276. for(int g=0; g<objs.size(); ++g)
  1277. {
  1278. 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 &&
  1279. (((objs[g].first->defInfo->blockMap[5-(objs[g].first->pos.y-pos.y)])>>((objs[g].first->pos.x-pos.x)))&1)==0
  1280. ) //checking position blocking
  1281. {
  1282. //unsigned char * blm = objs[g].first->defInfo->blockMap;
  1283. if (objs[g].first->state)
  1284. ret.push_back(objs[g].first->state->hoverText(objs[g].first));
  1285. else
  1286. ret.push_back(CGI->objh->objects[objs[g].first->ID].name);
  1287. }
  1288. }
  1289. return ret;
  1290. }
  1291. std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1292. {
  1293. std::vector < CGObjectInstance * > ret;
  1294. for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1295. {
  1296. CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1297. if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1298. ret.push_back(curi);
  1299. }
  1300. return ret;
  1301. }
  1302. CGObjectInstance * CMapHandler::createObject(int id, int subid, int3 pos, int owner)
  1303. {
  1304. CGObjectInstance * nobj;
  1305. switch(id)
  1306. {
  1307. case 34: //hero
  1308. {
  1309. CGHeroInstance * nobj;
  1310. nobj = new CGHeroInstance();
  1311. nobj->pos = pos;
  1312. nobj->tempOwner = owner;
  1313. nobj->defInfo = new CGDefInfo();
  1314. nobj->defInfo->id = 34;
  1315. nobj->defInfo->subid = subid;
  1316. nobj->type = CGI->heroh->heroes[subid];
  1317. for(int i=0;i<6;i++)
  1318. {
  1319. nobj->defInfo->blockMap[i]=255;
  1320. nobj->defInfo->visitMap[i]=0;
  1321. }
  1322. nobj->ID = id;
  1323. nobj->subID = subid;
  1324. nobj->defInfo->handler=NULL;
  1325. nobj->defInfo->blockMap[5] = 253;
  1326. nobj->defInfo->visitMap[5] = 2;
  1327. nobj->artifWorn.resize(20);
  1328. nobj->artifacts.resize(20);
  1329. nobj->artifWorn[16] = &CGI->arth->artifacts[3];
  1330. nobj->primSkills.resize(4);
  1331. nobj->primSkills[0] = nobj->type->heroClass->initialAttack;
  1332. nobj->primSkills[1] = nobj->type->heroClass->initialDefence;
  1333. nobj->primSkills[2] = nobj->type->heroClass->initialPower;
  1334. nobj->primSkills[3] = nobj->type->heroClass->initialKnowledge;
  1335. nobj->mana = 10 * nobj->primSkills[3];
  1336. return nobj;
  1337. }
  1338. case 98: //town
  1339. nobj = new CGTownInstance;
  1340. break;
  1341. default: //rest of objects
  1342. nobj = new CGObjectInstance;
  1343. nobj->defInfo = CGI->dobjinfo->gobjs[id][subid];
  1344. break;
  1345. }
  1346. nobj->ID = id;
  1347. nobj->subID = subid;
  1348. if(!nobj->defInfo)
  1349. std::cout <<"No def declaration for " <<id <<" "<<subid<<std::endl;
  1350. nobj->pos = pos;
  1351. //nobj->state = NULL;//new CLuaObjectScript();
  1352. nobj->tempOwner = owner;
  1353. nobj->info = NULL;
  1354. nobj->defInfo->id = id;
  1355. nobj->defInfo->subid = subid;
  1356. //assigning defhandler
  1357. if(nobj->ID==34 || nobj->ID==98)
  1358. return nobj;
  1359. nobj->defInfo = CGI->dobjinfo->gobjs[id][subid];
  1360. if(!nobj->defInfo->handler)
  1361. nobj->defInfo->handler = CGI->spriteh->giveDef(nobj->defInfo->name);
  1362. return nobj;
  1363. }
  1364. std::string CMapHandler::getDefName(int id, int subid)
  1365. {
  1366. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1367. if(temp)
  1368. return temp->name;
  1369. throw new std::exception("Def not found.");
  1370. }
  1371. bool CMapHandler::printObject(CGObjectInstance *obj)
  1372. {
  1373. CDefHandler * curd = obj->defInfo->handler;
  1374. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1375. {
  1376. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1377. {
  1378. SDL_Rect cr;
  1379. cr.w = 32;
  1380. cr.h = 32;
  1381. cr.x = fx*32;
  1382. cr.y = fy*32;
  1383. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1384. 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)
  1385. {
  1386. TerrainTile2 & curt =
  1387. ttiles
  1388. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1389. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1390. [obj->pos.z];
  1391. 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);
  1392. }
  1393. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1394. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1395. return true;
  1396. }
  1397. bool CMapHandler::hideObject(CGObjectInstance *obj)
  1398. {
  1399. CDefHandler * curd = obj->defInfo->handler;
  1400. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1401. {
  1402. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1403. {
  1404. 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)
  1405. {
  1406. 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;
  1407. for(int dd=0; dd<ctile.size(); ++dd)
  1408. {
  1409. if(ctile[dd].first->id==obj->id)
  1410. ctile.erase(ctile.begin() + dd);
  1411. }
  1412. }
  1413. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1414. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1415. return true;
  1416. }
  1417. std::string CMapHandler::getRandomizedDefName(CGDefInfo *di, CGObjectInstance * obj)
  1418. {
  1419. return std::string();
  1420. }
  1421. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1422. {
  1423. hideObject(obj);
  1424. std::vector<CGObjectInstance *>::iterator db = std::find(CGI->objh->objInstances.begin(), CGI->objh->objInstances.end(), obj);
  1425. recalculateHideVisPosUnderObj(*db);
  1426. delete *db;
  1427. CGI->objh->objInstances.erase(db);
  1428. return true;
  1429. }
  1430. bool CMapHandler::recalculateHideVisPos(int3 &pos)
  1431. {
  1432. ttiles[pos.x][pos.y][pos.z].visitable = false;
  1433. ttiles[pos.x][pos.y][pos.z].blocked = false;
  1434. for(int i=0; i<ttiles[pos.x][pos.y][pos.z].objects.size(); ++i)
  1435. {
  1436. CDefHandler * curd = ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->handler;
  1437. for(int fx=0; fx<8; ++fx)
  1438. {
  1439. for(int fy=0; fy<6; ++fy)
  1440. {
  1441. int xVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.x + fx - 7;
  1442. int yVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.y + fy - 5;
  1443. int zVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.z;
  1444. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  1445. {
  1446. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  1447. if(((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  1448. curt.visitable = true;
  1449. if(!((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  1450. curt.blocked = true;
  1451. }
  1452. }
  1453. }
  1454. }
  1455. return true;
  1456. }
  1457. bool CMapHandler::recalculateHideVisPosUnderObj(CGObjectInstance *obj, bool withBorder)
  1458. {
  1459. if(withBorder)
  1460. {
  1461. for(int fx=-1; fx<=obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1462. {
  1463. for(int fy=-1; fy<=obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1464. {
  1465. 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)
  1466. {
  1467. 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));
  1468. }
  1469. }
  1470. }
  1471. }
  1472. else
  1473. {
  1474. for(int fx=0; fx<obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1475. {
  1476. for(int fy=0; fy<obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1477. {
  1478. 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)
  1479. {
  1480. 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));
  1481. }
  1482. }
  1483. }
  1484. }
  1485. return true;
  1486. }
  1487. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1488. {
  1489. if(isMoving)
  1490. {
  1491. switch(dir)
  1492. {
  1493. case 1:
  1494. return 10;
  1495. case 2:
  1496. return 5;
  1497. case 3:
  1498. return 6;
  1499. case 4:
  1500. return 7;
  1501. case 5:
  1502. return 8;
  1503. case 6:
  1504. return 9;
  1505. case 7:
  1506. return 12;
  1507. case 8:
  1508. return 11;
  1509. default:
  1510. return -1; //should never happen
  1511. }
  1512. }
  1513. else //if(isMoving)
  1514. {
  1515. switch(dir)
  1516. {
  1517. case 1:
  1518. return 13;
  1519. case 2:
  1520. return 0;
  1521. case 3:
  1522. return 1;
  1523. case 4:
  1524. return 2;
  1525. case 5:
  1526. return 3;
  1527. case 6:
  1528. return 4;
  1529. case 7:
  1530. return 15;
  1531. case 8:
  1532. return 14;
  1533. default:
  1534. return -1; //should never happen
  1535. }
  1536. }
  1537. }
  1538. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1539. {
  1540. if(ext)
  1541. {
  1542. if(val->x<0)
  1543. {
  1544. val->w += val->x;
  1545. val->x = ext->x;
  1546. }
  1547. else
  1548. {
  1549. val->x += ext->x;
  1550. }
  1551. if(val->y<0)
  1552. {
  1553. val->h += val->y;
  1554. val->y = ext->y;
  1555. }
  1556. else
  1557. {
  1558. val->y += ext->y;
  1559. }
  1560. if(val->x+val->w > ext->x+ext->w)
  1561. {
  1562. val->w = ext->x+ext->w-val->x;
  1563. }
  1564. if(val->y+val->h > ext->y+ext->h)
  1565. {
  1566. val->h = ext->y+ext->h-val->y;
  1567. }
  1568. //for sign problems
  1569. if(val->h > 20000 || val->w > 20000)
  1570. {
  1571. val->h = val->w = 0;
  1572. }
  1573. }
  1574. }
  1575. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1576. {
  1577. if(a.z!=b.z)
  1578. return -1; //error!
  1579. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1580. {
  1581. return 0;
  1582. }
  1583. else if(a.x==b.x && a.y==b.y+1) //t
  1584. {
  1585. return 1;
  1586. }
  1587. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1588. {
  1589. return 2;
  1590. }
  1591. else if(a.x==b.x-1 && a.y==b.y) //r
  1592. {
  1593. return 3;
  1594. }
  1595. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1596. {
  1597. return 4;
  1598. }
  1599. else if(a.x==b.x && a.y==b.y-1) //b
  1600. {
  1601. return 5;
  1602. }
  1603. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1604. {
  1605. return 6;
  1606. }
  1607. else if(a.x==b.x+1 && a.y==b.y) //l
  1608. {
  1609. return 7;
  1610. }
  1611. return -2; //shouldn't happen
  1612. }