ObstacleSetHandler.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * ObstacleSetHandler.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 "ObstacleSetHandler.h"
  12. #include "../modding/IdentifierStorage.h"
  13. #include "../constants/StringConstants.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. ObstacleSet::ObstacleSet():
  16. type(INVALID),
  17. allowedTerrains({TerrainId::NONE})
  18. {
  19. }
  20. ObstacleSet::ObstacleSet(EObstacleType type, TerrainId terrain):
  21. type(type),
  22. allowedTerrains({terrain})
  23. {
  24. }
  25. void ObstacleSet::addObstacle(std::shared_ptr<const ObjectTemplate> obstacle)
  26. {
  27. obstacles.push_back(obstacle);
  28. }
  29. ObstacleSetFilter::ObstacleSetFilter(std::vector<ObstacleSet::EObstacleType> allowedTypes, TerrainId terrain = TerrainId::ANY_TERRAIN, EAlignment alignment = EAlignment::ANY):
  30. allowedTypes(allowedTypes),
  31. terrain(terrain),
  32. alignment(alignment)
  33. {
  34. }
  35. ObstacleSetFilter::ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain = TerrainId::ANY_TERRAIN, EAlignment alignment = EAlignment::ANY):
  36. allowedTypes({allowedType}),
  37. terrain(terrain),
  38. alignment(alignment)
  39. {
  40. }
  41. bool ObstacleSetFilter::filter(const ObstacleSet &set) const
  42. {
  43. if (terrain != TerrainId::ANY_TERRAIN && !vstd::contains(set.getTerrains(), terrain))
  44. {
  45. return false;
  46. }
  47. // TODO: Also check specific factions
  48. auto alignments = set.getAlignments();
  49. if (alignment != EAlignment::ANY && !alignments.empty() && !vstd::contains(alignments, alignment))
  50. {
  51. return false;
  52. }
  53. return true;
  54. }
  55. TerrainId ObstacleSetFilter::getTerrain() const
  56. {
  57. return terrain;
  58. }
  59. std::set<TerrainId> ObstacleSet::getTerrains() const
  60. {
  61. return allowedTerrains;
  62. }
  63. void ObstacleSet::setTerrain(TerrainId terrain)
  64. {
  65. this->allowedTerrains = {terrain};
  66. }
  67. void ObstacleSet::setTerrains(const std::set<TerrainId> & terrains)
  68. {
  69. this->allowedTerrains = terrains;
  70. }
  71. void ObstacleSet::addTerrain(TerrainId terrain)
  72. {
  73. this->allowedTerrains.insert(terrain);
  74. }
  75. void ObstacleSet::addAlignment(EAlignment alignment)
  76. {
  77. this->allowedAlignments.insert(alignment);
  78. }
  79. std::set<EAlignment> ObstacleSet::getAlignments() const
  80. {
  81. return allowedAlignments;
  82. }
  83. ObstacleSet::EObstacleType ObstacleSet::getType() const
  84. {
  85. return type;
  86. }
  87. void ObstacleSet::setType(EObstacleType type)
  88. {
  89. this->type = type;
  90. }
  91. std::vector<std::shared_ptr<const ObjectTemplate>> ObstacleSet::getObstacles() const
  92. {
  93. return obstacles;
  94. }
  95. ObstacleSet::EObstacleType ObstacleSetHandler::convertObstacleClass(MapObjectID id)
  96. {
  97. switch (id)
  98. {
  99. case Obj::MOUNTAIN:
  100. case Obj::VOLCANIC_VENT:
  101. case Obj::VOLCANO:
  102. case Obj::REEF:
  103. return ObstacleSet::MOUNTAINS;
  104. case Obj::OAK_TREES:
  105. case Obj::PINE_TREES:
  106. case Obj::TREES:
  107. case Obj::DEAD_VEGETATION:
  108. case Obj::HEDGE:
  109. case Obj::KELP:
  110. case Obj::WILLOW_TREES:
  111. case Obj::YUCCA_TREES:
  112. return ObstacleSet::TREES;
  113. case Obj::FROZEN_LAKE:
  114. case Obj::LAKE:
  115. case Obj::LAVA_FLOW:
  116. case Obj::LAVA_LAKE:
  117. return ObstacleSet::LAKES;
  118. case Obj::CANYON:
  119. case Obj::CRATER:
  120. case Obj::SAND_PIT:
  121. case Obj::TAR_PIT:
  122. return ObstacleSet::CRATERS;
  123. case Obj::HILL:
  124. case Obj::MOUND:
  125. case Obj::OUTCROPPING:
  126. case Obj::ROCK:
  127. case Obj::SAND_DUNE:
  128. case Obj::STALAGMITE:
  129. return ObstacleSet::ROCKS;
  130. case Obj::BUSH:
  131. case Obj::CACTUS:
  132. case Obj::FLOWERS:
  133. case Obj::MUSHROOMS:
  134. case Obj::LOG:
  135. case Obj::MANDRAKE:
  136. case Obj::MOSS:
  137. case Obj::PLANT:
  138. case Obj::SHRUB:
  139. case Obj::STUMP:
  140. case Obj::VINE:
  141. return ObstacleSet::PLANTS;
  142. case Obj::SKULL:
  143. return ObstacleSet::ANIMALS;
  144. default:
  145. return ObstacleSet::OTHER;
  146. }
  147. }
  148. ObstacleSet::EObstacleType ObstacleSet::typeFromString(const std::string &str)
  149. {
  150. static const std::map<std::string, EObstacleType> OBSTACLE_TYPE_NAMES =
  151. {
  152. {"mountain", MOUNTAINS},
  153. {"tree", TREES},
  154. {"lake", LAKES},
  155. {"crater", CRATERS},
  156. {"rock", ROCKS},
  157. {"plant", PLANTS},
  158. {"structure", STRUCTURES},
  159. {"animal", ANIMALS},
  160. {"other", OTHER}
  161. };
  162. if (OBSTACLE_TYPE_NAMES.find(str) != OBSTACLE_TYPE_NAMES.end())
  163. {
  164. return OBSTACLE_TYPE_NAMES.at(str);
  165. }
  166. // TODO: How to handle that?
  167. throw std::runtime_error("Invalid obstacle type: " + str);
  168. }
  169. std::string ObstacleSet::toString() const
  170. {
  171. static const std::map<EObstacleType, std::string> OBSTACLE_TYPE_STRINGS =
  172. {
  173. {MOUNTAINS, "mountain"},
  174. {TREES, "tree"},
  175. {LAKES, "lake"},
  176. {CRATERS, "crater"},
  177. {ROCKS, "rock"},
  178. {PLANTS, "plant"},
  179. {STRUCTURES, "structure"},
  180. {ANIMALS, "animal"},
  181. {OTHER, "other"}
  182. };
  183. return OBSTACLE_TYPE_STRINGS.at(type);
  184. }
  185. std::vector<ObstacleSet::EObstacleType> ObstacleSetFilter::getAllowedTypes() const
  186. {
  187. return allowedTypes;
  188. }
  189. void ObstacleSetFilter::setType(ObstacleSet::EObstacleType type)
  190. {
  191. allowedTypes = {type};
  192. }
  193. void ObstacleSetFilter::setTypes(std::vector<ObstacleSet::EObstacleType> types)
  194. {
  195. this->allowedTypes = types;
  196. }
  197. std::vector<JsonNode> ObstacleSetHandler::loadLegacyData()
  198. {
  199. return {};
  200. }
  201. void ObstacleSetHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  202. {
  203. auto os = loadFromJson(scope, data, name, biomes.size());
  204. if(os)
  205. {
  206. addObstacleSet(os);
  207. // TODO: Define some const array of object types ("biome" etc.)
  208. VLC->identifiersHandler->registerObject(scope, "biome", name, biomes.back()->id);
  209. }
  210. else
  211. {
  212. logMod->error("Failed to load obstacle set: %s", name);
  213. }
  214. }
  215. void ObstacleSetHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  216. {
  217. auto os = loadFromJson(scope, data, name, index);
  218. if(os)
  219. {
  220. addObstacleSet(os);
  221. VLC->identifiersHandler->registerObject(scope, "biome", name, biomes.at(index)->id);
  222. }
  223. else
  224. {
  225. logMod->error("Failed to load obstacle set: %s", name);
  226. }
  227. }
  228. std::shared_ptr<ObstacleSet> ObstacleSetHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index)
  229. {
  230. auto os = std::make_shared<ObstacleSet>();
  231. os->id = index;
  232. auto biome = json["biome"].Struct();
  233. os->setType(ObstacleSet::typeFromString(biome["objectType"].String()));
  234. if (biome["terrain"].isString())
  235. {
  236. auto terrainName = biome["terrain"].String();
  237. VLC->identifiers()->requestIdentifier(scope, "terrain", terrainName, [os](si32 id)
  238. {
  239. os->setTerrain(TerrainId(id));
  240. });
  241. }
  242. else // Other cases won't pass validation
  243. {
  244. auto terrains = biome["terrain"].Vector();
  245. for (const auto & terrain : terrains)
  246. {
  247. VLC->identifiers()->requestIdentifier(scope, "terrain", terrain.String(), [os](si32 id)
  248. {
  249. os->addTerrain(TerrainId(id));
  250. });
  251. }
  252. }
  253. // TODO: Move this parser to some utils
  254. auto parseAlignment = [](const std::string & str) ->EAlignment
  255. {
  256. int alignment = vstd::find_pos(GameConstants::ALIGNMENT_NAMES, str);
  257. if (alignment == -1)
  258. {
  259. logGlobal->error("Incorrect alignment: ", str);
  260. return EAlignment::ANY;
  261. }
  262. else
  263. {
  264. return static_cast<EAlignment>(alignment);
  265. }
  266. };
  267. if (json["alignment"].isString())
  268. {
  269. os->addAlignment(parseAlignment(json["alignment"].String()));
  270. }
  271. else if (json["alignment"].isVector())
  272. {
  273. auto alignments = json["alignment"].Vector();
  274. for (const auto & node : alignments)
  275. {
  276. os->addAlignment(parseAlignment(node.String()));
  277. }
  278. }
  279. auto templates = json["templates"].Vector();
  280. for (const auto & node : templates)
  281. {
  282. logGlobal->info("Registering obstacle template: %s in scope %s", node.String(), scope);
  283. auto identifier = boost::algorithm::to_lower_copy(node.String());
  284. auto jsonName = JsonNode(identifier);
  285. VLC->identifiers()->requestIdentifier(node.getModScope(), "obstacleTemplate", identifier, [this, os](si32 id)
  286. {
  287. logGlobal->info("Resolved obstacle id: %d", id);
  288. os->addObstacle(obstacleTemplates[id]);
  289. });
  290. }
  291. return os;
  292. }
  293. void ObstacleSetHandler::addTemplate(const std::string & scope, const std::string &name, std::shared_ptr<const ObjectTemplate> tmpl)
  294. {
  295. auto id = obstacleTemplates.size();
  296. auto strippedName = boost::algorithm::to_lower_copy(name);
  297. auto pos = strippedName.find(".def");
  298. if(pos != std::string::npos)
  299. strippedName.erase(pos, 4);
  300. if (VLC->identifiersHandler->getIdentifier(scope, "obstacleTemplate", strippedName, true))
  301. {
  302. logMod->warn("Duplicate obstacle template: %s", strippedName);
  303. return;
  304. }
  305. else
  306. {
  307. // Save by name
  308. VLC->identifiersHandler->registerObject(scope, "obstacleTemplate", strippedName, id);
  309. // Index by id
  310. obstacleTemplates[id] = tmpl;
  311. }
  312. }
  313. void ObstacleSetHandler::addObstacleSet(std::shared_ptr<ObstacleSet> os)
  314. {
  315. obstacleSets[os->getType()].push_back(os);
  316. biomes.push_back(os);
  317. }
  318. TObstacleTypes ObstacleSetHandler::getObstacles( const ObstacleSetFilter &filter) const
  319. {
  320. TObstacleTypes result;
  321. for (const auto &allowedType : filter.getAllowedTypes())
  322. {
  323. auto it = obstacleSets.find(allowedType);
  324. if(it != obstacleSets.end())
  325. {
  326. for (const auto &os : it->second)
  327. {
  328. if (filter.filter(*os))
  329. {
  330. result.push_back(os);
  331. }
  332. }
  333. }
  334. }
  335. return result;
  336. }
  337. VCMI_LIB_NAMESPACE_END