CGHeroInstance.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * CGHeroInstance.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 <vcmi/spells/Caster.h>
  12. #include "CObjectHandler.h"
  13. #include "CArmedInstance.h"
  14. #include "../CArtHandler.h" // For CArtifactSet
  15. #include "../CRandomGenerator.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. class CHero;
  18. class CGBoat;
  19. class CGTownInstance;
  20. class CMap;
  21. struct TerrainTile;
  22. struct TurnInfo;
  23. class CGHeroPlaceholder : public CGObjectInstance
  24. {
  25. public:
  26. //subID stores id of hero type. If it's 0xff then following field is used
  27. ui8 power;
  28. template <typename Handler> void serialize(Handler &h, const int version)
  29. {
  30. h & static_cast<CGObjectInstance&>(*this);
  31. h & power;
  32. }
  33. };
  34. class DLL_LINKAGE CGHeroInstance : public CArmedInstance, public IBoatGenerator, public CArtifactSet, public spells::Caster
  35. {
  36. // We serialize heroes into JSON for crossover
  37. friend class CCampaignState;
  38. friend class CMapLoaderH3M;
  39. using CArmedInstance::getPosition; //FIXME: recheck: overloaded-virtual
  40. private:
  41. std::set<SpellID> spells; //known spells (spell IDs)
  42. public:
  43. //////////////////////////////////////////////////////////////////////////
  44. ui8 moveDir; //format: 123
  45. // 8 4
  46. // 765
  47. mutable ui8 isStanding, tacticFormationEnabled;
  48. //////////////////////////////////////////////////////////////////////////
  49. ConstTransitivePtr<CHero> type;
  50. TExpType exp; //experience points
  51. ui32 level; //current level of hero
  52. std::string name; //may be custom
  53. std::string biography; //if custom
  54. si32 portrait; //may be custom
  55. si32 mana; // remaining spell points
  56. std::vector<std::pair<SecondarySkill,ui8> > secSkills; //first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert); if hero has ability (-1, -1) it meansthat it should have default secondary abilities
  57. ui32 movement; //remaining movement points
  58. ui8 sex;
  59. bool inTownGarrison; // if hero is in town garrison
  60. ConstTransitivePtr<CGTownInstance> visitedTown; //set if hero is visiting town or in the town garrison
  61. ConstTransitivePtr<CCommanderInstance> commander;
  62. const CGBoat *boat; //set to CGBoat when sailing
  63. static const si32 UNINITIALIZED_PORTRAIT = -1;
  64. static const si32 UNINITIALIZED_MANA = -1;
  65. static const ui32 UNINITIALIZED_MOVEMENT = -1;
  66. //std::vector<const CArtifact*> artifacts; //hero's artifacts from bag
  67. //std::map<ui16, const CArtifact*> artifWorn; //map<position,artifact_id>; positions: 0 - head; 1 - shoulders; 2 - neck; 3 - right hand; 4 - left hand; 5 - torso; 6 - right ring; 7 - left ring; 8 - feet; 9 - misc1; 10 - misc2; 11 - misc3; 12 - misc4; 13 - mach1; 14 - mach2; 15 - mach3; 16 - mach4; 17 - spellbook; 18 - misc5
  68. std::set<ObjectInstanceID> visitedObjects;
  69. struct DLL_LINKAGE Patrol
  70. {
  71. Patrol(){patrolling=false;initialPos=int3();patrolRadius=-1;};
  72. bool patrolling;
  73. int3 initialPos;
  74. ui32 patrolRadius;
  75. template <typename Handler> void serialize(Handler &h, const int version)
  76. {
  77. h & patrolling;
  78. h & initialPos;
  79. h & patrolRadius;
  80. }
  81. } patrol;
  82. // deprecated - used only for loading of old saves
  83. struct HeroSpecial : CBonusSystemNode
  84. {
  85. bool growsWithLevel;
  86. HeroSpecial(){growsWithLevel = false;};
  87. template <typename Handler> void serialize(Handler &h, const int version)
  88. {
  89. h & static_cast<CBonusSystemNode&>(*this);
  90. h & growsWithLevel;
  91. }
  92. };
  93. struct DLL_LINKAGE SecondarySkillsInfo
  94. {
  95. //skills are determined, initialized at map start
  96. //FIXME remove mutable
  97. mutable CRandomGenerator rand;
  98. ui8 magicSchoolCounter;
  99. ui8 wisdomCounter;
  100. SecondarySkillsInfo();
  101. void resetMagicSchoolCounter();
  102. void resetWisdomCounter();
  103. template <typename Handler> void serialize(Handler &h, const int version)
  104. {
  105. h & magicSchoolCounter;
  106. h & wisdomCounter;
  107. h & rand;
  108. }
  109. } skillsInfo;
  110. inline bool isInitialized() const
  111. { // has this hero been on the map at least once?
  112. return movement != UNINITIALIZED_MOVEMENT && mana != UNINITIALIZED_MANA;
  113. }
  114. //int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  115. int getSightRadius() const override; //sight distance (should be used if player-owned structure)
  116. //////////////////////////////////////////////////////////////////////////
  117. int getBoatType() const override; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  118. void getOutOffsets(std::vector<int3> &offsets) const override; //offsets to obj pos when we boat can be placed
  119. //////////////////////////////////////////////////////////////////////////
  120. bool hasSpellbook() const;
  121. int maxSpellLevel() const;
  122. void addSpellToSpellbook(SpellID spell);
  123. void removeSpellFromSpellbook(SpellID spell);
  124. bool spellbookContainsSpell(SpellID spell) const;
  125. void removeSpellbook();
  126. const std::set<SpellID> & getSpellsInSpellbook() const;
  127. EAlignment::EAlignment getAlignment() const;
  128. const std::string &getBiography() const;
  129. bool needsLastStack()const override;
  130. ui32 getTileCost(const TerrainTile &dest, const TerrainTile &from, const TurnInfo * ti) const; //move cost - applying pathfinding skill, road and terrain modifiers. NOT includes diagonal move penalty, last move levelling
  131. TerrainId getNativeTerrain() const;
  132. ui32 getLowestCreatureSpeed() const;
  133. int3 getPosition(bool h3m = false) const; //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  134. si32 manaRegain() const; //how many points of mana can hero regain "naturally" in one day
  135. si32 getManaNewTurn() const; //calculate how much mana this hero is going to have the next day
  136. int getCurrentLuck(int stack=-1, bool town=false) const;
  137. int32_t getSpellCost(const spells::Spell * sp) const; //do not use during battles -> bonuses from army would be ignored
  138. bool canLearnSpell(const spells::Spell * spell) const;
  139. bool canCastThisSpell(const spells::Spell * spell) const; //determines if this hero can cast given spell; takes into account existing spell in spellbook, existing spellbook and artifact bonuses
  140. // ----- primary and secondary skill, experience, level handling -----
  141. /// Returns true if hero has lower level than should upon his experience.
  142. bool gainsLevel() const;
  143. /// Returns the next primary skill on level up. Can only be called if hero can gain a level up.
  144. PrimarySkill::PrimarySkill nextPrimarySkill(CRandomGenerator & rand) const;
  145. /// Returns the next secondary skill randomly on level up. Can only be called if hero can gain a level up.
  146. boost::optional<SecondarySkill> nextSecondarySkill(CRandomGenerator & rand) const;
  147. /// Gets 0, 1 or 2 secondary skills which are proposed on hero level up.
  148. std::vector<SecondarySkill> getLevelUpProposedSecondarySkills() const;
  149. ui8 getSecSkillLevel(SecondarySkill skill) const; //0 - no skill
  150. /// Returns true if hero has free secondary skill slot.
  151. bool canLearnSkill() const;
  152. bool canLearnSkill(SecondarySkill which) const;
  153. void setPrimarySkill(PrimarySkill::PrimarySkill primarySkill, si64 value, ui8 abs);
  154. void setSecSkillLevel(SecondarySkill which, int val, bool abs);// abs == 0 - changes by value; 1 - sets to value
  155. void levelUp(std::vector<SecondarySkill> skills);
  156. int maxMovePoints(bool onLand) const;
  157. //cached version is much faster, TurnInfo construction is costly
  158. int maxMovePointsCached(bool onLand, const TurnInfo * ti) const;
  159. int movementPointsAfterEmbark(int MPsBefore, int basicCost, bool disembark = false, const TurnInfo * ti = nullptr) const;
  160. static int3 convertPosition(int3 src, bool toh3m); //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  161. double getFightingStrength() const; // takes attack / defense skill into account
  162. double getMagicStrength() const; // takes knowledge / spell power skill into account
  163. double getHeroStrength() const; // includes fighting and magic strength
  164. ui64 getTotalStrength() const; // includes fighting strength and army strength
  165. TExpType calculateXp(TExpType exp) const; //apply learning skill
  166. CStackBasicDescriptor calculateNecromancy (const BattleResult &battleResult) const;
  167. void showNecromancyDialog(const CStackBasicDescriptor &raisedStack, CRandomGenerator & rand) const;
  168. EDiggingStatus diggingStatus() const;
  169. //////////////////////////////////////////////////////////////////////////
  170. void setType(si32 ID, si32 subID) override;
  171. void initHero(CRandomGenerator & rand);
  172. void initHero(CRandomGenerator & rand, HeroTypeID SUBID);
  173. void putArtifact(ArtifactPosition pos, CArtifactInstance * art) override;
  174. void putInBackpack(CArtifactInstance *art);
  175. void initExp(CRandomGenerator & rand);
  176. void initArmy(CRandomGenerator & rand, IArmyDescriptor *dst = nullptr);
  177. //void giveArtifact (ui32 aid);
  178. void pushPrimSkill(PrimarySkill::PrimarySkill which, int val);
  179. ui8 maxlevelsToMagicSchool() const;
  180. ui8 maxlevelsToWisdom() const;
  181. void recreateSecondarySkillsBonuses();
  182. void updateSkillBonus(SecondarySkill which, int val);
  183. bool hasVisions(const CGObjectInstance * target, const int subtype) const;
  184. /// If this hero perishes, the scenario is failed
  185. bool isMissionCritical() const;
  186. CGHeroInstance();
  187. virtual ~CGHeroInstance();
  188. PlayerColor getOwner() const override;
  189. ///ArtBearer
  190. ArtBearer::ArtBearer bearerType() const override;
  191. ///IBonusBearer
  192. CBonusSystemNode & whereShouldBeAttached(CGameState * gs) override;
  193. std::string nodeName() const override;
  194. CBonusSystemNode * whereShouldBeAttachedOnSiege(const bool isBattleOutsideTown) const;
  195. CBonusSystemNode * whereShouldBeAttachedOnSiege(CGameState * gs);
  196. ///spells::Caster
  197. int32_t getCasterUnitId() const override;
  198. int32_t getSpellSchoolLevel(const spells::Spell * spell, int32_t * outSelectedSchool = nullptr) const override;
  199. int64_t getSpellBonus(const spells::Spell * spell, int64_t base, const battle::Unit * affectedStack) const override;
  200. int64_t getSpecificSpellBonus(const spells::Spell * spell, int64_t base) const override;
  201. int32_t getEffectLevel(const spells::Spell * spell) const override;
  202. int32_t getEffectPower(const spells::Spell * spell) const override;
  203. int32_t getEnchantPower(const spells::Spell * spell) const override;
  204. int64_t getEffectValue(const spells::Spell * spell) const override;
  205. PlayerColor getCasterOwner() const override;
  206. void getCasterName(MetaString & text) const override;
  207. void getCastDescription(const spells::Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
  208. void spendMana(ServerCallback * server, const int spellCost) const override;
  209. void deserializationFix();
  210. void initObj(CRandomGenerator & rand) override;
  211. void onHeroVisit(const CGHeroInstance * h) const override;
  212. std::string getObjectName() const override;
  213. void afterAddToMap(CMap * map) override;
  214. void afterRemoveFromMap(CMap * map) override;
  215. void updateFrom(const JsonNode & data) override;
  216. BattleField getBattlefield() const override;
  217. protected:
  218. void setPropertyDer(ui8 what, ui32 val) override;//synchr
  219. ///common part of hero instance and hero definition
  220. void serializeCommonOptions(JsonSerializeFormat & handler);
  221. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  222. private:
  223. void levelUpAutomatically(CRandomGenerator & rand);
  224. public:
  225. std::string getHeroTypeName() const;
  226. void setHeroTypeName(const std::string & identifier);
  227. void serializeJsonDefinition(JsonSerializeFormat & handler);
  228. template <typename Handler> void serialize(Handler &h, const int version)
  229. {
  230. h & static_cast<CArmedInstance&>(*this);
  231. h & static_cast<CArtifactSet&>(*this);
  232. h & exp;
  233. h & level;
  234. h & name;
  235. h & biography;
  236. h & portrait;
  237. h & mana;
  238. h & secSkills;
  239. h & movement;
  240. h & sex;
  241. h & inTownGarrison;
  242. h & spells;
  243. h & patrol;
  244. h & moveDir;
  245. h & skillsInfo;
  246. h & visitedTown;
  247. h & boat;
  248. h & type;
  249. h & commander;
  250. h & visitedObjects;
  251. BONUS_TREE_DESERIALIZATION_FIX
  252. }
  253. };
  254. VCMI_LIB_NAMESPACE_END