Terrain.cpp 7.2 KB

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