MapReaderH3M.cpp 9.6 KB

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