CGTownBuilding.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * CGTownBuilding.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 "IObjectInterface.h"
  12. #include "../rewardable/Interface.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CGTownInstance;
  15. class CBuilding;
  16. class DLL_LINKAGE CGTownBuilding : public IObjectInterface
  17. {
  18. ///basic class for town structures handled as map objects
  19. public:
  20. CGTownBuilding(CGTownInstance * town, const BuildingID & index);
  21. CGTownBuilding(IGameCallback *cb);
  22. CGTownInstance * town;
  23. STRONG_INLINE
  24. const BuildingID & getBuildingType() const
  25. {
  26. return bID;
  27. }
  28. PlayerColor getOwner() const override;
  29. MapObjectID getObjGroupIndex() const override;
  30. MapObjectSubID getObjTypeIndex() const override;
  31. int3 visitablePos() const override;
  32. int3 getPosition() const override;
  33. template <typename Handler> void serialize(Handler &h)
  34. {
  35. h & bID;
  36. if (h.version >= Handler::Version::NEW_TOWN_BUILDINGS)
  37. {
  38. // no-op
  39. }
  40. else
  41. {
  42. si32 indexOnTV = 0; //identifies its index on towns vector
  43. BuildingSubID::EBuildingSubID bType = BuildingSubID::NONE;
  44. h & indexOnTV;
  45. h & bType;
  46. }
  47. }
  48. private:
  49. BuildingID bID; //from building list
  50. };
  51. class DLL_LINKAGE CTownRewardableBuilding : public CGTownBuilding, public Rewardable::Interface
  52. {
  53. /// reward selected by player, no serialize
  54. ui16 selectedReward = 0;
  55. std::set<ObjectInstanceID> visitors;
  56. bool wasVisitedBefore(const CGHeroInstance * contextHero) const;
  57. void grantReward(ui32 rewardID, const CGHeroInstance * hero) const;
  58. Rewardable::Configuration generateConfiguration(vstd::RNG & rand) const;
  59. public:
  60. void setProperty(ObjProperty what, ObjPropertyID identifier) override;
  61. void onHeroVisit(const CGHeroInstance * h) const override;
  62. void newTurn(vstd::RNG & rand) const override;
  63. /// gives second part of reward after hero level-ups for proper granting of spells/mana
  64. void heroLevelUpDone(const CGHeroInstance *hero) const override;
  65. void initObj(vstd::RNG & rand) override;
  66. /// applies player selection of reward
  67. void blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const override;
  68. CTownRewardableBuilding(CGTownInstance * town, const BuildingID & index, vstd::RNG & rand);
  69. CTownRewardableBuilding(IGameCallback *cb);
  70. template <typename Handler> void serialize(Handler &h)
  71. {
  72. h & static_cast<CGTownBuilding&>(*this);
  73. h & static_cast<Rewardable::Interface&>(*this);
  74. h & visitors;
  75. }
  76. };
  77. /// Compatibility for old code
  78. class DLL_LINKAGE CTownCompatBuilding1 : public CTownRewardableBuilding
  79. {
  80. public:
  81. using CTownRewardableBuilding::CTownRewardableBuilding;
  82. template <typename Handler> void serialize(Handler &h)
  83. {
  84. if (h.version >= Handler::Version::NEW_TOWN_BUILDINGS)
  85. {
  86. h & static_cast<CTownRewardableBuilding&>(*this);
  87. }
  88. else
  89. {
  90. h & static_cast<CGTownBuilding&>(*this);
  91. std::set<ObjectInstanceID> visitors;
  92. h & visitors;
  93. }
  94. }
  95. };
  96. /// Compatibility for old code
  97. class DLL_LINKAGE CTownCompatBuilding2 : public CTownRewardableBuilding
  98. {
  99. public:
  100. using CTownRewardableBuilding::CTownRewardableBuilding;
  101. template <typename Handler> void serialize(Handler &h)
  102. {
  103. if (h.version >= Handler::Version::NEW_TOWN_BUILDINGS)
  104. {
  105. h & static_cast<CTownRewardableBuilding&>(*this);
  106. }
  107. else
  108. {
  109. h & static_cast<CGTownBuilding&>(*this);
  110. std::set<ObjectInstanceID> visitors;
  111. h & visitors;
  112. }
  113. }
  114. };
  115. VCMI_LIB_NAMESPACE_END