CArtHandler.h 10 KB

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