CArtHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. template <typename Handler> void serialize(Handler & h, const int version)
  49. {
  50. h & constituents;
  51. h & partOf;
  52. }
  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. template <typename Handler> void serialize(Handler & h, const int version)
  74. {
  75. h & bonusesPerLevel;
  76. h & thresholdBonuses;
  77. }
  78. };
  79. // Container for artifacts. Not for instances.
  80. class DLL_LINKAGE CArtifact
  81. : public Artifact, public CBonusSystemNode, public CCombinedArtifact, public CScrollArtifact, public CGrowingArtifact
  82. {
  83. ArtifactID id;
  84. std::string image;
  85. std::string large; // big image for custom artifacts, used in drag & drop
  86. std::string advMapDef; // used for adventure map object
  87. std::string modScope;
  88. std::string identifier;
  89. int32_t iconIndex;
  90. uint32_t price;
  91. CreatureID warMachine;
  92. // Bearer Type => ids of slots where artifact can be placed
  93. std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> possibleSlots;
  94. public:
  95. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  96. EartClass aClass = ART_SPECIAL;
  97. int32_t getIndex() const override;
  98. int32_t getIconIndex() const override;
  99. std::string getJsonKey() const override;
  100. void registerIcons(const IconRegistar & cb) const override;
  101. ArtifactID getId() const override;
  102. virtual const IBonusBearer * getBonusBearer() const override;
  103. std::string getDescriptionTranslated() const override;
  104. std::string getEventTranslated() const override;
  105. std::string getNameTranslated() const override;
  106. std::string getDescriptionTextID() const override;
  107. std::string getEventTextID() const override;
  108. std::string getNameTextID() const override;
  109. uint32_t getPrice() const override;
  110. CreatureID getWarMachine() const override;
  111. bool isBig() const override;
  112. bool isTradable() const override;
  113. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  114. std::string nodeName() const override;
  115. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  116. const std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition>> & getPossibleSlots() const;
  117. virtual bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE,
  118. bool assumeDestRemoved = false) const;
  119. void updateFrom(const JsonNode & data);
  120. // Is used for testing purposes only
  121. void setImage(int32_t iconIndex, std::string image, std::string large);
  122. template <typename Handler> void serialize(Handler & h, const int version)
  123. {
  124. h & static_cast<CBonusSystemNode&>(*this);
  125. h & static_cast<CCombinedArtifact&>(*this);
  126. h & static_cast<CGrowingArtifact&>(*this);
  127. h & image;
  128. h & large;
  129. h & advMapDef;
  130. h & iconIndex;
  131. h & price;
  132. h & possibleSlots;
  133. h & aClass;
  134. h & id;
  135. h & modScope;
  136. h & identifier;
  137. h & warMachine;
  138. }
  139. CArtifact();
  140. ~CArtifact();
  141. friend class CArtHandler;
  142. };
  143. class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  144. {
  145. public:
  146. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  147. std::vector<CArtifact *> allowedArtifacts;
  148. std::set<ArtifactID> growingArtifacts;
  149. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  150. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of given class. No side effects
  151. static CArtifact::EartClass stringToClass(const std::string & className); //TODO: rework EartClass to make this a constructor
  152. /// Gets a artifact ID randomly and removes the selected artifact from this handler.
  153. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags);
  154. ArtifactID pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts);
  155. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts);
  156. bool legalArtifact(const ArtifactID & id);
  157. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  158. static void makeItCreatureArt(CArtifact * a, bool onlyCreature = true);
  159. static void makeItCommanderArt(CArtifact * a, bool onlyCommander = true);
  160. ~CArtHandler();
  161. std::vector<JsonNode> loadLegacyData() override;
  162. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  163. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  164. void afterLoadFinalization() override;
  165. std::vector<bool> getDefaultAllowed() const override;
  166. template <typename Handler> void serialize(Handler &h, const int version)
  167. {
  168. h & objects;
  169. h & allowedArtifacts;
  170. h & treasures;
  171. h & minors;
  172. h & majors;
  173. h & relics;
  174. h & growingArtifacts;
  175. }
  176. protected:
  177. const std::vector<std::string> & getTypeNames() const override;
  178. CArtifact * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  179. private:
  180. void addSlot(CArtifact * art, const std::string & slotID) const;
  181. void loadSlots(CArtifact * art, const JsonNode & node) const;
  182. void loadClass(CArtifact * art, const JsonNode & node) const;
  183. void loadType(CArtifact * art, const JsonNode & node) const;
  184. void loadComponents(CArtifact * art, const JsonNode & node);
  185. void erasePickedArt(const ArtifactID & id);
  186. };
  187. struct DLL_LINKAGE ArtSlotInfo
  188. {
  189. ConstTransitivePtr<CArtifactInstance> artifact;
  190. ui8 locked; //if locked, then artifact points to the combined artifact
  191. ArtSlotInfo() : locked(false) {}
  192. const CArtifactInstance * getArt() const;
  193. template <typename Handler> void serialize(Handler & h, const int version)
  194. {
  195. h & artifact;
  196. h & locked;
  197. }
  198. };
  199. class DLL_LINKAGE CArtifactSet
  200. {
  201. public:
  202. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  203. 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
  204. std::vector<ArtSlotInfo> artifactsTransitionPos; // Used as transition position for dragAndDrop artifact exchange
  205. void setNewArtSlot(const ArtifactPosition & slot, CArtifactInstance * art, bool locked);
  206. void eraseArtSlot(const ArtifactPosition & slot);
  207. const ArtSlotInfo * getSlot(const ArtifactPosition & pos) const;
  208. const CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true) const; //nullptr - no artifact
  209. CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true); //nullptr - no artifact
  210. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  211. /// (if more than one such artifact lower ID is returned)
  212. ArtifactPosition getArtPos(const ArtifactID & aid, bool onlyWorn = true, bool allowLocked = true) const;
  213. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  214. ArtifactPosition getArtBackpackPos(const ArtifactID & aid) const;
  215. std::vector<ArtifactPosition> getAllArtPositions(const ArtifactID & aid, bool onlyWorn, bool allowLocked, bool getAll) const;
  216. std::vector<ArtifactPosition> getBackpackArtPositions(const ArtifactID & aid) const;
  217. const CArtifactInstance * getArtByInstanceId(const ArtifactInstanceID & artInstId) const;
  218. const ArtifactPosition getSlotByInstance(const CArtifactInstance * artInst) const;
  219. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  220. const CArtifactInstance * getHiddenArt(const ArtifactID & aid) const;
  221. const CArtifactInstance * getAssemblyByConstituent(const ArtifactID & aid) const;
  222. /// Checks if hero possess artifact of given id (either in backack or worn)
  223. bool hasArt(const ArtifactID & aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const;
  224. bool hasArtBackpack(const ArtifactID & aid) const;
  225. bool isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck = false) const;
  226. unsigned getArtPosCount(const ArtifactID & aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const;
  227. virtual ArtBearer::ArtBearer bearerType() const = 0;
  228. virtual void putArtifact(ArtifactPosition slot, CArtifactInstance * art);
  229. virtual void removeArtifact(ArtifactPosition slot);
  230. virtual ~CArtifactSet();
  231. template <typename Handler> void serialize(Handler &h, const int version)
  232. {
  233. h & artifactsInBackpack;
  234. h & artifactsWorn;
  235. }
  236. void artDeserializationFix(CBonusSystemNode *node);
  237. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map);
  238. protected:
  239. std::pair<const CArtifactInstance *, const CArtifactInstance *> searchForConstituent(const ArtifactID & aid) const;
  240. private:
  241. void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
  242. void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
  243. void serializeJsonCommander(JsonSerializeFormat & handler, CMap * map);
  244. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map);//normal slots
  245. };
  246. // Used to try on artifacts before the claimed changes have been applied
  247. class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
  248. {
  249. public:
  250. CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
  251. ArtBearer::ArtBearer bearerType() const override;
  252. protected:
  253. ArtBearer::ArtBearer Bearer;
  254. };
  255. VCMI_LIB_NAMESPACE_END