CArtHandler.h 12 KB

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