mapHandler.cpp 68 KB

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