CArtHandler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 CArtifact;
  18. class CGHeroInstance;
  19. struct ArtifactLocation;
  20. class CArtifactSet;
  21. class CArtifactInstance;
  22. class CRandomGenerator;
  23. class CMap;
  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 identifier;
  45. std::string image;
  46. std::string large; // big image for cutom artifacts, used in drag & drop
  47. std::string advMapDef; //used for adventure map object
  48. si32 iconIndex;
  49. const std::string &Name() const; //getter
  50. const std::string &Description() const; //getter
  51. const std::string &EventText() const;
  52. bool isBig () const;
  53. bool isTradable () const;
  54. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  55. std::string nodeName() const override;
  56. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  57. virtual void levelUpArtifact (CArtifactInstance * art){};
  58. ui32 price;
  59. std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  60. std::unique_ptr<std::vector<CArtifact *> > constituents; // Artifacts IDs a combined artifact consists of, or nullptr.
  61. std::vector<CArtifact *> constituentOf; // Reverse map of constituents - combined arts that include this art
  62. EartClass aClass;
  63. ArtifactID id;
  64. template <typename Handler> void serialize(Handler &h, const int version)
  65. {
  66. h & static_cast<CBonusSystemNode&>(*this);
  67. h & name & description & eventText & image & large & advMapDef & iconIndex &
  68. price & possibleSlots & constituents & constituentOf & aClass & id;
  69. if(version>=759)
  70. {
  71. h & identifier;
  72. }
  73. }
  74. CArtifact();
  75. ~CArtifact();
  76. friend class CArtHandler;
  77. };
  78. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  79. {
  80. public:
  81. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  82. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  83. void levelUpArtifact(CArtifactInstance * art) override;
  84. template <typename Handler> void serialize(Handler &h, const int version)
  85. {
  86. h & static_cast<CArtifact&>(*this);
  87. h & bonusesPerLevel & thresholdBonuses;
  88. }
  89. };
  90. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  91. {
  92. protected:
  93. void init();
  94. CArtifactInstance(CArtifact *Art);
  95. public:
  96. CArtifactInstance();
  97. ConstTransitivePtr<CArtifact> artType;
  98. ArtifactInstanceID id;
  99. //CArtifactInstance(int aid);
  100. std::string nodeName() const override;
  101. void deserializationFix();
  102. void setType(CArtifact *Art);
  103. std::string getEffectiveDescription(const CGHeroInstance *hero = nullptr) const;
  104. ArtifactPosition firstAvailableSlot(const CArtifactSet *h) const;
  105. ArtifactPosition firstBackpackSlot(const CArtifactSet *h) const;
  106. SpellID getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  107. virtual bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const;
  108. bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; //forwards to the above one
  109. virtual bool canBeDisassembled() const;
  110. virtual void putAt(ArtifactLocation al);
  111. virtual void removeFrom(ArtifactLocation al);
  112. /// Checks if this a part of this artifact: artifact instance is a part
  113. /// of itself, additionally truth is returned for constituents of combined arts
  114. virtual bool isPart(const CArtifactInstance *supposedPart) const;
  115. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  116. void move(ArtifactLocation src, ArtifactLocation dst);
  117. template <typename Handler> void serialize(Handler &h, const int version)
  118. {
  119. h & static_cast<CBonusSystemNode&>(*this);
  120. h & artType & id;
  121. BONUS_TREE_DESERIALIZATION_FIX
  122. }
  123. static CArtifactInstance *createScroll(const CSpell *s);
  124. static CArtifactInstance *createScroll(SpellID sid);
  125. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  126. static CArtifactInstance *createNewArtifactInstance(int aid);
  127. /**
  128. * Creates an artifact instance.
  129. *
  130. * @param aid the id of the artifact
  131. * @param spellID optional. the id of a spell if a spell scroll object should be created
  132. * @return the created artifact instance
  133. */
  134. static CArtifactInstance * createArtifact(CMap * map, int aid, int spellID = -1);
  135. };
  136. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  137. {
  138. CCombinedArtifactInstance(CArtifact *Art);
  139. public:
  140. struct ConstituentInfo
  141. {
  142. ConstTransitivePtr<CArtifactInstance> art;
  143. ArtifactPosition slot;
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & art & slot;
  147. }
  148. bool operator==(const ConstituentInfo &rhs) const;
  149. ConstituentInfo(CArtifactInstance *art = nullptr, ArtifactPosition slot = ArtifactPosition::PRE_FIRST);
  150. };
  151. std::vector<ConstituentInfo> constituentsInfo;
  152. bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const override;
  153. bool canBeDisassembled() const override;
  154. void putAt(ArtifactLocation al) override;
  155. void removeFrom(ArtifactLocation al) override;
  156. bool isPart(const CArtifactInstance *supposedPart) const override;
  157. void createConstituents();
  158. void addAsConstituent(CArtifactInstance *art, ArtifactPosition slot);
  159. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replaced with us (combined art), not lock
  160. CCombinedArtifactInstance();
  161. void deserializationFix();
  162. friend class CArtifactInstance;
  163. friend struct AssembledArtifact;
  164. template <typename Handler> void serialize(Handler &h, const int version)
  165. {
  166. h & static_cast<CArtifactInstance&>(*this);
  167. h & constituentsInfo;
  168. BONUS_TREE_DESERIALIZATION_FIX
  169. }
  170. };
  171. class DLL_LINKAGE CArtHandler : public IHandlerBase //handles artifacts
  172. {
  173. public:
  174. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  175. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  176. std::vector<CArtifact *> allowedArtifacts;
  177. std::set<ArtifactID> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  178. std::set<ArtifactID> growingArtifacts;
  179. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  180. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of gibven class. No side effects
  181. boost::optional<std::vector<CArtifact*>&> listFromClass(CArtifact::EartClass artifactClass);
  182. ArtifactPosition stringToSlot(std::string slotName);
  183. CArtifact::EartClass stringToClass(std::string className);
  184. /// Gets a artifact ID randomly and removes the selected artifact from this handler.
  185. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags);
  186. ArtifactID pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts);
  187. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts);
  188. bool legalArtifact(ArtifactID id);
  189. //void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  190. //void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  191. bool isBigArtifact (ArtifactID artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  192. bool isTradableArtifact (ArtifactID id) const;
  193. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  194. static ArtifactID creatureToMachineID(CreatureID id);
  195. static CreatureID machineIDToCreature(ArtifactID id);
  196. void makeItCreatureArt (CArtifact * a, bool onlyCreature = true);
  197. void makeItCreatureArt (ArtifactID aid, bool onlyCreature = true);
  198. void makeItCommanderArt (CArtifact * a, bool onlyCommander = true);
  199. void makeItCommanderArt (ArtifactID aid, bool onlyCommander = true);
  200. CArtHandler();
  201. ~CArtHandler();
  202. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  203. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  204. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  205. void afterLoadFinalization() override;
  206. std::vector<bool> getDefaultAllowed() const override;
  207. ///json serialization helper
  208. static si32 decodeArfifact(const std::string & identifier);
  209. ///json serialization helper
  210. static std::string encodeArtifact(const si32 index);
  211. template <typename Handler> void serialize(Handler &h, const int version)
  212. {
  213. h & artifacts & allowedArtifacts & treasures & minors & majors & relics
  214. & growingArtifacts;
  215. //if(!h.saving) sortArts();
  216. }
  217. private:
  218. CArtifact * loadFromJson(const JsonNode & node, const std::string & identifier);
  219. void addSlot(CArtifact * art, const std::string & slotID);
  220. void loadSlots(CArtifact * art, const JsonNode & node);
  221. void loadClass(CArtifact * art, const JsonNode & node);
  222. void loadType(CArtifact * art, const JsonNode & node);
  223. void loadComponents(CArtifact * art, const JsonNode & node);
  224. void loadGrowingArt(CGrowingArtifact * art, const JsonNode & node);
  225. void giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype = -1, Bonus::ValueType valType = Bonus::BASE_NUMBER, std::shared_ptr<ILimiter> limiter = std::shared_ptr<ILimiter>(), int additionalinfo = 0);
  226. void giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, std::shared_ptr<IPropagator> propagator, int additionalinfo = 0);
  227. void giveArtBonus(ArtifactID aid, std::shared_ptr<Bonus> bonus);
  228. void erasePickedArt(ArtifactID id);
  229. };
  230. struct DLL_LINKAGE ArtSlotInfo
  231. {
  232. ConstTransitivePtr<CArtifactInstance> artifact;
  233. ui8 locked; //if locked, then artifact points to the combined artifact
  234. ArtSlotInfo() : locked(false) {}
  235. template <typename Handler> void serialize(Handler &h, const int version)
  236. {
  237. h & artifact & locked;
  238. }
  239. };
  240. class DLL_LINKAGE CArtifactSet
  241. {
  242. public:
  243. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  244. 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
  245. ArtSlotInfo &retreiveNewArtSlot(ArtifactPosition slot);
  246. void setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked);
  247. void eraseArtSlot(ArtifactPosition slot);
  248. const ArtSlotInfo *getSlot(ArtifactPosition pos) const;
  249. const CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true) const; //nullptr - no artifact
  250. CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //nullptr - no artifact
  251. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  252. /// (if more than one such artifact lower ID is returned)
  253. ArtifactPosition getArtPos(int aid, bool onlyWorn = true) const;
  254. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  255. const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const;
  256. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  257. const CArtifactInstance *getHiddenArt(int aid) const;
  258. const CCombinedArtifactInstance *getAssemblyByConstituent(int aid) const;
  259. /// Checks if hero possess artifact of given id (either in backack or worn)
  260. bool hasArt(ui32 aid, bool onlyWorn = false, bool searchBackpackAssemblies = false) const;
  261. bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const;
  262. si32 getArtTypeId(ArtifactPosition pos) const;
  263. virtual ArtBearer::ArtBearer bearerType() const = 0;
  264. virtual ~CArtifactSet() = default;
  265. template <typename Handler> void serialize(Handler &h, const int version)
  266. {
  267. h & artifactsInBackpack & artifactsWorn;
  268. }
  269. void artDeserializationFix(CBonusSystemNode *node);
  270. protected:
  271. void writeJson(JsonNode & json) const;
  272. void readJson(const JsonNode & json);
  273. protected:
  274. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(int aid) const;
  275. };