CArtHandler.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "ConstTransitivePtr.h"
  16. #include "GameConstants.h"
  17. #include "IHandlerBase.h"
  18. #include "serializer/Serializeable.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. class CArtHandler;
  21. class CGHeroInstance;
  22. class CArtifactSet;
  23. class CArtifactInstance;
  24. class JsonSerializeFormat;
  25. #define ART_BEARER_LIST \
  26. ART_BEARER(HERO)\
  27. ART_BEARER(CREATURE)\
  28. ART_BEARER(COMMANDER)\
  29. ART_BEARER(ALTAR)
  30. namespace ArtBearer
  31. {
  32. enum ArtBearer
  33. {
  34. #define ART_BEARER(x) x,
  35. ART_BEARER_LIST
  36. #undef ART_BEARER
  37. };
  38. }
  39. class DLL_LINKAGE CCombinedArtifact
  40. {
  41. protected:
  42. CCombinedArtifact() : fused(false) {};
  43. std::vector<const CArtifact*> constituents; // Artifacts IDs a combined artifact consists of, or nullptr.
  44. std::set<const CArtifact*> partOf; // Reverse map of constituents - combined arts that include this art
  45. bool fused;
  46. public:
  47. bool isCombined() const;
  48. const std::vector<const CArtifact*> & getConstituents() const;
  49. const std::set<const CArtifact*> & getPartOf() const;
  50. void setFused(bool isFused);
  51. bool isFused() const;
  52. bool hasParts() const;
  53. };
  54. class DLL_LINKAGE CScrollArtifact
  55. {
  56. protected:
  57. CScrollArtifact() = default;
  58. public:
  59. bool isScroll() const;
  60. };
  61. class DLL_LINKAGE CGrowingArtifact
  62. {
  63. protected:
  64. CGrowingArtifact() = default;
  65. std::vector <std::pair<ui16, Bonus>> bonusesPerLevel; // Bonus given each n levels
  66. std::vector <std::pair<ui16, Bonus>> thresholdBonuses; // After certain level they will be added once
  67. public:
  68. bool isGrowing() const;
  69. std::vector <std::pair<ui16, Bonus>> & getBonusesPerLevel();
  70. const std::vector <std::pair<ui16, Bonus>> & getBonusesPerLevel() const;
  71. std::vector <std::pair<ui16, Bonus>> & getThresholdBonuses();
  72. const std::vector <std::pair<ui16, Bonus>> & getThresholdBonuses() const;
  73. };
  74. // Container for artifacts. Not for instances.
  75. class DLL_LINKAGE CArtifact
  76. : public Artifact, public CBonusSystemNode, public CCombinedArtifact, public CScrollArtifact, public CGrowingArtifact
  77. {
  78. ArtifactID id;
  79. std::string image;
  80. std::string large; // big image for custom artifacts, used in drag & drop
  81. std::string advMapDef; // used for adventure map object
  82. std::string modScope;
  83. std::string identifier;
  84. int32_t iconIndex;
  85. uint32_t price;
  86. CreatureID warMachine;
  87. // Bearer Type => ids of slots where artifact can be placed
  88. std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> possibleSlots;
  89. public:
  90. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  91. EartClass aClass = ART_SPECIAL;
  92. bool onlyOnWaterMap;
  93. int32_t getIndex() const override;
  94. int32_t getIconIndex() const override;
  95. std::string getJsonKey() const override;
  96. std::string getModScope() const override;
  97. void registerIcons(const IconRegistar & cb) const override;
  98. ArtifactID getId() const override;
  99. const IBonusBearer * getBonusBearer() const override;
  100. std::string getDescriptionTranslated() const override;
  101. std::string getEventTranslated() const override;
  102. std::string getNameTranslated() const override;
  103. std::string getDescriptionTextID() const override;
  104. std::string getEventTextID() const override;
  105. std::string getNameTextID() const override;
  106. uint32_t getPrice() const override;
  107. CreatureID getWarMachine() const override;
  108. bool isBig() const override;
  109. bool isTradable() const override;
  110. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  111. std::string nodeName() const override;
  112. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  113. const std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> & getPossibleSlots() const;
  114. virtual bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE,
  115. bool assumeDestRemoved = false) const;
  116. void updateFrom(const JsonNode & data);
  117. // Is used for testing purposes only
  118. void setImage(int32_t iconIndex, std::string image, std::string large);
  119. CArtifact();
  120. ~CArtifact();
  121. friend class CArtHandler;
  122. };
  123. class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  124. {
  125. public:
  126. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  127. static CArtifact::EartClass stringToClass(const std::string & className); //TODO: rework EartClass to make this a constructor
  128. bool legalArtifact(const ArtifactID & id) const;
  129. static void makeItCreatureArt(CArtifact * a, bool onlyCreature = true);
  130. static void makeItCommanderArt(CArtifact * a, bool onlyCommander = true);
  131. ~CArtHandler();
  132. std::vector<JsonNode> loadLegacyData() override;
  133. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  134. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  135. void afterLoadFinalization() override;
  136. std::set<ArtifactID> getDefaultAllowed() const;
  137. protected:
  138. const std::vector<std::string> & getTypeNames() const override;
  139. std::shared_ptr<CArtifact> loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  140. private:
  141. void addSlot(CArtifact * art, const std::string & slotID) const;
  142. void loadSlots(CArtifact * art, const JsonNode & node) const;
  143. void loadClass(CArtifact * art, const JsonNode & node) const;
  144. void loadType(CArtifact * art, const JsonNode & node) const;
  145. void loadComponents(CArtifact * art, const JsonNode & node);
  146. };
  147. struct DLL_LINKAGE ArtSlotInfo
  148. {
  149. CArtifactInstance * artifact;
  150. bool locked; //if locked, then artifact points to the combined artifact
  151. ArtSlotInfo() : artifact(nullptr), locked(false) {}
  152. const CArtifactInstance * getArt() const;
  153. template <typename Handler> void serialize(Handler & h)
  154. {
  155. h & artifact;
  156. h & locked;
  157. }
  158. };
  159. class DLL_LINKAGE CArtifactSet : public virtual Serializeable
  160. {
  161. public:
  162. using ArtPlacementMap = std::map<CArtifactInstance*, ArtifactPosition>;
  163. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  164. std::map<ArtifactPosition, ArtSlotInfo> artifactsWorn; //map<position,artifact_id>; positions: 0 - head; 1 - shoulders; 2 - neck; 3 - right hand; 4 - left hand; 5 - torso; 6 - right ring; 7 - left ring; 8 - feet; 9 - misc1; 10 - misc2; 11 - misc3; 12 - misc4; 13 - mach1; 14 - mach2; 15 - mach3; 16 - mach4; 17 - spellbook; 18 - misc5
  165. ArtSlotInfo artifactsTransitionPos; // Used as transition position for dragAndDrop artifact exchange
  166. const ArtSlotInfo * getSlot(const ArtifactPosition & pos) const;
  167. void lockSlot(const ArtifactPosition & pos);
  168. CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true) const;
  169. /// Looks for first artifact with given ID
  170. ArtifactPosition getArtPos(const ArtifactID & aid, bool onlyWorn = true, bool allowLocked = true) const;
  171. ArtifactPosition getArtPos(const CArtifactInstance * art) const;
  172. const CArtifactInstance * getArtByInstanceId(const ArtifactInstanceID & artInstId) const;
  173. bool hasArt(const ArtifactID & aid, bool onlyWorn = false, bool searchCombinedParts = false) const;
  174. bool isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck = false) const;
  175. virtual ArtBearer::ArtBearer bearerType() const = 0;
  176. virtual ArtPlacementMap putArtifact(const ArtifactPosition & slot, CArtifactInstance * art);
  177. virtual void removeArtifact(const ArtifactPosition & slot);
  178. virtual ~CArtifactSet() = default;
  179. template <typename Handler> void serialize(Handler &h)
  180. {
  181. h & artifactsInBackpack;
  182. h & artifactsWorn;
  183. }
  184. void artDeserializationFix(CBonusSystemNode *node);
  185. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName);
  186. const CArtifactInstance * getCombinedArtWithPart(const ArtifactID & partId) const;
  187. private:
  188. void serializeJsonHero(JsonSerializeFormat & handler);
  189. void serializeJsonCreature(JsonSerializeFormat & handler);
  190. void serializeJsonCommander(JsonSerializeFormat & handler);
  191. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot);//normal slots
  192. };
  193. // Used to try on artifacts before the claimed changes have been applied
  194. class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
  195. {
  196. public:
  197. CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
  198. explicit CArtifactFittingSet(const CArtifactSet & artSet);
  199. ArtBearer::ArtBearer bearerType() const override;
  200. protected:
  201. ArtBearer::ArtBearer bearer;
  202. };
  203. VCMI_LIB_NAMESPACE_END