2009-04-15 17:03:31 +03:00
|
|
|
/*
|
2023-05-01 19:29:53 +02:00
|
|
|
* Bonus.h, part of VCMI engine
|
2009-04-15 17:03:31 +03: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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
#include "BonusEnum.h"
|
2009-04-15 17:03:31 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
struct Bonus;
|
2017-07-04 13:24:46 +02:00
|
|
|
class IBonusBearer;
|
2010-05-02 21:20:26 +03:00
|
|
|
class CBonusSystemNode;
|
2010-07-12 13:20:25 +03:00
|
|
|
class ILimiter;
|
2010-11-19 00:22:51 +02:00
|
|
|
class IPropagator;
|
2017-09-09 07:43:53 +02:00
|
|
|
class IUpdater;
|
2011-09-06 16:59:26 +03:00
|
|
|
class BonusList;
|
2023-05-05 23:45:09 +02:00
|
|
|
class CSelector;
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2023-04-30 18:23:11 +02:00
|
|
|
using TBonusSubtype = int32_t;
|
2023-04-17 23:11:16 +02:00
|
|
|
using TBonusListPtr = std::shared_ptr<BonusList>;
|
|
|
|
using TConstBonusListPtr = std::shared_ptr<const BonusList>;
|
|
|
|
using TLimiterPtr = std::shared_ptr<ILimiter>;
|
|
|
|
using TPropagatorPtr = std::shared_ptr<IPropagator>;
|
|
|
|
using TUpdaterPtr = std::shared_ptr<IUpdater>;
|
2013-07-02 15:08:30 +03:00
|
|
|
|
2018-03-12 07:20:18 +02:00
|
|
|
class DLL_LINKAGE CAddInfo : public std::vector<si32>
|
|
|
|
{
|
|
|
|
public:
|
2018-03-17 00:03:02 +02:00
|
|
|
enum { NONE = -1 };
|
2018-03-12 07:20:18 +02:00
|
|
|
|
|
|
|
CAddInfo();
|
|
|
|
CAddInfo(si32 value);
|
|
|
|
|
|
|
|
bool operator==(si32 value) const;
|
|
|
|
bool operator!=(si32 value) const;
|
|
|
|
|
2018-03-17 00:03:02 +02:00
|
|
|
si32 & operator[](size_type pos);
|
2018-03-12 07:20:18 +02:00
|
|
|
si32 operator[](size_type pos) const;
|
|
|
|
|
|
|
|
std::string toString() const;
|
|
|
|
JsonNode toJsonNode() const;
|
|
|
|
};
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
#define BONUS_TREE_DESERIALIZATION_FIX if(!h.saving && h.smartPointerSerialization) deserializationFix();
|
|
|
|
|
2011-09-24 04:15:36 +03:00
|
|
|
/// Struct for handling bonuses of several types. Can be transferred to any hero
|
2016-09-19 23:36:35 +02:00
|
|
|
struct DLL_LINKAGE Bonus : public std::enable_shared_from_this<Bonus>
|
2009-02-04 15:40:54 +02:00
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
BonusDuration::Type duration = BonusDuration::PERMANENT; //uses BonusDuration values
|
2023-03-13 23:26:44 +02:00
|
|
|
si16 turnsRemain = 0; //used if duration is N_TURNS, N_DAYS or ONE_WEEK
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
BonusType type = BonusType::NONE; //uses BonusType values - says to what is this bonus - 1 byte
|
2023-03-13 23:26:44 +02:00
|
|
|
TBonusSubtype subtype = -1; //-1 if not applicable - 4 bytes
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
BonusSource source = BonusSource::OTHER; //source type" uses BonusSource values - what gave that bonus
|
2023-02-15 21:19:35 +02:00
|
|
|
BonusSource targetSourceType;//Bonuses of what origin this amplifies, uses BonusSource values. Needed for PERCENT_TO_TARGET_TYPE.
|
2023-03-13 23:26:44 +02:00
|
|
|
si32 val = 0;
|
|
|
|
ui32 sid = 0; //source id: id of object/artifact/spell
|
2023-05-01 00:20:01 +02:00
|
|
|
BonusValueType valType = BonusValueType::ADDITIVE_VALUE;
|
2018-03-27 09:54:58 +02:00
|
|
|
std::string stacking; // bonuses with the same stacking value don't stack (e.g. Angel/Archangel morale bonus)
|
2010-05-12 05:32:56 +03:00
|
|
|
|
2018-03-12 07:20:18 +02:00
|
|
|
CAddInfo additionalInfo;
|
2023-05-01 00:20:01 +02:00
|
|
|
BonusLimitEffect effectRange = BonusLimitEffect::NO_LIMIT; //if not NO_LIMIT, bonus will be omitted by default
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2013-01-20 15:06:18 +03:00
|
|
|
TLimiterPtr limiter;
|
2012-03-06 19:59:55 +03:00
|
|
|
TPropagatorPtr propagator;
|
2017-09-09 07:43:53 +02:00
|
|
|
TUpdaterPtr updater;
|
2021-09-12 13:30:54 +02:00
|
|
|
TUpdaterPtr propagationUpdater;
|
2010-07-12 13:20:25 +03:00
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
std::string description;
|
2009-02-04 15:40:54 +02:00
|
|
|
|
2023-05-05 11:56:53 +02:00
|
|
|
Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1);
|
|
|
|
Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype=-1, BonusValueType ValType = BonusValueType::ADDITIVE_VALUE);
|
2023-03-13 23:26:44 +02:00
|
|
|
Bonus() = default;
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2009-02-04 15:40:54 +02:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2017-07-31 15:35:42 +02:00
|
|
|
h & duration;
|
|
|
|
h & type;
|
|
|
|
h & subtype;
|
|
|
|
h & source;
|
|
|
|
h & val;
|
|
|
|
h & sid;
|
|
|
|
h & description;
|
2022-06-20 16:39:50 +02:00
|
|
|
h & additionalInfo;
|
2017-07-31 15:35:42 +02:00
|
|
|
h & turnsRemain;
|
|
|
|
h & valType;
|
2022-06-20 16:39:50 +02:00
|
|
|
h & stacking;
|
2017-07-31 15:35:42 +02:00
|
|
|
h & effectRange;
|
|
|
|
h & limiter;
|
|
|
|
h & propagator;
|
2022-06-20 16:39:50 +02:00
|
|
|
h & updater;
|
|
|
|
h & propagationUpdater;
|
2023-02-15 21:19:35 +02:00
|
|
|
h & targetSourceType;
|
2009-02-04 15:40:54 +02:00
|
|
|
}
|
2009-02-06 13:15:39 +02:00
|
|
|
|
2016-09-19 23:36:35 +02:00
|
|
|
template <typename Ptr>
|
|
|
|
static bool compareByAdditionalInfo(const Ptr& a, const Ptr& b)
|
2011-09-06 16:59:26 +03:00
|
|
|
{
|
|
|
|
return a->additionalInfo < b->additionalInfo;
|
|
|
|
}
|
2015-04-13 09:24:32 +02:00
|
|
|
static bool NDays(const Bonus *hb)
|
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::N_DAYS;
|
|
|
|
return set.any();
|
2015-04-13 09:24:32 +02:00
|
|
|
}
|
|
|
|
static bool NTurns(const Bonus *hb)
|
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::N_TURNS;
|
|
|
|
return set.any();
|
2016-01-31 17:01:58 +02:00
|
|
|
}
|
2010-11-19 00:06:56 +02:00
|
|
|
static bool OneDay(const Bonus *hb)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::ONE_DAY;
|
|
|
|
return set.any();
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-11-19 00:06:56 +02:00
|
|
|
static bool OneWeek(const Bonus *hb)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::ONE_WEEK;
|
|
|
|
return set.any();
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-11-19 00:06:56 +02:00
|
|
|
static bool OneBattle(const Bonus *hb)
|
2009-02-06 13:15:39 +02:00
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::ONE_BATTLE;
|
|
|
|
return set.any();
|
2009-02-06 13:15:39 +02:00
|
|
|
}
|
2015-04-13 09:24:32 +02:00
|
|
|
static bool Permanent(const Bonus *hb)
|
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::PERMANENT;
|
|
|
|
return set.any();
|
2015-04-13 09:24:32 +02:00
|
|
|
}
|
2011-01-18 19:23:31 +02:00
|
|
|
static bool UntilGetsTurn(const Bonus *hb)
|
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::STACK_GETS_TURN;
|
|
|
|
return set.any();
|
2011-01-18 19:23:31 +02:00
|
|
|
}
|
2010-11-19 00:06:56 +02:00
|
|
|
static bool UntilAttack(const Bonus *hb)
|
2009-02-06 13:15:39 +02:00
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::UNTIL_ATTACK;
|
|
|
|
return set.any();
|
2009-02-06 13:15:39 +02:00
|
|
|
}
|
2010-11-19 00:06:56 +02:00
|
|
|
static bool UntilBeingAttacked(const Bonus *hb)
|
2009-02-06 13:15:39 +02:00
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::UNTIL_BEING_ATTACKED;
|
|
|
|
return set.any();
|
2009-02-06 13:15:39 +02:00
|
|
|
}
|
2012-07-16 19:18:02 +03:00
|
|
|
static bool UntilCommanderKilled(const Bonus *hb)
|
|
|
|
{
|
2023-05-05 11:56:53 +02:00
|
|
|
auto set = hb->duration & BonusDuration::COMMANDER_KILLED;
|
|
|
|
return set.any();
|
2012-07-16 19:18:02 +03:00
|
|
|
}
|
2010-08-04 14:18:13 +03:00
|
|
|
inline bool operator == (const BonusType & cf) const
|
|
|
|
{
|
|
|
|
return type == cf;
|
|
|
|
}
|
2010-07-08 22:10:26 +03:00
|
|
|
inline void operator += (const ui32 Val) //no return
|
|
|
|
{
|
|
|
|
val += Val;
|
|
|
|
}
|
2021-01-14 00:02:13 +02:00
|
|
|
STRONG_INLINE static ui32 getSid32(ui32 high, ui32 low)
|
|
|
|
{
|
|
|
|
return (high << 16) + low;
|
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2023-04-09 03:36:16 +02:00
|
|
|
STRONG_INLINE static ui32 getHighFromSid32(ui32 sid)
|
|
|
|
{
|
|
|
|
return sid >> 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
STRONG_INLINE static ui32 getLowFromSid32(ui32 sid)
|
|
|
|
{
|
|
|
|
return sid & 0x0000FFFF;
|
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
std::string Description(std::optional<si32> customValue = {}) const;
|
2017-09-11 08:21:24 +02:00
|
|
|
JsonNode toJsonNode() const;
|
2010-11-20 02:03:31 +02:00
|
|
|
|
2023-03-13 23:26:44 +02:00
|
|
|
std::shared_ptr<Bonus> addLimiter(const TLimiterPtr & Limiter); //returns this for convenient chain-calls
|
|
|
|
std::shared_ptr<Bonus> addPropagator(const TPropagatorPtr & Propagator); //returns this for convenient chain-calls
|
|
|
|
std::shared_ptr<Bonus> addUpdater(const TUpdaterPtr & Updater); //returns this for convenient chain-calls
|
2009-02-06 13:15:39 +02:00
|
|
|
};
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus);
|
2010-07-12 13:20:25 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|