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

Renamed MetaIdentifier to VariantIdentifier

This commit is contained in:
Ivan Savenko 2023-10-15 16:50:24 +03:00
parent 80e6485965
commit 3b010d9596
4 changed files with 135 additions and 26 deletions

View File

@ -374,7 +374,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
${MAIN_LIB_DIR}/constants/EntityIdentifiers.h
${MAIN_LIB_DIR}/constants/Enumerations.h
${MAIN_LIB_DIR}/constants/IdentifierBase.h
${MAIN_LIB_DIR}/constants/MetaIdentifier.h
${MAIN_LIB_DIR}/constants/VariantIdentifier.h
${MAIN_LIB_DIR}/constants/NumericConstants.h
${MAIN_LIB_DIR}/constants/StringConstants.h

View File

@ -425,6 +425,14 @@ static void loadBonusSubtype(TBonusSubtype & subtype, BonusType type, const Json
return;
}
if (node.isNumber()) // Compatibility code for 1.3 or older
{
VLC->identifiers()->requestIdentifier( "bonusSubtype", node, [&subtype](int32_t identifier)
{
subtype = BonusSubtypeID(identifier);
});
}
if (!node.isString())
{
logMod->warn("Bonus subtype must be string!");
@ -432,16 +440,114 @@ static void loadBonusSubtype(TBonusSubtype & subtype, BonusType type, const Json
return;
}
VLC->identifiers()->requestIdentifier(node, [&subtype, node](int32_t identifier)
switch (type)
{
assert(0); //TODO
subtype = BonusSubtypeID(identifier);
});
case BonusType::MAGIC_SCHOOL_SKILL:
case BonusType::SPELL_DAMAGE:
case BonusType::SPELLS_OF_SCHOOL:
case BonusType::SPELL_DAMAGE_REDUCTION:
{
VLC->identifiers()->requestIdentifier( "spellSchool", node, [&subtype](int32_t identifier)
{
subtype = SpellSchool(identifier);
});
break;
}
case BonusType::NO_TERRAIN_PENALTY:
{
VLC->identifiers()->requestIdentifier( "terrain", node, [&subtype](int32_t identifier)
{
subtype = TerrainId(identifier);
});
break;
}
case BonusType::PRIMARY_SKILL:
{
VLC->identifiers()->requestIdentifier( "primarySkill", node, [&subtype](int32_t identifier)
{
subtype = PrimarySkill(identifier);
});
break;
}
case BonusType::IMPROVED_NECROMANCY:
case BonusType::HERO_GRANTS_ATTACKS:
case BonusType::BONUS_DAMAGE_CHANCE:
case BonusType::BONUS_DAMAGE_PERCENTAGE:
case BonusType::SPECIAL_UPGRADE:
case BonusType::HATE:
case BonusType::SUMMON_GUARDIANS:
{
VLC->identifiers()->requestIdentifier( "creature", node, [&subtype](int32_t identifier)
{
subtype = CreatureID(identifier);
});
break;
}
case BonusType::SPECIAL_SPELL_LEV:
case BonusType::SPECIFIC_SPELL_DAMAGE:
case BonusType::SPELL:
case BonusType::OPENING_BATTLE_SPELL:
case BonusType::SPELL_LIKE_ATTACK:
case BonusType::CATAPULT:
case BonusType::CATAPULT_EXTRA_SHOTS:
case BonusType::HEALER:
case BonusType::SPELLCASTER:
case BonusType::ENCHANTER:
case BonusType::SPELL_AFTER_ATTACK:
case BonusType::SPELL_BEFORE_ATTACK:
case BonusType::SPECIFIC_SPELL_POWER:
case BonusType::ENCHANTED:
case BonusType::MORE_DAMAGE_FROM_SPELL:
{
VLC->identifiers()->requestIdentifier( "spell", node, [&subtype](int32_t identifier)
{
subtype = SpellID(identifier);
});
break;
}
case BonusType::GENERATE_RESOURCE:
{
VLC->identifiers()->requestIdentifier( "resource", node, [&subtype](int32_t identifier)
{
subtype = GameResID(identifier);
});
break;
}
case BonusType::MOVEMENT:
case BonusType::WATER_WALKING:
case BonusType::FLYING_MOVEMENT:
case BonusType::SPECIAL_PECULIAR_ENCHANT:
case BonusType::NEGATE_ALL_NATURAL_IMMUNITIES:
case BonusType::CREATURE_DAMAGE:
case BonusType::FLYING:
case BonusType::GENERAL_DAMAGE_REDUCTION:
case BonusType::PERCENTAGE_DAMAGE_BOOST:
case BonusType::SOUL_STEAL:
case BonusType::TRANSMUTATION:
case BonusType::DESTRUCTION:
case BonusType::DEATH_STARE:
case BonusType::REBIRTH:
case BonusType::VISIONS:
case BonusType::SPELLS_OF_LEVEL: // spell level
case BonusType::CREATURE_GROWTH: // creature level
{
VLC->identifiers()->requestIdentifier( "bonusSubtype", node, [&subtype](int32_t identifier)
{
subtype = BonusSubtypeID(identifier);
});
break;
}
default:
for(const auto & i : bonusNameMap)
if(i.second == type)
logMod->warn("Bonus type %s does not supports subtypes!", i.first );
subtype = TBonusSubtype();
}
}
static void loadBonusSourceInstance(TBonusSourceID & sourceInstance, BonusSource sourceType, const JsonNode & node)
{
assert(0);
assert(0);//TODO
}
std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonVector & ability_vec)
@ -895,7 +1001,8 @@ bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b)
if (!value->isNull())
b->source = static_cast<BonusSource>(parseByMap(bonusSourceMap, value, "source type "));
loadBonusSourceInstance(b->sid, b->source, ability["sourceID"]);
if (!ability["sourceID"].isNull())
loadBonusSourceInstance(b->sid, b->source, ability["sourceID"]);
value = &ability["targetSourceType"];
if (!value->isNull())
@ -960,20 +1067,24 @@ CSelector JsonUtils::parseSelector(const JsonNode & ability)
ret = ret.And(base.Not());
}
BonusType type = BonusType::NONE;
// Actual selector parser
value = &ability["type"];
if(value->isString())
{
auto it = bonusNameMap.find(value->String());
if(it != bonusNameMap.end())
{
type = it->second;
ret = ret.And(Selector::type()(it->second));
}
}
value = &ability["subtype"];
if(!value->isNull())
if(!value->isNull() && type != BonusType::NONE)
{
TBonusSubtype subtype;
assert(0); //TODO
loadBonusSubtype(subtype, BonusType::NONE, ability);
loadBonusSubtype(subtype, type, ability);
ret = ret.And(Selector::subtype()(subtype));
}
value = &ability["sourceType"];

