CObjectClassesHandler.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #pragma once
  2. #include "ObjectTemplate.h"
  3. #include "../GameConstants.h"
  4. #include "../ConstTransitivePtr.h"
  5. #include "../IHandlerBase.h"
  6. /*
  7. * CObjectClassesHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class JsonNode;
  16. class CRandomGenerator;
  17. /// Structure that describes placement rules for this object in random map
  18. struct RandomMapInfo
  19. {
  20. /// How valuable this object is, 1k = worthless, 10k = Utopia-level
  21. ui32 value;
  22. /// How many of such objects can be placed on map, 0 = object can not be placed by RMG
  23. ui32 mapLimit;
  24. /// How many of such objects can be placed in one zone, 0 = unplaceable
  25. ui32 zoneLimit;
  26. /// Rarity of object, 5 = extremely rare, 100 = common
  27. ui32 rarity;
  28. RandomMapInfo():
  29. value(0),
  30. mapLimit(0),
  31. zoneLimit(0),
  32. rarity(0)
  33. {}
  34. template <typename Handler> void serialize(Handler &h, const int version)
  35. {
  36. h & value & mapLimit & zoneLimit & rarity;
  37. }
  38. };
  39. class IObjectInfo
  40. {
  41. public:
  42. virtual bool givesResources() const = 0;
  43. virtual bool givesExperience() const = 0;
  44. virtual bool givesMana() const = 0;
  45. virtual bool givesMovement() const = 0;
  46. virtual bool givesPrimarySkills() const = 0;
  47. virtual bool givesSecondarySkills() const = 0;
  48. virtual bool givesArtifacts() const = 0;
  49. virtual bool givesCreatures() const = 0;
  50. virtual bool givesSpells() const = 0;
  51. virtual bool givesBonuses() const = 0;
  52. };
  53. class CGObjectInstance;
  54. class AObjectTypeHandler : public boost::noncopyable
  55. {
  56. RandomMapInfo rmgInfo;
  57. si32 type;
  58. si32 subtype;
  59. JsonNode base; /// describes base template
  60. std::vector<ObjectTemplate> templates;
  61. protected:
  62. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  63. /// initialization for classes that inherit this one
  64. virtual void initTypeData(const JsonNode & input);
  65. public:
  66. virtual ~AObjectTypeHandler(){}
  67. void setType(si32 type, si32 subtype);
  68. /// loads generic data from Json structure and passes it towards type-specific constructors
  69. void init(const JsonNode & input);
  70. void addTemplate(ObjectTemplate templ);
  71. void addTemplate(JsonNode config);
  72. /// returns all templates matching parameters
  73. std::vector<ObjectTemplate> getTemplates() const;
  74. std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
  75. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  76. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  77. boost::optional<ObjectTemplate> getOverride(si32 terrainType, const CGObjectInstance * object) const;
  78. const RandomMapInfo & getRMGInfo();
  79. virtual bool isStaticObject();
  80. virtual void afterLoadFinalization();
  81. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  82. /// to allow creating objects before game start (e.g. map loading)
  83. virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
  84. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  85. /// This should set remaining properties, including randomized or depending on map
  86. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  87. /// Returns object configuration, if available. Othervice returns NULL
  88. virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const = 0;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. {
  91. h & type & subtype & templates & rmgInfo;
  92. }
  93. };
  94. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  95. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  96. {
  97. /// Small internal structure that contains information on specific group of objects
  98. /// (creating separate entity is overcomplicating at least at this point)
  99. struct ObjectContainter
  100. {
  101. si32 id;
  102. std::string name; // human-readable name
  103. std::string handlerName; // ID of handler that controls this object, shoul be determined using hadlerConstructor map
  104. JsonNode base;
  105. std::map<si32, TObjectTypeHandler> objects;
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & name & handlerName & base & objects;
  109. }
  110. };
  111. typedef std::multimap<std::pair<si32, si32>, ObjectTemplate> TTemplatesContainer;
  112. /// list of object handlers, each of them handles only one type
  113. std::map<si32, ObjectContainter * > objects;
  114. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  115. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  116. /// container with H3 templates, used only during loading, no need to serialize it
  117. TTemplatesContainer legacyTemplates;
  118. void loadObjectEntry(const JsonNode & entry, ObjectContainter * obj);
  119. ObjectContainter * loadFromJson(const JsonNode & json);
  120. public:
  121. CObjectClassesHandler();
  122. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  123. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  124. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  125. void loadSubObject(std::string name, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
  126. void removeSubObject(si32 ID, si32 subID);
  127. void beforeValidate(JsonNode & object) override;
  128. void afterLoadFinalization() override;
  129. std::vector<bool> getDefaultAllowed() const override;
  130. /// Queries to detect loaded objects
  131. std::set<si32> knownObjects() const;
  132. std::set<si32> knownSubObjects(si32 primaryID) const;
  133. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  134. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  135. std::string getObjectName(si32 type) const;
  136. template <typename Handler> void serialize(Handler &h, const int version)
  137. {
  138. h & objects;
  139. }
  140. };