CAmbarCendamo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include "stdafx.h"
  2. #include "CAmbarCendamo.h"
  3. #include "CSemiDefHandler.h"
  4. #include "CGameInfo.h"
  5. #include <set>
  6. unsigned int intPow(unsigned int a, unsigned int b)
  7. {
  8. unsigned int ret=1;
  9. for(int i=0; i<b; ++i)
  10. ret*=a;
  11. return ret;
  12. }
  13. CAmbarCendamo::CAmbarCendamo (const char * tie)
  14. {
  15. is = new std::ifstream();
  16. is -> open(tie,std::ios::binary);
  17. is->seekg(0,std::ios::end); // na koniec
  18. andame = is->tellg(); // read length
  19. is->seekg(0,std::ios::beg); // wracamy na poczatek
  20. bufor = new unsigned char[andame]; // allocate memory
  21. is->read((char*)bufor, andame); // read map file to buffer
  22. delete is;
  23. }
  24. CAmbarCendamo::~CAmbarCendamo ()
  25. {// free memory
  26. for (int ii=0;ii<map.width;ii++)
  27. delete map.terrain[ii] ;
  28. delete map.terrain;
  29. delete bufor;
  30. }
  31. void CAmbarCendamo::teceDef()
  32. {
  33. for (int i=0; i<map.defy.size(); i++)
  34. {
  35. std::ofstream * of = new std::ofstream(map.defy[i].name.c_str());
  36. for (int j=0;j<46;j++)
  37. {
  38. (*of) << map.defy[i].bytes[j]<<std::endl;
  39. }
  40. delete of;
  41. }
  42. }
  43. void CAmbarCendamo::deh3m()
  44. {
  45. THC timeHandler th;
  46. map.version = (Eformat)bufor[0]; //wersja mapy
  47. map.areAnyPLayers = bufor[4];
  48. map.height = map.width = bufor[5]; // wymiary mapy
  49. map.twoLevel = bufor[9]; //czy sa lochy
  50. map.terrain = new TerrainTile*[map.width]; // allocate memory
  51. for (int ii=0;ii<map.width;ii++)
  52. map.terrain[ii] = new TerrainTile[map.height]; // allocate memory
  53. if (map.twoLevel)
  54. {
  55. map.undergroungTerrain = new TerrainTile*[map.width]; // allocate memory
  56. for (int ii=0;ii<map.width;ii++)
  57. map.undergroungTerrain[ii] = new TerrainTile[map.height]; // allocate memory
  58. }
  59. int length = bufor[10]; //name length
  60. int i=14, pom;
  61. while (i-14<length) //read name
  62. map.name+=bufor[i++];
  63. length = bufor[i] + bufor[i+1]*256; //description length
  64. i+=4;
  65. for (pom=0;pom<length;pom++)
  66. map.description+=bufor[i++];
  67. map.difficulty = bufor[i++]; // reading map difficulty
  68. map.levelLimit = bufor[i++]; // hero level limit
  69. for (pom=0;pom<8;pom++)
  70. {
  71. map.players[pom].canHumanPlay = bufor[i++];
  72. map.players[pom].canComputerPlay = bufor[i++];
  73. if ((!(map.players[pom].canHumanPlay || map.players[pom].canComputerPlay)) || (!map.areAnyPLayers))
  74. {
  75. i+=13;
  76. continue;
  77. }
  78. map.players[pom].AITactic = bufor[i++];
  79. if (bufor[i++])
  80. {
  81. map.players[pom].allowedFactions = 0;
  82. map.players[pom].allowedFactions += bufor[i++];
  83. map.players[pom].allowedFactions += (bufor[i++])*256;
  84. }
  85. else
  86. {
  87. map.players[pom].allowedFactions = 511;
  88. i+=2;
  89. }
  90. map.players[pom].isFactionRandom = bufor[i++];
  91. map.players[pom].hasMainTown = bufor[i++];
  92. if (map.players[pom].hasMainTown)
  93. {
  94. map.players[pom].generateHeroAtMainTown = bufor[i++];
  95. i++; //unknown byte
  96. map.players[pom].posOfMainTown.x = bufor[i++];
  97. map.players[pom].posOfMainTown.y = bufor[i++];
  98. map.players[pom].posOfMainTown.z = bufor[i++];
  99. }
  100. i++; //unknown byte
  101. int unknown = bufor[i++];
  102. if (unknown == 255)
  103. {
  104. map.players[pom].mainHeroPortrait = 255;
  105. i+=5;
  106. continue;
  107. }
  108. map.players[pom].mainHeroPortrait = bufor[i++];
  109. int nameLength = bufor[i++];
  110. i+=3;
  111. for (int pp=0;pp<nameLength;pp++)
  112. map.players[pom].mainHeroName+=bufor[i++];
  113. i++; ////unknown byte
  114. int heroCount = bufor[i++];
  115. i+=3;
  116. for (int pp=0;pp<heroCount;pp++)
  117. {
  118. SheroName vv;
  119. vv.heroID=bufor[i++];
  120. int hnl = bufor[i++];
  121. i+=3;
  122. for (int zz=0;zz<hnl;zz++)
  123. {
  124. vv.heroName+=bufor[i++];
  125. }
  126. map.players[pom].heroesNames.push_back(vv);
  127. }
  128. }
  129. map.victoryCondition = (EvictoryConditions)bufor[i++];
  130. if (map.victoryCondition != winStandard) //specific victory conditions
  131. {
  132. int nr;
  133. switch (map.victoryCondition) //read victory conditions
  134. {
  135. case artifact:
  136. {
  137. map.vicConDetails = new VicCon0();
  138. ((VicCon0*)map.vicConDetails)->ArtifactID = bufor[i+2];
  139. nr=2;
  140. break;
  141. }
  142. case gatherTroop:
  143. {
  144. map.vicConDetails = new VicCon1();
  145. int temp1 = bufor[i+2];
  146. int temp2 = bufor[i+3];
  147. ((VicCon1*)map.vicConDetails)->monsterID = bufor[i+2];
  148. ((VicCon1*)map.vicConDetails)->neededQuantity=readNormalNr(i+4);
  149. nr=6;
  150. break;
  151. }
  152. case gatherResource:
  153. {
  154. map.vicConDetails = new VicCon2();
  155. ((VicCon2*)map.vicConDetails)->resourceID = bufor[i+2];
  156. ((VicCon2*)map.vicConDetails)->neededQuantity=readNormalNr(i+3);
  157. nr=5;
  158. break;
  159. }
  160. case buildCity:
  161. {
  162. map.vicConDetails = new VicCon3();
  163. ((VicCon3*)map.vicConDetails)->posOfCity.x = bufor[i+2];
  164. ((VicCon3*)map.vicConDetails)->posOfCity.y = bufor[i+3];
  165. ((VicCon3*)map.vicConDetails)->posOfCity.z = bufor[i+4];
  166. ((VicCon3*)map.vicConDetails)->councilNeededLevel = bufor[i+5];
  167. ((VicCon3*)map.vicConDetails)->fortNeededLevel = bufor[i+6];
  168. nr=5;
  169. break;
  170. }
  171. case buildGrail:
  172. {
  173. map.vicConDetails = new VicCon4();
  174. if (bufor[i+4]>2)
  175. ((VicCon4*)map.vicConDetails)->anyLocation = true;
  176. else
  177. {
  178. ((VicCon4*)map.vicConDetails)->whereBuildGrail.x = bufor[i+2];
  179. ((VicCon4*)map.vicConDetails)->whereBuildGrail.y = bufor[i+3];
  180. ((VicCon4*)map.vicConDetails)->whereBuildGrail.z = bufor[i+4];
  181. }
  182. nr=3;
  183. break;
  184. }
  185. case beatHero:
  186. {
  187. map.vicConDetails = new VicCon5();
  188. ((VicCon5*)map.vicConDetails)->locationOfHero.x = bufor[i+2];
  189. ((VicCon5*)map.vicConDetails)->locationOfHero.y = bufor[i+3];
  190. ((VicCon5*)map.vicConDetails)->locationOfHero.z = bufor[i+4];
  191. nr=3;
  192. break;
  193. }
  194. case captureCity:
  195. {
  196. map.vicConDetails = new VicCon6();
  197. ((VicCon6*)map.vicConDetails)->locationOfTown.x = bufor[i+2];
  198. ((VicCon6*)map.vicConDetails)->locationOfTown.y = bufor[i+3];
  199. ((VicCon6*)map.vicConDetails)->locationOfTown.z = bufor[i+4];
  200. nr=3;
  201. break;
  202. }
  203. case beatMonster:
  204. {
  205. map.vicConDetails = new VicCon7();
  206. ((VicCon7*)map.vicConDetails)->locationOfMonster.x = bufor[i+2];
  207. ((VicCon7*)map.vicConDetails)->locationOfMonster.y = bufor[i+3];
  208. ((VicCon7*)map.vicConDetails)->locationOfMonster.z = bufor[i+4];
  209. nr=3;
  210. break;
  211. }
  212. case takeDwellings:
  213. {
  214. map.vicConDetails = new CspecificVictoryConidtions();
  215. nr=3;
  216. break;
  217. }
  218. case takeMines:
  219. {
  220. map.vicConDetails = new CspecificVictoryConidtions();
  221. nr=3;
  222. break;
  223. }
  224. case transportItem:
  225. {
  226. map.vicConDetails = new VicCona();
  227. ((VicCona*)map.vicConDetails)->artifactID = bufor[i+2];
  228. ((VicCona*)map.vicConDetails)->destinationPlace.x = bufor[i+3];
  229. ((VicCona*)map.vicConDetails)->destinationPlace.y = bufor[i+4];
  230. ((VicCona*)map.vicConDetails)->destinationPlace.z = bufor[i+5];
  231. nr=3;
  232. break;
  233. }
  234. }
  235. map.vicConDetails->allowNormalVictory = bufor[i++];
  236. map.vicConDetails->appliesToAI = bufor[i++];
  237. i+=nr;
  238. }
  239. map.lossCondition.typeOfLossCon = (ElossCon)bufor[i++];
  240. switch (map.lossCondition.typeOfLossCon) //read loss conditions
  241. {
  242. case lossCastle:
  243. {
  244. map.lossCondition.castlePos.x=bufor[i++];
  245. map.lossCondition.castlePos.y=bufor[i++];
  246. map.lossCondition.castlePos.z=bufor[i++];
  247. }
  248. case lossHero:
  249. {
  250. map.lossCondition.heroPos.x=bufor[i++];
  251. map.lossCondition.heroPos.y=bufor[i++];
  252. map.lossCondition.heroPos.z=bufor[i++];
  253. }
  254. case timeExpires:
  255. {
  256. map.lossCondition.timeLimit = readNormalNr(i++,2);
  257. i++;
  258. }
  259. }
  260. map.howManyTeams=bufor[i++]; //read number of teams
  261. if(map.howManyTeams>0) //read team numbers
  262. {
  263. for(int rr=0; rr<8; ++rr)
  264. {
  265. map.players[rr].team=bufor[i++];
  266. }
  267. }
  268. //reading allowed heroes (20 bytes)
  269. int ist=i; //starting i for loop
  270. for(i; i<ist+20; ++i)
  271. {
  272. unsigned char c = bufor[i];
  273. for(int yy=0; yy<8; ++yy)
  274. {
  275. if((i-ist)*8+yy < CGameInfo::mainObj->heroh->heroes.size())
  276. {
  277. if(c == (c|((unsigned char)intPow(2, yy))))
  278. CGameInfo::mainObj->heroh->heroes[(i-ist)*8+yy].isAllowed = true;
  279. else
  280. CGameInfo::mainObj->heroh->heroes[(i-ist)*8+yy].isAllowed = false;
  281. }
  282. }
  283. }
  284. //allowed heroes have been read
  285. i+=36;
  286. //reading allowed artifacts //18 bytes
  287. ist=i; //starting i for loop
  288. for(i; i<ist+18; ++i)
  289. {
  290. unsigned char c = bufor[i];
  291. for(int yy=0; yy<8; ++yy)
  292. {
  293. if((i-ist)*8+yy < CGameInfo::mainObj->arth->artifacts.size())
  294. {
  295. if(c != (c|((unsigned char)intPow(2, yy))))
  296. CGameInfo::mainObj->arth->artifacts[(i-ist)*8+yy].isAllowed = true;
  297. else
  298. CGameInfo::mainObj->arth->artifacts[(i-ist)*8+yy].isAllowed = false;
  299. }
  300. }
  301. }
  302. //allowed artifacts have been read
  303. //reading allowed spells (9 bytes)
  304. ist=i; //starting i for loop
  305. for(i; i<ist+9; ++i)
  306. {
  307. unsigned char c = bufor[i];
  308. for(int yy=0; yy<8; ++yy)
  309. {
  310. if((i-ist)*8+yy < CGameInfo::mainObj->spellh->spells.size())
  311. {
  312. if(c != (c|((unsigned char)intPow(2, yy))))
  313. CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy].isAllowed = true;
  314. else
  315. CGameInfo::mainObj->spellh->spells[(i-ist)*8+yy].isAllowed = false;
  316. }
  317. }
  318. }
  319. //allowed spells have been read
  320. //allowed hero's abilities (4 bytes)
  321. ist=i; //starting i for loop
  322. for(i; i<ist+4; ++i)
  323. {
  324. unsigned char c = bufor[i];
  325. for(int yy=0; yy<8; ++yy)
  326. {
  327. if((i-ist)*8+yy < CGameInfo::mainObj->abilh->abilities.size())
  328. {
  329. if(c != (c|((unsigned char)intPow(2, yy))))
  330. CGameInfo::mainObj->abilh->abilities[(i-ist)*8+yy].isAllowed = true;
  331. else
  332. CGameInfo::mainObj->abilh->abilities[(i-ist)*8+yy].isAllowed = false;
  333. }
  334. }
  335. }
  336. //allowed hero's abilities have been read
  337. THC std::cout<<"Wczytywanie naglowka: "<<th.getDif()<<std::endl;
  338. int rumNr = readNormalNr(i,4);i+=4;
  339. for (int it=0;it<rumNr;it++)
  340. {
  341. Rumor ourRumor;
  342. int nameL = readNormalNr(i,4);i+=4; //read length of name of rumor
  343. for (int zz=0; zz<nameL; zz++)
  344. ourRumor.name+=bufor[i++];
  345. nameL = readNormalNr(i,4);i+=4; //read length of rumor
  346. for (int zz=0; zz<nameL; zz++)
  347. ourRumor.text+=bufor[i++];
  348. map.rumors.push_back(ourRumor); //add to our list
  349. }
  350. THC std::cout<<"Wczytywanie plotek: "<<th.getDif()<<std::endl;
  351. i+=156;
  352. for (int c=0; c<map.width; c++) // reading terrain
  353. {
  354. for (int z=0; z<map.height; z++)
  355. {
  356. map.terrain[z][c].tertype = (EterrainType)(bufor[i++]);
  357. map.terrain[z][c].terview = bufor[i++];
  358. map.terrain[z][c].nuine = (Eriver)bufor[i++];
  359. map.terrain[z][c].rivDir = bufor[i++];
  360. map.terrain[z][c].malle = (Eroad)bufor[i++];
  361. map.terrain[z][c].roadDir = bufor[i++];
  362. map.terrain[z][c].siodmyTajemniczyBajt = bufor[i++];
  363. }
  364. }
  365. if (map.twoLevel) // read underground terrain
  366. {
  367. for (int c=0; c<map.width; c++) // reading terrain
  368. {
  369. for (int z=0; z<map.height; z++)
  370. {
  371. map.undergroungTerrain[z][c].tertype = (EterrainType)(bufor[i++]);
  372. map.undergroungTerrain[z][c].terview = bufor[i++];
  373. map.undergroungTerrain[z][c].nuine = (Eriver)bufor[i++];
  374. map.undergroungTerrain[z][c].rivDir = bufor[i++];
  375. map.undergroungTerrain[z][c].malle = (Eroad)bufor[i++];
  376. map.undergroungTerrain[z][c].roadDir = bufor[i++];
  377. map.undergroungTerrain[z][c].siodmyTajemniczyBajt = bufor[i++];
  378. }
  379. }
  380. }
  381. THC std::cout<<"Wczytywanie terenu: "<<th.getDif()<<std::endl;
  382. int defAmount = bufor[i]; // liczba defow
  383. i+=4;
  384. for (int idd = 0 ; idd<defAmount; idd++) // reading defs
  385. {
  386. int nameLength = readNormalNr(i,4);i+=4;
  387. DefInfo vinya; // info about new def
  388. for (int cd=0;cd<nameLength;cd++)
  389. {
  390. vinya.name += bufor[i++];
  391. }
  392. for (int v=0; v<42; v++) // read info
  393. {
  394. vinya.bytes[v] = bufor[i++];
  395. }
  396. map.defy.push_back(vinya); // add this def to the vector
  397. //teceDef();
  398. }
  399. THC std::cout<<"Wczytywanie defow: "<<th.getDif()<<std::endl;
  400. //todo: read events
  401. }
  402. int CAmbarCendamo::readNormalNr (int pos, int bytCon)
  403. {
  404. int ret=0;
  405. int amp=1;
  406. for (int i=0; i<bytCon; i++)
  407. {
  408. ret+=bufor[pos+i]*amp;
  409. amp*=256;
  410. }
  411. return ret;
  412. }
  413. void CAmbarCendamo::loadDefs()
  414. {
  415. std::set<int> loadedTypes;
  416. for (int i=0; i<map.width; i++)
  417. {
  418. for (int j=0; j<map.width; j++)
  419. {
  420. if (loadedTypes.find(map.terrain[i][j].tertype)==loadedTypes.end())
  421. {
  422. CSemiDefHandler *sdh = new CSemiDefHandler();
  423. sdh->openDef((sdh->nameFromType(map.terrain[i][j].tertype)).c_str(),"H3sprite.lod");
  424. loadedTypes.insert(map.terrain[i][j].tertype);
  425. defs.push_back(sdh);
  426. }
  427. if (loadedTypes.find(map.undergroungTerrain[i][j].tertype)==loadedTypes.end())
  428. {
  429. CSemiDefHandler *sdh = new CSemiDefHandler();
  430. sdh->openDef((sdh->nameFromType(map.undergroungTerrain[i][j].tertype)).c_str(),"H3sprite.lod");
  431. loadedTypes.insert(map.undergroungTerrain[i][j].tertype);
  432. defs.push_back(sdh);
  433. }
  434. }
  435. }
  436. };