CHeroClassHandler.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "../../GameLibrary.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 = LIBRARY->engineSettings()->getVectorValue(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. LIBRARY->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. LIBRARY->identifiers()->requestIdentifierIfFound(skillPair.second.getModScope(), "skill", skillPair.first, [heroClass, probability](si32 skillID) {
  84. heroClass->secSkillProbability[skillID] = probability;
  85. });
  86. }
  87. LIBRARY->identifiers()->requestIdentifier ("creature", node["commander"], [=](si32 commanderID) {
  88. heroClass->commander = CreatureID(commanderID);
  89. });
  90. heroClass->defaultTavernChance = static_cast<ui32>(node["defaultTavern"].Float());
  91. for(const auto & tavern : node["tavern"].Struct())
  92. {
  93. int value = static_cast<int>(tavern.second.Float());
  94. LIBRARY->identifiers()->requestIdentifierIfFound(tavern.second.getModScope(), "faction", tavern.first, [=](si32 factionID) {
  95. heroClass->selectionProbability[FactionID(factionID)] = value;
  96. });
  97. }
  98. LIBRARY->identifiers()->requestIdentifier("faction", node["faction"], [=](si32 factionID) {
  99. heroClass->faction.setNum(factionID);
  100. });
  101. LIBRARY->identifiers()->requestIdentifier(scope, "object", "hero", [=](si32 index) {
  102. JsonNode classConf = node["mapObject"];
  103. classConf["heroClass"].String() = identifier;
  104. classConf["heroClass"].setModScope(scope);
  105. if (!node["compatibilityIdentifiers"].isNull())
  106. classConf["compatibilityIdentifiers"] = node["compatibilityIdentifiers"];
  107. LIBRARY->objtypeh->loadSubObject(identifier, classConf, index, heroClass->getIndex());
  108. });
  109. return heroClass;
  110. }
  111. std::vector<JsonNode> CHeroClassHandler::loadLegacyData()
  112. {
  113. size_t dataSize = LIBRARY->engineSettings()->getInteger(EGameSettings::TEXTS_HERO_CLASS);
  114. objects.resize(dataSize);
  115. std::vector<JsonNode> h3Data;
  116. h3Data.reserve(dataSize);
  117. CLegacyConfigParser parser(TextPath::builtin("DATA/HCTRAITS.TXT"));
  118. parser.endLine(); // header
  119. parser.endLine();
  120. for (size_t i=0; i<dataSize; i++)
  121. {
  122. JsonNode entry;
  123. entry["name"].String() = parser.readString();
  124. parser.readNumber(); // unused aggression
  125. for(const auto & name : NPrimarySkill::names)
  126. entry["primarySkills"][name].Float() = parser.readNumber();
  127. for(const auto & name : NPrimarySkill::names)
  128. entry["lowLevelChance"][name].Float() = parser.readNumber();
  129. for(const auto & name : NPrimarySkill::names)
  130. entry["highLevelChance"][name].Float() = parser.readNumber();
  131. for(const auto & name : NSecondarySkill::names)
  132. entry["secondarySkills"][name].Float() = parser.readNumber();
  133. for(const auto & name : NFaction::names)
  134. entry["tavern"][name].Float() = parser.readNumber();
  135. parser.endLine();
  136. h3Data.push_back(entry);
  137. }
  138. return h3Data;
  139. }
  140. void CHeroClassHandler::afterLoadFinalization()
  141. {
  142. // for each pair <class, town> set selection probability if it was not set before in tavern entries
  143. for(auto & heroClass : objects)
  144. {
  145. for(auto & faction : LIBRARY->townh->objects)
  146. {
  147. if (!faction->town)
  148. continue;
  149. if (heroClass->selectionProbability.count(faction->getId()))
  150. continue;
  151. auto chance = static_cast<float>(heroClass->defaultTavernChance * faction->town->defaultTavernChance);
  152. heroClass->selectionProbability[faction->getId()] = static_cast<int>(sqrt(chance) + 0.5); //FIXME: replace with std::round once MVS supports it
  153. }
  154. // set default probabilities for gaining secondary skills where not loaded previously
  155. for(int skillID = 0; skillID < LIBRARY->skillh->size(); skillID++)
  156. {
  157. if(heroClass->secSkillProbability.count(skillID) == 0)
  158. {
  159. const CSkill * skill = (*LIBRARY->skillh)[SecondarySkill(skillID)];
  160. logMod->trace("%s: no probability for %s, using default", heroClass->identifier, skill->getJsonKey());
  161. heroClass->secSkillProbability[skillID] = skill->gainChance[heroClass->affinity];
  162. }
  163. }
  164. }
  165. for(const auto & hc : objects)
  166. {
  167. if(!hc->imageMapMale.empty())
  168. {
  169. JsonNode templ;
  170. templ["animation"].String() = hc->imageMapMale;
  171. LIBRARY->objtypeh->getHandlerFor(Obj::HERO, hc->getIndex())->addTemplate(templ);
  172. }
  173. }
  174. }
  175. CHeroClassHandler::~CHeroClassHandler() = default;
  176. VCMI_LIB_NAMESPACE_END