CCreatureSet.h 7.9 KB

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