CCreatureSet.h 8.1 KB

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