MapFormatH3M.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "../vcmi_endian.h"
  15. #include "../int3.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(int 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<si32> convertBuildings(const std::set<si32> h3m, int castleID, bool addAuxiliary = true);
  172. /**
  173. * Reads events.
  174. */
  175. void readEvents();
  176. void readSpells(std::set<TSpell> & dest);
  177. void readResourses(TResources& resources);
  178. /** Reads bitmask to boolean vector
  179. * @param dest destination vector, shall be filed with "true" values
  180. * @param byteCount size in bytes of bimask
  181. * @param limit max count of vector elements to alter
  182. * @param negate if true then set bit in mask means clear flag in vertor
  183. */
  184. void readBitmask(std::vector<bool> & dest, const int byteCount, const int limit, bool negate = true);
  185. /**
  186. * Reverses the input argument.
  187. *
  188. * @param arg the input argument
  189. * @return the reversed 8-bit integer
  190. */
  191. ui8 reverse(ui8 arg);
  192. /**
  193. * Helper to read ui8 from buffer
  194. */
  195. inline ui8 readUI8()
  196. {
  197. return buffer[pos++];
  198. }
  199. /**
  200. * Helper to read si8 from buffer
  201. */
  202. inline si8 readSI8()
  203. {
  204. return static_cast<si8>(buffer[pos++]);
  205. }
  206. /**
  207. * Helper to read ui16 from buffer
  208. */
  209. inline ui16 readUI16()
  210. {
  211. ui16 ret = read_le_u16(buffer+pos);
  212. pos +=2;
  213. return ret;
  214. }
  215. /**
  216. * Helper to read ui32 from buffer
  217. */
  218. inline ui32 readUI32()
  219. {
  220. ui32 ret = read_le_u32(buffer+pos);
  221. pos +=4;
  222. return ret;
  223. }
  224. /**
  225. * Helper to read 8bit flag from buffer
  226. */
  227. inline bool readBool()
  228. {
  229. return readUI8() != 0;
  230. }
  231. /**
  232. * Helper to read string from buffer
  233. */
  234. inline std::string readString()
  235. {
  236. return ::readString(buffer,pos);
  237. }
  238. /**
  239. * Helper to skip unused data inbuffer
  240. */
  241. inline void skip(const int count)
  242. {
  243. pos += count;
  244. }
  245. inline int3 readInt3()
  246. {
  247. int3 p;
  248. p.x = readUI8();
  249. p.y = readUI8();
  250. p.z = readUI8();
  251. return p;
  252. }
  253. /**
  254. * Init buffer / size.
  255. *
  256. * @param stream the stream which serves as the data input
  257. */
  258. void initBuffer(CInputStream * stream);
  259. /** ptr to the map object which gets filled by data from the buffer */
  260. CMap * map;
  261. /**
  262. * ptr to the map header object which gets filled by data from the buffer.
  263. * (when loading a map then the mapHeader ptr points to the same object)
  264. */
  265. std::unique_ptr<CMapHeader> mapHeader;
  266. /** pointer to the array containing the map data;
  267. * TODO replace with CBinaryReader later (this makes pos & size redundant) */
  268. ui8 * buffer;
  269. /** current buffer reading position */
  270. int pos;
  271. /** size of the map in bytes */
  272. int size;
  273. };