CCreatureSet.h 6.3 KB

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