CArtHandler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #pragma once
  2. #include "../lib/HeroBonus.h"
  3. #include "../lib/ConstTransitivePtr.h"
  4. #include "JsonNode.h"
  5. /*
  6. * CArtHandler.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class CDefHandler;
  15. class CArtifact;
  16. class CGHeroInstance;
  17. struct ArtifactLocation;
  18. class CArtifactSet;
  19. class CArtifactInstance;
  20. namespace ArtifactPosition
  21. {
  22. enum ArtifactPosition
  23. {
  24. PRE_FIRST = -1,
  25. HEAD, SHOULDERS, NECK, RIGHT_HAND, LEFT_HAND, TORSO, RIGHT_RING, LEFT_RING, FEET, MISC1, MISC2, MISC3, MISC4,
  26. MACH1, MACH2, MACH3, MACH4, SPELLBOOK, MISC5,
  27. AFTER_LAST,
  28. //cres
  29. CREATURE_SLOT = 0,
  30. COMMANDER1 = 0, COMMANDER2, COMMANDER3, COMMANDER4, COMMANDER5, COMMANDER6, COMMANDER_AFTER_LAST
  31. };
  32. }
  33. namespace ArtifactID
  34. {
  35. enum ArtifactID
  36. {
  37. SPELLBOOK = 0,
  38. SPELL_SCROLL = 1,
  39. GRAIL = 2,
  40. CATAPULT = 3,
  41. BALLISTA = 4,
  42. AMMO_CART = 5,
  43. FIRST_AID_TENT = 6,
  44. CENTAUR_AXE = 7,
  45. BLACKSHARD_OF_THE_DEAD_KNIGHT = 8,
  46. CORNUCOPIA = 140
  47. };
  48. }
  49. #define ART_BEARER_LIST \
  50. ART_BEARER(HERO)\
  51. ART_BEARER(CREATURE)\
  52. ART_BEARER(COMMANDER)
  53. namespace ArtBearer
  54. {
  55. enum
  56. {
  57. #define ART_BEARER(x) x,
  58. ART_BEARER_LIST
  59. #undef ART_BEARER
  60. };
  61. }
  62. class DLL_LINKAGE CArtifact : public CBonusSystemNode //container for artifacts
  63. {
  64. protected:
  65. std::string name, description; //set if custom
  66. std::string eventText; //short story displayed upon picking
  67. public:
  68. enum EartClass {ART_SPECIAL=1, ART_TREASURE=2, ART_MINOR=4, ART_MAJOR=8, ART_RELIC=16}; //artifact classes
  69. std::string image;
  70. std::string large; // big image for cutom artifacts, used in drag & drop
  71. std::string advMapDef; //used for adventure map object
  72. si32 iconIndex; //TODO: handle automatically
  73. const std::string &Name() const; //getter
  74. const std::string &Description() const; //getter
  75. const std::string &EventText() const;
  76. bool isBig () const;
  77. void setName (std::string desc);
  78. void setDescription (std::string desc);
  79. void setEventText (std::string desc);
  80. void addConstituent (ui32 component);
  81. int getArtClassSerial() const; //0 - treasure, 1 - minor, 2 - major, 3 - relic, 4 - spell scroll, 5 - other
  82. std::string nodeName() const OVERRIDE;
  83. void addNewBonus(Bonus *b) OVERRIDE;
  84. virtual void levelUpArtifact (CArtifactInstance * art){};
  85. ui32 price;
  86. bmap<ui8, std::vector<ui16> > possibleSlots; //Bearer Type => ids of slots where artifact can be placed
  87. std::vector<ui32> * constituents; // Artifacts IDs a combined artifact consists of, or NULL.
  88. std::vector<ui32> * constituentOf; // Reverse map of constituents.
  89. EartClass aClass;
  90. si32 id;
  91. template <typename Handler> void serialize(Handler &h, const int version)
  92. {
  93. h & static_cast<CBonusSystemNode&>(*this);
  94. h & name & description & eventText & image & large & advMapDef & iconIndex &
  95. price & possibleSlots & constituents & constituentOf & aClass & id;
  96. }
  97. CArtifact();
  98. ~CArtifact();
  99. //override
  100. //void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
  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);
  108. template <typename Handler> void serialize(Handler &h, const int version)
  109. {
  110. h & static_cast<CArtifact&>(*this);
  111. h & bonusesPerLevel & 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. TArtifactInstanceID id;
  123. //CArtifactInstance(int aid);
  124. std::string nodeName() const OVERRIDE;
  125. void deserializationFix();
  126. void setType(CArtifact *Art);
  127. int firstAvailableSlot(const CArtifactSet *h) const;
  128. int firstBackpackSlot(const CArtifactSet *h) const;
  129. int getGivenSpellID() const; //to be used with scrolls (and similar arts), -1 if none
  130. virtual bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const;
  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. 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
  136. std::vector<const CArtifact *> assemblyPossibilities(const CArtifactSet *h) const;
  137. void move(ArtifactLocation src, ArtifactLocation dst);
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & static_cast<CBonusSystemNode&>(*this);
  141. h & artType & id;
  142. BONUS_TREE_DESERIALIZATION_FIX
  143. }
  144. static CArtifactInstance *createScroll(const CSpell *s);
  145. static CArtifactInstance *createNewArtifactInstance(CArtifact *Art);
  146. static CArtifactInstance *createNewArtifactInstance(int aid);
  147. };
  148. class DLL_LINKAGE CCombinedArtifactInstance : public CArtifactInstance
  149. {
  150. CCombinedArtifactInstance(CArtifact *Art);
  151. public:
  152. struct ConstituentInfo
  153. {
  154. ConstTransitivePtr<CArtifactInstance> art;
  155. si16 slot;
  156. template <typename Handler> void serialize(Handler &h, const int version)
  157. {
  158. h & art & slot;
  159. }
  160. bool operator==(const ConstituentInfo &rhs) const;
  161. ConstituentInfo(CArtifactInstance *art = NULL, ui16 slot = -1);
  162. };
  163. std::vector<ConstituentInfo> constituentsInfo;
  164. bool canBePutAt(const CArtifactSet *artSet, int slot, bool assumeDestRemoved = false) const OVERRIDE;
  165. bool canBeDisassembled() const OVERRIDE;
  166. void putAt(ArtifactLocation al) OVERRIDE;
  167. void removeFrom(ArtifactLocation al) OVERRIDE;
  168. bool isPart(const CArtifactInstance *supposedPart) const OVERRIDE;
  169. void createConstituents();
  170. void addAsConstituent(CArtifactInstance *art, int slot);
  171. CArtifactInstance *figureMainConstituent(const ArtifactLocation al); //main constituent is replcaed with us (combined art), not lock
  172. CCombinedArtifactInstance();
  173. void deserializationFix();
  174. friend class CArtifactInstance;
  175. friend struct AssembledArtifact;
  176. template <typename Handler> void serialize(Handler &h, const int version)
  177. {
  178. h & static_cast<CArtifactInstance&>(*this);
  179. h & constituentsInfo;
  180. BONUS_TREE_DESERIALIZATION_FIX
  181. }
  182. };
  183. class DLL_LINKAGE CArtHandler //handles artifacts
  184. {
  185. void giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype = -1, int valType = Bonus::BASE_NUMBER, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalinfo = 0);
  186. void giveArtBonus(TArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator, int additionalinfo = 0);
  187. void giveArtBonus(TArtifactID aid, Bonus *bonus);
  188. public:
  189. std::vector<CArtifact*> treasures, minors, majors, relics;
  190. std::vector< ConstTransitivePtr<CArtifact> > artifacts;
  191. std::vector<CArtifact *> allowedArtifacts;
  192. std::set<ui32> bigArtifacts; // Artifacts that cannot be moved to backpack, e.g. war machines.
  193. std::set<ui32> growingArtifacts;
  194. void loadArtifacts(bool onlyTxt);
  195. /// load all artifacts from json structure
  196. void load(const JsonNode & node);
  197. /// load one artifact from json config
  198. CArtifact * loadArtifact(const JsonNode & node);
  199. ///read (optional) components of combined artifact
  200. void readComponents (const JsonNode & node, CArtifact * art);
  201. void sortArts();
  202. void addBonuses();
  203. void clear();
  204. void clearHlpLists();
  205. ui16 getRandomArt (int flags);
  206. ui16 getArtSync (ui32 rand, int flags);
  207. void getAllowedArts(std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, int flag);
  208. void getAllowed(std::vector<ConstTransitivePtr<CArtifact> > &out, int flags);
  209. void erasePickedArt (TArtifactInstanceID id);
  210. bool isBigArtifact (TArtifactID artID) const {return bigArtifacts.find(artID) != bigArtifacts.end();}
  211. void initAllowedArtifactsList(const std::vector<ui8> &allowed); //allowed[art_id] -> 0 if not allowed, 1 if allowed
  212. static int convertMachineID(int id, bool creToArt);
  213. void makeItCreatureArt (CArtifact * a, bool onlyCreature = true);
  214. void makeItCreatureArt (TArtifactInstanceID aid, bool onlyCreature = true);
  215. void makeItCommanderArt (CArtifact * a, bool onlyCommander = true);
  216. void makeItCommanderArt (TArtifactInstanceID aid, bool onlyCommander = true);
  217. CArtHandler();
  218. ~CArtHandler();
  219. /**
  220. * Gets a list of default allowed artifacts.
  221. *
  222. * @return a list of allowed artifacts, the index is the artifact id and the value either 0 for not allowed or 1 for allowed
  223. */
  224. std::vector<ui8> getDefaultAllowedArtifacts() const;
  225. template <typename Handler> void serialize(Handler &h, const int version)
  226. {
  227. h & artifacts & allowedArtifacts & treasures & minors & majors & relics
  228. & growingArtifacts;
  229. //if(!h.saving) sortArts();
  230. }
  231. };
  232. struct DLL_LINKAGE ArtSlotInfo
  233. {
  234. ConstTransitivePtr<CArtifactInstance> artifact;
  235. ui8 locked; //if locked, then artifact points to the combined artifact
  236. ArtSlotInfo()
  237. {
  238. locked = false;
  239. }
  240. template <typename Handler> void serialize(Handler &h, const int version)
  241. {
  242. h & artifact & locked;
  243. }
  244. };
  245. class DLL_LINKAGE CArtifactSet
  246. {
  247. public:
  248. std::vector<ArtSlotInfo> artifactsInBackpack; //hero's artifacts from bag
  249. bmap<ui16, 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
  250. ArtSlotInfo &retreiveNewArtSlot(ui16 slot);
  251. void setNewArtSlot(ui16 slot, CArtifactInstance *art, bool locked);
  252. void eraseArtSlot(ui16 slot);
  253. const ArtSlotInfo *getSlot(ui16 pos) const;
  254. const CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true) const; //NULL - no artifact
  255. CArtifactInstance* getArt(ui16 pos, bool excludeLocked = true); //NULL - no artifact
  256. si32 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)
  257. si32 getArtPos(const CArtifactInstance *art) const;
  258. const CArtifactInstance *getArtByInstanceId(TArtifactInstanceID artInstId) const;
  259. bool hasArt(ui32 aid, bool onlyWorn = false) const; //checks if hero possess artifact of given id (either in backack or worn)
  260. bool isPositionFree(ui16 pos, bool onlyLockCheck = false) const;
  261. si32 getArtTypeId(ui16 pos) const;
  262. virtual ui8 bearerType() const = 0;
  263. virtual ~CArtifactSet();
  264. template <typename Handler> void serialize(Handler &h, const int version)
  265. {
  266. h & artifactsInBackpack & artifactsWorn;
  267. }
  268. void artDeserializationFix(CBonusSystemNode *node);
  269. };