CCreatureSet.h 11 KB

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