CArtHandler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 <vcmi/Artifact.h>
  12. #include <vcmi/ArtifactService.h>
  13. #include "HeroBonus.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 Artifact, 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 custom 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. int32_t getIndex() const override;
  58. int32_t getIconIndex() const override;
  59. const std::string & getName() const override;
  60. const std::string & getJsonKey() const override;
  61. void registerIcons(const IconRegistar & cb) const override;
  62. ArtifactID getId() const override;
  63. virtual const IBonusBearer * accessBonuses() const override;
  64. const std::string & getEventText() const override;
  65. const std::string & getDescription() const override;
  66. uint32_t getPrice() const override;
  67. CreatureID getWarMachine() const override;
  68. bool isBig() const override;
  69. bool isTradable() const override;
  70. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  71. std::string nodeName() const override;
  72. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  73. virtual void levelUpArtifact (CArtifactInstance * art){};
  74. void updateFrom(const JsonNode & data);
  75. void serializeJson(JsonSerializeFormat & handler);
  76. template <typename Handler> void serialize(Handler &h, const int version)
  77. {
  78. h & static_cast<CBonusSystemNode&>(*this);
  79. h & name;
  80. h & description;
  81. h & eventText;
  82. h & image;
  83. h & large;
  84. h & advMapDef;
  85. h & iconIndex;
  86. h & price;
  87. h & possibleSlots;
  88. h & constituents;
  89. h & constituentOf;
  90. h & aClass;
  91. h & id;
  92. h & identifier;
  93. h & warMachine;
  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(SpellID sid);
  149. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  150. static CArtifactInstance *createNewArtifactInstance(int aid);
  151. /**
  152. * Creates an artifact instance.
  153. *
  154. * @param aid the id of the artifact
  155. * @param spellID optional. the id of a spell if a spell scroll object should be created
  156. * @return the created artifact instance
  157. */
  158. static CArtifactInstance * createArtifact(CMap * map, int aid, int spellID = -1);
  159. };
  160. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  161. {
  162. CCombinedArtifactInstance(CArtifact *Art);
  163. public:
  164. struct ConstituentInfo
  165. {
  166. ConstTransitivePtr<CArtifactInstance> art;
  167. ArtifactPosition slot;
  168. template <typename Handler> void serialize(Handler &h, const int version)
  169. {
  170. h & art;
  171. h & slot;
  172. }
  173. bool operator==(const ConstituentInfo &rhs) const;
  174. ConstituentInfo(CArtifactInstance *art = nullptr, ArtifactPosition slot = ArtifactPosition::PRE_FIRST);
  175. };
  176. std::vector<ConstituentInfo> constituentsInfo;
  177. bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const override;
  178. bool canBeDisassembled() const override;
  179. void putAt(ArtifactLocation al) override;
  180. void removeFrom(ArtifactLocation al) override;
  181. bool isPart(const CArtifactInstance *supposedPart) const override;
  182. void createConstituents();
  183. void addAsConstituent(CArtifactInstance *art, ArtifactPosition slot);
  184. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replaced with us (combined art), not lock
  185. CCombinedArtifactInstance();
  186. void deserializationFix();
  187. friend class CArtifactInstance;
  188. friend struct AssembledArtifact;
  189. template <typename Handler> void serialize(Handler &h, const int version)
  190. {
  191. h & static_cast<CArtifactInstance&>(*this);
  192. h & constituentsInfo;
  193. BONUS_TREE_DESERIALIZATION_FIX
  194. }
  195. };
  196. class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  197. {
  198. public:
  199. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  200. std::vector<CArtifact *> allowedArtifacts;
  201. std::set<ArtifactID> growingArtifacts;
  202. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  203. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of given class. No side effects
  204. boost::optional<std::vector<CArtifact*>&> listFromClass(CArtifact::EartClass artifactClass);
  205. ArtifactPosition stringToSlot(std::string slotName);
  206. CArtifact::EartClass stringToClass(std::string className);
  207. /// Gets a artifact ID randomly and removes the selected artifact from this handler.
  208. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags);
  209. ArtifactID pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts);
  210. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts);
  211. bool legalArtifact(ArtifactID id);
  212. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  213. void makeItCreatureArt (CArtifact * a, bool onlyCreature = true);
  214. void makeItCommanderArt (CArtifact * a, bool onlyCommander = true);
  215. CArtHandler();
  216. ~CArtHandler();
  217. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  218. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  219. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  220. void afterLoadFinalization() override;
  221. std::vector<bool> getDefaultAllowed() const override;
  222. template <typename Handler> void serialize(Handler &h, const int version)
  223. {
  224. h & objects;
  225. h & allowedArtifacts;
  226. h & treasures;
  227. h & minors;
  228. h & majors;
  229. h & relics;
  230. h & growingArtifacts;
  231. }
  232. protected:
  233. const std::vector<std::string> & getTypeNames() const override;
  234. CArtifact * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  235. private:
  236. void addSlot(CArtifact * art, const std::string & slotID);
  237. void loadSlots(CArtifact * art, const JsonNode & node);
  238. void loadClass(CArtifact * art, const JsonNode & node);
  239. void loadType(CArtifact * art, const JsonNode & node);
  240. void loadComponents(CArtifact * art, const JsonNode & node);
  241. void loadGrowingArt(CGrowingArtifact * art, const JsonNode & node);
  242. void erasePickedArt(ArtifactID id);
  243. };
  244. struct DLL_LINKAGE ArtSlotInfo
  245. {
  246. ConstTransitivePtr<CArtifactInstance> artifact;
  247. ui8 locked; //if locked, then artifact points to the combined artifact
  248. ArtSlotInfo() : locked(false) {}
  249. template <typename Handler> void serialize(Handler &h, const int version)
  250. {
  251. h & artifact;
  252. h & locked;
  253. }
  254. };
  255. class DLL_LINKAGE CArtifactSet
  256. {
  257. public:
  258. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  259. 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
  260. ArtSlotInfo & retrieveNewArtSlot(ArtifactPosition slot);
  261. void setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked);
  262. void eraseArtSlot(ArtifactPosition slot);
  263. const ArtSlotInfo *getSlot(ArtifactPosition pos) const;
  264. const CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true) const; //nullptr - no artifact
  265. CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //nullptr - no artifact
  266. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  267. /// (if more than one such artifact lower ID is returned)
  268. ArtifactPosition getArtPos(int aid, bool onlyWorn = true, bool allowLocked = true) const;
  269. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  270. const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const;
  271. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  272. const CArtifactInstance *getHiddenArt(int aid) const;
  273. const CCombinedArtifactInstance *getAssemblyByConstituent(int aid) const;
  274. /// Checks if hero possess artifact of given id (either in backack or worn)
  275. bool hasArt(ui32 aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const;
  276. bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const;
  277. virtual ArtBearer::ArtBearer bearerType() const = 0;
  278. virtual void putArtifact(ArtifactPosition pos, CArtifactInstance * art) = 0;
  279. virtual ~CArtifactSet();
  280. template <typename Handler> void serialize(Handler &h, const int version)
  281. {
  282. h & artifactsInBackpack;
  283. h & artifactsWorn;
  284. }
  285. void artDeserializationFix(CBonusSystemNode *node);
  286. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map);
  287. protected:
  288. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(int aid) const;
  289. private:
  290. void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
  291. void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
  292. void serializeJsonCommander(JsonSerializeFormat & handler, CMap * map);
  293. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map);//normal slots
  294. };