Terrain.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Terrain.cpp, 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. #include "StdInc.h"
  11. #include "Terrain.h"
  12. #include "VCMI_Lib.h"
  13. #include "CModHandler.h"
  14. //regular expression to change id for string at config
  15. //("allowedTerrain"\s*:\s*\[.*)9(.*\],\n)
  16. //\1"rock"\2
  17. TerrainTypeHandler::TerrainTypeHandler()
  18. {
  19. auto allConfigs = VLC->modh->getActiveMods();
  20. allConfigs.insert(allConfigs.begin(), "core");
  21. std::vector<std::function<void()>> resolveLater;
  22. objects.resize(Terrain::ORIGINAL_TERRAIN_COUNT, nullptr); //make space for original terrains
  23. for(auto & mod : allConfigs)
  24. {
  25. if(!CResourceHandler::get(mod)->existsResource(ResourceID("config/terrains.json")))
  26. continue;
  27. JsonNode terrs(mod, ResourceID("config/terrains.json"));
  28. for(auto & terr : terrs.Struct())
  29. {
  30. auto * info = new TerrainType(terr.first);
  31. info->moveCost = terr.second["moveCost"].Integer();
  32. const JsonVector &unblockedVec = terr.second["minimapUnblocked"].Vector();
  33. info->minimapUnblocked =
  34. {
  35. ui8(unblockedVec[0].Float()),
  36. ui8(unblockedVec[1].Float()),
  37. ui8(unblockedVec[2].Float())
  38. };
  39. const JsonVector &blockedVec = terr.second["minimapBlocked"].Vector();
  40. info->minimapBlocked =
  41. {
  42. ui8(blockedVec[0].Float()),
  43. ui8(blockedVec[1].Float()),
  44. ui8(blockedVec[2].Float())
  45. };
  46. info->musicFilename = terr.second["music"].String();
  47. info->tilesFilename = terr.second["tiles"].String();
  48. if(terr.second["type"].isNull())
  49. {
  50. info->passabilityType = TerrainType::PassabilityType::LAND;
  51. }
  52. else
  53. {
  54. auto s = terr.second["type"].String();
  55. if(s == "LAND") info->passabilityType = TerrainType::PassabilityType::LAND;
  56. if(s == "WATER") info->passabilityType = TerrainType::PassabilityType::WATER;
  57. if(s == "ROCK") info->passabilityType = TerrainType::PassabilityType::ROCK;
  58. if(s == "SUB") info->passabilityType = TerrainType::PassabilityType::SUBTERRANEAN;
  59. }
  60. if(terr.second["rockTerrain"].isNull())
  61. {
  62. info->rockTerrain = "rock";
  63. }
  64. else
  65. {
  66. info->rockTerrain = terr.second["rockTerrain"].String();
  67. }
  68. if(terr.second["river"].isNull())
  69. {
  70. info->river = RIVER_NAMES[0];
  71. }
  72. else
  73. {
  74. info->river = terr.second["river"].String();
  75. }
  76. if(terr.second["horseSoundId"].isNull())
  77. {
  78. info->horseSoundId = 9; //rock sound as default
  79. }
  80. else
  81. {
  82. info->horseSoundId = terr.second["horseSoundId"].Integer();
  83. }
  84. if(!terr.second["text"].isNull())
  85. {
  86. info->terrainText = terr.second["text"].String();
  87. }
  88. if(terr.second["code"].isNull())
  89. {
  90. info->typeCode = terr.first.substr(0, 2);
  91. }
  92. else
  93. {
  94. info->typeCode = terr.second["code"].String();
  95. assert(info->typeCode.length() == 2);
  96. }
  97. if(!terr.second["battleFields"].isNull())
  98. {
  99. for(auto & t : terr.second["battleFields"].Vector())
  100. {
  101. info->battleFields.emplace_back(t.String());
  102. }
  103. }
  104. if(!terr.second["prohibitTransitions"].isNull())
  105. {
  106. for(auto & t : terr.second["prohibitTransitions"].Vector())
  107. {
  108. info->prohibitTransitions.emplace_back(t.String());
  109. }
  110. }
  111. info->transitionRequired = false;
  112. if(!terr.second["transitionRequired"].isNull())
  113. {
  114. info->transitionRequired = terr.second["transitionRequired"].Bool();
  115. }
  116. info->terrainViewPatterns = "normal";
  117. if(!terr.second["terrainViewPatterns"].isNull())
  118. {
  119. info->terrainViewPatterns = terr.second["terrainViewPatterns"].String();
  120. }
  121. //TODO: handle 10 origina terrains
  122. terrainInfoByName[terr.first] = info;
  123. terrainInfoByCode[info->typeCode] = info;
  124. terrainInfoById[info->id] = info;
  125. TTerrain id;
  126. if(!terr.second["originalTerrainId"].isNull())
  127. {
  128. //place in reserved slot
  129. id = (TTerrain)(terr.second["originalTerrainId"].Float());
  130. objects[id] = info;
  131. }
  132. else
  133. {
  134. //append at the end
  135. id = objects.size();
  136. objects.push_back(info);
  137. }
  138. }
  139. }
  140. for (size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
  141. {
  142. //Make sure that original terrains are loaded
  143. assert(objects(i));
  144. }
  145. //TODO: add ids to resolve
  146. for (auto& functor : resolveLater)
  147. {
  148. functor();
  149. }
  150. }
  151. const std::vector<TerrainType *> & TerrainTypeHandler::terrains()
  152. {
  153. return objects;
  154. }
  155. const TerrainType* TerrainTypeHandler::getInfoByName(const std::string& terrainName) const
  156. {
  157. }
  158. const TerrainType* TerrainTypeHandler::getInfoByCode(const std::string& terrainName) const
  159. {
  160. }
  161. const TerrainType* TerrainTypeHandler::getInfoById(TTerrain id) const
  162. {
  163. }
  164. std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType)
  165. {
  166. return os << static_cast<const std::string &>(terrainType);
  167. }
  168. TerrainType::operator std::string() const
  169. {
  170. return name;
  171. }
  172. TerrainType::TerrainType(const std::string & _name) : name(_name)
  173. {}
  174. TerrainType& TerrainType::operator=(const TerrainType & other)
  175. {
  176. //TODO
  177. name = other.name;
  178. return *this;
  179. }
  180. bool TerrainType::operator==(const TerrainType& other)
  181. {
  182. return id == other.id;
  183. }
  184. bool TerrainType::operator!=(const TerrainType& other)
  185. {
  186. return id != other.id;
  187. }
  188. bool TerrainType::operator<(const TerrainType& other)
  189. {
  190. return id < other.id;
  191. }
  192. bool TerrainType::isLand() const
  193. {
  194. return !isWater();
  195. }
  196. bool TerrainType::isWater() const
  197. {
  198. return passabilityType == PassabilityType::WATER;
  199. }
  200. bool TerrainType::isPassable() const
  201. {
  202. return passabilityType != PassabilityType::ROCK;
  203. }
  204. bool TerrainType::isUnderground() const
  205. {
  206. return passabilityType != PassabilityType::SUBTERRANEAN;
  207. }
  208. bool TerrainType::isTransitionRequired() const
  209. {
  210. return transitionRequired;
  211. }