CArtHandler.h 11 KB

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