CommonConstructors.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * CommonConstructors.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 "CObjectClassesHandler.h"
  12. #include "../CTownHandler.h" // for building ID-based filters
  13. #include "MapObjects.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CGObjectInstance;
  16. class CGTownInstance;
  17. class CGHeroInstance;
  18. class CGDwelling;
  19. class CHeroClass;
  20. class CBank;
  21. class CStackBasicDescriptor;
  22. /// Class that is used for objects that do not have dedicated handler
  23. template<class ObjectType>
  24. class CDefaultObjectTypeHandler : public AObjectTypeHandler
  25. {
  26. protected:
  27. ObjectType * createTyped(std::shared_ptr<const ObjectTemplate> tmpl /* = nullptr */) const
  28. {
  29. auto obj = new ObjectType();
  30. preInitObject(obj);
  31. //Set custom template or leave null
  32. if (tmpl)
  33. {
  34. obj->appearance = tmpl;
  35. }
  36. return obj;
  37. }
  38. public:
  39. CDefaultObjectTypeHandler() {}
  40. CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override
  41. {
  42. return createTyped(tmpl);
  43. }
  44. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override
  45. {
  46. }
  47. virtual std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override
  48. {
  49. return nullptr;
  50. }
  51. };
  52. class CObstacleConstructor : public CDefaultObjectTypeHandler<CGObjectInstance>
  53. {
  54. public:
  55. bool isStaticObject() override;
  56. };
  57. class CTownInstanceConstructor : public CDefaultObjectTypeHandler<CGTownInstance>
  58. {
  59. JsonNode filtersJson;
  60. protected:
  61. bool objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const override;
  62. void initTypeData(const JsonNode & input) override;
  63. public:
  64. CFaction * faction = nullptr;
  65. std::map<std::string, LogicalExpression<BuildingID>> filters;
  66. CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
  67. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
  68. void afterLoadFinalization() override;
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & filtersJson;
  72. h & faction;
  73. h & filters;
  74. h & static_cast<CDefaultObjectTypeHandler<CGTownInstance>&>(*this);
  75. }
  76. };
  77. class CHeroInstanceConstructor : public CDefaultObjectTypeHandler<CGHeroInstance>
  78. {
  79. JsonNode filtersJson;
  80. protected:
  81. bool objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const override;
  82. void initTypeData(const JsonNode & input) override;
  83. public:
  84. CHeroClass * heroClass = nullptr;
  85. std::map<std::string, LogicalExpression<HeroTypeID>> filters;
  86. CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
  87. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
  88. void afterLoadFinalization() override;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. {
  91. h & filtersJson;
  92. h & heroClass;
  93. h & filters;
  94. h & static_cast<CDefaultObjectTypeHandler<CGHeroInstance>&>(*this);
  95. }
  96. };
  97. class CDwellingInstanceConstructor : public CDefaultObjectTypeHandler<CGDwelling>
  98. {
  99. std::vector<std::vector<const CCreature *>> availableCreatures;
  100. JsonNode guards;
  101. protected:
  102. bool objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const override;
  103. void initTypeData(const JsonNode & input) override;
  104. public:
  105. bool hasNameTextID() const override;
  106. CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
  107. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
  108. bool producesCreature(const CCreature * crea) const;
  109. std::vector<const CCreature *> getProducedCreatures() const;
  110. template <typename Handler> void serialize(Handler &h, const int version)
  111. {
  112. h & availableCreatures;
  113. h & guards;
  114. h & static_cast<CDefaultObjectTypeHandler<CGDwelling>&>(*this);
  115. }
  116. };
  117. struct BankConfig
  118. {
  119. ui32 value = 0; //overall value of given things
  120. ui32 chance = 0; //chance for this level being chosen
  121. ui32 upgradeChance = 0; //chance for creatures to be in upgraded versions
  122. ui32 combatValue = 0; //how hard are guards of this level
  123. std::vector<CStackBasicDescriptor> guards; //creature ID, amount
  124. ResourceSet resources; //resources given in case of victory
  125. std::vector<CStackBasicDescriptor> creatures; //creatures granted in case of victory (creature ID, amount)
  126. std::vector<ArtifactID> artifacts; //artifacts given in case of victory
  127. std::vector<SpellID> spells; // granted spell(s), for Pyramid
  128. template <typename Handler> void serialize(Handler &h, const int version)
  129. {
  130. h & chance;
  131. h & upgradeChance;
  132. h & guards;
  133. h & combatValue;
  134. h & resources;
  135. h & creatures;
  136. h & artifacts;
  137. h & value;
  138. h & spells;
  139. }
  140. };
  141. typedef std::vector<std::pair<ui8, IObjectInfo::CArmyStructure>> TPossibleGuards;
  142. template <typename T>
  143. struct DLL_LINKAGE PossibleReward
  144. {
  145. int chance;
  146. T data;
  147. PossibleReward(int chance, const T & data) : chance(chance), data(data) {}
  148. };
  149. class DLL_LINKAGE CBankInfo : public IObjectInfo
  150. {
  151. const JsonVector & config;
  152. public:
  153. CBankInfo(const JsonVector & Config);
  154. TPossibleGuards getPossibleGuards() const;
  155. std::vector<PossibleReward<TResources>> getPossibleResourcesReward() const;
  156. std::vector<PossibleReward<CStackBasicDescriptor>> getPossibleCreaturesReward() const;
  157. // These functions should try to evaluate minimal possible/max possible guards to give provide information on possible thread to AI
  158. CArmyStructure minGuards() const override;
  159. CArmyStructure maxGuards() const override;
  160. bool givesResources() const override;
  161. bool givesArtifacts() const override;
  162. bool givesCreatures() const override;
  163. bool givesSpells() const override;
  164. };
  165. class CBankInstanceConstructor : public CDefaultObjectTypeHandler<CBank>
  166. {
  167. BankConfig generateConfig(const JsonNode & conf, CRandomGenerator & rng) const;
  168. JsonVector levels;
  169. protected:
  170. void initTypeData(const JsonNode & input) override;
  171. public:
  172. // all banks of this type will be reset N days after clearing,
  173. si32 bankResetDuration = 0;
  174. CGObjectInstance * create(std::shared_ptr<const ObjectTemplate> tmpl = nullptr) const override;
  175. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const override;
  176. bool hasNameTextID() const override;
  177. std::unique_ptr<IObjectInfo> getObjectInfo(std::shared_ptr<const ObjectTemplate> tmpl) const override;
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & levels;
  181. h & bankResetDuration;
  182. h & static_cast<CDefaultObjectTypeHandler<CBank>&>(*this);
  183. }
  184. };
  185. VCMI_LIB_NAMESPACE_END