CCreatureSet.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #pragma once
  2. #include "HeroBonus.h"
  3. #include "GameConstants.h"
  4. #include "CArtHandler.h"
  5. /*
  6. * CCreatureSet.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class JsonNode;
  15. class CCreature;
  16. class CGHeroInstance;
  17. class CArmedInstance;
  18. class CCreatureArtifactSet;
  19. class DLL_LINKAGE CStackBasicDescriptor
  20. {
  21. public:
  22. const CCreature *type;
  23. TQuantity count;
  24. CStackBasicDescriptor();
  25. CStackBasicDescriptor(CreatureID id, TQuantity Count);
  26. CStackBasicDescriptor(const CCreature *c, TQuantity Count);
  27. template <typename Handler> void serialize(Handler &h, const int version)
  28. {
  29. h & type & count;
  30. }
  31. void writeJson(JsonNode & json) const;
  32. void readJson(const JsonNode & json);
  33. };
  34. class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet
  35. {
  36. protected:
  37. const CArmedInstance *_armyObj; //stack must be part of some army, army must be part of some object
  38. public:
  39. // hlp variable used during loading map, when object (hero or town) have creatures that must have same alignment.
  40. // idRand < 0 -> normal, non-random creature
  41. // idRand / 2 -> level
  42. // idRand % 2 -> upgrade number
  43. int idRand;
  44. const CArmedInstance * const & armyObj; //stack must be part of some army, army must be part of some object
  45. TExpType experience;//commander needs same amount of exp as hero
  46. template <typename Handler> void serialize(Handler &h, const int version)
  47. {
  48. h & static_cast<CBonusSystemNode&>(*this);
  49. h & static_cast<CStackBasicDescriptor&>(*this);
  50. h & static_cast<CArtifactSet&>(*this);
  51. h & _armyObj & experience;
  52. BONUS_TREE_DESERIALIZATION_FIX
  53. }
  54. void writeJson(JsonNode & json) const;
  55. void readJson(const JsonNode & json);
  56. //overrides CBonusSystemNode
  57. std::string bonusToString(const Bonus *bonus, bool description) const override; // how would bonus description look for this particular type of node
  58. std::string bonusToGraphics(const Bonus *bonus) const; //file name of graphics from StackSkills , in future possibly others
  59. virtual ui64 getPower() const;
  60. int getQuantityID() const;
  61. std::string getQuantityTXT(bool capitalized = true) const;
  62. virtual int getExpRank() const;
  63. virtual int getLevel() const; //different for regular stack and commander
  64. si32 magicResistance() const override;
  65. CreatureID getCreatureID() const; //-1 if not available
  66. std::string getName() const; //plural or singular
  67. virtual void init();
  68. CStackInstance();
  69. CStackInstance(CreatureID id, TQuantity count);
  70. CStackInstance(const CCreature *cre, TQuantity count);
  71. ~CStackInstance();
  72. void setType(CreatureID creID);
  73. void setType(const CCreature *c);
  74. void setArmyObj(const CArmedInstance *ArmyObj);
  75. virtual void giveStackExp(TExpType exp);
  76. bool valid(bool allowUnrandomized) const;
  77. ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
  78. virtual std::string nodeName() const override; //from CBonusSystemnode
  79. void deserializationFix();
  80. };
  81. class DLL_LINKAGE CCommanderInstance : public CStackInstance
  82. {
  83. public:
  84. //TODO: what if Commander is not a part of creature set?
  85. //commander class is determined by its base creature
  86. ui8 alive;
  87. ui8 level; //required only to count callbacks
  88. std::string name; // each Commander has different name
  89. std::vector <ui8> secondarySkills; //ID -> level
  90. std::set <ui8> specialSKills;
  91. //std::vector <CArtifactInstance *> arts;
  92. void init() override;
  93. CCommanderInstance();
  94. CCommanderInstance (CreatureID id);
  95. ~CCommanderInstance();
  96. void setAlive (bool alive);
  97. void giveStackExp (TExpType exp) override;
  98. void levelUp ();
  99. bool gainsLevel() const; //true if commander has lower level than should upon his experience
  100. ui64 getPower() const override {return 0;};
  101. int getExpRank() const override;
  102. int getLevel() const override;
  103. ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
  104. template <typename Handler> void serialize(Handler &h, const int version)
  105. {
  106. h & static_cast<CStackInstance&>(*this);
  107. h & alive & level & name & secondarySkills & specialSKills;
  108. }
  109. };
  110. DLL_LINKAGE std::ostream & operator<<(std::ostream & str, const CStackInstance & sth);
  111. typedef std::map<SlotID, CStackInstance*> TSlots;
  112. typedef std::map<SlotID, CStackBasicDescriptor> TSimpleSlots;
  113. class IArmyDescriptor
  114. {
  115. public:
  116. virtual void clear() = 0;
  117. virtual bool setCreature(SlotID slot, CreatureID cre, TQuantity count) = 0;
  118. };
  119. //simplified version of CCreatureSet
  120. class DLL_LINKAGE CSimpleArmy : public IArmyDescriptor
  121. {
  122. public:
  123. TSimpleSlots army;
  124. void clear() override;
  125. bool setCreature(SlotID slot, CreatureID cre, TQuantity count) override;
  126. operator bool() const;
  127. template <typename Handler> void serialize(Handler &h, const int version)
  128. {
  129. h & army;
  130. }
  131. };
  132. class DLL_LINKAGE CCreatureSet : public IArmyDescriptor //seven combined creatures
  133. {
  134. CCreatureSet(const CCreatureSet&);
  135. CCreatureSet &operator=(const CCreatureSet&);
  136. public:
  137. TSlots stacks; //slots[slot_id]->> pair(creature_id,creature_quantity)
  138. ui8 formation; //false - wide, true - tight
  139. CCreatureSet();
  140. virtual ~CCreatureSet();
  141. virtual void armyChanged();
  142. const CStackInstance &operator[](SlotID slot) const;
  143. const TSlots &Slots() const {return stacks;}
  144. void addToSlot(SlotID slot, CreatureID cre, TQuantity count, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  145. void addToSlot(SlotID slot, CStackInstance *stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  146. void clear() override;
  147. void setFormation(bool tight);
  148. CArmedInstance *castToArmyObj();
  149. //basic operations
  150. void putStack(SlotID slot, CStackInstance *stack); //adds new stack to the army, slot must be empty
  151. void setStackCount(SlotID slot, TQuantity count); //stack must exist!
  152. CStackInstance *detachStack(SlotID slot); //removes stack from army but doesn't destroy it (so it can be moved somewhere else or safely deleted)
  153. void setStackType(SlotID slot, const CCreature *type);
  154. void giveStackExp(TExpType exp);
  155. void setStackExp(SlotID slot, TExpType exp);
  156. //derivative
  157. void eraseStack(SlotID slot); //slot must be occupied
  158. void joinStack(SlotID slot, CStackInstance * stack); //adds new stack to the existing stack of the same type
  159. void changeStackCount(SlotID slot, TQuantity toAdd); //stack must exist!
  160. bool setCreature (SlotID slot, CreatureID type, TQuantity quantity) override; //replaces creature in stack; slots 0 to 6, if quantity=0 erases stack
  161. 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.
  162. const CStackInstance& getStack(SlotID slot) const; //stack must exist
  163. const CStackInstance* getStackPtr(SlotID slot) const; //if stack doesn't exist, returns nullptr
  164. const CCreature* getCreature(SlotID slot) const; //workaround of map issue;
  165. int getStackCount (SlotID slot) const;
  166. TExpType getStackExperience(SlotID slot) const;
  167. SlotID findStack(const CStackInstance *stack) const; //-1 if none
  168. SlotID getSlotFor(CreatureID creature, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  169. SlotID getSlotFor(const CCreature *c, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  170. SlotID getFreeSlot(ui32 slotsAmount = GameConstants::ARMY_SIZE) const;
  171. bool mergableStacks(std::pair<SlotID, SlotID> &out, SlotID preferable = SlotID()) const; //looks for two same stacks, returns slot positions;
  172. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  173. bool slotEmpty(SlotID slot) const;
  174. int stacksCount() const;
  175. virtual bool needsLastStack() const; //true if last stack cannot be taken
  176. ui64 getArmyStrength() const; //sum of AI values of creatures
  177. ui64 getPower (SlotID slot) const; //value of specific stack
  178. std::string getRoughAmount(SlotID slot, int mode = 0) const; //rough size of specific stack
  179. std::string getArmyDescription() const;
  180. bool hasStackAtSlot(SlotID slot) const;
  181. bool contains(const CStackInstance *stack) const;
  182. bool canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks = true) const;
  183. template <typename Handler> void serialize(Handler &h, const int version)
  184. {
  185. h & stacks & formation;
  186. }
  187. void writeJson(JsonNode & json) const;
  188. void readJson(const JsonNode & json);
  189. operator bool() const
  190. {
  191. return !stacks.empty();
  192. }
  193. void sweep();
  194. };