CArtHandler.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef __CARTHANDLER_H__
  2. #define __CARTHANDLER_H__
  3. #include "../global.h"
  4. #include "../lib/HeroBonus.h"
  5. #include <set>
  6. #include <list>
  7. #include "../lib/ConstTransitivePtr.h"
  8. /*
  9. * CArtHandler.h, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. class CDefHandler;
  18. class CArtifact;
  19. class CGHeroInstance;
  20. struct ArtifactLocation;
  21. class DLL_EXPORT CArtifact : public CBonusSystemNode //container for artifacts
  22. {
  23. protected:
  24. std::string name, description; //set if custom
  25. public:
  26. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  27. const std::string &Name() const; //getter
  28. const std::string &Description() const; //getter
  29. bool isBig () const;
  30. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  31. std::string nodeName() const OVERRIDE;
  32. ui32 price;
  33. std::vector<ui16> possibleSlots; //ids of slots where artifact can be placed
  34. std::vector<ui32> * constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  35. std::vector<ui32> * constituentOf; // Reverse map of constituents.
  36. EartClass aClass;
  37. si32 id;
  38. template <typename Handler> void serialize(Handler &h, const int version)
  39. {
  40. h & static_cast<CBonusSystemNode&>(*this);
  41. h & name & description & price & possibleSlots & constituents & constituentOf & aClass & id;
  42. }
  43. CArtifact();
  44. ~CArtifact();
  45. //override
  46. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  47. };
  48. class DLL_EXPORT CArtifactInstance : public CBonusSystemNode
  49. {
  50. protected:
  51. void init();
  52. CArtifactInstance(CArtifact *Art);
  53. public:
  54. CArtifactInstance();
  55. ConstTransitivePtr<CArtifact> artType;
  56. si32 id; //id of the instance
  57. //CArtifactInstance(int aid);
  58. std::string nodeName() const OVERRIDE;
  59. void deserializationFix();
  60. void setType(CArtifact *Art);
  61. int firstAvailableSlot(const CGHeroInstance *h) const;
  62. int firstBackpackSlot(const CGHeroInstance *h) const;
  63. int getGivenSpellID() const; //to be used with scrolls (and similiar arts), -1 if none
  64. virtual bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const;
  65. virtual bool canBeDisassembled() const;
  66. virtual void putAt(CGHeroInstance *h, ui16 slot);
  67. virtual void removeFrom(CGHeroInstance *h, ui16 slot);
  68. std::vector<const CArtifact *> assemblyPossibilities(const CGHeroInstance *h) const;
  69. void move(ArtifactLocation &src, ArtifactLocation &dst);
  70. template <typename Handler> void serialize(Handler &h, const int version)
  71. {
  72. h & static_cast<CBonusSystemNode&>(*this);
  73. h & artType & id;
  74. BONUS_TREE_DESERIALIZATION_FIX
  75. }
  76. static CArtifactInstance *createScroll(const CSpell *s);
  77. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  78. static CArtifactInstance *createNewArtifactInstance(int aid);
  79. };
  80. class DLL_EXPORT CCombinedArtifactInstance : public CArtifactInstance
  81. {
  82. CCombinedArtifactInstance(CArtifact *Art);
  83. public:
  84. struct ConstituentInfo
  85. {
  86. ConstTransitivePtr<CArtifactInstance> art;
  87. si16 slot;
  88. template <typename Handler> void serialize(Handler &h, const int version)
  89. {
  90. h & art & slot;
  91. }
  92. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  93. };
  94. std::vector<ConstituentInfo> constituentsInfo;
  95. bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const OVERRIDE;
  96. bool canBeDisassembled() const OVERRIDE;
  97. void putAt(CGHeroInstance *h, ui16 slot) OVERRIDE;
  98. void removeFrom(CGHeroInstance *h, ui16 slot) OVERRIDE;
  99. void createConstituents();
  100. void addAsConstituent(CArtifactInstance *art, int slot);
  101. CArtifactInstance *figureMainConstituent(ui16 slot); //main constituent is replcaed with us (combined art), not lock
  102. CCombinedArtifactInstance();
  103. void deserializationFix();
  104. friend class CArtifactInstance;
  105. friend struct AssembledArtifact;
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & static_cast<CArtifactInstance&>(*this);
  109. h & constituentsInfo;
  110. BONUS_TREE_DESERIALIZATION_FIX
  111. }
  112. };
  113. // class DLL_EXPORT IModableArt : public CArtifact //artifact which can have different properties, such as scroll or banner
  114. // { //used only for dynamic cast :P
  115. // public:
  116. // si32 ID; //used for smart serialization
  117. //
  118. // template <typename Handler> void serialize(Handler &h, const int version)
  119. // {
  120. // h & static_cast<CArtifact&>(*this);
  121. // h & ID;
  122. // }
  123. // };
  124. //
  125. // class DLL_EXPORT CScroll : public IModableArt // Spell Scroll
  126. // {
  127. // public:
  128. // CScroll(){spellid=0;};
  129. // CScroll(spelltype sid){spellid = sid;};
  130. // spelltype spellid;
  131. // void Init();
  132. // void SetProperty (int mod){spellid = mod;};
  133. // template <typename Handler> void serialize(Handler &h, const int version)
  134. // {
  135. // h & static_cast<IModableArt&>(*this);
  136. // h & spellid;
  137. // }
  138. // };
  139. //
  140. // class DLL_EXPORT CCustomizableArt : public IModableArt // Warlord's Banner with multiple options
  141. // {
  142. // public:
  143. // ui8 mode;
  144. // CCustomizableArt(){mode=0;};
  145. // void Init(){};
  146. // void SetProperty (int mod){};
  147. // template <typename Handler> void serialize(Handler &h, const int version)
  148. // {
  149. // h & static_cast<IModableArt&>(*this);
  150. // h & mode;
  151. // }
  152. // };
  153. //
  154. // class DLL_EXPORT CCommanderArt : public IModableArt // Growing with time
  155. // {
  156. // public:
  157. // ui32 level;
  158. // CCommanderArt(){level = 0;};
  159. // void Init(){};
  160. // void SetProperty (int mod){level = mod;};
  161. // void Upgrade(){level++;};
  162. // template <typename Handler> void serialize(Handler &h, const int version)
  163. // {
  164. // h & static_cast<IModableArt&>(*this);
  165. // h & level;
  166. // }
  167. // };
  168. class DLL_EXPORT CArtHandler //handles artifacts
  169. {
  170. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, ILimiter * limiter = NULL);
  171. public:
  172. std::vector<CArtifact*> treasures, minors, majors, relics;
  173. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  174. std::vector<CArtifact *> allowedArtifacts;
  175. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  176. //std::map<ui32, ui8> modableArtifacts; //1-scroll, 2-banner, 3-commander art with progressive bonus
  177. void loadArtifacts(bool onlyTxt);
  178. void sortArts();
  179. void addBonuses();
  180. void clear();
  181. void clearHlpLists();
  182. ui16 getRandomArt (int flags);
  183. ui16 getArtSync (ui32 rand, int flags);
  184. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  185. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  186. void erasePickedArt (si32 id);
  187. bool isBigArtifact (ui32 artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  188. // void equipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art) const;
  189. // void unequipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const;
  190. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  191. static int convertMachineID(int id, bool creToArt);
  192. CArtHandler();
  193. ~CArtHandler();
  194. template <typename Handler> void serialize(Handler &h, const int version)
  195. {
  196. h & artifacts & allowedArtifacts & treasures & minors & majors & relics;
  197. //if(!h.saving) sortArts();
  198. }
  199. };
  200. #endif // __CARTHANDLER_H__