CArtHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. virtual bool isPart(const CArtifactInstance *supposedPart) const; //checks if this a part of this artifact: artifact instance is a part of itself, additionally truth is returned for consituents of combined arts
  69. std::vector<const CArtifact *> assemblyPossibilities(const CGHeroInstance *h) const;
  70. void move(ArtifactLocation &src, ArtifactLocation &dst);
  71. template <typename Handler> void serialize(Handler &h, const int version)
  72. {
  73. h & static_cast<CBonusSystemNode&>(*this);
  74. h & artType & id;
  75. BONUS_TREE_DESERIALIZATION_FIX
  76. }
  77. static CArtifactInstance *createScroll(const CSpell *s);
  78. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  79. static CArtifactInstance *createNewArtifactInstance(int aid);
  80. };
  81. class DLL_EXPORT CCombinedArtifactInstance : public CArtifactInstance
  82. {
  83. CCombinedArtifactInstance(CArtifact *Art);
  84. public:
  85. struct ConstituentInfo
  86. {
  87. ConstTransitivePtr<CArtifactInstance> art;
  88. si16 slot;
  89. template <typename Handler> void serialize(Handler &h, const int version)
  90. {
  91. h & art & slot;
  92. }
  93. bool operator==(const ConstituentInfo &rhs) const;
  94. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  95. };
  96. std::vector<ConstituentInfo> constituentsInfo;
  97. bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const OVERRIDE;
  98. bool canBeDisassembled() const OVERRIDE;
  99. void putAt(CGHeroInstance *h, ui16 slot) OVERRIDE;
  100. void removeFrom(CGHeroInstance *h, ui16 slot) OVERRIDE;
  101. bool isPart(const CArtifactInstance *supposedPart) const OVERRIDE;
  102. void createConstituents();
  103. void addAsConstituent(CArtifactInstance *art, int slot);
  104. CArtifactInstance *figureMainConstituent(ui16 slot); //main constituent is replcaed with us (combined art), not lock
  105. CCombinedArtifactInstance();
  106. void deserializationFix();
  107. friend class CArtifactInstance;
  108. friend struct AssembledArtifact;
  109. template <typename Handler> void serialize(Handler &h, const int version)
  110. {
  111. h & static_cast<CArtifactInstance&>(*this);
  112. h & constituentsInfo;
  113. BONUS_TREE_DESERIALIZATION_FIX
  114. }
  115. };
  116. // class DLL_EXPORT IModableArt : public CArtifact //artifact which can have different properties, such as scroll or banner
  117. // { //used only for dynamic cast :P
  118. // public:
  119. // si32 ID; //used for smart serialization
  120. //
  121. // template <typename Handler> void serialize(Handler &h, const int version)
  122. // {
  123. // h & static_cast<CArtifact&>(*this);
  124. // h & ID;
  125. // }
  126. // };
  127. //
  128. // class DLL_EXPORT CScroll : public IModableArt // Spell Scroll
  129. // {
  130. // public:
  131. // CScroll(){spellid=0;};
  132. // CScroll(spelltype sid){spellid = sid;};
  133. // spelltype spellid;
  134. // void Init();
  135. // void SetProperty (int mod){spellid = mod;};
  136. // template <typename Handler> void serialize(Handler &h, const int version)
  137. // {
  138. // h & static_cast<IModableArt&>(*this);
  139. // h & spellid;
  140. // }
  141. // };
  142. //
  143. // class DLL_EXPORT CCustomizableArt : public IModableArt // Warlord's Banner with multiple options
  144. // {
  145. // public:
  146. // ui8 mode;
  147. // CCustomizableArt(){mode=0;};
  148. // void Init(){};
  149. // void SetProperty (int mod){};
  150. // template <typename Handler> void serialize(Handler &h, const int version)
  151. // {
  152. // h & static_cast<IModableArt&>(*this);
  153. // h & mode;
  154. // }
  155. // };
  156. //
  157. // class DLL_EXPORT CCommanderArt : public IModableArt // Growing with time
  158. // {
  159. // public:
  160. // ui32 level;
  161. // CCommanderArt(){level = 0;};
  162. // void Init(){};
  163. // void SetProperty (int mod){level = mod;};
  164. // void Upgrade(){level++;};
  165. // template <typename Handler> void serialize(Handler &h, const int version)
  166. // {
  167. // h & static_cast<IModableArt&>(*this);
  168. // h & level;
  169. // }
  170. // };
  171. class DLL_EXPORT CArtHandler //handles artifacts
  172. {
  173. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, ILimiter * limiter = NULL);
  174. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype, IPropagator* propagator);
  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. struct DLL_EXPORT ArtSlotInfo
  205. {
  206. ConstTransitivePtr<CArtifactInstance> artifact;
  207. ui8 locked; //if locked, then artifact points to the combined artifact
  208. ArtSlotInfo()
  209. {
  210. locked = false;
  211. }
  212. template <typename Handler> void serialize(Handler &h, const int version)
  213. {
  214. h & artifact & locked;
  215. }
  216. };
  217. class DLL_EXPORT IArtifactSetBase
  218. { ///artifacts container
  219. public:
  220. virtual void setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked);
  221. virtual const CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true) const;
  222. virtual CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true); //NULL - no artifact
  223. virtual bool hasArt(ui32 aid, bool onlyWorn = false) const;
  224. virtual bool isPositionFree(ui16 pos, bool onlyLockCheck = false) const;
  225. virtual ArtSlotInfo &retreiveNewArtSlot(ui16 slot)=0;
  226. virtual void eraseArtSlot(ui16 slot)=0;
  227. virtual const ArtSlotInfo *getSlot(ui16 pos) const=0;
  228. virtual si32 getArtPos(int aid, bool onlyWorn = true) const=0; //looks for equipped artifact with given ID and returns its slot ID or -1 if none(if more than one such artifact lower ID is returned)
  229. virtual si32 getArtPos(const CArtifactInstance *art) const=0;
  230. virtual const CArtifactInstance *getArtByInstanceId(int artInstId) const=0;
  231. virtual si32 getArtTypeId(ui16 pos) const=0;
  232. };
  233. class DLL_EXPORT CArtifactSet : public IArtifactSetBase
  234. { ///hero artifacts
  235. public:
  236. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  237. bmap<ui16, ArtSlotInfo> artifactsWorn; //map<position,artifact_id>; positions: 0 - head; 1 - shoulders; 2 - neck; 3 - right hand; 4 - left hand; 5 - torso; 6 - right ring; 7 - left ring; 8 - feet; 9 - misc1; 10 - misc2; 11 - misc3; 12 - misc4; 13 - mach1; 14 - mach2; 15 - mach3; 16 - mach4; 17 - spellbook; 18 - misc5
  238. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  239. void eraseArtSlot(ui16 slot);
  240. const ArtSlotInfo *getSlot(ui16 pos) const;
  241. si32 getArtPos(int aid, bool onlyWorn = true) const;
  242. si32 getArtPos(const CArtifactInstance *art) const;
  243. const CArtifactInstance *getArtByInstanceId(int artInstId) const;
  244. si32 getArtTypeId(ui16 pos) const;
  245. virtual ~CArtifactSet();
  246. template <typename Handler> void serialize(Handler &h, const int version)
  247. {
  248. h & artifactsInBackpack & artifactsWorn;
  249. }
  250. };
  251. class DLL_EXPORT CCreatureArtifactSet : public IArtifactSetBase
  252. { ///creature artifacts
  253. public:
  254. std::vector<ArtSlotInfo> artifactsInBackpack; //artifacts carried by creature - 4 max
  255. ArtSlotInfo activeArtifact; //position 0 - Arts::CREATURE_ART
  256. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  257. void eraseArtSlot(ui16 slot);
  258. const ArtSlotInfo *getSlot(ui16 pos)const;
  259. si32 getArtPos(int aid, bool onlyWorn = true) const;
  260. si32 getArtPos(const CArtifactInstance *art) const;
  261. const CArtifactInstance *getArtByInstanceId(int artInstId) const;
  262. si32 getArtTypeId(ui16 pos) const;
  263. virtual ~CCreatureArtifactSet(){};
  264. template <typename Handler> void serialize(Handler &h, const int version)
  265. {
  266. h & artifactsInBackpack & activeArtifact;
  267. }
  268. };
  269. #endif // __CARTHANDLER_H__