CHeroHandler.h 7.8 KB

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