CObjectClassesHandler.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #include "Terrain.h"
  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. 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. mapLimit(0),
  45. zoneLimit(0),
  46. rarity(0)
  47. {}
  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 DLL_LINKAGE AObjectTypeHandler : public boost::noncopyable
  111. {
  112. RandomMapInfo rmgInfo;
  113. /// Human-readable name of this object, used for objects like banks and dwellings, if set
  114. boost::optional<std::string> objectName;
  115. JsonNode base; /// describes base template
  116. std::vector<ObjectTemplate> templates;
  117. SObjectSounds sounds;
  118. boost::optional<si32> aiValue;
  119. boost::optional<std::string> battlefield;
  120. protected:
  121. void preInitObject(CGObjectInstance * obj) const;
  122. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  123. /// initialization for classes that inherit this one
  124. virtual void initTypeData(const JsonNode & input);
  125. public:
  126. std::string typeName;
  127. std::string subTypeName;
  128. si32 type;
  129. si32 subtype;
  130. AObjectTypeHandler();
  131. virtual ~AObjectTypeHandler();
  132. void setType(si32 type, si32 subtype);
  133. void setTypeName(std::string type, std::string subtype);
  134. /// loads generic data from Json structure and passes it towards type-specific constructors
  135. void init(const JsonNode & input, boost::optional<std::string> name = boost::optional<std::string>());
  136. /// Returns object-specific name, if set
  137. boost::optional<std::string> getCustomName() const;
  138. SObjectSounds getSounds() const;
  139. void addTemplate(const ObjectTemplate & templ);
  140. void addTemplate(JsonNode config);
  141. /// returns all templates matching parameters
  142. std::vector<ObjectTemplate> getTemplates() const;
  143. std::vector<ObjectTemplate> getTemplates(const Terrain & terrainType) const;
  144. BattleField getBattlefield() const;
  145. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  146. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  147. boost::optional<ObjectTemplate> getOverride(const Terrain & terrainType, const CGObjectInstance * object) const;
  148. const RandomMapInfo & getRMGInfo();
  149. boost::optional<si32> getAiValue() const;
  150. virtual bool isStaticObject();
  151. virtual void afterLoadFinalization();
  152. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  153. /// to allow creating objects before game start (e.g. map loading)
  154. virtual CGObjectInstance * create(const ObjectTemplate & tmpl) const = 0;
  155. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  156. /// This should set remaining properties, including randomized or depending on map
  157. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  158. /// Returns object configuration, if available. Otherwise returns NULL
  159. virtual std::unique_ptr<IObjectInfo> getObjectInfo(const ObjectTemplate & tmpl) const = 0;
  160. template <typename Handler> void serialize(Handler &h, const int version)
  161. {
  162. h & type;
  163. h & subtype;
  164. h & templates;
  165. h & rmgInfo;
  166. h & objectName;
  167. h & typeName;
  168. h & subTypeName;
  169. h & sounds;
  170. h & aiValue;
  171. h & battlefield;
  172. }
  173. };
  174. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  175. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  176. {
  177. /// Small internal structure that contains information on specific group of objects
  178. /// (creating separate entity is overcomplicating at least at this point)
  179. struct ObjectContainter
  180. {
  181. si32 id;
  182. std::string identifier;
  183. std::string name; // human-readable name
  184. std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
  185. JsonNode base;
  186. std::map<si32, TObjectTypeHandler> subObjects;
  187. std::map<std::string, si32> subIds;//full id from core scope -> subtype
  188. SObjectSounds sounds;
  189. boost::optional<si32> groupDefaultAiValue;
  190. template <typename Handler> void serialize(Handler &h, const int version)
  191. {
  192. h & name;
  193. h & handlerName;
  194. h & base;
  195. h & subObjects;
  196. h & identifier;
  197. h & subIds;
  198. h & sounds;
  199. h & groupDefaultAiValue;
  200. }
  201. };
  202. /// list of object handlers, each of them handles only one type
  203. std::map<si32, ObjectContainter * > objects;
  204. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  205. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  206. /// container with H3 templates, used only during loading, no need to serialize it
  207. typedef std::multimap<std::pair<si32, si32>, ObjectTemplate> TTemplatesContainer;
  208. TTemplatesContainer legacyTemplates;
  209. /// contains list of custom names for H3 objects (e.g. Dwellings), used to load H3 data
  210. /// format: customNames[primaryID][secondaryID] -> name
  211. std::map<si32, std::vector<std::string>> customNames;
  212. void loadObjectEntry(const std::string & identifier, const JsonNode & entry, ObjectContainter * obj, bool isSubobject = false);
  213. ObjectContainter * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name);
  214. public:
  215. CObjectClassesHandler();
  216. ~CObjectClassesHandler();
  217. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  218. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  219. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  220. void loadSubObject(const std::string & identifier, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
  221. void removeSubObject(si32 ID, si32 subID);
  222. void beforeValidate(JsonNode & object) override;
  223. void afterLoadFinalization() override;
  224. std::vector<bool> getDefaultAllowed() const override;
  225. /// Queries to detect loaded objects
  226. std::set<si32> knownObjects() const;
  227. std::set<si32> knownSubObjects(si32 primaryID) const;
  228. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  229. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  230. TObjectTypeHandler getHandlerFor(std::string type, std::string subtype) const;
  231. TObjectTypeHandler getHandlerFor(CompoundMapObjectID compoundIdentifier) const;
  232. std::string getObjectName(si32 type) const;
  233. std::string getObjectName(si32 type, si32 subtype) const;
  234. SObjectSounds getObjectSounds(si32 type) const;
  235. SObjectSounds getObjectSounds(si32 type, si32 subtype) const;
  236. /// Returns handler string describing the handler (for use in client)
  237. std::string getObjectHandlerName(si32 type) const;
  238. boost::optional<si32> getObjGroupAiValue(si32 primaryID) const; //default AI value of objects belonging to particular primaryID
  239. template <typename Handler> void serialize(Handler &h, const int version)
  240. {
  241. h & objects;
  242. }
  243. };