View File

@ -11,7 +11,7 @@
#include "BonusEnum.h"
#include "BonusSubtypeID.h"
#include "../constants/MetaIdentifier.h"
#include "../constants/VariantIdentifier.h"
#include "../constants/EntityIdentifiers.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -25,8 +25,8 @@ class IUpdater;
class BonusList;
class CSelector;
using TBonusSubtype = MetaIdentifier<BonusSubtypeID, SpellID, CreatureID, PrimarySkill, TerrainId, GameResID, SpellSchool>;
using TBonusSourceID = MetaIdentifier<BonusSourceID, SpellID, CreatureID, ArtifactID, CampaignScenarioID, SecondarySkill, HeroTypeID, Obj, ObjectInstanceID, BuildingTypeUniqueID, BattleField>;
using TBonusSubtype = VariantIdentifier<BonusSubtypeID, SpellID, CreatureID, PrimarySkill, TerrainId, GameResID, SpellSchool>;
using TBonusSourceID = VariantIdentifier<BonusSourceID, SpellID, CreatureID, ArtifactID, CampaignScenarioID, SecondarySkill, HeroTypeID, Obj, ObjectInstanceID, BuildingTypeUniqueID, BattleField>;
using TBonusListPtr = std::shared_ptr<BonusList>;
using TConstBonusListPtr = std::shared_ptr<const BonusList>;
using TLimiterPtr = std::shared_ptr<ILimiter>;

View File

@ -1,5 +1,5 @@
/*
* MetaIdentifier.h, part of VCMI engine
* VariantIdentifier.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
@ -15,37 +15,35 @@ VCMI_LIB_NAMESPACE_BEGIN
/// This class represents field that may contain value of multiple different identifer types
template<typename... Types>
class DLL_LINKAGE MetaIdentifier
class DLL_LINKAGE VariantIdentifier
{
std::variant<Types...> value;
public:
MetaIdentifier()
VariantIdentifier()
{}
template<typename IdentifierType>
MetaIdentifier(const IdentifierType & identifier)
VariantIdentifier(const IdentifierType & identifier)
: value(identifier)
{}
int32_t getNum() const
{
std::optional<int32_t> result;
int32_t result;
std::visit([&result] (const auto& v) { result = v.getNum(); }, value);
assert(result.has_value());
return result.value_or(-1);
return result;
}
std::string toString() const
{
std::optional<std::string> result;
std::string result;
std::visit([&result] (const auto& v) { result = v.encode(v.getNum()); }, value);
assert(result.has_value());
return result.value_or("");
return result;
}
template<typename IdentifierType>
@ -65,15 +63,15 @@ public:
h & value;
}
bool operator == (const MetaIdentifier & other) const
bool operator == (const VariantIdentifier & other) const
{
return value == other.value;
}
bool operator != (const MetaIdentifier & other) const
bool operator != (const VariantIdentifier & other) const
{
return value != other.value;
}
bool operator < (const MetaIdentifier & other) const
bool operator < (const VariantIdentifier & other) const
{
return value < other.value;
}