CHeroHandler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. struct SSpecialtyInfo
  19. { si32 type;
  20. si32 val;
  21. si32 subtype;
  22. si32 additionalinfo;
  23. template <typename Handler> void serialize(Handler &h, const int version)
  24. {
  25. h & type & val & subtype & additionalinfo;
  26. }
  27. };
  28. class DLL_LINKAGE CHero
  29. {
  30. public:
  31. struct InitialArmyStack
  32. {
  33. ui32 minAmount;
  34. ui32 maxAmount;
  35. TCreature creature;
  36. template <typename Handler> void serialize(Handler &h, const int version)
  37. {
  38. h & minAmount & maxAmount & creature;
  39. }
  40. };
  41. enum EHeroClasses {KNIGHT, CLERIC, RANGER, DRUID, ALCHEMIST, WIZARD,
  42. DEMONIAC, HERETIC, DEATHKNIGHT, NECROMANCER, WARLOCK, OVERLORD,
  43. BARBARIAN, BATTLEMAGE, BEASTMASTER, WITCH, PLANESWALKER, ELEMENTALIST};
  44. std::string name; //name of hero
  45. si32 ID;
  46. InitialArmyStack initialArmy[3];
  47. CHeroClass * heroClass;
  48. EHeroClasses heroType; //hero class
  49. std::vector<std::pair<ui8,ui8> > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert)
  50. std::vector<SSpecialtyInfo> spec;
  51. si32 startingSpell; //-1 if none
  52. ui8 sex; // default sex: 0=male, 1=female
  53. //bool operator<(CHero& drugi){if (ID < drugi.ID) return true; else return false;}
  54. CHero();
  55. ~CHero();
  56. template <typename Handler> void serialize(Handler &h, const int version)
  57. {
  58. h & name & ID & initialArmy & heroClass & heroType & secSkillsInit & spec & startingSpell & sex;
  59. }
  60. };
  61. class DLL_LINKAGE CHeroClass
  62. {
  63. public:
  64. ui8 alignment;
  65. ui32 skillLimit; //how many secondary skills can hero learn
  66. std::string name;
  67. double aggression;
  68. int initialPrimSkills[GameConstants::PRIMARY_SKILLS]; //initial values of primary skills, uses PrimarySkill enum
  69. std::vector<std::pair<int,int> > primChance;//primChance[PRIMARY_SKILL_ID] - first is for levels 2 - 9, second for 10+;;; probability (%) of getting point of primary skill when getting new level
  70. std::vector<int> proSec; //probabilities of gaining secondary skills (out of 112), in id order
  71. std::map<TFaction, int> selectionProbability; //probability of selection in towns
  72. int chooseSecSkill(const std::set<int> & possibles) const; //picks secondary skill out from given possibilities
  73. CHeroClass(); //c-tor
  74. ~CHeroClass(); //d-tor
  75. template <typename Handler> void serialize(Handler &h, const int version)
  76. {
  77. h & skillLimit & name & aggression & initialPrimSkills & primChance
  78. & proSec & selectionProbability & alignment;
  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 CHeroHandler
  99. {
  100. public:
  101. std::vector< ConstTransitivePtr<CHero> > heroes; //changed from nodrze
  102. std::vector<CHeroClass *> heroClasses;
  103. 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
  104. //default costs of going through terrains: dirt, sand, grass, snow, swamp, rough, subterranean, lava, water, rock; -1 means terrain is imapassable
  105. std::vector<int> terrCosts;
  106. struct SBallisticsLevelInfo
  107. {
  108. ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
  109. ui8 shots; //how many shots we have
  110. ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
  111. ui8 sum; //I don't know if it is useful for anything, but it's in config file
  112. template <typename Handler> void serialize(Handler &h, const int version)
  113. {
  114. h & keep & tower & gate & wall & shots & noDmg & oneDmg & twoDmg & sum;
  115. }
  116. };
  117. std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert
  118. std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
  119. std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield
  120. void loadObstacles(); //loads info about obstacles
  121. ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
  122. ui64 reqExp(ui32 level) const; //calculates experience required for given level
  123. void loadHeroes();
  124. void loadHeroClasses();
  125. void initHeroClasses();
  126. void loadTerrains();
  127. CHeroHandler(); //c-tor
  128. ~CHeroHandler(); //d-tor
  129. /**
  130. * Gets a list of default allowed heroes.
  131. *
  132. * TODO Proposal for hero modding: Replace hero id with a unique machine readable hero name and
  133. * create a JSON config file or merge it with a existing config file which describes which heroes can be used for
  134. * random map generation / map editor(default map settings). (Gelu, ... should be excluded)
  135. *
  136. * @return a list of allowed heroes, the index is the hero id and the value either 0 for not allowed and 1 for allowed
  137. */
  138. std::vector<ui8> getDefaultAllowedHeroes() const;
  139. template <typename Handler> void serialize(Handler &h, const int version)
  140. {
  141. h & heroClasses & heroes & expPerLevel & ballistics & terrCosts;
  142. h & obstacles & absoluteObstacles;
  143. if(!h.saving)
  144. {
  145. //restore class pointers
  146. for (int i=0; i<heroes.size(); i++)
  147. {
  148. heroes[i]->heroClass = heroClasses[heroes[i]->heroType];
  149. }
  150. }
  151. }
  152. };