2
0

CObjectClassesHandler.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. };
  75. class CGObjectInstance;
  76. class DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
  77. {
  78. RandomMapInfo rmgInfo;
  79. /// Human-readable name of this object, used for objects like banks and dwellings, if set
  80. boost::optional<std::string> objectName;
  81. JsonNode base; /// describes base template
  82. std::vector<ObjectTemplate> templates;
  83. protected:
  84. void preInitObject(CGObjectInstance * obj) const;
  85. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  86. /// initialization for classes that inherit this one
  87. virtual void initTypeData(const JsonNode & input);
  88. public:
  89. std::string typeName;
  90. std::string subTypeName;
  91. si32 type;
  92. si32 subtype;
  93. virtual ~AObjectTypeHandler(){}
  94. void setType(si32 type, si32 subtype);
  95. void setTypeName(std::string type, std::string subtype);
  96. /// loads generic data from Json structure and passes it towards type-specific constructors
  97. void init(const JsonNode & input, boost::optional<std::string> name = boost::optional<std::string>());
  98. /// Returns object-specific name, if set
  99. boost::optional<std::string> getCustomName() const;
  100. void addTemplate(const ObjectTemplate & templ);
  101. void addTemplate(JsonNode config);
  102. /// returns all templates matching parameters
  103. std::vector<ObjectTemplate> getTemplates() const;
  104. std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
  105. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  106. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  107. boost::optional<ObjectTemplate> getOverride(si32 terrainType, const CGObjectInstance * object) const;
  108. const RandomMapInfo & getRMGInfo();
  109. virtual bool isStaticObject();
  110. virtual void afterLoadFinalization();
  111. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  112. /// to allow creating objects before game start (e.g. map loading)
  113. virtual CGObjectInstance * create(const ObjectTemplate & tmpl) const = 0;
  114. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  115. /// This should set remaining properties, including randomized or depending on map
  116. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  117. /// Returns object configuration, if available. Otherwise returns NULL
  118. virtual std::unique_ptr<IObjectInfo> getObjectInfo(const ObjectTemplate & tmpl) const = 0;
  119. template <typename Handler> void serialize(Handler &h, const int version)
  120. {
  121. h & type & subtype & templates & rmgInfo & objectName;
  122. if(version >= 759)
  123. {
  124. h & typeName & subTypeName;
  125. }
  126. }
  127. };
  128. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  129. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  130. {
  131. /// Small internal structure that contains information on specific group of objects
  132. /// (creating separate entity is overcomplicating at least at this point)
  133. struct ObjectContainter
  134. {
  135. si32 id;
  136. std::string identifier;
  137. std::string name; // human-readable name
  138. std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
  139. JsonNode base;
  140. std::map<si32, TObjectTypeHandler> subObjects;
  141. std::map<std::string, si32> subIds;//full id from core scope -> subtype
  142. template <typename Handler> void serialize(Handler &h, const int version)
  143. {
  144. h & name & handlerName & base & subObjects;
  145. if(version >= 759)
  146. {
  147. h & identifier & subIds;
  148. }
  149. }
  150. };
  151. /// list of object handlers, each of them handles only one type
  152. std::map<si32, ObjectContainter * > objects;
  153. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  154. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  155. /// container with H3 templates, used only during loading, no need to serialize it
  156. typedef std::multimap<std::pair<si32, si32>, ObjectTemplate> TTemplatesContainer;
  157. TTemplatesContainer legacyTemplates;
  158. /// contains list of custom names for H3 objects (e.g. Dwellings), used to load H3 data
  159. /// format: customNames[primaryID][secondaryID] -> name
  160. std::map<si32, std::vector<std::string>> customNames;
  161. void loadObjectEntry(const std::string & identifier, const JsonNode & entry, ObjectContainter * obj);
  162. ObjectContainter * loadFromJson(const JsonNode & json, const std::string & name);
  163. public:
  164. CObjectClassesHandler();
  165. ~CObjectClassesHandler();
  166. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  167. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  168. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  169. void loadSubObject(const std::string & identifier, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
  170. void removeSubObject(si32 ID, si32 subID);
  171. void beforeValidate(JsonNode & object) override;
  172. void afterLoadFinalization() override;
  173. std::vector<bool> getDefaultAllowed() const override;
  174. /// Queries to detect loaded objects
  175. std::set<si32> knownObjects() const;
  176. std::set<si32> knownSubObjects(si32 primaryID) const;
  177. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  178. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  179. TObjectTypeHandler getHandlerFor(std::string type, std::string subtype) const;
  180. std::string getObjectName(si32 type) const;
  181. std::string getObjectName(si32 type, si32 subtype) const;
  182. /// Returns handler string describing the handler (for use in client)
  183. std::string getObjectHandlerName(si32 type) const;
  184. template <typename Handler> void serialize(Handler &h, const int version)
  185. {
  186. h & objects;
  187. }
  188. };