2
0

CGHeroInstance.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #pragma once
  2. #include "CObjectHandler.h"
  3. #include "CArmedInstance.h"
  4. #include "../spells/Magic.h"
  5. #include "../CArtHandler.h" // For CArtifactSet
  6. #include "../CRandomGenerator.h"
  7. /*
  8. * CGHeroInstance.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CHero;
  17. class CGBoat;
  18. class CGTownInstance;
  19. class CMap;
  20. struct TerrainTile;
  21. struct TurnInfo;
  22. class CGHeroPlaceholder : public CGObjectInstance
  23. {
  24. public:
  25. //subID stores id of hero type. If it's 0xff then following field is used
  26. ui8 power;
  27. template <typename Handler> void serialize(Handler &h, const int version)
  28. {
  29. h & static_cast<CGObjectInstance&>(*this);
  30. h & power;
  31. }
  32. };
  33. class DLL_LINKAGE CGHeroInstance : public CArmedInstance, public IBoatGenerator, public CArtifactSet, public ISpellCaster
  34. {
  35. public:
  36. //////////////////////////////////////////////////////////////////////////
  37. ui8 moveDir; //format: 123
  38. // 8 4
  39. // 765
  40. mutable ui8 isStanding, tacticFormationEnabled;
  41. //////////////////////////////////////////////////////////////////////////
  42. ConstTransitivePtr<CHero> type;
  43. TExpType exp; //experience points
  44. ui32 level; //current level of hero
  45. std::string name; //may be custom
  46. std::string biography; //if custom
  47. si32 portrait; //may be custom
  48. si32 mana; // remaining spell points
  49. 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
  50. ui32 movement; //remaining movement points
  51. ui8 sex;
  52. bool inTownGarrison; // if hero is in town garrison
  53. ConstTransitivePtr<CGTownInstance> visitedTown; //set if hero is visiting town or in the town garrison
  54. ConstTransitivePtr<CCommanderInstance> commander;
  55. const CGBoat *boat; //set to CGBoat when sailing
  56. static const ui32 UNINITIALIZED_PORTRAIT = -1;
  57. static const ui32 UNINITIALIZED_MANA = -1;
  58. static const ui32 UNINITIALIZED_MOVEMENT = -1;
  59. //std::vector<const CArtifact*> artifacts; //hero's artifacts from bag
  60. //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
  61. std::set<SpellID> spells; //known spells (spell IDs)
  62. std::set<ObjectInstanceID> visitedObjects;
  63. struct DLL_LINKAGE Patrol
  64. {
  65. Patrol(){patrolling=false;initialPos=int3();patrolRadius=-1;};
  66. bool patrolling;
  67. int3 initialPos;
  68. ui32 patrolRadius;
  69. template <typename Handler> void serialize(Handler &h, const int version)
  70. {
  71. h & patrolling;
  72. if(version >= 755) //save format backward compatibility
  73. {
  74. h & initialPos;
  75. }
  76. else if(!h.saving)
  77. {
  78. patrolling = false;
  79. initialPos = int3();
  80. }
  81. h & patrolRadius;
  82. }
  83. } patrol;
  84. struct DLL_LINKAGE HeroSpecial : CBonusSystemNode
  85. {
  86. bool growsWithLevel;
  87. HeroSpecial(){growsWithLevel = false;};
  88. template <typename Handler> void serialize(Handler &h, const int version)
  89. {
  90. h & static_cast<CBonusSystemNode&>(*this);
  91. h & growsWithLevel;
  92. }
  93. };
  94. std::vector<HeroSpecial*> specialty;
  95. struct DLL_LINKAGE SecondarySkillsInfo
  96. {
  97. //skills are determined, initialized at map start
  98. //FIXME remove mutable
  99. mutable CRandomGenerator rand;
  100. ui8 magicSchoolCounter;
  101. ui8 wisdomCounter;
  102. SecondarySkillsInfo();
  103. void resetMagicSchoolCounter();
  104. void resetWisdomCounter();
  105. template <typename Handler> void serialize(Handler &h, const int version)
  106. {
  107. h & magicSchoolCounter & wisdomCounter & 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. EAlignment::EAlignment getAlignment() const;
  122. const std::string &getBiography() const;
  123. bool needsLastStack()const override;
  124. 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
  125. int getNativeTerrain() const;
  126. ui32 getLowestCreatureSpeed() const;
  127. int3 getPosition(bool h3m = false) const; //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  128. si32 manaRegain() const; //how many points of mana can hero regain "naturally" in one day
  129. si32 getManaNewTurn() const; //calculate how much mana this hero is going to have the next day
  130. int getCurrentLuck(int stack=-1, bool town=false) const;
  131. int getSpellCost(const CSpell *sp) const; //do not use during battles -> bonuses from army would be ignored
  132. // ----- primary and secondary skill, experience, level handling -----
  133. /// Returns true if hero has lower level than should upon his experience.
  134. bool gainsLevel() const;
  135. /// Returns the next primary skill on level up. Can only be called if hero can gain a level up.
  136. PrimarySkill::PrimarySkill nextPrimarySkill() const;
  137. /// Returns the next secondary skill randomly on level up. Can only be called if hero can gain a level up.
  138. boost::optional<SecondarySkill> nextSecondarySkill() const;
  139. /// Gets 0, 1 or 2 secondary skills which are proposed on hero level up.
  140. std::vector<SecondarySkill> getLevelUpProposedSecondarySkills() const;
  141. ui8 getSecSkillLevel(SecondarySkill skill) const; //0 - no skill
  142. /// Returns true if hero has free secondary skill slot.
  143. bool canLearnSkill() const;
  144. void setPrimarySkill(PrimarySkill::PrimarySkill primarySkill, si64 value, ui8 abs);
  145. void setSecSkillLevel(SecondarySkill which, int val, bool abs);// abs == 0 - changes by value; 1 - sets to value
  146. void levelUp(std::vector<SecondarySkill> skills);
  147. int maxMovePoints(bool onLand, const TurnInfo * ti = nullptr) const;
  148. int movementPointsAfterEmbark(int MPsBefore, int basicCost, bool disembark = false, const TurnInfo * ti = nullptr) const;
  149. static int3 convertPosition(int3 src, bool toh3m); //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  150. double getFightingStrength() const; // takes attack / defense skill into account
  151. double getMagicStrength() const; // takes knowledge / spell power skill into account
  152. double getHeroStrength() const; // includes fighting and magic strength
  153. ui64 getTotalStrength() const; // includes fighting strength and army strength
  154. TExpType calculateXp(TExpType exp) const; //apply learning skill
  155. bool canCastThisSpell(const CSpell * spell) const; //determines if this hero can cast given spell; takes into account existing spell in spellbook, existing spellbook and artifact bonuses
  156. CStackBasicDescriptor calculateNecromancy (const BattleResult &battleResult) const;
  157. void showNecromancyDialog(const CStackBasicDescriptor &raisedStack) const;
  158. EDiggingStatus diggingStatus() const;
  159. //////////////////////////////////////////////////////////////////////////
  160. void setType(si32 ID, si32 subID) override;
  161. void initHero();
  162. void initHero(HeroTypeID SUBID);
  163. void putArtifact(ArtifactPosition pos, CArtifactInstance *art);
  164. void putInBackpack(CArtifactInstance *art);
  165. void initExp();
  166. void initArmy(CRandomGenerator & rand, IArmyDescriptor *dst = nullptr);
  167. //void giveArtifact (ui32 aid);
  168. void pushPrimSkill(PrimarySkill::PrimarySkill which, int val);
  169. ui8 maxlevelsToMagicSchool() const;
  170. ui8 maxlevelsToWisdom() const;
  171. void Updatespecialty();
  172. void recreateSecondarySkillsBonuses();
  173. void updateSkill(SecondarySkill which, int val);
  174. bool hasVisions(const CGObjectInstance * target, const int subtype) const;
  175. /// If this hero perishes, the scenario is failed
  176. bool isMissionCritical() const;
  177. CGHeroInstance();
  178. virtual ~CGHeroInstance();
  179. ///ArtBearer
  180. ArtBearer::ArtBearer bearerType() const override;
  181. ///IBonusBearer
  182. CBonusSystemNode *whereShouldBeAttached(CGameState *gs) override;
  183. std::string nodeName() const override;
  184. ///ISpellCaster
  185. ui8 getSpellSchoolLevel(const CSpell * spell, int *outSelectedSchool = nullptr) const override;
  186. ui32 getSpellBonus(const CSpell * spell, ui32 base, const CStack * affectedStack) const override;
  187. ///default spell school level for effect calculation
  188. int getEffectLevel(const CSpell * spell) const override;
  189. ///default spell-power for damage/heal calculation
  190. int getEffectPower(const CSpell * spell) const override;
  191. ///default spell-power for timed effects duration
  192. int getEnchantPower(const CSpell * spell) const override;
  193. ///damage/heal override(ignores spell configuration, effect level and effect power)
  194. int getEffectValue(const CSpell * spell) const override;
  195. const PlayerColor getOwner() const override;
  196. void deserializationFix();
  197. void initObj() override;
  198. void onHeroVisit(const CGHeroInstance * h) const override;
  199. std::string getObjectName() const override;
  200. protected:
  201. void setPropertyDer(ui8 what, ui32 val) override;//synchr
  202. void serializeJsonOptions(JsonSerializeFormat & handler) override;
  203. private:
  204. void levelUpAutomatically();
  205. public:
  206. template <typename Handler> void serialize(Handler &h, const int version)
  207. {
  208. h & static_cast<CArmedInstance&>(*this);
  209. h & static_cast<CArtifactSet&>(*this);
  210. h & exp & level & name & biography & portrait & mana & secSkills & movement
  211. & sex & inTownGarrison & spells & patrol & moveDir & skillsInfo;
  212. h & visitedTown & boat;
  213. h & type & specialty & commander & visitedObjects;
  214. BONUS_TREE_DESERIALIZATION_FIX
  215. //visitied town pointer will be restored by map serialization method
  216. }
  217. };