CArtHandler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * CArtHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/HeroBonus.h"
  12. //#include "../lib/ConstTransitivePtr.h"
  13. //#include "JsonNode.h"
  14. #include "GameConstants.h"
  15. #include "IHandlerBase.h"
  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;
  70. h & description;
  71. h & eventText;
  72. h & image;
  73. h & large;
  74. h & advMapDef;
  75. h & iconIndex;
  76. h & price;
  77. h & possibleSlots;
  78. h & constituents;
  79. h & constituentOf;
  80. h & aClass;
  81. h & id;
  82. if(version >= 759)
  83. {
  84. h & identifier;
  85. }
  86. if(version >= 771)
  87. {
  88. h & warMachine;
  89. }
  90. else if(!h.saving)
  91. {
  92. fillWarMachine();
  93. }
  94. }
  95. CArtifact();
  96. ~CArtifact();
  97. friend class CArtHandler;
  98. private:
  99. void fillWarMachine();
  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) override;
  107. template <typename Handler> void serialize(Handler &h, const int version)
  108. {
  109. h & static_cast<CArtifact&>(*this);
  110. h & bonusesPerLevel;
  111. h & thresholdBonuses;
  112. }
  113. };
  114. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  115. {
  116. protected:
  117. void init();
  118. CArtifactInstance(CArtifact *Art);
  119. public:
  120. CArtifactInstance();
  121. ConstTransitivePtr<CArtifact> artType;
  122. ArtifactInstanceID id;
  123. //CArtifactInstance(int aid);
  124. std::string nodeName() const override;
  125. void deserializationFix();
  126. void setType(CArtifact *Art);
  127. std::string getEffectiveDescription(const CGHeroInstance *hero = nullptr) const;
  128. ArtifactPosition firstAvailableSlot(const CArtifactSet *h) const;
  129. ArtifactPosition firstBackpackSlot(const CArtifactSet *h) const;
  130. SpellID getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  131. virtual bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const;
  132. bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; //forwards to the above one
  133. virtual bool canBeDisassembled() const;
  134. virtual void putAt(ArtifactLocation al);
  135. virtual void removeFrom(ArtifactLocation al);
  136. /// Checks if this a part of this artifact: artifact instance is a part
  137. /// of itself, additionally truth is returned for constituents of combined arts
  138. virtual bool isPart(const CArtifactInstance *supposedPart) const;
  139. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  140. void move(ArtifactLocation src, ArtifactLocation dst);
  141. template <typename Handler> void serialize(Handler &h, const int version)
  142. {
  143. h & static_cast<CBonusSystemNode&>(*this);
  144. h & artType;
  145. h & id;
  146. BONUS_TREE_DESERIALIZATION_FIX
  147. }
  148. static CArtifactInstance *createScroll(const CSpell *s);
  149. static CArtifactInstance *createScroll(SpellID sid);
  150. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  151. static CArtifactInstance *createNewArtifactInstance(int aid);
  152. /**
  153. * Creates an artifact instance.
  154. *
  155. * @param aid the id of the artifact
  156. * @param spellID optional. the id of a spell if a spell scroll object should be created
  157. * @return the created artifact instance
  158. */
  159. static CArtifactInstance * createArtifact(CMap * map, int aid, int spellID = -1);
  160. };
  161. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  162. {
  163. CCombinedArtifactInstance(CArtifact *Art);
  164. public:
  165. struct ConstituentInfo
  166. {
  167. ConstTransitivePtr<CArtifactInstance> art;
  168. ArtifactPosition slot;
  169. template <typename Handler> void serialize(Handler &h, const int version)
  170. {
  171. h & art;
  172. h & slot;
  173. }
  174. bool operator==(const ConstituentInfo &rhs) const;
  175. ConstituentInfo(CArtifactInstance *art = nullptr, ArtifactPosition slot = ArtifactPosition::PRE_FIRST);
  176. };
  177. std::vector<ConstituentInfo> constituentsInfo;
  178. bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const override;
  179. bool canBeDisassembled() const override;
  180. void putAt(ArtifactLocation al) override;
  181. void removeFrom(ArtifactLocation al) override;
  182. bool isPart(const CArtifactInstance *supposedPart) const override;
  183. void createConstituents();
  184. void addAsConstituent(CArtifactInstance *art, ArtifactPosition slot);
  185. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replaced with us (combined art), not lock
  186. CCombinedArtifactInstance();
  187. void deserializationFix();
  188. friend class CArtifactInstance;
  189. friend struct AssembledArtifact;
  190. template <typename Handler> void serialize(Handler &h, const int version)
  191. {
  192. h & static_cast<CArtifactInstance&>(*this);
  193. h & constituentsInfo;
  194. BONUS_TREE_DESERIALIZATION_FIX
  195. }
  196. };
  197. class DLL_LINKAGE CArtHandler : public IHandlerBase //handles artifacts
  198. {
  199. public:
  200. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  201. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  202. std::vector<CArtifact *> allowedArtifacts;
  203. std::set<ArtifactID> growingArtifacts;
  204. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  205. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of gibven class. No side effects
  206. boost::optional<std::vector<CArtifact*>&> listFromClass(CArtifact::EartClass artifactClass);
  207. ArtifactPosition stringToSlot(std::string slotName);
  208. CArtifact::EartClass stringToClass(std::string className);
  209. /// Gets a artifact ID randomly and removes the selected artifact from this handler.
  210. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags);
  211. ArtifactID pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts);
  212. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts);
  213. bool legalArtifact(ArtifactID id);
  214. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  215. void makeItCreatureArt (CArtifact * a, bool onlyCreature = true);
  216. void makeItCreatureArt (ArtifactID aid, bool onlyCreature = true);
  217. void makeItCommanderArt (CArtifact * a, bool onlyCommander = true);
  218. void makeItCommanderArt (ArtifactID aid, bool onlyCommander = true);
  219. CArtHandler();
  220. ~CArtHandler();
  221. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  222. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  223. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  224. void afterLoadFinalization() override;
  225. std::vector<bool> getDefaultAllowed() const override;
  226. ///json serialization helper
  227. static si32 decodeArfifact(const std::string & identifier);
  228. ///json serialization helper
  229. static std::string encodeArtifact(const si32 index);
  230. template <typename Handler> void serialize(Handler &h, const int version)
  231. {
  232. h & artifacts;
  233. h & allowedArtifacts;
  234. h & treasures;
  235. h & minors;
  236. h & majors;
  237. h & relics;
  238. h & growingArtifacts;
  239. }
  240. private:
  241. CArtifact * loadFromJson(const JsonNode & node, const std::string & identifier);
  242. void addSlot(CArtifact * art, const std::string & slotID);
  243. void loadSlots(CArtifact * art, const JsonNode & node);
  244. void loadClass(CArtifact * art, const JsonNode & node);
  245. void loadType(CArtifact * art, const JsonNode & node);
  246. void loadComponents(CArtifact * art, const JsonNode & node);
  247. void loadGrowingArt(CGrowingArtifact * art, const JsonNode & node);
  248. 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);
  249. void giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, std::shared_ptr<IPropagator> propagator, int additionalinfo = 0);
  250. void giveArtBonus(ArtifactID aid, std::shared_ptr<Bonus> bonus);
  251. void erasePickedArt(ArtifactID id);
  252. };
  253. struct DLL_LINKAGE ArtSlotInfo
  254. {
  255. ConstTransitivePtr<CArtifactInstance> artifact;
  256. ui8 locked; //if locked, then artifact points to the combined artifact
  257. ArtSlotInfo() : locked(false) {}
  258. template <typename Handler> void serialize(Handler &h, const int version)
  259. {
  260. h & artifact;
  261. h & locked;
  262. }
  263. };
  264. class DLL_LINKAGE CArtifactSet
  265. {
  266. public:
  267. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  268. 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
  269. ArtSlotInfo &retreiveNewArtSlot(ArtifactPosition slot);
  270. void setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked);
  271. void eraseArtSlot(ArtifactPosition slot);
  272. const ArtSlotInfo *getSlot(ArtifactPosition pos) const;
  273. const CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true) const; //nullptr - no artifact
  274. CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //nullptr - no artifact
  275. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  276. /// (if more than one such artifact lower ID is returned)
  277. ArtifactPosition getArtPos(int aid, bool onlyWorn = true) const;
  278. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  279. const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const;
  280. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  281. const CArtifactInstance *getHiddenArt(int aid) const;
  282. const CCombinedArtifactInstance *getAssemblyByConstituent(int aid) const;
  283. /// Checks if hero possess artifact of given id (either in backack or worn)
  284. bool hasArt(ui32 aid, bool onlyWorn = false, bool searchBackpackAssemblies = false) const;
  285. bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const;
  286. virtual ArtBearer::ArtBearer bearerType() const = 0;
  287. virtual void putArtifact(ArtifactPosition pos, CArtifactInstance * art) = 0;
  288. virtual ~CArtifactSet();
  289. template <typename Handler> void serialize(Handler &h, const int version)
  290. {
  291. h & artifactsInBackpack;
  292. h & artifactsWorn;
  293. }
  294. void artDeserializationFix(CBonusSystemNode *node);
  295. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map);
  296. protected:
  297. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(int aid) const;
  298. private:
  299. void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
  300. void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
  301. void serializeJsonCommander(JsonSerializeFormat & handler, CMap * map);
  302. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map);//normal slots
  303. };