CCreatureSet.h 6.0 KB

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