CObjectClassesHandler.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #pragma once
  2. #include "ObjectTemplate.h"
  3. #include "../GameConstants.h"
  4. #include "../ConstTransitivePtr.h"
  5. #include "../IHandlerBase.h"
  6. /*
  7. * CObjectClassesHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class JsonNode;
  16. class CRandomGenerator;
  17. /// Structure that describes placement rules for this object in random map
  18. struct RandomMapInfo
  19. {
  20. /// How valuable this object is, 1k = worthless, 10k = Utopia-level
  21. ui32 value;
  22. /// How many of such objects can be placed on map, 0 = object can not be placed by RMG
  23. ui32 mapLimit;
  24. /// How many of such objects can be placed in one zone, 0 = unplaceable
  25. ui32 zoneLimit;
  26. /// Rarity of object, 5 = extremely rare, 100 = common
  27. ui32 rarity;
  28. RandomMapInfo():
  29. value(0),
  30. mapLimit(0),
  31. zoneLimit(0),
  32. rarity(0)
  33. {}
  34. template <typename Handler> void serialize(Handler &h, const int version)
  35. {
  36. h & value & mapLimit & zoneLimit & rarity;
  37. }
  38. };
  39. class IObjectInfo
  40. {
  41. public:
  42. virtual bool givesResources() const = 0;
  43. virtual bool givesExperience() const = 0;
  44. virtual bool givesMana() const = 0;
  45. virtual bool givesMovement() const = 0;
  46. virtual bool givesPrimarySkills() const = 0;
  47. virtual bool givesSecondarySkills() const = 0;
  48. virtual bool givesArtifacts() const = 0;
  49. virtual bool givesCreatures() const = 0;
  50. virtual bool givesSpells() const = 0;
  51. virtual bool givesBonuses() const = 0;
  52. };
  53. class CGObjectInstance;
  54. class AObjectTypeHandler
  55. {
  56. RandomMapInfo rmgInfo;
  57. si32 type;
  58. si32 subtype;
  59. JsonNode base; /// describes base template
  60. std::vector<ObjectTemplate> templates;
  61. protected:
  62. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  63. public:
  64. virtual ~AObjectTypeHandler(){}
  65. void setType(si32 type, si32 subtype);
  66. /// loads generic data from Json structure
  67. virtual void init(const JsonNode & input);
  68. void addTemplate(ObjectTemplate templ);
  69. void addTemplate(JsonNode config);
  70. /// returns all templates matching parameters
  71. std::vector<ObjectTemplate> getTemplates() const;
  72. std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
  73. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  74. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  75. boost::optional<ObjectTemplate> getOverride(si32 terrainType, const CGObjectInstance * object) const;
  76. const RandomMapInfo & getRMGInfo();
  77. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  78. /// to allow creating objects before game start (e.g. map loading)
  79. virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
  80. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  81. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  82. /// Returns object configuration, if available. Othervice returns NULL
  83. virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const = 0;
  84. template <typename Handler> void serialize(Handler &h, const int version)
  85. {
  86. h & type & subtype & templates & rmgInfo;
  87. }
  88. };
  89. /// Class that is used for objects that do not have dedicated handler
  90. template<class ObjectType>
  91. class CDefaultObjectTypeHandler : public AObjectTypeHandler
  92. {
  93. CGObjectInstance * create(ObjectTemplate tmpl) const
  94. {
  95. auto obj = new ObjectType();
  96. obj->ID = tmpl.id;
  97. obj->subID = tmpl.subid;
  98. obj->appearance = tmpl;
  99. return obj;
  100. }
  101. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  102. {
  103. }
  104. virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const
  105. {
  106. return nullptr;
  107. }
  108. };
  109. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  110. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  111. {
  112. /// Small internal structure that contains information on specific group of objects
  113. /// (creating separate entity is overcomplicating at least at this point)
  114. struct ObjectContainter
  115. {
  116. si32 id;
  117. std::string name; // human-readable name
  118. std::string handlerName; // ID of handler that controls this object, shoul be determined using hadlerConstructor map
  119. JsonNode base;
  120. std::map<si32, TObjectTypeHandler> objects;
  121. template <typename Handler> void serialize(Handler &h, const int version)
  122. {
  123. h & name & handlerName & base & objects;
  124. }
  125. };
  126. typedef std::multimap<std::pair<si32, si32>, ObjectTemplate> TTemplatesContainer;
  127. /// list of object handlers, each of them handles only one type
  128. std::map<si32, ObjectContainter * > objects;
  129. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  130. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  131. /// container with H3 templates, used only during loading, no need to serialize it
  132. TTemplatesContainer legacyTemplates;
  133. void loadObjectEntry(const JsonNode & entry, ObjectContainter * obj);
  134. ObjectContainter * loadFromJson(const JsonNode & json);
  135. public:
  136. CObjectClassesHandler();
  137. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  138. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  139. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  140. void loadSubObject(std::string name, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
  141. void removeSubObject(si32 ID, si32 subID);
  142. void beforeValidate(JsonNode & object) override;
  143. void afterLoadFinalization() override;
  144. std::vector<bool> getDefaultAllowed() const override;
  145. /// Queries to detect loaded objects
  146. std::set<si32> knownObjects() const;
  147. std::set<si32> knownSubObjects(si32 primaryID) const;
  148. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  149. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  150. std::string getObjectName(si32 type) const;
  151. template <typename Handler> void serialize(Handler &h, const int version)
  152. {
  153. h & objects;
  154. }
  155. };