CArtHandler.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * CArtHandler.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 <vcmi/Artifact.h>
  12. #include <vcmi/ArtifactService.h>
  13. #include "bonuses/Bonus.h"
  14. #include "bonuses/CBonusSystemNode.h"
  15. #include "IHandlerBase.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CArtHandler;
  18. class CGHeroInstance;
  19. class CMap;
  20. class CGameState;
  21. class CArtifactSet;
  22. class CArtifactInstance;
  23. class JsonSerializeFormat;
  24. #define ART_BEARER_LIST \
  25. ART_BEARER(HERO)\
  26. ART_BEARER(CREATURE)\
  27. ART_BEARER(COMMANDER)\
  28. ART_BEARER(ALTAR)
  29. namespace ArtBearer
  30. {
  31. enum ArtBearer
  32. {
  33. #define ART_BEARER(x) x,
  34. ART_BEARER_LIST
  35. #undef ART_BEARER
  36. };
  37. }
  38. class DLL_LINKAGE CCombinedArtifact
  39. {
  40. protected:
  41. CCombinedArtifact() : fused(false) {};
  42. std::vector<const CArtifact*> constituents; // Artifacts IDs a combined artifact consists of, or nullptr.
  43. std::set<const CArtifact*> partOf; // Reverse map of constituents - combined arts that include this art
  44. bool fused;
  45. public:
  46. bool isCombined() const;
  47. const std::vector<const CArtifact*> & getConstituents() const;
  48. const std::set<const CArtifact*> & getPartOf() const;
  49. void setFused(bool isFused);
  50. bool isFused() const;
  51. bool hasParts() const;
  52. };
  53. class DLL_LINKAGE CScrollArtifact
  54. {
  55. protected:
  56. CScrollArtifact() = default;
  57. public:
  58. bool isScroll() const;
  59. };
  60. class DLL_LINKAGE CGrowingArtifact
  61. {
  62. protected:
  63. CGrowingArtifact() = default;
  64. std::vector <std::pair<ui16, Bonus>> bonusesPerLevel; // Bonus given each n levels
  65. std::vector <std::pair<ui16, Bonus>> thresholdBonuses; // After certain level they will be added once
  66. public:
  67. bool isGrowing() const;
  68. std::vector <std::pair<ui16, Bonus>> & getBonusesPerLevel();
  69. const std::vector <std::pair<ui16, Bonus>> & getBonusesPerLevel() const;
  70. std::vector <std::pair<ui16, Bonus>> & getThresholdBonuses();
  71. const std::vector <std::pair<ui16, Bonus>> & getThresholdBonuses() const;
  72. };
  73. // Container for artifacts. Not for instances.
  74. class DLL_LINKAGE CArtifact
  75. : public Artifact, public CBonusSystemNode, public CCombinedArtifact, public CScrollArtifact, public CGrowingArtifact
  76. {
  77. ArtifactID id;
  78. std::string image;
  79. std::string large; // big image for custom artifacts, used in drag & drop
  80. std::string advMapDef; // used for adventure map object
  81. std::string modScope;
  82. std::string identifier;
  83. int32_t iconIndex;
  84. uint32_t price;
  85. CreatureID warMachine;
  86. // Bearer Type => ids of slots where artifact can be placed
  87. std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> possibleSlots;
  88. public:
  89. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  90. EartClass aClass = ART_SPECIAL;
  91. bool onlyOnWaterMap;
  92. int32_t getIndex() const override;
  93. int32_t getIconIndex() const override;
  94. std::string getJsonKey() const override;
  95. std::string getModScope() const override;
  96. void registerIcons(const IconRegistar & cb) const override;
  97. ArtifactID getId() const override;
  98. const IBonusBearer * getBonusBearer() const override;
  99. std::string getDescriptionTranslated() const override;
  100. std::string getEventTranslated() const override;
  101. std::string getNameTranslated() const override;
  102. std::string getDescriptionTextID() const override;
  103. std::string getEventTextID() const override;
  104. std::string getNameTextID() const override;
  105. uint32_t getPrice() const override;
  106. CreatureID getWarMachine() const override;
  107. bool isBig() const override;
  108. bool isTradable() const override;
  109. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  110. std::string nodeName() const override;
  111. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  112. const std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> & getPossibleSlots() const;
  113. virtual bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE,
  114. bool assumeDestRemoved = false) const;
  115. void updateFrom(const JsonNode & data);
  116. // Is used for testing purposes only
  117. void setImage(int32_t iconIndex, std::string image, std::string large);
  118. CArtifact();
  119. ~CArtifact();
  120. friend class CArtHandler;
  121. };
  122. class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  123. {
  124. public:
  125. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  126. static CArtifact::EartClass stringToClass(const std::string & className); //TODO: rework EartClass to make this a constructor
  127. bool legalArtifact(const ArtifactID & id) const;
  128. static void makeItCreatureArt(CArtifact * a, bool onlyCreature = true);
  129. static void makeItCommanderArt(CArtifact * a, bool onlyCommander = true);
  130. ~CArtHandler();
  131. std::vector<JsonNode> loadLegacyData() override;
  132. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  133. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  134. void afterLoadFinalization() override;
  135. std::set<ArtifactID> getDefaultAllowed() const;
  136. protected:
  137. const std::vector<std::string> & getTypeNames() const override;
  138. std::shared_ptr<CArtifact> loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  139. private:
  140. void addSlot(CArtifact * art, const std::string & slotID) const;
  141. void loadSlots(CArtifact * art, const JsonNode & node) const;
  142. void loadClass(CArtifact * art, const JsonNode & node) const;
  143. void loadType(CArtifact * art, const JsonNode & node) const;
  144. void loadComponents(CArtifact * art, const JsonNode & node);
  145. };
  146. VCMI_LIB_NAMESPACE_END