Terrain.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Terrain.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 "ConstTransitivePtr.h"
  12. #include "GameConstants.h"
  13. #include "JsonNode.h"
  14. class DLL_LINKAGE TerrainType
  15. {
  16. public:
  17. enum PassabilityType : ui8
  18. {
  19. LAND = 1,
  20. WATER = 2,
  21. SURFACE = 4,
  22. SUBTERRANEAN = 8,
  23. ROCK = 16
  24. };
  25. std::vector<std::string> battleFields;
  26. std::vector<TTerrainId> prohibitTransitions;
  27. std::array<int, 3> minimapBlocked;
  28. std::array<int, 3> minimapUnblocked;
  29. std::string name;
  30. std::string musicFilename;
  31. std::string tilesFilename;
  32. std::string terrainText;
  33. std::string typeCode;
  34. std::string terrainViewPatterns;
  35. TRiverId river;
  36. TTerrainId id;
  37. TTerrainId rockTerrain;
  38. int moveCost;
  39. int horseSoundId;
  40. ui8 passabilityType;
  41. bool transitionRequired;
  42. TerrainType(const std::string & name = "");
  43. TerrainType& operator=(const TerrainType & other);
  44. bool operator==(const TerrainType & other);
  45. bool operator!=(const TerrainType & other);
  46. bool operator<(const TerrainType & other);
  47. bool isLand() const;
  48. bool isWater() const;
  49. bool isPassable() const;
  50. bool isSurface() const;
  51. bool isUnderground() const;
  52. bool isTransitionRequired() const;
  53. operator std::string() const;
  54. template <typename Handler> void serialize(Handler &h, const int version)
  55. {
  56. h & battleFields;
  57. h & prohibitTransitions;
  58. h & minimapBlocked;
  59. h & minimapUnblocked;
  60. h & name;
  61. h & musicFilename;
  62. h & tilesFilename;
  63. h & terrainText;
  64. h & typeCode;
  65. h & terrainViewPatterns;
  66. h & rockTerrain;
  67. h & river;
  68. h & id;
  69. h & moveCost;
  70. h & horseSoundId;
  71. h & passabilityType;
  72. h & transitionRequired;
  73. }
  74. };
  75. class DLL_LINKAGE RiverType
  76. {
  77. public:
  78. std::string fileName;
  79. std::string code;
  80. std::string deltaName;
  81. TRiverId id;
  82. RiverType(const std::string & fileName = "", const std::string & code = "", TRiverId id = River::NO_RIVER);
  83. RiverType& operator=(const RiverType & other);
  84. template <typename Handler> void serialize(Handler& h, const int version)
  85. {
  86. h & fileName;
  87. h & code;
  88. h & deltaName;
  89. h & id;
  90. }
  91. };
  92. class DLL_LINKAGE RoadType
  93. {
  94. public:
  95. std::string fileName;
  96. std::string code;
  97. TRoadId id;
  98. ui8 movementCost;
  99. RoadType(const std::string & fileName = "", const std::string& code = "", TRoadId id = Road::NO_ROAD);
  100. template <typename Handler> void serialize(Handler& h, const int version)
  101. {
  102. h & fileName;
  103. h & code;
  104. h & id;
  105. h & movementCost;
  106. }
  107. };
  108. DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType);
  109. class DLL_LINKAGE TerrainTypeHandler //TODO: public IHandlerBase ?
  110. {
  111. public:
  112. TerrainTypeHandler();
  113. ~TerrainTypeHandler();
  114. const std::vector<TerrainType> & terrains() const;
  115. const TerrainType * getInfoByName(const std::string & terrainName) const;
  116. const TerrainType * getInfoByCode(const std::string & terrainCode) const;
  117. const TerrainType * getInfoById(TTerrainId id) const;
  118. const std::vector<RiverType> & rivers() const;
  119. const RiverType * getRiverByName(const std::string & riverName) const;
  120. const RiverType * getRiverByCode(const std::string & riverCode) const;
  121. const RiverType * getRiverById(TRiverId id) const;
  122. const std::vector<RoadType *> & roads() const;
  123. const RoadType * getRoadByName(const std::string & roadName) const;
  124. const RoadType * getRoadByCode(const std::string & roadCode) const;
  125. const RoadType * getRoadById(TRoadId id) const;
  126. template <typename Handler> void serialize(Handler &h, const int version)
  127. {
  128. h & objects;
  129. h & riverTypes;
  130. h & roadTypes;
  131. if (!h.saving)
  132. {
  133. recreateTerrainMaps();
  134. recreateRiverMaps();
  135. recreateRoadMaps();
  136. }
  137. }
  138. private:
  139. std::vector<TerrainType> objects;
  140. std::vector<RiverType> riverTypes;
  141. std::vector<RoadType *> roadTypes;
  142. std::unordered_map<std::string, const TerrainType*> terrainInfoByName;
  143. std::unordered_map<std::string, const TerrainType*> terrainInfoByCode;
  144. std::unordered_map<TTerrainId, const TerrainType*> terrainInfoById;
  145. std::unordered_map<std::string, const RiverType*> riverInfoByName;
  146. std::unordered_map<std::string, const RiverType*> riverInfoByCode;
  147. std::unordered_map<TRiverId, const RiverType*> riverInfoById;
  148. std::unordered_map<std::string, const RoadType*> roadInfoByName;
  149. std::unordered_map<std::string, const RoadType*> roadInfoByCode;
  150. std::unordered_map<TRoadId, const RoadType*> roadInfoById;
  151. void initTerrains(const std::vector<std::string> & allConfigs);
  152. void initRivers(const std::vector<std::string> & allConfigs);
  153. void initRoads(const std::vector<std::string> & allConfigs);
  154. void recreateTerrainMaps();
  155. void recreateRiverMaps();
  156. void recreateRoadMaps();
  157. };