CArtHandler.h 13 KB

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