2
0

CommonConstructors.h 5.6 KB

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