CArtHandler.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. VCMI_LIB_NAMESPACE_BEGIN
  18. class CArtHandler;
  19. class CGHeroInstance;
  20. class CArtifactSet;
  21. class CArtifactInstance;
  22. class CRandomGenerator;
  23. class CMap;
  24. class JsonSerializeFormat;
  25. #define ART_BEARER_LIST \
  26. ART_BEARER(HERO)\
  27. ART_BEARER(CREATURE)\
  28. ART_BEARER(COMMANDER)
  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() = default;
  42. std::vector<CArtifact*> constituents; // Artifacts IDs a combined artifact consists of, or nullptr.
  43. std::vector<CArtifact*> partOf; // Reverse map of constituents - combined arts that include this art
  44. public:
  45. bool isCombined() const;
  46. const std::vector<CArtifact*> & getConstituents() const;
  47. const std::vector<CArtifact*> & getPartOf() const;
  48. };
  49. class DLL_LINKAGE CScrollArtifact
  50. {
  51. protected:
  52. CScrollArtifact() = default;
  53. public:
  54. bool isScroll() const;
  55. };
  56. class DLL_LINKAGE CGrowingArtifact
  57. {
  58. protected:
  59. CGrowingArtifact() = default;
  60. std::vector <std::pair<ui16, Bonus>> bonusesPerLevel; // Bonus given each n levels
  61. std::vector <std::pair<ui16, Bonus>> thresholdBonuses; // After certain level they will be added once
  62. public:
  63. bool isGrowing() const;
  64. std::vector <std::pair<ui16, Bonus>> & getBonusesPerLevel();
  65. const std::vector <std::pair<ui16, Bonus>> & getBonusesPerLevel() const;
  66. std::vector <std::pair<ui16, Bonus>> & getThresholdBonuses();
  67. const std::vector <std::pair<ui16, Bonus>> & getThresholdBonuses() const;
  68. };
  69. // Container for artifacts. Not for instances.
  70. class DLL_LINKAGE CArtifact
  71. : public Artifact, public CBonusSystemNode, public CCombinedArtifact, public CScrollArtifact, public CGrowingArtifact
  72. {
  73. ArtifactID id;
  74. std::string image;
  75. std::string large; // big image for custom artifacts, used in drag & drop
  76. std::string advMapDef; // used for adventure map object
  77. std::string modScope;
  78. std::string identifier;
  79. int32_t iconIndex;
  80. uint32_t price;
  81. CreatureID warMachine;
  82. // Bearer Type => ids of slots where artifact can be placed
  83. std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> possibleSlots;
  84. public:
  85. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  86. EartClass aClass = ART_SPECIAL;
  87. bool onlyOnWaterMap;
  88. int32_t getIndex() const override;
  89. int32_t getIconIndex() const override;
  90. std::string getJsonKey() const override;
  91. void registerIcons(const IconRegistar & cb) const override;
  92. ArtifactID getId() const override;
  93. virtual const IBonusBearer * getBonusBearer() const override;
  94. std::string getDescriptionTranslated() const override;
  95. std::string getEventTranslated() const override;
  96. std::string getNameTranslated() const override;
  97. std::string getDescriptionTextID() const override;
  98. std::string getEventTextID() const override;
  99. std::string getNameTextID() const override;
  100. uint32_t getPrice() const override;
  101. CreatureID getWarMachine() const override;
  102. bool isBig() const override;
  103. bool isTradable() const override;
  104. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  105. std::string nodeName() const override;
  106. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  107. const std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> & getPossibleSlots() const;
  108. virtual bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE,
  109. bool assumeDestRemoved = false) const;
  110. void updateFrom(const JsonNode & data);
  111. // Is used for testing purposes only
  112. void setImage(int32_t iconIndex, std::string image, std::string large);
  113. CArtifact();
  114. ~CArtifact();
  115. friend class CArtHandler;
  116. };
  117. class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  118. {
  119. public:
  120. /// List of artifacts allowed on the map
  121. std::vector<const CArtifact *> allowedArtifacts;
  122. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  123. static CArtifact::EartClass stringToClass(const std::string & className); //TODO: rework EartClass to make this a constructor
  124. bool legalArtifact(const ArtifactID & id);
  125. void initAllowedArtifactsList(const std::set<ArtifactID> & allowed);
  126. static void makeItCreatureArt(CArtifact * a, bool onlyCreature = true);
  127. static void makeItCommanderArt(CArtifact * a, bool onlyCommander = true);
  128. ~CArtHandler();
  129. std::vector<JsonNode> loadLegacyData() override;
  130. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  131. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  132. void afterLoadFinalization() override;
  133. std::set<ArtifactID> getDefaultAllowed() const;
  134. protected:
  135. const std::vector<std::string> & getTypeNames() const override;
  136. CArtifact * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  137. private:
  138. void addSlot(CArtifact * art, const std::string & slotID) const;
  139. void loadSlots(CArtifact * art, const JsonNode & node) const;
  140. void loadClass(CArtifact * art, const JsonNode & node) const;
  141. void loadType(CArtifact * art, const JsonNode & node) const;
  142. void loadComponents(CArtifact * art, const JsonNode & node);
  143. };
  144. struct DLL_LINKAGE ArtSlotInfo
  145. {
  146. ConstTransitivePtr<CArtifactInstance> artifact;
  147. ui8 locked; //if locked, then artifact points to the combined artifact
  148. ArtSlotInfo() : locked(false) {}
  149. const CArtifactInstance * getArt() const;
  150. template <typename Handler> void serialize(Handler & h, const int version)
  151. {
  152. h & artifact;
  153. h & locked;
  154. }
  155. };
  156. class DLL_LINKAGE CArtifactSet
  157. {
  158. public:
  159. using ArtPlacementMap = std::map<CArtifactInstance*, ArtifactPosition>;
  160. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  161. 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
  162. std::vector<ArtSlotInfo> artifactsTransitionPos; // Used as transition position for dragAndDrop artifact exchange
  163. void setNewArtSlot(const ArtifactPosition & slot, ConstTransitivePtr<CArtifactInstance> art, bool locked);
  164. void eraseArtSlot(const ArtifactPosition & slot);
  165. const ArtSlotInfo * getSlot(const ArtifactPosition & pos) const;
  166. const CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true) const; //nullptr - no artifact
  167. CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true); //nullptr - no artifact
  168. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  169. /// (if more than one such artifact lower ID is returned)
  170. ArtifactPosition getArtPos(const ArtifactID & aid, bool onlyWorn = true, bool allowLocked = true) const;
  171. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  172. std::vector<ArtifactPosition> getAllArtPositions(const ArtifactID & aid, bool onlyWorn, bool allowLocked, bool getAll) const;
  173. std::vector<ArtifactPosition> getBackpackArtPositions(const ArtifactID & aid) const;
  174. const CArtifactInstance * getArtByInstanceId(const ArtifactInstanceID & artInstId) const;
  175. const ArtifactPosition getSlotByInstance(const CArtifactInstance * artInst) const;
  176. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  177. const CArtifactInstance * getHiddenArt(const ArtifactID & aid) const;
  178. const CArtifactInstance * getAssemblyByConstituent(const ArtifactID & aid) const;
  179. /// Checks if hero possess artifact of given id (either in backack or worn)
  180. bool hasArt(const ArtifactID & aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const;
  181. bool hasArtBackpack(const ArtifactID & aid) const;
  182. bool isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck = false) const;
  183. unsigned getArtPosCount(const ArtifactID & aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const;
  184. virtual ArtBearer::ArtBearer bearerType() const = 0;
  185. virtual ArtPlacementMap putArtifact(ArtifactPosition slot, CArtifactInstance * art);
  186. virtual void removeArtifact(ArtifactPosition slot);
  187. virtual ~CArtifactSet();
  188. template <typename Handler> void serialize(Handler &h, const int version)
  189. {
  190. h & artifactsInBackpack;
  191. h & artifactsWorn;
  192. }
  193. void artDeserializationFix(CBonusSystemNode *node);
  194. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map);
  195. protected:
  196. std::pair<const CArtifactInstance *, const CArtifactInstance *> searchForConstituent(const ArtifactID & aid) const;
  197. private:
  198. void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
  199. void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
  200. void serializeJsonCommander(JsonSerializeFormat & handler, CMap * map);
  201. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map);//normal slots
  202. };
  203. // Used to try on artifacts before the claimed changes have been applied
  204. class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
  205. {
  206. public:
  207. CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
  208. ArtBearer::ArtBearer bearerType() const override;
  209. protected:
  210. ArtBearer::ArtBearer Bearer;
  211. };
  212. VCMI_LIB_NAMESPACE_END