CArtHandler.h 11 KB

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