CArtHandler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. void init();
  58. public:
  59. ConstTransitivePtr<CArtifact> artType;
  60. si32 id; //id of the instance
  61. CArtifactInstance();
  62. CArtifactInstance(CArtifact *Art);
  63. CArtifactInstance(int aid);
  64. std::string nodeName() const OVERRIDE;
  65. void setType(CArtifact *Art);
  66. int firstAvailableSlot(const CGHeroInstance *h) const;
  67. int firstBackpackSlot(const CGHeroInstance *h) const;
  68. virtual bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const;
  69. virtual bool canBeDisassembled() const;
  70. std::vector<const CArtifact *> assemblyPossibilities(const CGHeroInstance *h) const;
  71. void putAt(CGHeroInstance *h, ui16 slot);
  72. void removeFrom(CGHeroInstance *h, ui16 slot);
  73. template <typename Handler> void serialize(Handler &h, const int version)
  74. {
  75. h & static_cast<CBonusSystemNode&>(*this);
  76. h & artType & id;
  77. }
  78. static CArtifactInstance *createScroll(const CSpell *s);
  79. };
  80. class DLL_EXPORT CCombinedArtifactInstance : public CArtifactInstance
  81. {
  82. public:
  83. bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const OVERRIDE;
  84. bool canBeDisassembled() const OVERRIDE;
  85. };
  86. class DLL_EXPORT IModableArt : public CArtifact //artifact which can have different properties, such as scroll or banner
  87. { //used only for dynamic cast :P
  88. public:
  89. si32 ID; //used for smart serialization
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & static_cast<CArtifact&>(*this);
  93. h & ID;
  94. }
  95. };
  96. class DLL_EXPORT CScroll : public IModableArt // Spell Scroll
  97. {
  98. public:
  99. CScroll(){spellid=0;};
  100. CScroll(spelltype sid){spellid = sid;};
  101. spelltype spellid;
  102. void Init();
  103. void SetProperty (int mod){spellid = mod;};
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & static_cast<IModableArt&>(*this);
  107. h & spellid;
  108. }
  109. };
  110. class DLL_EXPORT CCustomizableArt : public IModableArt // Warlord's Banner with multiple options
  111. {
  112. public:
  113. ui8 mode;
  114. CCustomizableArt(){mode=0;};
  115. void Init(){};
  116. void SetProperty (int mod){};
  117. template <typename Handler> void serialize(Handler &h, const int version)
  118. {
  119. h & static_cast<IModableArt&>(*this);
  120. h & mode;
  121. }
  122. };
  123. class DLL_EXPORT CCommanderArt : public IModableArt // Growing with time
  124. {
  125. public:
  126. ui32 level;
  127. CCommanderArt(){level = 0;};
  128. void Init(){};
  129. void SetProperty (int mod){level = mod;};
  130. void Upgrade(){level++;};
  131. template <typename Handler> void serialize(Handler &h, const int version)
  132. {
  133. h & static_cast<IModableArt&>(*this);
  134. h & level;
  135. }
  136. };
  137. class DLL_EXPORT CArtHandler //handles artifacts
  138. {
  139. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, ILimiter * limiter = NULL);
  140. public:
  141. std::vector<CArtifact*> treasures, minors, majors, relics;
  142. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  143. std::vector<CArtifact *> allowedArtifacts;
  144. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  145. std::map<ui32, ui8> modableArtifacts; //1-scroll, 2-banner, 3-commander art with progressive bonus
  146. void loadArtifacts(bool onlyTxt);
  147. void sortArts();
  148. void addBonuses();
  149. void clear();
  150. void clearHlpLists();
  151. ui16 getRandomArt (int flags);
  152. ui16 getArtSync (ui32 rand, int flags);
  153. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  154. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  155. void erasePickedArt (si32 id);
  156. bool isBigArtifact (ui32 artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  157. void equipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art) const;
  158. void unequipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const;
  159. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  160. static int convertMachineID(int id, bool creToArt);
  161. CArtHandler();
  162. ~CArtHandler();
  163. template <typename Handler> void serialize(Handler &h, const int version)
  164. {
  165. h & artifacts & allowedArtifacts & treasures & minors & majors & relics;
  166. //if(!h.saving) sortArts();
  167. }
  168. };
  169. #endif // __CARTHANDLER_H__