|
@@ -10,427 +10,22 @@
|
|
|
#include "StdInc.h"
|
|
|
#include "CHeroHandler.h"
|
|
|
|
|
|
-#include "filesystem/Filesystem.h"
|
|
|
-#include "VCMI_Lib.h"
|
|
|
-#include "constants/StringConstants.h"
|
|
|
-#include "battle/BattleHex.h"
|
|
|
-#include "CCreatureHandler.h"
|
|
|
-#include "IGameSettings.h"
|
|
|
-#include "CSkillHandler.h"
|
|
|
-#include "BattleFieldHandler.h"
|
|
|
-#include "bonuses/Limiters.h"
|
|
|
-#include "bonuses/Updaters.h"
|
|
|
-#include "entities/faction/CFaction.h"
|
|
|
-#include "entities/faction/CTown.h"
|
|
|
-#include "entities/faction/CTownHandler.h"
|
|
|
-#include "json/JsonBonus.h"
|
|
|
-#include "json/JsonUtils.h"
|
|
|
-#include "mapObjectConstructors/AObjectTypeHandler.h"
|
|
|
-#include "mapObjectConstructors/CObjectClassesHandler.h"
|
|
|
-#include "modding/IdentifierStorage.h"
|
|
|
-#include "texts/CGeneralTextHandler.h"
|
|
|
-#include "texts/CLegacyConfigParser.h"
|
|
|
-
|
|
|
-#include <vstd/RNG.h>
|
|
|
+#include "CHero.h"
|
|
|
+
|
|
|
+#include "../../VCMI_Lib.h"
|
|
|
+#include "../../constants/StringConstants.h"
|
|
|
+#include "../../CCreatureHandler.h"
|
|
|
+#include "../../IGameSettings.h"
|
|
|
+#include "../../bonuses/Limiters.h"
|
|
|
+#include "../../bonuses/Updaters.h"
|
|
|
+#include "../../json/JsonBonus.h"
|
|
|
+#include "../../json/JsonUtils.h"
|
|
|
+#include "../../modding/IdentifierStorage.h"
|
|
|
+#include "../../texts/CGeneralTextHandler.h"
|
|
|
+#include "../../texts/CLegacyConfigParser.h"
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
-CHero::CHero() = default;
|
|
|
-CHero::~CHero() = default;
|
|
|
-
|
|
|
-int32_t CHero::getIndex() const
|
|
|
-{
|
|
|
- return ID.getNum();
|
|
|
-}
|
|
|
-
|
|
|
-int32_t CHero::getIconIndex() const
|
|
|
-{
|
|
|
- return imageIndex;
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getJsonKey() const
|
|
|
-{
|
|
|
- return modScope + ':' + identifier;
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getModScope() const
|
|
|
-{
|
|
|
- return modScope;
|
|
|
-}
|
|
|
-
|
|
|
-HeroTypeID CHero::getId() const
|
|
|
-{
|
|
|
- return ID;
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getNameTranslated() const
|
|
|
-{
|
|
|
- return VLC->generaltexth->translate(getNameTextID());
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getBiographyTranslated() const
|
|
|
-{
|
|
|
- return VLC->generaltexth->translate(getBiographyTextID());
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getSpecialtyNameTranslated() const
|
|
|
-{
|
|
|
- return VLC->generaltexth->translate(getSpecialtyNameTextID());
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getSpecialtyDescriptionTranslated() const
|
|
|
-{
|
|
|
- return VLC->generaltexth->translate(getSpecialtyDescriptionTextID());
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getSpecialtyTooltipTranslated() const
|
|
|
-{
|
|
|
- return VLC->generaltexth->translate(getSpecialtyTooltipTextID());
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getNameTextID() const
|
|
|
-{
|
|
|
- return TextIdentifier("hero", modScope, identifier, "name").get();
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getBiographyTextID() const
|
|
|
-{
|
|
|
- return TextIdentifier("hero", modScope, identifier, "biography").get();
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getSpecialtyNameTextID() const
|
|
|
-{
|
|
|
- return TextIdentifier("hero", modScope, identifier, "specialty", "name").get();
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getSpecialtyDescriptionTextID() const
|
|
|
-{
|
|
|
- return TextIdentifier("hero", modScope, identifier, "specialty", "description").get();
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHero::getSpecialtyTooltipTextID() const
|
|
|
-{
|
|
|
- return TextIdentifier("hero", modScope, identifier, "specialty", "tooltip").get();
|
|
|
-}
|
|
|
-
|
|
|
-void CHero::registerIcons(const IconRegistar & cb) const
|
|
|
-{
|
|
|
- cb(getIconIndex(), 0, "UN32", iconSpecSmall);
|
|
|
- cb(getIconIndex(), 0, "UN44", iconSpecLarge);
|
|
|
- cb(getIconIndex(), 0, "PORTRAITSLARGE", portraitLarge);
|
|
|
- cb(getIconIndex(), 0, "PORTRAITSSMALL", portraitSmall);
|
|
|
-}
|
|
|
-
|
|
|
-void CHero::updateFrom(const JsonNode & data)
|
|
|
-{
|
|
|
- //todo: CHero::updateFrom
|
|
|
-}
|
|
|
-
|
|
|
-void CHero::serializeJson(JsonSerializeFormat & handler)
|
|
|
-{
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-SecondarySkill CHeroClass::chooseSecSkill(const std::set<SecondarySkill> & possibles, vstd::RNG & rand) const //picks secondary skill out from given possibilities
|
|
|
-{
|
|
|
- assert(!possibles.empty());
|
|
|
-
|
|
|
- if (possibles.size() == 1)
|
|
|
- return *possibles.begin();
|
|
|
-
|
|
|
- int totalProb = 0;
|
|
|
- for(const auto & possible : possibles)
|
|
|
- if (secSkillProbability.count(possible) != 0)
|
|
|
- totalProb += secSkillProbability.at(possible);
|
|
|
-
|
|
|
- if (totalProb == 0) // may trigger if set contains only banned skills (0 probability)
|
|
|
- return *RandomGeneratorUtil::nextItem(possibles, rand);
|
|
|
-
|
|
|
- auto ran = rand.nextInt(totalProb - 1);
|
|
|
- for(const auto & possible : possibles)
|
|
|
- {
|
|
|
- if (secSkillProbability.count(possible) != 0)
|
|
|
- ran -= secSkillProbability.at(possible);
|
|
|
-
|
|
|
- if(ran < 0)
|
|
|
- return possible;
|
|
|
- }
|
|
|
-
|
|
|
- assert(0); // should not be possible
|
|
|
- return *possibles.begin();
|
|
|
-}
|
|
|
-
|
|
|
-bool CHeroClass::isMagicHero() const
|
|
|
-{
|
|
|
- return affinity == MAGIC;
|
|
|
-}
|
|
|
-
|
|
|
-int CHeroClass::tavernProbability(FactionID targetFaction) const
|
|
|
-{
|
|
|
- auto it = selectionProbability.find(targetFaction);
|
|
|
- if (it != selectionProbability.end())
|
|
|
- return it->second;
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-EAlignment CHeroClass::getAlignment() const
|
|
|
-{
|
|
|
- return VLC->factions()->getById(faction)->getAlignment();
|
|
|
-}
|
|
|
-
|
|
|
-int32_t CHeroClass::getIndex() const
|
|
|
-{
|
|
|
- return id.getNum();
|
|
|
-}
|
|
|
-
|
|
|
-int32_t CHeroClass::getIconIndex() const
|
|
|
-{
|
|
|
- return getIndex();
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHeroClass::getJsonKey() const
|
|
|
-{
|
|
|
- return modScope + ':' + identifier;
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHeroClass::getModScope() const
|
|
|
-{
|
|
|
- return modScope;
|
|
|
-}
|
|
|
-
|
|
|
-HeroClassID CHeroClass::getId() const
|
|
|
-{
|
|
|
- return id;
|
|
|
-}
|
|
|
-
|
|
|
-void CHeroClass::registerIcons(const IconRegistar & cb) const
|
|
|
-{
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHeroClass::getNameTranslated() const
|
|
|
-{
|
|
|
- return VLC->generaltexth->translate(getNameTextID());
|
|
|
-}
|
|
|
-
|
|
|
-std::string CHeroClass::getNameTextID() const
|
|
|
-{
|
|
|
- return TextIdentifier("heroClass", modScope, identifier, "name").get();
|
|
|
-}
|
|
|
-
|
|
|
-void CHeroClass::updateFrom(const JsonNode & data)
|
|
|
-{
|
|
|
- //TODO: CHeroClass::updateFrom
|
|
|
-}
|
|
|
-
|
|
|
-void CHeroClass::serializeJson(JsonSerializeFormat & handler)
|
|
|
-{
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-CHeroClass::CHeroClass():
|
|
|
- faction(0),
|
|
|
- affinity(0),
|
|
|
- defaultTavernChance(0)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-void CHeroClassHandler::fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill pSkill) const
|
|
|
-{
|
|
|
- const auto & skillName = NPrimarySkill::names[pSkill.getNum()];
|
|
|
- auto currentPrimarySkillValue = static_cast<int>(node["primarySkills"][skillName].Integer());
|
|
|
- int primarySkillLegalMinimum = VLC->engineSettings()->getVector(EGameSettings::HEROES_MINIMAL_PRIMARY_SKILLS)[pSkill.getNum()];
|
|
|
-
|
|
|
- if(currentPrimarySkillValue < primarySkillLegalMinimum)
|
|
|
- {
|
|
|
- logMod->error("Hero class '%s' has incorrect initial value '%d' for skill '%s'. Value '%d' will be used instead.",
|
|
|
- heroClass->getNameTranslated(), currentPrimarySkillValue, skillName, primarySkillLegalMinimum);
|
|
|
- currentPrimarySkillValue = primarySkillLegalMinimum;
|
|
|
- }
|
|
|
- heroClass->primarySkillInitial.push_back(currentPrimarySkillValue);
|
|
|
- heroClass->primarySkillLowLevel.push_back(static_cast<int>(node["lowLevelChance"][skillName].Float()));
|
|
|
- heroClass->primarySkillHighLevel.push_back(static_cast<int>(node["highLevelChance"][skillName].Float()));
|
|
|
-}
|
|
|
-
|
|
|
-const std::vector<std::string> & CHeroClassHandler::getTypeNames() const
|
|
|
-{
|
|
|
- static const std::vector<std::string> typeNames = { "heroClass" };
|
|
|
- return typeNames;
|
|
|
-}
|
|
|
-
|
|
|
-std::shared_ptr<CHeroClass> CHeroClassHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index)
|
|
|
-{
|
|
|
- assert(identifier.find(':') == std::string::npos);
|
|
|
- assert(!scope.empty());
|
|
|
-
|
|
|
- std::string affinityStr[2] = { "might", "magic" };
|
|
|
-
|
|
|
- auto heroClass = std::make_shared<CHeroClass>();
|
|
|
-
|
|
|
- heroClass->id = HeroClassID(index);
|
|
|
- heroClass->identifier = identifier;
|
|
|
- heroClass->modScope = scope;
|
|
|
- heroClass->imageBattleFemale = AnimationPath::fromJson(node["animation"]["battle"]["female"]);
|
|
|
- heroClass->imageBattleMale = AnimationPath::fromJson(node["animation"]["battle"]["male"]);
|
|
|
- //MODS COMPATIBILITY FOR 0.96
|
|
|
- heroClass->imageMapFemale = node["animation"]["map"]["female"].String();
|
|
|
- heroClass->imageMapMale = node["animation"]["map"]["male"].String();
|
|
|
-
|
|
|
- VLC->generaltexth->registerString(scope, heroClass->getNameTextID(), node["name"].String());
|
|
|
-
|
|
|
- if (vstd::contains(affinityStr, node["affinity"].String()))
|
|
|
- {
|
|
|
- heroClass->affinity = vstd::find_pos(affinityStr, node["affinity"].String());
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- logGlobal->error("Mod '%s', hero class '%s': invalid affinity '%s'! Expected 'might' or 'magic'!", scope, identifier, node["affinity"].String());
|
|
|
- heroClass->affinity = CHeroClass::MIGHT;
|
|
|
- }
|
|
|
-
|
|
|
- fillPrimarySkillData(node, heroClass.get(), PrimarySkill::ATTACK);
|
|
|
- fillPrimarySkillData(node, heroClass.get(), PrimarySkill::DEFENSE);
|
|
|
- fillPrimarySkillData(node, heroClass.get(), PrimarySkill::SPELL_POWER);
|
|
|
- fillPrimarySkillData(node, heroClass.get(), PrimarySkill::KNOWLEDGE);
|
|
|
-
|
|
|
- auto percentSumm = std::accumulate(heroClass->primarySkillLowLevel.begin(), heroClass->primarySkillLowLevel.end(), 0);
|
|
|
- if(percentSumm <= 0)
|
|
|
- logMod->error("Hero class %s has wrong lowLevelChance values: must be above zero!", heroClass->identifier, percentSumm);
|
|
|
-
|
|
|
- percentSumm = std::accumulate(heroClass->primarySkillHighLevel.begin(), heroClass->primarySkillHighLevel.end(), 0);
|
|
|
- if(percentSumm <= 0)
|
|
|
- logMod->error("Hero class %s has wrong highLevelChance values: must be above zero!", heroClass->identifier, percentSumm);
|
|
|
-
|
|
|
- for(auto skillPair : node["secondarySkills"].Struct())
|
|
|
- {
|
|
|
- int probability = static_cast<int>(skillPair.second.Integer());
|
|
|
- VLC->identifiers()->requestIdentifier(skillPair.second.getModScope(), "skill", skillPair.first, [heroClass, probability](si32 skillID)
|
|
|
- {
|
|
|
- heroClass->secSkillProbability[skillID] = probability;
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- VLC->identifiers()->requestIdentifier ("creature", node["commander"],
|
|
|
- [=](si32 commanderID)
|
|
|
- {
|
|
|
- heroClass->commander = CreatureID(commanderID);
|
|
|
- });
|
|
|
-
|
|
|
- heroClass->defaultTavernChance = static_cast<ui32>(node["defaultTavern"].Float());
|
|
|
- for(const auto & tavern : node["tavern"].Struct())
|
|
|
- {
|
|
|
- int value = static_cast<int>(tavern.second.Float());
|
|
|
-
|
|
|
- VLC->identifiers()->requestIdentifier(tavern.second.getModScope(), "faction", tavern.first,
|
|
|
- [=](si32 factionID)
|
|
|
- {
|
|
|
- heroClass->selectionProbability[FactionID(factionID)] = value;
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- VLC->identifiers()->requestIdentifier("faction", node["faction"],
|
|
|
- [=](si32 factionID)
|
|
|
- {
|
|
|
- heroClass->faction.setNum(factionID);
|
|
|
- });
|
|
|
-
|
|
|
- VLC->identifiers()->requestIdentifier(scope, "object", "hero", [=](si32 index)
|
|
|
- {
|
|
|
- JsonNode classConf = node["mapObject"];
|
|
|
- classConf["heroClass"].String() = identifier;
|
|
|
- if (!node["compatibilityIdentifiers"].isNull())
|
|
|
- classConf["compatibilityIdentifiers"] = node["compatibilityIdentifiers"];
|
|
|
- classConf.setModScope(scope);
|
|
|
- VLC->objtypeh->loadSubObject(identifier, classConf, index, heroClass->getIndex());
|
|
|
- });
|
|
|
-
|
|
|
- return heroClass;
|
|
|
-}
|
|
|
-
|
|
|
-std::vector<JsonNode> CHeroClassHandler::loadLegacyData()
|
|
|
-{
|
|
|
- size_t dataSize = VLC->engineSettings()->getInteger(EGameSettings::TEXTS_HERO_CLASS);
|
|
|
-
|
|
|
- objects.resize(dataSize);
|
|
|
- std::vector<JsonNode> h3Data;
|
|
|
- h3Data.reserve(dataSize);
|
|
|
-
|
|
|
- CLegacyConfigParser parser(TextPath::builtin("DATA/HCTRAITS.TXT"));
|
|
|
-
|
|
|
- parser.endLine(); // header
|
|
|
- parser.endLine();
|
|
|
-
|
|
|
- for (size_t i=0; i<dataSize; i++)
|
|
|
- {
|
|
|
- JsonNode entry;
|
|
|
-
|
|
|
- entry["name"].String() = parser.readString();
|
|
|
-
|
|
|
- parser.readNumber(); // unused aggression
|
|
|
-
|
|
|
- for(const auto & name : NPrimarySkill::names)
|
|
|
- entry["primarySkills"][name].Float() = parser.readNumber();
|
|
|
-
|
|
|
- for(const auto & name : NPrimarySkill::names)
|
|
|
- entry["lowLevelChance"][name].Float() = parser.readNumber();
|
|
|
-
|
|
|
- for(const auto & name : NPrimarySkill::names)
|
|
|
- entry["highLevelChance"][name].Float() = parser.readNumber();
|
|
|
-
|
|
|
- for(const auto & name : NSecondarySkill::names)
|
|
|
- entry["secondarySkills"][name].Float() = parser.readNumber();
|
|
|
-
|
|
|
- for(const auto & name : NFaction::names)
|
|
|
- entry["tavern"][name].Float() = parser.readNumber();
|
|
|
-
|
|
|
- parser.endLine();
|
|
|
- h3Data.push_back(entry);
|
|
|
- }
|
|
|
- return h3Data;
|
|
|
-}
|
|
|
-
|
|
|
-void CHeroClassHandler::afterLoadFinalization()
|
|
|
-{
|
|
|
- // for each pair <class, town> set selection probability if it was not set before in tavern entries
|
|
|
- for(auto & heroClass : objects)
|
|
|
- {
|
|
|
- for(auto & faction : VLC->townh->objects)
|
|
|
- {
|
|
|
- if (!faction->town)
|
|
|
- continue;
|
|
|
- if (heroClass->selectionProbability.count(faction->getId()))
|
|
|
- continue;
|
|
|
-
|
|
|
- auto chance = static_cast<float>(heroClass->defaultTavernChance * faction->town->defaultTavernChance);
|
|
|
- heroClass->selectionProbability[faction->getId()] = static_cast<int>(sqrt(chance) + 0.5); //FIXME: replace with std::round once MVS supports it
|
|
|
- }
|
|
|
-
|
|
|
- // set default probabilities for gaining secondary skills where not loaded previously
|
|
|
- for(int skillID = 0; skillID < VLC->skillh->size(); skillID++)
|
|
|
- {
|
|
|
- if(heroClass->secSkillProbability.count(skillID) == 0)
|
|
|
- {
|
|
|
- const CSkill * skill = (*VLC->skillh)[SecondarySkill(skillID)];
|
|
|
- logMod->trace("%s: no probability for %s, using default", heroClass->identifier, skill->getJsonKey());
|
|
|
- heroClass->secSkillProbability[skillID] = skill->gainChance[heroClass->affinity];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for(const auto & hc : objects)
|
|
|
- {
|
|
|
- if(!hc->imageMapMale.empty())
|
|
|
- {
|
|
|
- JsonNode templ;
|
|
|
- templ["animation"].String() = hc->imageMapMale;
|
|
|
- VLC->objtypeh->getHandlerFor(Obj::HERO, hc->getIndex())->addTemplate(templ);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-CHeroClassHandler::~CHeroClassHandler() = default;
|
|
|
-
|
|
|
CHeroHandler::~CHeroHandler() = default;
|
|
|
|
|
|
CHeroHandler::CHeroHandler()
|