CHeroClassHandler.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * CHeroClassHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CHeroClassHandler.h"
  12. #include "CHeroClass.h"
  13. #include "../faction/CTown.h"
  14. #include "../faction/CTownHandler.h"
  15. #include "../../CSkillHandler.h"
  16. #include "../../IGameSettings.h"
  17. #include "../../VCMI_Lib.h"
  18. #include "../../constants/StringConstants.h"
  19. #include "../../json/JsonNode.h"
  20. #include "../../mapObjectConstructors/AObjectTypeHandler.h"
  21. #include "../../mapObjectConstructors/CObjectClassesHandler.h"
  22. #include "../../modding/IdentifierStorage.h"
  23. #include "../../texts/CGeneralTextHandler.h"
  24. #include "../../texts/CLegacyConfigParser.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. void CHeroClassHandler::fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill pSkill) const
  27. {
  28. const auto & skillName = NPrimarySkill::names[pSkill.getNum()];
  29. auto currentPrimarySkillValue = static_cast<int>(node["primarySkills"][skillName].Integer());
  30. int primarySkillLegalMinimum = VLC->engineSettings()->getVector(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS)[pSkill.getNum()];
  31. if(currentPrimarySkillValue < primarySkillLegalMinimum)
  32. {
  33. logMod->error("Hero class '%s' has incorrect initial value '%d' for skill '%s'. Value '%d' will be used instead.",
  34. heroClass->getNameTranslated(), currentPrimarySkillValue, skillName, primarySkillLegalMinimum);
  35. currentPrimarySkillValue = primarySkillLegalMinimum;
  36. }
  37. heroClass->primarySkillInitial.push_back(currentPrimarySkillValue);
  38. heroClass->primarySkillLowLevel.push_back(static_cast<int>(node["lowLevelChance"][skillName].Float()));
  39. heroClass->primarySkillHighLevel.push_back(static_cast<int>(node["highLevelChance"][skillName].Float()));
  40. }
  41. const std::vector<std::string> & CHeroClassHandler::getTypeNames() const
  42. {
  43. static const std::vector<std::string> typeNames = { "heroClass" };
  44. return typeNames;
  45. }
  46. std::shared_ptr<CHeroClass> CHeroClassHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index)
  47. {
  48. assert(identifier.find(':') == std::string::npos);
  49. assert(!scope.empty());
  50. std::string affinityStr[2] = { "might", "magic" };
  51. auto heroClass = std::make_shared<CHeroClass>();
  52. heroClass->id = HeroClassID(index);
  53. heroClass->identifier = identifier;
  54. heroClass->modScope = scope;
  55. heroClass->imageBattleFemale = AnimationPath::fromJson(node["animation"]["battle"]["female"]);
  56. heroClass->imageBattleMale = AnimationPath::fromJson(node["animation"]["battle"]["male"]);
  57. //MODS COMPATIBILITY FOR 0.96
  58. heroClass->imageMapFemale = node["animation"]["map"]["female"].String();
  59. heroClass->imageMapMale = node["animation"]["map"]["male"].String();
  60. VLC->generaltexth->registerString(scope, heroClass->getNameTextID(), node["name"].String());
  61. if (vstd::contains(affinityStr, node["affinity"].String()))
  62. {
  63. heroClass->affinity = vstd::find_pos(affinityStr, node["affinity"].String());
  64. }
  65. else
  66. {
  67. logGlobal->error("Mod '%s', hero class '%s': invalid affinity '%s'! Expected 'might' or 'magic'!", scope, identifier, node["affinity"].String());
  68. heroClass->affinity = CHeroClass::MIGHT;
  69. }
  70. fillPrimarySkillData(node, heroClass.get(), PrimarySkill::ATTACK);
  71. fillPrimarySkillData(node, heroClass.get(), PrimarySkill::DEFENSE);
  72. fillPrimarySkillData(node, heroClass.get(), PrimarySkill::SPELL_POWER);
  73. fillPrimarySkillData(node, heroClass.get(), PrimarySkill::KNOWLEDGE);
  74. auto percentSumm = std::accumulate(heroClass->primarySkillLowLevel.begin(), heroClass->primarySkillLowLevel.end(), 0);
  75. if(percentSumm <= 0)
  76. logMod->error("Hero class %s has wrong lowLevelChance values: must be above zero!", heroClass->identifier, percentSumm);
  77. percentSumm = std::accumulate(heroClass->primarySkillHighLevel.begin(), heroClass->primarySkillHighLevel.end(), 0);
  78. if(percentSumm <= 0)
  79. logMod->error("Hero class %s has wrong highLevelChance values: must be above zero!", heroClass->identifier, percentSumm);
  80. for(auto skillPair : node["secondarySkills"].Struct())
  81. {
  82. int probability = static_cast<int>(skillPair.second.Integer());
  83. VLC->identifiers()->requestIdentifier(skillPair.second.getModScope(), "skill", skillPair.first, [heroClass, probability](si32 skillID)
  84. {
  85. heroClass->secSkillProbability[skillID] = probability;
  86. });
  87. }
  88. VLC->identifiers()->requestIdentifier ("creature", node["commander"],
  89. [=](si32 commanderID)
  90. {
  91. heroClass->commander = CreatureID(commanderID);
  92. });
  93. heroClass->defaultTavernChance = static_cast<ui32>(node["defaultTavern"].Float());
  94. for(const auto & tavern : node["tavern"].Struct())
  95. {
  96. int value = static_cast<int>(tavern.second.Float());
  97. VLC->identifiers()->requestIdentifier(tavern.second.getModScope(), "faction", tavern.first,
  98. [=](si32 factionID)
  99. {
  100. heroClass->selectionProbability[FactionID(factionID)] = value;
  101. });
  102. }
  103. VLC->identifiers()->requestIdentifier("faction", node["faction"],
  104. [=](si32 factionID)
  105. {
  106. heroClass->faction.setNum(factionID);
  107. });
  108. VLC->identifiers()->requestIdentifier(scope, "object", "hero", [=](si32 index)
  109. {
  110. JsonNode classConf = node["mapObject"];
  111. classConf["heroClass"].String() = identifier;
  112. if (!node["compatibilityIdentifiers"].isNull())
  113. classConf["compatibilityIdentifiers"] = node["compatibilityIdentifiers"];
  114. classConf.setModScope(scope);
  115. VLC->objtypeh->loadSubObject(identifier, classConf, index, heroClass->getIndex());
  116. });
  117. return heroClass;
  118. }
  119. std::vector<JsonNode> CHeroClassHandler::loadLegacyData()
  120. {
  121. size_t dataSize = VLC->engineSettings()->getInteger(EGameSettings::TEXTS_HERO_CLASS);
  122. objects.resize(dataSize);
  123. std::vector<JsonNode> h3Data;
  124. h3Data.reserve(dataSize);
  125. CLegacyConfigParser parser(TextPath::builtin("DATA/HCTRAITS.TXT"));
  126. parser.endLine(); // header
  127. parser.endLine();
  128. for (size_t i=0; i<dataSize; i++)
  129. {
  130. JsonNode entry;
  131. entry["name"].String() = parser.readString();
  132. parser.readNumber(); // unused aggression
  133. for(const auto & name : NPrimarySkill::names)
  134. entry["primarySkills"][name].Float() = parser.readNumber();
  135. for(const auto & name : NPrimarySkill::names)
  136. entry["lowLevelChance"][name].Float() = parser.readNumber();
  137. for(const auto & name : NPrimarySkill::names)
  138. entry["highLevelChance"][name].Float() = parser.readNumber();
  139. for(const auto & name : NSecondarySkill::names)
  140. entry["secondarySkills"][name].Float() = parser.readNumber();
  141. for(const auto & name : NFaction::names)
  142. entry["tavern"][name].Float() = parser.readNumber();
  143. parser.endLine();
  144. h3Data.push_back(entry);
  145. }
  146. return h3Data;
  147. }
  148. void CHeroClassHandler::afterLoadFinalization()
  149. {
  150. // for each pair <class, town> set selection probability if it was not set before in tavern entries
  151. for(auto & heroClass : objects)
  152. {
  153. for(auto & faction : VLC->townh->objects)
  154. {
  155. if (!faction->town)
  156. continue;
  157. if (heroClass->selectionProbability.count(faction->getId()))
  158. continue;
  159. auto chance = static_cast<float>(heroClass->defaultTavernChance * faction->town->defaultTavernChance);
  160. heroClass->selectionProbability[faction->getId()] = static_cast<int>(sqrt(chance) + 0.5); //FIXME: replace with std::round once MVS supports it
  161. }
  162. // set default probabilities for gaining secondary skills where not loaded previously
  163. for(int skillID = 0; skillID < VLC->skillh->size(); skillID++)
  164. {
  165. if(heroClass->secSkillProbability.count(skillID) == 0)
  166. {
  167. const CSkill * skill = (*VLC->skillh)[SecondarySkill(skillID)];
  168. logMod->trace("%s: no probability for %s, using default", heroClass->identifier, skill->getJsonKey());
  169. heroClass->secSkillProbability[skillID] = skill->gainChance[heroClass->affinity];
  170. }
  171. }
  172. }
  173. for(const auto & hc : objects)
  174. {
  175. if(!hc->imageMapMale.empty())
  176. {
  177. JsonNode templ;
  178. templ["animation"].String() = hc->imageMapMale;
  179. VLC->objtypeh->getHandlerFor(Obj::HERO, hc->getIndex())->addTemplate(templ);
  180. }
  181. }
  182. }
  183. CHeroClassHandler::~CHeroClassHandler() = default;
  184. VCMI_LIB_NAMESPACE_END