CObjectClassesHandler.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * CObjectClassesHandler.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 "ObjectTemplate.h"
  12. #include "../GameConstants.h"
  13. #include "../ConstTransitivePtr.h"
  14. #include "../IHandlerBase.h"
  15. #include "../JsonNode.h"
  16. class JsonNode;
  17. class CRandomGenerator;
  18. /// Structure that describes placement rules for this object in random map
  19. struct DLL_LINKAGE RandomMapInfo
  20. {
  21. /// How valuable this object is, 1k = worthless, 10k = Utopia-level
  22. ui32 value;
  23. /// How many of such objects can be placed on map, 0 = object can not be placed by RMG
  24. ui32 mapLimit;
  25. /// How many of such objects can be placed in one zone, 0 = unplaceable
  26. ui32 zoneLimit;
  27. /// Rarity of object, 5 = extremely rare, 100 = common
  28. ui32 rarity;
  29. RandomMapInfo():
  30. value(0),
  31. mapLimit(0),
  32. zoneLimit(0),
  33. rarity(0)
  34. {}
  35. template <typename Handler> void serialize(Handler &h, const int version)
  36. {
  37. h & value;
  38. h & mapLimit;
  39. h & zoneLimit;
  40. h & rarity;
  41. }
  42. };
  43. class DLL_LINKAGE IObjectInfo
  44. {
  45. public:
  46. struct CArmyStructure
  47. {
  48. ui32 totalStrength;
  49. ui32 shootersStrength;
  50. ui32 flyersStrength;
  51. ui32 walkersStrength;
  52. CArmyStructure() :
  53. totalStrength(0),
  54. shootersStrength(0),
  55. flyersStrength(0),
  56. walkersStrength(0)
  57. {}
  58. bool operator <(const CArmyStructure & other) const
  59. {
  60. return this->totalStrength < other.totalStrength;
  61. }
  62. };
  63. /// Returns possible composition of guards. Actual guards would be
  64. /// somewhere between these two values
  65. virtual CArmyStructure minGuards() const { return CArmyStructure(); }
  66. virtual CArmyStructure maxGuards() const { return CArmyStructure(); }
  67. virtual bool givesResources() const { return false; }
  68. virtual bool givesExperience() const { return false; }
  69. virtual bool givesMana() const { return false; }
  70. virtual bool givesMovement() const { return false; }
  71. virtual bool givesPrimarySkills() const { return false; }
  72. virtual bool givesSecondarySkills() const { return false; }
  73. virtual bool givesArtifacts() const { return false; }
  74. virtual bool givesCreatures() const { return false; }
  75. virtual bool givesSpells() const { return false; }
  76. virtual bool givesBonuses() const { return false; }
  77. virtual ~IObjectInfo() = default;
  78. };
  79. class CGObjectInstance;
  80. class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
  81. {
  82. RandomMapInfo rmgInfo;
  83. /// Human-readable name of this object, used for objects like banks and dwellings, if set
  84. boost::optional<std::string> objectName;
  85. JsonNode base; /// describes base template
  86. std::vector<ObjectTemplate> templates;
  87. protected:
  88. void preInitObject(CGObjectInstance * obj) const;
  89. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  90. /// initialization for classes that inherit this one
  91. virtual void initTypeData(const JsonNode & input);
  92. public:
  93. std::string typeName;
  94. std::string subTypeName;
  95. si32 type;
  96. si32 subtype;
  97. AObjectTypeHandler();
  98. virtual ~AObjectTypeHandler();
  99. void setType(si32 type, si32 subtype);
  100. void setTypeName(std::string type, std::string subtype);
  101. /// loads generic data from Json structure and passes it towards type-specific constructors
  102. void init(const JsonNode & input, boost::optional<std::string> name = boost::optional<std::string>());
  103. /// Returns object-specific name, if set
  104. boost::optional<std::string> getCustomName() const;
  105. void addTemplate(const ObjectTemplate & templ);
  106. void addTemplate(JsonNode config);
  107. /// returns all templates matching parameters
  108. std::vector<ObjectTemplate> getTemplates() const;
  109. std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
  110. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  111. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  112. boost::optional<ObjectTemplate> getOverride(si32 terrainType, const CGObjectInstance * object) const;
  113. const RandomMapInfo & getRMGInfo();
  114. virtual bool isStaticObject();
  115. virtual void afterLoadFinalization();
  116. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  117. /// to allow creating objects before game start (e.g. map loading)
  118. virtual CGObjectInstance * create(const ObjectTemplate & tmpl) const = 0;
  119. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  120. /// This should set remaining properties, including randomized or depending on map
  121. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  122. /// Returns object configuration, if available. Otherwise returns NULL
  123. virtual std::unique_ptr<IObjectInfo> getObjectInfo(const ObjectTemplate & tmpl) const = 0;
  124. template <typename Handler> void serialize(Handler &h, const int version)
  125. {
  126. h & type;
  127. h & subtype;
  128. h & templates;
  129. h & rmgInfo;
  130. h & objectName;
  131. if(version >= 759)
  132. {
  133. h & typeName;
  134. h & subTypeName;
  135. }
  136. }
  137. };
  138. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  139. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  140. {
  141. /// Small internal structure that contains information on specific group of objects
  142. /// (creating separate entity is overcomplicating at least at this point)
  143. struct ObjectContainter
  144. {
  145. si32 id;
  146. std::string identifier;
  147. std::string name; // human-readable name
  148. std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
  149. JsonNode base;
  150. std::map<si32, TObjectTypeHandler> subObjects;
  151. std::map<std::string, si32> subIds;//full id from core scope -> subtype
  152. template <typename Handler> void serialize(Handler &h, const int version)
  153. {
  154. h & name;
  155. h & handlerName;
  156. h & base;
  157. h & subObjects;
  158. if(version >= 759)
  159. {
  160. h & identifier;
  161. h & subIds;
  162. }
  163. }
  164. };
  165. /// list of object handlers, each of them handles only one type
  166. std::map<si32, ObjectContainter * > objects;
  167. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  168. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  169. /// container with H3 templates, used only during loading, no need to serialize it
  170. typedef std::multimap<std::pair<si32, si32>, ObjectTemplate> TTemplatesContainer;
  171. TTemplatesContainer legacyTemplates;
  172. /// contains list of custom names for H3 objects (e.g. Dwellings), used to load H3 data
  173. /// format: customNames[primaryID][secondaryID] -> name
  174. std::map<si32, std::vector<std::string>> customNames;
  175. void loadObjectEntry(const std::string & identifier, const JsonNode & entry, ObjectContainter * obj);
  176. ObjectContainter * loadFromJson(const JsonNode & json, const std::string & name);
  177. public:
  178. CObjectClassesHandler();
  179. ~CObjectClassesHandler();
  180. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  181. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  182. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  183. void loadSubObject(const std::string & identifier, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
  184. void removeSubObject(si32 ID, si32 subID);
  185. void beforeValidate(JsonNode & object) override;
  186. void afterLoadFinalization() override;
  187. std::vector<bool> getDefaultAllowed() const override;
  188. /// Queries to detect loaded objects
  189. std::set<si32> knownObjects() const;
  190. std::set<si32> knownSubObjects(si32 primaryID) const;
  191. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  192. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  193. TObjectTypeHandler getHandlerFor(std::string type, std::string subtype) const;
  194. std::string getObjectName(si32 type) const;
  195. std::string getObjectName(si32 type, si32 subtype) const;
  196. /// Returns handler string describing the handler (for use in client)
  197. std::string getObjectHandlerName(si32 type) const;
  198. template <typename Handler> void serialize(Handler &h, const int version)
  199. {
  200. h & objects;
  201. }
  202. };