MapFormatH3M.h 6.2 KB

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