CArtHandler.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "GameConstants.h"
  16. #include "IHandlerBase.h"
  17. #include "serializer/Serializeable.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CArtHandler;
  20. class CGHeroInstance;
  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. struct DLL_LINKAGE ArtSlotInfo
  147. {
  148. CArtifactInstance * artifact;
  149. bool locked; //if locked, then artifact points to the combined artifact
  150. ArtSlotInfo() : artifact(nullptr), locked(false) {}
  151. const CArtifactInstance * getArt() const;
  152. template <typename Handler> void serialize(Handler & h)
  153. {
  154. h & artifact;
  155. h & locked;
  156. }
  157. };
  158. class DLL_LINKAGE CArtifactSet : public virtual Serializeable
  159. {
  160. public:
  161. using ArtPlacementMap = std::map<CArtifactInstance*, ArtifactPosition>;
  162. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  163. 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
  164. ArtSlotInfo artifactsTransitionPos; // Used as transition position for dragAndDrop artifact exchange
  165. const ArtSlotInfo * getSlot(const ArtifactPosition & pos) const;
  166. void lockSlot(const ArtifactPosition & pos);
  167. CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true) const;
  168. /// Looks for first artifact with given ID
  169. ArtifactPosition getArtPos(const ArtifactID & aid, bool onlyWorn = true, bool allowLocked = true) const;
  170. ArtifactPosition getArtPos(const CArtifactInstance * art) const;
  171. const CArtifactInstance * getArtByInstanceId(const ArtifactInstanceID & artInstId) const;
  172. bool hasArt(const ArtifactID & aid, bool onlyWorn = false, bool searchCombinedParts = false) const;
  173. bool isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck = false) const;
  174. virtual ArtBearer::ArtBearer bearerType() const = 0;
  175. virtual ArtPlacementMap putArtifact(const ArtifactPosition & slot, CArtifactInstance * art);
  176. virtual void removeArtifact(const ArtifactPosition & slot);
  177. virtual ~CArtifactSet() = default;
  178. template <typename Handler> void serialize(Handler &h)
  179. {
  180. h & artifactsInBackpack;
  181. h & artifactsWorn;
  182. }
  183. void artDeserializationFix(CBonusSystemNode *node);
  184. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName);
  185. const CArtifactInstance * getCombinedArtWithPart(const ArtifactID & partId) const;
  186. private:
  187. void serializeJsonHero(JsonSerializeFormat & handler);
  188. void serializeJsonCreature(JsonSerializeFormat & handler);
  189. void serializeJsonCommander(JsonSerializeFormat & handler);
  190. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot);//normal slots
  191. };
  192. // Used to try on artifacts before the claimed changes have been applied
  193. class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
  194. {
  195. public:
  196. CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
  197. explicit CArtifactFittingSet(const CArtifactSet & artSet);
  198. ArtBearer::ArtBearer bearerType() const override;
  199. protected:
  200. ArtBearer::ArtBearer bearer;
  201. };
  202. VCMI_LIB_NAMESPACE_END