1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
* 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 "constants/StringConstants.h"
#include "filesystem/Filesystem.h"
#include "json/JsonBonus.h"
#include "json/JsonUtils.h"
#include "modding/IdentifierStorage.h"
#include "modding/ModUtility.h"
#include "modding/ModScope.h"
#include "texts/CGeneralTextHandler.h"
#include "texts/CLegacyConfigParser.h"
#include "texts/TextOperations.h"
#include "VCMI_Lib.h"
VCMI_LIB_NAMESPACE_BEGIN
CSkill::CSkill(const SecondarySkill & id, std::string identifier, bool obligatoryMajor, bool obligatoryMinor):
id(id),
identifier(std::move(identifier)),
obligatoryMajor(obligatoryMajor),
obligatoryMinor(obligatoryMinor),
special(false),
onlyOnWaterMap(false)
{
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() * 3 + 3; // Base master level
}
int32_t CSkill::getIconIndex(uint8_t skillMasterLevel) const
{
return getIconIndex() + skillMasterLevel;
}
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
{
return modScope + ':' + identifier;
}
std::string CSkill::getModScope() const
{
return modScope;
}
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++)
{
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());
b->description.appendRawString(" %+d");
levels[level-1].effects.push_back(b);
}
const CSkill::LevelInfo & CSkill::at(int level) const
{
assert(1 <= level && level < NSecondarySkill::levels.size());
return levels[level - 1];
}
CSkill::LevelInfo & CSkill::at(int level)
{
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(nullptr);
return out << "])";
}
DLL_LINKAGE std::ostream & operator<<(std::ostream & out, const CSkill & skill)
{
out << "Skill(" << skill.id.getNum() << "," << skill.identifier << "): [";
for(int i=0; i < skill.levels.size(); i++)
out << (i ? "," : "") << skill.levels[i];
return out << "]";
}
std::string CSkill::toString() const
{
std::ostringstream ss;
ss << *this;
return ss.str();
}
void CSkill::updateFrom(const JsonNode & data)
{
}
void CSkill::serializeJson(JsonSerializeFormat & handler)
{
}
///CSkillHandler
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());
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;
}
std::shared_ptr<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();
auto skill = std::make_shared<CSkill>(SecondarySkill(index), identifier, major, minor);
skill->modScope = scope;
skill->onlyOnWaterMap = json["onlyOnWaterMap"].Bool();
skill->special = json["special"].Bool();
VLC->generaltexth->registerString(scope, skill->getNameTextID(), json["name"]);
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
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"]);
skillAtLevel.iconSmall = levelNode["images"]["small"].String();
skillAtLevel.iconMedium = levelNode["images"]["medium"].String();
skillAtLevel.iconLarge = levelNode["images"]["large"].String();
}
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)
if (!skill->special)
result.insert(skill->getId());
return result;
}
VCMI_LIB_NAMESPACE_END
|