CStackInstance.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * CStackInstance.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 "CStackBasicDescriptor.h"
  12. #include "CCreatureHandler.h"
  13. #include "bonuses/BonusCache.h"
  14. #include "bonuses/CBonusSystemNode.h"
  15. #include "callback/GameCallbackHolder.h"
  16. #include "mapObjects/CGObjectInstance.h"
  17. #include "entities/artifact/CArtifactSet.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class JsonNode;
  20. class CCreature;
  21. class CGHeroInstance;
  22. class CArmedInstance;
  23. class CCreatureArtifactSet;
  24. class JsonSerializeFormat;
  25. class DLL_LINKAGE CStackInstance : public CBonusSystemNode, public CStackBasicDescriptor, public CArtifactSet, public ACreature, public GameCallbackHolder
  26. {
  27. BonusValueCache nativeTerrain;
  28. BonusValueCache initiative;
  29. CArmedInstance * armyInstance = nullptr; //stack must be part of some army, army must be part of some object
  30. IGameInfoCallback * getCallback() const final { return cb; }
  31. TExpType totalExperience;//commander needs same amount of exp as hero
  32. public:
  33. struct RandomStackInfo
  34. {
  35. uint8_t level;
  36. uint8_t upgrade;
  37. };
  38. // helper variable used during loading map, when object (hero or town) have creatures that must have same alignment.
  39. std::optional<RandomStackInfo> randomStack;
  40. CArmedInstance * getArmy();
  41. const CArmedInstance * getArmy() const; //stack must be part of some army, army must be part of some object
  42. void setArmy(CArmedInstance *ArmyObj);
  43. TExpType getTotalExperience() const;
  44. TExpType getAverageExperience() const;
  45. virtual bool canGainExperience() const;
  46. template <typename Handler> void serialize(Handler &h)
  47. {
  48. h & static_cast<CBonusSystemNode&>(*this);
  49. h & static_cast<CStackBasicDescriptor&>(*this);
  50. h & static_cast<CArtifactSet&>(*this);
  51. if (h.hasFeature(Handler::Version::STACK_INSTANCE_ARMY_FIX))
  52. {
  53. // no-op
  54. }
  55. if (h.hasFeature(Handler::Version::NO_RAW_POINTERS_IN_SERIALIZER))
  56. {
  57. ObjectInstanceID dummyID;
  58. h & dummyID;
  59. }
  60. else
  61. {
  62. std::shared_ptr<CGObjectInstance> army;
  63. h & army;
  64. }
  65. h & totalExperience;
  66. if (!h.hasFeature(Handler::Version::STACK_INSTANCE_EXPERIENCE_FIX))
  67. {
  68. totalExperience *= getCount();
  69. }
  70. }
  71. void serializeJson(JsonSerializeFormat & handler);
  72. //overrides CBonusSystemNode
  73. std::string bonusToString(const std::shared_ptr<Bonus>& bonus) const override; // how would bonus description look for this particular type of node
  74. ImagePath bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const; //file name of graphics from StackSkills , in future possibly others
  75. //IConstBonusProvider
  76. const IBonusBearer* getBonusBearer() const override;
  77. //INativeTerrainProvider
  78. FactionID getFactionID() const override;
  79. virtual ui64 getPower() const;
  80. /// Returns total market value of resources needed to recruit this unit
  81. virtual ui64 getMarketValue() const;
  82. CCreature::CreatureQuantityId getQuantityID() const;
  83. std::string getQuantityTXT(bool capitalized = true) const;
  84. virtual int getExpRank() const;
  85. virtual int getLevel() const; //different for regular stack and commander
  86. CreatureID getCreatureID() const; //-1 if not available
  87. std::string getName() const; //plural or singular
  88. CStackInstance(IGameInfoCallback *cb);
  89. CStackInstance(IGameInfoCallback *cb, BonusNodeType nodeType, bool isHypothetic = false);
  90. CStackInstance(IGameInfoCallback *cb, const CreatureID & id, TQuantity count, bool isHypothetic = false);
  91. virtual ~CStackInstance() = default;
  92. void setType(const CreatureID & creID);
  93. void setType(const CCreature * c) final;
  94. void setCount(TQuantity amount) final;
  95. /// Gives specified amount of stack experience that will not be scaled by unit size
  96. void giveAverageStackExperience(TExpType exp);
  97. void giveTotalStackExperience(TExpType exp);
  98. bool valid(bool allowUnrandomized) const;
  99. ArtPlacementMap putArtifact(const ArtifactPosition & pos, const CArtifactInstance * art) override;//from CArtifactSet
  100. void removeArtifact(const ArtifactPosition & pos) override;
  101. ArtBearer bearerType() const override; //from CArtifactSet
  102. std::string nodeName() const override; //from CBonusSystemnode
  103. PlayerColor getOwner() const override;
  104. int32_t getInitiative(int turn = 0) const final;
  105. TerrainId getNativeTerrain() const final;
  106. TerrainId getCurrentTerrain() const;
  107. };
  108. VCMI_LIB_NAMESPACE_END