CCreatureSet.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef __CCREATURESET_H__
  2. #define __CCREATURESET_H__
  3. #include "../global.h"
  4. #include <map>
  5. #include "HeroBonus.h"
  6. class CCreature;
  7. class CGHeroInstance;
  8. class CArmedInstance;
  9. class DLL_EXPORT 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_EXPORT CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor
  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; //TODO: handle
  29. //TODO: stack artifacts
  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 & _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. int getQuantityID() const;
  42. std::string getQuantityTXT(bool capitalized = true) const;
  43. int getExpRank() const;
  44. si32 magicResistance() const;
  45. int getCreatureID() const; //-1 if not available
  46. void init();
  47. CStackInstance();
  48. CStackInstance(TCreature id, TQuantity count);
  49. CStackInstance(const CCreature *cre, TQuantity count);
  50. ~CStackInstance();
  51. void setType(int creID);
  52. void setType(const CCreature *c);
  53. void setArmyObj(const CArmedInstance *ArmyObj);
  54. void giveStackExp(expType exp);
  55. bool valid(bool allowUnrandomized) const;
  56. virtual std::string nodeName() const OVERRIDE;
  57. void deserializationFix();
  58. };
  59. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth);
  60. typedef std::map<TSlot, CStackInstance*> TSlots;
  61. typedef std::map<TSlot, CStackBasicDescriptor> TSimpleSlots;
  62. class IArmyDescriptor
  63. {
  64. public:
  65. virtual void clear() = 0;
  66. virtual bool setCreature(TSlot slot, TCreature cre, TQuantity count) = 0;
  67. };
  68. //simplified version of CCreatureSet
  69. class DLL_EXPORT CSimpleArmy : public IArmyDescriptor
  70. {
  71. public:
  72. TSimpleSlots army;
  73. void clear() OVERRIDE;
  74. bool setCreature(TSlot slot, TCreature cre, TQuantity count) OVERRIDE;
  75. operator bool() const;
  76. template <typename Handler> void serialize(Handler &h, const int version)
  77. {
  78. h & army;
  79. }
  80. };
  81. class DLL_EXPORT CCreatureSet : public IArmyDescriptor //seven combined creatures
  82. {
  83. CCreatureSet(const CCreatureSet&);;
  84. CCreatureSet &operator=(const CCreatureSet&);
  85. public:
  86. TSlots stacks; //slots[slot_id]->> pair(creature_id,creature_quantity)
  87. ui8 formation; //false - wide, true - tight
  88. CCreatureSet();
  89. virtual ~CCreatureSet();
  90. virtual void armyChanged();
  91. const CStackInstance &operator[](TSlot slot) const;
  92. const TSlots &Slots() const {return stacks;}
  93. void addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  94. void addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  95. void clear() OVERRIDE;
  96. void setFormation(bool tight);
  97. CArmedInstance *castToArmyObj();
  98. //basic operations
  99. void putStack(TSlot slot, CStackInstance *stack); //adds new stack to the army, slot must be empty
  100. void setStackCount(TSlot slot, TQuantity count); //stack must exist!
  101. CStackInstance *detachStack(TSlot slot); //removes stack from army but doesn't destroy it (so it can be moved somewhere else or safely deleted)
  102. void setStackType(TSlot slot, const CCreature *type);
  103. void giveStackExp(expType exp);
  104. void setStackExp(TSlot slot, expType exp);
  105. //derivative
  106. void eraseStack(TSlot slot); //slot must be occupied
  107. void joinStack(TSlot slot, CStackInstance * stack); //adds new stack to the existing stack of the same type
  108. void changeStackCount(TSlot slot, TQuantity toAdd); //stack must exist!
  109. bool setCreature (TSlot slot, TCreature type, TQuantity quantity) OVERRIDE; //replaces creature in stack; slots 0 to 6, if quantity=0 erases stack
  110. 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.
  111. const CStackInstance& getStack(TSlot slot) const; //stack must exist
  112. const CStackInstance* getStackPtr(TSlot slot) const; //if stack doesn't exist, returns NULL
  113. const CCreature* getCreature(TSlot slot) const; //workaround of map issue;
  114. int getStackCount (TSlot slot) const;
  115. expType getStackExperience(TSlot slot) const;
  116. TSlot findStack(const CStackInstance *stack) const; //-1 if none
  117. TSlot getSlotFor(TCreature creature, ui32 slotsAmount=ARMY_SIZE) const; //returns -1 if no slot available
  118. TSlot getSlotFor(const CCreature *c, ui32 slotsAmount=ARMY_SIZE) const; //returns -1 if no slot available
  119. TSlot getFreeSlot(ui32 slotsAmount=ARMY_SIZE) const;
  120. bool mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable = -1) const; //looks for two same stacks, returns slot positions;
  121. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  122. bool slotEmpty(TSlot slot) const;
  123. int stacksCount() const;
  124. virtual bool needsLastStack() const; //true if last stack cannot be taken
  125. ui64 getArmyStrength() const; //sum of AI values of creatures
  126. ui64 getPower (TSlot slot) const; //value of specific stack
  127. std::string getRoughAmount (TSlot slot) const; //rough size of specific stack
  128. bool hasStackAtSlot(TSlot slot) const;
  129. bool contains(const CStackInstance *stack) const;
  130. bool canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks = true) const;
  131. template <typename Handler> void serialize(Handler &h, const int version)
  132. {
  133. h & stacks & formation;
  134. }
  135. operator bool() const
  136. {
  137. return stacks.size() > 0;
  138. }
  139. void sweep();
  140. };
  141. #endif // __CCREATURESET_H__