CHeroHandler.h 8.4 KB

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