1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/lib/CSkillHandler.cpp

271 lines
7.1 KiB
C++
Raw Normal View History

/*
* CSkillHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include <cctype>
#include "CSkillHandler.h"
#include "CGeneralTextHandler.h"
#include "filesystem/Filesystem.h"
2024-02-14 15:48:06 +02:00
#include "json/JsonBonus.h"
#include "json/JsonUtils.h"
#include "modding/IdentifierStorage.h"
#include "modding/ModUtility.h"
#include "modding/ModScope.h"
#include "constants/StringConstants.h"
VCMI_LIB_NAMESPACE_BEGIN
2023-03-13 23:26:44 +02:00
CSkill::CSkill(const SecondarySkill & id, std::string identifier, bool obligatoryMajor, bool obligatoryMinor):
id(id),
identifier(std::move(identifier)),
obligatoryMajor(obligatoryMajor),
obligatoryMinor(obligatoryMinor)
{
gainChance[0] = gainChance[1] = 0; //affects CHeroClassHandler::afterLoadFinalization()
levels.resize(NSecondarySkill::levels.size() - 1);
}
int32_t CSkill::getIndex() const
{
return id.num;
}
int32_t CSkill::getIconIndex() const
{
return getIndex(); //TODO: actual value with skill level
}
std::string CSkill::getNameTextID() const
{
TextIdentifier id("skill", modScope, identifier, "name");
return id.get();
}
std::string CSkill::getNameTranslated() const
{
return VLC->generaltexth->translate(getNameTextID());
}
std::string CSkill::getJsonKey() const
{
2024-01-16 21:02:39 +02:00
return modScope + ':' + identifier;
}
std::string CSkill::getDescriptionTextID(int level) const
{
TextIdentifier id("skill", modScope, identifier, "description", NSecondarySkill::levels[level]);
return id.get();
}
std::string CSkill::getDescriptionTranslated(int level) const
{
return VLC->generaltexth->translate(getDescriptionTextID(level));
}
void CSkill::registerIcons(const IconRegistar & cb) const
{
for(int level = 1; level <= 3; level++)
{
2023-11-05 19:13:18 +02:00
int frame = 2 + level + 3 * id.getNum();
const LevelInfo & skillAtLevel = at(level);
cb(frame, 0, "SECSK32", skillAtLevel.iconSmall);
cb(frame, 0, "SECSKILL", skillAtLevel.iconMedium);
cb(frame, 0, "SECSK82", skillAtLevel.iconLarge);
}
}
SecondarySkill CSkill::getId() const
{
return id;
}
void CSkill::addNewBonus(const std::shared_ptr<Bonus> & b, int level)
{
b->source = BonusSource::SECONDARY_SKILL;
b->sid = BonusSourceID(id);
b->duration = BonusDuration::PERMANENT;
b->description.appendTextID(getNameTextID());
2024-04-08 17:21:24 +02:00
b->description.appendRawString(" %+d");
levels[level-1].effects.push_back(b);
2017-08-25 03:35:02 +02:00
}
const CSkill::LevelInfo & CSkill::at(int level) const
2017-08-25 03:35:02 +02:00
{
assert(1 <= level && level < NSecondarySkill::levels.size());
return levels[level - 1];
2017-08-25 03:35:02 +02:00
}
CSkill::LevelInfo & CSkill::at(int level)
2017-08-25 03:35:02 +02:00
{
assert(1 <= level && level < NSecondarySkill::levels.size());
return levels[level - 1];
}
DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill::LevelInfo & info)
{
for(int i=0; i < info.effects.size(); i++)
out << (i ? "," : "") << info.effects[i]->Description();
return out << "])";
}
DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill)
2017-08-23 00:19:30 +02:00
{
2023-11-02 17:48:48 +02:00
out << "Skill(" << skill.id.getNum() << "," << skill.identifier << "): [";
for(int i=0; i < skill.levels.size(); i++)
out << (i ? "," : "") << skill.levels[i];
return out << "]";
2017-08-26 04:06:47 +02:00
}
std::string CSkill::toString() const
{
std::ostringstream ss;
ss << *this;
return ss.str();
2017-08-23 00:19:30 +02:00
}
void CSkill::updateFrom(const JsonNode & data)
{
}
void CSkill::serializeJson(JsonSerializeFormat & handler)
{
}
///CSkillHandler
2023-03-15 21:34:29 +02:00
std::vector<JsonNode> CSkillHandler::loadLegacyData()
{
CLegacyConfigParser parser(TextPath::builtin("DATA/SSTRAITS.TXT"));
//skip header
parser.endLine();
parser.endLine();
std::vector<std::string> skillNames;
std::vector<std::vector<std::string>> skillInfoTexts;
do
{
skillNames.push_back(parser.readString());
2023-03-13 23:26:44 +02:00
skillInfoTexts.emplace_back();
for(int i = 0; i < 3; i++)
skillInfoTexts.back().push_back(parser.readString());
}
while (parser.endLine());
assert(skillNames.size() == GameConstants::SKILL_QUANTITY);
//store & construct JSON
std::vector<JsonNode> legacyData;
for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
{
JsonNode skillNode;
skillNode["name"].String() = skillNames[id];
for(int level = 1; level < NSecondarySkill::levels.size(); level++)
{
std::string & desc = skillInfoTexts[id][level-1];
auto & levelNode = skillNode[NSecondarySkill::levels[level]].Struct();
levelNode["description"].String() = desc;
levelNode["effects"].Struct(); // create empty effects objects
}
legacyData.push_back(skillNode);
}
objects.resize(legacyData.size());
return legacyData;
}
const std::vector<std::string> & CSkillHandler::getTypeNames() const
{
static const std::vector<std::string> typeNames = { "skill", "secondarySkill" };
return typeNames;
}
CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index)
{
assert(identifier.find(':') == std::string::npos);
assert(!scope.empty());
bool major;
bool minor;
major = json["obligatoryMajor"].Bool();
minor = json["obligatoryMinor"].Bool();
2023-03-13 23:26:44 +02:00
auto * skill = new CSkill(SecondarySkill((si32)index), identifier, major, minor);
skill->modScope = scope;
2023-07-18 10:54:36 +02:00
skill->onlyOnWaterMap = json["onlyOnWaterMap"].Bool();
VLC->generaltexth->registerString(scope, skill->getNameTextID(), json["name"].String());
switch(json["gainChance"].getType())
{
case JsonNode::JsonType::DATA_INTEGER:
skill->gainChance[0] = static_cast<si32>(json["gainChance"].Integer());
skill->gainChance[1] = static_cast<si32>(json["gainChance"].Integer());
break;
case JsonNode::JsonType::DATA_STRUCT:
skill->gainChance[0] = static_cast<si32>(json["gainChance"]["might"].Integer());
skill->gainChance[1] = static_cast<si32>(json["gainChance"]["magic"].Integer());
break;
default:
break;
}
for(int level = 1; level < NSecondarySkill::levels.size(); level++)
{
const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert
const JsonNode & levelNode = json[levelName];
// parse bonus effects
2023-03-13 23:26:44 +02:00
for(const auto & b : levelNode["effects"].Struct())
{
auto bonus = JsonUtils::parseBonus(b.second);
skill->addNewBonus(bonus, level);
}
CSkill::LevelInfo & skillAtLevel = skill->at(level);
VLC->generaltexth->registerString(scope, skill->getDescriptionTextID(level), levelNode["description"].String());
skillAtLevel.iconSmall = levelNode["images"]["small"].String();
skillAtLevel.iconMedium = levelNode["images"]["medium"].String();
skillAtLevel.iconLarge = levelNode["images"]["large"].String();
}
2023-11-02 17:48:48 +02:00
logMod->debug("loaded secondary skill %s(%d)", identifier, skill->id.getNum());
return skill;
}
void CSkillHandler::afterLoadFinalization()
{
}
void CSkillHandler::beforeValidate(JsonNode & object)
{
//handle "base" level info
JsonNode & base = object["base"];
auto inheritNode = [&](const std::string & name){
JsonUtils::inherit(object[name], base);
};
inheritNode("basic");
inheritNode("advanced");
inheritNode("expert");
}
std::set<SecondarySkill> CSkillHandler::getDefaultAllowed() const
{
std::set<SecondarySkill> result;
for (auto const & skill : objects)
result.insert(skill->getId());
return result;
}
VCMI_LIB_NAMESPACE_END