Terrain.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. 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. RiverId id;
  82. RiverType(const std::string & fileName = "", const std::string & code = "", RiverId id = River::NO_RIVER);
  83. template <typename Handler> void serialize(Handler& h, const int version)
  84. {
  85. h & fileName;
  86. h & code;
  87. h & deltaName;
  88. h & id;
  89. }
  90. };
  91. class DLL_LINKAGE RoadType
  92. {
  93. public:
  94. std::string fileName;
  95. std::string code;
  96. RoadId id;
  97. ui8 movementCost;
  98. RoadType(const std::string & fileName = "", const std::string& code = "", RoadId id = Road::NO_ROAD);
  99. template <typename Handler> void serialize(Handler& h, const int version)
  100. {
  101. h & fileName;
  102. h & code;
  103. h & id;
  104. h & movementCost;
  105. }
  106. };
  107. DLL_LINKAGE std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType);
  108. class DLL_LINKAGE TerrainTypeHandler //TODO: public IHandlerBase ?
  109. {
  110. public:
  111. TerrainTypeHandler();
  112. ~TerrainTypeHandler() {};
  113. const std::vector<TerrainType> & terrains() const;
  114. const TerrainType * getInfoByName(const std::string & terrainName) const;
  115. const TerrainType * getInfoByCode(const std::string & terrainCode) const;
  116. const TerrainType * getInfoById(TerrainId id) const;
  117. const std::vector<RiverType> & rivers() const;
  118. const RiverType * getRiverByName(const std::string & riverName) const;
  119. const RiverType * getRiverByCode(const std::string & riverCode) const;
  120. const RiverType * getRiverById(RiverId id) const;
  121. const std::vector<RoadType> & roads() const;
  122. const RoadType * getRoadByName(const std::string & roadName) const;
  123. const RoadType * getRoadByCode(const std::string & roadCode) const;
  124. const RoadType * getRoadById(RoadId id) const;
  125. template <typename Handler> void serialize(Handler &h, const int version)
  126. {
  127. h & objects;
  128. h & riverTypes;
  129. h & roadTypes;
  130. if (!h.saving)
  131. {
  132. recreateTerrainMaps();
  133. recreateRiverMaps();
  134. recreateRoadMaps();
  135. }
  136. }
  137. private:
  138. std::vector<TerrainType> objects;
  139. std::vector<RiverType> riverTypes;
  140. std::vector<RoadType> roadTypes;
  141. std::unordered_map<std::string, const TerrainType*> terrainInfoByName;
  142. std::unordered_map<std::string, const TerrainType*> terrainInfoByCode;
  143. std::unordered_map<TerrainId, const TerrainType*> terrainInfoById;
  144. std::unordered_map<std::string, const RiverType*> riverInfoByName;
  145. std::unordered_map<std::string, const RiverType*> riverInfoByCode;
  146. std::unordered_map<RiverId, const RiverType*> riverInfoById;
  147. std::unordered_map<std::string, const RoadType*> roadInfoByName;
  148. std::unordered_map<std::string, const RoadType*> roadInfoByCode;
  149. std::unordered_map<RoadId, const RoadType*> roadInfoById;
  150. void initTerrains(const std::vector<std::string> & allConfigs);
  151. void initRivers(const std::vector<std::string> & allConfigs);
  152. void initRoads(const std::vector<std::string> & allConfigs);
  153. void recreateTerrainMaps();
  154. void recreateRiverMaps();
  155. void recreateRoadMaps();
  156. };
  157. VCMI_LIB_NAMESPACE_END