MapFormatH3M.h 5.7 KB

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