2
0

CObjectClassesHandler.h 8.0 KB

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