Terrain.h 4.7 KB

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