MapReaderH3M.cpp 10.0 KB

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