CArtHandler.h 10 KB

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