2
0

mapHandler.cpp 67 KB

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