CObjectClassesHandler.h 9.6 KB

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