CCreatureSet.h 10 KB

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