mapHandler.cpp 63 KB

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