CCreatureSet.h 6.8 KB

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