CCreatureSet.h 11 KB

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