1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/CBonusTypeHandler.cpp

250 lines
6.1 KiB
C++
Raw Normal View History

/*
* 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"
2017-08-08 16:53:01 +02:00
#define INSTANTIATE_CBonusTypeHandler_HERE
#include "CBonusTypeHandler.h"
#include "JsonNode.h"
#include "filesystem/Filesystem.h"
2014-01-19 16:29:13 +03:00
#include "GameConstants.h"
#include "CCreatureHandler.h"
2023-01-17 12:42:43 +02:00
#include "CGeneralTextHandler.h"
#include "spells/CSpellHandler.h"
template class std::vector<VCMI_LIB_WRAP_NAMESPACE(CBonusType)>;
VCMI_LIB_NAMESPACE_BEGIN
2017-08-08 16:53:01 +02:00
///CBonusType
2023-01-17 12:42:43 +02:00
CBonusType::CBonusType():
hidden(true)
{}
2023-01-17 12:42:43 +02:00
std::string CBonusType::getNameTextID() const
{
2023-01-17 12:42:43 +02:00
return TextIdentifier( "core", "bonus", identifier, "name").get();
}
2023-01-17 12:42:43 +02:00
std::string CBonusType::getDescriptionTextID() const
{
2023-01-17 12:42:43 +02:00
return TextIdentifier( "core", "bonus", identifier, "description").get();
}
///CBonusTypeHandler
CBonusTypeHandler::CBonusTypeHandler()
{
//register predefined bonus types
#define BONUS_NAME(x) \
2017-08-08 16:53:01 +02:00
do { \
bonusTypes.push_back(CBonusType()); \
} while(0);
BONUS_LIST;
#undef BONUS_NAME
load();
}
CBonusTypeHandler::~CBonusTypeHandler()
{
//dtor
}
2017-08-08 16:53:01 +02:00
std::string CBonusTypeHandler::bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer, bool description) const
{
const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
2014-12-25 23:02:50 +02:00
if(bt.hidden)
return "";
2017-08-08 16:53:01 +02:00
2023-01-17 12:42:43 +02:00
std::string textID = description ? bt.getDescriptionTextID() : bt.getNameTextID();
std::string text = VLC->generaltexth->translate(textID);
if (text.find("${val}") != std::string::npos)
boost::algorithm::replace_all(text, "${val}", std::to_string(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
if (text.find("${subtype.creature}") != std::string::npos)
boost::algorithm::replace_all(text, "${subtype.creature}", CreatureID(bonus->subtype).toCreature()->getNamePluralTranslated());
if (text.find("${subtype.spell}") != std::string::npos)
boost::algorithm::replace_all(text, "${subtype.spell}", SpellID(bonus->subtype).toSpell()->getNameTranslated());
return text;
}
ImagePath CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
{
std::string fileName;
bool fullPath = false;
2017-08-08 16:53:01 +02:00
switch(bonus->type)
{
case BonusType::SPELL_IMMUNITY:
2017-08-08 16:53:01 +02:00
{
fullPath = true;
const CSpell * sp = SpellID(bonus->subtype).toSpell();
fileName = sp->getIconImmune();
break;
}
case BonusType::SPELL_DAMAGE_REDUCTION: //Spell damage reduction for all schools
{
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::ANY.getNum())
fileName = "E_GOLEM.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::AIR.getNum())
fileName = "E_LIGHT.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::FIRE.getNum())
fileName = "E_FIRE.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::WATER.getNum())
fileName = "E_COLD.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::EARTH.getNum())
fileName = "E_SPEATH1.bmp"; //No separate icon for earth damage
2023-08-20 19:09:32 +02:00
break;
}
case BonusType::SPELL_SCHOOL_IMMUNITY: //for all school
{
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::AIR.getNum())
fileName = "E_SPAIR.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::FIRE.getNum())
2017-08-08 16:53:01 +02:00
fileName = "E_SPFIRE.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::WATER.getNum())
2017-08-08 16:53:01 +02:00
fileName = "E_SPWATER.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::EARTH.getNum())
fileName = "E_SPEATH.bmp";
2023-08-20 19:09:32 +02:00
2017-08-08 16:53:01 +02:00
break;
}
case BonusType::NEGATIVE_EFFECTS_IMMUNITY:
{
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::AIR.getNum())
2017-08-08 16:53:01 +02:00
fileName = "E_SPAIR1.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::FIRE.getNum())
fileName = "E_SPFIRE1.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::WATER.getNum())
fileName = "E_SPWATER1.bmp";
2023-08-20 19:09:32 +02:00
if (bonus->subtype == SpellSchool::EARTH.getNum())
2017-08-08 16:53:01 +02:00
fileName = "E_SPEATH1.bmp";
2023-08-20 19:09:32 +02:00
2017-08-08 16:53:01 +02:00
break;
}
case BonusType::LEVEL_SPELL_IMMUNITY:
2017-08-08 16:53:01 +02:00
{
if(vstd::iswithin(bonus->val, 1, 5))
{
2023-03-09 15:36:46 +02:00
fileName = "E_SPLVL" + std::to_string(bonus->val) + ".bmp";
}
2017-08-08 16:53:01 +02:00
break;
}
case BonusType::KING:
{
if(vstd::iswithin(bonus->val, 0, 3))
{
fileName = "E_KING" + std::to_string(std::max(1, bonus->val)) + ".bmp";
}
break;
}
case BonusType::GENERAL_DAMAGE_REDUCTION:
2017-08-08 16:53:01 +02:00
{
switch(bonus->subtype)
{
2017-08-08 16:53:01 +02:00
case 0:
fileName = "DamageReductionMelee.bmp";
break;
case 1:
fileName = "DamageReductionRanged.bmp";
break;
}
break;
}
2017-08-08 16:53:01 +02:00
default:
{
const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
fileName = bt.icon;
fullPath = true;
2017-08-08 16:53:01 +02:00
}
break;
}
2017-08-08 16:53:01 +02:00
if(!fileName.empty() && !fullPath)
fileName = "zvs/Lib1.res/" + fileName;
return ImagePath::builtinTODO(fileName);
}
void CBonusTypeHandler::load()
{
const JsonNode gameConf(JsonPath::builtin("config/gameConfig.json"));
2017-08-08 16:53:01 +02:00
const JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"].convertTo<std::vector<std::string>>()));
load(config);
}
2017-08-08 16:53:01 +02:00
void CBonusTypeHandler::load(const JsonNode & config)
{
2023-03-13 23:26:44 +02:00
for(const auto & node : config.Struct())
{
auto it = bonusNameMap.find(node.first);
2017-08-08 16:53:01 +02:00
if(it == bonusNameMap.end())
{
//TODO: new bonus
// CBonusType bt;
// loadItem(node.second, bt);
2017-08-08 16:53:01 +02:00
//
// auto new_id = bonusTypes.size();
2017-08-08 16:53:01 +02:00
//
// bonusTypes.push_back(bt);
2017-08-08 16:53:01 +02:00
2023-01-17 12:42:43 +02:00
logBonus->warn("Unrecognized bonus name! (%s)", node.first);
}
else
{
CBonusType & bt = bonusTypes[vstd::to_underlying(it->second)];
2017-08-08 16:53:01 +02:00
2023-01-17 12:42:43 +02:00
loadItem(node.second, bt, node.first);
logBonus->trace("Loaded bonus type %s", node.first);
2017-08-08 16:53:01 +02:00
}
}
}
2023-03-13 23:26:44 +02:00
void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
{
2023-01-17 12:42:43 +02:00
dest.identifier = name;
2013-03-14 20:56:52 +03:00
dest.hidden = source["hidden"].Bool(); //Null -> false
2017-08-08 16:53:01 +02:00
2023-01-17 12:42:43 +02:00
if (!dest.hidden)
{
VLC->generaltexth->registerString( "core", dest.getNameTextID(), source["name"].String());
VLC->generaltexth->registerString( "core", dest.getDescriptionTextID(), source["description"].String());
2023-01-17 12:42:43 +02:00
}
2017-08-08 16:53:01 +02:00
const JsonNode & graphics = source["graphics"];
if(!graphics.isNull())
2013-03-14 20:56:52 +03:00
dest.icon = graphics["icon"].String();
}
VCMI_LIB_NAMESPACE_END