2013-03-06 21:49:56 +03: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"
|
2017-08-08 16:53:01 +02:00
|
|
|
|
|
|
|
#define INSTANTIATE_CBonusTypeHandler_HERE
|
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
#include "CBonusTypeHandler.h"
|
|
|
|
|
|
|
|
#include "JsonNode.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "filesystem/Filesystem.h"
|
2013-03-06 21:49:56 +03:00
|
|
|
|
2014-01-19 16:29:13 +03:00
|
|
|
#include "GameConstants.h"
|
2013-03-06 21:49:56 +03:00
|
|
|
#include "CCreatureHandler.h"
|
2023-01-17 12:42:43 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "spells/CSpellHandler.h"
|
2013-03-06 21:49:56 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
template class std::vector<VCMI_LIB_WRAP_NAMESPACE(CBonusType)>;
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
2017-08-08 16:53:01 +02:00
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
///CBonusType
|
|
|
|
|
2023-01-17 12:42:43 +02:00
|
|
|
CBonusType::CBonusType():
|
|
|
|
hidden(true)
|
|
|
|
{}
|
2013-03-06 21:49:56 +03:00
|
|
|
|
2023-01-17 12:42:43 +02:00
|
|
|
std::string CBonusType::getNameTextID() const
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2023-01-17 12:42:43 +02:00
|
|
|
return TextIdentifier( "core", "bonus", identifier, "name").get();
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
2023-01-17 12:42:43 +02:00
|
|
|
std::string CBonusType::getDescriptionTextID() const
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2023-01-17 12:42:43 +02:00
|
|
|
return TextIdentifier( "core", "bonus", identifier, "description").get();
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
///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);
|
|
|
|
|
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
BONUS_LIST;
|
|
|
|
#undef BONUS_NAME
|
2013-04-21 15:49:26 +03:00
|
|
|
|
|
|
|
load();
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
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;
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
ImagePath CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
|
|
|
std::string fileName;
|
|
|
|
bool fullPath = false;
|
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
switch(bonus->type)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
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;
|
|
|
|
}
|
2023-08-21 20:51:06 +02:00
|
|
|
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())
|
2023-08-21 20:51:06 +02:00
|
|
|
fileName = "E_GOLEM.bmp";
|
2023-08-20 19:09:32 +02:00
|
|
|
|
|
|
|
if (bonus->subtype == SpellSchool::AIR.getNum())
|
2023-08-21 20:51:06 +02:00
|
|
|
fileName = "E_LIGHT.bmp";
|
2023-08-20 19:09:32 +02:00
|
|
|
|
|
|
|
if (bonus->subtype == SpellSchool::FIRE.getNum())
|
2023-08-21 20:51:06 +02:00
|
|
|
fileName = "E_FIRE.bmp";
|
2023-08-20 19:09:32 +02:00
|
|
|
|
|
|
|
if (bonus->subtype == SpellSchool::WATER.getNum())
|
2023-08-21 20:51:06 +02:00
|
|
|
fileName = "E_COLD.bmp";
|
2023-08-20 19:09:32 +02:00
|
|
|
|
|
|
|
if (bonus->subtype == SpellSchool::EARTH.getNum())
|
2023-08-21 20:51:06 +02:00
|
|
|
fileName = "E_SPEATH1.bmp"; //No separate icon for earth damage
|
2023-08-20 19:09:32 +02:00
|
|
|
|
2023-08-21 20:51:06 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-08-19 19:19:59 +02:00
|
|
|
case BonusType::SPELL_SCHOOL_IMMUNITY: //for all school
|
|
|
|
{
|
2023-08-20 19:09:32 +02:00
|
|
|
if (bonus->subtype == SpellSchool::AIR.getNum())
|
2023-08-19 19:19:59 +02:00
|
|
|
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())
|
2023-08-19 19:19:59 +02:00
|
|
|
fileName = "E_SPEATH.bmp";
|
2023-08-20 19:09:32 +02:00
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
break;
|
2023-08-19 19:19:59 +02:00
|
|
|
}
|
|
|
|
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())
|
2023-08-19 19:19:59 +02:00
|
|
|
fileName = "E_SPFIRE1.bmp";
|
2023-08-20 19:09:32 +02:00
|
|
|
|
|
|
|
if (bonus->subtype == SpellSchool::WATER.getNum())
|
2023-08-19 19:19:59 +02:00
|
|
|
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;
|
2023-08-19 19:19:59 +02:00
|
|
|
}
|
2023-05-01 00:20:01 +02:00
|
|
|
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";
|
2014-10-28 20:04:34 +02:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-05-01 00:20:01 +02:00
|
|
|
case BonusType::KING:
|
2023-03-04 22:43:10 +02:00
|
|
|
{
|
|
|
|
if(vstd::iswithin(bonus->val, 0, 3))
|
|
|
|
{
|
|
|
|
fileName = "E_KING" + std::to_string(std::max(1, bonus->val)) + ".bmp";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-05-01 00:20:01 +02:00
|
|
|
case BonusType::GENERAL_DAMAGE_REDUCTION:
|
2017-08-08 16:53:01 +02:00
|
|
|
{
|
|
|
|
switch(bonus->subtype)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
case 0:
|
|
|
|
fileName = "DamageReductionMelee.bmp";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
fileName = "DamageReductionRanged.bmp";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-03-06 21:49:56 +03:00
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
default:
|
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
|
2013-03-06 21:49:56 +03:00
|
|
|
fileName = bt.icon;
|
|
|
|
fullPath = true;
|
2017-08-08 16:53:01 +02:00
|
|
|
}
|
|
|
|
break;
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
if(!fileName.empty() && !fullPath)
|
|
|
|
fileName = "zvs/Lib1.res/" + fileName;
|
2023-08-23 14:07:50 +02:00
|
|
|
return ImagePath::builtinTODO(fileName);
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusTypeHandler::load()
|
|
|
|
{
|
2023-09-01 23:26:14 +02:00
|
|
|
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>>()));
|
2013-03-06 21:49:56 +03:00
|
|
|
load(config);
|
|
|
|
}
|
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
void CBonusTypeHandler::load(const JsonNode & config)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2023-03-13 23:26:44 +02:00
|
|
|
for(const auto & node : config.Struct())
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
|
|
|
auto it = bonusNameMap.find(node.first);
|
2017-08-08 16:53:01 +02:00
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
if(it == bonusNameMap.end())
|
|
|
|
{
|
|
|
|
//TODO: new bonus
|
2013-03-10 19:16:43 +03:00
|
|
|
// CBonusType bt;
|
|
|
|
// loadItem(node.second, bt);
|
2017-08-08 16:53:01 +02:00
|
|
|
//
|
2013-03-10 19:16:43 +03:00
|
|
|
// auto new_id = bonusTypes.size();
|
2017-08-08 16:53:01 +02:00
|
|
|
//
|
2013-03-10 19:16:43 +03: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);
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
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);
|
2017-08-11 19:03:05 +02:00
|
|
|
logBonus->trace("Loaded bonus type %s", node.first);
|
2017-08-08 16:53:01 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
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)
|
|
|
|
{
|
2023-02-09 15:03:49 +02:00
|
|
|
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"];
|
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
if(!graphics.isNull())
|
2013-03-14 20:56:52 +03:00
|
|
|
dest.icon = graphics["icon"].String();
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|