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/EntityIdentifiers.h
${MAIN_LIB_DIR}/constants/Enumerations.h ${MAIN_LIB_DIR}/constants/Enumerations.h
${MAIN_LIB_DIR}/constants/IdentifierBase.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/NumericConstants.h
${MAIN_LIB_DIR}/constants/StringConstants.h ${MAIN_LIB_DIR}/constants/StringConstants.h

View File

@ -425,6 +425,14 @@ static void loadBonusSubtype(TBonusSubtype & subtype, BonusType type, const Json
return; 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()) if (!node.isString())
{ {
logMod->warn("Bonus subtype must be string!"); logMod->warn("Bonus subtype must be string!");
@ -432,16 +440,114 @@ static void loadBonusSubtype(TBonusSubtype & subtype, BonusType type, const Json
return; return;
} }
VLC->identifiers()->requestIdentifier(node, [&subtype, node](int32_t identifier) switch (type)
{
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)
{ {
assert(0); //TODO
subtype = BonusSubtypeID(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) 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) std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonVector & ability_vec)
@ -895,6 +1001,7 @@ bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b)
if (!value->isNull()) if (!value->isNull())
b->source = static_cast<BonusSource>(parseByMap(bonusSourceMap, value, "source type ")); b->source = static_cast<BonusSource>(parseByMap(bonusSourceMap, value, "source type "));
if (!ability["sourceID"].isNull())
loadBonusSourceInstance(b->sid, b->source, ability["sourceID"]); loadBonusSourceInstance(b->sid, b->source, ability["sourceID"]);
value = &ability["targetSourceType"]; value = &ability["targetSourceType"];
@ -960,20 +1067,24 @@ CSelector JsonUtils::parseSelector(const JsonNode & ability)
ret = ret.And(base.Not()); ret = ret.And(base.Not());
} }
BonusType type = BonusType::NONE;
// Actual selector parser // Actual selector parser
value = &ability["type"]; value = &ability["type"];
if(value->isString()) if(value->isString())
{ {
auto it = bonusNameMap.find(value->String()); auto it = bonusNameMap.find(value->String());
if(it != bonusNameMap.end()) if(it != bonusNameMap.end())
{
type = it->second;
ret = ret.And(Selector::type()(it->second)); ret = ret.And(Selector::type()(it->second));
} }
}
value = &ability["subtype"]; value = &ability["subtype"];
if(!value->isNull()) if(!value->isNull() && type != BonusType::NONE)
{ {
TBonusSubtype subtype; TBonusSubtype subtype;
assert(0); //TODO loadBonusSubtype(subtype, type, ability);
loadBonusSubtype(subtype, BonusType::NONE, ability);
ret = ret.And(Selector::subtype()(subtype)); ret = ret.And(Selector::subtype()(subtype));
} }
value = &ability["sourceType"]; value = &ability["sourceType"];

View File

@ -11,7 +11,7 @@
#include "BonusEnum.h" #include "BonusEnum.h"
#include "BonusSubtypeID.h" #include "BonusSubtypeID.h"
#include "../constants/MetaIdentifier.h" #include "../constants/VariantIdentifier.h"
#include "../constants/EntityIdentifiers.h" #include "../constants/EntityIdentifiers.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@ -25,8 +25,8 @@ class IUpdater;
class BonusList; class BonusList;
class CSelector; class CSelector;
using TBonusSubtype = MetaIdentifier<BonusSubtypeID, SpellID, CreatureID, PrimarySkill, TerrainId, GameResID, SpellSchool>; using TBonusSubtype = VariantIdentifier<BonusSubtypeID, SpellID, CreatureID, PrimarySkill, TerrainId, GameResID, SpellSchool>;
using TBonusSourceID = MetaIdentifier<BonusSourceID, SpellID, CreatureID, ArtifactID, CampaignScenarioID, SecondarySkill, HeroTypeID, Obj, ObjectInstanceID, BuildingTypeUniqueID, BattleField>; using TBonusSourceID = VariantIdentifier<BonusSourceID, SpellID, CreatureID, ArtifactID, CampaignScenarioID, SecondarySkill, HeroTypeID, Obj, ObjectInstanceID, BuildingTypeUniqueID, BattleField>;
using TBonusListPtr = std::shared_ptr<BonusList>; using TBonusListPtr = std::shared_ptr<BonusList>;
using TConstBonusListPtr = std::shared_ptr<const BonusList>; using TConstBonusListPtr = std::shared_ptr<const BonusList>;
using TLimiterPtr = std::shared_ptr<ILimiter>; 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 * 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 /// This class represents field that may contain value of multiple different identifer types
template<typename... Types> template<typename... Types>
class DLL_LINKAGE MetaIdentifier class DLL_LINKAGE VariantIdentifier
{ {
std::variant<Types...> value; std::variant<Types...> value;
public: public:
MetaIdentifier() VariantIdentifier()
{} {}
template<typename IdentifierType> template<typename IdentifierType>
MetaIdentifier(const IdentifierType & identifier) VariantIdentifier(const IdentifierType & identifier)
: value(identifier) : value(identifier)
{} {}
int32_t getNum() const int32_t getNum() const
{ {
std::optional<int32_t> result; int32_t result;
std::visit([&result] (const auto& v) { result = v.getNum(); }, value); std::visit([&result] (const auto& v) { result = v.getNum(); }, value);
assert(result.has_value()); return result;
return result.value_or(-1);
} }
std::string toString() const std::string toString() const
{ {
std::optional<std::string> result; std::string result;
std::visit([&result] (const auto& v) { result = v.encode(v.getNum()); }, value); std::visit([&result] (const auto& v) { result = v.encode(v.getNum()); }, value);
assert(result.has_value()); return result;
return result.value_or("");
} }
template<typename IdentifierType> template<typename IdentifierType>
@ -65,15 +63,15 @@ public:
h & value; h & value;
} }
bool operator == (const MetaIdentifier & other) const bool operator == (const VariantIdentifier & other) const
{ {
return value == other.value; return value == other.value;
} }
bool operator != (const MetaIdentifier & other) const bool operator != (const VariantIdentifier & other) const
{ {
return value != other.value; return value != other.value;
} }
bool operator < (const MetaIdentifier & other) const bool operator < (const VariantIdentifier & other) const
{ {
return value < other.value; return value < other.value;
} }