2010-02-10 04:56:00 +02:00
|
|
|
#define VCMI_DLL
|
|
|
|
#include "HeroBonus.h"
|
2010-04-03 06:33:46 +03:00
|
|
|
#include <boost/foreach.hpp>
|
2010-05-02 21:20:26 +03:00
|
|
|
#include "VCMI_Lib.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CSpellHandler.h"
|
2010-05-02 21:20:26 +03:00
|
|
|
#include <sstream>
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CCreatureHandler.h"
|
2010-07-12 13:20:25 +03:00
|
|
|
#include <boost/assign/list_of.hpp>
|
|
|
|
#include "CCreatureSet.h"
|
2010-07-13 08:25:40 +03:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2010-11-20 02:03:31 +02:00
|
|
|
#include <boost/bind.hpp>
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CHeroHandler.h"
|
2011-01-15 04:17:56 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
#define FOREACH_CONST_PARENT(pname) TCNodes parents; getParents(parents); BOOST_FOREACH(const CBonusSystemNode *pname, parents)
|
|
|
|
#define FOREACH_PARENT(pname) TNodes parents; getParents(parents); BOOST_FOREACH(CBonusSystemNode *pname, parents)
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2010-07-12 13:20:25 +03:00
|
|
|
#define BONUS_NAME(x) ( #x, Bonus::x )
|
|
|
|
DLL_EXPORT const std::map<std::string, int> bonusNameMap = boost::assign::map_list_of BONUS_LIST;
|
|
|
|
#undef BONUS_NAME
|
|
|
|
|
2010-12-12 01:11:26 +02:00
|
|
|
#define BONUS_LOG_LINE(x) tlog0 << x << std::endl
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
int DLL_EXPORT BonusList::totalValue() const
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
int base = 0;
|
|
|
|
int percentToBase = 0;
|
|
|
|
int percentToAll = 0;
|
|
|
|
int additive = 0;
|
2010-08-05 12:11:38 +03:00
|
|
|
int indepMax = 0;
|
|
|
|
bool hasIndepMax = false;
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2010-11-19 00:06:56 +02:00
|
|
|
BOOST_FOREACH(Bonus *i, *this)
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
switch(i->valType)
|
|
|
|
{
|
|
|
|
case Bonus::BASE_NUMBER:
|
|
|
|
base += i->val;
|
|
|
|
break;
|
|
|
|
case Bonus::PERCENT_TO_ALL:
|
|
|
|
percentToAll += i->val;
|
|
|
|
break;
|
|
|
|
case Bonus::PERCENT_TO_BASE:
|
|
|
|
percentToBase += i->val;
|
|
|
|
break;
|
|
|
|
case Bonus::ADDITIVE_VALUE:
|
|
|
|
additive += i->val;
|
2010-08-05 12:11:38 +03:00
|
|
|
break;
|
|
|
|
case Bonus::INDEPENDENT_MAX:
|
|
|
|
if (!indepMax)
|
|
|
|
{
|
|
|
|
indepMax = i->val;
|
|
|
|
hasIndepMax = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
amax(indepMax, i->val);
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
2010-05-02 21:20:26 +03:00
|
|
|
int modifiedBase = base + (base * percentToBase) / 100;
|
|
|
|
modifiedBase += additive;
|
2010-08-05 12:11:38 +03:00
|
|
|
int valFirst = (modifiedBase * (100 + percentToAll)) / 100;
|
|
|
|
if (hasIndepMax)
|
|
|
|
return std::max(valFirst, indepMax);
|
|
|
|
else
|
|
|
|
return valFirst;
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
2010-05-02 21:20:26 +03:00
|
|
|
const DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &selector) const
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2010-11-19 00:06:56 +02:00
|
|
|
BOOST_FOREACH(Bonus *i, *this)
|
|
|
|
if(selector(i))
|
2010-05-02 21:20:26 +03:00
|
|
|
return &*i;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
DLL_EXPORT Bonus * BonusList::getFirst(const CSelector &select)
|
|
|
|
{
|
2010-11-19 00:06:56 +02:00
|
|
|
BOOST_FOREACH(Bonus *i, *this)
|
|
|
|
if(select(i))
|
2010-05-02 21:20:26 +03:00
|
|
|
return &*i;
|
|
|
|
return NULL;
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
void DLL_EXPORT BonusList::getModifiersWDescr(TModDescr &out) const
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2010-11-19 00:06:56 +02:00
|
|
|
BOOST_FOREACH(Bonus *i, *this)
|
2010-05-02 21:20:26 +03:00
|
|
|
out.push_back(std::make_pair(i->val, i->Description()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *source /*= NULL*/) const
|
|
|
|
{
|
2010-11-19 00:06:56 +02:00
|
|
|
BOOST_FOREACH(Bonus *i, *this)
|
|
|
|
if(selector(i) && i->effectRange == Bonus::NO_LIMIT)
|
|
|
|
out.push_back(i);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DLL_EXPORT BonusList::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit, const CBonusSystemNode *source /*= NULL*/) const
|
|
|
|
{
|
2010-11-19 00:06:56 +02:00
|
|
|
BOOST_FOREACH(Bonus *i, *this)
|
|
|
|
if(selector(i) && (!limit || limit(i)))
|
|
|
|
out.push_back(i);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-08-31 13:22:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
void DLL_EXPORT BonusList::removeSpells(Bonus::BonusSource sourceType)
|
|
|
|
{
|
2010-11-20 02:03:31 +02:00
|
|
|
remove_if(Selector::sourceType(sourceType));
|
2010-08-30 21:06:17 +03:00
|
|
|
}
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2010-07-12 13:20:25 +03:00
|
|
|
void BonusList::limit(const CBonusSystemNode &node)
|
|
|
|
{
|
2010-11-27 03:46:19 +02:00
|
|
|
remove_if(boost::bind(&CBonusSystemNode::isLimitedOnUs, boost::ref(node), _1));
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2010-08-25 11:40:50 +03:00
|
|
|
int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
|
|
|
|
{
|
|
|
|
return valOfBonuses(Selector::type(type) && selector);
|
|
|
|
}
|
2010-12-06 01:10:02 +02:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
int CBonusSystemNode::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
|
|
|
|
{
|
|
|
|
CSelector s = Selector::type(type);
|
|
|
|
if(subtype != -1)
|
|
|
|
s = s && Selector::subtype(subtype);
|
|
|
|
|
|
|
|
return valOfBonuses(s);
|
|
|
|
}
|
2010-12-06 01:10:02 +02:00
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
int CBonusSystemNode::valOfBonuses(const CSelector &selector) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
BonusList hlp;
|
2010-11-13 22:26:15 +02:00
|
|
|
getBonuses(hlp, selector);
|
2010-05-02 21:20:26 +03:00
|
|
|
return hlp.totalValue();
|
|
|
|
}
|
2010-11-13 22:26:15 +02:00
|
|
|
bool CBonusSystemNode::hasBonus(const CSelector &selector) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
return getBonuses(selector).size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBonusSystemNode::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
|
|
|
|
{
|
|
|
|
CSelector s = Selector::type(type);
|
|
|
|
if(subtype != -1)
|
|
|
|
s = s && Selector::subtype(subtype);
|
|
|
|
|
|
|
|
return hasBonus(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bonus * CBonusSystemNode::getBonus(const CSelector &selector)
|
|
|
|
{
|
|
|
|
Bonus *ret = bonuses.getFirst(selector);
|
|
|
|
if(ret)
|
|
|
|
return ret;
|
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
FOREACH_PARENT(pname)
|
2010-09-30 16:15:18 +03:00
|
|
|
{
|
2010-10-31 00:53:41 +03:00
|
|
|
ret = pname->getBonus(selector);
|
|
|
|
if (ret)
|
2010-05-02 21:20:26 +03:00
|
|
|
return ret;
|
2010-09-30 16:15:18 +03:00
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-17 20:47:07 +02:00
|
|
|
const Bonus * CBonusSystemNode::getBonus( const CSelector &selector ) const
|
|
|
|
{
|
|
|
|
return (const_cast<CBonusSystemNode*>(this))->getBonus(selector);
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
void CBonusSystemNode::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2011-01-15 04:17:56 +02:00
|
|
|
getModifiersWDescr(out, subtype != -1 ? Selector::typeSybtype(type, subtype) : Selector::type(type));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
void CBonusSystemNode::getModifiersWDescr(TModDescr &out, const CSelector &selector) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
getBonuses(selector).getModifiersWDescr(out);
|
|
|
|
}
|
|
|
|
int CBonusSystemNode::getBonusesCount(int from, int id) const
|
|
|
|
{
|
|
|
|
return getBonusesCount(Selector::source(from, id));
|
|
|
|
}
|
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
int CBonusSystemNode::getBonusesCount(const CSelector &selector) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2010-11-13 22:26:15 +02:00
|
|
|
return getBonuses(selector).size();
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2010-11-13 22:26:15 +02:00
|
|
|
BOOST_FOREACH(const CBonusSystemNode *parent, parents)
|
|
|
|
out.insert(parent);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
void CBonusSystemNode::getParents(TNodes &out)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2010-11-13 22:26:15 +02:00
|
|
|
BOOST_FOREACH(const CBonusSystemNode *parent, parents)
|
|
|
|
out.insert(const_cast<CBonusSystemNode*>(parent));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
|
|
|
|
{
|
2010-11-13 22:26:15 +02:00
|
|
|
FOREACH_CONST_PARENT(p) //unwinded macro
|
2010-05-02 21:20:26 +03:00
|
|
|
p->getBonuses(out, selector, root ? root : this);
|
2010-09-25 23:23:55 +03:00
|
|
|
|
|
|
|
bonuses.getBonuses(out, selector);
|
2010-07-12 13:20:25 +03:00
|
|
|
|
|
|
|
if(!root)
|
|
|
|
out.limit(*this);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
BonusList CBonusSystemNode::getBonuses(const CSelector &selector) const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
BonusList ret;
|
2010-11-13 22:26:15 +02:00
|
|
|
getBonuses(ret, selector);
|
2010-05-02 21:20:26 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
void CBonusSystemNode::getBonuses(BonusList &out, const CSelector &selector, const CSelector &limit) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2010-11-13 22:26:15 +02:00
|
|
|
getBonuses(out, selector); //first get all the bonuses
|
|
|
|
out.remove_if(std::not1(limit)); //now remove the ones we don't like
|
|
|
|
out.limit(*this); //apply bonuses' limiters
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
BonusList CBonusSystemNode::getBonuses(const CSelector &selector, const CSelector &limit) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
BonusList ret;
|
2010-11-13 22:26:15 +02:00
|
|
|
getBonuses(ret, selector, limit);
|
2010-04-03 06:33:46 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
bool CBonusSystemNode::hasBonusFrom(ui8 source, ui32 sourceID) const
|
|
|
|
{
|
|
|
|
return hasBonus(Selector::source(source,sourceID));
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBonusSystemNode::MoraleVal() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
|
|
|
|
hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
|
|
|
|
return 0;
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
int ret = valOfBonuses(Selector::type(Bonus::MORALE));
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
|
|
|
|
amax(ret, +1);
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
return abetw(ret, -3, +3);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
int CBonusSystemNode::LuckVal() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
if(hasBonusOfType(Bonus::NO_LUCK))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ret = valOfBonuses(Selector::type(Bonus::LUCK));
|
|
|
|
|
|
|
|
if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
|
|
|
|
amax(ret, +1);
|
|
|
|
|
|
|
|
return abetw(ret, -3, +3);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
si32 CBonusSystemNode::Attack() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
|
|
|
|
|
|
|
|
if(int frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
|
|
|
|
{
|
|
|
|
ret += frenzyPower * Defense(false);
|
|
|
|
}
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
return ret;
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
si32 CBonusSystemNode::Defense(bool withFrenzy /*= true*/) const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2010-04-03 06:33:46 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
ui16 CBonusSystemNode::MaxHealth() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
return valOfBonuses(Bonus::STACK_HEALTH);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2010-08-30 21:06:17 +03:00
|
|
|
ui32 CBonusSystemNode::getMinDamage() const
|
|
|
|
{
|
|
|
|
return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 1));
|
|
|
|
}
|
|
|
|
ui32 CBonusSystemNode::getMaxDamage() const
|
|
|
|
{
|
|
|
|
return valOfBonuses(Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 0) || Selector::typeSybtype(Bonus::CREATURE_DAMAGE, 2));
|
|
|
|
}
|
|
|
|
|
2010-07-12 13:20:25 +03:00
|
|
|
CBonusSystemNode::CBonusSystemNode()
|
|
|
|
{
|
|
|
|
nodeType = UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
CBonusSystemNode::~CBonusSystemNode()
|
|
|
|
{
|
2010-12-06 01:10:02 +02:00
|
|
|
detachFromAll();
|
2010-07-12 13:20:25 +03:00
|
|
|
|
2010-11-27 03:46:19 +02:00
|
|
|
if(children.size())
|
|
|
|
{
|
|
|
|
tlog2 << "Warning: an orphaned child!\n";
|
|
|
|
}
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2010-11-22 02:34:46 +02:00
|
|
|
void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
|
2010-11-13 22:26:15 +02:00
|
|
|
{
|
2010-11-22 02:34:46 +02:00
|
|
|
assert(!vstd::contains(parents, parent));
|
|
|
|
parents.push_back(parent);
|
2010-12-06 01:10:02 +02:00
|
|
|
// BOOST_FOREACH(Bonus *b, exportedBonuses)
|
|
|
|
// propagateBonus(b);
|
|
|
|
//
|
|
|
|
// if(parent->weActAsBonusSourceOnly())
|
|
|
|
// {
|
|
|
|
//
|
|
|
|
// }
|
2010-11-22 02:34:46 +02:00
|
|
|
|
|
|
|
parent->newChildAttached(this);
|
2010-11-13 22:26:15 +02:00
|
|
|
}
|
|
|
|
|
2010-11-22 02:34:46 +02:00
|
|
|
void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
|
2010-11-13 22:26:15 +02:00
|
|
|
{
|
2010-11-22 02:34:46 +02:00
|
|
|
assert(vstd::contains(parents, parent));
|
|
|
|
parents -= parent;
|
|
|
|
//unpropagate bonus
|
2010-11-13 22:26:15 +02:00
|
|
|
|
2010-11-22 02:34:46 +02:00
|
|
|
parent->childDetached(this);
|
2010-11-13 22:26:15 +02:00
|
|
|
}
|
|
|
|
|
2010-11-19 00:06:56 +02:00
|
|
|
void CBonusSystemNode::popBonuses(const CSelector &s)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
//prop
|
2010-11-19 00:22:51 +02:00
|
|
|
BonusList bl;
|
|
|
|
exportedBonuses.getBonuses(bl, s);
|
|
|
|
BOOST_FOREACH(Bonus *b, bl)
|
|
|
|
removeBonus(b);
|
2010-11-19 00:06:56 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(CBonusSystemNode *child, children)
|
|
|
|
child->popBonuses(s);
|
|
|
|
}
|
|
|
|
|
2010-11-20 02:03:31 +02:00
|
|
|
// void CBonusSystemNode::addNewBonus(const Bonus &b)
|
|
|
|
// {
|
|
|
|
// addNewBonus(new Bonus(b));
|
|
|
|
// }
|
2010-11-19 00:06:56 +02:00
|
|
|
|
|
|
|
void CBonusSystemNode::addNewBonus(Bonus *b)
|
|
|
|
{
|
2010-11-19 00:22:51 +02:00
|
|
|
exportedBonuses.push_back(b);
|
2010-11-22 02:34:46 +02:00
|
|
|
propagateBonus(b);
|
2010-11-19 00:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::removeBonus(Bonus *b)
|
|
|
|
{
|
2010-11-19 00:22:51 +02:00
|
|
|
exportedBonuses -= b;
|
2010-11-20 02:03:31 +02:00
|
|
|
CBonusSystemNode *whereIsOurBonus = whereToPropagate(b);
|
|
|
|
whereIsOurBonus->bonuses -= b;
|
|
|
|
delNull(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
CBonusSystemNode * CBonusSystemNode::whereToPropagate(Bonus *b)
|
|
|
|
{
|
|
|
|
if(b->propagator)
|
|
|
|
return b->propagator->getDestNode(this);
|
|
|
|
else
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBonusSystemNode::isLimitedOnUs(Bonus *b) const
|
|
|
|
{
|
|
|
|
return b->limiter && b->limiter->limit(b, *this);
|
2010-11-19 00:06:56 +02:00
|
|
|
}
|
|
|
|
|
2010-11-20 19:36:02 +02:00
|
|
|
bool CBonusSystemNode::weActAsBonusSourceOnly() const
|
|
|
|
{
|
|
|
|
switch(nodeType)
|
|
|
|
{
|
|
|
|
case CREATURE:
|
|
|
|
case ARTIFACT:
|
|
|
|
case ARTIFACT_INSTANCE:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TNodesVector & CBonusSystemNode::nodesOnWhichWePropagate()
|
|
|
|
{
|
|
|
|
return weActAsBonusSourceOnly() ? children : parents;
|
|
|
|
}
|
|
|
|
|
2010-11-22 02:34:46 +02:00
|
|
|
void CBonusSystemNode::propagateBonus(Bonus * b)
|
|
|
|
{
|
|
|
|
whereToPropagate(b)->bonuses.push_back(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
|
|
|
|
{
|
|
|
|
assert(!vstd::contains(children, child));
|
|
|
|
children.push_back(child);
|
2010-12-12 01:11:26 +02:00
|
|
|
BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
|
2010-11-22 02:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::childDetached(CBonusSystemNode *child)
|
|
|
|
{
|
|
|
|
assert(vstd::contains(children, child));
|
|
|
|
children -= child;
|
2010-12-12 01:11:26 +02:00
|
|
|
BONUS_LOG_LINE(child->nodeName() << " #detached from# " << nodeName());
|
2010-11-22 02:34:46 +02:00
|
|
|
}
|
|
|
|
|
2010-12-06 01:10:02 +02:00
|
|
|
void CBonusSystemNode::detachFromAll()
|
|
|
|
{
|
|
|
|
while(parents.size())
|
|
|
|
detachFrom(parents.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBonusSystemNode::isIndependentNode() const
|
|
|
|
{
|
|
|
|
return parents.empty() && children.empty();
|
|
|
|
}
|
|
|
|
|
2010-12-12 01:11:26 +02:00
|
|
|
std::string CBonusSystemNode::nodeName() const
|
|
|
|
{
|
|
|
|
return std::string("Bonus system node of type ") + typeid(*this).name();
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
|
|
|
if(obj)
|
|
|
|
return obj->valOfBonuses(type, subtype);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
|
|
|
if(obj)
|
|
|
|
return obj->hasBonusOfType(type, subtype);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
|
|
|
if(obj)
|
|
|
|
return obj->getModifiersWDescr(out, type, subtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NBonus::getCount(const CBonusSystemNode *obj, int from, int id)
|
|
|
|
{
|
|
|
|
if(obj)
|
|
|
|
return obj->getBonusesCount(from, id);
|
|
|
|
return 0;
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const CSpell * Bonus::sourceSpell() const
|
|
|
|
{
|
|
|
|
if(source == SPELL_EFFECT)
|
2010-12-19 16:39:56 +02:00
|
|
|
return VLC->spellh->spells[id];
|
2010-05-02 21:20:26 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Bonus::Description() const
|
|
|
|
{
|
|
|
|
if(description.size())
|
|
|
|
return description;
|
|
|
|
|
|
|
|
std::ostringstream str;
|
|
|
|
if(val < 0)
|
|
|
|
str << '-';
|
|
|
|
else if(val > 0)
|
|
|
|
str << '+';
|
|
|
|
|
|
|
|
str << val << " ";
|
|
|
|
|
|
|
|
switch(source)
|
|
|
|
{
|
|
|
|
case CREATURE_ABILITY:
|
|
|
|
str << VLC->creh->creatures[id]->namePl;
|
|
|
|
break;
|
2011-01-15 04:17:56 +02:00
|
|
|
case SECONDARY_SKILL:
|
|
|
|
str << VLC->generaltexth->skillName[id] << " secondary skill";
|
|
|
|
break;
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-12-06 01:10:02 +02:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
2010-07-13 08:25:40 +03:00
|
|
|
Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
|
|
|
|
: duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), description(Desc)
|
|
|
|
{
|
|
|
|
additionalInfo = -1;
|
|
|
|
turnsRemain = 0;
|
|
|
|
valType = ADDITIVE_VALUE;
|
|
|
|
effectRange = NO_LIMIT;
|
|
|
|
boost::algorithm::trim(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bonus::Bonus(ui8 Dur, ui8 Type, ui8 Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ui8 ValType /*= ADDITIVE_VALUE*/)
|
|
|
|
: duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), id(ID), valType(ValType)
|
|
|
|
{
|
|
|
|
additionalInfo = -1;
|
|
|
|
turnsRemain = 0;
|
|
|
|
effectRange = NO_LIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bonus::Bonus()
|
|
|
|
{
|
|
|
|
subtype = -1;
|
|
|
|
additionalInfo = -1;
|
|
|
|
turnsRemain = 0;
|
|
|
|
valType = ADDITIVE_VALUE;
|
|
|
|
effectRange = NO_LIMIT;
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Bonus::~Bonus()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Bonus * Bonus::addLimiter(ILimiter *Limiter)
|
|
|
|
{
|
|
|
|
limiter.reset(Limiter);
|
|
|
|
return this;
|
2010-07-13 08:25:40 +03:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
CSelector DLL_EXPORT operator&&(const CSelector &first, const CSelector &second)
|
|
|
|
{
|
|
|
|
return CSelectorsConjunction(first, second);
|
|
|
|
}
|
2010-08-30 21:06:17 +03:00
|
|
|
CSelector DLL_EXPORT operator||(const CSelector &first, const CSelector &second)
|
|
|
|
{
|
|
|
|
return CSelectorsAlternative(first, second);
|
|
|
|
}
|
2010-05-02 21:20:26 +03:00
|
|
|
|
|
|
|
namespace Selector
|
|
|
|
{
|
|
|
|
DLL_EXPORT CSelectFieldEqual<TBonusType> type(&Bonus::type, 0);
|
|
|
|
DLL_EXPORT CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype, 0);
|
|
|
|
DLL_EXPORT CSelectFieldEqual<si32> info(&Bonus::additionalInfo, 0);
|
|
|
|
DLL_EXPORT CSelectFieldEqual<ui8> sourceType(&Bonus::source, 0);
|
|
|
|
DLL_EXPORT CSelectFieldEqual<ui8> effectRange(&Bonus::effectRange, Bonus::NO_LIMIT);
|
|
|
|
DLL_EXPORT CWillLastTurns turns;;
|
|
|
|
|
|
|
|
CSelector DLL_EXPORT typeSybtype(TBonusType Type, TBonusSubtype Subtype)
|
|
|
|
{
|
|
|
|
return type(Type) && subtype(Subtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSelector DLL_EXPORT typeSybtypeInfo(TBonusType type, TBonusSubtype subtype, si32 info)
|
|
|
|
{
|
|
|
|
return CSelectFieldEqual<TBonusType>(&Bonus::type, type) && CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype, subtype) && CSelectFieldEqual<si32>(&Bonus::additionalInfo, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSelector DLL_EXPORT source(ui8 source, ui32 sourceID)
|
|
|
|
{
|
|
|
|
return CSelectFieldEqual<ui8>(&Bonus::source, source) && CSelectFieldEqual<ui32>(&Bonus::id, sourceID);
|
|
|
|
}
|
|
|
|
|
2010-08-31 13:22:21 +03:00
|
|
|
CSelector DLL_EXPORT sourceTypeSel(ui8 source)
|
|
|
|
{
|
|
|
|
return CSelectFieldEqual<ui8>(&Bonus::source, source);
|
|
|
|
}
|
|
|
|
|
2010-05-14 05:18:37 +03:00
|
|
|
bool DLL_EXPORT matchesType(const CSelector &sel, TBonusType type)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
Bonus dummy;
|
|
|
|
dummy.type = type;
|
2010-11-19 00:06:56 +02:00
|
|
|
return sel(&dummy);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-05-14 05:18:37 +03:00
|
|
|
|
|
|
|
bool DLL_EXPORT matchesTypeSubtype(const CSelector &sel, TBonusType type, TBonusSubtype subtype)
|
|
|
|
{
|
|
|
|
Bonus dummy;
|
|
|
|
dummy.type = type;
|
|
|
|
dummy.subtype = subtype;
|
2010-11-19 00:06:56 +02:00
|
|
|
return sel(&dummy);
|
2010-05-14 05:18:37 +03:00
|
|
|
}
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2010-11-20 02:03:31 +02:00
|
|
|
const CCreature * retrieveCreature(const CBonusSystemNode *node)
|
|
|
|
{
|
|
|
|
switch(node->nodeType)
|
|
|
|
{
|
|
|
|
case CBonusSystemNode::CREATURE:
|
|
|
|
return (static_cast<const CCreature *>(node));
|
|
|
|
case CBonusSystemNode::STACK:
|
|
|
|
return (static_cast<const CStackInstance *>(node))->type;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 13:20:25 +03:00
|
|
|
DLL_EXPORT std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
|
|
|
|
{
|
|
|
|
int i = 0;
|
2010-11-19 00:06:56 +02:00
|
|
|
BOOST_FOREACH(const Bonus *b, bonusList)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2010-11-19 00:06:56 +02:00
|
|
|
out << "Bonus " << i++ << "\n" << *b << std::endl;
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
DLL_EXPORT std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
|
|
|
|
{
|
|
|
|
for(std::map<std::string, int>::const_iterator i = bonusNameMap.begin(); i != bonusNameMap.end(); i++)
|
|
|
|
if(i->second == bonus.type)
|
|
|
|
out << "\tType: " << i->first << " \t";
|
|
|
|
|
|
|
|
#define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
|
|
|
|
printField(val);
|
|
|
|
printField(subtype);
|
|
|
|
printField(duration);
|
|
|
|
printField(source);
|
|
|
|
printField(id);
|
|
|
|
printField(additionalInfo);
|
|
|
|
printField(turnsRemain);
|
|
|
|
printField(valType);
|
|
|
|
printField(effectRange);
|
|
|
|
#undef printField
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ILimiter::~ILimiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-19 00:06:56 +02:00
|
|
|
bool ILimiter::limit(const Bonus *b, const CBonusSystemNode &node) const /*return true to drop the bonus */
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-19 00:06:56 +02:00
|
|
|
bool CCreatureTypeLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2010-08-24 17:26:57 +03:00
|
|
|
switch (node.nodeType)
|
|
|
|
{
|
|
|
|
case CBonusSystemNode::STACK:
|
|
|
|
{
|
|
|
|
const CCreature *c = (static_cast<const CStackInstance *>(&node))->type;
|
|
|
|
return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
|
|
|
|
} //drop bonus if it's not our creature and (we dont check upgrades or its not our upgrade)
|
|
|
|
break;
|
|
|
|
case CBonusSystemNode::CREATURE:
|
|
|
|
{
|
|
|
|
const CCreature *c = (static_cast<const CCreature *>(&node));
|
|
|
|
return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2010-08-26 10:23:08 +03:00
|
|
|
return true;
|
2010-08-24 17:26:57 +03:00
|
|
|
}
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, ui8 IncludeUpgrades /*= true*/)
|
|
|
|
:creature(&Creature), includeUpgrades(IncludeUpgrades)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCreatureTypeLimiter::CCreatureTypeLimiter()
|
|
|
|
{
|
|
|
|
creature = NULL;
|
|
|
|
includeUpgrades = false;
|
2010-08-06 13:46:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus )
|
|
|
|
: type(bonus), subtype(0), isSubtypeRelevant(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( TBonusType bonus, TBonusSubtype _subtype )
|
|
|
|
: type(bonus), subtype(_subtype), isSubtypeRelevant(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-19 00:06:56 +02:00
|
|
|
bool HasAnotherBonusLimiter::limit( const Bonus *b, const CBonusSystemNode &node ) const
|
2010-08-06 13:46:40 +03:00
|
|
|
{
|
|
|
|
if(isSubtypeRelevant)
|
|
|
|
{
|
|
|
|
return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type), subtype);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return !node.hasBonusOfType(static_cast<Bonus::BonusType>(type));
|
|
|
|
}
|
|
|
|
}
|
2010-11-19 00:22:51 +02:00
|
|
|
|
|
|
|
IPropagator::~IPropagator()
|
|
|
|
{
|
|
|
|
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source)
|
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
|
|
|
|
: terrainType(TerrainType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
bool CreatureNativeTerrainLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
|
|
|
|
{
|
|
|
|
const CCreature *c = retrieveCreature(&node);
|
|
|
|
return !c || VLC->heroh->nativeTerrains[c->faction] != terrainType; //drop bonus for non-creatures or non-native residents
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
|
|
|
|
: faction(Faction)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureFactionLimiter::CreatureFactionLimiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool CreatureFactionLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
|
|
|
|
{
|
|
|
|
const CCreature *c = retrieveCreature(&node);
|
|
|
|
return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureAlignmentLimiter::CreatureAlignmentLimiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
|
|
|
|
: alignment(Alignment)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CreatureAlignmentLimiter::limit(const Bonus *b, const CBonusSystemNode &node) const
|
|
|
|
{
|
|
|
|
const CCreature *c = retrieveCreature(&node);
|
|
|
|
if(!c)
|
|
|
|
return true;
|
|
|
|
switch(alignment)
|
|
|
|
{
|
|
|
|
case GOOD:
|
|
|
|
return !c->isGood(); //if not good -> return true (drop bonus)
|
|
|
|
case NEUTRAL:
|
|
|
|
return c->isEvil() || c->isGood();
|
|
|
|
case EVIL:
|
|
|
|
return !c->isEvil();
|
|
|
|
default:
|
|
|
|
tlog1 << "Warning: illegal alignment in limiter!\n";
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-19 00:22:51 +02:00
|
|
|
}
|