CCreatureSet.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * CCreatureSet.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "CSimpleArmy.h"
  12. #include "CStackInstance.h"
  13. #include "serializer/Serializeable.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CStackInstance;
  16. class CArmedInstance;
  17. class CStackBasicDescriptor;
  18. class JsonSerializeFormat;
  19. using TSlots = std::map<SlotID, std::unique_ptr<CStackInstance>>;
  20. using TPairCreatureSlot = std::pair<const CCreature *, SlotID>;
  21. using TMapCreatureSlot = std::map<const CCreature *, SlotID>;
  22. struct DLL_LINKAGE CreatureSlotComparer
  23. {
  24. bool operator()(const TPairCreatureSlot & lhs, const TPairCreatureSlot & rhs);
  25. };
  26. using TCreatureQueue = std::priority_queue<TPairCreatureSlot, std::vector<TPairCreatureSlot>, CreatureSlotComparer>;
  27. namespace NArmyFormation
  28. {
  29. static const std::vector<std::string> names{"wide", "tight"};
  30. }
  31. class DLL_LINKAGE CCreatureSet : public IArmyDescriptor, public virtual Serializeable, boost::noncopyable //seven combined creatures
  32. {
  33. CCreatureSet(const CCreatureSet &) = delete;
  34. CCreatureSet & operator=(const CCreatureSet &) = delete;
  35. public:
  36. TSlots stacks; //slots[slot_id]->> pair(creature_id,creature_quantity)
  37. EArmyFormation formation = EArmyFormation::LOOSE; //0 - wide, 1 - tight
  38. CCreatureSet() = default; //Should be here to avoid compile errors
  39. virtual ~CCreatureSet();
  40. virtual void armyChanged();
  41. const CStackInstance & operator[](const SlotID & slot) const;
  42. const TSlots &Slots() const {return stacks;}
  43. //Adds stack to slot. Slot must be empty or with same type creature
  44. void addToSlot(const SlotID & slot, const CreatureID & cre, TQuantity count, bool allowMerging = true);
  45. //Adds stack to slot. Slot must be empty or with same type creature
  46. void addToSlot(const SlotID & slot, std::unique_ptr<CStackInstance> stack, bool allowMerging = true);
  47. void clearSlots() override;
  48. void setFormation(EArmyFormation tight);
  49. virtual CArmedInstance * getArmy() { return nullptr; }
  50. virtual const CArmedInstance * getArmy() const { return nullptr; }
  51. //adds new stack to the army, slot must be empty
  52. void putStack(const SlotID & slot, std::unique_ptr<CStackInstance> stack);
  53. //stack must exist!
  54. void setStackCount(const SlotID & slot, TQuantity count);
  55. //removes stack from army but doesn't destroy it (so it can be moved somewhere else or safely deleted)
  56. std::unique_ptr<CStackInstance> detachStack(const SlotID & slot);
  57. void setStackType(const SlotID & slot, const CreatureID & type);
  58. /// Give specified amount of experience to all units in army
  59. /// Amount of granted experience is scaled by unit stack size
  60. void giveAverageStackExperience(TExpType exp);
  61. /// Give specified amount of experience to unit in specified slot
  62. /// Amount of granted experience is not scaled by unit stack size
  63. void giveTotalStackExperience(const SlotID & slot, TExpType exp);
  64. /// Erased stack from specified slot. Slot must be non-empty
  65. void eraseStack(const SlotID & slot);
  66. /// Joins stack into stack that occupies targeted slot.
  67. /// Slot must be non-empty and contain same creature type
  68. void joinStack(const SlotID & slot, std::unique_ptr<CStackInstance> stack); //adds new stack to the existing stack of the same type
  69. /// Splits off some units of specified stack and returns newly created stack
  70. /// Slot must be non-empty and contain more units that split quantity
  71. std::unique_ptr<CStackInstance> splitStack(const SlotID & slot, TQuantity toSplit);
  72. //stack must exist!
  73. void changeStackCount(const SlotID & slot, TQuantity toAdd);
  74. //replaces creature in stack; slots 0 to 6, if quantity=0 erases stack
  75. bool setCreature(SlotID slot, CreatureID type, TQuantity quantity) override;
  76. //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.
  77. void setToArmy(CSimpleArmy & src);
  78. const CStackInstance & getStack(const SlotID & slot) const; //stack must exist
  79. CStackInstance * getStackPtr(const SlotID & slot) const; //if stack doesn't exist, returns nullptr
  80. const CCreature * getCreature(const SlotID & slot) const; //workaround of map issue;
  81. int getStackCount(const SlotID & slot) const;
  82. TExpType getStackTotalExperience(const SlotID & slot) const;
  83. TExpType getStackAverageExperience(const SlotID & slot) const;
  84. SlotID findStack(const CStackInstance * stack) const; //-1 if none
  85. SlotID getSlotFor(const CreatureID & creature, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  86. SlotID getSlotFor(const CCreature * c, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  87. bool hasCreatureSlots(const CCreature * c, const SlotID & exclude) const;
  88. std::vector<SlotID> getCreatureSlots(const CCreature * c, const SlotID & exclude, TQuantity ignoreAmount = -1) const;
  89. bool isCreatureBalanced(const CCreature * c, TQuantity ignoreAmount = 1) const; // Check if the creature is evenly distributed across slots
  90. SlotID getFreeSlot(ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns first free slot
  91. std::vector<SlotID> getFreeSlots(ui32 slotsAmount = GameConstants::ARMY_SIZE) const;
  92. TMapCreatureSlot getCreatureMap() const;
  93. TCreatureQueue getCreatureQueue(const SlotID & exclude) const;
  94. bool mergeableStacks(std::pair<SlotID, SlotID> & out, const SlotID & preferable = SlotID()) const; //looks for two same stacks, returns slot positions;
  95. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  96. bool slotEmpty(const SlotID & slot) const;
  97. int stacksCount() const;
  98. virtual bool needsLastStack() const; //true if last stack cannot be taken
  99. ui64 getArmyStrength(int fortLevel = 0) const; //sum of AI values of creatures
  100. ui64 getArmyCost() const; //sum of cost of creatures
  101. ui64 getPower(const SlotID & slot) const; //value of specific stack
  102. std::string getRoughAmount(const SlotID & slot, int mode = 0) const; //rough size of specific stack
  103. std::string getArmyDescription() const;
  104. bool hasStackAtSlot(const SlotID & slot) const;
  105. bool contains(const CStackInstance * stack) const;
  106. bool canBeMergedWith(const CCreatureSet & cs, bool allowMergingStacks = true) const;
  107. /// Returns true if this creature set contains all listed units
  108. /// If requireLastStack is true, then this function will also
  109. /// require presence of any unit other than requested (or more units than requested)
  110. bool hasUnits(const std::vector<CStackBasicDescriptor> & units, bool requireLastStack = true) const;
  111. template<typename Handler>
  112. void serialize(Handler & h)
  113. {
  114. h & stacks;
  115. h & formation;
  116. }
  117. void serializeJson(JsonSerializeFormat & handler, const std::string & armyFieldName, const std::optional<int> fixedSize = std::nullopt);
  118. operator bool() const
  119. {
  120. return !stacks.empty();
  121. }
  122. };
  123. VCMI_LIB_NAMESPACE_END