CCreatureSet.h 12 KB

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