CObjectClassesHandler.h 6.6 KB

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