Terrain.cpp 11 KB

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