CArtifactInstance.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * CArtifactInstance.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 "bonuses/CBonusSystemNode.h"
  12. #include "GameCallbackHolder.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. struct ArtifactLocation;
  15. class CGameState;
  16. class CArtifactSet;
  17. class DLL_LINKAGE CCombinedArtifactInstance : public GameCallbackHolder
  18. {
  19. protected:
  20. using GameCallbackHolder::GameCallbackHolder;
  21. public:
  22. using ArtPlacementMap = std::map<const CArtifactInstance*, ArtifactPosition>;
  23. struct PartInfo : public GameCallbackHolder
  24. {
  25. explicit PartInfo(IGameCallback * cb);
  26. PartInfo(const CArtifactInstance * artifact, ArtifactPosition slot);
  27. ArtifactInstanceID artifactID;
  28. ArtifactPosition slot;
  29. const CArtifactInstance * getArtifact() const;
  30. template <typename Handler>
  31. void serialize(Handler & h);
  32. };
  33. void addPart(const CArtifactInstance * art, const ArtifactPosition & slot);
  34. // Checks if supposed part inst is part of this combined art inst
  35. bool isPart(const CArtifactInstance * supposedPart) const;
  36. bool hasParts() const;
  37. const std::vector<PartInfo> & getPartsInfo() const;
  38. void addPlacementMap(const ArtPlacementMap & placementMap);
  39. template <typename Handler> void serialize(Handler & h)
  40. {
  41. h & partsInfo;
  42. }
  43. protected:
  44. std::vector<PartInfo> partsInfo;
  45. };
  46. class DLL_LINKAGE CScrollArtifactInstance
  47. {
  48. protected:
  49. CScrollArtifactInstance() = default;
  50. public:
  51. SpellID getScrollSpellID() const;
  52. };
  53. class DLL_LINKAGE CGrowingArtifactInstance
  54. {
  55. protected:
  56. CGrowingArtifactInstance() = default;
  57. public:
  58. void growingUp();
  59. };
  60. class DLL_LINKAGE CArtifactInstance final
  61. : public CBonusSystemNode, public CCombinedArtifactInstance, public CScrollArtifactInstance, public CGrowingArtifactInstance
  62. {
  63. ArtifactInstanceID id;
  64. ArtifactID artTypeID;
  65. public:
  66. CArtifactInstance(IGameCallback *cb, const CArtifact * art);
  67. CArtifactInstance(IGameCallback *cb);
  68. void setType(const CArtifact * art);
  69. std::string nodeName() const override;
  70. ArtifactID getTypeId() const;
  71. const CArtifact * getType() const;
  72. ArtifactInstanceID getId() const;
  73. void setId(ArtifactInstanceID id);
  74. static void saveCompatibilityFixArtifactID(std::shared_ptr<CArtifactInstance> self);
  75. bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE,
  76. bool assumeDestRemoved = false) const;
  77. bool isCombined() const;
  78. bool isScroll() const;
  79. void attachToBonusSystem(CGameState & gs);
  80. template <typename Handler> void serialize(Handler & h)
  81. {
  82. h & static_cast<CBonusSystemNode&>(*this);
  83. h & static_cast<CCombinedArtifactInstance&>(*this);
  84. h & artTypeID;
  85. h & id;
  86. if(!h.saving && h.loadingGamestate)
  87. setType(artTypeID.toArtifact());
  88. }
  89. };
  90. template <typename Handler>
  91. void CCombinedArtifactInstance::PartInfo::serialize(Handler & h)
  92. {
  93. if (h.saving || h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  94. {
  95. h & artifactID;
  96. }
  97. else
  98. {
  99. std::shared_ptr<CArtifactInstance> pointer;
  100. h & pointer;
  101. if (pointer->getId() == ArtifactInstanceID())
  102. CArtifactInstance::saveCompatibilityFixArtifactID(pointer);
  103. artifactID = pointer->getId();
  104. }
  105. h & slot;
  106. }
  107. VCMI_LIB_NAMESPACE_END