CMapService.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * CMapService.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. class CMap;
  12. class CMapHeader;
  13. class CInputStream;
  14. class CGHeroInstance;
  15. class CArtifactInstance;
  16. class CGObjectInstance;
  17. class CGSeerHut;
  18. class IQuestObject;
  19. class CGTownInstance;
  20. class CCreatureSet;
  21. class CInputStream;
  22. class IMapLoader;
  23. /**
  24. * The map service provides loading of VCMI/H3 map files. It can
  25. * be extended to save maps later as well.
  26. */
  27. class DLL_LINKAGE CMapService
  28. {
  29. public:
  30. /**
  31. * Loads the VCMI/H3 map file specified by the name.
  32. *
  33. * @param name the name of the map
  34. * @return a unique ptr to the loaded map class
  35. */
  36. static std::unique_ptr<CMap> loadMap(const std::string & name);
  37. /**
  38. * Loads the VCMI/H3 map header specified by the name.
  39. *
  40. * @param name the name of the map
  41. * @return a unique ptr to the loaded map header class
  42. */
  43. static std::unique_ptr<CMapHeader> loadMapHeader(const std::string & name);
  44. /**
  45. * Loads the VCMI/H3 map file from a buffer. This method is temporarily
  46. * in use to ease the transition to use the new map service.
  47. *
  48. * TODO Replace method params with a CampaignMapInfo struct which contains
  49. * a campaign loading object + name of map.
  50. *
  51. * @param buffer a pointer to a buffer containing the map data
  52. * @param size the size of the buffer
  53. * @return a unique ptr to the loaded map class
  54. */
  55. static std::unique_ptr<CMap> loadMap(const ui8 * buffer, int size);
  56. /**
  57. * Loads the VCMI/H3 map header from a buffer. This method is temporarily
  58. * in use to ease the transition to use the new map service.
  59. *
  60. * TODO Replace method params with a CampaignMapInfo struct which contains
  61. * a campaign loading object + name of map.
  62. *
  63. * @param buffer a pointer to a buffer containing the map header data
  64. * @param size the size of the buffer
  65. * @return a unique ptr to the loaded map class
  66. */
  67. static std::unique_ptr<CMapHeader> loadMapHeader(const ui8 * buffer, int size);
  68. private:
  69. /**
  70. * Gets a map input stream object specified by a map name.
  71. *
  72. * @param name the name of the map
  73. * @return a unique ptr to the input stream class
  74. */
  75. static std::unique_ptr<CInputStream> getStreamFromFS(const std::string & name);
  76. /**
  77. * Gets a map input stream from a buffer.
  78. *
  79. * @param buffer a pointer to a buffer containing the map data
  80. * @param size the size of the buffer
  81. * @return a unique ptr to the input stream class
  82. */
  83. static std::unique_ptr<CInputStream> getStreamFromMem(const ui8 * buffer, int size);
  84. /**
  85. * Gets a map loader from the given stream. It performs checks to test
  86. * in which map format the map is.
  87. *
  88. * @param stream the input map stream
  89. * @return the constructed map loader
  90. */
  91. static std::unique_ptr<IMapLoader> getMapLoader(std::unique_ptr<CInputStream> & stream);
  92. };
  93. /**
  94. * Interface for loading a map.
  95. */
  96. class DLL_LINKAGE IMapLoader
  97. {
  98. public:
  99. /**
  100. * Loads the VCMI/H3 map file.
  101. *
  102. * @return a unique ptr of the loaded map class
  103. */
  104. virtual std::unique_ptr<CMap> loadMap() = 0;
  105. /**
  106. * Loads the VCMI/H3 map header.
  107. *
  108. * @return a unique ptr of the loaded map header class
  109. */
  110. virtual std::unique_ptr<CMapHeader> loadMapHeader() = 0;
  111. };
  112. class DLL_LINKAGE CMapLoaderH3M : public IMapLoader
  113. {
  114. public:
  115. /**
  116. * Default constructor.
  117. *
  118. * @param stream a stream containing the map data
  119. */
  120. CMapLoaderH3M(CInputStream * stream);
  121. /**
  122. * Destructor.
  123. */
  124. ~CMapLoaderH3M();
  125. /**
  126. * Loads the VCMI/H3 map file.
  127. *
  128. * @return a unique ptr of the loaded map class
  129. */
  130. std::unique_ptr<CMap> loadMap();
  131. /**
  132. * Loads the VCMI/H3 map header.
  133. *
  134. * @return a unique ptr of the loaded map header class
  135. */
  136. std::unique_ptr<CMapHeader> loadMapHeader();
  137. /** true if you want to enable the map loader profiler to see how long a specific part took; default=false */
  138. static const bool IS_PROFILING_ENABLED;
  139. private:
  140. /**
  141. * Initializes the map object from parsing the input buffer.
  142. */
  143. void init();
  144. /**
  145. * Reads the map header.
  146. */
  147. void readHeader();
  148. /**
  149. * Reads player information.
  150. */
  151. void readPlayerInfo();
  152. /**
  153. * Reads victory/loss conditions.
  154. */
  155. void readVictoryLossConditions();
  156. /**
  157. * Reads team information.
  158. */
  159. void readTeamInfo();
  160. /**
  161. * Reads the list of allowed heroes.
  162. */
  163. void readAllowedHeroes();
  164. /**
  165. * Reads the list of disposed heroes.
  166. */
  167. void readDisposedHeroes();
  168. /**
  169. * Reads the list of allowed artifacts.
  170. */
  171. void readAllowedArtifacts();
  172. /**
  173. * Reads the list of allowed spells and abilities.
  174. */
  175. void readAllowedSpellsAbilities();
  176. /**
  177. * Loads artifacts of a hero.
  178. *
  179. * @param hero the hero which should hold those artifacts
  180. */
  181. void loadArtifactsOfHero(CGHeroInstance * hero);
  182. /**
  183. * Loads an artifact to the given slot of the specified hero.
  184. *
  185. * @param hero the hero which should hold that artifact
  186. * @param slot the artifact slot where to place that artifact
  187. * @return true if it loaded an artifact
  188. */
  189. bool loadArtifactToSlot(CGHeroInstance * hero, int slot);
  190. /**
  191. * Creates an artifact instance.
  192. *
  193. * @param aid the id of the artifact
  194. * @param spellID optional. the id of a spell if a spell scroll object should be created
  195. * @return the created artifact instance
  196. */
  197. CArtifactInstance * createArtifact(int aid, int spellID = -1);
  198. /**
  199. * Read rumors.
  200. */
  201. void readRumors();
  202. /**
  203. * Reads predefined heroes.
  204. */
  205. void readPredefinedHeroes();
  206. /**
  207. * Reads terrain data.
  208. */
  209. void readTerrain();
  210. /**
  211. * Reads custom(map) def information.
  212. */
  213. void readDefInfo();
  214. /**
  215. * Reads objects(towns, mines,...).
  216. */
  217. void readObjects();
  218. /**
  219. * Reads a creature set.
  220. *
  221. * @param out the loaded creature set
  222. * @param number the count of creatures to read
  223. * @param version true for > ROE maps
  224. */
  225. void readCreatureSet(CCreatureSet * out, int number, bool version);
  226. /**
  227. * Reads a hero.
  228. *
  229. * @param idToBeGiven the object id which should be set for the hero
  230. * @return a object instance
  231. */
  232. CGObjectInstance * readHero(int idToBeGiven);
  233. /**
  234. * Reads a seer hut.
  235. *
  236. * @return the initialized seer hut object
  237. */
  238. CGSeerHut * readSeerHut();
  239. /**
  240. * Reads a quest for the given quest guard.
  241. *
  242. * @param guard the quest guard where that quest should be applied to
  243. */
  244. void readQuest(IQuestObject * guard);
  245. /**
  246. * Reads a town.
  247. *
  248. * @param castleID the id of the castle type
  249. * @return the loaded town object
  250. */
  251. CGTownInstance * readTown(int castleID);
  252. /**
  253. * Converts buildings to the specified castle id.
  254. *
  255. * @param h3m the ids of the buildings
  256. * @param castleID the castle id
  257. * @param addAuxiliary true if the village hall should be added
  258. * @return the converted buildings
  259. */
  260. std::set<si32> convertBuildings(const std::set<si32> h3m, int castleID, bool addAuxiliary = true);
  261. /**
  262. * Reads events.
  263. */
  264. void readEvents();
  265. /**
  266. * Reverses the input argument.
  267. *
  268. * @param arg the input argument
  269. * @return the reversed 8-bit integer
  270. */
  271. ui8 reverse(ui8 arg);
  272. /**
  273. * Init buffer / size.
  274. *
  275. * @param stream the stream which serves as the data input
  276. */
  277. void initBuffer(CInputStream * stream);
  278. /** ptr to the map object which gets filled by data from the buffer */
  279. CMap * map;
  280. /**
  281. * ptr to the map header object which gets filled by data from the buffer.
  282. * (when loading a map then the mapHeader ptr points to the same object)
  283. */
  284. std::unique_ptr<CMapHeader> mapHeader;
  285. /** pointer to the array containing the map data;
  286. * TODO replace with CBinaryReader later (this makes pos & size redundant) */
  287. ui8 * buffer;
  288. /** current buffer reading position */
  289. int pos;
  290. /** size of the map in bytes */
  291. int size;
  292. };