CCreatureSet.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. #include "HeroBonus.h"
  3. #include "GameConstants.h"
  4. #include "CArtHandler.h"
  5. /*
  6. * CCreatureSet.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 JsonNode;
  15. class CCreature;
  16. class CGHeroInstance;
  17. class CArmedInstance;
  18. class CCreatureArtifactSet;
  19. class JsonSerializeFormat;
  20. class DLL_LINKAGE CStackBasicDescriptor
  21. {
  22. public:
  23. const CCreature *type;
  24. TQuantity count;
  25. CStackBasicDescriptor();
  26. CStackBasicDescriptor(CreatureID id, TQuantity Count);
  27. CStackBasicDescriptor(const CCreature *c, TQuantity Count);
  28. virtual ~CStackBasicDescriptor() = default;
  29. template <typename Handler> void serialize(Handler &h, const int version)
  30. {
  31. h & type & count;
  32. }
  33. void writeJson(JsonNode & json) const;
  34. void readJson(const JsonNode & json);
  35. };
  36. class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet
  37. {
  38. protected:
  39. const CArmedInstance *_armyObj; //stack must be part of some army, army must be part of some object
  40. public:
  41. // hlp variable used during loading map, when object (hero or town) have creatures that must have same alignment.
  42. // idRand < 0 -> normal, non-random creature
  43. // idRand / 2 -> level
  44. // idRand % 2 -> upgrade number
  45. int idRand;
  46. const CArmedInstance * const & armyObj; //stack must be part of some army, army must be part of some object
  47. TExpType experience;//commander needs same amount of exp as hero
  48. template <typename Handler> void serialize(Handler &h, const int version)
  49. {
  50. h & static_cast<CBonusSystemNode&>(*this);
  51. h & static_cast<CStackBasicDescriptor&>(*this);
  52. h & static_cast<CArtifactSet&>(*this);
  53. h & _armyObj & experience;
  54. BONUS_TREE_DESERIALIZATION_FIX
  55. }
  56. void writeJson(JsonNode & json) const;
  57. void readJson(const JsonNode & json);
  58. //overrides CBonusSystemNode
  59. std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const override; // how would bonus description look for this particular type of node
  60. std::string bonusToGraphics(const std::shared_ptr<Bonus>& bonus) const; //file name of graphics from StackSkills , in future possibly others
  61. virtual ui64 getPower() const;
  62. int getQuantityID() const;
  63. std::string getQuantityTXT(bool capitalized = true) const;
  64. virtual int getExpRank() const;
  65. virtual int getLevel() const; //different for regular stack and commander
  66. si32 magicResistance() const override;
  67. CreatureID getCreatureID() const; //-1 if not available
  68. std::string getName() const; //plural or singular
  69. virtual void init();
  70. CStackInstance();
  71. CStackInstance(CreatureID id, TQuantity count);
  72. CStackInstance(const CCreature *cre, TQuantity count);
  73. virtual ~CStackInstance();
  74. void setType(CreatureID creID);
  75. void setType(const CCreature *c);
  76. void setArmyObj(const CArmedInstance *ArmyObj);
  77. virtual void giveStackExp(TExpType exp);
  78. bool valid(bool allowUnrandomized) const;
  79. ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
  80. virtual std::string nodeName() const override; //from CBonusSystemnode
  81. void deserializationFix();
  82. };
  83. class DLL_LINKAGE CCommanderInstance : public CStackInstance
  84. {
  85. public:
  86. //TODO: what if Commander is not a part of creature set?
  87. //commander class is determined by its base creature
  88. ui8 alive; //maybe change to bool when breaking save compatibility?
  89. ui8 level; //required only to count callbacks
  90. std::string name; // each Commander has different name
  91. std::vector <ui8> secondarySkills; //ID -> level
  92. std::set <ui8> specialSKills;
  93. //std::vector <CArtifactInstance *> arts;
  94. void init() override;
  95. CCommanderInstance();
  96. CCommanderInstance (CreatureID id);
  97. virtual ~CCommanderInstance();
  98. void setAlive (bool alive);
  99. void giveStackExp (TExpType exp) override;
  100. void levelUp ();
  101. bool gainsLevel() const; //true if commander has lower level than should upon his experience
  102. ui64 getPower() const override {return 0;};
  103. int getExpRank() const override;
  104. int getLevel() const override;
  105. ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
  106. template <typename Handler> void serialize(Handler &h, const int version)
  107. {
  108. h & static_cast<CStackInstance&>(*this);
  109. h & alive & level & name & secondarySkills & specialSKills;
  110. }
  111. };
  112. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth);
  113. typedef std::map<SlotID, CStackInstance*> TSlots;
  114. typedef std::map<SlotID, CStackBasicDescriptor> TSimpleSlots;
  115. class IArmyDescriptor
  116. {
  117. public:
  118. virtual void clear() = 0;
  119. virtual bool setCreature(SlotID slot, CreatureID cre, TQuantity count) = 0;
  120. };
  121. //simplified version of CCreatureSet
  122. class DLL_LINKAGE CSimpleArmy : public IArmyDescriptor
  123. {
  124. public:
  125. TSimpleSlots army;
  126. void clear() override;
  127. bool setCreature(SlotID slot, CreatureID cre, TQuantity count) override;
  128. operator bool() const;
  129. template <typename Handler> void serialize(Handler &h, const int version)
  130. {
  131. h & army;
  132. }
  133. };
  134. class DLL_LINKAGE CCreatureSet : public IArmyDescriptor //seven combined creatures
  135. {
  136. CCreatureSet(const CCreatureSet&);
  137. CCreatureSet &operator=(const CCreatureSet&);
  138. public:
  139. TSlots stacks; //slots[slot_id]->> pair(creature_id,creature_quantity)
  140. ui8 formation; //false - wide, true - tight
  141. CCreatureSet();
  142. virtual ~CCreatureSet();
  143. virtual void armyChanged();
  144. const CStackInstance &operator[](SlotID slot) const;
  145. const TSlots &Slots() const {return stacks;}
  146. void addToSlot(SlotID slot, CreatureID cre, TQuantity count, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  147. void addToSlot(SlotID slot, CStackInstance *stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  148. void clear() override;
  149. void setFormation(bool tight);
  150. CArmedInstance *castToArmyObj();
  151. //basic operations
  152. void putStack(SlotID slot, CStackInstance *stack); //adds new stack to the army, slot must be empty
  153. void setStackCount(SlotID slot, TQuantity count); //stack must exist!
  154. CStackInstance *detachStack(SlotID slot); //removes stack from army but doesn't destroy it (so it can be moved somewhere else or safely deleted)
  155. void setStackType(SlotID slot, const CCreature *type);
  156. void giveStackExp(TExpType exp);
  157. void setStackExp(SlotID slot, TExpType exp);
  158. //derivative
  159. void eraseStack(SlotID slot); //slot must be occupied
  160. void joinStack(SlotID slot, CStackInstance * stack); //adds new stack to the existing stack of the same type
  161. void changeStackCount(SlotID slot, TQuantity toAdd); //stack must exist!
  162. bool setCreature (SlotID slot, CreatureID type, TQuantity quantity) override; //replaces creature in stack; slots 0 to 6, if quantity=0 erases stack
  163. void setToArmy(CSimpleArmy &src); //erases all our army and moves stacks from src to us; src MUST NOT be an armed object! WARNING: use it wisely. Or better do not use at all.
  164. const CStackInstance& getStack(SlotID slot) const; //stack must exist
  165. const CStackInstance* getStackPtr(SlotID slot) const; //if stack doesn't exist, returns nullptr
  166. const CCreature* getCreature(SlotID slot) const; //workaround of map issue;
  167. int getStackCount (SlotID slot) const;
  168. TExpType getStackExperience(SlotID slot) const;
  169. SlotID findStack(const CStackInstance *stack) const; //-1 if none
  170. SlotID getSlotFor(CreatureID creature, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  171. SlotID getSlotFor(const CCreature *c, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  172. SlotID getFreeSlot(ui32 slotsAmount = GameConstants::ARMY_SIZE) const;
  173. bool mergableStacks(std::pair<SlotID, SlotID> &out, SlotID preferable = SlotID()) const; //looks for two same stacks, returns slot positions;
  174. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  175. bool slotEmpty(SlotID slot) const;
  176. int stacksCount() const;
  177. virtual bool needsLastStack() const; //true if last stack cannot be taken
  178. ui64 getArmyStrength() const; //sum of AI values of creatures
  179. ui64 getPower (SlotID slot) const; //value of specific stack
  180. std::string getRoughAmount(SlotID slot, int mode = 0) const; //rough size of specific stack
  181. std::string getArmyDescription() const;
  182. bool hasStackAtSlot(SlotID slot) const;
  183. bool contains(const CStackInstance *stack) const;
  184. bool canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks = true) const;
  185. template <typename Handler> void serialize(Handler &h, const int version)
  186. {
  187. h & stacks & formation;
  188. }
  189. void serializeJson(JsonSerializeFormat & handler, const std::string & fieldName);
  190. operator bool() const
  191. {
  192. return !stacks.empty();
  193. }
  194. void sweep();
  195. };