2
0

MapReaderH3M.cpp 10 KB

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