CObjectClassesHandler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. struct CArmyStructure
  43. {
  44. ui32 totalStrength;
  45. ui32 shootersStrength;
  46. ui32 flyersStrength;
  47. ui32 walkersStrength;
  48. CArmyStructure() :
  49. totalStrength(0),
  50. shootersStrength(0),
  51. flyersStrength(0),
  52. walkersStrength(0)
  53. {}
  54. bool operator <(const CArmyStructure & other)
  55. {
  56. return this->totalStrength < other.totalStrength;
  57. }
  58. };
  59. /// Returns possible composition of guards. Actual guards would be
  60. /// somewhere between these two values
  61. virtual CArmyStructure minGuards() const { return CArmyStructure(); }
  62. virtual CArmyStructure maxGuards() const { return CArmyStructure(); }
  63. virtual bool givesResources() const { return false; }
  64. virtual bool givesExperience() const { return false; }
  65. virtual bool givesMana() const { return false; }
  66. virtual bool givesMovement() const { return false; }
  67. virtual bool givesPrimarySkills() const { return false; }
  68. virtual bool givesSecondarySkills() const { return false; }
  69. virtual bool givesArtifacts() const { return false; }
  70. virtual bool givesCreatures() const { return false; }
  71. virtual bool givesSpells() const { return false; }
  72. virtual bool givesBonuses() const { return false; }
  73. };
  74. class CGObjectInstance;
  75. class DLL_EXPORT AObjectTypeHandler : public boost::noncopyable
  76. {
  77. RandomMapInfo rmgInfo;
  78. si32 type;
  79. si32 subtype;
  80. JsonNode base; /// describes base template
  81. std::vector<ObjectTemplate> templates;
  82. protected:
  83. virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  84. /// initialization for classes that inherit this one
  85. virtual void initTypeData(const JsonNode & input);
  86. public:
  87. virtual ~AObjectTypeHandler(){}
  88. void setType(si32 type, si32 subtype);
  89. /// loads generic data from Json structure and passes it towards type-specific constructors
  90. void init(const JsonNode & input);
  91. void addTemplate(ObjectTemplate templ);
  92. void addTemplate(JsonNode config);
  93. /// returns all templates matching parameters
  94. std::vector<ObjectTemplate> getTemplates() const;
  95. std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
  96. /// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
  97. /// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
  98. boost::optional<ObjectTemplate> getOverride(si32 terrainType, const CGObjectInstance * object) const;
  99. const RandomMapInfo & getRMGInfo();
  100. virtual bool isStaticObject();
  101. virtual void afterLoadFinalization();
  102. /// Creates object and set up core properties (like ID/subID). Object is NOT initialized
  103. /// to allow creating objects before game start (e.g. map loading)
  104. virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
  105. /// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
  106. /// This should set remaining properties, including randomized or depending on map
  107. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
  108. /// Returns object configuration, if available. Othervice returns NULL
  109. virtual std::unique_ptr<IObjectInfo> getObjectInfo(ObjectTemplate tmpl) const = 0;
  110. template <typename Handler> void serialize(Handler &h, const int version)
  111. {
  112. h & type & subtype & templates & rmgInfo;
  113. }
  114. };
  115. typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
  116. class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
  117. {
  118. /// Small internal structure that contains information on specific group of objects
  119. /// (creating separate entity is overcomplicating at least at this point)
  120. struct ObjectContainter
  121. {
  122. si32 id;
  123. std::string name; // human-readable name
  124. std::string handlerName; // ID of handler that controls this object, shoul be determined using hadlerConstructor map
  125. JsonNode base;
  126. std::map<si32, TObjectTypeHandler> objects;
  127. template <typename Handler> void serialize(Handler &h, const int version)
  128. {
  129. h & name & handlerName & base & objects;
  130. }
  131. };
  132. typedef std::multimap<std::pair<si32, si32>, ObjectTemplate> TTemplatesContainer;
  133. /// list of object handlers, each of them handles only one type
  134. std::map<si32, ObjectContainter * > objects;
  135. /// map that is filled during contruction with all known handlers. Not serializeable due to usage of std::function
  136. std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
  137. /// container with H3 templates, used only during loading, no need to serialize it
  138. TTemplatesContainer legacyTemplates;
  139. void loadObjectEntry(const JsonNode & entry, ObjectContainter * obj);
  140. ObjectContainter * loadFromJson(const JsonNode & json);
  141. public:
  142. CObjectClassesHandler();
  143. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  144. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  145. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  146. void loadSubObject(std::string name, JsonNode config, si32 ID, boost::optional<si32> subID = boost::optional<si32>());
  147. void removeSubObject(si32 ID, si32 subID);
  148. void beforeValidate(JsonNode & object) override;
  149. void afterLoadFinalization() override;
  150. std::vector<bool> getDefaultAllowed() const override;
  151. /// Queries to detect loaded objects
  152. std::set<si32> knownObjects() const;
  153. std::set<si32> knownSubObjects(si32 primaryID) const;
  154. /// returns handler for specified object (ID-based). ObjectHandler keeps ownership
  155. TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
  156. std::string getObjectName(si32 type) const;
  157. template <typename Handler> void serialize(Handler &h, const int version)
  158. {
  159. h & objects;
  160. }
  161. };