CommonConstructors.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. #include "CObjectClassesHandler.h"
  3. #include "../CTownHandler.h" // for building ID-based filters
  4. /*
  5. * CommonConstructors.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CGTownInstance;
  14. class CGHeroInstance;
  15. class CGDwelling;
  16. //class CGArtifact;
  17. //class CGCreature;
  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(ObjectTemplate tmpl) const
  27. {
  28. auto obj = new ObjectType();
  29. obj->ID = tmpl.id;
  30. obj->subID = tmpl.subid;
  31. obj->appearance = tmpl;
  32. return obj;
  33. }
  34. public:
  35. CDefaultObjectTypeHandler(){}
  36. CGObjectInstance * create(ObjectTemplate tmpl) const
  37. {
  38. return createTyped(tmpl);
  39. }
  40. virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  41. {
  42. }
  43. virtual std::unique_ptr<IObjectInfo> getObjectInfo(ObjectTemplate tmpl) const
  44. {
  45. return nullptr;
  46. }
  47. };
  48. class CObstacleConstructor : public CDefaultObjectTypeHandler<CGObjectInstance>
  49. {
  50. public:
  51. CObstacleConstructor();
  52. bool isStaticObject();
  53. };
  54. class CTownInstanceConstructor : public CDefaultObjectTypeHandler<CGTownInstance>
  55. {
  56. JsonNode filtersJson;
  57. protected:
  58. bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  59. void initTypeData(const JsonNode & input);
  60. public:
  61. CFaction * faction;
  62. std::map<std::string, LogicalExpression<BuildingID>> filters;
  63. CTownInstanceConstructor();
  64. CGObjectInstance * create(ObjectTemplate tmpl) const;
  65. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const;
  66. void afterLoadFinalization();
  67. template <typename Handler> void serialize(Handler &h, const int version)
  68. {
  69. h & filtersJson & faction & filters;
  70. h & static_cast<CDefaultObjectTypeHandler<CGTownInstance>&>(*this);
  71. }
  72. };
  73. class CHeroInstanceConstructor : public CDefaultObjectTypeHandler<CGHeroInstance>
  74. {
  75. JsonNode filtersJson;
  76. protected:
  77. bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  78. void initTypeData(const JsonNode & input);
  79. public:
  80. CHeroClass * heroClass;
  81. std::map<std::string, LogicalExpression<HeroTypeID>> filters;
  82. CHeroInstanceConstructor();
  83. CGObjectInstance * create(ObjectTemplate tmpl) const;
  84. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const;
  85. void afterLoadFinalization();
  86. template <typename Handler> void serialize(Handler &h, const int version)
  87. {
  88. h & filtersJson & heroClass & filters;
  89. h & static_cast<CDefaultObjectTypeHandler<CGHeroInstance>&>(*this);
  90. }
  91. };
  92. class CDwellingInstanceConstructor : public CDefaultObjectTypeHandler<CGDwelling>
  93. {
  94. std::vector<std::vector<const CCreature *>> availableCreatures;
  95. JsonNode guards;
  96. protected:
  97. bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
  98. void initTypeData(const JsonNode & input);
  99. public:
  100. CDwellingInstanceConstructor();
  101. CGObjectInstance * create(ObjectTemplate tmpl) const;
  102. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const;
  103. bool producesCreature(const CCreature * crea) const;
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & availableCreatures & guards;
  107. h & static_cast<CDefaultObjectTypeHandler<CGDwelling>&>(*this);
  108. }
  109. };
  110. struct BankConfig
  111. {
  112. BankConfig() { chance = upgradeChance = combatValue = value = 0; };
  113. ui32 value; //overall value of given things
  114. ui32 chance; //chance for this level being chosen
  115. ui32 upgradeChance; //chance for creatures to be in upgraded versions
  116. ui32 combatValue; //how hard are guards of this level
  117. std::vector<CStackBasicDescriptor> guards; //creature ID, amount
  118. Res::ResourceSet resources; //resources given in case of victory
  119. std::vector<CStackBasicDescriptor> creatures; //creatures granted in case of victory (creature ID, amount)
  120. std::vector<ArtifactID> artifacts; //artifacts given in case of victory
  121. std::vector<SpellID> spells; // granted spell(s), for Pyramid
  122. template <typename Handler> void serialize(Handler &h, const int version)
  123. {
  124. h & chance & upgradeChance & guards & combatValue & resources & creatures & artifacts & value & spells;
  125. }
  126. };
  127. class CBankInfo : public IObjectInfo
  128. {
  129. JsonVector config;
  130. public:
  131. CBankInfo(JsonVector config);
  132. CArmyStructure minGuards() const;
  133. CArmyStructure maxGuards() const;
  134. bool givesResources() const;
  135. bool givesArtifacts() const;
  136. bool givesCreatures() const;
  137. bool givesSpells() const;
  138. };
  139. class CBankInstanceConstructor : public CDefaultObjectTypeHandler<CBank>
  140. {
  141. BankConfig generateConfig(const JsonNode & conf, CRandomGenerator & rng) const;
  142. JsonVector levels;
  143. protected:
  144. void initTypeData(const JsonNode & input);
  145. public:
  146. // all banks of this type will be reset N days after clearing,
  147. si32 bankResetDuration;
  148. CBankInstanceConstructor();
  149. CGObjectInstance *create(ObjectTemplate tmpl) const;
  150. void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const;
  151. std::unique_ptr<IObjectInfo> getObjectInfo(ObjectTemplate tmpl) const;
  152. };