2
0

MapFormatH3M.h 9.3 KB

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