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"
|
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
|
|
|
///MacroString
|
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
MacroString::MacroString(const std::string & format)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
|
|
|
static const std::string MACRO_START = "${";
|
|
|
|
static const std::string MACRO_END = "}";
|
|
|
|
static const size_t MACRO_START_L = 2;
|
|
|
|
static const size_t MACRO_END_L = 1;
|
2017-08-08 16:53:01 +02:00
|
|
|
|
|
|
|
size_t end_pos = 0;
|
2013-03-06 21:49:56 +03:00
|
|
|
size_t start_pos = std::string::npos;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
start_pos = format.find(MACRO_START, end_pos);
|
2017-08-08 16:53:01 +02:00
|
|
|
|
|
|
|
if(!(start_pos == std::string::npos))
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
|
|
|
//chunk before macro
|
2017-08-08 16:53:01 +02:00
|
|
|
items.push_back(Item(Item::STRING, format.substr(end_pos, start_pos - end_pos)));
|
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
start_pos += MACRO_START_L;
|
|
|
|
end_pos = format.find(MACRO_END, start_pos);
|
2017-08-08 16:53:01 +02:00
|
|
|
|
|
|
|
if(end_pos == std::string::npos)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logBonus->warn("Format error in: %s", format);
|
2013-03-06 21:49:56 +03:00
|
|
|
end_pos = start_pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
items.push_back(Item(Item::MACRO, format.substr(start_pos, end_pos - start_pos)));
|
2013-03-06 21:49:56 +03:00
|
|
|
end_pos += MACRO_END_L;
|
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
}
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
while(start_pos != std::string::npos);
|
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
//no more macros
|
2017-08-08 16:53:01 +02:00
|
|
|
items.push_back(Item(Item::STRING, format.substr(end_pos)));
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
std::string MacroString::build(const GetValue & getValue) const
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
|
|
|
std::string result;
|
2017-08-08 16:53:01 +02:00
|
|
|
|
|
|
|
for(const Item & i : items)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
switch(i.type)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
case Item::MACRO:
|
|
|
|
{
|
|
|
|
result += getValue(i.value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Item::STRING:
|
|
|
|
{
|
|
|
|
result += i.value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
///CBonusType
|
|
|
|
|
|
|
|
CBonusType::CBonusType()
|
|
|
|
{
|
|
|
|
hidden = true;
|
2022-11-15 02:20:55 +02:00
|
|
|
icon.clear();
|
|
|
|
nameTemplate.clear();
|
|
|
|
descriptionTemplate.clear();
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CBonusType::~CBonusType()
|
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusType::buildMacros()
|
|
|
|
{
|
|
|
|
name = MacroString(nameTemplate);
|
|
|
|
description = MacroString(descriptionTemplate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///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
|
|
|
|
{
|
|
|
|
auto getValue = [=](const std::string & name) -> std::string
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
if(name == "val")
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
return boost::lexical_cast<std::string>(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype)));
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
else if(name == "subtype.creature")
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
const CreatureID cre(bonus->subtype);
|
|
|
|
return cre.toCreature()->namePl;
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
else if(name == "subtype.spell")
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
const SpellID sp(bonus->subtype);
|
|
|
|
return sp.toSpell()->name;
|
|
|
|
}
|
|
|
|
else if(name == "MR")
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
|
|
|
return boost::lexical_cast<std::string>(bearer->magicResistance());
|
2017-08-08 16:53:01 +02:00
|
|
|
}
|
2013-03-06 21:49:56 +03:00
|
|
|
else
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logBonus->warn("Unknown macro in bonus config: %s", name);
|
2017-08-08 16:53:01 +02:00
|
|
|
return "[error]";
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
};
|
2017-08-08 16:53:01 +02:00
|
|
|
|
|
|
|
const CBonusType & bt = bonusTypes[bonus->type];
|
2014-12-25 23:02:50 +02:00
|
|
|
if(bt.hidden)
|
|
|
|
return "";
|
2017-08-08 16:53:01 +02:00
|
|
|
const MacroString & macro = description ? bt.description : bt.name;
|
|
|
|
|
|
|
|
return macro.build(getValue);
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
std::string 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
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
case Bonus::SECONDARY_SKILL_PREMY:
|
|
|
|
if(bonus->subtype == SecondarySkill::RESISTANCE)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
fileName = "E_DWARF.bmp";
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
break;
|
|
|
|
case Bonus::SPELL_IMMUNITY:
|
|
|
|
{
|
|
|
|
fullPath = true;
|
|
|
|
const CSpell * sp = SpellID(bonus->subtype).toSpell();
|
|
|
|
fileName = sp->getIconImmune();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Bonus::FIRE_IMMUNITY:
|
|
|
|
switch(bonus->subtype)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
case 0:
|
|
|
|
fileName = "E_SPFIRE.bmp";
|
|
|
|
break;//all
|
|
|
|
case 1:
|
|
|
|
fileName = "E_SPFIRE1.bmp";
|
|
|
|
break;//not positive
|
|
|
|
case 2:
|
|
|
|
fileName = "E_FIRE.bmp";
|
|
|
|
break;//direct damage
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
break;
|
|
|
|
case Bonus::WATER_IMMUNITY:
|
|
|
|
switch(bonus->subtype)
|
2014-10-28 20:04:34 +02:00
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
case 0:
|
|
|
|
fileName = "E_SPWATER.bmp";
|
|
|
|
break;//all
|
|
|
|
case 1:
|
|
|
|
fileName = "E_SPWATER1.bmp";
|
|
|
|
break;//not positive
|
|
|
|
case 2:
|
|
|
|
fileName = "E_SPCOLD.bmp";
|
|
|
|
break;//direct damage
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Bonus::AIR_IMMUNITY:
|
|
|
|
switch(bonus->subtype)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
fileName = "E_SPAIR.bmp";
|
|
|
|
break;//all
|
|
|
|
case 1:
|
|
|
|
fileName = "E_SPAIR1.bmp";
|
|
|
|
break;//not positive
|
|
|
|
case 2:
|
|
|
|
fileName = "E_LIGHT.bmp";
|
|
|
|
break;//direct damage
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Bonus::EARTH_IMMUNITY:
|
|
|
|
switch(bonus->subtype)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
fileName = "E_SPEATH.bmp";
|
|
|
|
break;//all
|
|
|
|
case 1:
|
|
|
|
case 2://no specific icon for direct damage immunity
|
|
|
|
fileName = "E_SPEATH1.bmp";
|
|
|
|
break;//not positive
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Bonus::LEVEL_SPELL_IMMUNITY:
|
|
|
|
{
|
|
|
|
if(vstd::iswithin(bonus->val, 1, 5))
|
|
|
|
{
|
|
|
|
fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
|
2014-10-28 20:04:34 +02:00
|
|
|
}
|
2017-08-08 16:53:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Bonus::GENERAL_DAMAGE_REDUCTION:
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
{
|
|
|
|
const CBonusType & bt = bonusTypes[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;
|
2017-08-08 16:53:01 +02:00
|
|
|
return fileName;
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusTypeHandler::load()
|
|
|
|
{
|
|
|
|
const JsonNode gameConf(ResourceID("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
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(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
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
logBonus->warn("Adding new bonuses not implemented (%s)", node.first);
|
2013-03-06 21:49:56 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-08 16:53:01 +02:00
|
|
|
CBonusType & bt = bonusTypes[it->second];
|
|
|
|
|
2013-03-06 21:49:56 +03:00
|
|
|
loadItem(node.second, bt);
|
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
|
|
|
}
|
|
|
|
|
2017-08-08 16:53:01 +02:00
|
|
|
void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest)
|
2013-03-06 21:49:56 +03:00
|
|
|
{
|
2013-03-14 20:56:52 +03:00
|
|
|
dest.nameTemplate = source["name"].String();
|
|
|
|
dest.descriptionTemplate = source["description"].String();
|
|
|
|
dest.hidden = source["hidden"].Bool(); //Null -> false
|
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();
|
2017-08-08 16:53:01 +02:00
|
|
|
}
|
2013-03-06 21:49:56 +03:00
|
|
|
dest.buildMacros();
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|