CArtHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #pragma once
  2. #include "../lib/HeroBonus.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. #include "JsonNode.h"
  5. #include "GameConstants.h"
  6. #include "IHandlerBase.h"
  7. /*
  8. * CArtHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CArtHandler;
  17. class CDefHandler;
  18. class CArtifact;
  19. class CGHeroInstance;
  20. struct ArtifactLocation;
  21. class CArtifactSet;
  22. class CArtifactInstance;
  23. #define ART_BEARER_LIST \
  24. ART_BEARER(HERO)\
  25. ART_BEARER(CREATURE)\
  26. ART_BEARER(COMMANDER)
  27. namespace ArtBearer
  28. {
  29. enum ArtBearer
  30. {
  31. #define ART_BEARER(x) x,
  32. ART_BEARER_LIST
  33. #undef ART_BEARER
  34. };
  35. }
  36. class DLL_LINKAGE CArtifact : public CBonusSystemNode //container for artifacts
  37. {
  38. protected:
  39. std::string name, description; //set if custom
  40. std::string eventText; //short story displayed upon picking
  41. public:
  42. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  43. std::string image;
  44. std::string large; // big image for cutom artifacts, used in drag & drop
  45. std::string advMapDef; //used for adventure map object
  46. si32 iconIndex;
  47. const std::string &Name() const; //getter
  48. const std::string &Description() const; //getter
  49. const std::string &EventText() const;
  50. bool isBig () const;
  51. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  52. std::string nodeName() const override;
  53. void addNewBonus(Bonus *b) override;
  54. virtual void levelUpArtifact (CArtifactInstance * art){};
  55. ui32 price;
  56. bmap<ArtBearer::ArtBearer, std::vector<ArtifactPosition> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  57. std::unique_ptr<std::vector<CArtifact *> > constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  58. std::vector<CArtifact *> constituentOf; // Reverse map of constituents - combined arts that include this art
  59. EartClass aClass;
  60. ArtifactID id;
  61. template <typename Handler> void serialize(Handler &h, const int version)
  62. {
  63. h & static_cast<CBonusSystemNode&>(*this);
  64. h & name & description & eventText & image & large & advMapDef & iconIndex &
  65. price & possibleSlots & constituents & constituentOf & aClass & id;
  66. }
  67. CArtifact();
  68. ~CArtifact();
  69. friend class CArtHandler;
  70. };
  71. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  72. {
  73. public:
  74. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  75. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  76. void levelUpArtifact (CArtifactInstance * art);
  77. template <typename Handler> void serialize(Handler &h, const int version)
  78. {
  79. h & static_cast<CArtifact&>(*this);
  80. h & bonusesPerLevel & thresholdBonuses;
  81. }
  82. };
  83. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  84. {
  85. protected:
  86. void init();
  87. CArtifactInstance(CArtifact *Art);
  88. public:
  89. CArtifactInstance();
  90. ConstTransitivePtr<CArtifact> artType;
  91. ArtifactInstanceID id;
  92. //CArtifactInstance(int aid);
  93. std::string nodeName() const override;
  94. void deserializationFix();
  95. void setType(CArtifact *Art);
  96. ArtifactPosition firstAvailableSlot(const CArtifactSet *h) const;
  97. ArtifactPosition firstBackpackSlot(const CArtifactSet *h) const;
  98. SpellID getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  99. virtual bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const;
  100. bool canBePutAt(const ArtifactLocation al, bool assumeDestRemoved = false) const; //forwards to the above one
  101. virtual bool canBeDisassembled() const;
  102. virtual void putAt(ArtifactLocation al);
  103. virtual void removeFrom(ArtifactLocation al);
  104. virtual bool isPart(const CArtifactInstance *supposedPart) const; //checks if this a part of this artifact: artifact instance is a part of itself, additionally truth is returned for consituents of combined arts
  105. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  106. void move(ArtifactLocation src, ArtifactLocation dst);
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. h & static_cast<CBonusSystemNode&>(*this);
  110. h & artType & id;
  111. BONUS_TREE_DESERIALIZATION_FIX
  112. }
  113. static CArtifactInstance *createScroll(const CSpell *s);
  114. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  115. static CArtifactInstance *createNewArtifactInstance(int aid);
  116. };
  117. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  118. {
  119. CCombinedArtifactInstance(CArtifact *Art);
  120. public:
  121. struct ConstituentInfo
  122. {
  123. ConstTransitivePtr<CArtifactInstance> art;
  124. ArtifactPosition slot;
  125. template <typename Handler> void serialize(Handler &h, const int version)
  126. {
  127. h & art & slot;
  128. }
  129. bool operator==(const ConstituentInfo &rhs) const;
  130. ConstituentInfo(CArtifactInstance *art = NULL, ArtifactPosition slot = ArtifactPosition::PRE_FIRST);
  131. };
  132. std::vector<ConstituentInfo> constituentsInfo;
  133. bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const override;
  134. bool canBeDisassembled() const override;
  135. void putAt(ArtifactLocation al) override;
  136. void removeFrom(ArtifactLocation al) override;
  137. bool isPart(const CArtifactInstance *supposedPart) const override;
  138. void createConstituents();
  139. void addAsConstituent(CArtifactInstance *art, ArtifactPosition slot);
  140. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replcaed with us (combined art), not lock
  141. CCombinedArtifactInstance();
  142. void deserializationFix();
  143. friend class CArtifactInstance;
  144. friend struct AssembledArtifact;
  145. template <typename Handler> void serialize(Handler &h, const int version)
  146. {
  147. h & static_cast<CArtifactInstance&>(*this);
  148. h & constituentsInfo;
  149. BONUS_TREE_DESERIALIZATION_FIX
  150. }
  151. };
  152. class DLL_LINKAGE CArtHandler : public IHandlerBase //handles artifacts
  153. {
  154. CArtifact * loadFromJson(const JsonNode & node);
  155. void addSlot(CArtifact * art, const std::string & slotID);
  156. void loadSlots(CArtifact * art, const JsonNode & node);
  157. void loadClass(CArtifact * art, const JsonNode & node);
  158. void loadType(CArtifact * art, const JsonNode & node);
  159. void loadComponents(CArtifact * art, const JsonNode & node);
  160. void loadGrowingArt(CGrowingArtifact * art, const JsonNode & node);
  161. void giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype = -1, Bonus::ValueType valType = Bonus::BASE_NUMBER, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalinfo = 0);
  162. void giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator, int additionalinfo = 0);
  163. void giveArtBonus(ArtifactID aid, Bonus *bonus);
  164. public:
  165. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  166. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  167. std::vector<CArtifact *> allowedArtifacts;
  168. std::set<ArtifactID> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  169. std::set<ArtifactID> growingArtifacts;
  170. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  171. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of gibven class. No side effects
  172. boost::optional<std::vector<CArtifact*>&> listFromClass(CArtifact::EartClass artifactClass);
  173. void erasePickedArt(ArtifactID id);
  174. ArtifactID getRandomArt (int flags);
  175. ArtifactID getArtSync (ui32 rand, int flags, bool erasePicked = false);
  176. bool legalArtifact(ArtifactID id);
  177. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  178. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  179. bool isBigArtifact (ArtifactID artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  180. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  181. static ArtifactID creatureToMachineID(CreatureID id);
  182. static CreatureID machineIDToCreature(ArtifactID id);
  183. void makeItCreatureArt (CArtifact * a, bool onlyCreature = true);
  184. void makeItCreatureArt (ArtifactID aid, bool onlyCreature = true);
  185. void makeItCommanderArt (CArtifact * a, bool onlyCommander = true);
  186. void makeItCommanderArt (ArtifactID aid, bool onlyCommander = true);
  187. CArtHandler();
  188. ~CArtHandler();
  189. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  190. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  191. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  192. std::vector<bool> getDefaultAllowed() const override;
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & artifacts & allowedArtifacts & treasures & minors & majors & relics
  196. & growingArtifacts;
  197. //if(!h.saving) sortArts();
  198. }
  199. };
  200. struct DLL_LINKAGE ArtSlotInfo
  201. {
  202. ConstTransitivePtr<CArtifactInstance> artifact;
  203. ui8 locked; //if locked, then artifact points to the combined artifact
  204. ArtSlotInfo()
  205. {
  206. locked = false;
  207. }
  208. template <typename Handler> void serialize(Handler &h, const int version)
  209. {
  210. h & artifact & locked;
  211. }
  212. };
  213. class DLL_LINKAGE CArtifactSet
  214. {
  215. public:
  216. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  217. bmap<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
  218. ArtSlotInfo &retreiveNewArtSlot(ArtifactPosition slot);
  219. void setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked);
  220. void eraseArtSlot(ArtifactPosition slot);
  221. const ArtSlotInfo *getSlot(ArtifactPosition pos) const;
  222. const CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true) const; //NULL - no artifact
  223. CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //NULL - no artifact
  224. ArtifactPosition getArtPos(int aid, bool onlyWorn = true) const; //looks for equipped artifact with given ID and returns its slot ID or -1 if none(if more than one such artifact lower ID is returned)
  225. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  226. const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const;
  227. bool hasArt(ui32 aid, bool onlyWorn = false) const; //checks if hero possess artifact of given id (either in backack or worn)
  228. bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const;
  229. si32 getArtTypeId(ArtifactPosition pos) const;
  230. virtual ArtBearer::ArtBearer bearerType() const = 0;
  231. virtual ~CArtifactSet();
  232. template <typename Handler> void serialize(Handler &h, const int version)
  233. {
  234. h & artifactsInBackpack & artifactsWorn;
  235. }
  236. void artDeserializationFix(CBonusSystemNode *node);
  237. };