CObjectClassesHandler.h 8.6 KB

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