CCreatureSet.h 6.7 KB

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