CCreatureSet.h 11 KB

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