CCreatureSet.h 5.2 KB

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