CCreatureSet.h 11 KB

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