CArtHandler.h 9.6 KB

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