MapFormatH3M.h 5.7 KB

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