CGHeroInstance.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #pragma once
  2. #include "CObjectHandler.h"
  3. #include "CArmedInstance.h"
  4. #include "../CArtHandler.h" // For CArtifactSet
  5. #include "../CRandomGenerator.h"
  6. /*
  7. * CGHeroInstance.h, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. class CHero;
  16. class CGBoat;
  17. class CGTownInstance;
  18. struct TerrainTile;
  19. class CGHeroPlaceholder : public CGObjectInstance
  20. {
  21. public:
  22. //subID stores id of hero type. If it's 0xff then following field is used
  23. ui8 power;
  24. template <typename Handler> void serialize(Handler &h, const int version)
  25. {
  26. h & static_cast<CGObjectInstance&>(*this);
  27. h & power;
  28. }
  29. };
  30. class DLL_LINKAGE CGHeroInstance : public CArmedInstance, public IBoatGenerator, public CArtifactSet
  31. {
  32. public:
  33. enum ECanDig
  34. {
  35. CAN_DIG, LACK_OF_MOVEMENT, WRONG_TERRAIN, TILE_OCCUPIED
  36. };
  37. //////////////////////////////////////////////////////////////////////////
  38. ui8 moveDir; //format: 123
  39. // 8 4
  40. // 765
  41. mutable ui8 isStanding, tacticFormationEnabled;
  42. //////////////////////////////////////////////////////////////////////////
  43. ConstTransitivePtr<CHero> type;
  44. TExpType exp; //experience points
  45. ui32 level; //current level of hero
  46. std::string name; //may be custom
  47. std::string biography; //if custom
  48. si32 portrait; //may be custom
  49. si32 mana; // remaining spell points
  50. 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
  51. ui32 movement; //remaining movement points
  52. ui8 sex;
  53. bool inTownGarrison; // if hero is in town garrison
  54. ConstTransitivePtr<CGTownInstance> visitedTown; //set if hero is visiting town or in the town garrison
  55. ConstTransitivePtr<CCommanderInstance> commander;
  56. const CGBoat *boat; //set to CGBoat when sailing
  57. //std::vector<const CArtifact*> artifacts; //hero's artifacts from bag
  58. //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
  59. std::set<SpellID> spells; //known spells (spell IDs)
  60. std::set<ObjectInstanceID> visitedObjects;
  61. struct DLL_LINKAGE Patrol
  62. {
  63. Patrol(){patrolling=false;patrolRadious=-1;};
  64. bool patrolling;
  65. ui32 patrolRadious;
  66. template <typename Handler> void serialize(Handler &h, const int version)
  67. {
  68. h & patrolling & patrolRadious;
  69. }
  70. } patrol;
  71. struct DLL_LINKAGE HeroSpecial : CBonusSystemNode
  72. {
  73. bool growsWithLevel;
  74. HeroSpecial(){growsWithLevel = false;};
  75. template <typename Handler> void serialize(Handler &h, const int version)
  76. {
  77. h & static_cast<CBonusSystemNode&>(*this);
  78. h & growsWithLevel;
  79. }
  80. };
  81. std::vector<HeroSpecial*> specialty;
  82. struct DLL_LINKAGE SecondarySkillsInfo
  83. {
  84. //skills are determined, initialized at map start
  85. //FIXME remove mutable
  86. mutable CRandomGenerator rand;
  87. ui8 magicSchoolCounter;
  88. ui8 wisdomCounter;
  89. void resetMagicSchoolCounter();
  90. void resetWisdomCounter();
  91. template <typename Handler> void serialize(Handler &h, const int version)
  92. {
  93. h & magicSchoolCounter & wisdomCounter & rand;
  94. }
  95. } skillsInfo;
  96. //int3 getSightCenter() const; //"center" tile from which the sight distance is calculated
  97. int getSightRadious() const; //sight distance (should be used if player-owned structure)
  98. //////////////////////////////////////////////////////////////////////////
  99. int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
  100. void getOutOffsets(std::vector<int3> &offsets) const; //offsets to obj pos when we boat can be placed
  101. //////////////////////////////////////////////////////////////////////////
  102. bool hasSpellbook() const;
  103. EAlignment::EAlignment getAlignment() const;
  104. const std::string &getBiography() const;
  105. bool needsLastStack()const;
  106. ui32 getTileCost(const TerrainTile &dest, const TerrainTile &from) const; //move cost - applying pathfinding skill, road and terrain modifiers. NOT includes diagonal move penalty, last move levelling
  107. ui32 getLowestCreatureSpeed() const;
  108. int3 getPosition(bool h3m = false) const; //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  109. si32 manaRegain() const; //how many points of mana can hero regain "naturally" in one day
  110. bool canWalkOnSea() const;
  111. int getCurrentLuck(int stack=-1, bool town=false) const;
  112. int getSpellCost(const CSpell *sp) const; //do not use during battles -> bonuses from army would be ignored
  113. // ----- primary and secondary skill, experience, level handling -----
  114. /// Returns true if hero has lower level than should upon his experience.
  115. bool gainsLevel() const;
  116. /// Returns the next primary skill on level up. Can only be called if hero can gain a level up.
  117. PrimarySkill::PrimarySkill nextPrimarySkill() const;
  118. /// Returns the next secondary skill randomly on level up. Can only be called if hero can gain a level up.
  119. boost::optional<SecondarySkill> nextSecondarySkill() const;
  120. /// Gets 0, 1 or 2 secondary skills which are proposed on hero level up.
  121. std::vector<SecondarySkill> getLevelUpProposedSecondarySkills() const;
  122. ui8 getSecSkillLevel(SecondarySkill skill) const; //0 - no skill
  123. /// Returns true if hero has free secondary skill slot.
  124. bool canLearnSkill() const;
  125. void setPrimarySkill(PrimarySkill::PrimarySkill primarySkill, si64 value, ui8 abs);
  126. void setSecSkillLevel(SecondarySkill which, int val, bool abs);// abs == 0 - changes by value; 1 - sets to value
  127. void levelUp(std::vector<SecondarySkill> skills);
  128. int maxMovePoints(bool onLand) const;
  129. int movementPointsAfterEmbark(int MPsBefore, int basicCost, bool disembark = false) const;
  130. //int getSpellSecLevel(int spell) const; //returns level of secondary ability (fire, water, earth, air magic) known to this hero and applicable to given spell; -1 if error
  131. static int3 convertPosition(int3 src, bool toh3m); //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  132. double getFightingStrength() const; // takes attack / defense skill into account
  133. double getMagicStrength() const; // takes knowledge / spell power skill into account
  134. double getHeroStrength() const; // includes fighting and magic strength
  135. ui64 getTotalStrength() const; // includes fighting strength and army strength
  136. TExpType calculateXp(TExpType exp) const; //apply learning skill
  137. ui8 getSpellSchoolLevel(const CSpell * spell, int *outSelectedSchool = nullptr) const; //returns level on which given spell would be cast by this hero (0 - none, 1 - basic etc); optionally returns number of selected school by arg - 0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  138. 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
  139. CStackBasicDescriptor calculateNecromancy (const BattleResult &battleResult) const;
  140. void showNecromancyDialog(const CStackBasicDescriptor &raisedStack) const;
  141. ECanDig diggingStatus() const; //0 - can dig; 1 - lack of movement; 2 -
  142. //////////////////////////////////////////////////////////////////////////
  143. void setType(si32 ID, si32 subID);
  144. void initHero();
  145. void initHero(HeroTypeID SUBID);
  146. void putArtifact(ArtifactPosition pos, CArtifactInstance *art);
  147. void putInBackpack(CArtifactInstance *art);
  148. void initExp();
  149. void initArmy(IArmyDescriptor *dst = nullptr);
  150. //void giveArtifact (ui32 aid);
  151. void pushPrimSkill(PrimarySkill::PrimarySkill which, int val);
  152. ui8 maxlevelsToMagicSchool() const;
  153. ui8 maxlevelsToWisdom() const;
  154. void Updatespecialty();
  155. void recreateSecondarySkillsBonuses();
  156. void updateSkill(SecondarySkill which, int val);
  157. CGHeroInstance();
  158. virtual ~CGHeroInstance();
  159. //////////////////////////////////////////////////////////////////////////
  160. //
  161. ArtBearer::ArtBearer bearerType() const override;
  162. //////////////////////////////////////////////////////////////////////////
  163. CBonusSystemNode *whereShouldBeAttached(CGameState *gs) override;
  164. std::string nodeName() const override;
  165. void deserializationFix();
  166. void initObj() override;
  167. void onHeroVisit(const CGHeroInstance * h) const override;
  168. std::string getObjectName() const override;
  169. protected:
  170. void setPropertyDer(ui8 what, ui32 val) override;//synchr
  171. private:
  172. void levelUpAutomatically();
  173. public:
  174. template <typename Handler> void serialize(Handler &h, const int version)
  175. {
  176. h & static_cast<CArmedInstance&>(*this);
  177. h & static_cast<CArtifactSet&>(*this);
  178. h & exp & level & name & biography & portrait & mana & secSkills & movement
  179. & sex & inTownGarrison & spells & patrol & moveDir & skillsInfo;
  180. h & visitedTown & boat;
  181. h & type & specialty & commander & visitedObjects;
  182. BONUS_TREE_DESERIALIZATION_FIX
  183. //visitied town pointer will be restored by map serialization method
  184. }
  185. };