2023-10-19 16:19:09 +02:00
|
|
|
/*
|
|
|
|
* CBonusTypeHandler.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"
|
|
|
|
|
|
|
|
#define INSTANTIATE_CBonusTypeHandler_HERE
|
|
|
|
|
|
|
|
#include "CBonusTypeHandler.h"
|
|
|
|
|
|
|
|
#include "filesystem/Filesystem.h"
|
|
|
|
|
|
|
|
#include "CCreatureHandler.h"
|
2024-10-30 10:51:02 +00:00
|
|
|
#include "GameConstants.h"
|
2025-02-14 16:23:37 +00:00
|
|
|
#include "GameLibrary.h"
|
2024-10-30 10:51:02 +00:00
|
|
|
#include "modding/ModScope.h"
|
2025-02-11 22:08:45 +00:00
|
|
|
#include "modding/IdentifierStorage.h"
|
2024-10-30 10:51:02 +00:00
|
|
|
#include "spells/CSpellHandler.h"
|
2024-07-20 12:55:17 +00:00
|
|
|
#include "texts/CGeneralTextHandler.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "json/JsonUtils.h"
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
///CBonusType
|
|
|
|
|
|
|
|
std::string CBonusType::getDescriptionTextID() const
|
|
|
|
{
|
|
|
|
return TextIdentifier( "core", "bonus", identifier, "description").get();
|
|
|
|
}
|
|
|
|
|
|
|
|
///CBonusTypeHandler
|
|
|
|
|
|
|
|
CBonusTypeHandler::CBonusTypeHandler()
|
|
|
|
{
|
|
|
|
//register predefined bonus types
|
|
|
|
|
2025-06-09 11:40:21 +03:00
|
|
|
#define BONUS_NAME(x) { #x },
|
2025-06-13 16:36:24 +03:00
|
|
|
builtinBonusNames = {
|
2025-06-09 11:40:21 +03:00
|
|
|
BONUS_LIST
|
|
|
|
};
|
|
|
|
#undef BONUS_NAME
|
|
|
|
|
2025-06-13 16:36:24 +03:00
|
|
|
for (int i = 0; i < builtinBonusNames.size(); ++i)
|
|
|
|
bonusTypes.push_back(std::make_shared<CBonusType>());
|
2025-06-13 13:29:31 +03:00
|
|
|
|
2025-06-13 16:36:24 +03:00
|
|
|
for (int i = 0; i < builtinBonusNames.size(); ++i)
|
|
|
|
registerObject(ModScope::scopeBuiltin(), "bonus", builtinBonusNames[i], i);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2025-02-11 22:08:45 +00:00
|
|
|
CBonusTypeHandler::~CBonusTypeHandler() = default;
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-05-23 17:12:51 +03:00
|
|
|
std::string CBonusTypeHandler::bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer) const
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-06-13 16:36:24 +03:00
|
|
|
const CBonusType & bt = *bonusTypes.at(vstd::to_underlying(bonus->type));
|
2025-05-23 19:19:03 +03:00
|
|
|
int bonusValue = bearer->valOfBonuses(bonus->type, bonus->subtype);
|
2023-10-19 16:19:09 +02:00
|
|
|
if(bt.hidden)
|
|
|
|
return "";
|
|
|
|
|
2025-05-23 17:12:51 +03:00
|
|
|
std::string textID = bt.getDescriptionTextID();
|
2025-02-14 16:23:37 +00:00
|
|
|
std::string text = LIBRARY->generaltexth->translate(textID);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-05-23 19:19:03 +03:00
|
|
|
auto subtype = bonus->subtype.getNum();
|
|
|
|
if (bt.subtypeDescriptions.count(subtype))
|
2024-12-24 14:05:21 +01:00
|
|
|
{
|
2025-05-23 19:19:03 +03:00
|
|
|
std::string fullTextID = textID + '.' + bt.subtypeDescriptions.at(subtype);
|
|
|
|
text = LIBRARY->generaltexth->translate(fullTextID);
|
|
|
|
}
|
|
|
|
else if (bt.valueDescriptions.count(bonusValue))
|
|
|
|
{
|
|
|
|
std::string fullTextID = textID + '.' + bt.valueDescriptions.at(bonusValue);
|
2025-02-14 16:23:37 +00:00
|
|
|
text = LIBRARY->generaltexth->translate(fullTextID);
|
2024-12-24 14:05:21 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
if (text.find("${val}") != std::string::npos)
|
2025-05-23 19:19:03 +03:00
|
|
|
boost::algorithm::replace_all(text, "${val}", std::to_string(bonusValue));
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-01-24 13:43:35 +02:00
|
|
|
if (text.find("${subtype.creature}") != std::string::npos && bonus->subtype.as<CreatureID>().hasValue())
|
2023-10-22 18:36:41 +03:00
|
|
|
boost::algorithm::replace_all(text, "${subtype.creature}", bonus->subtype.as<CreatureID>().toCreature()->getNamePluralTranslated());
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2024-01-24 13:43:35 +02:00
|
|
|
if (text.find("${subtype.spell}") != std::string::npos && bonus->subtype.as<SpellID>().hasValue())
|
2023-10-22 18:36:41 +03:00
|
|
|
boost::algorithm::replace_all(text, "${subtype.spell}", bonus->subtype.as<SpellID>().toSpell()->getNameTranslated());
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImagePath CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
|
|
|
|
{
|
2025-06-13 16:36:24 +03:00
|
|
|
const CBonusType & bt = *bonusTypes.at(vstd::to_underlying(bonus->type));
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-04-10 22:24:22 +03:00
|
|
|
if (bonus->type == BonusType::SPELL_IMMUNITY && bonus->subtype.as<SpellID>().hasValue())
|
|
|
|
{
|
|
|
|
const CSpell * sp = bonus->subtype.as<SpellID>().toSpell();
|
|
|
|
return sp->getIconImmune();
|
|
|
|
}
|
|
|
|
|
2025-02-11 22:08:45 +00:00
|
|
|
if (bt.subtypeIcons.count(bonus->subtype.getNum()))
|
|
|
|
return bt.subtypeIcons.at(bonus->subtype.getNum());
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-02-11 22:08:45 +00:00
|
|
|
if (bt.valueIcons.count(bonus->val))
|
|
|
|
return bt.valueIcons.at(bonus->val);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-02-11 22:08:45 +00:00
|
|
|
return bt.icon;
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-02-11 22:08:45 +00:00
|
|
|
std::vector<JsonNode> CBonusTypeHandler::loadLegacyData()
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
|
2025-02-11 22:08:45 +00:00
|
|
|
void CBonusTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
|
|
|
|
{
|
2025-06-13 16:36:24 +03:00
|
|
|
if (vstd::contains(builtinBonusNames, name))
|
2025-06-09 12:53:44 +03:00
|
|
|
{
|
|
|
|
//h3 bonus
|
2025-06-13 16:36:24 +03:00
|
|
|
BonusType bonus = static_cast<BonusType>(vstd::find_pos(builtinBonusNames, name));
|
|
|
|
CBonusType & bt =*bonusTypes.at(vstd::to_underlying(bonus));
|
2025-06-09 12:53:44 +03:00
|
|
|
loadItem(data, bt, name);
|
|
|
|
logBonus->trace("Loaded bonus type %s", name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// new bonus
|
2025-06-13 16:36:24 +03:00
|
|
|
registerObject(scope, "bonus", name, bonusTypes.size());
|
|
|
|
bonusTypes.push_back(std::make_shared<CBonusType>());
|
|
|
|
loadItem(data, *bonusTypes.back(), name);
|
2025-06-09 12:53:44 +03:00
|
|
|
logBonus->trace("New bonus type %s", name);
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2025-02-11 22:08:45 +00:00
|
|
|
void CBonusTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
|
2023-10-19 16:19:09 +02:00
|
|
|
{
|
2025-02-11 22:08:45 +00:00
|
|
|
assert(0);
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
|
|
|
|
{
|
|
|
|
dest.identifier = name;
|
|
|
|
dest.hidden = source["hidden"].Bool(); //Null -> false
|
2025-06-09 12:53:44 +03:00
|
|
|
dest.creatureNature = source["creatureNature"].Bool(); //Null -> false
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
if (!dest.hidden)
|
2025-02-14 16:23:37 +00:00
|
|
|
LIBRARY->generaltexth->registerString( "vcmi", dest.getDescriptionTextID(), source["description"]);
|
2023-10-19 16:19:09 +02:00
|
|
|
|
|
|
|
const JsonNode & graphics = source["graphics"];
|
|
|
|
|
|
|
|
if(!graphics.isNull())
|
2025-02-11 22:08:45 +00:00
|
|
|
dest.icon = ImagePath::fromJson(graphics["icon"]);
|
|
|
|
|
|
|
|
for (const auto & additionalIcon : graphics["subtypeIcons"].Struct())
|
|
|
|
{
|
|
|
|
auto path = ImagePath::fromJson(additionalIcon.second);
|
2025-02-28 14:59:00 +00:00
|
|
|
LIBRARY->identifiers()->requestIdentifier(additionalIcon.second.getModScope(), additionalIcon.first, [&dest, path](int32_t index)
|
2025-02-11 22:08:45 +00:00
|
|
|
{
|
|
|
|
dest.subtypeIcons[index] = path;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & additionalIcon : graphics["valueIcons"].Struct())
|
|
|
|
{
|
|
|
|
auto path = ImagePath::fromJson(additionalIcon.second);
|
|
|
|
int value = std::stoi(additionalIcon.first);
|
|
|
|
dest.valueIcons[value] = path;
|
|
|
|
}
|
2025-05-23 19:19:03 +03:00
|
|
|
|
|
|
|
for (const auto & additionalDescription : source["subtypeDescriptions"].Struct())
|
|
|
|
{
|
|
|
|
LIBRARY->generaltexth->registerString( "vcmi", dest.getDescriptionTextID() + "." + additionalDescription.first, additionalDescription.second);
|
|
|
|
auto stringID = additionalDescription.first;
|
|
|
|
LIBRARY->identifiers()->requestIdentifier(additionalDescription.second.getModScope(), additionalDescription.first, [&dest, stringID](int32_t index)
|
|
|
|
{
|
|
|
|
dest.subtypeDescriptions[index] = stringID;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto & additionalDescription : source["valueDescriptions"].Struct())
|
|
|
|
{
|
|
|
|
LIBRARY->generaltexth->registerString( "vcmi", dest.getDescriptionTextID() + "." + additionalDescription.first, additionalDescription.second);
|
|
|
|
auto stringID = additionalDescription.first;
|
|
|
|
int value = std::stoi(additionalDescription.first);
|
|
|
|
dest.valueDescriptions[value] = stringID;
|
|
|
|
}
|
2023-10-19 16:19:09 +02:00
|
|
|
}
|
|
|
|
|
2025-06-09 20:41:16 +03:00
|
|
|
const std::string & CBonusTypeHandler::bonusToString(BonusType bonus) const
|
2025-06-09 11:40:21 +03:00
|
|
|
{
|
2025-06-13 16:36:24 +03:00
|
|
|
return bonusTypes.at(static_cast<int>(bonus))->identifier;
|
2025-06-09 11:40:21 +03:00
|
|
|
}
|
|
|
|
|
2025-06-09 12:53:44 +03:00
|
|
|
bool CBonusTypeHandler::isCreatureNatureBonus(BonusType bonus) const
|
|
|
|
{
|
2025-06-13 16:36:24 +03:00
|
|
|
return bonusTypes.at(static_cast<int>(bonus))->creatureNature;
|
2025-06-09 12:53:44 +03:00
|
|
|
}
|
|
|
|
|
2025-06-09 11:40:21 +03:00
|
|
|
std::vector<BonusType> CBonusTypeHandler::getAllObjets() const
|
|
|
|
{
|
|
|
|
std::vector<BonusType> ret;
|
2025-06-13 16:36:24 +03:00
|
|
|
for (int i = 0; i < bonusTypes.size(); ++i)
|
2025-06-09 11:40:21 +03:00
|
|
|
ret.push_back(static_cast<BonusType>(i));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:19:09 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|