mapHandler.cpp 66 KB

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