CAmbarCendamo.cpp 13 KB

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