CArtHandler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. std::string advMapDef; //used for adventure map object
  72. si32 iconIndex; //TODO: handle automatically
  73. const std::string &Name() const; //getter
  74. const std::string &Description() const; //getter
  75. const std::string &EventText() const;
  76. bool isBig () const;
  77. void setName (std::string desc);
  78. void setDescription (std::string desc);
  79. void setEventText (std::string desc);
  80. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  81. std::string nodeName() const OVERRIDE;
  82. void addNewBonus(Bonus *b) OVERRIDE;
  83. virtual void levelUpArtifact (CArtifactInstance * art){};
  84. ui32 price;
  85. bmap<ui8, std::vector<ui16> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  86. std::vector<ui32> * constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  87. std::vector<ui32> * constituentOf; // Reverse map of constituents.
  88. EartClass aClass;
  89. si32 id;
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. h & static_cast<CBonusSystemNode&>(*this);
  93. h & name & description & eventText & image & large & advMapDef & iconIndex &
  94. price & possibleSlots & constituents & constituentOf & aClass & id;
  95. }
  96. CArtifact();
  97. ~CArtifact();
  98. //override
  99. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  100. };
  101. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  102. {
  103. public:
  104. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  105. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  106. void levelUpArtifact (CArtifactInstance * art);
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. h & static_cast<CArtifact&>(*this);
  110. h & bonusesPerLevel & thresholdBonuses;
  111. }
  112. };
  113. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  114. {
  115. protected:
  116. void init();
  117. CArtifactInstance(CArtifact *Art);
  118. public:
  119. CArtifactInstance();
  120. ConstTransitivePtr<CArtifact> artType;
  121. TArtifactInstanceID id;
  122. //CArtifactInstance(int aid);
  123. std::string nodeName() const OVERRIDE;
  124. void deserializationFix();
  125. void setType(CArtifact *Art);
  126. int firstAvailableSlot(const CArtifactSet *h) const;
  127. int firstBackpackSlot(const CArtifactSet *h) const;
  128. int getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  129. virtual bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const;
  130. bool canBePutAt(const ArtifactLocation al, bool assumeDestRemoved = false) const; //forwards to the above one
  131. virtual bool canBeDisassembled() const;
  132. virtual void putAt(ArtifactLocation al);
  133. virtual void removeFrom(ArtifactLocation al);
  134. 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
  135. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  136. void move(ArtifactLocation src, ArtifactLocation dst);
  137. template <typename Handler> void serialize(Handler &h, const int version)
  138. {
  139. h & static_cast<CBonusSystemNode&>(*this);
  140. h & artType & id;
  141. BONUS_TREE_DESERIALIZATION_FIX
  142. }
  143. static CArtifactInstance *createScroll(const CSpell *s);
  144. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  145. static CArtifactInstance *createNewArtifactInstance(int aid);
  146. };
  147. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  148. {
  149. CCombinedArtifactInstance(CArtifact *Art);
  150. public:
  151. struct ConstituentInfo
  152. {
  153. ConstTransitivePtr<CArtifactInstance> art;
  154. si16 slot;
  155. template <typename Handler> void serialize(Handler &h, const int version)
  156. {
  157. h & art & slot;
  158. }
  159. bool operator==(const ConstituentInfo &rhs) const;
  160. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  161. };
  162. std::vector<ConstituentInfo> constituentsInfo;
  163. bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const OVERRIDE;
  164. bool canBeDisassembled() const OVERRIDE;
  165. void putAt(ArtifactLocation al) OVERRIDE;
  166. void removeFrom(ArtifactLocation al) OVERRIDE;
  167. bool isPart(const CArtifactInstance *supposedPart) const OVERRIDE;
  168. void createConstituents();
  169. void addAsConstituent(CArtifactInstance *art, int slot);
  170. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replcaed with us (combined art), not lock
  171. CCombinedArtifactInstance();
  172. void deserializationFix();
  173. friend class CArtifactInstance;
  174. friend struct AssembledArtifact;
  175. template <typename Handler> void serialize(Handler &h, const int version)
  176. {
  177. h & static_cast<CArtifactInstance&>(*this);
  178. h & constituentsInfo;
  179. BONUS_TREE_DESERIALIZATION_FIX
  180. }
  181. };
  182. class DLL_LINKAGE CArtHandler //handles artifacts
  183. {
  184. 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);
  185. void giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator, int additionalinfo = 0);
  186. void giveArtBonus(TArtifactID aid, Bonus *bonus);
  187. public:
  188. std::vector<CArtifact*> treasures, minors, majors, relics;
  189. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  190. std::vector<CArtifact *> allowedArtifacts;
  191. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  192. std::set<ui32> growingArtifacts;
  193. void loadArtifacts(bool onlyTxt);
  194. /// load all artifacts from json structure
  195. void load(const JsonNode & node);
  196. /// load one artifact from json config
  197. CArtifact * loadArtifact(const JsonNode & node);
  198. void sortArts();
  199. void addBonuses();
  200. void clear();
  201. void clearHlpLists();
  202. ui16 getRandomArt (int flags);
  203. ui16 getArtSync (ui32 rand, int flags);
  204. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  205. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  206. void erasePickedArt (TArtifactInstanceID id);
  207. bool isBigArtifact (TArtifactID artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  208. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  209. static int convertMachineID(int id, bool creToArt);
  210. void makeItCreatureArt (TArtifactInstanceID aid, bool onlyCreature = true);
  211. void makeItCommanderArt (TArtifactInstanceID aid, bool onlyCommander = true);
  212. CArtHandler();
  213. ~CArtHandler();
  214. template <typename Handler> void serialize(Handler &h, const int version)
  215. {
  216. h & artifacts & allowedArtifacts & treasures & minors & majors & relics
  217. & growingArtifacts;
  218. //if(!h.saving) sortArts();
  219. }
  220. };
  221. struct DLL_LINKAGE ArtSlotInfo
  222. {
  223. ConstTransitivePtr<CArtifactInstance> artifact;
  224. ui8 locked; //if locked, then artifact points to the combined artifact
  225. ArtSlotInfo()
  226. {
  227. locked = false;
  228. }
  229. template <typename Handler> void serialize(Handler &h, const int version)
  230. {
  231. h & artifact & locked;
  232. }
  233. };
  234. class DLL_LINKAGE CArtifactSet
  235. {
  236. public:
  237. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  238. 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
  239. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  240. void setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked);
  241. void eraseArtSlot(ui16 slot);
  242. const ArtSlotInfo *getSlot(ui16 pos) const;
  243. const CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true) const; //NULL - no artifact
  244. CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true); //NULL - no artifact
  245. 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)
  246. si32 getArtPos(const CArtifactInstance *art) const;
  247. const CArtifactInstance *getArtByInstanceId(TArtifactInstanceID artInstId) const;
  248. bool hasArt(ui32 aid, bool onlyWorn = false) const; //checks if hero possess artifact of given id (either in backack or worn)
  249. bool isPositionFree(ui16 pos, bool onlyLockCheck = false) const;
  250. si32 getArtTypeId(ui16 pos) const;
  251. virtual ui8 bearerType() const = 0;
  252. virtual ~CArtifactSet();
  253. template <typename Handler> void serialize(Handler &h, const int version)
  254. {
  255. h & artifactsInBackpack & artifactsWorn;
  256. }
  257. void artDeserializationFix(CBonusSystemNode *node);
  258. };