CArtHandler.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #pragma once
  2. #include "../lib/HeroBonus.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. /*
  5. * CArtHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CDefHandler;
  14. class CArtifact;
  15. class CGHeroInstance;
  16. struct ArtifactLocation;
  17. class CArtifactSet;
  18. class CArtifactInstance;
  19. namespace ArtifactPosition
  20. {
  21. enum ArtifactPosition
  22. {
  23. PRE_FIRST = -1,
  24. HEAD, SHOULDERS, NECK, RIGHT_HAND, LEFT_HAND, TORSO, RIGHT_RING, LEFT_RING, FEET, MISC1, MISC2, MISC3, MISC4,
  25. MACH1, MACH2, MACH3, MACH4, SPELLBOOK, MISC5,
  26. AFTER_LAST,
  27. //cres
  28. CREATURE_SLOT = 0,
  29. COMMANDER1 = 0, COMMANDER2, COMMANDER3, COMMANDER4, COMMANDER5, COMMANDER6, COMMANDER_AFTER_LAST
  30. };
  31. }
  32. namespace ArtBearer
  33. {
  34. enum
  35. {
  36. HERO, CREATURE, COMMANDER
  37. };
  38. }
  39. class DLL_LINKAGE CArtifact : public CBonusSystemNode //container for artifacts
  40. {
  41. protected:
  42. std::string name, description; //set if custom
  43. public:
  44. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  45. const std::string &Name() const; //getter
  46. const std::string &Description() const; //getter
  47. bool isBig () const;
  48. void setDescription (std::string desc);
  49. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  50. std::string nodeName() const OVERRIDE;
  51. virtual void levelUpArtifact (CArtifactInstance * art){};
  52. ui32 price;
  53. bmap<ui8, std::vector<ui16> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  54. std::vector<ui32> * constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  55. std::vector<ui32> * constituentOf; // Reverse map of constituents.
  56. EartClass aClass;
  57. si32 id;
  58. template <typename Handler> void serialize(Handler &h, const int version)
  59. {
  60. h & static_cast<CBonusSystemNode&>(*this);
  61. h & name & description & price & possibleSlots & constituents & constituentOf & aClass & id;
  62. }
  63. CArtifact();
  64. ~CArtifact();
  65. //override
  66. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  67. };
  68. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  69. {
  70. public:
  71. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  72. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  73. void levelUpArtifact (CArtifactInstance * art);
  74. template <typename Handler> void serialize(Handler &h, const int version)
  75. {
  76. h & static_cast<CArtifact&>(*this);
  77. h & bonusesPerLevel & thresholdBonuses;
  78. }
  79. };
  80. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  81. {
  82. protected:
  83. void init();
  84. CArtifactInstance(CArtifact *Art);
  85. public:
  86. CArtifactInstance();
  87. ConstTransitivePtr<CArtifact> artType;
  88. si32 id; //id of the instance
  89. //CArtifactInstance(int aid);
  90. std::string nodeName() const OVERRIDE;
  91. void deserializationFix();
  92. void setType(CArtifact *Art);
  93. int firstAvailableSlot(const CArtifactSet *h) const;
  94. int firstBackpackSlot(const CArtifactSet *h) const;
  95. int getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  96. virtual bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const;
  97. bool canBePutAt(const ArtifactLocation al, bool assumeDestRemoved = false) const; //forwards to the above one
  98. virtual bool canBeDisassembled() const;
  99. virtual void putAt(ArtifactLocation al);
  100. virtual void removeFrom(ArtifactLocation al);
  101. 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
  102. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  103. void move(ArtifactLocation src, ArtifactLocation dst);
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & static_cast<CBonusSystemNode&>(*this);
  107. h & artType & id;
  108. BONUS_TREE_DESERIALIZATION_FIX
  109. }
  110. static CArtifactInstance *createScroll(const CSpell *s);
  111. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  112. static CArtifactInstance *createNewArtifactInstance(int aid);
  113. };
  114. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  115. {
  116. CCombinedArtifactInstance(CArtifact *Art);
  117. public:
  118. struct ConstituentInfo
  119. {
  120. ConstTransitivePtr<CArtifactInstance> art;
  121. si16 slot;
  122. template <typename Handler> void serialize(Handler &h, const int version)
  123. {
  124. h & art & slot;
  125. }
  126. bool operator==(const ConstituentInfo &rhs) const;
  127. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  128. };
  129. std::vector<ConstituentInfo> constituentsInfo;
  130. bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const OVERRIDE;
  131. bool canBeDisassembled() const OVERRIDE;
  132. void putAt(ArtifactLocation al) OVERRIDE;
  133. void removeFrom(ArtifactLocation al) OVERRIDE;
  134. bool isPart(const CArtifactInstance *supposedPart) const OVERRIDE;
  135. void createConstituents();
  136. void addAsConstituent(CArtifactInstance *art, int slot);
  137. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replcaed with us (combined art), not lock
  138. CCombinedArtifactInstance();
  139. void deserializationFix();
  140. friend class CArtifactInstance;
  141. friend struct AssembledArtifact;
  142. template <typename Handler> void serialize(Handler &h, const int version)
  143. {
  144. h & static_cast<CArtifactInstance&>(*this);
  145. h & constituentsInfo;
  146. BONUS_TREE_DESERIALIZATION_FIX
  147. }
  148. };
  149. class DLL_LINKAGE CArtHandler //handles artifacts
  150. {
  151. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, ILimiter * limiter = NULL, int additionalinfo = 0);
  152. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype, IPropagator* propagator, int additionalinfo = 0);
  153. public:
  154. std::vector<CArtifact*> treasures, minors, majors, relics;
  155. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  156. std::vector<CArtifact *> allowedArtifacts;
  157. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  158. std::set<ui32> growingArtifacts;
  159. void loadArtifacts(bool onlyTxt);
  160. void sortArts();
  161. void addBonuses();
  162. void clear();
  163. void clearHlpLists();
  164. ui16 getRandomArt (int flags);
  165. ui16 getArtSync (ui32 rand, int flags);
  166. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  167. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  168. void erasePickedArt (si32 id);
  169. bool isBigArtifact (ui32 artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  170. // void equipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art) const;
  171. // void unequipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const;
  172. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  173. static int convertMachineID(int id, bool creToArt);
  174. void makeItCreatureArt (int aid, bool onlyCreature = true);
  175. void makeItCommanderArt (int aid, bool onlyCommander = true);
  176. CArtHandler();
  177. ~CArtHandler();
  178. template <typename Handler> void serialize(Handler &h, const int version)
  179. {
  180. h & artifacts & allowedArtifacts & treasures & minors & majors & relics
  181. & growingArtifacts;
  182. //if(!h.saving) sortArts();
  183. }
  184. };
  185. struct DLL_LINKAGE ArtSlotInfo
  186. {
  187. ConstTransitivePtr<CArtifactInstance> artifact;
  188. ui8 locked; //if locked, then artifact points to the combined artifact
  189. ArtSlotInfo()
  190. {
  191. locked = false;
  192. }
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & artifact & locked;
  196. }
  197. };
  198. class DLL_LINKAGE CArtifactSet
  199. {
  200. public:
  201. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  202. bmap<ui16, 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
  203. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  204. void setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked);
  205. void eraseArtSlot(ui16 slot);
  206. const ArtSlotInfo *getSlot(ui16 pos) const;
  207. const CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true) const; //NULL - no artifact
  208. CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true); //NULL - no artifact
  209. si32 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)
  210. si32 getArtPos(const CArtifactInstance *art) const;
  211. const CArtifactInstance *getArtByInstanceId(int artInstId) const;
  212. bool hasArt(ui32 aid, bool onlyWorn = false) const; //checks if hero possess artifact of given id (either in backack or worn)
  213. bool isPositionFree(ui16 pos, bool onlyLockCheck = false) const;
  214. si32 getArtTypeId(ui16 pos) const;
  215. virtual ui8 bearerType() const = 0;
  216. virtual ~CArtifactSet();
  217. template <typename Handler> void serialize(Handler &h, const int version)
  218. {
  219. h & artifactsInBackpack & artifactsWorn;
  220. }
  221. void artDeserializationFix(CBonusSystemNode *node);
  222. };