ObstacleSetHandler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, // Inluding 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. enum EMapLevel // TODO: Move somewhere to map definitions
  35. {
  36. ANY = -1,
  37. SURFACE = 0,
  38. UNDERGROUND = 1
  39. };
  40. ObstacleSet();
  41. explicit ObstacleSet(EObstacleType type, TerrainId terrain);
  42. void addObstacle(std::shared_ptr<const ObjectTemplate> obstacle);
  43. void removeEmptyTemplates();
  44. std::vector<std::shared_ptr<const ObjectTemplate>> getObstacles() const;
  45. EObstacleType getType() const;
  46. void setType(EObstacleType type);
  47. std::set<TerrainId> getTerrains() const;
  48. void setTerrain(TerrainId terrain);
  49. void setTerrains(const std::set<TerrainId> & terrains);
  50. void addTerrain(TerrainId terrain);
  51. EMapLevel getLevel() const;
  52. void setLevel(EMapLevel level);
  53. std::set<EAlignment> getAlignments() const;
  54. void addAlignment(EAlignment alignment);
  55. std::set<FactionID> getFactions() const;
  56. void addFaction(FactionID faction);
  57. static EObstacleType typeFromString(const std::string &str);
  58. std::string toString() const;
  59. static EMapLevel levelFromString(const std::string &str);
  60. si32 id;
  61. private:
  62. EObstacleType type;
  63. EMapLevel level;
  64. std::set<TerrainId> allowedTerrains; // Empty means all terrains
  65. std::set<FactionID> allowedFactions; // Empty means all factions
  66. std::set<EAlignment> allowedAlignments; // Empty means all alignments
  67. std::vector<std::shared_ptr<const ObjectTemplate>> obstacles;
  68. };
  69. typedef std::vector<std::shared_ptr<ObstacleSet>> TObstacleTypes;
  70. class DLL_LINKAGE ObstacleSetFilter
  71. {
  72. public:
  73. ObstacleSetFilter(ObstacleSet::EObstacleType allowedType, TerrainId terrain, ObstacleSet::EMapLevel level, FactionID faction, EAlignment alignment);
  74. ObstacleSetFilter(std::vector<ObstacleSet::EObstacleType> allowedTypes, TerrainId terrain, ObstacleSet::EMapLevel level, FactionID faction, EAlignment alignment);
  75. bool filter(const ObstacleSet &set) const;
  76. void setType(ObstacleSet::EObstacleType type);
  77. void setTypes(std::vector<ObstacleSet::EObstacleType> types);
  78. std::vector<ObstacleSet::EObstacleType> getAllowedTypes() const;
  79. TerrainId getTerrain() const;
  80. void setAlignment(EAlignment alignment);
  81. private:
  82. std::vector<ObstacleSet::EObstacleType> allowedTypes;
  83. FactionID faction;
  84. EAlignment alignment;
  85. // TODO: Filter by faction, surface/underground, etc.
  86. const TerrainId terrain;
  87. ObstacleSet::EMapLevel level;
  88. };
  89. // TODO: Instantiate ObstacleSetHandler
  90. class DLL_LINKAGE ObstacleSetHandler : public IHandlerBase, boost::noncopyable
  91. {
  92. public:
  93. ObstacleSetHandler() = default;
  94. ~ObstacleSetHandler() = default;
  95. std::vector<JsonNode> loadLegacyData() override;
  96. virtual void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  97. virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  98. std::shared_ptr<ObstacleSet> loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name, size_t index);
  99. ObstacleSet::EObstacleType convertObstacleClass(MapObjectID id);
  100. // TODO: Populate obstacleSets with all the obstacle sets from the game data
  101. void addTemplate(const std::string & scope, const std::string &name, std::shared_ptr<const ObjectTemplate> tmpl);
  102. void addObstacleSet(std::shared_ptr<ObstacleSet> set);
  103. void afterLoadFinalization() override;
  104. TObstacleTypes getObstacles(const ObstacleSetFilter &filter) const;
  105. private:
  106. std::vector< std::shared_ptr<ObstacleSet> > biomes;
  107. // TODO: Serialize?
  108. std::map<si32, std::shared_ptr<const ObjectTemplate>> obstacleTemplates;
  109. // FIXME: Store pointers?
  110. std::map<ObstacleSet::EObstacleType, std::vector<std::shared_ptr<ObstacleSet>>> obstacleSets;
  111. };
  112. VCMI_LIB_NAMESPACE_END