CCreatureSet.h 7.6 KB

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