CObjectClassesHandler.h 7.2 KB

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