CCreatureSet.h 4.8 KB

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