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