1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

remove base json from specialty bonuses

This commit is contained in:
Henning Koehler 2017-09-13 20:27:01 +12:00
parent eb02ce0f31
commit 038cb4bf19
4 changed files with 40 additions and 1 deletions

View File

@ -766,7 +766,8 @@ void CHeroHandler::afterLoadFinalization()
if(!base.isEmpty())
{
specNode["base"] = base;
//TODO: subtract base from bonuses
for(JsonNode & node : specVec)
node = JsonUtils::difference(node, base);
}
}
// add json for bonuses

View File

@ -1736,6 +1736,8 @@ std::string nameForBonus(const Bonus & bonus)
return CreatureID::encode(bonus.subtype) + "2" + CreatureID::encode(bonus.additionalInfo);
case Bonus::GENERATE_RESOURCE:
return GameConstants::RESOURCE_NAMES[bonus.subtype];
case Bonus::STACKS_SPEED:
return "speed";
default:
return vstd::findKey(bonusNameMap, bonus.type);
}

View File

@ -899,6 +899,36 @@ JsonNode JsonUtils::intersect(const JsonNode & a, const JsonNode & b, bool prune
return nullNode;
}
JsonNode JsonUtils::difference(const JsonNode & node, const JsonNode & base)
{
if(node.getType() == JsonNode::DATA_STRUCT && base.getType() == JsonNode::DATA_STRUCT)
{
// subtract individual properties
JsonNode result(JsonNode::DATA_STRUCT);
for(auto property : node.Struct())
{
if(vstd::contains(base.Struct(), property.first))
{
JsonNode propertyDifference = JsonUtils::difference(property.second, base.Struct().find(property.first)->second);
if(propertyDifference.isEmpty())
continue;
result[property.first] = propertyDifference;
}
else
{
result[property.first] = property.second;
}
}
return result;
}
else
{
if(node == base)
return nullNode;
}
return node;
}
JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files)
{
bool isValid;

View File

@ -199,6 +199,12 @@ namespace JsonUtils
DLL_LINKAGE JsonNode intersect(const JsonNode & a, const JsonNode & b, bool pruneEmpty = true);
DLL_LINKAGE JsonNode intersect(const std::vector<JsonNode> & nodes, bool pruneEmpty = true);
/**
* @brief construct node representing the difference "node - base"
* merging difference with base gives node
*/
DLL_LINKAGE JsonNode difference(const JsonNode & node, const JsonNode & base);
/**
* @brief generate one Json structure from multiple files
* @param files - list of filenames with parts of json structure