Terrain.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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); //set name
  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 = Terrain::ROCK;
  63. }
  64. else
  65. {
  66. auto rockTerrainName = terr.second["rockTerrain"].String();
  67. resolveLater.push_back([this, rockTerrainName, info]()
  68. {
  69. info->rockTerrain = getInfoByName(rockTerrainName)->id;
  70. });
  71. }
  72. if(terr.second["river"].isNull())
  73. {
  74. info->river = RIVER_NAMES[0];
  75. }
  76. else
  77. {
  78. info->river = terr.second["river"].String();
  79. }
  80. if(terr.second["horseSoundId"].isNull())
  81. {
  82. info->horseSoundId = 9; //rock sound as default
  83. }
  84. else
  85. {
  86. info->horseSoundId = terr.second["horseSoundId"].Float();
  87. }
  88. if(!terr.second["text"].isNull())
  89. {
  90. info->terrainText = terr.second["text"].String();
  91. }
  92. if(terr.second["code"].isNull())
  93. {
  94. info->typeCode = terr.first.substr(0, 2);
  95. }
  96. else
  97. {
  98. info->typeCode = terr.second["code"].String();
  99. assert(info->typeCode.length() == 2);
  100. }
  101. if(!terr.second["battleFields"].isNull())
  102. {
  103. for(auto & t : terr.second["battleFields"].Vector())
  104. {
  105. info->battleFields.emplace_back(t.String());
  106. }
  107. }
  108. if(!terr.second["prohibitTransitions"].isNull())
  109. {
  110. for(auto & t : terr.second["prohibitTransitions"].Vector())
  111. {
  112. std::string prohibitedTerrainName = t.String();
  113. resolveLater.push_back([this, prohibitedTerrainName, info]()
  114. {
  115. info->prohibitTransitions.emplace_back(getInfoByName(prohibitedTerrainName)->id);
  116. });
  117. }
  118. }
  119. info->transitionRequired = false;
  120. if(!terr.second["transitionRequired"].isNull())
  121. {
  122. info->transitionRequired = terr.second["transitionRequired"].Bool();
  123. }
  124. info->terrainViewPatterns = "normal";
  125. if(!terr.second["terrainViewPatterns"].isNull())
  126. {
  127. info->terrainViewPatterns = terr.second["terrainViewPatterns"].String();
  128. }
  129. TTerrain id = Terrain::WRONG;
  130. if(!terr.second["originalTerrainId"].isNull())
  131. {
  132. //place in reserved slot
  133. id = (TTerrain)(terr.second["originalTerrainId"].Float());
  134. objects[id] = info;
  135. }
  136. else
  137. {
  138. //append at the end
  139. id = objects.size();
  140. objects.push_back(info);
  141. }
  142. info->id = id;
  143. }
  144. }
  145. for (size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
  146. {
  147. //Make sure that original terrains are loaded
  148. assert(objects(i));
  149. }
  150. recreateTerrainMaps();
  151. for (auto& functor : resolveLater)
  152. {
  153. functor();
  154. }
  155. }
  156. void TerrainTypeHandler::recreateTerrainMaps()
  157. {
  158. for (const TerrainType * terrainInfo : objects)
  159. {
  160. terrainInfoByName[terrainInfo->name] = terrainInfo;
  161. terrainInfoByCode[terrainInfo->typeCode] = terrainInfo;
  162. terrainInfoById[terrainInfo->id] = terrainInfo;
  163. }
  164. }
  165. const std::vector<TerrainType *> & TerrainTypeHandler::terrains() const
  166. {
  167. return objects;
  168. }
  169. const TerrainType* TerrainTypeHandler::getInfoByName(const std::string& terrainName) const
  170. {
  171. return terrainInfoByName.at(terrainName);
  172. }
  173. const TerrainType* TerrainTypeHandler::getInfoByCode(const std::string& terrainCode) const
  174. {
  175. return terrainInfoByCode.at(terrainCode);
  176. }
  177. const TerrainType* TerrainTypeHandler::getInfoById(TTerrain id) const
  178. {
  179. return terrainInfoById.at(id);
  180. }
  181. std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType)
  182. {
  183. return os << static_cast<const std::string &>(terrainType);
  184. }
  185. TerrainType::operator std::string() const
  186. {
  187. return name;
  188. }
  189. TerrainType::TerrainType(const std::string& _name):
  190. name(_name),
  191. id(Terrain::WRONG),
  192. rockTerrain(Terrain::ROCK),
  193. moveCost(100),
  194. horseSoundId(0),
  195. passabilityType(PassabilityType::LAND),
  196. transitionRequired(false)
  197. {
  198. }
  199. TerrainType& TerrainType::operator=(const TerrainType & other)
  200. {
  201. //TODO
  202. name = other.name;
  203. return *this;
  204. }
  205. bool TerrainType::operator==(const TerrainType& other)
  206. {
  207. return id == other.id;
  208. }
  209. bool TerrainType::operator!=(const TerrainType& other)
  210. {
  211. return id != other.id;
  212. }
  213. bool TerrainType::operator<(const TerrainType& other)
  214. {
  215. return id < other.id;
  216. }
  217. bool TerrainType::isLand() const
  218. {
  219. return !isWater();
  220. }
  221. bool TerrainType::isWater() const
  222. {
  223. return passabilityType == PassabilityType::WATER;
  224. }
  225. bool TerrainType::isPassable() const
  226. {
  227. return passabilityType != PassabilityType::ROCK;
  228. }
  229. bool TerrainType::isUnderground() const
  230. {
  231. return passabilityType != PassabilityType::SUBTERRANEAN;
  232. }
  233. bool TerrainType::isTransitionRequired() const
  234. {
  235. return transitionRequired;
  236. }