CCreatureSet.h 5.8 KB

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