CArtHandler.h 12 KB

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