CHeroHandler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. //bool operator<(CHero& drugi){if (ID < drugi.ID) return true; else return false;}
  51. CHero();
  52. ~CHero();
  53. template <typename Handler> void serialize(Handler &h, const int version)
  54. {
  55. h & name & ID & initialArmy & heroClass & secSkillsInit & spec & startingSpell & sex;
  56. }
  57. };
  58. class DLL_LINKAGE CHeroClass
  59. {
  60. public:
  61. std::string identifier;
  62. std::string name; // translatable
  63. double aggression;
  64. TFaction faction;
  65. ui8 id;
  66. std::vector<int> primarySkillInitial; // initial primary skills
  67. std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
  68. std::vector<int> primarySkillHighLevel;// same for high levels (> 10)
  69. std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order
  70. std::map<TFaction, int> selectionProbability; //probability of selection in towns
  71. int chooseSecSkill(const std::set<int> & possibles) const; //picks secondary skill out from given possibilities
  72. CHeroClass(); //c-tor
  73. ~CHeroClass(); //d-tor
  74. template <typename Handler> void serialize(Handler &h, const int version)
  75. {
  76. h & identifier & name & faction & aggression;
  77. h & primarySkillInitial & primarySkillLowLevel;
  78. h & primarySkillHighLevel & secSkillProbability;
  79. h & selectionProbability;
  80. }
  81. EAlignment::EAlignment getAlignment() const;
  82. };
  83. struct DLL_LINKAGE CObstacleInfo
  84. {
  85. si32 ID;
  86. std::string defName;
  87. std::vector<ui8> allowedTerrains;
  88. std::vector<ui8> allowedSpecialBfields;
  89. ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
  90. si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
  91. std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)
  92. std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'
  93. bool isAppropriate(int terrainType, int specialBattlefield = -1) const;
  94. template <typename Handler> void serialize(Handler &h, const int version)
  95. {
  96. h & ID & defName & allowedTerrains & allowedSpecialBfields & isAbsoluteObstacle & width & height & blockedTiles;
  97. }
  98. };
  99. class DLL_LINKAGE CHeroClassHandler
  100. {
  101. public:
  102. std::vector< ConstTransitivePtr<CHeroClass> > heroClasses;
  103. /// load from H3 config
  104. void load();
  105. /// load any number of classes from json
  106. void load(const JsonNode & classes);
  107. /// load one class from json
  108. void loadClass(const JsonNode & heroClass);
  109. ~CHeroClassHandler();
  110. template <typename Handler> void serialize(Handler &h, const int version)
  111. {
  112. h & heroClasses;
  113. }
  114. };
  115. class DLL_LINKAGE CHeroHandler
  116. {
  117. std::vector<ui64> expPerLevel; //expPerLEvel[i] is amount of exp needed to reach level i; if it is not in this vector, multiplicate last value by 1,2 to get next value
  118. public:
  119. CHeroClassHandler classes;
  120. std::vector< ConstTransitivePtr<CHero> > heroes; //changed from nodrze
  121. //default costs of going through terrains: dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock; -1 means terrain is imapassable
  122. std::vector<int> terrCosts;
  123. struct SBallisticsLevelInfo
  124. {
  125. ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
  126. ui8 shots; //how many shots we have
  127. ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
  128. ui8 sum; //I don't know if it is useful for anything, but it's in config file
  129. template <typename Handler> void serialize(Handler &h, const int version)
  130. {
  131. h & keep & tower & gate & wall & shots & noDmg & oneDmg & twoDmg & sum;
  132. }
  133. };
  134. std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
  135. std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
  136. std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
  137. void loadObstacles(); //loads info about obstacles
  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. void load();
  141. void loadHeroes();
  142. void loadTerrains();
  143. CHeroHandler(); //c-tor
  144. ~CHeroHandler(); //d-tor
  145. /**
  146. * Gets a list of default allowed heroes.
  147. *
  148. * TODO Proposal for hero modding: Replace hero id with a unique machine readable hero name and
  149. * create a JSON config file or merge it with a existing config file which describes which heroes can be used for
  150. * random map generation / map editor(default map settings). (Gelu, ... should be excluded)
  151. *
  152. * @return a list of allowed heroes, the index is the hero id and the value either 0 for not allowed and 1 for allowed
  153. */
  154. std::vector<ui8> getDefaultAllowedHeroes() const;
  155. template <typename Handler> void serialize(Handler &h, const int version)
  156. {
  157. h & classes & heroes & expPerLevel & ballistics & terrCosts;
  158. h & obstacles & absoluteObstacles;
  159. }
  160. };