|
|
@@ -10,8 +10,10 @@
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
#include "HeroBonus.h"
|
|
|
+#include "CBonusSystemNode.h"
|
|
|
#include "Limiters.h"
|
|
|
#include "Updaters.h"
|
|
|
+#include "Propagators.h"
|
|
|
|
|
|
#include "../VCMI_Lib.h"
|
|
|
#include "../spells/CSpellHandler.h"
|
|
|
@@ -30,9 +32,6 @@
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
-#define FOREACH_PARENT(pname) TNodes lparents; getParents(lparents); for(CBonusSystemNode *pname : lparents)
|
|
|
-#define FOREACH_RED_CHILD(pname) TNodes lchildren; getRedChildren(lchildren); for(CBonusSystemNode *pname : lchildren)
|
|
|
-
|
|
|
#define BONUS_NAME(x) { #x, Bonus::x },
|
|
|
const std::map<std::string, Bonus::BonusType> bonusNameMap = {
|
|
|
BONUS_LIST
|
|
|
@@ -71,16 +70,6 @@ const std::map<std::string, Bonus::LimitEffect> bonusLimitEffect =
|
|
|
BONUS_ITEM(ONLY_MELEE_FIGHT)
|
|
|
};
|
|
|
|
|
|
-const std::map<std::string, TPropagatorPtr> bonusPropagatorMap =
|
|
|
-{
|
|
|
- {"BATTLE_WIDE", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::BATTLE)},
|
|
|
- {"VISITED_TOWN_AND_VISITOR", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::TOWN_AND_VISITOR)},
|
|
|
- {"PLAYER_PROPAGATOR", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::PLAYER)},
|
|
|
- {"HERO", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::HERO)},
|
|
|
- {"TEAM_PROPAGATOR", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::TEAM)}, //untested
|
|
|
- {"GLOBAL_EFFECT", std::make_shared<CPropagatorNodeType>(CBonusSystemNode::GLOBAL_EFFECTS)}
|
|
|
-}; //untested
|
|
|
-
|
|
|
const std::set<std::string> deprecatedBonusSet = {
|
|
|
"SECONDARY_SKILL_PREMY",
|
|
|
"SECONDARY_SKILL_VAL2",
|
|
|
@@ -159,9 +148,6 @@ JsonNode CAddInfo::toJsonNode() const
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-std::atomic<int64_t> CBonusSystemNode::treeChanged(1);
|
|
|
-constexpr bool CBonusSystemNode::cachingEnabled = true;
|
|
|
-
|
|
|
BonusList::BonusList(bool BelongsToTree) : belongsToTree(BelongsToTree)
|
|
|
{
|
|
|
|
|
|
@@ -477,669 +463,6 @@ std::shared_ptr<const Bonus> IBonusBearer::getBonus(const CSelector &selector) c
|
|
|
return bonuses->getFirst(Selector::all);
|
|
|
}
|
|
|
|
|
|
-PlayerColor CBonusSystemNode::retrieveNodeOwner(const CBonusSystemNode * node)
|
|
|
-{
|
|
|
- return node ? node->getOwner() : PlayerColor::CANNOT_DETERMINE;
|
|
|
-}
|
|
|
-
|
|
|
-std::shared_ptr<Bonus> CBonusSystemNode::getBonusLocalFirst(const CSelector & selector)
|
|
|
-{
|
|
|
- auto ret = bonuses.getFirst(selector);
|
|
|
- if(ret)
|
|
|
- return ret;
|
|
|
-
|
|
|
- FOREACH_PARENT(pname)
|
|
|
- {
|
|
|
- ret = pname->getBonusLocalFirst(selector);
|
|
|
- if (ret)
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- return nullptr;
|
|
|
-}
|
|
|
-
|
|
|
-std::shared_ptr<const Bonus> CBonusSystemNode::getBonusLocalFirst(const CSelector & selector) const
|
|
|
-{
|
|
|
- return (const_cast<CBonusSystemNode*>(this))->getBonusLocalFirst(selector);
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::getParents(TCNodes & out) const /*retrieves list of parent nodes (nodes to inherit bonuses from) */
|
|
|
-{
|
|
|
- for(const auto & elem : parents)
|
|
|
- {
|
|
|
- const CBonusSystemNode *parent = elem;
|
|
|
- out.insert(parent);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::getParents(TNodes &out)
|
|
|
-{
|
|
|
- for (auto & elem : parents)
|
|
|
- {
|
|
|
- const CBonusSystemNode *parent = elem;
|
|
|
- out.insert(const_cast<CBonusSystemNode*>(parent));
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::getAllParents(TCNodes & out) const //retrieves list of parent nodes (nodes to inherit bonuses from)
|
|
|
-{
|
|
|
- for(auto * parent : parents)
|
|
|
- {
|
|
|
- out.insert(parent);
|
|
|
- parent->getAllParents(out);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::getAllBonusesRec(BonusList &out, const CSelector & selector) const
|
|
|
-{
|
|
|
- //out has been reserved sufficient capacity at getAllBonuses() call
|
|
|
-
|
|
|
- BonusList beforeUpdate;
|
|
|
- TCNodes lparents;
|
|
|
- getAllParents(lparents);
|
|
|
-
|
|
|
- if(!lparents.empty())
|
|
|
- {
|
|
|
- //estimate on how many bonuses are missing yet - must be positive
|
|
|
- beforeUpdate.reserve(std::max(out.capacity() - out.size(), bonuses.size()));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- beforeUpdate.reserve(bonuses.size()); //at most all local bonuses
|
|
|
- }
|
|
|
-
|
|
|
- for(const auto * parent : lparents)
|
|
|
- {
|
|
|
- parent->getAllBonusesRec(beforeUpdate, selector);
|
|
|
- }
|
|
|
- bonuses.getAllBonuses(beforeUpdate);
|
|
|
-
|
|
|
- for(const auto & b : beforeUpdate)
|
|
|
- {
|
|
|
- //We should not run updaters on non-selected bonuses
|
|
|
- auto updated = selector(b.get()) && b->updater
|
|
|
- ? getUpdatedBonus(b, b->updater)
|
|
|
- : b;
|
|
|
-
|
|
|
- //do not add bonus with updater
|
|
|
- bool bonusExists = false;
|
|
|
- for(const auto & bonus : out)
|
|
|
- {
|
|
|
- if (bonus == updated)
|
|
|
- bonusExists = true;
|
|
|
- if (bonus->updater && bonus->updater == updated->updater)
|
|
|
- bonusExists = true;
|
|
|
- }
|
|
|
-
|
|
|
- if (!bonusExists)
|
|
|
- out.push_back(updated);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-TConstBonusListPtr CBonusSystemNode::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root, const std::string &cachingStr) const
|
|
|
-{
|
|
|
- bool limitOnUs = (!root || root == this); //caching won't work when we want to limit bonuses against an external node
|
|
|
- if (CBonusSystemNode::cachingEnabled && limitOnUs)
|
|
|
- {
|
|
|
- // Exclusive access for one thread
|
|
|
- boost::lock_guard<boost::mutex> lock(sync);
|
|
|
-
|
|
|
- // If the bonus system tree changes(state of a single node or the relations to each other) then
|
|
|
- // cache all bonus objects. Selector objects doesn't matter.
|
|
|
- if (cachedLast != treeChanged)
|
|
|
- {
|
|
|
- BonusList allBonuses;
|
|
|
- allBonuses.reserve(cachedBonuses.capacity()); //we assume we'll get about the same number of bonuses
|
|
|
-
|
|
|
- cachedBonuses.clear();
|
|
|
- cachedRequests.clear();
|
|
|
-
|
|
|
- getAllBonusesRec(allBonuses, Selector::all);
|
|
|
- limitBonuses(allBonuses, cachedBonuses);
|
|
|
- cachedBonuses.stackBonuses();
|
|
|
-
|
|
|
- cachedLast = treeChanged;
|
|
|
- }
|
|
|
-
|
|
|
- // If a bonus system request comes with a caching string then look up in the map if there are any
|
|
|
- // pre-calculated bonus results. Limiters can't be cached so they have to be calculated.
|
|
|
- if(!cachingStr.empty())
|
|
|
- {
|
|
|
- auto it = cachedRequests.find(cachingStr);
|
|
|
- if(it != cachedRequests.end())
|
|
|
- {
|
|
|
- //Cached list contains bonuses for our query with applied limiters
|
|
|
- return it->second;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //We still don't have the bonuses (didn't returned them from cache)
|
|
|
- //Perform bonus selection
|
|
|
- auto ret = std::make_shared<BonusList>();
|
|
|
- cachedBonuses.getBonuses(*ret, selector, limit);
|
|
|
-
|
|
|
- // Save the results in the cache
|
|
|
- if(!cachingStr.empty())
|
|
|
- cachedRequests[cachingStr] = ret;
|
|
|
-
|
|
|
- return ret;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return getAllBonusesWithoutCaching(selector, limit, root);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-TConstBonusListPtr CBonusSystemNode::getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root) const
|
|
|
-{
|
|
|
- auto ret = std::make_shared<BonusList>();
|
|
|
-
|
|
|
- // Get bonus results without caching enabled.
|
|
|
- BonusList beforeLimiting;
|
|
|
- BonusList afterLimiting;
|
|
|
- getAllBonusesRec(beforeLimiting, selector);
|
|
|
-
|
|
|
- if(!root || root == this)
|
|
|
- {
|
|
|
- limitBonuses(beforeLimiting, afterLimiting);
|
|
|
- }
|
|
|
- else if(root)
|
|
|
- {
|
|
|
- //We want to limit our query against an external node. We get all its bonuses,
|
|
|
- // add the ones we're considering and see if they're cut out by limiters
|
|
|
- BonusList rootBonuses;
|
|
|
- BonusList limitedRootBonuses;
|
|
|
- getAllBonusesRec(rootBonuses, selector);
|
|
|
-
|
|
|
- for(const auto & b : beforeLimiting)
|
|
|
- rootBonuses.push_back(b);
|
|
|
-
|
|
|
- root->limitBonuses(rootBonuses, limitedRootBonuses);
|
|
|
-
|
|
|
- for(const auto & b : beforeLimiting)
|
|
|
- if(vstd::contains(limitedRootBonuses, b))
|
|
|
- afterLimiting.push_back(b);
|
|
|
-
|
|
|
- }
|
|
|
- afterLimiting.getBonuses(*ret, selector, limit);
|
|
|
- ret->stackBonuses();
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-std::shared_ptr<Bonus> CBonusSystemNode::getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const
|
|
|
-{
|
|
|
- assert(updater);
|
|
|
- return updater->createUpdatedBonus(b, * this);
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::CBonusSystemNode()
|
|
|
- :CBonusSystemNode(false)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::CBonusSystemNode(bool isHypotetic):
|
|
|
- bonuses(true),
|
|
|
- exportedBonuses(true),
|
|
|
- nodeType(UNKNOWN),
|
|
|
- cachedLast(0),
|
|
|
- isHypotheticNode(isHypotetic)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::CBonusSystemNode(ENodeTypes NodeType):
|
|
|
- bonuses(true),
|
|
|
- exportedBonuses(true),
|
|
|
- nodeType(NodeType),
|
|
|
- cachedLast(0),
|
|
|
- isHypotheticNode(false)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::CBonusSystemNode(CBonusSystemNode && other) noexcept:
|
|
|
- bonuses(std::move(other.bonuses)),
|
|
|
- exportedBonuses(std::move(other.exportedBonuses)),
|
|
|
- nodeType(other.nodeType),
|
|
|
- description(other.description),
|
|
|
- cachedLast(0),
|
|
|
- isHypotheticNode(other.isHypotheticNode)
|
|
|
-{
|
|
|
- std::swap(parents, other.parents);
|
|
|
- std::swap(children, other.children);
|
|
|
-
|
|
|
- //fixing bonus tree without recalculation
|
|
|
-
|
|
|
- if(!isHypothetic())
|
|
|
- {
|
|
|
- for(CBonusSystemNode * n : parents)
|
|
|
- {
|
|
|
- n->children -= &other;
|
|
|
- n->children.push_back(this);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- for(CBonusSystemNode * n : children)
|
|
|
- {
|
|
|
- n->parents -= &other;
|
|
|
- n->parents.push_back(this);
|
|
|
- }
|
|
|
-
|
|
|
- //cache ignored
|
|
|
-
|
|
|
- //cachedBonuses
|
|
|
- //cachedRequests
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::~CBonusSystemNode()
|
|
|
-{
|
|
|
- detachFromAll();
|
|
|
-
|
|
|
- if(!children.empty())
|
|
|
- {
|
|
|
- while(!children.empty())
|
|
|
- children.front()->detachFrom(*this);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::attachTo(CBonusSystemNode & parent)
|
|
|
-{
|
|
|
- assert(!vstd::contains(parents, &parent));
|
|
|
- parents.push_back(&parent);
|
|
|
-
|
|
|
- if(!isHypothetic())
|
|
|
- {
|
|
|
- if(parent.actsAsBonusSourceOnly())
|
|
|
- parent.newRedDescendant(*this);
|
|
|
- else
|
|
|
- newRedDescendant(parent);
|
|
|
-
|
|
|
- parent.newChildAttached(*this);
|
|
|
- }
|
|
|
-
|
|
|
- CBonusSystemNode::treeHasChanged();
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::detachFrom(CBonusSystemNode & parent)
|
|
|
-{
|
|
|
- assert(vstd::contains(parents, &parent));
|
|
|
-
|
|
|
- if(!isHypothetic())
|
|
|
- {
|
|
|
- if(parent.actsAsBonusSourceOnly())
|
|
|
- parent.removedRedDescendant(*this);
|
|
|
- else
|
|
|
- removedRedDescendant(parent);
|
|
|
- }
|
|
|
-
|
|
|
- if (vstd::contains(parents, &parent))
|
|
|
- {
|
|
|
- parents -= &parent;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- logBonus->error("Error on Detach. Node %s (nodeType=%d) has not parent %s (nodeType=%d)"
|
|
|
- , nodeShortInfo(), nodeType, parent.nodeShortInfo(), parent.nodeType);
|
|
|
- }
|
|
|
-
|
|
|
- if(!isHypothetic())
|
|
|
- {
|
|
|
- parent.childDetached(*this);
|
|
|
- }
|
|
|
- CBonusSystemNode::treeHasChanged();
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::removeBonusesRecursive(const CSelector & s)
|
|
|
-{
|
|
|
- removeBonuses(s);
|
|
|
- for(CBonusSystemNode * child : children)
|
|
|
- child->removeBonusesRecursive(s);
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::reduceBonusDurations(const CSelector &s)
|
|
|
-{
|
|
|
- BonusList bl;
|
|
|
- exportedBonuses.getBonuses(bl, s, Selector::all);
|
|
|
- for(const auto & b : bl)
|
|
|
- {
|
|
|
- b->turnsRemain--;
|
|
|
- if(b->turnsRemain <= 0)
|
|
|
- removeBonus(b);
|
|
|
- }
|
|
|
-
|
|
|
- for(CBonusSystemNode *child : children)
|
|
|
- child->reduceBonusDurations(s);
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::addNewBonus(const std::shared_ptr<Bonus>& b)
|
|
|
-{
|
|
|
- //turnsRemain shouldn't be zero for following durations
|
|
|
- if(Bonus::NTurns(b.get()) || Bonus::NDays(b.get()) || Bonus::OneWeek(b.get()))
|
|
|
- {
|
|
|
- assert(b->turnsRemain);
|
|
|
- }
|
|
|
-
|
|
|
- assert(!vstd::contains(exportedBonuses, b));
|
|
|
- exportedBonuses.push_back(b);
|
|
|
- exportBonus(b);
|
|
|
- CBonusSystemNode::treeHasChanged();
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::accumulateBonus(const std::shared_ptr<Bonus>& b)
|
|
|
-{
|
|
|
- auto bonus = exportedBonuses.getFirst(Selector::typeSubtype(b->type, b->subtype)); //only local bonuses are interesting //TODO: what about value type?
|
|
|
- if(bonus)
|
|
|
- bonus->val += b->val;
|
|
|
- else
|
|
|
- addNewBonus(std::make_shared<Bonus>(*b)); //duplicate needed, original may get destroyed
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::removeBonus(const std::shared_ptr<Bonus>& b)
|
|
|
-{
|
|
|
- exportedBonuses -= b;
|
|
|
- if(b->propagator)
|
|
|
- unpropagateBonus(b);
|
|
|
- else
|
|
|
- bonuses -= b;
|
|
|
- CBonusSystemNode::treeHasChanged();
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::removeBonuses(const CSelector & selector)
|
|
|
-{
|
|
|
- BonusList toRemove;
|
|
|
- exportedBonuses.getBonuses(toRemove, selector, Selector::all);
|
|
|
- for(const auto & bonus : toRemove)
|
|
|
- removeBonus(bonus);
|
|
|
-}
|
|
|
-
|
|
|
-bool CBonusSystemNode::actsAsBonusSourceOnly() const
|
|
|
-{
|
|
|
- switch(nodeType)
|
|
|
- {
|
|
|
- case CREATURE:
|
|
|
- case ARTIFACT:
|
|
|
- case ARTIFACT_INSTANCE:
|
|
|
- return true;
|
|
|
- default:
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source)
|
|
|
-{
|
|
|
- if(b->propagator->shouldBeAttached(this))
|
|
|
- {
|
|
|
- auto propagated = b->propagationUpdater
|
|
|
- ? source.getUpdatedBonus(b, b->propagationUpdater)
|
|
|
- : b;
|
|
|
- bonuses.push_back(propagated);
|
|
|
- logBonus->trace("#$# %s #propagated to# %s", propagated->Description(), nodeName());
|
|
|
- }
|
|
|
-
|
|
|
- FOREACH_RED_CHILD(child)
|
|
|
- child->propagateBonus(b, source);
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::unpropagateBonus(const std::shared_ptr<Bonus> & b)
|
|
|
-{
|
|
|
- if(b->propagator->shouldBeAttached(this))
|
|
|
- {
|
|
|
- bonuses -= b;
|
|
|
- logBonus->trace("#$# %s #is no longer propagated to# %s", b->Description(), nodeName());
|
|
|
- }
|
|
|
-
|
|
|
- FOREACH_RED_CHILD(child)
|
|
|
- child->unpropagateBonus(b);
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::newChildAttached(CBonusSystemNode & child)
|
|
|
-{
|
|
|
- assert(!vstd::contains(children, &child));
|
|
|
- children.push_back(&child);
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::childDetached(CBonusSystemNode & child)
|
|
|
-{
|
|
|
- if(vstd::contains(children, &child))
|
|
|
- children -= &child;
|
|
|
- else
|
|
|
- {
|
|
|
- logBonus->error("Error on Detach. Node %s (nodeType=%d) is not a child of %s (nodeType=%d)"
|
|
|
- , child.nodeShortInfo(), child.nodeType, nodeShortInfo(), nodeType);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::detachFromAll()
|
|
|
-{
|
|
|
- while(!parents.empty())
|
|
|
- detachFrom(*parents.front());
|
|
|
-}
|
|
|
-
|
|
|
-bool CBonusSystemNode::isIndependentNode() const
|
|
|
-{
|
|
|
- return parents.empty() && children.empty();
|
|
|
-}
|
|
|
-
|
|
|
-std::string CBonusSystemNode::nodeName() const
|
|
|
-{
|
|
|
- return !description.empty()
|
|
|
- ? description
|
|
|
- : std::string("Bonus system node of type ") + typeid(*this).name();
|
|
|
-}
|
|
|
-
|
|
|
-std::string CBonusSystemNode::nodeShortInfo() const
|
|
|
-{
|
|
|
- std::ostringstream str;
|
|
|
- str << "'" << typeid(* this).name() << "'";
|
|
|
- description.length() > 0
|
|
|
- ? str << " (" << description << ")"
|
|
|
- : str << " (no description)";
|
|
|
- return str.str();
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::deserializationFix()
|
|
|
-{
|
|
|
- exportBonuses();
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::getRedParents(TNodes & out)
|
|
|
-{
|
|
|
- FOREACH_PARENT(pname)
|
|
|
- {
|
|
|
- if(pname->actsAsBonusSourceOnly())
|
|
|
- {
|
|
|
- out.insert(pname);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(!actsAsBonusSourceOnly())
|
|
|
- {
|
|
|
- for(CBonusSystemNode *child : children)
|
|
|
- {
|
|
|
- out.insert(child);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::getRedChildren(TNodes &out)
|
|
|
-{
|
|
|
- FOREACH_PARENT(pname)
|
|
|
- {
|
|
|
- if(!pname->actsAsBonusSourceOnly())
|
|
|
- {
|
|
|
- out.insert(pname);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(actsAsBonusSourceOnly())
|
|
|
- {
|
|
|
- for(CBonusSystemNode *child : children)
|
|
|
- {
|
|
|
- out.insert(child);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::newRedDescendant(CBonusSystemNode & descendant)
|
|
|
-{
|
|
|
- for(const auto & b : exportedBonuses)
|
|
|
- {
|
|
|
- if(b->propagator)
|
|
|
- descendant.propagateBonus(b, *this);
|
|
|
- }
|
|
|
- TNodes redParents;
|
|
|
- getRedAncestors(redParents); //get all red parents recursively
|
|
|
-
|
|
|
- for(auto * parent : redParents)
|
|
|
- {
|
|
|
- for(const auto & b : parent->exportedBonuses)
|
|
|
- {
|
|
|
- if(b->propagator)
|
|
|
- descendant.propagateBonus(b, *this);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::removedRedDescendant(CBonusSystemNode & descendant)
|
|
|
-{
|
|
|
- for(const auto & b : exportedBonuses)
|
|
|
- if(b->propagator)
|
|
|
- descendant.unpropagateBonus(b);
|
|
|
-
|
|
|
- TNodes redParents;
|
|
|
- getRedAncestors(redParents); //get all red parents recursively
|
|
|
-
|
|
|
- for(auto * parent : redParents)
|
|
|
- {
|
|
|
- for(const auto & b : parent->exportedBonuses)
|
|
|
- if(b->propagator)
|
|
|
- descendant.unpropagateBonus(b);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::getRedAncestors(TNodes &out)
|
|
|
-{
|
|
|
- getRedParents(out);
|
|
|
-
|
|
|
- TNodes redParents;
|
|
|
- getRedParents(redParents);
|
|
|
-
|
|
|
- for(CBonusSystemNode * parent : redParents)
|
|
|
- parent->getRedAncestors(out);
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::exportBonus(const std::shared_ptr<Bonus> & b)
|
|
|
-{
|
|
|
- if(b->propagator)
|
|
|
- propagateBonus(b, *this);
|
|
|
- else
|
|
|
- bonuses.push_back(b);
|
|
|
-
|
|
|
- CBonusSystemNode::treeHasChanged();
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::exportBonuses()
|
|
|
-{
|
|
|
- for(const auto & b : exportedBonuses)
|
|
|
- exportBonus(b);
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::ENodeTypes CBonusSystemNode::getNodeType() const
|
|
|
-{
|
|
|
- return nodeType;
|
|
|
-}
|
|
|
-
|
|
|
-const BonusList& CBonusSystemNode::getBonusList() const
|
|
|
-{
|
|
|
- return bonuses;
|
|
|
-}
|
|
|
-
|
|
|
-const TNodesVector& CBonusSystemNode::getParentNodes() const
|
|
|
-{
|
|
|
- return parents;
|
|
|
-}
|
|
|
-
|
|
|
-const TNodesVector& CBonusSystemNode::getChildrenNodes() const
|
|
|
-{
|
|
|
- return children;
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::setNodeType(CBonusSystemNode::ENodeTypes type)
|
|
|
-{
|
|
|
- nodeType = type;
|
|
|
-}
|
|
|
-
|
|
|
-BonusList & CBonusSystemNode::getExportedBonusList()
|
|
|
-{
|
|
|
- return exportedBonuses;
|
|
|
-}
|
|
|
-
|
|
|
-const BonusList & CBonusSystemNode::getExportedBonusList() const
|
|
|
-{
|
|
|
- return exportedBonuses;
|
|
|
-}
|
|
|
-
|
|
|
-const std::string& CBonusSystemNode::getDescription() const
|
|
|
-{
|
|
|
- return description;
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::setDescription(const std::string &description)
|
|
|
-{
|
|
|
- this->description = description;
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::limitBonuses(const BonusList &allBonuses, BonusList &out) const
|
|
|
-{
|
|
|
- assert(&allBonuses != &out); //todo should it work in-place?
|
|
|
-
|
|
|
- BonusList undecided = allBonuses;
|
|
|
- BonusList & accepted = out;
|
|
|
-
|
|
|
- while(true)
|
|
|
- {
|
|
|
- int undecidedCount = static_cast<int>(undecided.size());
|
|
|
- for(int i = 0; i < undecided.size(); i++)
|
|
|
- {
|
|
|
- auto b = undecided[i];
|
|
|
- BonusLimitationContext context = {*b, *this, out, undecided};
|
|
|
- auto decision = b->limiter ? b->limiter->limit(context) : ILimiter::EDecision::ACCEPT; //bonuses without limiters will be accepted by default
|
|
|
- if(decision == ILimiter::EDecision::DISCARD)
|
|
|
- {
|
|
|
- undecided.erase(i);
|
|
|
- i--; continue;
|
|
|
- }
|
|
|
- else if(decision == ILimiter::EDecision::ACCEPT)
|
|
|
- {
|
|
|
- accepted.push_back(b);
|
|
|
- undecided.erase(i);
|
|
|
- i--; continue;
|
|
|
- }
|
|
|
- else
|
|
|
- assert(decision == ILimiter::EDecision::NOT_SURE);
|
|
|
- }
|
|
|
-
|
|
|
- if(undecided.size() == undecidedCount) //we haven't moved a single bonus -> limiters reached a stable state
|
|
|
- return;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-TBonusListPtr CBonusSystemNode::limitBonuses(const BonusList &allBonuses) const
|
|
|
-{
|
|
|
- auto ret = std::make_shared<BonusList>();
|
|
|
- limitBonuses(allBonuses, *ret);
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-void CBonusSystemNode::treeHasChanged()
|
|
|
-{
|
|
|
- treeChanged++;
|
|
|
-}
|
|
|
-
|
|
|
-int64_t CBonusSystemNode::getTreeVersion() const
|
|
|
-{
|
|
|
- return treeChanged;
|
|
|
-}
|
|
|
-
|
|
|
std::string Bonus::Description(std::optional<si32> customValue) const
|
|
|
{
|
|
|
std::ostringstream str;
|
|
|
@@ -1314,246 +637,6 @@ std::string Bonus::nameForBonus() const
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-BonusParams::BonusParams(std::string deprecatedTypeStr, std::string deprecatedSubtypeStr, int deprecatedSubtype):
|
|
|
- isConverted(true)
|
|
|
-{
|
|
|
- if(deprecatedTypeStr == "SECONDARY_SKILL_PREMY" || deprecatedTypeStr == "SPECIAL_SECONDARY_SKILL")
|
|
|
- {
|
|
|
- if(deprecatedSubtype == SecondarySkill::PATHFINDING || deprecatedSubtypeStr == "skill.pathfinding")
|
|
|
- type = Bonus::ROUGH_TERRAIN_DISCOUNT;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::DIPLOMACY || deprecatedSubtypeStr == "skill.diplomacy")
|
|
|
- type = Bonus::WANDERING_CREATURES_JOIN_BONUS;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::WISDOM || deprecatedSubtypeStr == "skill.wisdom")
|
|
|
- type = Bonus::MAX_LEARNABLE_SPELL_LEVEL;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::MYSTICISM || deprecatedSubtypeStr == "skill.mysticism")
|
|
|
- type = Bonus::MANA_REGENERATION;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::NECROMANCY || deprecatedSubtypeStr == "skill.necromancy")
|
|
|
- type = Bonus::UNDEAD_RAISE_PERCENTAGE;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::LEARNING || deprecatedSubtypeStr == "skill.learning")
|
|
|
- type = Bonus::HERO_EXPERIENCE_GAIN_PERCENT;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::RESISTANCE || deprecatedSubtypeStr == "skill.resistance")
|
|
|
- type = Bonus::MAGIC_RESISTANCE;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::EAGLE_EYE || deprecatedSubtypeStr == "skill.eagleEye")
|
|
|
- type = Bonus::LEARN_BATTLE_SPELL_CHANCE;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::SCOUTING || deprecatedSubtypeStr == "skill.scouting")
|
|
|
- type = Bonus::SIGHT_RADIUS;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::INTELLIGENCE || deprecatedSubtypeStr == "skill.intelligence")
|
|
|
- {
|
|
|
- type = Bonus::MANA_PER_KNOWLEDGE;
|
|
|
- valueType = Bonus::PERCENT_TO_BASE;
|
|
|
- valueTypeRelevant = true;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::SORCERY || deprecatedSubtypeStr == "skill.sorcery")
|
|
|
- type = Bonus::SPELL_DAMAGE;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::SCHOLAR || deprecatedSubtypeStr == "skill.scholar")
|
|
|
- type = Bonus::LEARN_MEETING_SPELL_LIMIT;
|
|
|
- else if(deprecatedSubtype == SecondarySkill::ARCHERY|| deprecatedSubtypeStr == "skill.archery")
|
|
|
- {
|
|
|
- subtype = 1;
|
|
|
- subtypeRelevant = true;
|
|
|
- type = Bonus::PERCENTAGE_DAMAGE_BOOST;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::OFFENCE || deprecatedSubtypeStr == "skill.offence")
|
|
|
- {
|
|
|
- subtype = 0;
|
|
|
- subtypeRelevant = true;
|
|
|
- type = Bonus::PERCENTAGE_DAMAGE_BOOST;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::ARMORER || deprecatedSubtypeStr == "skill.armorer")
|
|
|
- {
|
|
|
- subtype = -1;
|
|
|
- subtypeRelevant = true;
|
|
|
- type = Bonus::GENERAL_DAMAGE_REDUCTION;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::NAVIGATION || deprecatedSubtypeStr == "skill.navigation")
|
|
|
- {
|
|
|
- subtype = 0;
|
|
|
- subtypeRelevant = true;
|
|
|
- valueType = Bonus::PERCENT_TO_BASE;
|
|
|
- valueTypeRelevant = true;
|
|
|
- type = Bonus::MOVEMENT;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::LOGISTICS || deprecatedSubtypeStr == "skill.logistics")
|
|
|
- {
|
|
|
- subtype = 1;
|
|
|
- subtypeRelevant = true;
|
|
|
- valueType = Bonus::PERCENT_TO_BASE;
|
|
|
- valueTypeRelevant = true;
|
|
|
- type = Bonus::MOVEMENT;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::ESTATES || deprecatedSubtypeStr == "skill.estates")
|
|
|
- {
|
|
|
- type = Bonus::GENERATE_RESOURCE;
|
|
|
- subtype = GameResID(EGameResID::GOLD);
|
|
|
- subtypeRelevant = true;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::AIR_MAGIC || deprecatedSubtypeStr == "skill.airMagic")
|
|
|
- {
|
|
|
- type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtype = 4;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::WATER_MAGIC || deprecatedSubtypeStr == "skill.waterMagic")
|
|
|
- {
|
|
|
- type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtype = 1;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::FIRE_MAGIC || deprecatedSubtypeStr == "skill.fireMagic")
|
|
|
- {
|
|
|
- type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtype = 2;
|
|
|
- }
|
|
|
- else if(deprecatedSubtype == SecondarySkill::EARTH_MAGIC || deprecatedSubtypeStr == "skill.earthMagic")
|
|
|
- {
|
|
|
- type = Bonus::MAGIC_SCHOOL_SKILL;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtype = 8;
|
|
|
- }
|
|
|
- else if (deprecatedSubtype == SecondarySkill::ARTILLERY || deprecatedSubtypeStr == "skill.artillery")
|
|
|
- {
|
|
|
- type = Bonus::BONUS_DAMAGE_CHANCE;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtypeStr = "core:creature.ballista";
|
|
|
- }
|
|
|
- else if (deprecatedSubtype == SecondarySkill::FIRST_AID || deprecatedSubtypeStr == "skill.firstAid")
|
|
|
- {
|
|
|
- type = Bonus::SPECIFIC_SPELL_POWER;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtypeStr = "core:spell.firstAid";
|
|
|
- }
|
|
|
- else if (deprecatedSubtype == SecondarySkill::BALLISTICS || deprecatedSubtypeStr == "skill.ballistics")
|
|
|
- {
|
|
|
- type = Bonus::CATAPULT_EXTRA_SHOTS;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtypeStr = "core:spell.catapultShot";
|
|
|
- }
|
|
|
- else
|
|
|
- isConverted = false;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "SECONDARY_SKILL_VAL2")
|
|
|
- {
|
|
|
- if(deprecatedSubtype == SecondarySkill::EAGLE_EYE || deprecatedSubtypeStr == "skill.eagleEye")
|
|
|
- type = Bonus::LEARN_BATTLE_SPELL_LEVEL_LIMIT;
|
|
|
- else if (deprecatedSubtype == SecondarySkill::ARTILLERY || deprecatedSubtypeStr == "skill.artillery")
|
|
|
- {
|
|
|
- type = Bonus::HERO_GRANTS_ATTACKS;
|
|
|
- subtypeRelevant = true;
|
|
|
- subtypeStr = "core:creature.ballista";
|
|
|
- }
|
|
|
- else
|
|
|
- isConverted = false;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "SEA_MOVEMENT")
|
|
|
- {
|
|
|
- subtype = 0;
|
|
|
- subtypeRelevant = true;
|
|
|
- valueType = Bonus::ADDITIVE_VALUE;
|
|
|
- valueTypeRelevant = true;
|
|
|
- type = Bonus::MOVEMENT;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "LAND_MOVEMENT")
|
|
|
- {
|
|
|
- subtype = 1;
|
|
|
- subtypeRelevant = true;
|
|
|
- valueType = Bonus::ADDITIVE_VALUE;
|
|
|
- valueTypeRelevant = true;
|
|
|
- type = Bonus::MOVEMENT;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "MAXED_SPELL")
|
|
|
- {
|
|
|
- type = Bonus::SPELL;
|
|
|
- subtypeStr = deprecatedSubtypeStr;
|
|
|
- subtypeRelevant = true;
|
|
|
- valueType = Bonus::INDEPENDENT_MAX;
|
|
|
- valueTypeRelevant = true;
|
|
|
- val = 3;
|
|
|
- valRelevant = true;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "FULL_HP_REGENERATION")
|
|
|
- {
|
|
|
- type = Bonus::HP_REGENERATION;
|
|
|
- val = 100000; //very high value to always chose stack health
|
|
|
- valRelevant = true;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "KING1")
|
|
|
- {
|
|
|
- type = Bonus::KING;
|
|
|
- val = 0;
|
|
|
- valRelevant = true;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "KING2")
|
|
|
- {
|
|
|
- type = Bonus::KING;
|
|
|
- val = 2;
|
|
|
- valRelevant = true;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "KING3")
|
|
|
- {
|
|
|
- type = Bonus::KING;
|
|
|
- val = 3;
|
|
|
- valRelevant = true;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "SIGHT_RADIOUS")
|
|
|
- type = Bonus::SIGHT_RADIUS;
|
|
|
- else if (deprecatedTypeStr == "SELF_MORALE")
|
|
|
- {
|
|
|
- type = Bonus::MORALE;
|
|
|
- val = 1;
|
|
|
- valRelevant = true;
|
|
|
- valueType = Bonus::INDEPENDENT_MAX;
|
|
|
- valueTypeRelevant = true;
|
|
|
- }
|
|
|
- else if (deprecatedTypeStr == "SELF_LUCK")
|
|
|
- {
|
|
|
- type = Bonus::LUCK;
|
|
|
- val = 1;
|
|
|
- valRelevant = true;
|
|
|
- valueType = Bonus::INDEPENDENT_MAX;
|
|
|
- valueTypeRelevant = true;
|
|
|
- }
|
|
|
- else
|
|
|
- isConverted = false;
|
|
|
-}
|
|
|
-
|
|
|
-const JsonNode & BonusParams::toJson()
|
|
|
-{
|
|
|
- assert(isConverted);
|
|
|
- if(ret.isNull())
|
|
|
- {
|
|
|
- ret["type"].String() = vstd::findKey(bonusNameMap, type);
|
|
|
- if(subtypeRelevant && !subtypeStr.empty())
|
|
|
- ret["subtype"].String() = subtypeStr;
|
|
|
- else if(subtypeRelevant)
|
|
|
- ret["subtype"].Integer() = subtype;
|
|
|
- if(valueTypeRelevant)
|
|
|
- ret["valueType"].String() = vstd::findKey(bonusValueMap, valueType);
|
|
|
- if(valRelevant)
|
|
|
- ret["val"].Float() = val;
|
|
|
- if(targetTypeRelevant)
|
|
|
- ret["targetSourceType"].String() = vstd::findKey(bonusSourceMap, targetType);
|
|
|
- jsonCreated = true;
|
|
|
- }
|
|
|
- return ret;
|
|
|
-};
|
|
|
-
|
|
|
-CSelector BonusParams::toSelector()
|
|
|
-{
|
|
|
- assert(isConverted);
|
|
|
- if(subtypeRelevant && !subtypeStr.empty())
|
|
|
- JsonUtils::resolveIdentifier(subtype, toJson(), "subtype");
|
|
|
-
|
|
|
- auto ret = Selector::type()(type);
|
|
|
- if(subtypeRelevant)
|
|
|
- ret = ret.And(Selector::subtype()(subtype));
|
|
|
- if(valueTypeRelevant)
|
|
|
- ret = ret.And(Selector::valueType(valueType));
|
|
|
- if(targetTypeRelevant)
|
|
|
- ret = ret.And(Selector::targetSourceType()(targetType));
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
Bonus::Bonus(Bonus::BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype):
|
|
|
duration(static_cast<ui16>(Duration)),
|
|
|
type(Type),
|
|
|
@@ -1722,31 +805,6 @@ std::shared_ptr<Bonus> Bonus::addLimiter(const TLimiterPtr & Limiter)
|
|
|
return this->shared_from_this();
|
|
|
}
|
|
|
|
|
|
-bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
|
|
|
-{
|
|
|
- return false;
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::ENodeTypes IPropagator::getPropagatorType() const
|
|
|
-{
|
|
|
- return CBonusSystemNode::ENodeTypes::NONE;
|
|
|
-}
|
|
|
-
|
|
|
-CPropagatorNodeType::CPropagatorNodeType(CBonusSystemNode::ENodeTypes NodeType)
|
|
|
- : nodeType(NodeType)
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-CBonusSystemNode::ENodeTypes CPropagatorNodeType::getPropagatorType() const
|
|
|
-{
|
|
|
- return nodeType;
|
|
|
-}
|
|
|
-
|
|
|
-bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
|
|
|
-{
|
|
|
- return nodeType == dest->getNodeType();
|
|
|
-}
|
|
|
-
|
|
|
// Updaters
|
|
|
|
|
|
std::shared_ptr<Bonus> Bonus::addUpdater(const TUpdaterPtr & Updater)
|