MapFormatH3M.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * MapFormatH3M.h, 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. #pragma once
  11. #include "CMapService.h"
  12. #include "../GameConstants.h"
  13. #include "../ResourceSet.h"
  14. #include "../mapObjects/ObjectTemplate.h"
  15. #include "../int3.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CGHeroInstance;
  18. class CBinaryReader;
  19. class CArtifactInstance;
  20. class CGObjectInstance;
  21. class CGSeerHut;
  22. class IQuestObject;
  23. class CGTownInstance;
  24. class CCreatureSet;
  25. class CInputStream;
  26. class DLL_LINKAGE CMapLoaderH3M : public IMapLoader
  27. {
  28. public:
  29. /**
  30. * Default constructor.
  31. *
  32. * @param stream a stream containing the map data
  33. */
  34. CMapLoaderH3M(const std::string & mapName, const std::string & encodingName, CInputStream * stream);
  35. /**
  36. * Destructor.
  37. */
  38. ~CMapLoaderH3M();
  39. /**
  40. * Loads the VCMI/H3 map file.
  41. *
  42. * @return a unique ptr of the loaded map class
  43. */
  44. std::unique_ptr<CMap> loadMap() override;
  45. /**
  46. * Loads the VCMI/H3 map header.
  47. *
  48. * @return a unique ptr of the loaded map header class
  49. */
  50. std::unique_ptr<CMapHeader> loadMapHeader() override;
  51. /** true if you want to enable the map loader profiler to see how long a specific part took; default=false */
  52. static const bool IS_PROFILING_ENABLED;
  53. private:
  54. /**
  55. * Initializes the map object from parsing the input buffer.
  56. */
  57. void init();
  58. /**
  59. * Reads the map header.
  60. */
  61. void readHeader();
  62. /**
  63. * Reads player information.
  64. */
  65. void readPlayerInfo();
  66. /**
  67. * Reads victory/loss conditions.
  68. */
  69. void readVictoryLossConditions();
  70. /**
  71. * Reads team information.
  72. */
  73. void readTeamInfo();
  74. /**
  75. * Reads the list of allowed heroes.
  76. */
  77. void readAllowedHeroes();
  78. /**
  79. * Reads the list of disposed heroes.
  80. */
  81. void readDisposedHeroes();
  82. /**
  83. * Reads the list of allowed artifacts.
  84. */
  85. void readAllowedArtifacts();
  86. /**
  87. * Reads the list of allowed spells and abilities.
  88. */
  89. void readAllowedSpellsAbilities();
  90. /**
  91. * Loads artifacts of a hero.
  92. *
  93. * @param hero the hero which should hold those artifacts
  94. */
  95. void loadArtifactsOfHero(CGHeroInstance * hero);
  96. /**
  97. * Loads an artifact to the given slot of the specified hero.
  98. *
  99. * @param hero the hero which should hold that artifact
  100. * @param slot the artifact slot where to place that artifact
  101. * @return true if it loaded an artifact
  102. */
  103. bool loadArtifactToSlot(CGHeroInstance * hero, int slot);
  104. /**
  105. * Read rumors.
  106. */
  107. void readRumors();
  108. /**
  109. * Reads predefined heroes.
  110. */
  111. void readPredefinedHeroes();
  112. /**
  113. * Reads terrain data.
  114. */
  115. void readTerrain();
  116. /**
  117. * Reads custom(map) def information.
  118. */
  119. void readDefInfo();
  120. /**
  121. * Reads objects(towns, mines,...).
  122. */
  123. void readObjects();
  124. /**
  125. * Reads a creature set.
  126. *
  127. * @param out the loaded creature set
  128. * @param number the count of creatures to read
  129. */
  130. void readCreatureSet(CCreatureSet * out, int number);
  131. /**
  132. * Reads a hero.
  133. *
  134. * @param idToBeGiven the object id which should be set for the hero
  135. * @return a object instance
  136. */
  137. CGObjectInstance * readHero(const ObjectInstanceID & idToBeGiven, const int3 & initialPos);
  138. /**
  139. * Reads a seer hut.
  140. *
  141. * @return the initialized seer hut object
  142. */
  143. CGSeerHut * readSeerHut();
  144. /**
  145. * Reads a quest for the given quest guard.
  146. *
  147. * @param guard the quest guard where that quest should be applied to
  148. */
  149. void readQuest(IQuestObject * guard);
  150. /**
  151. * Reads a town.
  152. *
  153. * @param castleID the id of the castle type
  154. * @return the loaded town object
  155. */
  156. CGTownInstance * readTown(int castleID);
  157. /**
  158. * Converts buildings to the specified castle id.
  159. *
  160. * @param h3m the ids of the buildings
  161. * @param castleID the castle id
  162. * @param addAuxiliary true if the village hall should be added
  163. * @return the converted buildings
  164. */
  165. std::set<BuildingID> convertBuildings(const std::set<BuildingID> & h3m, int castleID, bool addAuxiliary = true) const;
  166. /**
  167. * Reads events.
  168. */
  169. void readEvents();
  170. /**
  171. * read optional message and optional guards
  172. */
  173. void readMessageAndGuards(std::string& message, CCreatureSet * guards);
  174. void readSpells(std::set<SpellID> & dest);
  175. void readResourses(TResources& resources);
  176. template <class Indenifier>
  177. void readBitmask(std::set<Indenifier> &dest, const int byteCount, const int limit, bool negate = true);
  178. /** Reads bitmask to boolean vector
  179. * @param dest destination vector, shall be filed with "true" values
  180. * @param byteCount size in bytes of bimask
  181. * @param limit max count of vector elements to alter
  182. * @param negate if true then set bit in mask means clear flag in vertor
  183. */
  184. void readBitmask(std::vector<bool> & dest, const int byteCount, const int limit, bool negate = true);
  185. /**
  186. * Reverses the input argument.
  187. *
  188. * @param arg the input argument
  189. * @return the reversed 8-bit integer
  190. */
  191. ui8 reverse(ui8 arg) const;
  192. /**
  193. * Helper to read map position
  194. */
  195. int3 readInt3();
  196. /// reads string from input stream and converts it to unicode
  197. std::string readLocalizedString();
  198. void afterRead();
  199. /** List of templates loaded from the map, used on later stage to create
  200. * objects but not needed for fully functional CMap */
  201. std::vector<std::shared_ptr<const ObjectTemplate>> templates;
  202. /** ptr to the map object which gets filled by data from the buffer */
  203. CMap * map;
  204. /**
  205. * ptr to the map header object which gets filled by data from the buffer.
  206. * (when loading a map then the mapHeader ptr points to the same object)
  207. */
  208. std::unique_ptr<CMapHeader> mapHeader;
  209. std::unique_ptr<CBinaryReader> reader;
  210. CInputStream * inputStream;
  211. std::string mapName;
  212. std::string fileEncoding;
  213. };
  214. VCMI_LIB_NAMESPACE_END