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