CCreatureSet.h 9.0 KB

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