CObjectClassesHandler.h 9.9 KB

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