2023-04-27 23:47:29 +02:00
|
|
|
/*
|
2023-04-30 15:52:48 +02:00
|
|
|
* Limiters.cpp, part of VCMI engine
|
2023-04-27 23:47:29 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2023-04-30 15:52:48 +02:00
|
|
|
#include "Limiters.h"
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
#include "../VCMI_Lib.h"
|
2024-07-21 12:49:40 +02:00
|
|
|
#include "../entities/faction/CFaction.h"
|
|
|
|
#include "../entities/faction/CTownHandler.h"
|
2023-04-27 23:47:29 +02:00
|
|
|
#include "../spells/CSpellHandler.h"
|
|
|
|
#include "../CCreatureHandler.h"
|
|
|
|
#include "../CCreatureSet.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../texts/CGeneralTextHandler.h"
|
2023-04-27 23:47:29 +02:00
|
|
|
#include "../CSkillHandler.h"
|
|
|
|
#include "../CStack.h"
|
|
|
|
#include "../CArtHandler.h"
|
|
|
|
#include "../TerrainHandler.h"
|
2023-08-19 23:22:31 +02:00
|
|
|
#include "../constants/StringConstants.h"
|
2023-04-27 23:47:29 +02:00
|
|
|
#include "../battle/BattleInfo.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../json/JsonUtils.h"
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
const std::map<std::string, TLimiterPtr> bonusLimiterMap =
|
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
{"SHOOTER_ONLY", std::make_shared<HasAnotherBonusLimiter>(BonusType::SHOOTER)},
|
|
|
|
{"DRAGON_NATURE", std::make_shared<HasAnotherBonusLimiter>(BonusType::DRAGON_NATURE)},
|
|
|
|
{"IS_UNDEAD", std::make_shared<HasAnotherBonusLimiter>(BonusType::UNDEAD)},
|
2023-04-27 23:47:29 +02:00
|
|
|
{"CREATURE_NATIVE_TERRAIN", std::make_shared<CreatureTerrainLimiter>()},
|
|
|
|
{"CREATURE_FACTION", std::make_shared<AllOfLimiter>(std::initializer_list<TLimiterPtr>{std::make_shared<CreatureLevelLimiter>(), std::make_shared<FactionLimiter>()})},
|
|
|
|
{"SAME_FACTION", std::make_shared<FactionLimiter>()},
|
|
|
|
{"CREATURES_ONLY", std::make_shared<CreatureLevelLimiter>()},
|
|
|
|
{"OPPOSITE_SIDE", std::make_shared<OppositeSideLimiter>()},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const CStack * retrieveStackBattle(const CBonusSystemNode * node)
|
|
|
|
{
|
|
|
|
switch(node->getNodeType())
|
|
|
|
{
|
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
|
|
|
return dynamic_cast<const CStack *>(node);
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const CStackInstance * retrieveStackInstance(const CBonusSystemNode * node)
|
|
|
|
{
|
|
|
|
switch(node->getNodeType())
|
|
|
|
{
|
|
|
|
case CBonusSystemNode::STACK_INSTANCE:
|
|
|
|
return (dynamic_cast<const CStackInstance *>(node));
|
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
|
|
|
return (dynamic_cast<const CStack *>(node))->base;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const CCreature * retrieveCreature(const CBonusSystemNode *node)
|
|
|
|
{
|
|
|
|
switch(node->getNodeType())
|
|
|
|
{
|
|
|
|
case CBonusSystemNode::CREATURE:
|
|
|
|
return (dynamic_cast<const CCreature *>(node));
|
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
|
|
|
return (dynamic_cast<const CStack *>(node))->unitType();
|
|
|
|
default:
|
|
|
|
const CStackInstance * csi = retrieveStackInstance(node);
|
|
|
|
if(csi)
|
2024-10-12 18:02:35 +02:00
|
|
|
return csi->getCreature();
|
2023-04-27 23:47:29 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision ILimiter::limit(const BonusLimitationContext &context) const /*return true to drop the bonus */
|
|
|
|
{
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ILimiter::toString() const
|
|
|
|
{
|
|
|
|
return typeid(*this).name();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode ILimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
root["type"].String() = toString();
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision CCreatureTypeLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const CCreature *c = retrieveCreature(&context.node);
|
|
|
|
if(!c)
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
|
2024-10-12 20:19:58 +02:00
|
|
|
auto accept = c->getId() == creatureID || (includeUpgrades && creatureID.toCreature()->isMyUpgrade(c));
|
2023-04-27 23:47:29 +02:00
|
|
|
return accept ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
|
|
|
//drop bonus if it's not our creature and (we don`t check upgrades or its not our upgrade)
|
|
|
|
}
|
|
|
|
|
|
|
|
CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature & creature_, bool IncludeUpgrades)
|
2024-10-12 20:19:58 +02:00
|
|
|
: creatureID(creature_.getId()), includeUpgrades(IncludeUpgrades)
|
2023-04-27 23:47:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureTypeLimiter::setCreature(const CreatureID & id)
|
|
|
|
{
|
2024-10-12 20:19:58 +02:00
|
|
|
creatureID = id;
|
2023-04-27 23:47:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CCreatureTypeLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("CCreatureTypeLimiter(creature=%s, includeUpgrades=%s)");
|
2024-10-12 20:19:58 +02:00
|
|
|
fmt % creatureID.toEntity(VLC)->getJsonKey() % (includeUpgrades ? "true" : "false");
|
2023-04-27 23:47:29 +02:00
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CCreatureTypeLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_TYPE_LIMITER";
|
2024-10-12 20:19:58 +02:00
|
|
|
root["parameters"].Vector().emplace_back(creatureID.toEntity(VLC)->getJsonKey());
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(includeUpgrades);
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( BonusType bonus )
|
2023-10-05 15:13:52 +02:00
|
|
|
: type(bonus), isSubtypeRelevant(false), isSourceRelevant(false), isSourceIDRelevant(false)
|
2023-04-27 23:47:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-21 13:50:42 +02:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( BonusType bonus, BonusSubtypeID _subtype )
|
2023-04-27 23:47:29 +02:00
|
|
|
: type(bonus), subtype(_subtype), isSubtypeRelevant(true), isSourceRelevant(false), isSourceIDRelevant(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter(BonusType bonus, BonusSource src)
|
2023-04-27 23:47:29 +02:00
|
|
|
: type(bonus), source(src), isSubtypeRelevant(false), isSourceRelevant(true), isSourceIDRelevant(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-10-21 13:50:42 +02:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter(BonusType bonus, BonusSubtypeID _subtype, BonusSource src)
|
2023-04-27 23:47:29 +02:00
|
|
|
: type(bonus), subtype(_subtype), isSubtypeRelevant(true), source(src), isSourceRelevant(true), isSourceIDRelevant(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision HasAnotherBonusLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
//TODO: proper selector config with parsing of JSON
|
|
|
|
auto mySelector = Selector::type()(type);
|
|
|
|
|
|
|
|
if(isSubtypeRelevant)
|
|
|
|
mySelector = mySelector.And(Selector::subtype()(subtype));
|
|
|
|
if(isSourceRelevant && isSourceIDRelevant)
|
|
|
|
mySelector = mySelector.And(Selector::source(source, sid));
|
|
|
|
else if (isSourceRelevant)
|
|
|
|
mySelector = mySelector.And(Selector::sourceTypeSel(source));
|
|
|
|
|
|
|
|
//if we have a bonus of required type accepted, limiter should accept also this bonus
|
|
|
|
if(context.alreadyAccepted.getFirst(mySelector))
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
|
|
|
|
//if there are no matching bonuses pending, we can (and must) reject right away
|
|
|
|
if(!context.stillUndecided.getFirst(mySelector))
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
|
|
|
|
//do not accept for now but it may change if more bonuses gets included
|
|
|
|
return ILimiter::EDecision::NOT_SURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HasAnotherBonusLimiter::toString() const
|
|
|
|
{
|
|
|
|
std::string typeName = vstd::findKey(bonusNameMap, type);
|
|
|
|
if(isSubtypeRelevant)
|
|
|
|
{
|
2023-10-05 15:13:52 +02:00
|
|
|
boost::format fmt("HasAnotherBonusLimiter(type=%s, subtype=%s)");
|
|
|
|
fmt % typeName % subtype.toString();
|
2023-04-27 23:47:29 +02:00
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boost::format fmt("HasAnotherBonusLimiter(type=%s)");
|
|
|
|
fmt % typeName;
|
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode HasAnotherBonusLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
std::string typeName = vstd::findKey(bonusNameMap, type);
|
|
|
|
auto sourceTypeName = vstd::findKey(bonusSourceMap, source);
|
|
|
|
|
|
|
|
root["type"].String() = "HAS_ANOTHER_BONUS_LIMITER";
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(typeName);
|
2023-04-27 23:47:29 +02:00
|
|
|
if(isSubtypeRelevant)
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(subtype.toString());
|
2023-04-27 23:47:29 +02:00
|
|
|
if(isSourceRelevant)
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(sourceTypeName);
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision UnitOnHexLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const auto * stack = retrieveStackBattle(&context.node);
|
|
|
|
if(!stack)
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
|
|
|
|
auto accept = false;
|
|
|
|
for (const auto & hex : stack->getHexes())
|
|
|
|
accept |= !!applicableHexes.count(hex);
|
|
|
|
|
|
|
|
return accept ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitOnHexLimiter::UnitOnHexLimiter(const std::set<BattleHex> & applicableHexes):
|
|
|
|
applicableHexes(applicableHexes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode UnitOnHexLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
root["type"].String() = "UNIT_ON_HEXES";
|
|
|
|
for(const auto & hex : applicableHexes)
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(hex);
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureTerrainLimiter::CreatureTerrainLimiter()
|
|
|
|
: terrainType(ETerrainId::NATIVE_TERRAIN)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureTerrainLimiter::CreatureTerrainLimiter(TerrainId terrain):
|
|
|
|
terrainType(terrain)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision CreatureTerrainLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const CStack *stack = retrieveStackBattle(&context.node);
|
|
|
|
if(stack)
|
|
|
|
{
|
|
|
|
if (terrainType == ETerrainId::NATIVE_TERRAIN && stack->isOnNativeTerrain())//terrainType not specified = native
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
|
|
|
|
if(terrainType != ETerrainId::NATIVE_TERRAIN && stack->isOnTerrain(terrainType))
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
|
|
|
|
}
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
//TODO neutral creatues
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CreatureTerrainLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("CreatureTerrainLimiter(terrainType=%s)");
|
|
|
|
auto terrainName = VLC->terrainTypeHandler->getById(terrainType)->getJsonKey();
|
|
|
|
fmt % (terrainType == ETerrainId::NATIVE_TERRAIN ? "native" : terrainName);
|
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CreatureTerrainLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_TERRAIN_LIMITER";
|
|
|
|
auto terrainName = VLC->terrainTypeHandler->getById(terrainType)->getJsonKey();
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(terrainName);
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
FactionLimiter::FactionLimiter(FactionID creatureFaction)
|
|
|
|
: faction(creatureFaction)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision FactionLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const auto * bearer = dynamic_cast<const INativeTerrainProvider*>(&context.node);
|
|
|
|
|
|
|
|
if(bearer)
|
|
|
|
{
|
|
|
|
if(faction != FactionID::DEFAULT)
|
2024-10-05 21:37:52 +02:00
|
|
|
return bearer->getFactionID() == faction ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
switch(context.b.source)
|
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
case BonusSource::CREATURE_ABILITY:
|
2024-10-05 21:37:52 +02:00
|
|
|
return bearer->getFactionID() == context.b.sid.as<CreatureID>().toCreature()->getFactionID() ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
case BonusSource::TOWN_STRUCTURE:
|
2024-10-05 21:37:52 +02:00
|
|
|
return bearer->getFactionID() == context.b.sid.as<BuildingTypeUniqueID>().getFaction() ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
//TODO: other sources of bonuses
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ILimiter::EDecision::DISCARD; //Discard by default
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FactionLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("FactionLimiter(faction=%s)");
|
2023-11-02 16:56:02 +02:00
|
|
|
fmt % VLC->factions()->getById(faction)->getJsonKey();
|
2023-04-27 23:47:29 +02:00
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode FactionLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
root["type"].String() = "FACTION_LIMITER";
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(VLC->factions()->getById(faction)->getJsonKey());
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureLevelLimiter::CreatureLevelLimiter(uint32_t minLevel, uint32_t maxLevel) :
|
|
|
|
minLevel(minLevel),
|
|
|
|
maxLevel(maxLevel)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision CreatureLevelLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const auto *c = retrieveCreature(&context.node);
|
|
|
|
auto accept = c && (c->getLevel() < maxLevel && c->getLevel() >= minLevel);
|
|
|
|
return accept ? ILimiter::EDecision::ACCEPT : ILimiter::EDecision::DISCARD; //drop bonus for non-creatures or non-native residents
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CreatureLevelLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("CreatureLevelLimiter(minLevel=%d,maxLevel=%d)");
|
|
|
|
fmt % minLevel % maxLevel;
|
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CreatureLevelLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_LEVEL_LIMITER";
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(minLevel);
|
|
|
|
root["parameters"].Vector().emplace_back(maxLevel);
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureAlignmentLimiter::CreatureAlignmentLimiter(EAlignment Alignment)
|
|
|
|
: alignment(Alignment)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision CreatureAlignmentLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const auto * c = retrieveCreature(&context.node);
|
|
|
|
if(c) {
|
|
|
|
if(alignment == EAlignment::GOOD && c->isGood())
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
if(alignment == EAlignment::EVIL && c->isEvil())
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
if(alignment == EAlignment::NEUTRAL && !c->isEvil() && !c->isGood())
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CreatureAlignmentLimiter::toString() const
|
|
|
|
{
|
|
|
|
boost::format fmt("CreatureAlignmentLimiter(alignment=%s)");
|
|
|
|
fmt % GameConstants::ALIGNMENT_NAMES[vstd::to_underlying(alignment)];
|
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode CreatureAlignmentLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode root;
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
root["type"].String() = "CREATURE_ALIGNMENT_LIMITER";
|
2024-02-13 13:18:10 +02:00
|
|
|
root["parameters"].Vector().emplace_back(GameConstants::ALIGNMENT_NAMES[vstd::to_underlying(alignment)]);
|
2023-04-27 23:47:29 +02:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
|
|
|
|
:minRank(Min), maxRank(Max)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RankRangeLimiter::RankRangeLimiter()
|
|
|
|
{
|
|
|
|
minRank = maxRank = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision RankRangeLimiter::limit(const BonusLimitationContext &context) const
|
|
|
|
{
|
|
|
|
const CStackInstance * csi = retrieveStackInstance(&context.node);
|
|
|
|
if(csi)
|
|
|
|
{
|
|
|
|
if (csi->getNodeType() == CBonusSystemNode::COMMANDER) //no stack exp bonuses for commander creatures
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
if (csi->getExpRank() > minRank && csi->getExpRank() < maxRank)
|
|
|
|
return ILimiter::EDecision::ACCEPT;
|
|
|
|
}
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
OppositeSideLimiter::OppositeSideLimiter(PlayerColor Owner):
|
|
|
|
owner(std::move(Owner))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision OppositeSideLimiter::limit(const BonusLimitationContext & context) const
|
|
|
|
{
|
2023-05-03 15:09:21 +02:00
|
|
|
auto contextOwner = context.node.getOwner();
|
2023-04-27 23:47:29 +02:00
|
|
|
auto decision = (owner == contextOwner || owner == PlayerColor::CANNOT_DETERMINE) ? ILimiter::EDecision::DISCARD : ILimiter::EDecision::ACCEPT;
|
|
|
|
return decision;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggregate/Boolean Limiters
|
|
|
|
|
|
|
|
AggregateLimiter::AggregateLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
limiters(std::move(limiters))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggregateLimiter::add(const TLimiterPtr & limiter)
|
|
|
|
{
|
|
|
|
if(limiter)
|
|
|
|
limiters.push_back(limiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode AggregateLimiter::toJsonNode() const
|
|
|
|
{
|
2024-02-13 13:18:10 +02:00
|
|
|
JsonNode result;
|
|
|
|
result.Vector().emplace_back(getAggregator());
|
2023-04-27 23:47:29 +02:00
|
|
|
for(const auto & l : limiters)
|
|
|
|
result.Vector().push_back(l->toJsonNode());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string AllOfLimiter::aggregator = "allOf";
|
|
|
|
const std::string & AllOfLimiter::getAggregator() const
|
|
|
|
{
|
|
|
|
return aggregator;
|
|
|
|
}
|
|
|
|
|
|
|
|
AllOfLimiter::AllOfLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
AggregateLimiter(limiters)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision AllOfLimiter::limit(const BonusLimitationContext & context) const
|
|
|
|
{
|
|
|
|
bool wasntSure = false;
|
|
|
|
|
|
|
|
for(const auto & limiter : limiters)
|
|
|
|
{
|
|
|
|
auto result = limiter->limit(context);
|
|
|
|
if(result == ILimiter::EDecision::DISCARD)
|
|
|
|
return result;
|
|
|
|
if(result == ILimiter::EDecision::NOT_SURE)
|
|
|
|
wasntSure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasntSure ? ILimiter::EDecision::NOT_SURE : ILimiter::EDecision::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string AnyOfLimiter::aggregator = "anyOf";
|
|
|
|
const std::string & AnyOfLimiter::getAggregator() const
|
|
|
|
{
|
|
|
|
return aggregator;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnyOfLimiter::AnyOfLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
AggregateLimiter(limiters)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision AnyOfLimiter::limit(const BonusLimitationContext & context) const
|
|
|
|
{
|
|
|
|
bool wasntSure = false;
|
|
|
|
|
|
|
|
for(const auto & limiter : limiters)
|
|
|
|
{
|
|
|
|
auto result = limiter->limit(context);
|
|
|
|
if(result == ILimiter::EDecision::ACCEPT)
|
|
|
|
return result;
|
|
|
|
if(result == ILimiter::EDecision::NOT_SURE)
|
|
|
|
wasntSure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasntSure ? ILimiter::EDecision::NOT_SURE : ILimiter::EDecision::DISCARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string NoneOfLimiter::aggregator = "noneOf";
|
|
|
|
const std::string & NoneOfLimiter::getAggregator() const
|
|
|
|
{
|
|
|
|
return aggregator;
|
|
|
|
}
|
|
|
|
|
|
|
|
NoneOfLimiter::NoneOfLimiter(std::vector<TLimiterPtr> limiters):
|
|
|
|
AggregateLimiter(limiters)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::EDecision NoneOfLimiter::limit(const BonusLimitationContext & context) const
|
|
|
|
{
|
|
|
|
bool wasntSure = false;
|
|
|
|
|
|
|
|
for(const auto & limiter : limiters)
|
|
|
|
{
|
|
|
|
auto result = limiter->limit(context);
|
|
|
|
if(result == ILimiter::EDecision::ACCEPT)
|
|
|
|
return ILimiter::EDecision::DISCARD;
|
|
|
|
if(result == ILimiter::EDecision::NOT_SURE)
|
|
|
|
wasntSure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasntSure ? ILimiter::EDecision::NOT_SURE : ILimiter::EDecision::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|