Terrain.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. initRivers(allConfigs);
  22. recreateRiverMaps();
  23. initRoads(allConfigs);
  24. recreateRoadMaps();
  25. initTerrains(allConfigs); //maps will be populated inside
  26. }
  27. void TerrainTypeHandler::initTerrains(const std::vector<std::string> & allConfigs)
  28. {
  29. std::vector<std::function<void()>> resolveLater;
  30. objects.resize(Terrain::ORIGINAL_TERRAIN_COUNT, nullptr); //make space for original terrains
  31. for(auto & mod : allConfigs)
  32. {
  33. if(!CResourceHandler::get(mod)->existsResource(ResourceID("config/terrains.json")))
  34. continue;
  35. JsonNode terrs(mod, ResourceID("config/terrains.json"));
  36. for(auto & terr : terrs.Struct())
  37. {
  38. auto * info = new TerrainType(terr.first); //set name
  39. info->moveCost = terr.second["moveCost"].Integer();
  40. const JsonVector &unblockedVec = terr.second["minimapUnblocked"].Vector();
  41. info->minimapUnblocked =
  42. {
  43. ui8(unblockedVec[0].Float()),
  44. ui8(unblockedVec[1].Float()),
  45. ui8(unblockedVec[2].Float())
  46. };
  47. const JsonVector &blockedVec = terr.second["minimapBlocked"].Vector();
  48. info->minimapBlocked =
  49. {
  50. ui8(blockedVec[0].Float()),
  51. ui8(blockedVec[1].Float()),
  52. ui8(blockedVec[2].Float())
  53. };
  54. info->musicFilename = terr.second["music"].String();
  55. info->tilesFilename = terr.second["tiles"].String();
  56. if(terr.second["type"].isNull())
  57. {
  58. info->passabilityType = TerrainType::PassabilityType::LAND | TerrainType::PassabilityType::SURFACE;
  59. }
  60. else if (terr.second["type"].getType() == JsonNode::JsonType::DATA_VECTOR)
  61. {
  62. for (const auto& node : terr.second["type"].Vector())
  63. {
  64. //Set bits
  65. auto s = node.String();
  66. if (s == "LAND") info->passabilityType |= TerrainType::PassabilityType::LAND;
  67. if (s == "WATER") info->passabilityType |= TerrainType::PassabilityType::WATER;
  68. if (s == "ROCK") info->passabilityType |= TerrainType::PassabilityType::ROCK;
  69. if (s == "SURFACE") info->passabilityType |= TerrainType::PassabilityType::SURFACE;
  70. if (s == "SUB") info->passabilityType |= TerrainType::PassabilityType::SUBTERRANEAN;
  71. }
  72. }
  73. else //should be string - one option only
  74. {
  75. auto s = terr.second["type"].String();
  76. if (s == "LAND") info->passabilityType = TerrainType::PassabilityType::LAND;
  77. if (s == "WATER") info->passabilityType = TerrainType::PassabilityType::WATER;
  78. if (s == "ROCK") info->passabilityType = TerrainType::PassabilityType::ROCK;
  79. if (s == "SURFACE") info->passabilityType = TerrainType::PassabilityType::SURFACE;
  80. if (s == "SUB") info->passabilityType = TerrainType::PassabilityType::SUBTERRANEAN;
  81. }
  82. if(terr.second["rockTerrain"].isNull())
  83. {
  84. info->rockTerrain = Terrain::ROCK;
  85. }
  86. else
  87. {
  88. auto rockTerrainName = terr.second["rockTerrain"].String();
  89. resolveLater.push_back([this, rockTerrainName, info]()
  90. {
  91. info->rockTerrain = getInfoByName(rockTerrainName)->id;
  92. });
  93. }
  94. if(terr.second["river"].isNull())
  95. {
  96. info->river = River::NO_RIVER;
  97. }
  98. else
  99. {
  100. info->river = getRiverByCode(terr.second["river"].String())->id;
  101. }
  102. if(terr.second["horseSoundId"].isNull())
  103. {
  104. info->horseSoundId = Terrain::ROCK; //rock sound as default
  105. }
  106. else
  107. {
  108. info->horseSoundId = terr.second["horseSoundId"].Float();
  109. }
  110. if(!terr.second["text"].isNull())
  111. {
  112. info->terrainText = terr.second["text"].String();
  113. }
  114. if(terr.second["code"].isNull())
  115. {
  116. info->typeCode = terr.first.substr(0, 2);
  117. }
  118. else
  119. {
  120. info->typeCode = terr.second["code"].String();
  121. assert(info->typeCode.length() == 2);
  122. }
  123. if(!terr.second["battleFields"].isNull())
  124. {
  125. for(auto & t : terr.second["battleFields"].Vector())
  126. {
  127. info->battleFields.emplace_back(t.String());
  128. }
  129. }
  130. if(!terr.second["prohibitTransitions"].isNull())
  131. {
  132. for(auto & t : terr.second["prohibitTransitions"].Vector())
  133. {
  134. std::string prohibitedTerrainName = t.String();
  135. resolveLater.push_back([this, prohibitedTerrainName, info]()
  136. {
  137. info->prohibitTransitions.emplace_back(getInfoByName(prohibitedTerrainName)->id);
  138. });
  139. }
  140. }
  141. info->transitionRequired = false;
  142. if(!terr.second["transitionRequired"].isNull())
  143. {
  144. info->transitionRequired = terr.second["transitionRequired"].Bool();
  145. }
  146. info->terrainViewPatterns = "normal";
  147. if(!terr.second["terrainViewPatterns"].isNull())
  148. {
  149. info->terrainViewPatterns = terr.second["terrainViewPatterns"].String();
  150. }
  151. TTerrainId id = Terrain::WRONG;
  152. if(!terr.second["originalTerrainId"].isNull())
  153. {
  154. //place in reserved slot
  155. id = (TTerrainId)(terr.second["originalTerrainId"].Float());
  156. objects[id] = info;
  157. }
  158. else
  159. {
  160. //append at the end
  161. id = objects.size();
  162. objects.push_back(info);
  163. }
  164. info->id = id;
  165. }
  166. }
  167. for (size_t i = Terrain::FIRST_REGULAR_TERRAIN; i < Terrain::ORIGINAL_TERRAIN_COUNT; i++)
  168. {
  169. //Make sure that original terrains are loaded
  170. assert(objects(i));
  171. }
  172. recreateTerrainMaps();
  173. for (auto& functor : resolveLater)
  174. {
  175. functor();
  176. }
  177. }
  178. void TerrainTypeHandler::initRivers(const std::vector<std::string> & allConfigs)
  179. {
  180. riverTypes.resize(River::ORIGINAL_RIVER_COUNT, nullptr); //make space for original rivers
  181. riverTypes[River::NO_RIVER] = new RiverType(); //default
  182. for (auto & mod : allConfigs)
  183. {
  184. if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/rivers.json")))
  185. continue;
  186. JsonNode rivs(mod, ResourceID("config/rivers.json"));
  187. for (auto & river : rivs.Struct())
  188. {
  189. auto * info = new RiverType();
  190. info->fileName = river.second["animation"].String();
  191. info->code = river.second["code"].String();
  192. info->deltaName = river.second["delta"].String();
  193. //info->movementCost = river.second["moveCost"].Integer();
  194. if (!river.second["originalRiverId"].isNull())
  195. {
  196. info->id = static_cast<TRiverId>(river.second["originalRiverId"].Float());
  197. riverTypes[info->id] = info;
  198. }
  199. else
  200. {
  201. info->id = riverTypes.size();
  202. riverTypes.push_back(info);
  203. }
  204. }
  205. }
  206. recreateRiverMaps();
  207. }
  208. void TerrainTypeHandler::initRoads(const std::vector<std::string> & allConfigs)
  209. {
  210. roadTypes.resize(Road::ORIGINAL_ROAD_COUNT, nullptr); //make space for original rivers
  211. roadTypes[Road::NO_ROAD] = new RoadType(); //default
  212. for (auto & mod : allConfigs)
  213. {
  214. if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/roads.json")))
  215. continue;
  216. JsonNode rds(mod, ResourceID("config/roads.json"));
  217. for (auto & road : rds.Struct())
  218. {
  219. auto * info = new RoadType();
  220. info->fileName = road.second["animation"].String();
  221. info->code = road.second["code"].String();
  222. info->movementCost = static_cast<ui8>(road.second["moveCost"].Float());
  223. if (!road.second["originalRoadId"].isNull())
  224. {
  225. info->id = static_cast<TRoadId>(road.second["originalRoadId"].Float());
  226. roadTypes[info->id] = info;
  227. }
  228. else
  229. {
  230. info->id = roadTypes.size();
  231. roadTypes.push_back(info);
  232. }
  233. }
  234. }
  235. recreateRoadMaps();
  236. }
  237. void TerrainTypeHandler::recreateTerrainMaps()
  238. {
  239. for (const TerrainType * terrainInfo : objects)
  240. {
  241. terrainInfoByName[terrainInfo->name] = terrainInfo;
  242. terrainInfoByCode[terrainInfo->typeCode] = terrainInfo;
  243. terrainInfoById[terrainInfo->id] = terrainInfo;
  244. }
  245. }
  246. void TerrainTypeHandler::recreateRiverMaps()
  247. {
  248. for (const RiverType * riverInfo : riverTypes)
  249. {
  250. if (riverInfo->id == River::NO_RIVER)
  251. continue;
  252. riverInfoByName[riverInfo->fileName] = riverInfo;
  253. riverInfoByCode[riverInfo->code] = riverInfo;
  254. riverInfoById[riverInfo->id] = riverInfo;
  255. }
  256. }
  257. void TerrainTypeHandler::recreateRoadMaps()
  258. {
  259. for (const RoadType * roadInfo : roadTypes)
  260. {
  261. if (roadInfo->id == Road::NO_ROAD)
  262. continue;
  263. roadInfoByName[roadInfo->fileName] = roadInfo;
  264. roadInfoByCode[roadInfo->code] = roadInfo;
  265. roadInfoById[roadInfo->id] = roadInfo;
  266. }
  267. }
  268. const std::vector<TerrainType *> & TerrainTypeHandler::terrains() const
  269. {
  270. return objects;
  271. }
  272. const std::vector<RiverType*>& TerrainTypeHandler::rivers() const
  273. {
  274. return riverTypes;
  275. }
  276. const std::vector<RoadType*>& TerrainTypeHandler::roads() const
  277. {
  278. return roadTypes;
  279. }
  280. const TerrainType* TerrainTypeHandler::getInfoByName(const std::string& terrainName) const
  281. {
  282. return terrainInfoByName.at(terrainName);
  283. }
  284. const TerrainType* TerrainTypeHandler::getInfoByCode(const std::string& terrainCode) const
  285. {
  286. return terrainInfoByCode.at(terrainCode);
  287. }
  288. const TerrainType* TerrainTypeHandler::getInfoById(TTerrainId id) const
  289. {
  290. return terrainInfoById.at(id);
  291. }
  292. const RiverType* TerrainTypeHandler::getRiverByName(const std::string& riverName) const
  293. {
  294. return riverInfoByName.at(riverName);
  295. }
  296. const RiverType* TerrainTypeHandler::getRiverByCode(const std::string& riverCode) const
  297. {
  298. return riverInfoByCode.at(riverCode);
  299. }
  300. const RiverType* TerrainTypeHandler::getRiverById(TRiverId id) const
  301. {
  302. return riverInfoById.at(id);
  303. }
  304. const RoadType* TerrainTypeHandler::getRoadByName(const std::string& roadName) const
  305. {
  306. return roadInfoByName.at(roadName);
  307. }
  308. const RoadType* TerrainTypeHandler::getRoadByCode(const std::string& roadCode) const
  309. {
  310. return roadInfoByCode.at(roadCode);
  311. }
  312. const RoadType* TerrainTypeHandler::getRoadById(TRoadId id) const
  313. {
  314. return roadInfoById.at(id);
  315. }
  316. std::ostream & operator<<(std::ostream & os, const TerrainType & terrainType)
  317. {
  318. return os << static_cast<const std::string &>(terrainType);
  319. }
  320. TerrainType::operator std::string() const
  321. {
  322. return name;
  323. }
  324. TerrainType::TerrainType(const std::string& _name):
  325. name(_name),
  326. id(Terrain::WRONG),
  327. rockTerrain(Terrain::ROCK),
  328. moveCost(100),
  329. horseSoundId(0),
  330. passabilityType(0),
  331. transitionRequired(false)
  332. {
  333. }
  334. TerrainType& TerrainType::operator=(const TerrainType & other)
  335. {
  336. //TODO
  337. name = other.name;
  338. return *this;
  339. }
  340. bool TerrainType::operator==(const TerrainType& other)
  341. {
  342. return id == other.id;
  343. }
  344. bool TerrainType::operator!=(const TerrainType& other)
  345. {
  346. return id != other.id;
  347. }
  348. bool TerrainType::operator<(const TerrainType& other)
  349. {
  350. return id < other.id;
  351. }
  352. bool TerrainType::isLand() const
  353. {
  354. return !isWater();
  355. }
  356. bool TerrainType::isWater() const
  357. {
  358. return passabilityType & PassabilityType::WATER;
  359. }
  360. bool TerrainType::isPassable() const
  361. {
  362. return !(passabilityType & PassabilityType::ROCK);
  363. }
  364. bool TerrainType::isSurface() const
  365. {
  366. return passabilityType & PassabilityType::SURFACE;
  367. }
  368. bool TerrainType::isUnderground() const
  369. {
  370. return passabilityType & PassabilityType::SUBTERRANEAN;
  371. }
  372. bool TerrainType::isTransitionRequired() const
  373. {
  374. return transitionRequired;
  375. }
  376. RiverType::RiverType(const std::string & fileName, const std::string & code, TRiverId id):
  377. fileName(fileName),
  378. code(code),
  379. id(id)
  380. {
  381. }
  382. RoadType::RoadType(const std::string& fileName, const std::string& code, TRoadId id):
  383. fileName(fileName),
  384. code(code),
  385. id(id)
  386. {
  387. }