CArtHandler.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. }
  81. static CArtifactInstance *createScroll(const CSpell *s);
  82. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  83. static CArtifactInstance *createNewArtifactInstance(int aid);
  84. };
  85. class DLL_EXPORT CCombinedArtifactInstance : public CArtifactInstance
  86. {
  87. CCombinedArtifactInstance(CArtifact *Art);
  88. public:
  89. struct ConstituentInfo
  90. {
  91. ConstTransitivePtr<CArtifactInstance> art;
  92. si16 slot;
  93. template <typename Handler> void serialize(Handler &h, const int version)
  94. {
  95. h & art & slot;
  96. }
  97. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  98. };
  99. std::vector<ConstituentInfo> constituentsInfo;
  100. bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const OVERRIDE;
  101. bool canBeDisassembled() const OVERRIDE;
  102. void putAt(CGHeroInstance *h, ui16 slot) OVERRIDE;
  103. void removeFrom(CGHeroInstance *h, ui16 slot) OVERRIDE;
  104. void createConstituents();
  105. void addAsConstituent(CArtifactInstance *art, int slot);
  106. CArtifactInstance *figureMainConstituent(ui16 slot); //main constituent is replcaed with us (combined art), not lock
  107. CCombinedArtifactInstance();
  108. void deserializationFix();
  109. friend class CArtifactInstance;
  110. friend class AssembledArtifact;
  111. template <typename Handler> void serialize(Handler &h, const int version)
  112. {
  113. h & static_cast<CArtifactInstance&>(*this);
  114. h & constituentsInfo;
  115. }
  116. };
  117. // class DLL_EXPORT IModableArt : public CArtifact //artifact which can have different properties, such as scroll or banner
  118. // { //used only for dynamic cast :P
  119. // public:
  120. // si32 ID; //used for smart serialization
  121. //
  122. // template <typename Handler> void serialize(Handler &h, const int version)
  123. // {
  124. // h & static_cast<CArtifact&>(*this);
  125. // h & ID;
  126. // }
  127. // };
  128. //
  129. // class DLL_EXPORT CScroll : public IModableArt // Spell Scroll
  130. // {
  131. // public:
  132. // CScroll(){spellid=0;};
  133. // CScroll(spelltype sid){spellid = sid;};
  134. // spelltype spellid;
  135. // void Init();
  136. // void SetProperty (int mod){spellid = mod;};
  137. // template <typename Handler> void serialize(Handler &h, const int version)
  138. // {
  139. // h & static_cast<IModableArt&>(*this);
  140. // h & spellid;
  141. // }
  142. // };
  143. //
  144. // class DLL_EXPORT CCustomizableArt : public IModableArt // Warlord's Banner with multiple options
  145. // {
  146. // public:
  147. // ui8 mode;
  148. // CCustomizableArt(){mode=0;};
  149. // void Init(){};
  150. // void SetProperty (int mod){};
  151. // template <typename Handler> void serialize(Handler &h, const int version)
  152. // {
  153. // h & static_cast<IModableArt&>(*this);
  154. // h & mode;
  155. // }
  156. // };
  157. //
  158. // class DLL_EXPORT CCommanderArt : public IModableArt // Growing with time
  159. // {
  160. // public:
  161. // ui32 level;
  162. // CCommanderArt(){level = 0;};
  163. // void Init(){};
  164. // void SetProperty (int mod){level = mod;};
  165. // void Upgrade(){level++;};
  166. // template <typename Handler> void serialize(Handler &h, const int version)
  167. // {
  168. // h & static_cast<IModableArt&>(*this);
  169. // h & level;
  170. // }
  171. // };
  172. class DLL_EXPORT CArtHandler //handles artifacts
  173. {
  174. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, ILimiter * limiter = NULL);
  175. public:
  176. std::vector<CArtifact*> treasures, minors, majors, relics;
  177. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  178. std::vector<CArtifact *> allowedArtifacts;
  179. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  180. //std::map<ui32, ui8> modableArtifacts; //1-scroll, 2-banner, 3-commander art with progressive bonus
  181. void loadArtifacts(bool onlyTxt);
  182. void sortArts();
  183. void addBonuses();
  184. void clear();
  185. void clearHlpLists();
  186. ui16 getRandomArt (int flags);
  187. ui16 getArtSync (ui32 rand, int flags);
  188. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  189. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  190. void erasePickedArt (si32 id);
  191. bool isBigArtifact (ui32 artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  192. // void equipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art) const;
  193. // void unequipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const;
  194. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  195. static int convertMachineID(int id, bool creToArt);
  196. CArtHandler();
  197. ~CArtHandler();
  198. template <typename Handler> void serialize(Handler &h, const int version)
  199. {
  200. h & artifacts & allowedArtifacts & treasures & minors & majors & relics;
  201. //if(!h.saving) sortArts();
  202. }
  203. };
  204. #endif // __CARTHANDLER_H__