CArtHandler.h 10 KB

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