2
0

CArtHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. public:
  24. std::string name, description; //set if custom
  25. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  26. const std::string &Name() const; //getter
  27. const std::string &Description() const; //getter
  28. bool isBig () const;
  29. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  30. std::string nodeName() const OVERRIDE;
  31. ui32 price;
  32. std::vector<ui16> possibleSlots; //ids of slots where artifact can be placed
  33. std::vector<ui32> * constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  34. std::vector<ui32> * constituentOf; // Reverse map of constituents.
  35. EartClass aClass;
  36. si32 id;
  37. template <typename Handler> void serialize(Handler &h, const int version)
  38. {
  39. h & static_cast<CBonusSystemNode&>(*this);
  40. h & name & description & price & possibleSlots & constituents & constituentOf & aClass & id;
  41. }
  42. CArtifact();
  43. ~CArtifact();
  44. //override
  45. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  46. };
  47. class DLL_EXPORT CArtifactInstance : public CBonusSystemNode
  48. {
  49. public:
  50. void init();
  51. CArtifactInstance(CArtifact *Art);
  52. CArtifactInstance();
  53. ConstTransitivePtr<CArtifact> artType;
  54. si32 id; //id of the instance
  55. //CArtifactInstance(int aid);
  56. std::string nodeName() const OVERRIDE;
  57. void deserializationFix();
  58. void setType(CArtifact *Art);
  59. int firstAvailableSlot(const CGHeroInstance *h) const;
  60. int firstBackpackSlot(const CGHeroInstance *h) const;
  61. int getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  62. virtual bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const;
  63. virtual bool canBeDisassembled() const;
  64. virtual void putAt(CGHeroInstance *h, ui16 slot);
  65. virtual void removeFrom(CGHeroInstance *h, ui16 slot);
  66. 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
  67. std::vector<const CArtifact *> assemblyPossibilities(const CGHeroInstance *h) const;
  68. void move(ArtifactLocation &src, ArtifactLocation &dst);
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & static_cast<CBonusSystemNode&>(*this);
  72. h & artType & id;
  73. BONUS_TREE_DESERIALIZATION_FIX
  74. }
  75. static CArtifactInstance *createScroll(const CSpell *s);
  76. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  77. static CArtifactInstance *createNewArtifactInstance(int aid);
  78. };
  79. class DLL_EXPORT CCombinedArtifactInstance : public CArtifactInstance
  80. {
  81. CCombinedArtifactInstance(CArtifact *Art);
  82. public:
  83. struct ConstituentInfo
  84. {
  85. ConstTransitivePtr<CArtifactInstance> art;
  86. si16 slot;
  87. template <typename Handler> void serialize(Handler &h, const int version)
  88. {
  89. h & art & slot;
  90. }
  91. bool operator==(const ConstituentInfo &rhs) const;
  92. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  93. };
  94. std::vector<ConstituentInfo> constituentsInfo;
  95. bool canBePutAt(const ArtifactLocation &al, bool assumeDestRemoved = false) const OVERRIDE;
  96. bool canBeDisassembled() const OVERRIDE;
  97. void putAt(CGHeroInstance *h, ui16 slot) OVERRIDE;
  98. void removeFrom(CGHeroInstance *h, ui16 slot) OVERRIDE;
  99. bool isPart(const CArtifactInstance *supposedPart) const OVERRIDE;
  100. void createConstituents();
  101. void addAsConstituent(CArtifactInstance *art, int slot);
  102. CArtifactInstance *figureMainConstituent(ui16 slot); //main constituent is replcaed with us (combined art), not lock
  103. CCombinedArtifactInstance();
  104. void deserializationFix();
  105. friend class CArtifactInstance;
  106. friend struct AssembledArtifact;
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. h & static_cast<CArtifactInstance&>(*this);
  110. h & constituentsInfo;
  111. BONUS_TREE_DESERIALIZATION_FIX
  112. }
  113. };
  114. // class DLL_EXPORT IModableArt : public CArtifact //artifact which can have different properties, such as scroll or banner
  115. // { //used only for dynamic cast :P
  116. // public:
  117. // si32 ID; //used for smart serialization
  118. //
  119. // template <typename Handler> void serialize(Handler &h, const int version)
  120. // {
  121. // h & static_cast<CArtifact&>(*this);
  122. // h & ID;
  123. // }
  124. // };
  125. //
  126. // class DLL_EXPORT CScroll : public IModableArt // Spell Scroll
  127. // {
  128. // public:
  129. // CScroll(){spellid=0;};
  130. // CScroll(spelltype sid){spellid = sid;};
  131. // spelltype spellid;
  132. // void Init();
  133. // void SetProperty (int mod){spellid = mod;};
  134. // template <typename Handler> void serialize(Handler &h, const int version)
  135. // {
  136. // h & static_cast<IModableArt&>(*this);
  137. // h & spellid;
  138. // }
  139. // };
  140. //
  141. // class DLL_EXPORT CCustomizableArt : public IModableArt // Warlord's Banner with multiple options
  142. // {
  143. // public:
  144. // ui8 mode;
  145. // CCustomizableArt(){mode=0;};
  146. // void Init(){};
  147. // void SetProperty (int mod){};
  148. // template <typename Handler> void serialize(Handler &h, const int version)
  149. // {
  150. // h & static_cast<IModableArt&>(*this);
  151. // h & mode;
  152. // }
  153. // };
  154. //
  155. // class DLL_EXPORT CCommanderArt : public IModableArt // Growing with time
  156. // {
  157. // public:
  158. // ui32 level;
  159. // CCommanderArt(){level = 0;};
  160. // void Init(){};
  161. // void SetProperty (int mod){level = mod;};
  162. // void Upgrade(){level++;};
  163. // template <typename Handler> void serialize(Handler &h, const int version)
  164. // {
  165. // h & static_cast<IModableArt&>(*this);
  166. // h & level;
  167. // }
  168. // };
  169. class DLL_EXPORT CArtHandler //handles artifacts
  170. {
  171. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, ILimiter * limiter = NULL);
  172. void giveArtBonus(int aid, Bonus::BonusType type, int val, int subtype, IPropagator* propagator);
  173. public:
  174. std::vector<CArtifact*> treasures, minors, majors, relics;
  175. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  176. std::vector<CArtifact *> allowedArtifacts;
  177. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  178. //std::map<ui32, ui8> modableArtifacts; //1-scroll, 2-banner, 3-commander art with progressive bonus
  179. void loadArtifacts(bool onlyTxt);
  180. void sortArts();
  181. void addBonuses();
  182. void clear();
  183. void clearHlpLists();
  184. ui16 getRandomArt (int flags);
  185. ui16 getArtSync (ui32 rand, int flags);
  186. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  187. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  188. void erasePickedArt (si32 id);
  189. bool isBigArtifact (ui32 artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  190. // void equipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID, const CArtifact* art) const;
  191. // void unequipArtifact (std::map<ui16, const CArtifact*> &artifWorn, ui16 slotID) const;
  192. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  193. static int convertMachineID(int id, bool creToArt);
  194. CArtHandler();
  195. ~CArtHandler();
  196. template <typename Handler> void serialize(Handler &h, const int version)
  197. {
  198. h & artifacts & allowedArtifacts & treasures & minors & majors & relics;
  199. //if(!h.saving) sortArts();
  200. }
  201. };
  202. struct DLL_EXPORT ArtSlotInfo
  203. {
  204. ConstTransitivePtr<CArtifactInstance> artifact;
  205. ui8 locked; //if locked, then artifact points to the combined artifact
  206. ArtSlotInfo()
  207. {
  208. locked = false;
  209. }
  210. template <typename Handler> void serialize(Handler &h, const int version)
  211. {
  212. h & artifact & locked;
  213. }
  214. };
  215. class DLL_EXPORT IArtifactSetBase
  216. { ///artifacts container
  217. public:
  218. virtual void setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked);
  219. virtual const CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true) const;
  220. virtual CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true); //NULL - no artifact
  221. virtual bool hasArt(ui32 aid, bool onlyWorn = false) const;
  222. virtual bool isPositionFree(ui16 pos, bool onlyLockCheck = false) const;
  223. virtual ArtSlotInfo &retreiveNewArtSlot(ui16 slot)=0;
  224. virtual void eraseArtSlot(ui16 slot)=0;
  225. virtual const ArtSlotInfo *getSlot(ui16 pos) const=0;
  226. 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)
  227. virtual si32 getArtPos(const CArtifactInstance *art) const=0;
  228. virtual const CArtifactInstance *getArtByInstanceId(int artInstId) const=0;
  229. virtual si32 getArtTypeId(ui16 pos) const=0;
  230. };
  231. class DLL_EXPORT CArtifactSet : public IArtifactSetBase
  232. { ///hero artifacts
  233. public:
  234. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  235. 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
  236. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  237. void eraseArtSlot(ui16 slot);
  238. const ArtSlotInfo *getSlot(ui16 pos) const;
  239. si32 getArtPos(int aid, bool onlyWorn = true) const;
  240. si32 getArtPos(const CArtifactInstance *art) const;
  241. const CArtifactInstance *getArtByInstanceId(int artInstId) const;
  242. si32 getArtTypeId(ui16 pos) const;
  243. virtual ~CArtifactSet();
  244. template <typename Handler> void serialize(Handler &h, const int version)
  245. {
  246. h & artifactsInBackpack & artifactsWorn;
  247. }
  248. };
  249. class DLL_EXPORT CCreatureArtifactSet : public IArtifactSetBase
  250. { ///creature artifacts
  251. public:
  252. std::vector<ArtSlotInfo> artifactsInBackpack; //artifacts carried by creature - 4 max
  253. ArtSlotInfo activeArtifact; //position 0 - Arts::CREATURE_ART
  254. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  255. void eraseArtSlot(ui16 slot);
  256. const ArtSlotInfo *getSlot(ui16 pos)const;
  257. si32 getArtPos(int aid, bool onlyWorn = true) const;
  258. si32 getArtPos(const CArtifactInstance *art) const;
  259. const CArtifactInstance *getArtByInstanceId(int artInstId) const;
  260. si32 getArtTypeId(ui16 pos) const;
  261. virtual ~CCreatureArtifactSet(){};
  262. template <typename Handler> void serialize(Handler &h, const int version)
  263. {
  264. h & artifactsInBackpack & activeArtifact;
  265. }
  266. };
  267. #endif // __CARTHANDLER_H__