2
0

CArtHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #pragma once
  2. #include "../lib/HeroBonus.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. #include "JsonNode.h"
  5. #include "GameConstants.h"
  6. /*
  7. * CArtHandler.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CArtHandler;
  16. class CDefHandler;
  17. class CArtifact;
  18. class CGHeroInstance;
  19. struct ArtifactLocation;
  20. class CArtifactSet;
  21. class CArtifactInstance;
  22. #define ART_BEARER_LIST \
  23. ART_BEARER(HERO)\
  24. ART_BEARER(CREATURE)\
  25. ART_BEARER(COMMANDER)
  26. namespace ArtBearer
  27. {
  28. enum ArtBearer
  29. {
  30. #define ART_BEARER(x) x,
  31. ART_BEARER_LIST
  32. #undef ART_BEARER
  33. };
  34. }
  35. class DLL_LINKAGE CArtifact : public CBonusSystemNode //container for artifacts
  36. {
  37. protected:
  38. std::string name, description; //set if custom
  39. std::string eventText; //short story displayed upon picking
  40. public:
  41. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  42. std::string image;
  43. std::string large; // big image for cutom artifacts, used in drag & drop
  44. std::string advMapDef; //used for adventure map object
  45. si32 iconIndex; //TODO: handle automatically
  46. const std::string &Name() const; //getter
  47. const std::string &Description() const; //getter
  48. const std::string &EventText() const;
  49. bool isBig () const;
  50. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  51. std::string nodeName() const OVERRIDE;
  52. void addNewBonus(Bonus *b) OVERRIDE;
  53. virtual void levelUpArtifact (CArtifactInstance * art){};
  54. ui32 price;
  55. bmap<ArtBearer::ArtBearer, std::vector<ArtifactPosition> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  56. std::unique_ptr<std::vector<CArtifact *> > constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  57. std::vector<CArtifact *> constituentOf; // Reverse map of constituents - combined arts that include this art
  58. EartClass aClass;
  59. ArtifactID id;
  60. template <typename Handler> void serialize(Handler &h, const int version)
  61. {
  62. h & static_cast<CBonusSystemNode&>(*this);
  63. h & name & description & eventText & image & large & advMapDef & iconIndex &
  64. price & possibleSlots & constituents & constituentOf & aClass & id;
  65. }
  66. CArtifact();
  67. ~CArtifact();
  68. friend class CArtHandler;
  69. };
  70. class DLL_LINKAGE CGrowingArtifact : public CArtifact //for example commander artifacts getting bonuses after battle
  71. {
  72. public:
  73. std::vector <std::pair <ui16, Bonus> > bonusesPerLevel; //bonus given each n levels
  74. std::vector <std::pair <ui16, Bonus> > thresholdBonuses; //after certain level they will be added once
  75. void levelUpArtifact (CArtifactInstance * art);
  76. template <typename Handler> void serialize(Handler &h, const int version)
  77. {
  78. h & static_cast<CArtifact&>(*this);
  79. h & bonusesPerLevel & thresholdBonuses;
  80. }
  81. };
  82. class DLL_LINKAGE CArtifactInstance : public CBonusSystemNode
  83. {
  84. protected:
  85. void init();
  86. CArtifactInstance(CArtifact *Art);
  87. public:
  88. CArtifactInstance();
  89. ConstTransitivePtr<CArtifact> artType;
  90. ArtifactInstanceID id;
  91. //CArtifactInstance(int aid);
  92. std::string nodeName() const OVERRIDE;
  93. void deserializationFix();
  94. void setType(CArtifact *Art);
  95. ArtifactPosition firstAvailableSlot(const CArtifactSet *h) const;
  96. ArtifactPosition firstBackpackSlot(const CArtifactSet *h) const;
  97. SpellID getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  98. virtual bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const;
  99. bool canBePutAt(const ArtifactLocation al, bool assumeDestRemoved = false) const; //forwards to the above one
  100. virtual bool canBeDisassembled() const;
  101. virtual void putAt(ArtifactLocation al);
  102. virtual void removeFrom(ArtifactLocation al);
  103. virtual bool isPart(const CArtifactInstance *supposedPart) const; //checks if this a part of this artifact: artifact instance is a part of itself, additionally truth is returned for consituents of combined arts
  104. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  105. void move(ArtifactLocation src, ArtifactLocation dst);
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & static_cast<CBonusSystemNode&>(*this);
  109. h & artType & id;
  110. BONUS_TREE_DESERIALIZATION_FIX
  111. }
  112. static CArtifactInstance *createScroll(const CSpell *s);
  113. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  114. static CArtifactInstance *createNewArtifactInstance(int aid);
  115. };
  116. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  117. {
  118. CCombinedArtifactInstance(CArtifact *Art);
  119. public:
  120. struct ConstituentInfo
  121. {
  122. ConstTransitivePtr<CArtifactInstance> art;
  123. ArtifactPosition slot;
  124. template <typename Handler> void serialize(Handler &h, const int version)
  125. {
  126. h & art & slot;
  127. }
  128. bool operator==(const ConstituentInfo &rhs) const;
  129. ConstituentInfo(CArtifactInstance *art = NULL, ArtifactPosition slot = ArtifactPosition::PRE_FIRST);
  130. };
  131. std::vector<ConstituentInfo> constituentsInfo;
  132. bool canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved = false) const OVERRIDE;
  133. bool canBeDisassembled() const OVERRIDE;
  134. void putAt(ArtifactLocation al) OVERRIDE;
  135. void removeFrom(ArtifactLocation al) OVERRIDE;
  136. bool isPart(const CArtifactInstance *supposedPart) const OVERRIDE;
  137. void createConstituents();
  138. void addAsConstituent(CArtifactInstance *art, ArtifactPosition slot);
  139. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replcaed with us (combined art), not lock
  140. CCombinedArtifactInstance();
  141. void deserializationFix();
  142. friend class CArtifactInstance;
  143. friend struct AssembledArtifact;
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & static_cast<CArtifactInstance&>(*this);
  147. h & constituentsInfo;
  148. BONUS_TREE_DESERIALIZATION_FIX
  149. }
  150. };
  151. class DLL_LINKAGE CArtHandler //handles artifacts
  152. {
  153. void giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype = -1, Bonus::ValueType valType = Bonus::BASE_NUMBER, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalinfo = 0);
  154. void giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator, int additionalinfo = 0);
  155. void giveArtBonus(ArtifactID aid, Bonus *bonus);
  156. public:
  157. std::vector<CArtifact*> treasures, minors, majors, relics; //tmp vectors!!! do not touch if you don't know what you are doing!!!
  158. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  159. std::vector<CArtifact *> allowedArtifacts;
  160. std::set<ArtifactID> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  161. std::set<ArtifactID> growingArtifacts;
  162. void load(bool onlyTxt = false);
  163. /// load artifact from json structure
  164. void load(std::string objectID, const JsonNode & node);
  165. /// load one artifact from json config
  166. CArtifact * loadArtifact(const JsonNode & node);
  167. void loadArtifactJson(CArtifact * art, const JsonNode & node);
  168. void addBonuses(CArtifact *art, const JsonNode &bonusList);
  169. void fillList(std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass); //fills given empty list with allowed artifacts of gibven class. No side effects
  170. boost::optional<std::vector<CArtifact*>&> listFromClass(CArtifact::EartClass artifactClass);
  171. void erasePickedArt(ArtifactID id);
  172. ArtifactID getRandomArt (int flags);
  173. ArtifactID getArtSync (ui32 rand, int flags, bool erasePicked = false);
  174. bool legalArtifact(ArtifactID id);
  175. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  176. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  177. bool isBigArtifact (ArtifactID artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  178. void initAllowedArtifactsList(const std::vector<bool> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  179. static ArtifactID creatureToMachineID(CreatureID id);
  180. static CreatureID machineIDToCreature(ArtifactID id);
  181. void makeItCreatureArt (CArtifact * a, bool onlyCreature = true);
  182. void makeItCreatureArt (ArtifactID aid, bool onlyCreature = true);
  183. void makeItCommanderArt (CArtifact * a, bool onlyCommander = true);
  184. void makeItCommanderArt (ArtifactID aid, bool onlyCommander = true);
  185. CArtHandler();
  186. ~CArtHandler();
  187. /**
  188. * Gets a list of default allowed artifacts.
  189. *
  190. * @return a list of allowed artifacts, the index is the artifact id
  191. */
  192. std::vector<bool> getDefaultAllowedArtifacts() const;
  193. template <typename Handler> void serialize(Handler &h, const int version)
  194. {
  195. h & artifacts & allowedArtifacts & treasures & minors & majors & relics
  196. & growingArtifacts;
  197. //if(!h.saving) sortArts();
  198. }
  199. };
  200. struct DLL_LINKAGE ArtSlotInfo
  201. {
  202. ConstTransitivePtr<CArtifactInstance> artifact;
  203. ui8 locked; //if locked, then artifact points to the combined artifact
  204. ArtSlotInfo()
  205. {
  206. locked = false;
  207. }
  208. template <typename Handler> void serialize(Handler &h, const int version)
  209. {
  210. h & artifact & locked;
  211. }
  212. };
  213. class DLL_LINKAGE CArtifactSet
  214. {
  215. public:
  216. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  217. bmap<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
  218. ArtSlotInfo &retreiveNewArtSlot(ArtifactPosition slot);
  219. void setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked);
  220. void eraseArtSlot(ArtifactPosition slot);
  221. const ArtSlotInfo *getSlot(ArtifactPosition pos) const;
  222. const CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true) const; //NULL - no artifact
  223. CArtifactInstance* getArt(ArtifactPosition pos, bool excludeLocked = true); //NULL - no artifact
  224. ArtifactPosition getArtPos(int aid, bool onlyWorn = true) const; //looks for equipped artifact with given ID and returns its slot ID or -1 if none(if more than one such artifact lower ID is returned)
  225. ArtifactPosition getArtPos(const CArtifactInstance *art) const;
  226. const CArtifactInstance *getArtByInstanceId(ArtifactInstanceID artInstId) const;
  227. bool hasArt(ui32 aid, bool onlyWorn = false) const; //checks if hero possess artifact of given id (either in backack or worn)
  228. bool isPositionFree(ArtifactPosition pos, bool onlyLockCheck = false) const;
  229. si32 getArtTypeId(ArtifactPosition pos) const;
  230. virtual ArtBearer::ArtBearer bearerType() const = 0;
  231. virtual ~CArtifactSet();
  232. template <typename Handler> void serialize(Handler &h, const int version)
  233. {
  234. h & artifactsInBackpack & artifactsWorn;
  235. }
  236. void artDeserializationFix(CBonusSystemNode *node);
  237. };