2023-04-30 16:35:15 +02:00
|
|
|
/*
|
|
|
|
* CBonusSystemNode.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2023-05-05 23:45:09 +02:00
|
|
|
#include "GameConstants.h"
|
|
|
|
|
2023-04-30 19:34:25 +02:00
|
|
|
#include "BonusList.h"
|
2023-04-30 18:13:55 +02:00
|
|
|
#include "IBonusBearer.h"
|
2023-04-30 16:35:15 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
using TNodes = std::set<CBonusSystemNode *>;
|
|
|
|
using TCNodes = std::set<const CBonusSystemNode *>;
|
|
|
|
using TNodesVector = std::vector<CBonusSystemNode *>;
|
2024-01-01 12:49:17 +02:00
|
|
|
using TCNodesVector = std::vector<const CBonusSystemNode *>;
|
2023-04-30 16:35:15 +02:00
|
|
|
|
|
|
|
class DLL_LINKAGE CBonusSystemNode : public virtual IBonusBearer, public boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum ENodeTypes
|
|
|
|
{
|
|
|
|
NONE = -1,
|
|
|
|
UNKNOWN, STACK_INSTANCE, STACK_BATTLE, SPECIALTY, ARTIFACT, CREATURE, ARTIFACT_INSTANCE, HERO, PLAYER, TEAM,
|
|
|
|
TOWN_AND_VISITOR, BATTLE, COMMANDER, GLOBAL_EFFECTS, ALL_CREATURES, TOWN
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
BonusList bonuses; //wielded bonuses (local or up-propagated here)
|
|
|
|
BonusList exportedBonuses; //bonuses coming from this node (wielded or propagated away)
|
|
|
|
|
2024-01-01 12:49:17 +02:00
|
|
|
TCNodesVector parentsToInherit; // we inherit bonuses from them
|
|
|
|
TNodesVector parentsToPropagate; // we may attach our bonuses to them
|
2023-04-30 16:35:15 +02:00
|
|
|
TNodesVector children;
|
|
|
|
|
|
|
|
ENodeTypes nodeType;
|
|
|
|
bool isHypotheticNode;
|
|
|
|
|
|
|
|
static const bool cachingEnabled;
|
|
|
|
mutable BonusList cachedBonuses;
|
|
|
|
mutable int64_t cachedLast;
|
|
|
|
static std::atomic<int64_t> treeChanged;
|
|
|
|
|
|
|
|
// Setting a value to cachingStr before getting any bonuses caches the result for later requests.
|
|
|
|
// This string needs to be unique, that's why it has to be setted in the following manner:
|
|
|
|
// [property key]_[value] => only for selector
|
|
|
|
mutable std::map<std::string, TBonusListPtr > cachedRequests;
|
|
|
|
mutable boost::mutex sync;
|
|
|
|
|
|
|
|
void getAllBonusesRec(BonusList &out, const CSelector & selector) const;
|
|
|
|
TConstBonusListPtr getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr) const;
|
|
|
|
std::shared_ptr<Bonus> getUpdatedBonus(const std::shared_ptr<Bonus> & b, const TUpdaterPtr & updater) const;
|
|
|
|
|
2024-01-01 00:44:09 +02:00
|
|
|
void getRedParents(TCNodes &out) const; //retrieves list of red parent nodes (nodes bonuses propagate from)
|
|
|
|
void getRedAncestors(TCNodes &out) const;
|
2023-05-02 16:54:18 +02:00
|
|
|
void getRedChildren(TNodes &out);
|
|
|
|
|
|
|
|
void getAllParents(TCNodes & out) const;
|
|
|
|
|
|
|
|
void newChildAttached(CBonusSystemNode & child);
|
|
|
|
void childDetached(CBonusSystemNode & child);
|
|
|
|
void propagateBonus(const std::shared_ptr<Bonus> & b, const CBonusSystemNode & source);
|
|
|
|
void unpropagateBonus(const std::shared_ptr<Bonus> & b);
|
|
|
|
bool actsAsBonusSourceOnly() const;
|
|
|
|
|
2024-01-01 12:49:17 +02:00
|
|
|
void newRedDescendant(CBonusSystemNode & descendant) const; //propagation needed
|
|
|
|
void removedRedDescendant(CBonusSystemNode & descendant) const; //de-propagation needed
|
2023-05-02 16:54:18 +02:00
|
|
|
|
|
|
|
std::string nodeShortInfo() const;
|
|
|
|
|
|
|
|
void exportBonus(const std::shared_ptr<Bonus> & b);
|
|
|
|
|
2023-05-03 15:09:21 +02:00
|
|
|
protected:
|
|
|
|
bool isIndependentNode() const; //node is independent when it has no parents nor children
|
|
|
|
void exportBonuses();
|
|
|
|
|
2023-04-30 16:35:15 +02:00
|
|
|
public:
|
2023-05-02 16:54:18 +02:00
|
|
|
explicit CBonusSystemNode(bool isHypotetic = false);
|
2023-04-30 16:35:15 +02:00
|
|
|
explicit CBonusSystemNode(ENodeTypes NodeType);
|
|
|
|
virtual ~CBonusSystemNode();
|
|
|
|
|
|
|
|
void limitBonuses(const BonusList &allBonuses, BonusList &out) const; //out will bo populed with bonuses that are not limited here
|
|
|
|
TBonusListPtr limitBonuses(const BonusList &allBonuses) const; //same as above, returns out by val for convienence
|
|
|
|
TConstBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = nullptr, const std::string &cachingStr = "") const override;
|
|
|
|
void getParents(TCNodes &out) const; //retrieves list of parent nodes (nodes to inherit bonuses from),
|
|
|
|
|
2024-01-16 18:14:40 +02:00
|
|
|
/// Returns first bonus matching selector
|
|
|
|
std::shared_ptr<const Bonus> getFirstBonus(const CSelector & selector) const;
|
|
|
|
|
|
|
|
/// Provides write access to first bonus from this node that matches selector
|
|
|
|
std::shared_ptr<Bonus> getLocalBonus(const CSelector & selector);
|
2023-04-30 16:35:15 +02:00
|
|
|
|
|
|
|
void attachTo(CBonusSystemNode & parent);
|
2024-01-01 12:49:17 +02:00
|
|
|
void attachToSource(const CBonusSystemNode & parent);
|
2023-04-30 16:35:15 +02:00
|
|
|
void detachFrom(CBonusSystemNode & parent);
|
2024-01-01 12:49:17 +02:00
|
|
|
void detachFromSource(const CBonusSystemNode & parent);
|
2023-04-30 16:35:15 +02:00
|
|
|
void detachFromAll();
|
|
|
|
virtual void addNewBonus(const std::shared_ptr<Bonus>& b);
|
|
|
|
void accumulateBonus(const std::shared_ptr<Bonus>& b); //add value of bonus with same type/subtype or create new
|
|
|
|
|
|
|
|
void removeBonus(const std::shared_ptr<Bonus>& b);
|
|
|
|
void removeBonuses(const CSelector & selector);
|
|
|
|
void removeBonusesRecursive(const CSelector & s);
|
|
|
|
|
|
|
|
///updates count of remaining turns and removes outdated bonuses by selector
|
|
|
|
void reduceBonusDurations(const CSelector &s);
|
|
|
|
virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const {return "";}; //description or bonus name
|
|
|
|
virtual std::string nodeName() const;
|
|
|
|
bool isHypothetic() const { return isHypotheticNode; }
|
|
|
|
|
|
|
|
void deserializationFix();
|
|
|
|
|
|
|
|
BonusList & getExportedBonusList();
|
|
|
|
const BonusList & getExportedBonusList() const;
|
|
|
|
CBonusSystemNode::ENodeTypes getNodeType() const;
|
|
|
|
void setNodeType(CBonusSystemNode::ENodeTypes type);
|
2024-01-10 19:43:34 +02:00
|
|
|
const TCNodesVector & getParentNodes() const;
|
2023-04-30 16:35:15 +02:00
|
|
|
|
|
|
|
static void treeHasChanged();
|
|
|
|
|
|
|
|
int64_t getTreeVersion() const override;
|
|
|
|
|
|
|
|
virtual PlayerColor getOwner() const
|
|
|
|
{
|
|
|
|
return PlayerColor::NEUTRAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & nodeType;
|
|
|
|
h & exportedBonuses;
|
|
|
|
BONUS_TREE_DESERIALIZATION_FIX
|
|
|
|
}
|
|
|
|
|
|
|
|
friend class CBonusProxy;
|
|
|
|
};
|
|
|
|
|
2024-01-01 00:44:09 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|