CommonConstructors.h 6.0 KB

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