CArtHandler.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. protected:
  42. std::string name, description; //set if custom
  43. std::string eventText; //short story displayed upon picking
  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 identifier;
  47. std::string image;
  48. std::string large; // big image for custom artifacts, used in drag & drop
  49. std::string advMapDef; //used for adventure map object
  50. si32 iconIndex;
  51. ui32 price;
  52. std::map<ArtBearer::ArtBearer, std::vector<ArtifactPosition> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  53. std::unique_ptr<std::vector<CArtifact *> > constituents; // Artifacts IDs a combined artifact consists of, or nullptr.
  54. std::vector<CArtifact *> constituentOf; // Reverse map of constituents - combined arts that include this art
  55. EartClass aClass;
  56. ArtifactID id;
  57. CreatureID warMachine;
  58. int32_t getIndex() const override;
  59. int32_t getIconIndex() const override;
  60. const std::string & getName() const override;
  61. const std::string & getJsonKey() const override;
  62. void registerIcons(const IconRegistar & cb) const override;
  63. ArtifactID getId() const override;
  64. virtual const IBonusBearer * accessBonuses() const override;
  65. const std::string & getEventText() const override;
  66. const std::string & getDescription() const override;
  67. uint32_t getPrice() const override;
  68. CreatureID getWarMachine() const override;
  69. bool isBig() const override;
  70. bool isTradable() const override;
  71. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  72. std::string nodeName() const override;
  73. void addNewBonus(const std::shared_ptr<Bonus>& b) override;
  74. virtual void levelUpArtifact (CArtifactInstance * art){};
  75. void updateFrom(const JsonNode & data);
  76. void serializeJson(JsonSerializeFormat & handler);
  77. template <typename Handler> void serialize(Handler &h, const int version)
  78. {
  79. h & static_cast<CBonusSystemNode&>(*this);
  80. h & name;
  81. h & description;
  82. h & eventText;
  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 & identifier;
  94. h & warMachine;
  95. }
  96. CArtifact();
  97. ~CArtifact();
  98. friend class CArtHandler;
  99. private:
  100. void fillWarMachine();
  101. };
  102. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  103. {
  104. public:
  105. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  106. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  107. void levelUpArtifact(CArtifactInstance * art) override;
  108. template <typename Handler> void serialize(Handler &h, const int version)
  109. {
  110. h & static_cast<CArtifact&>(*this);
  111. h & bonusesPerLevel;
  112. h & thresholdBonuses;
  113. }
  114. };
  115. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  116. {
  117. protected:
  118. void init();
  119. CArtifactInstance(CArtifact *Art);
  120. public:
  121. CArtifactInstance();
  122. ConstTransitivePtr<CArtifact> artType;
  123. ArtifactInstanceID id;
  124. //CArtifactInstance(int aid);
  125. std::string nodeName() const override;
  126. void deserializationFix();
  127. void setType(CArtifact *Art);
  128. std::string getEffectiveDescription(const CGHeroInstance *hero = nullptr) const;
  129. ArtifactPosition firstAvailableSlot(const CArtifactSet *h) const;
  130. ArtifactPosition firstBackpackSlot(const CArtifactSet *h) const;
  131. SpellID getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  132. virtual bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const;
  133. bool canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved = false) const; //forwards to the above one
  134. virtual bool canBeDisassembled() const;
  135. virtual void putAt(ArtifactLocation al);
  136. virtual void removeFrom(ArtifactLocation al);
  137. /// Checks if this a part of this artifact: artifact instance is a part
  138. /// of itself, additionally truth is returned for constituents of combined arts
  139. virtual bool isPart(const CArtifactInstance *supposedPart) const;
  140. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet * h, bool equipped) const;
  141. void move(ArtifactLocation src, ArtifactLocation dst);
  142. template <typename Handler> void serialize(Handler &h, const int version)
  143. {
  144. h & static_cast<CBonusSystemNode&>(*this);
  145. h & artType;
  146. h & id;
  147. BONUS_TREE_DESERIALIZATION_FIX
  148. }
  149. static CArtifactInstance *createScroll(SpellID sid);
  150. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  151. static CArtifactInstance *createNewArtifactInstance(ArtifactID 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, ArtifactID 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 CHandlerBase<ArtifactID, Artifact, CArtifact, ArtifactService>
  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<CArtifact *> allowedArtifacts;
  202. std::set<ArtifactID> growingArtifacts;
  203. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  204. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of given class. No side effects
  205. boost::optional<std::vector<CArtifact*>&> listFromClass(CArtifact::EartClass artifactClass);
  206. ArtifactPosition stringToSlot(std::string slotName);
  207. CArtifact::EartClass stringToClass(std::string className);
  208. /// Gets a artifact ID randomly and removes the selected artifact from this handler.
  209. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags);
  210. ArtifactID pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts);
  211. ArtifactID pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts);
  212. bool legalArtifact(ArtifactID id);
  213. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  214. void makeItCreatureArt (CArtifact * a, bool onlyCreature = true);
  215. void makeItCommanderArt (CArtifact * a, bool onlyCommander = true);
  216. CArtHandler();
  217. ~CArtHandler();
  218. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  219. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  220. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  221. void afterLoadFinalization() override;
  222. std::vector<bool> getDefaultAllowed() const override;
  223. template <typename Handler> void serialize(Handler &h, const int version)
  224. {
  225. h & objects;
  226. h & allowedArtifacts;
  227. h & treasures;
  228. h & minors;
  229. h & majors;
  230. h & relics;
  231. h & growingArtifacts;
  232. }
  233. protected:
  234. const std::vector<std::string> & getTypeNames() const override;
  235. CArtifact * loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) override;
  236. private:
  237. void addSlot(CArtifact * art, const std::string & slotID);
  238. void loadSlots(CArtifact * art, const JsonNode & node);
  239. void loadClass(CArtifact * art, const JsonNode & node);
  240. void loadType(CArtifact * art, const JsonNode & node);
  241. void loadComponents(CArtifact * art, const JsonNode & node);
  242. void loadGrowingArt(CGrowingArtifact * art, const JsonNode & node);
  243. void erasePickedArt(ArtifactID id);
  244. };
  245. struct DLL_LINKAGE ArtSlotInfo
  246. {
  247. ConstTransitivePtr<CArtifactInstance> artifact;
  248. ui8 locked; //if locked, then artifact points to the combined artifact
  249. ArtSlotInfo() : locked(false) {}
  250. const CArtifactInstance * getArt() const;
  251. template <typename Handler> void serialize(Handler & h, const int version)
  252. {
  253. h & artifact;
  254. h & locked;
  255. }
  256. };
  257. class DLL_LINKAGE CArtifactSet
  258. {
  259. public:
  260. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  261. 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
  262. ArtSlotInfo & retrieveNewArtSlot(ArtifactPosition slot);
  263. void setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked);
  264. void eraseArtSlot(ArtifactPosition slot);
  265. const ArtSlotInfo *getSlot(ArtifactPosition pos) const;
  266. const CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true) const; //nullptr - no artifact
  267. CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //nullptr - no artifact
  268. /// Looks for equipped artifact with given ID and returns its slot ID or -1 if none
  269. /// (if more than one such artifact lower ID is returned)
  270. ArtifactPosition getArtPos(ArtifactID aid, bool onlyWorn = true, bool allowLocked = true) const;
  271. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  272. ArtifactPosition getArtBackpackPos(ArtifactID aid) const;
  273. std::vector<ArtifactPosition> getAllArtPositions(ArtifactID aid, bool onlyWorn, bool allowLocked, bool getAll) const;
  274. std::vector<ArtifactPosition> getBackpackArtPositions(ArtifactID aid) const;
  275. const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const;
  276. /// Search for constituents of assemblies in backpack which do not have an ArtifactPosition
  277. const CArtifactInstance *getHiddenArt(ArtifactID aid) const;
  278. const CCombinedArtifactInstance *getAssemblyByConstituent(ArtifactID aid) const;
  279. /// Checks if hero possess artifact of given id (either in backack or worn)
  280. bool hasArt(ArtifactID aid, bool onlyWorn = false, bool searchBackpackAssemblies = false, bool allowLocked = true) const;
  281. bool hasArtBackpack(ArtifactID aid) const;
  282. bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const;
  283. unsigned getArtPosCount(ArtifactID aid, bool onlyWorn = true, bool searchBackpackAssemblies = true, bool allowLocked = true) const;
  284. virtual ArtBearer::ArtBearer bearerType() const = 0;
  285. virtual void putArtifact(ArtifactPosition pos, CArtifactInstance * art) = 0;
  286. virtual ~CArtifactSet();
  287. template <typename Handler> void serialize(Handler &h, const int version)
  288. {
  289. h & artifactsInBackpack;
  290. h & artifactsWorn;
  291. }
  292. void artDeserializationFix(CBonusSystemNode *node);
  293. void serializeJsonArtifacts(JsonSerializeFormat & handler, const std::string & fieldName, CMap * map);
  294. protected:
  295. std::pair<const CCombinedArtifactInstance *, const CArtifactInstance *> searchForConstituent(ArtifactID aid) const;
  296. private:
  297. void serializeJsonHero(JsonSerializeFormat & handler, CMap * map);
  298. void serializeJsonCreature(JsonSerializeFormat & handler, CMap * map);
  299. void serializeJsonCommander(JsonSerializeFormat & handler, CMap * map);
  300. void serializeJsonSlot(JsonSerializeFormat & handler, const ArtifactPosition & slot, CMap * map);//normal slots
  301. };
  302. // Used to try on artifacts before the claimed changes have been applied
  303. class DLL_LINKAGE CArtifactFittingSet : public CArtifactSet
  304. {
  305. public:
  306. CArtifactFittingSet(ArtBearer::ArtBearer Bearer);
  307. void setNewArtSlot(ArtifactPosition slot, CArtifactInstance * art, bool locked);
  308. void putArtifact(ArtifactPosition pos, CArtifactInstance * art) override;
  309. ArtBearer::ArtBearer bearerType() const override;
  310. protected:
  311. ArtBearer::ArtBearer Bearer;
  312. };
  313. namespace ArtifactUtils
  314. {
  315. // Calculates where an artifact gets placed when it gets transferred from one hero to another.
  316. DLL_LINKAGE ArtifactPosition getArtifactDstPosition( const CArtifactInstance * artifact,
  317. const CArtifactSet * target,
  318. ArtBearer::ArtBearer bearer);
  319. const std::array<ArtifactPosition::EArtifactPosition, 2> unmovableSlots =
  320. {
  321. ArtifactPosition::SPELLBOOK,
  322. ArtifactPosition::MACH4
  323. };
  324. const std::array<ArtifactPosition::EArtifactPosition, 14> constituentWornSlots =
  325. {
  326. ArtifactPosition::HEAD,
  327. ArtifactPosition::SHOULDERS,
  328. ArtifactPosition::NECK,
  329. ArtifactPosition::RIGHT_HAND,
  330. ArtifactPosition::LEFT_HAND,
  331. ArtifactPosition::TORSO,
  332. ArtifactPosition::RIGHT_RING,
  333. ArtifactPosition::LEFT_RING,
  334. ArtifactPosition::FEET,
  335. ArtifactPosition::MISC1,
  336. ArtifactPosition::MISC2,
  337. ArtifactPosition::MISC3,
  338. ArtifactPosition::MISC4,
  339. ArtifactPosition::MISC5,
  340. };
  341. DLL_LINKAGE bool isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot);
  342. DLL_LINKAGE bool checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, ArtifactID artID, ArtifactPosition slot);
  343. DLL_LINKAGE bool isSlotBackpack(ArtifactPosition slot);
  344. }
  345. VCMI_LIB_NAMESPACE_END