CCreatureSet.h 10 KB

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