CArtHandler.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. class CArtifactInstance;
  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. //cres
  28. CREATURE_SLOT = 0,
  29. COMMANDER1 = 0, COMMANDER2, COMMANDER3, COMMANDER4, COMMANDER5, COMMANDER6, COMMANDER_AFTER_LAST
  30. };
  31. }
  32. namespace ArtifactID
  33. {
  34. enum ArtifactID
  35. {
  36. SPELLBOOK = 0,
  37. SPELL_SCROLL = 1,
  38. GRAIL = 2,
  39. CATAPULT = 3,
  40. BALLISTA = 4,
  41. AMMO_CART = 5,
  42. FIRST_AID_TENT = 6,
  43. CENTAUR_AXE = 7,
  44. BLACKSHARD_OF_THE_DEAD_KNIGHT = 8,
  45. CORNUCOPIA = 140
  46. };
  47. }
  48. namespace ArtBearer
  49. {
  50. enum
  51. {
  52. HERO, CREATURE, COMMANDER
  53. };
  54. }
  55. class DLL_LINKAGE CArtifact : public CBonusSystemNode //container for artifacts
  56. {
  57. protected:
  58. std::string name, description; //set if custom
  59. public:
  60. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  61. const std::string &Name() const; //getter
  62. const std::string &Description() const; //getter
  63. bool isBig () const;
  64. void setDescription (std::string desc);
  65. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  66. std::string nodeName() const OVERRIDE;
  67. virtual void levelUpArtifact (CArtifactInstance * art){};
  68. ui32 price;
  69. bmap<ui8, std::vector<ui16> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  70. std::vector<ui32> * constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  71. std::vector<ui32> * constituentOf; // Reverse map of constituents.
  72. EartClass aClass;
  73. si32 id;
  74. template <typename Handler> void serialize(Handler &h, const int version)
  75. {
  76. h & static_cast<CBonusSystemNode&>(*this);
  77. h & name & description & price & possibleSlots & constituents & constituentOf & aClass & id;
  78. }
  79. CArtifact();
  80. ~CArtifact();
  81. //override
  82. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  83. };
  84. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  85. {
  86. public:
  87. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  88. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  89. void levelUpArtifact (CArtifactInstance * art);
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & static_cast<CArtifact&>(*this);
  93. h & bonusesPerLevel & thresholdBonuses;
  94. }
  95. };
  96. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  97. {
  98. protected:
  99. void init();
  100. CArtifactInstance(CArtifact *Art);
  101. public:
  102. CArtifactInstance();
  103. ConstTransitivePtr<CArtifact> artType;
  104. TArtifactInstanceID id;
  105. //CArtifactInstance(int aid);
  106. std::string nodeName() const OVERRIDE;
  107. void deserializationFix();
  108. void setType(CArtifact *Art);
  109. int firstAvailableSlot(const CArtifactSet *h) const;
  110. int firstBackpackSlot(const CArtifactSet *h) const;
  111. int getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  112. virtual bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const;
  113. bool canBePutAt(const ArtifactLocation al, bool assumeDestRemoved = false) const; //forwards to the above one
  114. virtual bool canBeDisassembled() const;
  115. virtual void putAt(ArtifactLocation al);
  116. virtual void removeFrom(ArtifactLocation al);
  117. 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
  118. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  119. void move(ArtifactLocation src, ArtifactLocation dst);
  120. template <typename Handler> void serialize(Handler &h, const int version)
  121. {
  122. h & static_cast<CBonusSystemNode&>(*this);
  123. h & artType & id;
  124. BONUS_TREE_DESERIALIZATION_FIX
  125. }
  126. static CArtifactInstance *createScroll(const CSpell *s);
  127. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  128. static CArtifactInstance *createNewArtifactInstance(int aid);
  129. };
  130. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  131. {
  132. CCombinedArtifactInstance(CArtifact *Art);
  133. public:
  134. struct ConstituentInfo
  135. {
  136. ConstTransitivePtr<CArtifactInstance> art;
  137. si16 slot;
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & art & slot;
  141. }
  142. bool operator==(const ConstituentInfo &rhs) const;
  143. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  144. };
  145. std::vector<ConstituentInfo> constituentsInfo;
  146. bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const OVERRIDE;
  147. bool canBeDisassembled() const OVERRIDE;
  148. void putAt(ArtifactLocation al) OVERRIDE;
  149. void removeFrom(ArtifactLocation al) OVERRIDE;
  150. bool isPart(const CArtifactInstance *supposedPart) const OVERRIDE;
  151. void createConstituents();
  152. void addAsConstituent(CArtifactInstance *art, int slot);
  153. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replcaed with us (combined art), not lock
  154. CCombinedArtifactInstance();
  155. void deserializationFix();
  156. friend class CArtifactInstance;
  157. friend struct AssembledArtifact;
  158. template <typename Handler> void serialize(Handler &h, const int version)
  159. {
  160. h & static_cast<CArtifactInstance&>(*this);
  161. h & constituentsInfo;
  162. BONUS_TREE_DESERIALIZATION_FIX
  163. }
  164. };
  165. class DLL_LINKAGE CArtHandler //handles artifacts
  166. {
  167. void giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalinfo = 0);
  168. void giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator, int additionalinfo = 0);
  169. void giveArtBonus(TArtifactID aid, Bonus *bonus);
  170. public:
  171. std::vector<CArtifact*> treasures, minors, majors, relics;
  172. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  173. std::vector<CArtifact *> allowedArtifacts;
  174. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  175. std::set<ui32> growingArtifacts;
  176. void loadArtifacts(bool onlyTxt);
  177. void sortArts();
  178. void addBonuses();
  179. void clear();
  180. void clearHlpLists();
  181. ui16 getRandomArt (int flags);
  182. ui16 getArtSync (ui32 rand, int flags);
  183. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  184. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  185. void erasePickedArt (TArtifactInstanceID id);
  186. bool isBigArtifact (TArtifactID artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  187. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  188. static int convertMachineID(int id, bool creToArt);
  189. void makeItCreatureArt (TArtifactInstanceID aid, bool onlyCreature = true);
  190. void makeItCommanderArt (TArtifactInstanceID aid, bool onlyCommander = true);
  191. CArtHandler();
  192. ~CArtHandler();
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & artifacts & allowedArtifacts & treasures & minors & majors & relics
  196. & growingArtifacts;
  197. //if(!h.saving) sortArts();
  198. }
  199. };
  200. struct DLL_LINKAGE ArtSlotInfo
  201. {
  202. ConstTransitivePtr<CArtifactInstance> artifact;
  203. ui8 locked; //if locked, then artifact points to the combined artifact
  204. ArtSlotInfo()
  205. {
  206. locked = false;
  207. }
  208. template <typename Handler> void serialize(Handler &h, const int version)
  209. {
  210. h & artifact & locked;
  211. }
  212. };
  213. class DLL_LINKAGE CArtifactSet
  214. {
  215. public:
  216. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  217. 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
  218. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  219. void setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked);
  220. void eraseArtSlot(ui16 slot);
  221. const ArtSlotInfo *getSlot(ui16 pos) const;
  222. const CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true) const; //NULL - no artifact
  223. CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true); //NULL - no artifact
  224. 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)
  225. si32 getArtPos(const CArtifactInstance *art) const;
  226. const CArtifactInstance *getArtByInstanceId(TArtifactInstanceID artInstId) const;
  227. bool hasArt(ui32 aid, bool onlyWorn = false) const; //checks if hero possess artifact of given id (either in backack or worn)
  228. bool isPositionFree(ui16 pos, bool onlyLockCheck = false) const;
  229. si32 getArtTypeId(ui16 pos) const;
  230. virtual ui8 bearerType() const = 0;
  231. virtual ~CArtifactSet();
  232. template <typename Handler> void serialize(Handler &h, const int version)
  233. {
  234. h & artifactsInBackpack & artifactsWorn;
  235. }
  236. void artDeserializationFix(CBonusSystemNode *node);
  237. };