mapHandler.cpp 71 KB

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