MapFormatH3M.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "MapFeaturesH3M.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CGHeroInstance;
  15. class MapReaderH3M;
  16. class CArtifactInstance;
  17. class CGObjectInstance;
  18. class CGSeerHut;
  19. class IQuestObject;
  20. class CGTownInstance;
  21. class CCreatureSet;
  22. class CInputStream;
  23. class TextIdentifier;
  24. class CGPandoraBox;
  25. class ObjectInstanceID;
  26. class BuildingID;
  27. class ObjectTemplate;
  28. class SpellID;
  29. class PlayerColor;
  30. class int3;
  31. class DLL_LINKAGE CMapLoaderH3M : public IMapLoader
  32. {
  33. public:
  34. /**
  35. * Default constructor.
  36. *
  37. * @param stream a stream containing the map data
  38. */
  39. CMapLoaderH3M(const std::string & mapName, const std::string & modName, const std::string & encodingName, CInputStream * stream);
  40. /**
  41. * Destructor.
  42. */
  43. ~CMapLoaderH3M();
  44. /**
  45. * Loads the VCMI/H3 map file.
  46. *
  47. * @return a unique ptr of the loaded map class
  48. */
  49. std::unique_ptr<CMap> loadMap() override;
  50. /**
  51. * Loads the VCMI/H3 map header.
  52. *
  53. * @return a unique ptr of the loaded map header class
  54. */
  55. std::unique_ptr<CMapHeader> loadMapHeader() override;
  56. private:
  57. /**
  58. * Initializes the map object from parsing the input buffer.
  59. */
  60. void init();
  61. /**
  62. * Reads the map header.
  63. */
  64. void readHeader();
  65. /**
  66. * Reads player information.
  67. */
  68. void readPlayerInfo();
  69. /**
  70. * Reads victory/loss conditions.
  71. */
  72. void readVictoryLossConditions();
  73. /**
  74. * Reads team information.
  75. */
  76. void readTeamInfo();
  77. /**
  78. * Reads the list of map flags.
  79. */
  80. void readMapOptions();
  81. /**
  82. * Reads the list of allowed heroes.
  83. */
  84. void readAllowedHeroes();
  85. /**
  86. * Reads the list of disposed heroes.
  87. */
  88. void readDisposedHeroes();
  89. /**
  90. * Reads the list of allowed artifacts.
  91. */
  92. void readAllowedArtifacts();
  93. /**
  94. * Reads the list of allowed spells and abilities.
  95. */
  96. void readAllowedSpellsAbilities();
  97. /**
  98. * Loads artifacts of a hero.
  99. *
  100. * @param hero the hero which should hold those artifacts
  101. */
  102. void loadArtifactsOfHero(CGHeroInstance * hero);
  103. /**
  104. * Loads an artifact to the given slot of the specified hero.
  105. *
  106. * @param hero the hero which should hold that artifact
  107. * @param slot the artifact slot where to place that artifact
  108. * @return true if it loaded an artifact
  109. */
  110. bool loadArtifactToSlot(CGHeroInstance * hero, int slot);
  111. /**
  112. * Read rumors.
  113. */
  114. void readRumors();
  115. /**
  116. * Reads predefined heroes.
  117. */
  118. void readPredefinedHeroes();
  119. /**
  120. * Reads terrain data.
  121. */
  122. void readTerrain();
  123. /**
  124. * Reads custom(map) def information.
  125. */
  126. void readDefInfo();
  127. /**
  128. * Reads objects(towns, mines,...).
  129. */
  130. void readObjects();
  131. /// Reads single object from input stream based on template
  132. CGObjectInstance * readObject(std::shared_ptr<const ObjectTemplate> objectTemplate, const int3 & objectPosition, const ObjectInstanceID & idToBeGiven);
  133. CGObjectInstance * readEvent(const int3 & objectPosition);
  134. CGObjectInstance * readMonster(const int3 & objectPosition, const ObjectInstanceID & idToBeGiven);
  135. CGObjectInstance * readHero(const int3 & initialPos, const ObjectInstanceID & idToBeGiven);
  136. CGObjectInstance * readSeerHut(const int3 & initialPos);
  137. CGObjectInstance * readTown(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  138. CGObjectInstance * readSign(const int3 & position);
  139. CGObjectInstance * readWitchHut();
  140. CGObjectInstance * readScholar();
  141. CGObjectInstance * readGarrison(const int3 & mapPosition);
  142. CGObjectInstance * readArtifact(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  143. CGObjectInstance * readResource(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  144. CGObjectInstance * readMine(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  145. CGObjectInstance * readPandora(const int3 & position);
  146. CGObjectInstance * readDwelling(const int3 & position);
  147. CGObjectInstance * readDwellingRandom(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  148. CGObjectInstance * readShrine();
  149. CGObjectInstance * readHeroPlaceholder(const int3 & position);
  150. CGObjectInstance * readGrail(const int3 & position);
  151. CGObjectInstance * readPyramid(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  152. CGObjectInstance * readBorderGuard();
  153. CGObjectInstance * readBorderGate(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  154. CGObjectInstance * readQuestGuard(const int3 & position);
  155. CGObjectInstance * readShipyard(const int3 & mapPosition);
  156. CGObjectInstance * readLighthouse(const int3 & mapPosition);
  157. CGObjectInstance * readGeneric(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  158. CGObjectInstance * readBank(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  159. /**
  160. * Reads a creature set.
  161. *
  162. * @param out the loaded creature set
  163. * @param number the count of creatures to read
  164. */
  165. void readCreatureSet(CCreatureSet * out, int number);
  166. /**
  167. * Reads a quest for the given quest guard.
  168. *
  169. * @param guard the quest guard where that quest should be applied to
  170. */
  171. void readBoxContent(CGPandoraBox * object, const int3 & position);
  172. /**
  173. * Reads a quest for the given quest guard.
  174. *
  175. * @param guard the quest guard where that quest should be applied to
  176. */
  177. void readQuest(IQuestObject * guard, const int3 & position);
  178. void readSeerHutQuest(CGSeerHut * hut, const int3 & position);
  179. /**
  180. * Converts buildings to the specified castle id.
  181. *
  182. * @param h3m the ids of the buildings
  183. * @param castleID the castle id
  184. * @param addAuxiliary true if the village hall should be added
  185. * @return the converted buildings
  186. */
  187. std::set<BuildingID> convertBuildings(const std::set<BuildingID> & h3m, int castleID, bool addAuxiliary = true) const;
  188. /**
  189. * Reads events.
  190. */
  191. void readEvents();
  192. /**
  193. * read optional message and optional guards
  194. */
  195. void readMessageAndGuards(std::string & message, CCreatureSet * guards, const int3 & position);
  196. /// reads string from input stream and converts it to unicode
  197. std::string readBasicString();
  198. /// reads string from input stream, converts it to unicode and attempts to translate it
  199. std::string readLocalizedString(const TextIdentifier & identifier);
  200. void setOwnerAndValidate(const int3 & mapPosition, CGObjectInstance * object, const PlayerColor & owner);
  201. void readSpells(std::set<SpellID> & dest);
  202. void afterRead();
  203. MapFormatFeaturesH3M features;
  204. /** List of templates loaded from the map, used on later stage to create
  205. * objects but not needed for fully functional CMap */
  206. std::vector<std::shared_ptr<const ObjectTemplate>> templates;
  207. /** ptr to the map object which gets filled by data from the buffer */
  208. CMap * map;
  209. /**
  210. * ptr to the map header object which gets filled by data from the buffer.
  211. * (when loading a map then the mapHeader ptr points to the same object)
  212. */
  213. std::unique_ptr<CMapHeader> mapHeader;
  214. std::unique_ptr<MapReaderH3M> reader;
  215. CInputStream * inputStream;
  216. std::string mapName;
  217. std::string modName;
  218. std::string fileEncoding;
  219. };
  220. VCMI_LIB_NAMESPACE_END