mapHandler.cpp 68 KB

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