CHeroHandler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #pragma once
  2. #include "../lib/ConstTransitivePtr.h"
  3. #include "GameConstants.h"
  4. /*
  5. * CHeroHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CHeroClass;
  14. class CDefHandler;
  15. class CGameInfo;
  16. class CGHeroInstance;
  17. struct BattleHex;
  18. class JsonNode;
  19. struct SSpecialtyInfo
  20. { si32 type;
  21. si32 val;
  22. si32 subtype;
  23. si32 additionalinfo;
  24. template <typename Handler> void serialize(Handler &h, const int version)
  25. {
  26. h & type & val & subtype & additionalinfo;
  27. }
  28. };
  29. class DLL_LINKAGE CHero
  30. {
  31. public:
  32. struct InitialArmyStack
  33. {
  34. ui32 minAmount;
  35. ui32 maxAmount;
  36. TCreature creature;
  37. template <typename Handler> void serialize(Handler &h, const int version)
  38. {
  39. h & minAmount & maxAmount & creature;
  40. }
  41. };
  42. std::string name; //name of hero
  43. si32 ID;
  44. InitialArmyStack initialArmy[3];
  45. CHeroClass * heroClass;
  46. std::vector<std::pair<ui8,ui8> > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert)
  47. std::vector<SSpecialtyInfo> spec;
  48. si32 startingSpell; //-1 if none
  49. ui8 sex; // default sex: 0=male, 1=female
  50. CHero();
  51. ~CHero();
  52. template <typename Handler> void serialize(Handler &h, const int version)
  53. {
  54. h & name & ID & initialArmy & heroClass & secSkillsInit & spec & startingSpell & sex;
  55. }
  56. };
  57. class DLL_LINKAGE CHeroClass
  58. {
  59. public:
  60. std::string identifier;
  61. std::string name; // translatable
  62. double aggression;
  63. TFaction faction;
  64. ui8 id;
  65. std::vector<int> primarySkillInitial; // initial primary skills
  66. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  67. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  68. std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  69. std::map<TFaction, int> selectionProbability; //probability of selection in towns
  70. int chooseSecSkill(const std::set<int> & possibles) const; //picks secondary skill out from given possibilities
  71. CHeroClass(); //c-tor
  72. ~CHeroClass(); //d-tor
  73. template <typename Handler> void serialize(Handler &h, const int version)
  74. {
  75. h & identifier & name & faction & aggression;
  76. h & primarySkillInitial & primarySkillLowLevel;
  77. h & primarySkillHighLevel & secSkillProbability;
  78. h & selectionProbability;
  79. }
  80. EAlignment::EAlignment getAlignment() const;
  81. };
  82. struct DLL_LINKAGE CObstacleInfo
  83. {
  84. si32 ID;
  85. std::string defName;
  86. std::vector<ui8> allowedTerrains;
  87. std::vector<ui8> allowedSpecialBfields;
  88. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  89. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  90. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  91. std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
  92. bool isAppropriate(int terrainType, int specialBattlefield = -1) const;
  93. template <typename Handler> void serialize(Handler &h, const int version)
  94. {
  95. h & ID & defName & allowedTerrains & allowedSpecialBfields & isAbsoluteObstacle & width & height & blockedTiles;
  96. }
  97. };
  98. class DLL_LINKAGE CHeroClassHandler
  99. {
  100. public:
  101. std::vector< ConstTransitivePtr<CHeroClass> > heroClasses;
  102. /// load from H3 config
  103. void load();
  104. /// load any number of classes from json
  105. void load(const JsonNode & classes);
  106. /// load one class from json
  107. CHeroClass * loadClass(const JsonNode & heroClass);
  108. ~CHeroClassHandler();
  109. template <typename Handler> void serialize(Handler &h, const int version)
  110. {
  111. h & heroClasses;
  112. }
  113. };
  114. class DLL_LINKAGE CHeroHandler
  115. {
  116. /// expPerLEvel[i] is amount of exp needed to reach level i;
  117. /// consists of 201 values. Any higher levels require experience larger that ui64 can hold
  118. std::vector<ui64> expPerLevel;
  119. public:
  120. CHeroClassHandler classes;
  121. std::vector< ConstTransitivePtr<CHero> > heroes;
  122. //default costs of going through terrains. -1 means terrain is impassable
  123. std::vector<int> terrCosts;
  124. struct SBallisticsLevelInfo
  125. {
  126. ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
  127. ui8 shots; //how many shots we have
  128. ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
  129. ui8 sum; //I don't know if it is useful for anything, but it's in config file
  130. template <typename Handler> void serialize(Handler &h, const int version)
  131. {
  132. h & keep & tower & gate & wall & shots & noDmg & oneDmg & twoDmg & sum;
  133. }
  134. };
  135. std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
  136. std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
  137. std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
  138. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  139. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  140. /// Load multiple heroes from json
  141. void load(const JsonNode & heroes);
  142. /// Load single hero from json
  143. CHero * loadHero(const JsonNode & hero);
  144. /// Load everything (calls functions below + classes.load())
  145. void load();
  146. void loadHeroes();
  147. void loadExperience();
  148. void loadBallistics();
  149. void loadTerrains();
  150. void loadObstacles();
  151. CHeroHandler(); //c-tor
  152. ~CHeroHandler(); //d-tor
  153. /**
  154. * Gets a list of default allowed heroes.
  155. *
  156. * TODO Proposal for hero modding: Replace hero id with a unique machine readable hero name and
  157. * create a JSON config file or merge it with a existing config file which describes which heroes can be used for
  158. * random map generation / map editor(default map settings). (Gelu, ... should be excluded)
  159. *
  160. * @return a list of allowed heroes, the index is the hero id and the value either 0 for not allowed and 1 for allowed
  161. */
  162. std::vector<ui8> getDefaultAllowedHeroes() const;
  163. template <typename Handler> void serialize(Handler &h, const int version)
  164. {
  165. h & classes & heroes & expPerLevel & ballistics & terrCosts;
  166. h & obstacles & absoluteObstacles;
  167. }
  168. };