CCreatureSet.h 7.3 KB

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