map.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "stdafx.h"
  2. #include "map.h"
  3. int readNormalNr (unsigned char * bufor, int pos, int bytCon = 4)
  4. {
  5. int ret=0;
  6. int amp=1;
  7. for (int i=0; i<bytCon; i++)
  8. {
  9. ret+=bufor[pos+i]*amp;
  10. amp*=256;
  11. }
  12. return ret;
  13. }
  14. bool DefInfo::isVisitable()
  15. {
  16. for (int i=6; i<12; i++)
  17. {
  18. if (bytes[i])
  19. return true;
  20. }
  21. return false;
  22. }
  23. CMapHeader::CMapHeader(unsigned char *map)
  24. {
  25. this->version = (Eformat)map[0]; //wersja mapy
  26. this->areAnyPLayers = map[4]; //seems to be invalid
  27. this->height = this->width = map[5]; // wymiary mapy
  28. this->twoLevel = map[9]; //czy sa lochy
  29. int length = map[10]; //name length
  30. int i=14, pom;
  31. while (i-14<length) //read name
  32. this->name+=map[i++];
  33. length = map[i] + map[i+1]*256; //description length
  34. i+=4;
  35. for (pom=0;pom<length;pom++)
  36. this->description+=map[i++];
  37. this->difficulty = map[i++]; // reading map difficulty
  38. if(version!=Eformat::RoE)
  39. {
  40. this->levelLimit = map[i++]; // hero level limit
  41. }
  42. else
  43. {
  44. levelLimit = 0;
  45. }
  46. for (pom=0;pom<8;pom++)
  47. {
  48. this->players[pom].canHumanPlay = map[i++];
  49. this->players[pom].canComputerPlay = map[i++];
  50. if ((!(this->players[pom].canHumanPlay || this->players[pom].canComputerPlay)))
  51. {
  52. switch(version)
  53. {
  54. case Eformat::SoD: case Eformat::WoG:
  55. i+=13;
  56. break;
  57. case Eformat::AB:
  58. i+=12;
  59. break;
  60. case Eformat::RoE:
  61. i+=6;
  62. break;
  63. }
  64. continue;
  65. }
  66. this->players[pom].AITactic = map[i++];
  67. if(version == Eformat::SoD || version == Eformat::WoG)
  68. i++;
  69. this->players[pom].allowedFactions = 0;
  70. this->players[pom].allowedFactions += map[i++];
  71. if(version != Eformat::RoE)
  72. this->players[pom].allowedFactions += (map[i++])*256;
  73. this->players[pom].isFactionRandom = map[i++];
  74. this->players[pom].hasMainTown = map[i++];
  75. if (this->players[pom].hasMainTown)
  76. {
  77. if(version != Eformat::RoE)
  78. {
  79. this->players[pom].generateHeroAtMainTown = map[i++];
  80. this->players[pom].generateHero = map[i++];
  81. }
  82. this->players[pom].posOfMainTown.x = map[i++];
  83. this->players[pom].posOfMainTown.y = map[i++];
  84. this->players[pom].posOfMainTown.z = map[i++];
  85. }
  86. i++; //unknown byte
  87. int customPortrait = map[i++];
  88. if (customPortrait != 255)
  89. {
  90. players[pom].mainHeroPortrait = map[i++];
  91. int nameLength = map[i++];
  92. i+=3;
  93. for (int pp=0;pp<nameLength;pp++)
  94. players[pom].mainHeroName+=map[i++];
  95. }
  96. if(version!=Eformat::RoE)
  97. {
  98. i++; ////heroes placeholders //domostwa
  99. int heroCount = map[i++];
  100. i+=3;
  101. for (int pp=0;pp<heroCount;pp++)
  102. {
  103. SheroName vv;
  104. vv.heroID=map[i++];
  105. int hnl = map[i++];
  106. i+=3;
  107. for (int zz=0;zz<hnl;zz++)
  108. {
  109. vv.heroName+=map[i++];
  110. }
  111. this->players[pom].heroesNames.push_back(vv);
  112. }
  113. }
  114. }
  115. this->victoryCondition = (EvictoryConditions)map[i++];
  116. if (this->victoryCondition != winStandard) //specific victory conditions
  117. {
  118. int nr;
  119. switch (this->victoryCondition) //read victory conditions
  120. {
  121. case artifact:
  122. {
  123. this->vicConDetails = new VicCon0();
  124. ((VicCon0*)this->vicConDetails)->ArtifactID = map[i+2];
  125. nr=(version==RoE ? 1 : 2);
  126. break;
  127. }
  128. case gatherTroop:
  129. {
  130. this->vicConDetails = new VicCon1();
  131. int temp1 = map[i+2];
  132. int temp2 = map[i+3];
  133. ((VicCon1*)this->vicConDetails)->monsterID = map[i+2];
  134. ((VicCon1*)this->vicConDetails)->neededQuantity=readNormalNr(map, i+(version==RoE ? 3 : 4));
  135. nr=(version==RoE ? 5 : 6);
  136. break;
  137. }
  138. case gatherResource:
  139. {
  140. this->vicConDetails = new VicCon2();
  141. ((VicCon2*)this->vicConDetails)->resourceID = map[i+2];
  142. ((VicCon2*)this->vicConDetails)->neededQuantity=readNormalNr(map, i+3);
  143. nr=5;
  144. break;
  145. }
  146. case buildCity:
  147. {
  148. this->vicConDetails = new VicCon3();
  149. ((VicCon3*)this->vicConDetails)->posOfCity.x = map[i+2];
  150. ((VicCon3*)this->vicConDetails)->posOfCity.y = map[i+3];
  151. ((VicCon3*)this->vicConDetails)->posOfCity.z = map[i+4];
  152. ((VicCon3*)this->vicConDetails)->councilNeededLevel = map[i+5];
  153. ((VicCon3*)this->vicConDetails)->fortNeededLevel = map[i+6];
  154. nr=5;
  155. break;
  156. }
  157. case buildGrail:
  158. {
  159. this->vicConDetails = new VicCon4();
  160. if (map[i+4]>2)
  161. ((VicCon4*)this->vicConDetails)->anyLocation = true;
  162. else
  163. {
  164. ((VicCon4*)this->vicConDetails)->whereBuildGrail.x = map[i+2];
  165. ((VicCon4*)this->vicConDetails)->whereBuildGrail.y = map[i+3];
  166. ((VicCon4*)this->vicConDetails)->whereBuildGrail.z = map[i+4];
  167. }
  168. nr=3;
  169. break;
  170. }
  171. case beatHero:
  172. {
  173. this->vicConDetails = new VicCon5();
  174. ((VicCon5*)this->vicConDetails)->locationOfHero.x = map[i+2];
  175. ((VicCon5*)this->vicConDetails)->locationOfHero.y = map[i+3];
  176. ((VicCon5*)this->vicConDetails)->locationOfHero.z = map[i+4];
  177. nr=3;
  178. break;
  179. }
  180. case captureCity:
  181. {
  182. this->vicConDetails = new VicCon6();
  183. ((VicCon6*)this->vicConDetails)->locationOfTown.x = map[i+2];
  184. ((VicCon6*)this->vicConDetails)->locationOfTown.y = map[i+3];
  185. ((VicCon6*)this->vicConDetails)->locationOfTown.z = map[i+4];
  186. nr=3;
  187. break;
  188. }
  189. case beatMonster:
  190. {
  191. this->vicConDetails = new VicCon7();
  192. ((VicCon7*)this->vicConDetails)->locationOfMonster.x = map[i+2];
  193. ((VicCon7*)this->vicConDetails)->locationOfMonster.y = map[i+3];
  194. ((VicCon7*)this->vicConDetails)->locationOfMonster.z = map[i+4];
  195. nr=3;
  196. break;
  197. }
  198. case takeDwellings:
  199. {
  200. this->vicConDetails = new CspecificVictoryConidtions();
  201. nr=3;
  202. break;
  203. }
  204. case takeMines:
  205. {
  206. this->vicConDetails = new CspecificVictoryConidtions();
  207. nr=3;
  208. break;
  209. }
  210. case transportItem:
  211. {
  212. this->vicConDetails = new VicCona();
  213. ((VicCona*)this->vicConDetails)->artifactID = map[i+2];
  214. ((VicCona*)this->vicConDetails)->destinationPlace.x = map[i+3];
  215. ((VicCona*)this->vicConDetails)->destinationPlace.y = map[i+4];
  216. ((VicCona*)this->vicConDetails)->destinationPlace.z = map[i+5];
  217. nr=3;
  218. break;
  219. }
  220. }
  221. this->vicConDetails->allowNormalVictory = map[i++];
  222. this->vicConDetails->appliesToAI = map[i++];
  223. i+=nr;
  224. }
  225. this->lossCondition.typeOfLossCon = (ElossCon)map[i++];
  226. switch (this->lossCondition.typeOfLossCon) //read loss conditions
  227. {
  228. case lossCastle:
  229. {
  230. this->lossCondition.castlePos.x=map[i++];
  231. this->lossCondition.castlePos.y=map[i++];
  232. this->lossCondition.castlePos.z=map[i++];
  233. }
  234. case lossHero:
  235. {
  236. this->lossCondition.heroPos.x=map[i++];
  237. this->lossCondition.heroPos.y=map[i++];
  238. this->lossCondition.heroPos.z=map[i++];
  239. }
  240. case timeExpires:
  241. {
  242. this->lossCondition.timeLimit = readNormalNr(map, i++,2);
  243. i++;
  244. }
  245. }
  246. this->howManyTeams=map[i++]; //read number of teams
  247. if(this->howManyTeams>0) //read team numbers
  248. {
  249. for(int rr=0; rr<8; ++rr)
  250. {
  251. this->players[rr].team=map[i++];
  252. }
  253. }
  254. }