MapReaderH3M.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * MapReaderH3M.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "MapReaderH3M.h"
  12. #include "../filesystem/CBinaryReader.h"
  13. #include "../int3.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. template<>
  16. BuildingID MapReaderH3M::remapIdentifier(const BuildingID & identifier)
  17. {
  18. return identifier;
  19. }
  20. template<>
  21. GameResID MapReaderH3M::remapIdentifier(const GameResID & identifier)
  22. {
  23. return identifier;
  24. }
  25. template<>
  26. SpellID MapReaderH3M::remapIdentifier(const SpellID & identifier)
  27. {
  28. return identifier;
  29. }
  30. template<class Identifier>
  31. Identifier MapReaderH3M::remapIdentifier(const Identifier & identifier)
  32. {
  33. return remapper.remap(identifier);
  34. }
  35. MapReaderH3M::MapReaderH3M(CInputStream * stream)
  36. : reader(std::make_unique<CBinaryReader>(stream))
  37. {
  38. }
  39. void MapReaderH3M::setFormatLevel(const MapFormatFeaturesH3M & newFeatures)
  40. {
  41. features = newFeatures;
  42. }
  43. void MapReaderH3M::setIdentifierRemapper(const MapIdentifiersH3M & newRemapper)
  44. {
  45. remapper = newRemapper;
  46. }
  47. ArtifactID MapReaderH3M::readArtifact()
  48. {
  49. ArtifactID result;
  50. if(features.levelAB)
  51. result = ArtifactID(reader->readUInt16());
  52. else
  53. result = ArtifactID(reader->readUInt8());
  54. if(result == features.artifactIdentifierInvalid)
  55. return ArtifactID::NONE;
  56. if (result < features.artifactsCount)
  57. return remapIdentifier(result);
  58. logGlobal->warn("Map contains invalid artifact %d. Will be removed!", result.getNum());
  59. return ArtifactID::NONE;
  60. }
  61. ArtifactID MapReaderH3M::readArtifact32()
  62. {
  63. ArtifactID result(reader->readInt32());
  64. if(result == ArtifactID::NONE)
  65. return ArtifactID::NONE;
  66. if (result < features.artifactsCount)
  67. return remapIdentifier(result);
  68. logGlobal->warn("Map contains invalid artifact %d. Will be removed!", result.getNum());
  69. return ArtifactID::NONE;
  70. }
  71. HeroTypeID MapReaderH3M::readHero()
  72. {
  73. HeroTypeID result(reader->readUInt8());
  74. if(result.getNum() == features.heroIdentifierInvalid)
  75. return HeroTypeID(-1);
  76. assert(result.getNum() < features.heroesPortraitsCount);
  77. return remapIdentifier(result);;
  78. }
  79. CreatureID MapReaderH3M::readCreature()
  80. {
  81. CreatureID result;
  82. if(features.levelAB)
  83. result = CreatureID(reader->readUInt16());
  84. else
  85. result = CreatureID(reader->readUInt8());
  86. if(result == features.creatureIdentifierInvalid)
  87. return CreatureID::NONE;
  88. if(result < features.creaturesCount)
  89. return remapIdentifier(result);;
  90. // this may be random creature in army/town, to be randomized later
  91. CreatureID randomIndex(result.getNum() - features.creatureIdentifierInvalid - 1);
  92. assert(randomIndex < CreatureID::NONE);
  93. if (randomIndex > -16)
  94. return randomIndex;
  95. logGlobal->warn("Map contains invalid creature %d. Will be removed!", result.getNum());
  96. return CreatureID::NONE;
  97. }
  98. TerrainId MapReaderH3M::readTerrain()
  99. {
  100. TerrainId result(readUInt8());
  101. assert(result.getNum() < features.terrainsCount);
  102. return remapIdentifier(result);;
  103. }
  104. RoadId MapReaderH3M::readRoad()
  105. {
  106. RoadId result(readInt8());
  107. assert(result < Road::ORIGINAL_ROAD_COUNT);
  108. return result;
  109. }
  110. RiverId MapReaderH3M::readRiver()
  111. {
  112. RiverId result(readInt8());
  113. assert(result < River::ORIGINAL_RIVER_COUNT);
  114. return result;
  115. }
  116. SecondarySkill MapReaderH3M::readSkill()
  117. {
  118. SecondarySkill result(readUInt8());
  119. assert(result < features.skillsCount);
  120. return remapIdentifier(result);;
  121. }
  122. SpellID MapReaderH3M::readSpell()
  123. {
  124. SpellID result(readUInt8());
  125. if(result == features.spellIdentifierInvalid)
  126. return SpellID::NONE;
  127. if(result == features.spellIdentifierInvalid - 1)
  128. return SpellID::PRESET;
  129. assert(result < features.spellsCount);
  130. return remapIdentifier(result);;
  131. }
  132. SpellID MapReaderH3M::readSpell32()
  133. {
  134. SpellID result(readInt32());
  135. if(result == features.spellIdentifierInvalid)
  136. return SpellID::NONE;
  137. assert(result < features.spellsCount);
  138. return result;
  139. }
  140. PlayerColor MapReaderH3M::readPlayer()
  141. {
  142. PlayerColor result(readUInt8());
  143. assert(result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
  144. return result;
  145. }
  146. PlayerColor MapReaderH3M::readPlayer32()
  147. {
  148. PlayerColor result(readInt32());
  149. assert(result < PlayerColor::PLAYER_LIMIT || result == PlayerColor::NEUTRAL);
  150. return result;
  151. }
  152. void MapReaderH3M::readBitmaskBuildings(std::set<BuildingID> & dest, std::optional<FactionID> faction)
  153. {
  154. std::set<BuildingID> h3m;
  155. readBitmask(h3m, features.buildingsBytes, features.buildingsCount, false);
  156. for (auto const & h3mEntry : h3m)
  157. {
  158. BuildingID mapped = remapper.remapBuilding(faction, h3mEntry);
  159. if (mapped != BuildingID::NONE) // artifact merchant may be set in random town, but not present in actual town
  160. dest.insert(mapped);
  161. }
  162. }
  163. void MapReaderH3M::readBitmaskFactions(std::set<FactionID> & dest, bool invert)
  164. {
  165. readBitmask(dest, features.factionsBytes, features.factionsCount, invert);
  166. }
  167. void MapReaderH3M::readBitmaskResources(std::set<GameResID> & dest, bool invert)
  168. {
  169. readBitmask(dest, features.resourcesBytes, features.resourcesCount, invert);
  170. }
  171. void MapReaderH3M::readBitmaskHeroClassesSized(std::set<HeroClassID> & dest, bool invert)
  172. {
  173. uint32_t classesCount = reader->readUInt32();
  174. uint32_t classesBytes = (classesCount + 7) / 8;
  175. readBitmask(dest, classesBytes, classesCount, invert);
  176. }
  177. void MapReaderH3M::readBitmaskHeroes(std::vector<bool> & dest, bool invert)
  178. {
  179. readBitmask<HeroTypeID>(dest, features.heroesBytes, features.heroesCount, invert);
  180. }
  181. void MapReaderH3M::readBitmaskHeroesSized(std::vector<bool> & dest, bool invert)
  182. {
  183. uint32_t heroesCount = readUInt32();
  184. uint32_t heroesBytes = (heroesCount + 7) / 8;
  185. assert(heroesCount <= features.heroesCount);
  186. readBitmask<HeroTypeID>(dest, heroesBytes, heroesCount, invert);
  187. }
  188. void MapReaderH3M::readBitmaskArtifacts(std::vector<bool> &dest, bool invert)
  189. {
  190. readBitmask<ArtifactID>(dest, features.artifactsBytes, features.artifactsCount, invert);
  191. }
  192. void MapReaderH3M::readBitmaskArtifactsSized(std::vector<bool> &dest, bool invert)
  193. {
  194. uint32_t artifactsCount = reader->readUInt32();
  195. uint32_t artifactsBytes = (artifactsCount + 7) / 8;
  196. assert(artifactsCount <= features.artifactsCount);
  197. readBitmask<ArtifactID>(dest, artifactsBytes, artifactsCount, invert);
  198. }
  199. void MapReaderH3M::readBitmaskSpells(std::vector<bool> & dest, bool invert)
  200. {
  201. readBitmask<SpellID>(dest, features.spellsBytes, features.spellsCount, invert);
  202. }
  203. void MapReaderH3M::readBitmaskSpells(std::set<SpellID> & dest, bool invert)
  204. {
  205. readBitmask(dest, features.spellsBytes, features.spellsCount, invert);
  206. }
  207. void MapReaderH3M::readBitmaskSkills(std::vector<bool> & dest, bool invert)
  208. {
  209. readBitmask<SecondarySkill>(dest, features.skillsBytes, features.skillsCount, invert);
  210. }
  211. void MapReaderH3M::readBitmaskSkills(std::set<SecondarySkill> & dest, bool invert)
  212. {
  213. readBitmask(dest, features.skillsBytes, features.skillsCount, invert);
  214. }
  215. template<class Identifier>
  216. void MapReaderH3M::readBitmask(std::vector<bool> & dest, const int bytesToRead, const int objectsToRead, bool invert)
  217. {
  218. for(int byte = 0; byte < bytesToRead; ++byte)
  219. {
  220. const ui8 mask = reader->readUInt8();
  221. for(int bit = 0; bit < 8; ++bit)
  222. {
  223. if(byte * 8 + bit < objectsToRead)
  224. {
  225. const size_t index = byte * 8 + bit;
  226. const bool flag = mask & (1 << bit);
  227. const bool result = (flag != invert);
  228. Identifier h3mID(index);
  229. Identifier vcmiID = remapIdentifier(h3mID);
  230. if (vcmiID.getNum() >= dest.size())
  231. dest.resize(vcmiID.getNum() + 1);
  232. dest[vcmiID.getNum()] = result;
  233. }
  234. }
  235. }
  236. }
  237. template<class Identifier>
  238. void MapReaderH3M::readBitmask(std::set<Identifier> & dest, int bytesToRead, int objectsToRead, bool invert)
  239. {
  240. std::vector<bool> bitmap;
  241. bitmap.resize(objectsToRead, false);
  242. readBitmask<Identifier>(bitmap, bytesToRead, objectsToRead, invert);
  243. for(int i = 0; i < bitmap.size(); i++)
  244. if(bitmap[i])
  245. dest.insert(static_cast<Identifier>(i));
  246. }
  247. int3 MapReaderH3M::readInt3()
  248. {
  249. int3 p;
  250. p.x = reader->readUInt8();
  251. p.y = reader->readUInt8();
  252. p.z = reader->readUInt8();
  253. return p;
  254. }
  255. void MapReaderH3M::skipUnused(size_t amount)
  256. {
  257. reader->skip(amount);
  258. }
  259. void MapReaderH3M::skipZero(size_t amount)
  260. {
  261. #ifdef NDEBUG
  262. skipUnused(amount);
  263. #else
  264. for(size_t i = 0; i < amount; ++i)
  265. {
  266. uint8_t value = reader->readUInt8();
  267. assert(value == 0);
  268. }
  269. #endif
  270. }
  271. void MapReaderH3M::readResourses(TResources & resources)
  272. {
  273. for(int x = 0; x < features.resourcesCount; ++x)
  274. resources[x] = reader->readInt32();
  275. }
  276. bool MapReaderH3M::readBool()
  277. {
  278. uint8_t result = readUInt8();
  279. assert(result == 0 || result == 1);
  280. return result != 0;
  281. }
  282. ui8 MapReaderH3M::readUInt8()
  283. {
  284. return reader->readUInt8();
  285. }
  286. si8 MapReaderH3M::readInt8()
  287. {
  288. return reader->readInt8();
  289. }
  290. ui16 MapReaderH3M::readUInt16()
  291. {
  292. return reader->readUInt16();
  293. }
  294. si16 MapReaderH3M::readInt16()
  295. {
  296. return reader->readInt16();
  297. }
  298. ui32 MapReaderH3M::readUInt32()
  299. {
  300. return reader->readUInt32();
  301. }
  302. si32 MapReaderH3M::readInt32()
  303. {
  304. return reader->readInt32();
  305. }
  306. std::string MapReaderH3M::readBaseString()
  307. {
  308. return reader->readBaseString();
  309. }
  310. CBinaryReader & MapReaderH3M::getInternalReader()
  311. {
  312. return *reader;
  313. }
  314. VCMI_LIB_NAMESPACE_END