CCreatureSet.h 7.7 KB

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