CArtHandler.h 7.7 KB

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