ObstacleSetHandler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * ObstacleSetHandler.h, 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. #pragma once
  11. #include "../GameConstants.h"
  12. #include "../constants/EntityIdentifiers.h"
  13. #include "../IHandlerBase.h"
  14. #include "../json/JsonNode.h"
  15. #include "ObjectTemplate.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class DLL_LINKAGE ObstacleSet
  18. {
  19. public:
  20. // TODO: Create string constants for these
  21. enum EObstacleType
  22. {
  23. INVALID = -1,
  24. MOUNTAINS = 0,
  25. TREES,
  26. LAKES, // Including dry or lava lakes
  27. CRATERS, // Chasms, Canyons, etc.
  28. ROCKS,
  29. PLANTS, // Flowers, cacti, mushrooms, logs, shrubs, etc.
  30. STRUCTURES, // Buildings, ruins, etc.
  31. ANIMALS, // Living, or bones
  32. OTHER // Crystals, shipwrecks, barrels, etc.
  33. };
  34. ObstacleSet();
  35. explicit ObstacleSet(EObstacleType type, TerrainId terrain);
  36. void addObstacle(std::shared_ptr<const ObjectTemplate> obstacle);
  37. void removeEmptyTemplates();
  38. std::vector<std::shared_ptr<const ObjectTemplate>> getObstacles() const;
  39. EObstacleType getType() const;
  40. void setType(EObstacleType type);
  41. std::set<TerrainId> getTerrains() const;
  42. void setTerrain(TerrainId terrain);
  43. void setTerrains(const std::set<TerrainId> & terrains);
  44. void addTerrain(TerrainId terrain);
  45. EMapLevel getLevel() const;
  46. void setLevel(EMapLevel level);
  47. std::set<EAlignment> getAlignments() const;
  48. void addAlignment(EAlignment alignment);
  49. std::set<FactionID> getFactions() const;
  50. void addFaction(FactionID faction);
  51. static EObstacleType typeFromString(const std::string &str);
  52. std::string toString() const;
  53. static EMapLevel levelFromString(const std::string &str);
  54. si32 id;
  55. private:
  56. EObstacleType type;
  57. EMapLevel level;
  58. std::set<TerrainId> allowedTerrains; // Empty means all terrains
  59. std::set<FactionID> allowedFactions; // Empty means all factions
  60. std::set<EAlignment> allowedAlignments; // Empty means all alignments
  61. std::vector<std::shared_ptr<const ObjectTemplate>> obstacles;
  62. };
  63. using TObstacleTypes = std::vector<std::shared_ptr<ObstacleSet>>;
  64. class DLL_LINKAGE ObstacleSetFilter
  65. {
  66. public:
  67. ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain, EMapLevel level, FactionID faction, EAlignment alignment);
  68. ObstacleSetFilter(std::vector<ObstacleSet::EObstacleType> allowedTypes, TerrainId terrain, EMapLevel level, FactionID faction, EAlignment alignment);
  69. bool filter(const ObstacleSet &set) const;
  70. void setType(ObstacleSet::EObstacleType type);
  71. void setTypes(const std::vector<ObstacleSet::EObstacleType> & types);
  72. std::vector<ObstacleSet::EObstacleType> getAllowedTypes() const;
  73. TerrainId getTerrain() const;
  74. void setAlignment(EAlignment alignment);
  75. private:
  76. std::vector<ObstacleSet::EObstacleType> allowedTypes;
  77. FactionID faction;
  78. EAlignment alignment;
  79. // TODO: Filter by faction, surface/underground, etc.
  80. const TerrainId terrain;
  81. EMapLevel level;
  82. };
  83. // TODO: Instantiate ObstacleSetHandler
  84. class DLL_LINKAGE ObstacleSetHandler : public IHandlerBase, boost::noncopyable
  85. {
  86. public:
  87. ObstacleSetHandler() = default;
  88. ~ObstacleSetHandler() = default;
  89. std::vector<JsonNode> loadLegacyData() override;
  90. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  91. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  92. std::shared_ptr<ObstacleSet> loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index);
  93. ObstacleSet::EObstacleType convertObstacleClass(MapObjectID id);
  94. // TODO: Populate obstacleSets with all the obstacle sets from the game data
  95. void addTemplate(const std::string & scope, const std::string &name, std::shared_ptr<const ObjectTemplate> tmpl);
  96. void addObstacleSet(std::shared_ptr<ObstacleSet> set);
  97. void afterLoadFinalization() override;
  98. TObstacleTypes getObstacles(const ObstacleSetFilter &filter) const;
  99. private:
  100. std::vector< std::shared_ptr<ObstacleSet> > biomes;
  101. // TODO: Serialize?
  102. std::map<si32, std::shared_ptr<const ObjectTemplate>> obstacleTemplates;
  103. // FIXME: Store pointers?
  104. std::map<ObstacleSet::EObstacleType, std::vector<std::shared_ptr<ObstacleSet>>> obstacleSets;
  105. };
  106. VCMI_LIB_NAMESPACE_END