CArtHandler.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. VCMI_LIB_NAMESPACE_BEGIN
  17. class CArtHandler;
  18. class CArtifact;
  19. class CGHeroInstance;
  20. struct ArtifactLocation;
  21. class CArtifactSet;
  22. class CArtifactInstance;
  23. class CRandomGenerator;
  24. class CMap;
  25. class JsonSerializeFormat;
  26. #define ART_BEARER_LIST \
  27. ART_BEARER(HERO)\
  28. ART_BEARER(CREATURE)\
  29. ART_BEARER(COMMANDER)
  30. namespace ArtBearer
  31. {
  32. enum ArtBearer
  33. {
  34. #define ART_BEARER(x) x,
  35. ART_BEARER_LIST
  36. #undef ART_BEARER
  37. };
  38. }
  39. class DLL_LINKAGE CArtifact : public Artifact, public CBonusSystemNode //container for artifacts
  40. {
  41. ArtifactID id;
  42. std::string modScope;
  43. std::string identifier;
  44. public:
  45. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  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 = ArtifactID::NONE;
  50. ui32 price = 0;
  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 = ART_SPECIAL;
  55. CreatureID warMachine;
  56. int32_t getIndex() const override;
  57. int32_t getIconIndex() const override;
  58. std::string getJsonKey() const override;
  59. void registerIcons(const IconRegistar & cb) const override;
  60. ArtifactID getId() const override;
  61. virtual const IBonusBearer * accessBonuses() const override;
  62. std::string getDescriptionTranslated() const override;
  63. std::string getEventTranslated() const override;
  64. std::string getNameTranslated() const override;
  65. std::string getDescriptionTextID() const override;
  66. std::string getEventTextID() const override;
  67. std::string getNameTextID() const override;
  68. uint32_t getPrice() const override;
  69. CreatureID getWarMachine() const override;
  70. bool isBig() const override;
  71. bool isTradable() const override;
  72. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  73. std::string nodeName() const override;
  74. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  75. virtual void levelUpArtifact (CArtifactInstance * art){};
  76. virtual bool canBeDisassembled() const;
  77. virtual bool canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const;
  78. void updateFrom(const JsonNode & data);
  79. void serializeJson(JsonSerializeFormat & handler);
  80. template <typename Handler> void serialize(Handler &h, const int version)
  81. {
  82. h & static_cast<CBonusSystemNode&>(*this);
  83. h & image;
  84. h & large;
  85. h & advMapDef;
  86. h & iconIndex;
  87. h & price;
  88. h & possibleSlots;
  89. h & constituents;
  90. h & constituentOf;
  91. h & aClass;
  92. h & id;
  93. h & modScope;
  94. h & identifier;
  95. h & warMachine;
  96. }
  97. CArtifact();
  98. ~CArtifact();
  99. friend class CArtHandler;
  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. bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; //forwards to the above one
  132. virtual bool canBeDisassembled() const;
  133. virtual void putAt(ArtifactLocation al);
  134. virtual void removeFrom(ArtifactLocation al);
  135. /// Checks if this a part of this artifact: artifact instance is a part
  136. /// of itself, additionally truth is returned for constituents of combined arts
  137. virtual bool isPart(const CArtifactInstance *supposedPart) const;
  138. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet * h, bool equipped) const;
  139. void move(const ArtifactLocation & src,const ArtifactLocation & dst);
  140. template <typename Handler> void serialize(Handler &h, const int version)
  141. {
  142. h & static_cast<CBonusSystemNode&>(*this);
  143. h & artType;
  144. h & id;
  145. BONUS_TREE_DESERIALIZATION_FIX
  146. }
  147. static CArtifactInstance * createScroll(const SpellID & sid);
  148. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  149. static CArtifactInstance * createNewArtifactInstance(const ArtifactID & aid);
  150. /**
  151. * Creates an artifact instance.
  152. *
  153. * @param aid the id of the artifact
  154. * @param spellID optional. the id of a spell if a spell scroll object should be created
  155. * @return the created artifact instance
  156. */
  157. static CArtifactInstance * createArtifact(CMap * map, const ArtifactID & aid, int spellID = -1);
  158. };
  159. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  160. {
  161. CCombinedArtifactInstance(CArtifact *Art);
  162. public:
  163. struct ConstituentInfo
  164. {
  165. ConstTransitivePtr<CArtifactInstance> art;
  166. ArtifactPosition slot;
  167. template <typename Handler> void serialize(Handler &h, const int version)
  168. {
  169. h & art;
  170. h & slot;
  171. }
  172. bool operator==(const ConstituentInfo &rhs) const;
  173. ConstituentInfo(CArtifactInstance * art = nullptr, const ArtifactPosition & slot = ArtifactPosition::PRE_FIRST);
  174. };
  175. std::vector<ConstituentInfo> constituentsInfo;
  176. void putAt(ArtifactLocation al) override;
  177. void removeFrom(ArtifactLocation al) override;
  178. bool isPart(const CArtifactInstance *supposedPart) const override;
  179. void createConstituents();
  180. void addAsConstituent(CArtifactInstance * art, const ArtifactPosition & slot);
  181. CArtifactInstance * figureMainConstituent(const ArtifactLocation & al); //main constituent is replaced with us (combined art), not lock
  182. CCombinedArtifactInstance() = default;
  183. void deserializationFix();
  184. friend class CArtifactInstance;
  185. friend struct AssembledArtifact;
  186. template <typename Handler> void serialize(Handler &h, const int version)
  187. {
  188. h & static_cast<CArtifactInstance&>(*this);
  189. h & constituentsInfo;
  190. BONUS_TREE_DESERIALIZATION_FIX
  191. }
  192. };
  193. class DLL_LINKAGE CArtHandler : public CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  194. {
  195. public:
  196. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  197. std::vector<CArtifact *> allowedArtifacts;
  198. std::set<ArtifactID> growingArtifacts;
  199. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  200. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of given class. No side effects
  201. boost::optional<std::vector<CArtifact*>&> listFromClass(CArtifact::EartClass artifactClass);
  202. static CArtifact::EartClass stringToClass(const std::string & className); //TODO: rework EartClass to make this a constructor
  203. /// Gets a artifact ID randomly and removes the selected artifact from this handler.
  204. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags);
  205. ArtifactID pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts);
  206. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts);
  207. bool legalArtifact(const ArtifactID & id);
  208. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  209. static void makeItCreatureArt(CArtifact * a, bool onlyCreature = true);
  210. static void makeItCommanderArt(CArtifact * a, bool onlyCommander = true);
  211. ~CArtHandler();
  212. std::vector<JsonNode> loadLegacyData() override;
  213. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  214. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  215. void afterLoadFinalization() override;
  216. std::vector<bool> getDefaultAllowed() const override;
  217. template <typename Handler> void serialize(Handler &h, const int version)
  218. {
  219. h & objects;
  220. h & allowedArtifacts;
  221. h & treasures;
  222. h & minors;
  223. h & majors;
  224. h & relics;
  225. h & growingArtifacts;
  226. }
  227. protected:
  228. const std::vector<std::string> & getTypeNames() const override;
  229. CArtifact * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  230. private:
  231. void addSlot(CArtifact * art, const std::string & slotID) const;
  232. void loadSlots(CArtifact * art, const JsonNode & node) const;
  233. void loadClass(CArtifact * art, const JsonNode & node) const;
  234. void loadType(CArtifact * art, const JsonNode & node) const;
  235. void loadComponents(CArtifact * art, const JsonNode & node);
  236. void loadGrowingArt(CGrowingArtifact * art, const JsonNode & node) const;
  237. void erasePickedArt(const ArtifactID & id);
  238. };
  239. struct DLL_LINKAGE ArtSlotInfo
  240. {
  241. ConstTransitivePtr<CArtifactInstance> artifact;
  242. ui8 locked; //if locked, then artifact points to the combined artifact
  243. ArtSlotInfo() : locked(false) {}
  244. const CArtifactInstance * getArt() const;
  245. template <typename Handler> void serialize(Handler & h, const int version)
  246. {
  247. h & artifact;
  248. h & locked;
  249. }
  250. };
  251. class DLL_LINKAGE CArtifactSet
  252. {
  253. public:
  254. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  255. 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
  256. std::vector<ArtSlotInfo> artifactsTransitionPos; // Used as transition position for dragAndDrop artifact exchange
  257. ArtSlotInfo & retrieveNewArtSlot(const ArtifactPosition & slot);
  258. void setNewArtSlot(const ArtifactPosition & slot, CArtifactInstance * art, bool locked);
  259. void eraseArtSlot(const ArtifactPosition & slot);
  260. const ArtSlotInfo * getSlot(const ArtifactPosition & pos) const;
  261. const CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true) const; //nullptr - no artifact
  262. CArtifactInstance * getArt(const ArtifactPosition & pos, bool excludeLocked = true); //nullptr - no artifact
  263. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  264. /// (if more than one such artifact lower ID is returned)
  265. ArtifactPosition getArtPos(const ArtifactID & aid, bool onlyWorn = true, bool allowLocked = true) const;
  266. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  267. ArtifactPosition getArtBackpackPos(const ArtifactID & aid) const;
  268. std::vector<ArtifactPosition> getAllArtPositions(const ArtifactID & aid, bool onlyWorn, bool allowLocked, bool getAll) const;
  269. std::vector<ArtifactPosition> getBackpackArtPositions(const ArtifactID & aid) const;
  270. const CArtifactInstance * getArtByInstanceId(const ArtifactInstanceID & artInstId) const;
  271. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  272. const CArtifactInstance * getHiddenArt(const ArtifactID & aid) const;
  273. const CCombinedArtifactInstance * getAssemblyByConstituent(const ArtifactID & aid) const;
  274. /// Checks if hero possess artifact of given id (either in backack or worn)
  275. bool hasArt(const ArtifactID & aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const;
  276. bool hasArtBackpack(const ArtifactID & aid) const;
  277. bool isPositionFree(const ArtifactPosition & pos, bool onlyLockCheck = false) const;
  278. unsigned getArtPosCount(const ArtifactID & aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const;
  279. virtual ArtBearer::ArtBearer bearerType() const = 0;
  280. virtual void putArtifact(ArtifactPosition pos, CArtifactInstance * art) = 0;
  281. virtual ~CArtifactSet();
  282. template <typename Handler> void serialize(Handler &h, const int version)
  283. {
  284. h & artifactsInBackpack;
  285. h & artifactsWorn;
  286. }
  287. void artDeserializationFix(CBonusSystemNode *node);
  288. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map);
  289. protected:
  290. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(const ArtifactID & aid) const;
  291. private:
  292. void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
  293. void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
  294. void serializeJsonCommander(JsonSerializeFormat & handler, CMap * map);
  295. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map);//normal slots
  296. };
  297. // Used to try on artifacts before the claimed changes have been applied
  298. class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
  299. {
  300. public:
  301. CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
  302. void putArtifact(ArtifactPosition pos, CArtifactInstance * art) override;
  303. void removeArtifact(ArtifactPosition pos);
  304. ArtBearer::ArtBearer bearerType() const override;
  305. protected:
  306. ArtBearer::ArtBearer Bearer;
  307. };
  308. namespace ArtifactUtils
  309. {
  310. // Calculates where an artifact gets placed when it gets transferred from one hero to another.
  311. DLL_LINKAGE ArtifactPosition getArtAnyPosition(const CArtifactSet * target, const ArtifactID & aid);
  312. DLL_LINKAGE ArtifactPosition getArtBackpackPosition(const CArtifactSet * target, const ArtifactID & aid);
  313. // TODO: Make this constexpr when the toolset is upgraded
  314. DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & unmovableSlots();
  315. DLL_LINKAGE const std::vector<ArtifactPosition::EArtifactPosition> & constituentWornSlots();
  316. DLL_LINKAGE bool isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot);
  317. DLL_LINKAGE bool checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, const ArtifactID & artID, const ArtifactPosition & slot);
  318. DLL_LINKAGE bool isSlotBackpack(const ArtifactPosition & slot);
  319. DLL_LINKAGE bool isSlotEquipment(const ArtifactPosition & slot);
  320. DLL_LINKAGE bool isBackpackFreeSlots(const CArtifactSet * target, const size_t reqSlots = 1);
  321. DLL_LINKAGE bool isPossibleToGetArt(const CArtifactSet * target, const ArtifactID & aid,
  322. ArtifactPosition slot = ArtifactPosition::FIRST_AVAILABLE);
  323. }
  324. VCMI_LIB_NAMESPACE_END