MapReaderH3M.cpp 11 KB

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