CCreatureSet.h 8.3 KB

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