MapFormatH3M.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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(IGameInfoCallback * 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 HotA events system.
  112. */
  113. void readHotaScripts();
  114. void readHotaScriptActions();
  115. void readHotaScriptCondition();
  116. void readHotaScriptConditionInternal();
  117. void readHotaScriptExpression();
  118. void readHotaScriptExpressionInternal();
  119. /**
  120. * Reads the list of allowed heroes.
  121. */
  122. void readAllowedHeroes();
  123. /**
  124. * Reads the list of disposed heroes.
  125. */
  126. void readDisposedHeroes();
  127. /**
  128. * Reads the list of allowed artifacts.
  129. */
  130. void readAllowedArtifacts();
  131. /**
  132. * Reads the list of allowed spells and abilities.
  133. */
  134. void readAllowedSpellsAbilities();
  135. /**
  136. * Loads artifacts of a hero.
  137. *
  138. * @param hero the hero which should hold those artifacts
  139. */
  140. void loadArtifactsOfHero(CGHeroInstance * hero);
  141. /**
  142. * Loads an artifact to the given slot of the specified hero.
  143. *
  144. * @param hero the hero which should hold that artifact
  145. * @param slot the artifact slot where to place that artifact
  146. * @return true if it loaded an artifact
  147. */
  148. bool loadArtifactToSlot(CGHeroInstance * hero, int slot);
  149. /**
  150. * Read rumors.
  151. */
  152. void readRumors();
  153. /**
  154. * Reads predefined heroes.
  155. */
  156. void readPredefinedHeroes();
  157. /**
  158. * Reads terrain data.
  159. */
  160. void readTerrain();
  161. /**
  162. * Reads custom(map) def information.
  163. */
  164. void readObjectTemplates();
  165. /**
  166. * Reads objects(towns, mines,...).
  167. */
  168. void readObjects();
  169. /// Reads single object from input stream based on template
  170. std::shared_ptr<CGObjectInstance> readObject(MapObjectID id, MapObjectSubID subid, std::shared_ptr<const ObjectTemplate> objectTemplate, const int3 & objectPosition, const ObjectInstanceID & idToBeGiven);
  171. std::shared_ptr<CGObjectInstance> readEvent(const int3 & objectPosition, const ObjectInstanceID & idToBeGiven);
  172. std::shared_ptr<CGObjectInstance> readMonster(const int3 & objectPosition, const ObjectInstanceID & idToBeGiven);
  173. std::shared_ptr<CGObjectInstance> readHero(const int3 & initialPos, const ObjectInstanceID & idToBeGiven);
  174. std::shared_ptr<CGObjectInstance> readSeerHut(const int3 & initialPos, const ObjectInstanceID & idToBeGiven);
  175. std::shared_ptr<CGObjectInstance> readTown(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl, const ObjectInstanceID & idToBeGiven);
  176. std::shared_ptr<CGObjectInstance> readSign(const int3 & position);
  177. std::shared_ptr<CGObjectInstance> readWitchHut(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  178. std::shared_ptr<CGObjectInstance> readScholar(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  179. std::shared_ptr<CGObjectInstance> readGarrison(const int3 & mapPosition, const ObjectInstanceID & idToBeGiven);
  180. std::shared_ptr<CGObjectInstance> readArtifact(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl, const ObjectInstanceID & idToBeGiven);
  181. std::shared_ptr<CGObjectInstance> readScroll(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl, const ObjectInstanceID & idToBeGiven);
  182. std::shared_ptr<CGObjectInstance> readResource(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl, const ObjectInstanceID & idToBeGiven);
  183. std::shared_ptr<CGObjectInstance> readMine(const int3 & position);
  184. std::shared_ptr<CGObjectInstance> readAbandonedMine(const int3 & position);
  185. std::shared_ptr<CGObjectInstance> readPandora(const int3 & position, const ObjectInstanceID & idToBeGiven);
  186. std::shared_ptr<CGObjectInstance> readDwelling(const int3 & position);
  187. std::shared_ptr<CGObjectInstance> readDwellingRandom(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
  188. std::shared_ptr<CGObjectInstance> readShrine(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  189. std::shared_ptr<CGObjectInstance> readHeroPlaceholder(const int3 & position);
  190. std::shared_ptr<CGObjectInstance> readGrail(const int3 & position);
  191. std::shared_ptr<CGObjectInstance> readHotaBattleLocation(const int3 & position);
  192. std::shared_ptr<CGObjectInstance> readQuestGuard(const int3 & position);
  193. std::shared_ptr<CGObjectInstance> readShipyard(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate);
  194. std::shared_ptr<CGObjectInstance> readLighthouse(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate);
  195. std::shared_ptr<CGObjectInstance> readGeneric(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  196. std::shared_ptr<CGObjectInstance> readBank(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  197. std::shared_ptr<CGObjectInstance> readRewardWithArtifact(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate, int artifactRewardIndex);
  198. std::shared_ptr<CGObjectInstance> readPyramid(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  199. std::shared_ptr<CGObjectInstance> readRewardWithGarbage(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  200. std::shared_ptr<CGObjectInstance> readHotaGrave(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  201. std::shared_ptr<CGObjectInstance> readWagon(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  202. std::shared_ptr<CGObjectInstance> readCampfire(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  203. std::shared_ptr<CGObjectInstance> readLeanTo(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  204. std::shared_ptr<CGObjectInstance> readHotaTrapperLodge(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  205. std::shared_ptr<CGObjectInstance> readRewardWithAmount(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  206. std::shared_ptr<CGObjectInstance> readBlackMarket(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  207. std::shared_ptr<CGObjectInstance> readUniversity(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
  208. /**
  209. * Reads a creature set.
  210. *
  211. * @param out the loaded creature set
  212. * @param number the count of creatures to read
  213. */
  214. void readCreatureSet(CArmedInstance * out, const ObjectInstanceID & idToBeGiven);
  215. void readBoxContent(CGPandoraBox * object, const int3 & position, const ObjectInstanceID & idToBeGiven);
  216. void readBoxHotaContent(CGPandoraBox * object, const int3 & position, const ObjectInstanceID & idToBeGiven);
  217. void readEventCommon(CMapEvent & object, const TextIdentifier & messageID);
  218. /**
  219. * Reads a quest for the given quest guard.
  220. *
  221. * @param guard the quest guard where that quest should be applied to
  222. */
  223. EQuestMission readQuest(IQuestObject * guard, const int3 & position);
  224. void readSeerHutQuest(CGSeerHut * hut, const int3 & position, const ObjectInstanceID & idToBeGiven);
  225. /**
  226. * Reads events.
  227. */
  228. void readEvents();
  229. /**
  230. * read optional message and optional guards
  231. */
  232. void readMessageAndGuards(MetaString & message, CArmedInstance * guards, const int3 & position, const ObjectInstanceID & idToBeGiven);
  233. /// reads string from input stream and converts it to unicode
  234. std::string readBasicString();
  235. /// reads string from input stream, converts it to unicode and attempts to translate it
  236. std::string readLocalizedString(const TextIdentifier & identifier);
  237. void setOwnerAndValidate(const int3 & mapPosition, CGObjectInstance * object, const PlayerColor & owner);
  238. void afterRead();
  239. MapFormatFeaturesH3M features;
  240. /** List of templates loaded from the map, used on later stage to create
  241. * objects but not needed for fully functional CMap */
  242. std::vector<std::shared_ptr<ObjectTemplate>> originalTemplates;
  243. std::vector<std::shared_ptr<ObjectTemplate>> remappedTemplates;
  244. /// associative list to identify which hero/creature id belongs to which object id(index for objects)
  245. std::map<si32, ObjectInstanceID> questIdentifierToId;
  246. std::map<IQuestObject*, si32> questsToResolve;
  247. /** ptr to the map object which gets filled by data from the buffer */
  248. CMap * map;
  249. /**
  250. * ptr to the map header object which gets filled by data from the buffer.
  251. * (when loading a map then the mapHeader ptr points to the same object)
  252. */
  253. std::unique_ptr<CMapHeader> mapHeader;
  254. std::unique_ptr<MapReaderH3M> reader;
  255. CInputStream * inputStream;
  256. std::string mapName;
  257. std::string modName;
  258. std::string fileEncoding;
  259. };
  260. VCMI_LIB_NAMESPACE_END