CCreatureSet.h 5.9 KB

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