CCreatureSet.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. //a few typedefs for CCreatureSet
  10. typedef si32 TSlot;
  11. typedef si32 TQuantity;
  12. typedef ui32 TCreature; //creature id
  13. const int ARMY_SIZE = 7;
  14. class DLL_EXPORT CStackBasicDescriptor
  15. {
  16. public:
  17. const CCreature *type;
  18. TQuantity count;
  19. CStackBasicDescriptor();
  20. CStackBasicDescriptor(TCreature id, TQuantity Count);
  21. CStackBasicDescriptor(const CCreature *c, TQuantity Count);
  22. template <typename Handler> void serialize(Handler &h, const int version)
  23. {
  24. h & type & count;
  25. }
  26. };
  27. class DLL_EXPORT CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor
  28. {
  29. const CArmedInstance *_armyObj; //stack must be part of some army, army must be part of some object
  30. public:
  31. int idRand; //hlp variable used during loading game -> "id" placeholder for randomization
  32. const CArmedInstance * const & armyObj; //stack must be part of some army, army must be part of some object
  33. ui32 experience; //TODO: handle
  34. //TODO: stack artifacts
  35. template <typename Handler> void serialize(Handler &h, const int version)
  36. {
  37. h & static_cast<CBonusSystemNode&>(*this);
  38. h & static_cast<CStackBasicDescriptor&>(*this);
  39. h & _armyObj & experience;
  40. }
  41. //overrides CBonusSystemNode
  42. //void getParents(TCNodes &out, const CBonusSystemNode *source = NULL) const; //retrieves list of parent nodes (nodes to inherit bonuses from), source is the prinary asker
  43. int getQuantityID() const;
  44. std::string getQuantityTXT(bool capitalized = true) const;
  45. void init();
  46. CStackInstance();
  47. CStackInstance(TCreature id, TQuantity count);
  48. CStackInstance(const CCreature *cre, TQuantity count);
  49. ~CStackInstance();
  50. void setType(int creID);
  51. void setType(const CCreature *c);
  52. void setArmyObj(const CArmedInstance *ArmyObj);
  53. bool valid(bool allowUnrandomized) const;
  54. virtual std::string nodeName() const OVERRIDE;
  55. };
  56. DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth);
  57. typedef std::map<TSlot, CStackInstance*> TSlots;
  58. class DLL_EXPORT CCreatureSet //seven combined creatures
  59. {
  60. CCreatureSet(const CCreatureSet&);;
  61. CCreatureSet &operator=(const CCreatureSet&);
  62. public:
  63. TSlots slots; //slots[slot_id]->> pair(creature_id,creature_quantity)
  64. ui8 formation; //false - wide, true - tight
  65. CCreatureSet();
  66. virtual ~CCreatureSet();
  67. const CStackInstance &operator[](TSlot slot) const;
  68. const TSlots &Slots() const {return slots;}
  69. void addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  70. void addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  71. void clear();
  72. void setFormation(bool tight);
  73. CArmedInstance *castToArmyObj();
  74. //basic operations
  75. void eraseStack(TSlot slot); //slot must be occupied
  76. void putStack(TSlot slot, CStackInstance *stack); //adds new stack to the army, slot must be empty
  77. void joinStack(TSlot slot, CStackInstance * stack); //adds new stack to the existing stack of the same type
  78. void setStackCount(TSlot slot, TQuantity count); //stack must exist!
  79. CStackInstance *detachStack(TSlot slot); //removes stack from army but doesn't destroy it (so it can be moved somewhere else)
  80. void setStackType(TSlot slot, const CCreature *type);
  81. //derivative
  82. void changeStackCount(TSlot slot, TQuantity toAdd); //stack must exist!
  83. bool setCreature (TSlot slot, TCreature type, TQuantity quantity); //replaces creature in stack; slots 0 to 6, if quantity=0 erases stack
  84. void setToArmy(CCreatureSet &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.
  85. const CStackInstance& getStack(TSlot slot) const;
  86. const CCreature* getCreature(TSlot slot) const; //workaround of map issue;
  87. int getStackCount (TSlot slot) const;
  88. TSlot findStack(const CStackInstance *stack) const; //-1 if none
  89. TSlot getSlotFor(TCreature creature, ui32 slotsAmount=ARMY_SIZE) const; //returns -1 if no slot available
  90. TSlot getSlotFor(const CCreature *c, ui32 slotsAmount=ARMY_SIZE) const; //returns -1 if no slot available
  91. bool mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable = -1) const; //looks for two same stacks, returns slot positions;
  92. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  93. bool slotEmpty(TSlot slot) const;
  94. int stacksCount() const;
  95. virtual bool needsLastStack() const; //true if last stack cannot be taken
  96. int getArmyStrength() const; //sum of AI values of creatures
  97. ui64 getPower (TSlot slot) const; //value of specific stack
  98. std::string getRoughAmount (TSlot slot) const; //rough size of specific stack
  99. bool hasStackAtSlot(TSlot slot) const;
  100. bool contains(const CStackInstance *stack) const;
  101. bool canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks = true) const;
  102. template <typename Handler> void serialize(Handler &h, const int version)
  103. {
  104. h & slots & formation;
  105. }
  106. operator bool() const
  107. {
  108. return slots.size() > 0;
  109. }
  110. void sweep();
  111. };
  112. #endif // __CCREATURESET_H__