CHeroHandler.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #pragma once
  2. #include "../lib/ConstTransitivePtr.h"
  3. #include "GameConstants.h"
  4. #include "HeroBonus.h"
  5. #include "IHandlerBase.h"
  6. /*
  7. * CHeroHandler.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 CHeroClass;
  16. class CDefHandler;
  17. class CGameInfo;
  18. class CGHeroInstance;
  19. struct BattleHex;
  20. class JsonNode;
  21. class CRandomGenerator;
  22. struct SSpecialtyInfo
  23. { si32 type;
  24. si32 val;
  25. si32 subtype;
  26. si32 additionalinfo;
  27. template <typename Handler> void serialize(Handler &h, const int version)
  28. {
  29. h & type & val & subtype & additionalinfo;
  30. }
  31. };
  32. struct SSpecialtyBonus
  33. /// temporary hold
  34. {
  35. ui8 growsWithLevel;
  36. BonusList bonuses;
  37. template <typename Handler> void serialize(Handler &h, const int version)
  38. {
  39. h & growsWithLevel & bonuses;
  40. }
  41. };
  42. class DLL_LINKAGE CHero
  43. {
  44. public:
  45. struct InitialArmyStack
  46. {
  47. ui32 minAmount;
  48. ui32 maxAmount;
  49. CreatureID creature;
  50. template <typename Handler> void serialize(Handler &h, const int version)
  51. {
  52. h & minAmount & maxAmount & creature;
  53. }
  54. };
  55. HeroTypeID ID;
  56. si32 imageIndex;
  57. std::vector<InitialArmyStack> initialArmy;
  58. CHeroClass * heroClass;
  59. std::vector<std::pair<SecondarySkill, ui8> > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert)
  60. std::vector<SSpecialtyInfo> spec;
  61. std::vector<SSpecialtyBonus> specialty;
  62. std::set<SpellID> spells;
  63. bool haveSpellBook;
  64. bool special; // hero is special and won't be placed in game (unless preset on map), e.g. campaign heroes
  65. ui8 sex; // default sex: 0=male, 1=female
  66. /// Localized texts
  67. std::string name; //name of hero
  68. std::string biography;
  69. std::string specName;
  70. std::string specDescr;
  71. std::string specTooltip;
  72. /// Graphics
  73. std::string iconSpecSmall;
  74. std::string iconSpecLarge;
  75. std::string portraitSmall;
  76. std::string portraitLarge;
  77. template <typename Handler> void serialize(Handler &h, const int version)
  78. {
  79. h & ID & imageIndex & initialArmy & heroClass & secSkillsInit & spec & specialty & spells & haveSpellBook & sex & special;
  80. h & name & biography & specName & specDescr & specTooltip;
  81. h & iconSpecSmall & iconSpecLarge & portraitSmall & portraitLarge;
  82. }
  83. };
  84. class DLL_LINKAGE CHeroClass
  85. {
  86. public:
  87. enum EClassAffinity
  88. {
  89. MIGHT,
  90. MAGIC
  91. };
  92. std::string identifier;
  93. std::string name; // translatable
  94. //double aggression; // not used in vcmi.
  95. TFaction faction;
  96. ui8 id;
  97. ui8 affinity; // affility, using EClassAffinity enum
  98. // default chance for hero of specific class to appear in tavern, if field "tavern" was not set
  99. // resulting chance = sqrt(town.chance * heroClass.chance)
  100. ui32 defaultTavernChance;
  101. CCreature * commander;
  102. std::vector<int> primarySkillInitial; // initial primary skills
  103. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  104. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  105. std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  106. std::map<TFaction, int> selectionProbability; //probability of selection in towns
  107. std::string imageBattleMale;
  108. std::string imageBattleFemale;
  109. std::string imageMapMale;
  110. std::string imageMapFemale;
  111. CHeroClass();
  112. bool isMagicHero() const;
  113. SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles, CRandomGenerator & rand) const; //picks secondary skill out from given possibilities
  114. template <typename Handler> void serialize(Handler &h, const int version)
  115. {
  116. h & identifier & name & faction & id & defaultTavernChance;// & aggression;
  117. h & primarySkillInitial & primarySkillLowLevel;
  118. h & primarySkillHighLevel & secSkillProbability;
  119. h & selectionProbability & affinity & commander;
  120. h & imageBattleMale & imageBattleFemale & imageMapMale & imageMapFemale;
  121. }
  122. EAlignment::EAlignment getAlignment() const;
  123. };
  124. struct DLL_LINKAGE CObstacleInfo
  125. {
  126. si32 ID;
  127. std::string defName;
  128. std::vector<ETerrainType> allowedTerrains;
  129. std::vector<BFieldType> allowedSpecialBfields;
  130. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  131. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  132. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  133. std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
  134. bool isAppropriate(ETerrainType terrainType, int specialBattlefield = -1) const;
  135. template <typename Handler> void serialize(Handler &h, const int version)
  136. {
  137. h & ID & defName & allowedTerrains & allowedSpecialBfields & isAbsoluteObstacle & width & height & blockedTiles;
  138. }
  139. };
  140. class DLL_LINKAGE CHeroClassHandler : public IHandlerBase
  141. {
  142. CHeroClass *loadFromJson(const JsonNode & node);
  143. public:
  144. std::vector< ConstTransitivePtr<CHeroClass> > heroClasses;
  145. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  146. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  147. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  148. void afterLoadFinalization() override;
  149. std::vector<bool> getDefaultAllowed() const override;
  150. ~CHeroClassHandler();
  151. template <typename Handler> void serialize(Handler &h, const int version)
  152. {
  153. h & heroClasses;
  154. }
  155. };
  156. class DLL_LINKAGE CHeroHandler : public IHandlerBase
  157. {
  158. /// expPerLEvel[i] is amount of exp needed to reach level i;
  159. /// consists of 201 values. Any higher levels require experience larger that ui64 can hold
  160. std::vector<ui64> expPerLevel;
  161. /// helpers for loading to avoid huge load functions
  162. void loadHeroArmy(CHero * hero, const JsonNode & node);
  163. void loadHeroSkills(CHero * hero, const JsonNode & node);
  164. void loadHeroSpecialty(CHero * hero, const JsonNode & node);
  165. void loadExperience();
  166. void loadBallistics();
  167. void loadTerrains();
  168. void loadObstacles();
  169. /// Load single hero from json
  170. CHero * loadFromJson(const JsonNode & node);
  171. public:
  172. CHeroClassHandler classes;
  173. std::vector< ConstTransitivePtr<CHero> > heroes;
  174. //default costs of going through terrains. -1 means terrain is impassable
  175. std::vector<int> terrCosts;
  176. struct SBallisticsLevelInfo
  177. {
  178. ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
  179. ui8 shots; //how many shots we have
  180. ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
  181. ui8 sum; //I don't know if it is useful for anything, but it's in config file
  182. template <typename Handler> void serialize(Handler &h, const int version)
  183. {
  184. h & keep & tower & gate & wall & shots & noDmg & oneDmg & twoDmg & sum;
  185. }
  186. };
  187. std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
  188. std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
  189. std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
  190. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  191. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  192. std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
  193. void loadObject(std::string scope, std::string name, const JsonNode & data) override;
  194. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
  195. CHeroHandler(); //c-tor
  196. ~CHeroHandler(); //d-tor
  197. std::vector<bool> getDefaultAllowed() const override;
  198. /**
  199. * Gets a list of default allowed abilities. OH3 abilities/skills are all allowed by default.
  200. *
  201. * @return a list of allowed abilities, the index is the ability id
  202. */
  203. std::vector<bool> getDefaultAllowedAbilities() const;
  204. template <typename Handler> void serialize(Handler &h, const int version)
  205. {
  206. h & classes & heroes & expPerLevel & ballistics & terrCosts;
  207. h & obstacles & absoluteObstacles;
  208. }
  209. };