mapHandler.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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. bord->notFreeImgs = true;
  526. for (int i=0-Woff; i<reader->map.width+Woff; i++) //jest po szerokoœci
  527. {
  528. for (int j=0-Hoff; j<reader->map.height+Hoff;j++) //po wysokoœci
  529. {
  530. for(int k=0; k<=reader->map.twoLevel; ++k)
  531. {
  532. if(i < 0 || i > (reader->map.width-1) || j < 0 || j > (reader->map.height-1))
  533. {
  534. if(i==-1 && j==-1)
  535. {
  536. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[16].bitmap);
  537. continue;
  538. }
  539. else if(i==-1 && j==(reader->map.height))
  540. {
  541. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[19].bitmap);
  542. continue;
  543. }
  544. else if(i==(reader->map.width) && j==-1)
  545. {
  546. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[17].bitmap);
  547. continue;
  548. }
  549. else if(i==(reader->map.width) && j==(reader->map.height))
  550. {
  551. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[18].bitmap);
  552. continue;
  553. }
  554. else if(j == -1 && i > -1 && i < reader->map.height)
  555. {
  556. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[22+rand()%2].bitmap);
  557. continue;
  558. }
  559. else if(i == -1 && j > -1 && j < reader->map.height)
  560. {
  561. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[33+rand()%2].bitmap);
  562. continue;
  563. }
  564. else if(j == reader->map.height && i >-1 && i < reader->map.width)
  565. {
  566. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[29+rand()%2].bitmap);
  567. continue;
  568. }
  569. else if(i == reader->map.width && j > -1 && j < reader->map.height)
  570. {
  571. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[25+rand()%2].bitmap);
  572. continue;
  573. }
  574. else
  575. {
  576. ttiles[i][j][k].terbitmap.push_back(bord->ourImages[rand()%16].bitmap);
  577. continue;
  578. }
  579. }
  580. //TerrainTile zz = reader->map.terrain[i-Woff][j-Hoff];
  581. std::string name;
  582. if (k>0)
  583. name = CSemiDefHandler::nameFromType(reader->map.undergroungTerrain[i][j].tertype);
  584. else
  585. name = CSemiDefHandler::nameFromType(reader->map.terrain[i][j].tertype);
  586. for (unsigned int m=0; m<reader->defs.size(); m++)
  587. {
  588. try
  589. {
  590. if (reader->defs[m]->defName != name)
  591. continue;
  592. else
  593. {
  594. int ktora;
  595. if (k==0)
  596. ktora = reader->map.terrain[i][j].terview;
  597. else
  598. ktora = reader->map.undergroungTerrain[i][j].terview;
  599. ttiles[i][j][k].terbitmap.push_back(reader->defs[m]->ourImages[ktora].bitmap);
  600. int zz;
  601. if (k==0)
  602. zz = (reader->map.terrain[i][j].siodmyTajemniczyBajt)%4;
  603. else
  604. zz = (reader->map.undergroungTerrain[i][j].siodmyTajemniczyBajt)%4;
  605. switch (zz)
  606. {
  607. case 1:
  608. {
  609. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate01(ttiles[i][j][k].terbitmap[0]);
  610. break;
  611. }
  612. case 2:
  613. {
  614. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::hFlip(ttiles[i][j][k].terbitmap[0]);
  615. break;
  616. }
  617. case 3:
  618. {
  619. ttiles[i][j][k].terbitmap[0] = CSDL_Ext::rotate03(ttiles[i][j][k].terbitmap[0]);
  620. break;
  621. }
  622. }
  623. break;
  624. }
  625. }
  626. catch (...)
  627. {
  628. continue;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. delete bord;
  635. }
  636. void CMapHandler::initObjectRects()
  637. {
  638. //initializing objects / rects
  639. for(int f=0; f<CGI->objh->objInstances.size(); ++f)
  640. {
  641. /*CGI->objh->objInstances[f]->pos.x+=1;
  642. CGI->objh->objInstances[f]->pos.y+=1;*/
  643. if(!CGI->objh->objInstances[f]->defInfo)
  644. {
  645. continue;
  646. }
  647. CDefHandler * curd = CGI->objh->objInstances[f]->defInfo->handler;
  648. if(curd)
  649. {
  650. for(int fx=0; fx<curd->ourImages[0].bitmap->w>>5; ++fx) //curd->ourImages[0].bitmap->w/32
  651. {
  652. for(int fy=0; fy<curd->ourImages[0].bitmap->h>>5; ++fy) //curd->ourImages[0].bitmap->h/32
  653. {
  654. SDL_Rect cr;
  655. cr.w = 32;
  656. cr.h = 32;
  657. cr.x = fx<<5; //fx*32
  658. cr.y = fy<<5; //fy*32
  659. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(CGI->objh->objInstances[f],cr);
  660. 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)
  661. {
  662. //TerrainTile2 & curt =
  663. // ttiles
  664. // [CGI->objh->objInstances[f]->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  665. //[CGI->objh->objInstances[f]->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  666. //[CGI->objh->objInstances[f]->pos.z];
  667. 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);
  668. }
  669. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  670. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  671. }//if curd
  672. } // for(int f=0; f<CGI->objh->objInstances.size(); ++f)
  673. for(int ix=0; ix<ttiles.size()-Woff; ++ix)
  674. {
  675. for(int iy=0; iy<ttiles[0].size()-Hoff; ++iy)
  676. {
  677. for(int iz=0; iz<ttiles[0][0].size(); ++iz)
  678. {
  679. stable_sort(ttiles[ix][iy][iz].objects.begin(), ttiles[ix][iy][iz].objects.end(), ocmptwo);
  680. }
  681. }
  682. }
  683. }
  684. void CMapHandler::calculateBlockedPos()
  685. {
  686. for(int f=0; f<CGI->objh->objInstances.size(); ++f) //calculationg blocked / visitable positions
  687. {
  688. if(!CGI->objh->objInstances[f]->defInfo)
  689. continue;
  690. CDefHandler * curd = CGI->objh->objInstances[f]->defInfo->handler;
  691. for(int fx=0; fx<8; ++fx)
  692. {
  693. for(int fy=0; fy<6; ++fy)
  694. {
  695. int xVal = CGI->objh->objInstances[f]->pos.x + fx - 7;
  696. int yVal = CGI->objh->objInstances[f]->pos.y + fy - 5;
  697. int zVal = CGI->objh->objInstances[f]->pos.z;
  698. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  699. {
  700. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  701. if(((CGI->objh->objInstances[f]->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  702. curt.visitable = true;
  703. if(!((CGI->objh->objInstances[f]->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  704. curt.blocked = true;
  705. }
  706. }
  707. }
  708. }
  709. }
  710. void CMapHandler::init()
  711. {
  712. //loading castles' defs
  713. std::ifstream ifs("config/townsDefs.txt");
  714. int ccc;
  715. ifs>>ccc;
  716. for(int i=0;i<ccc*2;i++)
  717. {
  718. CGDefInfo * n = new CGDefInfo(*CGI->dobjinfo->castles[i%ccc]);
  719. ifs >> n->name;
  720. if (!(n->handler = CGI->spriteh->giveDef(n->name)))
  721. std::cout << "Cannot open "<<n->name<<std::endl;
  722. if(i<ccc)
  723. villages[i]=n;
  724. else
  725. capitols[i%ccc]=n;
  726. alphaTransformDef(n);
  727. }
  728. for(int i=0;i<CGI->scenarioOps.playerInfos.size();i++)
  729. {
  730. if(CGI->scenarioOps.playerInfos[i].castle==-1)
  731. {
  732. int f;
  733. do
  734. {
  735. f = rand()%F_NUMBER;
  736. }while(!(reader->map.players[CGI->scenarioOps.playerInfos[i].color].allowedFactions & 1<<f));
  737. CGI->scenarioOps.playerInfos[i].castle = f;
  738. }
  739. }
  740. for(int i=0;i<PLAYER_LIMIT;i++)
  741. {
  742. for(int j=0; j<reader->map.players[i].heroesNames.size();j++)
  743. {
  744. usedHeroes.insert(reader->map.players[i].heroesNames[j].heroID);
  745. }
  746. }
  747. timeHandler th;
  748. th.getDif();
  749. randomizeObjects();//randomizing objects on map
  750. std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
  751. for(int h=0; h<reader->map.defy.size(); ++h) //initializing loaded def handler's info
  752. {
  753. //std::string hlp = reader->map.defy[h]->name;
  754. //std::transform(hlp.begin(), hlp.end(), hlp.begin(), (int(*)(int))toupper);
  755. CGI->mh->loadedDefs.insert(std::make_pair(reader->map.defy[h]->name, reader->map.defy[h]->handler));
  756. }
  757. std::cout<<"\tCollecting loaded def's handlers: "<<th.getDif()<<std::endl;
  758. prepareFOWDefs();
  759. roadsRiverTerrainInit(); //road's and river's DefHandlers; and simple values initialization
  760. borderAndTerrainBitmapInit();
  761. std::cout<<"\tPreparing FoW, roads, rivers,borders: "<<th.getDif()<<std::endl;
  762. //giving starting hero
  763. for(int i=0;i<PLAYER_LIMIT;i++)
  764. {
  765. if((reader->map.players[i].generateHeroAtMainTown && reader->map.players[i].hasMainTown) || (reader->map.players[i].hasMainTown && reader->map.version==RoE))
  766. {
  767. int3 hpos = reader->map.players[i].posOfMainTown;
  768. hpos.x+=1;// hpos.y+=1;
  769. int j;
  770. for(j=0;j<CGI->scenarioOps.playerInfos.size();j++)
  771. if(CGI->scenarioOps.playerInfos[j].color==i)
  772. break;
  773. if(j==CGI->scenarioOps.playerInfos.size())
  774. continue;
  775. int h; //= CGI->scenarioOps.playerInfos[j].hero;
  776. //if(h<0)
  777. h=pickHero(i);
  778. CGHeroInstance * nnn = (CGHeroInstance*)createObject(34,h,hpos,i);
  779. nnn->defInfo->handler = CGI->heroh->flags1[0];
  780. CGI->heroh->heroInstances.push_back(nnn);
  781. CGI->objh->objInstances.push_back(nnn);
  782. }
  783. }
  784. std::cout<<"\tGiving starting heroes: "<<th.getDif()<<std::endl;
  785. initObjectRects();
  786. std::cout<<"\tMaking object rects: "<<th.getDif()<<std::endl;
  787. calculateBlockedPos();
  788. std::cout<<"\tCalculating blockmap: "<<th.getDif()<<std::endl;
  789. //initailizing battle backgrounds
  790. std::ifstream bback("config/battleBack.txt");
  791. battleBacks.resize(9);
  792. for(int i=0; i<9; ++i) //9 - number of terrains battle can be fought on
  793. {
  794. int am;
  795. bback>>am;
  796. battleBacks[i].resize(am);
  797. for(int f=0; f<am; ++f)
  798. {
  799. bback>>battleBacks[i][f];
  800. }
  801. }
  802. //initializing battle hero animation
  803. std::ifstream bher("config/battleHeroes.txt");
  804. int numberofh;
  805. bher>>numberofh;
  806. battleHeroes.resize(numberofh);
  807. for(int i=0; i<numberofh; ++i) //9 - number of terrains battle can be fought on
  808. {
  809. bher>>battleHeroes[i];
  810. }
  811. }
  812. 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)
  813. {
  814. if(!otherHeroAnim)
  815. heroAnim = anim; //the same, as it should be
  816. //setting surface to blit at
  817. SDL_Surface * su = NULL; //blitting surface CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  818. if(extSurf)
  819. {
  820. su = extSurf;
  821. }
  822. else
  823. {
  824. su = CSDL_Ext::newSurface(dx*32, dy*32, CSDL_Ext::std32bppSurface);
  825. }
  826. if (((dx+x)>((reader->map.width+Woff)) || (dy+y)>((reader->map.height+Hoff))) || ((x<-Woff)||(y<-Hoff) ) )
  827. throw new std::string("terrainRect: out of range");
  828. ////printing terrain
  829. for (int bx=0; bx<dx; bx++)
  830. {
  831. for (int by=0; by<dy; by++)
  832. {
  833. SDL_Rect sr;
  834. sr.y=by*32;
  835. sr.x=bx*32;
  836. sr.h=sr.w=32;
  837. validateRectTerr(&sr, extRect);
  838. 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);
  839. }
  840. }
  841. ////terrain printed
  842. ////printing rivers
  843. for (int bx=0; bx<dx; bx++)
  844. {
  845. for (int by=0; by<dy; by++)
  846. {
  847. SDL_Rect sr;
  848. sr.y=by*32;
  849. sr.x=bx*32;
  850. sr.h=sr.w=32;
  851. validateRectTerr(&sr, extRect);
  852. if(ttiles[x+bx][y+by][level].rivbitmap.size())
  853. {
  854. 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);
  855. }
  856. }
  857. }
  858. ////rivers printed
  859. ////printing roads
  860. for (int bx=0; bx<dx; bx++)
  861. {
  862. for (int by=-1; by<dy; by++)
  863. {
  864. if(y+by<=-4)
  865. continue;
  866. SDL_Rect sr;
  867. sr.y=by*32+16;
  868. sr.x=bx*32;
  869. sr.h=sr.w=32;
  870. validateRectTerr(&sr, extRect);
  871. if(ttiles[x+bx][y+by][level].roadbitmap.size())
  872. {
  873. 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);
  874. }
  875. }
  876. }
  877. ////roads printed
  878. ////printing objects
  879. for (int bx=0; bx<dx; bx++)
  880. {
  881. for (int by=0; by<dy; by++)
  882. {
  883. for(int h=0; h<ttiles[x+bx][y+by][level].objects.size(); ++h)
  884. {
  885. SDL_Rect sr;
  886. sr.w = 32;
  887. sr.h = 32;
  888. sr.x = (bx)*32;
  889. sr.y = (by)*32;
  890. validateRectTerr(&sr, extRect);
  891. SDL_Rect pp = ttiles[x+bx][y+by][level].objects[h].second;
  892. pp.h = sr.h;
  893. pp.w = sr.w;
  894. CGHeroInstance * themp = (dynamic_cast<CGHeroInstance*>(ttiles[x+bx][y+by][level].objects[h].first));
  895. if(themp && themp->moveDir && !themp->isStanding && themp->ID!=62) //last condition - this is not prison
  896. {
  897. int imgVal = 8;
  898. SDL_Surface * tb;
  899. if(themp->type==NULL)
  900. continue;
  901. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  902. int gg;
  903. for(gg=0; gg<iv.size(); ++gg)
  904. {
  905. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  906. {
  907. tb = iv[gg+heroAnim%imgVal].bitmap;
  908. break;
  909. }
  910. }
  911. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  912. pp.y+=imgVal*2-32;
  913. sr.y-=16;
  914. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[gg+heroAnim%imgVal+35].bitmap, &pp, su, &sr);
  915. }
  916. else if(themp && themp->moveDir && themp->isStanding && themp->ID!=62) //last condition - this is not prison)
  917. {
  918. int imgVal = 8;
  919. SDL_Surface * tb;
  920. if(themp->type==NULL)
  921. continue;
  922. std::vector<Cimage> & iv = themp->type->heroClass->moveAnim->ourImages;
  923. int gg;
  924. for(gg=0; gg<iv.size(); ++gg)
  925. {
  926. if(iv[gg].groupNumber==getHeroFrameNum(themp->moveDir, !themp->isStanding))
  927. {
  928. tb = iv[gg].bitmap;
  929. break;
  930. }
  931. }
  932. CSDL_Ext::blit8bppAlphaTo24bpp(tb,&pp,su,&sr);
  933. if(themp->pos.x==x+bx && themp->pos.y==y+by)
  934. {
  935. SDL_Rect bufr = sr;
  936. bufr.x-=2*32;
  937. bufr.y-=1*32;
  938. bufr.h = 64;
  939. bufr.w = 96;
  940. if(bufr.x-extRect->x>-64)
  941. SDL_BlitSurface(CGI->heroh->flags4[themp->getOwner()]->ourImages[ getHeroFrameNum(themp->moveDir, !themp->isStanding) *8+(heroAnim/4)%imgVal].bitmap, NULL, su, &bufr);
  942. themp->flagPrinted = true;
  943. }
  944. }
  945. else
  946. {
  947. int imgVal = ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages.size();
  948. //setting appropriate flag color
  949. 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)
  950. 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);
  951. CSDL_Ext::blit8bppAlphaTo24bpp(ttiles[x+bx][y+by][level].objects[h].first->defInfo->handler->ourImages[anim%imgVal].bitmap,&pp,su,&sr);
  952. }
  953. }
  954. }
  955. }
  956. ////objects printed, printing shadow
  957. for (int bx=0; bx<dx; bx++)
  958. {
  959. for (int by=0; by<dy; by++)
  960. {
  961. SDL_Rect sr;
  962. sr.y=by*32;
  963. sr.x=bx*32;
  964. sr.h=sr.w=32;
  965. validateRectTerr(&sr, extRect);
  966. 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])
  967. {
  968. SDL_Surface * hide = getVisBitmap(bx+x, by+y, visibilityMap, level);
  969. CSDL_Ext::blit8bppAlphaTo24bpp(hide, &genRect(sr.h, sr.w, 0, 0), su, &sr);
  970. }
  971. }
  972. }
  973. ////shadow printed
  974. //printing borders
  975. for (int bx=0; bx<dx; bx++)
  976. {
  977. for (int by=0; by<dy; by++)
  978. {
  979. if(bx+x<0 || by+y<0 || bx+x>reader->map.width+(-1) || by+y>reader->map.height+(-1))
  980. {
  981. SDL_Rect sr;
  982. sr.y=by*32;
  983. sr.x=bx*32;
  984. sr.h=sr.w=32;
  985. validateRectTerr(&sr, extRect);
  986. 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);
  987. }
  988. else
  989. {
  990. if(MARK_BLOCKED_POSITIONS && ttiles[x+bx][y+by][level].blocked) //temporary hiding blocked positions
  991. {
  992. SDL_Rect sr;
  993. sr.y=by*32;
  994. sr.x=bx*32;
  995. sr.h=sr.w=32;
  996. validateRectTerr(&sr, extRect);
  997. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  998. for(int f=0; f<ns->w*ns->h*4; ++f)
  999. {
  1000. *((unsigned char*)(ns->pixels) + f) = 128;
  1001. }
  1002. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  1003. SDL_FreeSurface(ns);
  1004. }
  1005. if(MARK_VISITABLE_POSITIONS && ttiles[x+bx][y+by][level].visitable) //temporary hiding visitable positions
  1006. {
  1007. SDL_Rect sr;
  1008. sr.y=by*32;
  1009. sr.x=bx*32;
  1010. sr.h=sr.w=32;
  1011. validateRectTerr(&sr, extRect);
  1012. SDL_Surface * ns = CSDL_Ext::newSurface(32, 32, CSDL_Ext::std32bppSurface);
  1013. for(int f=0; f<ns->w*ns->h*4; ++f)
  1014. {
  1015. *((unsigned char*)(ns->pixels) + f) = 128;
  1016. }
  1017. SDL_BlitSurface(ns,&genRect(sr.h, sr.w, 0, 0),su,&sr);
  1018. SDL_FreeSurface(ns);
  1019. }
  1020. }
  1021. }
  1022. }
  1023. //borders printed
  1024. return su;
  1025. }
  1026. SDL_Surface * CMapHandler::terrBitmap(int x, int y)
  1027. {
  1028. return ttiles[x+Woff][y+Hoff][0].terbitmap[0];
  1029. }
  1030. SDL_Surface * CMapHandler::undTerrBitmap(int x, int y)
  1031. {
  1032. return ttiles[x+Woff][y+Hoff][0].terbitmap[1];
  1033. }
  1034. SDL_Surface * CMapHandler::getVisBitmap(int x, int y, PseudoV< PseudoV< PseudoV<unsigned char> > > & visibilityMap, int lvl)
  1035. {
  1036. 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])
  1037. {
  1038. return fullHide->ourImages[hideBitmap[x][y][lvl]].bitmap; //fully hidden
  1039. }
  1040. 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])
  1041. {
  1042. return partialHide->ourImages[22].bitmap; //visible right bottom corner
  1043. }
  1044. 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])
  1045. {
  1046. return partialHide->ourImages[15].bitmap; //visible right top corner
  1047. }
  1048. 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])
  1049. {
  1050. //return CSDL_Ext::rotate01(partialHide->ourImages[22].bitmap); //visible left bottom corner
  1051. return partialHide->ourImages[34].bitmap; //visible left bottom corner
  1052. }
  1053. 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])
  1054. {
  1055. //return CSDL_Ext::rotate01(partialHide->ourImages[15].bitmap); //visible left top corner
  1056. return partialHide->ourImages[35].bitmap;
  1057. }
  1058. 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])
  1059. {
  1060. //return partialHide->ourImages[rand()%2].bitmap; //visible top
  1061. return partialHide->ourImages[0].bitmap; //visible top
  1062. }
  1063. 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])
  1064. {
  1065. //return partialHide->ourImages[4+rand()%2].bitmap; //visble bottom
  1066. return partialHide->ourImages[4].bitmap; //visble bottom
  1067. }
  1068. 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])
  1069. {
  1070. //return CSDL_Ext::rotate01(partialHide->ourImages[2+rand()%2].bitmap); //visible left
  1071. //return CSDL_Ext::rotate01(partialHide->ourImages[2].bitmap); //visible left
  1072. return partialHide->ourImages[36].bitmap;
  1073. }
  1074. 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])
  1075. {
  1076. //return partialHide->ourImages[2+rand()%2].bitmap; //visible right
  1077. return partialHide->ourImages[2].bitmap; //visible right
  1078. }
  1079. 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])
  1080. {
  1081. //return partialHide->ourImages[12+2*(rand()%2)].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  1082. return partialHide->ourImages[12].bitmap; //visible bottom, right - bottom, right; left top corner hidden
  1083. }
  1084. 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])
  1085. {
  1086. return partialHide->ourImages[13].bitmap; //visible right, right - top; left bottom corner hidden
  1087. }
  1088. 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])
  1089. {
  1090. //return CSDL_Ext::rotate01(partialHide->ourImages[13].bitmap); //visible top, top - left, left; right bottom corner hidden
  1091. return partialHide->ourImages[37].bitmap;
  1092. }
  1093. 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])
  1094. {
  1095. //return CSDL_Ext::rotate01(partialHide->ourImages[12+2*(rand()%2)].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1096. //return CSDL_Ext::rotate01(partialHide->ourImages[12].bitmap); //visible left, left - bottom, bottom; right top corner hidden
  1097. return partialHide->ourImages[38].bitmap;
  1098. }
  1099. 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])
  1100. {
  1101. return partialHide->ourImages[10].bitmap; //visible left, right, bottom and top
  1102. }
  1103. 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])
  1104. {
  1105. return partialHide->ourImages[16].bitmap; //visible right corners
  1106. }
  1107. 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])
  1108. {
  1109. return partialHide->ourImages[18].bitmap; //visible top corners
  1110. }
  1111. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1112. {
  1113. //return CSDL_Ext::rotate01(partialHide->ourImages[16].bitmap); //visible left corners
  1114. return partialHide->ourImages[39].bitmap;
  1115. }
  1116. 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])
  1117. {
  1118. //return CSDL_Ext::hFlip(partialHide->ourImages[18].bitmap); //visible bottom corners
  1119. return partialHide->ourImages[40].bitmap;
  1120. }
  1121. 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])
  1122. {
  1123. return partialHide->ourImages[17].bitmap; //visible right - top and bottom - left corners
  1124. }
  1125. 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])
  1126. {
  1127. //return CSDL_Ext::hFlip(partialHide->ourImages[17].bitmap); //visible top - left and bottom - right corners
  1128. return partialHide->ourImages[41].bitmap;
  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] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1131. {
  1132. return partialHide->ourImages[19].bitmap; //visible corners without left top
  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] && visibilityMap[x+1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1135. {
  1136. return partialHide->ourImages[20].bitmap; //visible corners without left bottom
  1137. }
  1138. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1139. {
  1140. //return CSDL_Ext::rotate01(partialHide->ourImages[20].bitmap); //visible corners without right bottom
  1141. return partialHide->ourImages[42].bitmap;
  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] && !visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1144. {
  1145. //return CSDL_Ext::rotate01(partialHide->ourImages[19].bitmap); //visible corners without right top
  1146. return partialHide->ourImages[43].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] && visibilityMap[x+1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1149. {
  1150. return partialHide->ourImages[21].bitmap; //visible all corners only
  1151. }
  1152. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl])
  1153. {
  1154. return partialHide->ourImages[6].bitmap; //hidden top
  1155. }
  1156. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1157. {
  1158. return partialHide->ourImages[7].bitmap; //hidden right
  1159. }
  1160. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1161. {
  1162. return partialHide->ourImages[8].bitmap; //hidden bottom
  1163. }
  1164. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1165. {
  1166. //return CSDL_Ext::rotate01(partialHide->ourImages[7].bitmap); //hidden left
  1167. return partialHide->ourImages[44].bitmap;
  1168. }
  1169. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl])
  1170. {
  1171. return partialHide->ourImages[9].bitmap; //hidden top and bottom
  1172. }
  1173. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl])
  1174. {
  1175. return partialHide->ourImages[29].bitmap; //hidden left and right
  1176. }
  1177. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1178. {
  1179. return partialHide->ourImages[24].bitmap; //visible top and right bottom corner
  1180. }
  1181. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x-1][y+1][lvl])
  1182. {
  1183. //return CSDL_Ext::rotate01(partialHide->ourImages[24].bitmap); //visible top and left bottom corner
  1184. return partialHide->ourImages[45].bitmap;
  1185. }
  1186. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && visibilityMap[x-1][y+1][lvl])
  1187. {
  1188. return partialHide->ourImages[33].bitmap; //visible top and bottom corners
  1189. }
  1190. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x+1][y+1][lvl] && visibilityMap[x+1][y-1][lvl])
  1191. {
  1192. //return CSDL_Ext::rotate01(partialHide->ourImages[26].bitmap); //visible left and right top corner
  1193. return partialHide->ourImages[46].bitmap;
  1194. }
  1195. if(!visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x+1][y+1][lvl] && !visibilityMap[x+1][y-1][lvl])
  1196. {
  1197. //return CSDL_Ext::rotate01(partialHide->ourImages[25].bitmap); //visible left and right bottom corner
  1198. return partialHide->ourImages[47].bitmap;
  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])
  1201. {
  1202. return partialHide->ourImages[32].bitmap; //visible left and 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])
  1205. {
  1206. //return CSDL_Ext::rotate01(partialHide->ourImages[30].bitmap); //visible bottom and left top corner
  1207. return partialHide->ourImages[48].bitmap;
  1208. }
  1209. if(visibilityMap[x][y+1][lvl] && !visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x+1][y-1][lvl])
  1210. {
  1211. return partialHide->ourImages[30].bitmap; //visible bottom and right top corner
  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])
  1214. {
  1215. return partialHide->ourImages[31].bitmap; //visible bottom and top corners
  1216. }
  1217. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && !visibilityMap[x-1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1218. {
  1219. return partialHide->ourImages[25].bitmap; //visible right and left bottom corner
  1220. }
  1221. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && !visibilityMap[x-1][y+1][lvl])
  1222. {
  1223. return partialHide->ourImages[26].bitmap; //visible right and left top corner
  1224. }
  1225. if(!visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl] && visibilityMap[x-1][y+1][lvl])
  1226. {
  1227. //return CSDL_Ext::rotate01(partialHide->ourImages[32].bitmap); //visible right and left cornres
  1228. return partialHide->ourImages[49].bitmap;
  1229. }
  1230. if(visibilityMap[x][y+1][lvl] && visibilityMap[x+1][y][lvl] && !visibilityMap[x-1][y][lvl] && !visibilityMap[x][y-1][lvl] && visibilityMap[x-1][y-1][lvl])
  1231. {
  1232. return partialHide->ourImages[28].bitmap; //visible bottom, right - bottom, right; left top corner visible
  1233. }
  1234. 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])
  1235. {
  1236. return partialHide->ourImages[27].bitmap; //visible right, right - top; left bottom corner visible
  1237. }
  1238. 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])
  1239. {
  1240. //return CSDL_Ext::rotate01(partialHide->ourImages[27].bitmap); //visible top, top - left, left; right bottom corner visible
  1241. return partialHide->ourImages[50].bitmap;
  1242. }
  1243. 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])
  1244. {
  1245. //return CSDL_Ext::rotate01(partialHide->ourImages[28].bitmap); //visible left, left - bottom, bottom; right top corner visible
  1246. return partialHide->ourImages[51].bitmap;
  1247. }
  1248. //newly added
  1249. 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
  1250. {
  1251. return partialHide->ourImages[0].bitmap;
  1252. }
  1253. 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
  1254. {
  1255. return partialHide->ourImages[1].bitmap;
  1256. }
  1257. 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
  1258. {
  1259. return partialHide->ourImages[4].bitmap;
  1260. }
  1261. 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
  1262. {
  1263. return partialHide->ourImages[5].bitmap;
  1264. }
  1265. 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
  1266. {
  1267. return partialHide->ourImages[36].bitmap;
  1268. }
  1269. 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
  1270. {
  1271. return partialHide->ourImages[36].bitmap;
  1272. }
  1273. 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
  1274. {
  1275. return partialHide->ourImages[2].bitmap;
  1276. }
  1277. 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
  1278. {
  1279. return partialHide->ourImages[3].bitmap;
  1280. }
  1281. return fullHide->ourImages[0].bitmap; //this case should never happen, but it is better to hide too much than reveal it....
  1282. }
  1283. int CMapHandler::getCost(int3 &a, int3 &b, const CGHeroInstance *hero)
  1284. {
  1285. int ret=-1;
  1286. if(a.x>=CGI->mh->reader->map.width && a.y>=CGI->mh->reader->map.height)
  1287. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->reader->map.width-1][CGI->mh->reader->map.width-1][a.z].malle];
  1288. else if(a.x>=CGI->mh->reader->map.width && a.y<CGI->mh->reader->map.height)
  1289. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[CGI->mh->reader->map.width-1][a.y][a.z].malle];
  1290. else if(a.x<CGI->mh->reader->map.width && a.y>=CGI->mh->reader->map.height)
  1291. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][CGI->mh->reader->map.width-1][a.z].malle];
  1292. else
  1293. ret = hero->type->heroClass->terrCosts[CGI->mh->ttiles[a.x][a.y][a.z].malle];
  1294. if(!(a.x==b.x || a.y==b.y))
  1295. ret*=1.41421;
  1296. //TODO: use hero's pathfinding skill during calculating cost
  1297. return ret;
  1298. }
  1299. std::vector < std::string > CMapHandler::getObjDescriptions(int3 pos)
  1300. {
  1301. std::vector < std::pair<CGObjectInstance*,SDL_Rect > > objs = ttiles[pos.x][pos.y][pos.z].objects;
  1302. std::vector<std::string> ret;
  1303. for(int g=0; g<objs.size(); ++g)
  1304. {
  1305. 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 &&
  1306. (((objs[g].first->defInfo->blockMap[5-(objs[g].first->pos.y-pos.y)])>>((objs[g].first->pos.x-pos.x)))&1)==0
  1307. ) //checking position blocking
  1308. {
  1309. //unsigned char * blm = objs[g].first->defInfo->blockMap;
  1310. if (objs[g].first->state)
  1311. ret.push_back(objs[g].first->state->hoverText(objs[g].first));
  1312. else
  1313. ret.push_back(CGI->objh->objects[objs[g].first->ID].name);
  1314. }
  1315. }
  1316. return ret;
  1317. }
  1318. std::vector < CGObjectInstance * > CMapHandler::getVisitableObjs(int3 pos)
  1319. {
  1320. std::vector < CGObjectInstance * > ret;
  1321. for(int h=0; h<ttiles[pos.x][pos.y][pos.z].objects.size(); ++h)
  1322. {
  1323. CGObjectInstance * curi = ttiles[pos.x][pos.y][pos.z].objects[h].first;
  1324. if(curi->visitableAt(- curi->pos.x + pos.x + curi->getWidth() - 1, -curi->pos.y + pos.y + curi->getHeight() - 1))
  1325. ret.push_back(curi);
  1326. }
  1327. return ret;
  1328. }
  1329. CGObjectInstance * CMapHandler::createObject(int id, int subid, int3 pos, int owner)
  1330. {
  1331. CGObjectInstance * nobj;
  1332. switch(id)
  1333. {
  1334. case 34: //hero
  1335. {
  1336. CGHeroInstance * nobj;
  1337. nobj = new CGHeroInstance();
  1338. nobj->pos = pos;
  1339. nobj->tempOwner = owner;
  1340. nobj->defInfo = new CGDefInfo();
  1341. nobj->defInfo->id = 34;
  1342. nobj->defInfo->subid = subid;
  1343. nobj->defInfo->printPriority = 0;
  1344. nobj->type = CGI->heroh->heroes[subid];
  1345. for(int i=0;i<6;i++)
  1346. {
  1347. nobj->defInfo->blockMap[i]=255;
  1348. nobj->defInfo->visitMap[i]=0;
  1349. }
  1350. nobj->ID = id;
  1351. nobj->subID = subid;
  1352. nobj->defInfo->handler=NULL;
  1353. nobj->defInfo->blockMap[5] = 253;
  1354. nobj->defInfo->visitMap[5] = 2;
  1355. nobj->artifWorn.resize(20);
  1356. nobj->artifacts.resize(20);
  1357. nobj->artifWorn[16] = &CGI->arth->artifacts[3];
  1358. nobj->primSkills.resize(4);
  1359. nobj->primSkills[0] = nobj->type->heroClass->initialAttack;
  1360. nobj->primSkills[1] = nobj->type->heroClass->initialDefence;
  1361. nobj->primSkills[2] = nobj->type->heroClass->initialPower;
  1362. nobj->primSkills[3] = nobj->type->heroClass->initialKnowledge;
  1363. nobj->mana = 10 * nobj->primSkills[3];
  1364. return nobj;
  1365. }
  1366. case 98: //town
  1367. nobj = new CGTownInstance;
  1368. break;
  1369. default: //rest of objects
  1370. nobj = new CGObjectInstance;
  1371. nobj->defInfo = CGI->dobjinfo->gobjs[id][subid];
  1372. break;
  1373. }
  1374. nobj->ID = id;
  1375. nobj->subID = subid;
  1376. if(!nobj->defInfo)
  1377. std::cout <<"No def declaration for " <<id <<" "<<subid<<std::endl;
  1378. nobj->pos = pos;
  1379. //nobj->state = NULL;//new CLuaObjectScript();
  1380. nobj->tempOwner = owner;
  1381. nobj->info = NULL;
  1382. nobj->defInfo->id = id;
  1383. nobj->defInfo->subid = subid;
  1384. //assigning defhandler
  1385. if(nobj->ID==34 || nobj->ID==98)
  1386. return nobj;
  1387. nobj->defInfo = CGI->dobjinfo->gobjs[id][subid];
  1388. if(!nobj->defInfo->handler)
  1389. nobj->defInfo->handler = CGI->spriteh->giveDef(nobj->defInfo->name);
  1390. return nobj;
  1391. }
  1392. std::string CMapHandler::getDefName(int id, int subid)
  1393. {
  1394. CGDefInfo* temp = CGI->dobjinfo->gobjs[id][subid];
  1395. if(temp)
  1396. return temp->name;
  1397. throw new std::exception("Def not found.");
  1398. }
  1399. bool CMapHandler::printObject(CGObjectInstance *obj)
  1400. {
  1401. CDefHandler * curd = obj->defInfo->handler;
  1402. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1403. {
  1404. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1405. {
  1406. SDL_Rect cr;
  1407. cr.w = 32;
  1408. cr.h = 32;
  1409. cr.x = fx*32;
  1410. cr.y = fy*32;
  1411. std::pair<CGObjectInstance*,SDL_Rect> toAdd = std::make_pair(obj, cr);
  1412. 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)
  1413. {
  1414. TerrainTile2 & curt =
  1415. ttiles
  1416. [obj->pos.x + fx - curd->ourImages[0].bitmap->w/32]
  1417. [obj->pos.y + fy - curd->ourImages[0].bitmap->h/32]
  1418. [obj->pos.z];
  1419. 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);
  1420. }
  1421. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1422. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1423. return true;
  1424. }
  1425. bool CMapHandler::hideObject(CGObjectInstance *obj)
  1426. {
  1427. CDefHandler * curd = obj->defInfo->handler;
  1428. for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1429. {
  1430. for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1431. {
  1432. 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)
  1433. {
  1434. 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;
  1435. for(int dd=0; dd<ctile.size(); ++dd)
  1436. {
  1437. if(ctile[dd].first->id==obj->id)
  1438. ctile.erase(ctile.begin() + dd);
  1439. }
  1440. }
  1441. } // for(int fy=0; fy<curd->ourImages[0].bitmap->h/32; ++fy)
  1442. } //for(int fx=0; fx<curd->ourImages[0].bitmap->w/32; ++fx)
  1443. return true;
  1444. }
  1445. std::string CMapHandler::getRandomizedDefName(CGDefInfo *di, CGObjectInstance * obj)
  1446. {
  1447. return std::string();
  1448. }
  1449. bool CMapHandler::removeObject(CGObjectInstance *obj)
  1450. {
  1451. hideObject(obj);
  1452. std::vector<CGObjectInstance *>::iterator db = std::find(CGI->objh->objInstances.begin(), CGI->objh->objInstances.end(), obj);
  1453. recalculateHideVisPosUnderObj(*db);
  1454. delete *db;
  1455. CGI->objh->objInstances.erase(db);
  1456. return true;
  1457. }
  1458. bool CMapHandler::recalculateHideVisPos(int3 &pos)
  1459. {
  1460. ttiles[pos.x][pos.y][pos.z].visitable = false;
  1461. ttiles[pos.x][pos.y][pos.z].blocked = false;
  1462. for(int i=0; i<ttiles[pos.x][pos.y][pos.z].objects.size(); ++i)
  1463. {
  1464. CDefHandler * curd = ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->handler;
  1465. for(int fx=0; fx<8; ++fx)
  1466. {
  1467. for(int fy=0; fy<6; ++fy)
  1468. {
  1469. int xVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.x + fx - 7;
  1470. int yVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.y + fy - 5;
  1471. int zVal = ttiles[pos.x][pos.y][pos.z].objects[i].first->pos.z;
  1472. if(xVal>=0 && xVal<ttiles.size()-Woff && yVal>=0 && yVal<ttiles[0].size()-Hoff)
  1473. {
  1474. TerrainTile2 & curt = ttiles[xVal][yVal][zVal];
  1475. if(((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->visitMap[fy] >> (7 - fx)) & 1))
  1476. curt.visitable = true;
  1477. if(!((ttiles[pos.x][pos.y][pos.z].objects[i].first->defInfo->blockMap[fy] >> (7 - fx)) & 1))
  1478. curt.blocked = true;
  1479. }
  1480. }
  1481. }
  1482. }
  1483. return true;
  1484. }
  1485. bool CMapHandler::recalculateHideVisPosUnderObj(CGObjectInstance *obj, bool withBorder)
  1486. {
  1487. if(withBorder)
  1488. {
  1489. for(int fx=-1; fx<=obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1490. {
  1491. for(int fy=-1; fy<=obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1492. {
  1493. 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)
  1494. {
  1495. 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));
  1496. }
  1497. }
  1498. }
  1499. }
  1500. else
  1501. {
  1502. for(int fx=0; fx<obj->defInfo->handler->ourImages[0].bitmap->w/32; ++fx)
  1503. {
  1504. for(int fy=0; fy<obj->defInfo->handler->ourImages[0].bitmap->h/32; ++fy)
  1505. {
  1506. 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)
  1507. {
  1508. 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));
  1509. }
  1510. }
  1511. }
  1512. }
  1513. return true;
  1514. }
  1515. unsigned char CMapHandler::getHeroFrameNum(const unsigned char &dir, const bool &isMoving) const
  1516. {
  1517. if(isMoving)
  1518. {
  1519. switch(dir)
  1520. {
  1521. case 1:
  1522. return 10;
  1523. case 2:
  1524. return 5;
  1525. case 3:
  1526. return 6;
  1527. case 4:
  1528. return 7;
  1529. case 5:
  1530. return 8;
  1531. case 6:
  1532. return 9;
  1533. case 7:
  1534. return 12;
  1535. case 8:
  1536. return 11;
  1537. default:
  1538. return -1; //should never happen
  1539. }
  1540. }
  1541. else //if(isMoving)
  1542. {
  1543. switch(dir)
  1544. {
  1545. case 1:
  1546. return 13;
  1547. case 2:
  1548. return 0;
  1549. case 3:
  1550. return 1;
  1551. case 4:
  1552. return 2;
  1553. case 5:
  1554. return 3;
  1555. case 6:
  1556. return 4;
  1557. case 7:
  1558. return 15;
  1559. case 8:
  1560. return 14;
  1561. default:
  1562. return -1; //should never happen
  1563. }
  1564. }
  1565. }
  1566. void CMapHandler::validateRectTerr(SDL_Rect * val, const SDL_Rect * ext)
  1567. {
  1568. if(ext)
  1569. {
  1570. if(val->x<0)
  1571. {
  1572. val->w += val->x;
  1573. val->x = ext->x;
  1574. }
  1575. else
  1576. {
  1577. val->x += ext->x;
  1578. }
  1579. if(val->y<0)
  1580. {
  1581. val->h += val->y;
  1582. val->y = ext->y;
  1583. }
  1584. else
  1585. {
  1586. val->y += ext->y;
  1587. }
  1588. if(val->x+val->w > ext->x+ext->w)
  1589. {
  1590. val->w = ext->x+ext->w-val->x;
  1591. }
  1592. if(val->y+val->h > ext->y+ext->h)
  1593. {
  1594. val->h = ext->y+ext->h-val->y;
  1595. }
  1596. //for sign problems
  1597. if(val->h > 20000 || val->w > 20000)
  1598. {
  1599. val->h = val->w = 0;
  1600. }
  1601. }
  1602. }
  1603. unsigned char CMapHandler::getDir(const int3 &a, const int3 &b)
  1604. {
  1605. if(a.z!=b.z)
  1606. return -1; //error!
  1607. if(a.x==b.x+1 && a.y==b.y+1) //lt
  1608. {
  1609. return 0;
  1610. }
  1611. else if(a.x==b.x && a.y==b.y+1) //t
  1612. {
  1613. return 1;
  1614. }
  1615. else if(a.x==b.x-1 && a.y==b.y+1) //rt
  1616. {
  1617. return 2;
  1618. }
  1619. else if(a.x==b.x-1 && a.y==b.y) //r
  1620. {
  1621. return 3;
  1622. }
  1623. else if(a.x==b.x-1 && a.y==b.y-1) //rb
  1624. {
  1625. return 4;
  1626. }
  1627. else if(a.x==b.x && a.y==b.y-1) //b
  1628. {
  1629. return 5;
  1630. }
  1631. else if(a.x==b.x+1 && a.y==b.y-1) //lb
  1632. {
  1633. return 6;
  1634. }
  1635. else if(a.x==b.x+1 && a.y==b.y) //l
  1636. {
  1637. return 7;
  1638. }
  1639. return -2; //shouldn't happen
  1640. }