CArtHandler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 CArtifact;
  20. class CGHeroInstance;
  21. struct ArtifactLocation;
  22. class CArtifactSet;
  23. class CArtifactInstance;
  24. class CRandomGenerator;
  25. class CMap;
  26. class JsonSerializeFormat;
  27. #define ART_BEARER_LIST \
  28. ART_BEARER(HERO)\
  29. ART_BEARER(CREATURE)\
  30. ART_BEARER(COMMANDER)
  31. namespace ArtBearer
  32. {
  33. enum ArtBearer
  34. {
  35. #define ART_BEARER(x) x,
  36. ART_BEARER_LIST
  37. #undef ART_BEARER
  38. };
  39. }
  40. class DLL_LINKAGE CArtifact : public Artifact, public CBonusSystemNode //container for artifacts
  41. {
  42. ArtifactID id;
  43. std::string modScope;
  44. std::string identifier;
  45. public:
  46. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  47. std::string image;
  48. std::string large; // big image for custom artifacts, used in drag & drop
  49. std::string advMapDef; //used for adventure map object
  50. si32 iconIndex = ArtifactID::NONE;
  51. ui32 price = 0;
  52. std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  53. std::unique_ptr<std::vector<CArtifact *> > constituents; // Artifacts IDs a combined artifact consists of, or nullptr.
  54. std::vector<CArtifact *> constituentOf; // Reverse map of constituents - combined arts that include this art
  55. EartClass aClass = ART_SPECIAL;
  56. CreatureID warMachine;
  57. int32_t getIndex() const override;
  58. int32_t getIconIndex() const override;
  59. std::string getJsonKey() const override;
  60. void registerIcons(const IconRegistar & cb) const override;
  61. ArtifactID getId() const override;
  62. virtual const IBonusBearer * getBonusBearer() const override;
  63. std::string getDescriptionTranslated() const override;
  64. std::string getEventTranslated() const override;
  65. std::string getNameTranslated() const override;
  66. std::string getDescriptionTextID() const override;
  67. std::string getEventTextID() const override;
  68. std::string getNameTextID() const override;
  69. uint32_t getPrice() const override;
  70. CreatureID getWarMachine() const override;
  71. bool isBig() const override;
  72. bool isTradable() const override;
  73. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  74. std::string nodeName() const override;
  75. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  76. virtual void levelUpArtifact (CArtifactInstance * art){};
  77. virtual bool canBeDisassembled() const;
  78. virtual bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE,
  79. bool assumeDestRemoved = false) const;
  80. void updateFrom(const JsonNode & data);
  81. void serializeJson(JsonSerializeFormat & handler);
  82. template <typename Handler> void serialize(Handler &h, const int version)
  83. {
  84. h & static_cast<CBonusSystemNode&>(*this);
  85. h & image;
  86. h & large;
  87. h & advMapDef;
  88. h & iconIndex;
  89. h & price;
  90. h & possibleSlots;
  91. h & constituents;
  92. h & constituentOf;
  93. h & aClass;
  94. h & id;
  95. h & modScope;
  96. h & identifier;
  97. h & warMachine;
  98. }
  99. CArtifact();
  100. ~CArtifact();
  101. friend class CArtHandler;
  102. };
  103. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  104. {
  105. public:
  106. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  107. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  108. void levelUpArtifact(CArtifactInstance * art) override;
  109. template <typename Handler> void serialize(Handler &h, const int version)
  110. {
  111. h & static_cast<CArtifact&>(*this);
  112. h & bonusesPerLevel;
  113. h & thresholdBonuses;
  114. }
  115. };
  116. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  117. {
  118. protected:
  119. void init();
  120. public:
  121. CArtifactInstance(CArtifact * Art);
  122. CArtifactInstance();
  123. ConstTransitivePtr<CArtifact> artType;
  124. ArtifactInstanceID id;
  125. std::string nodeName() const override;
  126. void deserializationFix();
  127. void setType(CArtifact *Art);
  128. std::string getDescription() const;
  129. SpellID getScrollSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  130. ArtifactID getTypeId() const;
  131. bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; //forwards to the above one
  132. virtual bool canBeDisassembled() const;
  133. virtual void putAt(ArtifactLocation al);
  134. virtual void removeFrom(ArtifactLocation al);
  135. /// Checks if this a part of this artifact: artifact instance is a part
  136. /// of itself, additionally truth is returned for constituents of combined arts
  137. virtual bool isPart(const CArtifactInstance *supposedPart) const;
  138. void move(const ArtifactLocation & src,const ArtifactLocation & dst);
  139. template <typename Handler> void serialize(Handler &h, const int version)
  140. {
  141. h & static_cast<CBonusSystemNode&>(*this);
  142. h & artType;
  143. h & id;
  144. BONUS_TREE_DESERIALIZATION_FIX
  145. }
  146. };
  147. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  148. {
  149. public:
  150. CCombinedArtifactInstance(CArtifact * Art);
  151. struct ConstituentInfo
  152. {
  153. ConstTransitivePtr<CArtifactInstance> art;
  154. ArtifactPosition slot;
  155. template <typename Handler> void serialize(Handler &h, const int version)
  156. {
  157. h & art;
  158. h & slot;
  159. }
  160. bool operator==(const ConstituentInfo &rhs) const;
  161. ConstituentInfo(CArtifactInstance * art = nullptr, const ArtifactPosition & slot = ArtifactPosition::PRE_FIRST);
  162. };
  163. std::vector<ConstituentInfo> constituentsInfo;
  164. void removeFrom(ArtifactLocation al) override;
  165. bool isPart(const CArtifactInstance *supposedPart) const override;
  166. void createConstituents();
  167. void addAsConstituent(CArtifactInstance * art, const ArtifactPosition & slot);
  168. CCombinedArtifactInstance() = default;
  169. void deserializationFix();
  170. friend class CArtifactInstance;
  171. friend struct AssembledArtifact;
  172. template <typename Handler> void serialize(Handler &h, const int version)
  173. {
  174. h & static_cast<CArtifactInstance&>(*this);
  175. h & constituentsInfo;
  176. BONUS_TREE_DESERIALIZATION_FIX
  177. }
  178. };
  179. class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  180. {
  181. public:
  182. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  183. std::vector<CArtifact *> allowedArtifacts;
  184. std::set<ArtifactID> growingArtifacts;
  185. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  186. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of given class. No side effects
  187. static CArtifact::EartClass stringToClass(const std::string & className); //TODO: rework EartClass to make this a constructor
  188. /// Gets a artifact ID randomly and removes the selected artifact from this handler.
  189. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags);
  190. ArtifactID pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts);
  191. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts);
  192. bool legalArtifact(const ArtifactID & id);
  193. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  194. static void makeItCreatureArt(CArtifact * a, bool onlyCreature = true);
  195. static void makeItCommanderArt(CArtifact * a, bool onlyCommander = true);
  196. ~CArtHandler();
  197. std::vector<JsonNode> loadLegacyData() override;
  198. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  199. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  200. void afterLoadFinalization() override;
  201. std::vector<bool> getDefaultAllowed() const override;
  202. template <typename Handler> void serialize(Handler &h, const int version)
  203. {
  204. h & objects;
  205. h & allowedArtifacts;
  206. h & treasures;
  207. h & minors;
  208. h & majors;
  209. h & relics;
  210. h & growingArtifacts;
  211. }
  212. protected:
  213. const std::vector<std::string> & getTypeNames() const override;
  214. CArtifact * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  215. private:
  216. void addSlot(CArtifact * art, const std::string & slotID) const;
  217. void loadSlots(CArtifact * art, const JsonNode & node) const;
  218. void loadClass(CArtifact * art, const JsonNode & node) const;
  219. void loadType(CArtifact * art, const JsonNode & node) const;
  220. void loadComponents(CArtifact * art, const JsonNode & node);
  221. void loadGrowingArt(CGrowingArtifact * art, const JsonNode & node) const;
  222. void erasePickedArt(const ArtifactID & id);
  223. };
  224. struct DLL_LINKAGE ArtSlotInfo
  225. {
  226. ConstTransitivePtr<CArtifactInstance> artifact;
  227. ui8 locked; //if locked, then artifact points to the combined artifact
  228. ArtSlotInfo() : locked(false) {}
  229. const CArtifactInstance * getArt() const;
  230. template <typename Handler> void serialize(Handler & h, const int version)
  231. {
  232. h & artifact;
  233. h & locked;
  234. }
  235. };
  236. class DLL_LINKAGE CArtifactSet
  237. {
  238. public:
  239. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  240. 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
  241. std::vector<ArtSlotInfo> artifactsTransitionPos; // Used as transition position for dragAndDrop artifact exchange
  242. ArtSlotInfo & retrieveNewArtSlot(const ArtifactPosition & slot);
  243. void setNewArtSlot(const ArtifactPosition & slot, CArtifactInstance * art, bool locked);
  244. void eraseArtSlot(const ArtifactPosition & slot);
  245. const ArtSlotInfo * getSlot(const ArtifactPosition & pos) const;
  246. const CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true) const; //nullptr - no artifact
  247. CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true); //nullptr - no artifact
  248. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  249. /// (if more than one such artifact lower ID is returned)
  250. ArtifactPosition getArtPos(const ArtifactID & aid, bool onlyWorn = true, bool allowLocked = true) const;
  251. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  252. ArtifactPosition getArtBackpackPos(const ArtifactID & aid) const;
  253. std::vector<ArtifactPosition> getAllArtPositions(const ArtifactID & aid, bool onlyWorn, bool allowLocked, bool getAll) const;
  254. std::vector<ArtifactPosition> getBackpackArtPositions(const ArtifactID & aid) const;
  255. const CArtifactInstance * getArtByInstanceId(const ArtifactInstanceID & artInstId) const;
  256. const ArtifactPosition getSlotByInstance(const CArtifactInstance * artInst) const;
  257. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  258. const CArtifactInstance * getHiddenArt(const ArtifactID & aid) const;
  259. const CCombinedArtifactInstance * getAssemblyByConstituent(const ArtifactID & aid) const;
  260. /// Checks if hero possess artifact of given id (either in backack or worn)
  261. bool hasArt(const ArtifactID & aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const;
  262. bool hasArtBackpack(const ArtifactID & aid) const;
  263. bool isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck = false) const;
  264. unsigned getArtPosCount(const ArtifactID & aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const;
  265. virtual ArtBearer::ArtBearer bearerType() const = 0;
  266. virtual void putArtifact(ArtifactPosition slot, CArtifactInstance * art);
  267. void removeArtifact(ArtifactPosition slot);
  268. virtual ~CArtifactSet();
  269. template <typename Handler> void serialize(Handler &h, const int version)
  270. {
  271. h & artifactsInBackpack;
  272. h & artifactsWorn;
  273. }
  274. void artDeserializationFix(CBonusSystemNode *node);
  275. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map);
  276. protected:
  277. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(const ArtifactID & aid) const;
  278. private:
  279. void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
  280. void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
  281. void serializeJsonCommander(JsonSerializeFormat & handler, CMap * map);
  282. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map);//normal slots
  283. };
  284. // Used to try on artifacts before the claimed changes have been applied
  285. class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
  286. {
  287. public:
  288. CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
  289. ArtBearer::ArtBearer bearerType() const override;
  290. protected:
  291. ArtBearer::ArtBearer Bearer;
  292. };
  293. VCMI_LIB_NAMESPACE_END