CCreatureSet.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "bonuses/Bonus.h"
  12. #include "bonuses/BonusCache.h"
  13. #include "bonuses/CBonusSystemNode.h"
  14. #include "serializer/Serializeable.h"
  15. #include "GameConstants.h"
  16. #include "CArtHandler.h"
  17. #include "CArtifactInstance.h"
  18. #include "CCreatureHandler.h"
  19. #include "GameLibrary.h"
  20. #include <vcmi/Entity.h>
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class JsonNode;
  23. class CCreature;
  24. class CGHeroInstance;
  25. class CArmedInstance;
  26. class CCreatureArtifactSet;
  27. class JsonSerializeFormat;
  28. class DLL_LINKAGE CStackBasicDescriptor
  29. {
  30. CreatureID typeID;
  31. public:
  32. TQuantity count = -1; //exact quantity or quantity ID from CCreature::getQuantityID when getting info about enemy army
  33. CStackBasicDescriptor();
  34. CStackBasicDescriptor(const CreatureID & id, TQuantity Count);
  35. CStackBasicDescriptor(const CCreature *c, TQuantity Count);
  36. virtual ~CStackBasicDescriptor() = default;
  37. const Creature * getType() const;
  38. const CCreature * getCreature() const;
  39. CreatureID getId() const;
  40. TQuantity getCount() const;
  41. virtual void setType(const CCreature * c);
  42. friend bool operator== (const CStackBasicDescriptor & l, const CStackBasicDescriptor & r);
  43. template <typename Handler> void serialize(Handler &h)
  44. {
  45. if(h.saving)
  46. {
  47. h & typeID;
  48. }
  49. else
  50. {
  51. CreatureID creatureID;
  52. h & creatureID;
  53. if(creatureID != CreatureID::NONE)
  54. setType(creatureID.toCreature());
  55. }
  56. h & count;
  57. }
  58. void serializeJson(JsonSerializeFormat & handler);
  59. };
  60. class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet, public ACreature
  61. {
  62. BonusValueCache nativeTerrain;
  63. BonusValueCache initiative;
  64. protected:
  65. const CArmedInstance *_armyObj; //stack must be part of some army, army must be part of some object
  66. public:
  67. struct RandomStackInfo
  68. {
  69. uint8_t level;
  70. uint8_t upgrade;
  71. };
  72. // helper variable used during loading map, when object (hero or town) have creatures that must have same alignment.
  73. std::optional<RandomStackInfo> randomStack;
  74. const CArmedInstance * const & armyObj; //stack must be part of some army, army must be part of some object
  75. TExpType experience;//commander needs same amount of exp as hero
  76. template <typename Handler> void serialize(Handler &h)
  77. {
  78. h & static_cast<CBonusSystemNode&>(*this);
  79. h & static_cast<CStackBasicDescriptor&>(*this);
  80. h & static_cast<CArtifactSet&>(*this);
  81. h & _armyObj;
  82. h & experience;
  83. if(!h.saving)
  84. deserializationFix();
  85. }
  86. void serializeJson(JsonSerializeFormat & handler);
  87. //overrides CBonusSystemNode
  88. std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const override; // how would bonus description look for this particular type of node
  89. ImagePath bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const; //file name of graphics from StackSkills , in future possibly others
  90. //IConstBonusProvider
  91. const IBonusBearer* getBonusBearer() const override;
  92. //INativeTerrainProvider
  93. FactionID getFactionID() const override;
  94. virtual ui64 getPower() const;
  95. /// Returns total market value of resources needed to recruit this unit
  96. virtual ui64 getMarketValue() const;
  97. CCreature::CreatureQuantityId getQuantityID() const;
  98. std::string getQuantityTXT(bool capitalized = true) const;
  99. virtual int getExpRank() const;
  100. virtual int getLevel() const; //different for regular stack and commander
  101. CreatureID getCreatureID() const; //-1 if not available
  102. std::string getName() const; //plural or singular
  103. CStackInstance(bool isHypothetic = false);
  104. CStackInstance(const CreatureID & id, TQuantity count, bool isHypothetic = false);
  105. CStackInstance(const CCreature *cre, TQuantity count, bool isHypothetic = false);
  106. virtual ~CStackInstance() = default;
  107. void setType(const CreatureID & creID);
  108. void setType(const CCreature * c) final;
  109. void setArmyObj(const CArmedInstance *ArmyObj);
  110. virtual void giveStackExp(TExpType exp);
  111. bool valid(bool allowUnrandomized) const;
  112. ArtPlacementMap putArtifact(const ArtifactPosition & pos, CArtifactInstance * art) override;//from CArtifactSet
  113. void removeArtifact(const ArtifactPosition & pos) override;
  114. ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
  115. std::string nodeName() const override; //from CBonusSystemnode
  116. void deserializationFix();
  117. PlayerColor getOwner() const override;
  118. int32_t getInitiative(int turn = 0) const final;
  119. TerrainId getNativeTerrain() const final;
  120. TerrainId getCurrentTerrain() const;
  121. };
  122. class DLL_LINKAGE CCommanderInstance : public CStackInstance
  123. {
  124. public:
  125. //TODO: what if Commander is not a part of creature set?
  126. //commander class is determined by its base creature
  127. ui8 alive; //maybe change to bool when breaking save compatibility?
  128. ui8 level; //required only to count callbacks
  129. std::string name; // each Commander has different name
  130. std::vector <ui8> secondarySkills; //ID -> level
  131. std::set <ui8> specialSkills;
  132. //std::vector <CArtifactInstance *> arts;
  133. CCommanderInstance();
  134. CCommanderInstance(const CreatureID & id);
  135. void setAlive (bool alive);
  136. void giveStackExp (TExpType exp) override;
  137. void levelUp ();
  138. bool gainsLevel() const; //true if commander has lower level than should upon his experience
  139. ui64 getPower() const override {return 0;};
  140. int getExpRank() const override;
  141. int getLevel() const override;
  142. ArtBearer::ArtBearer bearerType() const override; //from CArtifactSet
  143. template <typename Handler> void serialize(Handler &h)
  144. {
  145. h & static_cast<CStackInstance&>(*this);
  146. h & alive;
  147. h & level;
  148. h & name;
  149. h & secondarySkills;
  150. h & specialSkills;
  151. }
  152. };
  153. using TSlots = std::map<SlotID, CStackInstance *>;
  154. using TSimpleSlots = std::map<SlotID, std::pair<CreatureID, TQuantity>>;
  155. using TPairCreatureSlot = std::pair<const CCreature *, SlotID>;
  156. using TMapCreatureSlot = std::map<const CCreature *, SlotID>;
  157. struct DLL_LINKAGE CreatureSlotComparer
  158. {
  159. bool operator()(const TPairCreatureSlot & lhs, const TPairCreatureSlot & rhs);
  160. };
  161. using TCreatureQueue = std::priority_queue<TPairCreatureSlot, std::vector<TPairCreatureSlot>, CreatureSlotComparer>;
  162. class IArmyDescriptor
  163. {
  164. public:
  165. virtual void clearSlots() = 0;
  166. virtual bool setCreature(SlotID slot, CreatureID cre, TQuantity count) = 0;
  167. };
  168. //simplified version of CCreatureSet
  169. class DLL_LINKAGE CSimpleArmy : public IArmyDescriptor
  170. {
  171. public:
  172. TSimpleSlots army;
  173. void clearSlots() override;
  174. bool setCreature(SlotID slot, CreatureID cre, TQuantity count) override;
  175. operator bool() const;
  176. template <typename Handler> void serialize(Handler &h)
  177. {
  178. h & army;
  179. }
  180. };
  181. namespace NArmyFormation
  182. {
  183. static const std::vector<std::string> names{ "wide", "tight" };
  184. }
  185. class DLL_LINKAGE CCreatureSet : public IArmyDescriptor, public virtual Serializeable //seven combined creatures
  186. {
  187. CCreatureSet(const CCreatureSet &) = delete;
  188. CCreatureSet &operator=(const CCreatureSet&);
  189. public:
  190. TSlots stacks; //slots[slot_id]->> pair(creature_id,creature_quantity)
  191. EArmyFormation formation = EArmyFormation::LOOSE; //0 - wide, 1 - tight
  192. CCreatureSet() = default; //Should be here to avoid compile errors
  193. virtual ~CCreatureSet();
  194. virtual void armyChanged();
  195. const CStackInstance & operator[](const SlotID & slot) const;
  196. const TSlots &Slots() const {return stacks;}
  197. 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
  198. void addToSlot(const SlotID & slot, CStackInstance * stack, bool allowMerging = true); //Adds stack to slot. Slot must be empty or with same type creature
  199. void clearSlots() override;
  200. void setFormation(EArmyFormation tight);
  201. CArmedInstance *castToArmyObj();
  202. //basic operations
  203. void putStack(const SlotID & slot, CStackInstance * stack); //adds new stack to the army, slot must be empty
  204. void setStackCount(const SlotID & slot, TQuantity count); //stack must exist!
  205. CStackInstance * detachStack(const SlotID & slot); //removes stack from army but doesn't destroy it (so it can be moved somewhere else or safely deleted)
  206. void setStackType(const SlotID & slot, const CreatureID & type);
  207. void giveStackExp(TExpType exp);
  208. void setStackExp(const SlotID & slot, TExpType exp);
  209. //derivative
  210. void eraseStack(const SlotID & slot); //slot must be occupied
  211. void joinStack(const SlotID & slot, CStackInstance * stack); //adds new stack to the existing stack of the same type
  212. void changeStackCount(const SlotID & slot, TQuantity toAdd); //stack must exist!
  213. bool setCreature (SlotID slot, CreatureID type, TQuantity quantity) override; //replaces creature in stack; slots 0 to 6, if quantity=0 erases stack
  214. 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.
  215. const CStackInstance & getStack(const SlotID & slot) const; //stack must exist
  216. CStackInstance * getStackPtr(const SlotID & slot) const; //if stack doesn't exist, returns nullptr
  217. const CCreature * getCreature(const SlotID & slot) const; //workaround of map issue;
  218. int getStackCount(const SlotID & slot) const;
  219. TExpType getStackExperience(const SlotID & slot) const;
  220. SlotID findStack(const CStackInstance *stack) const; //-1 if none
  221. SlotID getSlotFor(const CreatureID & creature, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  222. SlotID getSlotFor(const CCreature *c, ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns -1 if no slot available
  223. bool hasCreatureSlots(const CCreature * c, const SlotID & exclude) const;
  224. std::vector<SlotID> getCreatureSlots(const CCreature * c, const SlotID & exclude, TQuantity ignoreAmount = -1) const;
  225. bool isCreatureBalanced(const CCreature* c, TQuantity ignoreAmount = 1) const; // Check if the creature is evenly distributed across slots
  226. SlotID getFreeSlot(ui32 slotsAmount = GameConstants::ARMY_SIZE) const; //returns first free slot
  227. std::vector<SlotID> getFreeSlots(ui32 slotsAmount = GameConstants::ARMY_SIZE) const;
  228. std::queue<SlotID> getFreeSlotsQueue(ui32 slotsAmount = GameConstants::ARMY_SIZE) const;
  229. TMapCreatureSlot getCreatureMap() const;
  230. TCreatureQueue getCreatureQueue(const SlotID & exclude) const;
  231. bool mergeableStacks(std::pair<SlotID, SlotID> & out, const SlotID & preferable = SlotID()) const; //looks for two same stacks, returns slot positions;
  232. bool validTypes(bool allowUnrandomized = false) const; //checks if all types of creatures are set properly
  233. bool slotEmpty(const SlotID & slot) const;
  234. int stacksCount() const;
  235. virtual bool needsLastStack() const; //true if last stack cannot be taken
  236. ui64 getArmyStrength(int fortLevel = 0) const; //sum of AI values of creatures
  237. ui64 getArmyCost() const; //sum of cost of creatures
  238. ui64 getPower(const SlotID & slot) const; //value of specific stack
  239. std::string getRoughAmount(const SlotID & slot, int mode = 0) const; //rough size of specific stack
  240. std::string getArmyDescription() const;
  241. bool hasStackAtSlot(const SlotID & slot) const;
  242. bool contains(const CStackInstance *stack) const;
  243. bool canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks = true) const;
  244. template <typename Handler> void serialize(Handler &h)
  245. {
  246. h & stacks;
  247. h & formation;
  248. }
  249. void serializeJson(JsonSerializeFormat & handler, const std::string & armyFieldName, const std::optional<int> fixedSize = std::nullopt);
  250. operator bool() const
  251. {
  252. return !stacks.empty();
  253. }
  254. void sweep();
  255. };
  256. VCMI_LIB_NAMESPACE_END