CArtHandler.h 13 KB

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