CArtHandler.h 15 KB

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