2012-12-19 17:54:10 +03:00
|
|
|
/*
|
|
|
|
* HeroBonus.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2010-02-10 04:56:00 +02:00
|
|
|
#include "HeroBonus.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
#include "VCMI_Lib.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "spells/CSpellHandler.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CCreatureHandler.h"
|
2010-07-12 13:20:25 +03:00
|
|
|
#include "CCreatureSet.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CHeroHandler.h"
|
2011-01-15 04:17:56 +02:00
|
|
|
#include "CGeneralTextHandler.h"
|
2011-02-12 18:12:48 +02:00
|
|
|
#include "BattleState.h"
|
2011-02-22 11:47:25 +02:00
|
|
|
#include "CArtHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "GameConstants.h"
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
#define FOREACH_PARENT(pname) TNodes lparents; getParents(lparents); for(CBonusSystemNode *pname : lparents)
|
|
|
|
#define FOREACH_CPARENT(pname) TCNodes lparents; getParents(lparents); for(const CBonusSystemNode *pname : lparents)
|
|
|
|
#define FOREACH_RED_CHILD(pname) TNodes lchildren; getRedChildren(lchildren); for(CBonusSystemNode *pname : lchildren)
|
|
|
|
#define FOREACH_RED_PARENT(pname) TNodes lparents; getRedParents(lparents); for(CBonusSystemNode *pname : lparents)
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_NAME(x) { #x, Bonus::x },
|
|
|
|
const std::map<std::string, Bonus::BonusType> bonusNameMap = { BONUS_LIST };
|
2010-07-12 13:20:25 +03:00
|
|
|
#undef BONUS_NAME
|
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_VALUE(x) { #x, Bonus::x },
|
|
|
|
const std::map<std::string, Bonus::ValueType> bonusValueMap = { BONUS_VALUE_LIST };
|
2012-08-23 21:46:43 +03:00
|
|
|
#undef BONUS_VALUE
|
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_SOURCE(x) { #x, Bonus::x },
|
|
|
|
const std::map<std::string, Bonus::BonusSource> bonusSourceMap = { BONUS_SOURCE_LIST };
|
2012-08-23 21:46:43 +03:00
|
|
|
#undef BONUS_SOURCE
|
|
|
|
|
2014-10-02 18:43:46 +03:00
|
|
|
#define BONUS_ITEM(x) { #x, Bonus::x },
|
2012-08-23 21:46:43 +03:00
|
|
|
|
2016-08-18 14:03:20 +02:00
|
|
|
const std::map<std::string, ui16> bonusDurationMap =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
2012-08-23 21:46:43 +03:00
|
|
|
BONUS_ITEM(PERMANENT)
|
|
|
|
BONUS_ITEM(ONE_BATTLE)
|
|
|
|
BONUS_ITEM(ONE_DAY)
|
|
|
|
BONUS_ITEM(ONE_WEEK)
|
|
|
|
BONUS_ITEM(N_TURNS)
|
|
|
|
BONUS_ITEM(N_DAYS)
|
|
|
|
BONUS_ITEM(UNITL_BEING_ATTACKED)
|
|
|
|
BONUS_ITEM(UNTIL_ATTACK)
|
|
|
|
BONUS_ITEM(STACK_GETS_TURN)
|
2014-10-02 18:43:46 +03:00
|
|
|
BONUS_ITEM(COMMANDER_KILLED)
|
|
|
|
};
|
2012-08-23 21:46:43 +03:00
|
|
|
|
2016-08-18 14:03:20 +02:00
|
|
|
const std::map<std::string, Bonus::LimitEffect> bonusLimitEffect =
|
2014-10-02 18:43:46 +03:00
|
|
|
{
|
2012-08-23 21:46:43 +03:00
|
|
|
BONUS_ITEM(NO_LIMIT)
|
|
|
|
BONUS_ITEM(ONLY_DISTANCE_FIGHT)
|
2014-05-21 12:02:20 +03:00
|
|
|
BONUS_ITEM(ONLY_MELEE_FIGHT)
|
2014-10-02 18:43:46 +03:00
|
|
|
BONUS_ITEM(ONLY_ENEMY_ARMY)
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::map<std::string, TLimiterPtr> bonusLimiterMap =
|
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
{"SHOOTER_ONLY", std::make_shared<HasAnotherBonusLimiter>(Bonus::SHOOTER)},
|
|
|
|
{"DRAGON_NATURE", std::make_shared<HasAnotherBonusLimiter>(Bonus::DRAGON_NATURE)},
|
|
|
|
{"IS_UNDEAD", std::make_shared<HasAnotherBonusLimiter>(Bonus::UNDEAD)}
|
2014-10-02 18:43:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const std::map<std::string, TPropagatorPtr> bonusPropagatorMap =
|
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
{"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)}
|
2014-10-02 18:43:46 +03:00
|
|
|
}; //untested
|
2012-09-27 19:48:46 +03:00
|
|
|
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
#define BONUS_LOG_LINE(x) logBonus->traceStream() << x
|
2010-12-12 01:11:26 +02:00
|
|
|
|
2011-06-25 16:53:15 +03:00
|
|
|
int CBonusSystemNode::treeChanged = 1;
|
2011-07-13 21:39:02 +03:00
|
|
|
const bool CBonusSystemNode::cachingEnabled = true;
|
2011-06-25 16:53:15 +03:00
|
|
|
|
2011-07-13 21:39:02 +03:00
|
|
|
BonusList::BonusList(bool BelongsToTree /* =false */) : belongsToTree(BelongsToTree)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BonusList::BonusList(const BonusList &bonusList)
|
|
|
|
{
|
|
|
|
bonuses.resize(bonusList.size());
|
|
|
|
std::copy(bonusList.begin(), bonusList.end(), bonuses.begin());
|
|
|
|
belongsToTree = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BonusList& BonusList::operator=(const BonusList &bonusList)
|
|
|
|
{
|
|
|
|
bonuses.resize(bonusList.size());
|
|
|
|
std::copy(bonusList.begin(), bonusList.end(), bonuses.begin());
|
|
|
|
belongsToTree = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int 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;
|
2011-02-12 18:12:48 +02:00
|
|
|
int indepMin = 0;
|
|
|
|
bool hasIndepMin = false;
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : bonuses)
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
Bonus *b = elem;
|
2011-07-16 16:57:25 +03:00
|
|
|
|
|
|
|
switch(b->valType)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
|
|
|
case Bonus::BASE_NUMBER:
|
2011-07-16 16:57:25 +03:00
|
|
|
base += b->val;
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
|
|
|
case Bonus::PERCENT_TO_ALL:
|
2011-07-16 16:57:25 +03:00
|
|
|
percentToAll += b->val;
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
|
|
|
case Bonus::PERCENT_TO_BASE:
|
2011-07-16 16:57:25 +03:00
|
|
|
percentToBase += b->val;
|
2010-05-02 21:20:26 +03:00
|
|
|
break;
|
|
|
|
case Bonus::ADDITIVE_VALUE:
|
2011-07-16 16:57:25 +03:00
|
|
|
additive += b->val;
|
2010-08-05 12:11:38 +03:00
|
|
|
break;
|
|
|
|
case Bonus::INDEPENDENT_MAX:
|
2011-02-21 18:53:23 +02:00
|
|
|
if (!hasIndepMax)
|
2010-08-05 12:11:38 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
indepMax = b->val;
|
2010-08-05 12:11:38 +03:00
|
|
|
hasIndepMax = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(indepMax, b->val);
|
2010-08-05 12:11:38 +03:00
|
|
|
}
|
|
|
|
|
2011-02-12 18:12:48 +02:00
|
|
|
break;
|
|
|
|
case Bonus::INDEPENDENT_MIN:
|
2011-02-21 18:53:23 +02:00
|
|
|
if (!hasIndepMin)
|
2011-02-12 18:12:48 +02:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
indepMin = b->val;
|
2011-02-12 18:12:48 +02:00
|
|
|
hasIndepMin = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amin(indepMin, b->val);
|
2011-02-12 18:12:48 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2011-02-12 18:12:48 +02:00
|
|
|
|
|
|
|
if(hasIndepMin && hasIndepMax)
|
|
|
|
assert(indepMin < indepMax);
|
2012-08-26 12:07:48 +03:00
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
const int notIndepBonuses = boost::count_if(bonuses, [](const Bonus *b)
|
|
|
|
{
|
|
|
|
return b->valType != Bonus::INDEPENDENT_MAX && b->valType != Bonus::INDEPENDENT_MIN;
|
2012-08-26 12:07:48 +03:00
|
|
|
});
|
|
|
|
|
2010-08-05 12:11:38 +03:00
|
|
|
if (hasIndepMax)
|
2012-08-26 12:07:48 +03:00
|
|
|
{
|
|
|
|
if(notIndepBonuses)
|
|
|
|
vstd::amax(valFirst, indepMax);
|
|
|
|
else
|
|
|
|
valFirst = indepMax;
|
|
|
|
}
|
2011-02-21 18:53:23 +02:00
|
|
|
if (hasIndepMin)
|
2012-08-26 12:07:48 +03:00
|
|
|
{
|
|
|
|
if(notIndepBonuses)
|
|
|
|
vstd::amin(valFirst, indepMin);
|
|
|
|
else
|
|
|
|
valFirst = indepMin;
|
|
|
|
}
|
2011-02-12 18:12:48 +02:00
|
|
|
|
|
|
|
return valFirst;
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
2011-07-13 21:39:02 +03:00
|
|
|
const Bonus * BonusList::getFirst(const CSelector &selector) const
|
2010-02-10 04:56:00 +02:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : bonuses)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
const Bonus *b = elem;
|
2011-06-25 16:53:15 +03:00
|
|
|
if(selector(b))
|
|
|
|
return &*b;
|
|
|
|
}
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-02-10 04:56:00 +02:00
|
|
|
|
2011-07-13 21:39:02 +03:00
|
|
|
Bonus * BonusList::getFirst(const CSelector &select)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : bonuses)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
Bonus *b = elem;
|
2011-06-25 16:53:15 +03:00
|
|
|
if(select(b))
|
|
|
|
return &*b;
|
|
|
|
}
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-02-10 04:56:00 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void BonusList::getBonuses(BonusList & out, const CSelector &selector) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
getBonuses(out, selector, nullptr);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : bonuses)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
Bonus *b = elem;
|
2011-06-25 16:53:15 +03:00
|
|
|
|
|
|
|
//add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
|
2013-07-12 22:13:39 +03:00
|
|
|
if(selector(b) && ((!limit && b->effectRange == Bonus::NO_LIMIT) || ((bool)limit && limit(b))))
|
2012-03-06 19:59:55 +03:00
|
|
|
out.push_back(b);
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-08-31 13:22:21 +03:00
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void BonusList::getAllBonuses(BonusList &out) const
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : bonuses)
|
2012-03-06 19:59:55 +03:00
|
|
|
out.push_back(b);
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
int BonusList::valOfBonuses(const CSelector &select) const
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
BonusList ret;
|
2013-06-29 16:05:48 +03:00
|
|
|
CSelector limit = nullptr;
|
2012-03-06 19:59:55 +03:00
|
|
|
getBonuses(ret, select, limit);
|
|
|
|
ret.eliminateDuplicates();
|
|
|
|
return ret.totalValue();
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
// void BonusList::limit(const CBonusSystemNode &node)
|
|
|
|
// {
|
2014-08-04 21:33:59 +03:00
|
|
|
// remove_if(std::bind(&CBonusSystemNode::isLimitedOnUs, std::ref(node), _1));
|
2012-03-06 19:59:55 +03:00
|
|
|
// }
|
|
|
|
|
2011-06-25 16:53:15 +03:00
|
|
|
|
2011-07-13 21:39:02 +03:00
|
|
|
void BonusList::eliminateDuplicates()
|
|
|
|
{
|
|
|
|
sort( bonuses.begin(), bonuses.end() );
|
|
|
|
bonuses.erase( unique( bonuses.begin(), bonuses.end() ), bonuses.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void BonusList::push_back(Bonus* const &x)
|
2011-02-27 13:26:18 +02:00
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
bonuses.push_back(x);
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2011-07-13 21:39:02 +03:00
|
|
|
if (belongsToTree)
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
std::vector<Bonus*>::iterator BonusList::erase(const int position)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
if (belongsToTree)
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-07-16 16:57:25 +03:00
|
|
|
return bonuses.erase(bonuses.begin() + position);
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BonusList::clear()
|
|
|
|
{
|
|
|
|
bonuses.clear();
|
|
|
|
|
|
|
|
if (belongsToTree)
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BonusList*>::size_type BonusList::operator-=(Bonus* const &i)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
auto itr = std::find(bonuses.begin(), bonuses.end(), i);
|
2011-07-13 21:39:02 +03:00
|
|
|
if(itr == bonuses.end())
|
|
|
|
return false;
|
|
|
|
bonuses.erase(itr);
|
|
|
|
|
|
|
|
if (belongsToTree)
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-07-13 21:39:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BonusList::resize(std::vector<Bonus*>::size_type sz, Bonus* c )
|
|
|
|
{
|
|
|
|
bonuses.resize(sz, c);
|
|
|
|
|
|
|
|
if (belongsToTree)
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-07-13 21:39:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BonusList::insert(std::vector<Bonus*>::iterator position, std::vector<Bonus*>::size_type n, Bonus* const &x)
|
|
|
|
{
|
|
|
|
bonuses.insert(position, n, x);
|
|
|
|
|
|
|
|
if (belongsToTree)
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-02-27 13:26:18 +02:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
int IBonusBearer::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
|
2010-08-25 11:40:50 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return valOfBonuses(Selector::type(type).And(selector));
|
2010-08-25 11:40:50 +03:00
|
|
|
}
|
2010-12-06 01:10:02 +02:00
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "type_" << type << "s_" << subtype;
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
CSelector s = Selector::type(type);
|
|
|
|
if(subtype != -1)
|
2013-07-02 15:08:30 +03:00
|
|
|
s = s.And(Selector::subtype(subtype));
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
return valOfBonuses(s, cachingStr.str());
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2010-12-06 01:10:02 +02:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
CSelector limit = nullptr;
|
2013-06-26 14:18:27 +03:00
|
|
|
TBonusListPtr hlp = getAllBonuses(selector, limit, nullptr, cachingStr);
|
2011-06-25 16:53:15 +03:00
|
|
|
return hlp->totalValue();
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2011-07-13 21:39:02 +03:00
|
|
|
bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr /*= ""*/) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
return getBonuses(selector, cachingStr)->size() > 0;
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "type_" << type << "s_" << subtype;
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
CSelector s = Selector::type(type);
|
|
|
|
if(subtype != -1)
|
2013-07-02 15:08:30 +03:00
|
|
|
s = s.And(Selector::subtype(subtype));
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
return hasBonus(s, cachingStr.str());
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
int IBonusBearer::getBonusesCount(Bonus::BonusSource from, int id) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "source_" << from << "id_" << id;
|
|
|
|
return getBonusesCount(Selector::source(from, id), cachingStr.str());
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
int IBonusBearer::getBonusesCount(const CSelector &selector, const std::string &cachingStr /* =""*/) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
return getBonuses(selector, cachingStr)->size();
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2011-09-06 16:59:26 +03:00
|
|
|
const TBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr /*= ""*/) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
return getAllBonuses(selector, nullptr, nullptr, cachingStr);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2011-09-06 16:59:26 +03:00
|
|
|
const TBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr /*= ""*/) const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
return getAllBonuses(selector, limit, nullptr, cachingStr);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
bool IBonusBearer::hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "source_" << source << "id_" << sourceID;
|
|
|
|
return hasBonus(Selector::source(source,sourceID), cachingStr.str());
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
int IBonusBearer::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;
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
int ret = valOfBonuses(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
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(ret, +1);
|
2010-04-03 06:33:46 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
return vstd::abetween(ret, -3, +3);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
int IBonusBearer::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;
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
int ret = valOfBonuses(Bonus::LUCK);
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(ret, +1);
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
return vstd::abetween(ret, -3, +3);
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
si32 IBonusBearer::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);
|
|
|
|
|
2013-01-16 14:19:04 +03:00
|
|
|
if (double frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-01-16 14:19:04 +03:00
|
|
|
ret += (frenzyPower/100) * (double)Defense(false);
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(ret, 0);
|
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
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
si32 IBonusBearer::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;
|
|
|
|
}
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(ret, 0);
|
2010-04-03 06:33:46 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-26 19:48:53 +03:00
|
|
|
ui32 IBonusBearer::MaxHealth() const
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
2012-01-26 19:48:53 +03:00
|
|
|
return std::max(1, valOfBonuses(Bonus::STACK_HEALTH)); //never 0
|
2010-04-03 06:33:46 +03:00
|
|
|
}
|
|
|
|
|
2011-02-27 21:58:14 +02:00
|
|
|
ui32 IBonusBearer::getMinDamage() const
|
2010-08-30 21:06:17 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "type_" << Bonus::CREATURE_DAMAGE << "s_0Otype_" << Bonus::CREATURE_DAMAGE << "s_1";
|
2013-07-02 15:08:30 +03:00
|
|
|
return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1)), cachingStr.str());
|
2010-08-30 21:06:17 +03:00
|
|
|
}
|
2011-02-27 21:58:14 +02:00
|
|
|
ui32 IBonusBearer::getMaxDamage() const
|
2010-08-30 21:06:17 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "type_" << Bonus::CREATURE_DAMAGE << "s_0Otype_" << Bonus::CREATURE_DAMAGE << "s_2";
|
2013-07-02 15:08:30 +03:00
|
|
|
return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2)), cachingStr.str());
|
2010-08-30 21:06:17 +03:00
|
|
|
}
|
|
|
|
|
2012-02-20 19:26:14 +03:00
|
|
|
si32 IBonusBearer::manaLimit() const
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
return si32(getPrimSkillLevel(PrimarySkill::KNOWLEDGE)
|
|
|
|
* (100.0 + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::INTELLIGENCE))
|
2013-01-20 15:06:18 +03:00
|
|
|
/ 10.0);
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2013-02-06 13:16:44 +03:00
|
|
|
int IBonusBearer::getPrimSkillLevel(PrimarySkill::PrimarySkill id) const
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
if(id == PrimarySkill::ATTACK)
|
|
|
|
ret = Attack();
|
|
|
|
else if(id == PrimarySkill::DEFENSE)
|
|
|
|
ret = Defense();
|
|
|
|
else
|
|
|
|
ret = valOfBonuses(Bonus::PRIMARY_SKILL, id);
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::amax(ret, id/2); //minimal value is 0 for attack and defense and 1 for spell power and knowledge
|
2011-02-27 21:58:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-29 21:28:50 +03:00
|
|
|
si32 IBonusBearer::magicResistance() const
|
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
return valOfBonuses(Bonus::MAGIC_RESISTANCE);
|
2011-05-29 21:28:50 +03:00
|
|
|
}
|
|
|
|
|
2013-07-22 20:06:32 +03:00
|
|
|
ui32 IBonusBearer::Speed( int turn /*= 0*/ , bool useBind /* = false*/) const
|
2013-07-22 13:39:11 +03:00
|
|
|
{
|
2013-07-22 20:06:32 +03:00
|
|
|
//war machines cannot move
|
|
|
|
if(hasBonus(Selector::type(Bonus::SIEGE_WEAPON).And(Selector::turns(turn))))
|
2013-07-22 13:39:11 +03:00
|
|
|
{
|
2013-07-22 20:06:32 +03:00
|
|
|
return 0;
|
2013-07-22 13:39:11 +03:00
|
|
|
}
|
|
|
|
//bind effect check - doesn't influence stack initiative
|
|
|
|
if (useBind && getEffect (SpellID::BIND))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-22 20:06:32 +03:00
|
|
|
return valOfBonuses(Selector::type(Bonus::STACKS_SPEED).And(Selector::turns(turn)));
|
2016-08-18 14:03:20 +02:00
|
|
|
}
|
2013-07-22 13:39:11 +03:00
|
|
|
|
2011-06-25 18:05:01 +03:00
|
|
|
bool IBonusBearer::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
|
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
2014-03-23 15:59:03 +03:00
|
|
|
cachingStr << "type_" << Bonus::UNDEAD << "s_-1Otype_" << Bonus::NON_LIVING << "s_-11type_" << Bonus::SIEGE_WEAPON; //I don't really get what string labels mean?
|
2013-07-02 15:08:30 +03:00
|
|
|
return !hasBonus(Selector::type(Bonus::UNDEAD)
|
|
|
|
.Or(Selector::type(Bonus::NON_LIVING))
|
|
|
|
.Or(Selector::type(Bonus::SIEGE_WEAPON)), cachingStr.str());
|
2011-06-25 18:05:01 +03:00
|
|
|
}
|
|
|
|
|
2011-09-06 16:59:26 +03:00
|
|
|
const TBonusListPtr IBonusBearer::getSpellBonuses() const
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
std::stringstream cachingStr;
|
2015-09-15 07:51:05 +02:00
|
|
|
cachingStr << "!type_" << Bonus::NONE << "source_" << Bonus::SPELL_EFFECT;
|
|
|
|
CSelector selector = Selector::sourceType(Bonus::SPELL_EFFECT)
|
|
|
|
.And(CSelector([](const Bonus * b)->bool
|
|
|
|
{
|
2015-10-12 16:10:40 +02:00
|
|
|
return b->type != Bonus::NONE;
|
2016-08-18 14:03:20 +02:00
|
|
|
}));
|
2015-09-15 07:51:05 +02:00
|
|
|
return getBonuses(selector, Selector::anyRange(), cachingStr.str());
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
const Bonus * IBonusBearer::getEffect(ui16 id, int turn /*= 0*/) const
|
|
|
|
{
|
|
|
|
//TODO should check only local bonuses?
|
|
|
|
auto bonuses = getAllBonuses();
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const Bonus *it : *bonuses)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
|
|
|
if(it->source == Bonus::SPELL_EFFECT && it->sid == id)
|
|
|
|
{
|
|
|
|
if(!turn || it->turnsRemain > turn)
|
|
|
|
return &(*it);
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2012-09-20 19:55:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ui8 IBonusBearer::howManyEffectsSet(ui16 id) const
|
|
|
|
{
|
|
|
|
//TODO should check only local bonuses?
|
|
|
|
ui8 ret = 0;
|
|
|
|
|
|
|
|
auto bonuses = getAllBonuses();
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const Bonus *it : *bonuses)
|
2012-09-20 19:55:21 +03:00
|
|
|
{
|
|
|
|
if(it->source == Bonus::SPELL_EFFECT && it->sid == id) //effect found
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TBonusListPtr IBonusBearer::getAllBonuses() const
|
|
|
|
{
|
|
|
|
auto matchAll= [] (const Bonus *) { return true; };
|
|
|
|
auto matchNone= [] (const Bonus *) { return true; };
|
|
|
|
return getAllBonuses(matchAll, matchNone);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Bonus * IBonusBearer::getBonus(const CSelector &selector) const
|
|
|
|
{
|
|
|
|
auto bonuses = getAllBonuses();
|
|
|
|
return bonuses->getFirst(selector);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bonus * CBonusSystemNode::getBonusLocalFirst(const CSelector &selector)
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
|
|
|
Bonus *ret = bonuses.getFirst(selector);
|
|
|
|
if(ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
FOREACH_PARENT(pname)
|
|
|
|
{
|
2012-09-20 19:55:21 +03:00
|
|
|
ret = pname->getBonusLocalFirst(selector);
|
2011-02-27 21:58:14 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2012-09-20 19:55:21 +03:00
|
|
|
const Bonus * CBonusSystemNode::getBonusLocalFirst( const CSelector &selector ) const
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2012-09-20 19:55:21 +03:00
|
|
|
return (const_cast<CBonusSystemNode*>(this))->getBonusLocalFirst(selector);
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : parents)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
const CBonusSystemNode *parent = elem;
|
2011-02-27 21:58:14 +02:00
|
|
|
out.insert(parent);
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getParents(TNodes &out)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : parents)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
const CBonusSystemNode *parent = elem;
|
2011-02-27 21:58:14 +02:00
|
|
|
out.insert(const_cast<CBonusSystemNode*>(parent));
|
2013-02-13 22:35:43 +03:00
|
|
|
}
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void CBonusSystemNode::getBonusesRec(BonusList &out, const CSelector &selector, const CSelector &limit) const
|
2011-02-27 21:58:14 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
FOREACH_CPARENT(p)
|
|
|
|
{
|
|
|
|
p->getBonusesRec(out, selector, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
bonuses.getBonuses(out, selector, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getAllBonusesRec(BonusList &out) const
|
|
|
|
{
|
|
|
|
FOREACH_CPARENT(p)
|
|
|
|
{
|
|
|
|
p->getAllBonusesRec(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
bonuses.getAllBonuses(out);
|
2011-02-27 21:58:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
const TBonusListPtr CBonusSystemNode::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= nullptr*/, const std::string &cachingStr /*= ""*/) const
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
bool limitOnUs = (!root || root == this); //caching won't work when we want to limit bonuses against an external node
|
2013-02-13 22:35:43 +03:00
|
|
|
if (CBonusSystemNode::cachingEnabled && limitOnUs)
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2011-07-16 16:57:25 +03:00
|
|
|
// Exclusive access for one thread
|
2011-07-13 21:39:02 +03:00
|
|
|
static boost::mutex m;
|
|
|
|
boost::mutex::scoped_lock lock(m);
|
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
// If the bonus system tree changes(state of a single node or the relations to each other) then
|
2011-07-16 16:57:25 +03:00
|
|
|
// cache all bonus objects. Selector objects doesn't matter.
|
2011-06-25 16:53:15 +03:00
|
|
|
if (cachedLast != treeChanged)
|
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
cachedBonuses.clear();
|
2011-06-25 16:53:15 +03:00
|
|
|
cachedRequests.clear();
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
BonusList allBonuses;
|
|
|
|
getAllBonusesRec(allBonuses);
|
|
|
|
allBonuses.eliminateDuplicates();
|
|
|
|
limitBonuses(allBonuses, cachedBonuses);
|
|
|
|
|
2011-06-25 16:53:15 +03:00
|
|
|
cachedLast = treeChanged;
|
|
|
|
}
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2011-07-16 16:57:25 +03:00
|
|
|
// 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.
|
2011-06-25 16:53:15 +03:00
|
|
|
if (cachingStr != "")
|
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
auto it = cachedRequests.find(cachingStr);
|
|
|
|
if(it != cachedRequests.end())
|
2011-06-25 16:53:15 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
//Cached list contains bonuses for our query with applied limiters
|
|
|
|
return it->second;
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
//We still don't have the bonuses (didn't returned them from cache)
|
|
|
|
//Perform bonus selection
|
2015-12-29 04:43:33 +02:00
|
|
|
auto ret = std::make_shared<BonusList>();
|
2013-02-13 22:35:43 +03:00
|
|
|
cachedBonuses.getBonuses(*ret, selector, limit);
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
// Save the results in the cache
|
|
|
|
if(cachingStr != "")
|
2011-06-25 16:53:15 +03:00
|
|
|
cachedRequests[cachingStr] = ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
return getAllBonusesWithoutCaching(selector, limit, root);
|
|
|
|
}
|
|
|
|
}
|
2011-07-16 16:57:25 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
const TBonusListPtr CBonusSystemNode::getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= nullptr*/) const
|
2012-03-06 19:59:55 +03:00
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
auto ret = std::make_shared<BonusList>();
|
2011-07-16 16:57:25 +03:00
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
// Get bonus results without caching enabled.
|
|
|
|
BonusList beforeLimiting, afterLimiting;
|
|
|
|
getAllBonusesRec(beforeLimiting);
|
|
|
|
beforeLimiting.eliminateDuplicates();
|
|
|
|
|
|
|
|
if(!root || root == this)
|
|
|
|
{
|
|
|
|
limitBonuses(beforeLimiting, afterLimiting);
|
|
|
|
afterLimiting.getBonuses(*ret, selector, limit);
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
2012-03-06 19:59:55 +03:00
|
|
|
else if(root)
|
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
//We want to limit our query against an external node. We get all its bonuses,
|
2012-03-06 19:59:55 +03:00
|
|
|
// add the ones we're considering and see if they're cut out by limiters
|
|
|
|
BonusList rootBonuses, limitedRootBonuses;
|
|
|
|
getAllBonusesRec(rootBonuses);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : beforeLimiting)
|
2012-03-06 19:59:55 +03:00
|
|
|
rootBonuses.push_back(b);
|
|
|
|
|
|
|
|
rootBonuses.eliminateDuplicates();
|
|
|
|
root->limitBonuses(rootBonuses, limitedRootBonuses);
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : beforeLimiting)
|
2012-03-06 19:59:55 +03:00
|
|
|
if(vstd::contains(limitedRootBonuses, b))
|
|
|
|
afterLimiting.push_back(b);
|
|
|
|
|
|
|
|
afterLimiting.getBonuses(*ret, selector, limit);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
beforeLimiting.getBonuses(*ret, selector, limit);
|
|
|
|
|
|
|
|
return ret;
|
2011-06-25 16:53:15 +03:00
|
|
|
}
|
|
|
|
|
2011-07-13 21:39:02 +03:00
|
|
|
CBonusSystemNode::CBonusSystemNode() : bonuses(true), exportedBonuses(true), nodeType(UNKNOWN), cachedLast(0)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logBonus->warnStream() << "Warning: an orphaned child!";
|
2011-02-04 16:58:14 +02:00
|
|
|
while(children.size())
|
|
|
|
children.front()->detachFrom(this);
|
2010-11-27 03:46:19 +02:00
|
|
|
}
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : exportedBonuses)
|
2011-02-21 06:13:00 +02:00
|
|
|
delete b;
|
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);
|
2011-02-21 06:13:00 +02:00
|
|
|
|
|
|
|
if(parent->actsAsBonusSourceOnly())
|
|
|
|
parent->newRedDescendant(this);
|
|
|
|
else
|
|
|
|
newRedDescendant(parent);
|
2010-11-22 02:34:46 +02:00
|
|
|
|
|
|
|
parent->newChildAttached(this);
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
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));
|
2010-11-13 22:26:15 +02:00
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
if(parent->actsAsBonusSourceOnly())
|
|
|
|
parent->removedRedDescendant(this);
|
|
|
|
else
|
|
|
|
removedRedDescendant(parent);
|
|
|
|
|
|
|
|
parents -= parent;
|
2010-11-22 02:34:46 +02:00
|
|
|
parent->childDetached(this);
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2010-11-13 22:26:15 +02:00
|
|
|
}
|
|
|
|
|
2010-11-19 00:06:56 +02:00
|
|
|
void CBonusSystemNode::popBonuses(const CSelector &s)
|
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
BonusList bl;
|
2010-11-19 00:22:51 +02:00
|
|
|
exportedBonuses.getBonuses(bl, s);
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : bl)
|
2010-11-19 00:22:51 +02:00
|
|
|
removeBonus(b);
|
2010-11-19 00:06:56 +02:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CBonusSystemNode *child : children)
|
2010-11-19 00:06:56 +02:00
|
|
|
child->popBonuses(s);
|
|
|
|
}
|
|
|
|
|
2015-11-09 13:18:36 +02:00
|
|
|
void CBonusSystemNode::updateBonuses(const CSelector &s)
|
|
|
|
{
|
|
|
|
BonusList bl;
|
|
|
|
exportedBonuses.getBonuses(bl, s);
|
|
|
|
for(Bonus *b : bl)
|
|
|
|
{
|
2015-11-09 14:48:07 +02:00
|
|
|
b->turnsRemain--;
|
|
|
|
if(b->turnsRemain <= 0)
|
|
|
|
removeBonus(b);
|
2015-11-09 13:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(CBonusSystemNode *child : children)
|
|
|
|
child->updateBonuses(s);
|
|
|
|
}
|
|
|
|
|
2010-11-19 00:06:56 +02:00
|
|
|
void CBonusSystemNode::addNewBonus(Bonus *b)
|
|
|
|
{
|
2015-11-20 13:37:57 +02:00
|
|
|
//turnsRemain shouldn't be zero for following durations
|
|
|
|
if(Bonus::NTurns(b) || Bonus::NDays(b) || Bonus::OneWeek(b))
|
|
|
|
{
|
|
|
|
assert(b->turnsRemain);
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
assert(!vstd::contains(exportedBonuses,b));
|
2010-11-19 00:22:51 +02:00
|
|
|
exportedBonuses.push_back(b);
|
2011-02-23 05:57:45 +02:00
|
|
|
exportBonus(b);
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2010-11-19 00:06:56 +02:00
|
|
|
}
|
|
|
|
|
2012-04-22 22:56:03 +03:00
|
|
|
void CBonusSystemNode::accumulateBonus(Bonus &b)
|
2012-04-04 20:41:55 +03:00
|
|
|
{
|
2012-04-23 08:01:52 +03:00
|
|
|
Bonus *bonus = exportedBonuses.getFirst(Selector::typeSubtype(b.type, b.subtype)); //only local bonuses are interesting //TODO: what about value type?
|
2012-04-04 20:41:55 +03:00
|
|
|
if(bonus)
|
2012-05-18 17:02:27 +03:00
|
|
|
bonus->val += b.val;
|
2012-04-04 20:41:55 +03:00
|
|
|
else
|
2012-04-22 22:56:03 +03:00
|
|
|
addNewBonus(new Bonus(b)); //duplicate needed, original may get destroyed
|
2012-04-04 20:41:55 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
if(b->propagator)
|
2011-02-21 06:13:00 +02:00
|
|
|
unpropagateBonus(b);
|
2010-11-20 02:03:31 +02:00
|
|
|
else
|
2011-02-21 06:13:00 +02:00
|
|
|
bonuses -= b;
|
2011-12-14 00:23:17 +03:00
|
|
|
vstd::clear_pointer(b);
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
bool CBonusSystemNode::actsAsBonusSourceOnly() const
|
2010-11-20 19:36:02 +02:00
|
|
|
{
|
|
|
|
switch(nodeType)
|
|
|
|
{
|
|
|
|
case CREATURE:
|
|
|
|
case ARTIFACT:
|
|
|
|
case ARTIFACT_INSTANCE:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
void CBonusSystemNode::propagateBonus(Bonus * b)
|
2010-11-20 19:36:02 +02:00
|
|
|
{
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator->shouldBeAttached(this))
|
|
|
|
{
|
|
|
|
bonuses.push_back(b);
|
|
|
|
BONUS_LOG_LINE("#$# " << b->Description() << " #propagated to# " << nodeName());
|
|
|
|
}
|
|
|
|
|
|
|
|
FOREACH_RED_CHILD(child)
|
|
|
|
child->propagateBonus(b);
|
2010-11-20 19:36:02 +02:00
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
void CBonusSystemNode::unpropagateBonus(Bonus * b)
|
2010-11-22 02:34:46 +02:00
|
|
|
{
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator->shouldBeAttached(this))
|
|
|
|
{
|
|
|
|
bonuses -= b;
|
2012-03-31 18:10:16 +03:00
|
|
|
while(vstd::contains(bonuses, b))
|
|
|
|
{
|
2016-03-12 03:41:27 +02:00
|
|
|
logBonus->errorStream() << "Bonus was duplicated (" << b->Description() << ") at " << nodeName();
|
2012-03-31 18:10:16 +03:00
|
|
|
bonuses -= b;
|
|
|
|
}
|
2011-02-21 06:13:00 +02:00
|
|
|
BONUS_LOG_LINE("#$#" << b->Description() << " #is no longer propagated to# " << nodeName());
|
|
|
|
}
|
|
|
|
|
|
|
|
FOREACH_RED_CHILD(child)
|
|
|
|
child->unpropagateBonus(b);
|
2010-11-22 02:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
|
|
|
|
{
|
|
|
|
assert(!vstd::contains(children, child));
|
|
|
|
children.push_back(child);
|
2014-07-29 16:58:54 +03:00
|
|
|
//BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
|
2010-11-22 02:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::childDetached(CBonusSystemNode *child)
|
|
|
|
{
|
2014-09-17 12:03:53 +03:00
|
|
|
if (vstd::contains(children, child))
|
|
|
|
children -= child;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logBonus->errorStream() << std::string("Error!" + child->nodeName() + " #cannot be detached from# " + nodeName());
|
|
|
|
assert(0);
|
|
|
|
}
|
2016-08-18 14:03:20 +02:00
|
|
|
|
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
|
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
return description.size()
|
2011-02-10 15:12:53 +02:00
|
|
|
? description
|
|
|
|
: std::string("Bonus system node of type ") + typeid(*this).name();
|
2010-12-12 01:11:26 +02:00
|
|
|
}
|
|
|
|
|
2011-02-04 16:58:14 +02:00
|
|
|
void CBonusSystemNode::deserializationFix()
|
|
|
|
{
|
2011-03-01 12:19:05 +02:00
|
|
|
exportBonuses();
|
|
|
|
|
2011-02-04 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
void CBonusSystemNode::getRedParents(TNodes &out)
|
|
|
|
{
|
|
|
|
FOREACH_PARENT(pname)
|
|
|
|
{
|
|
|
|
if(pname->actsAsBonusSourceOnly())
|
|
|
|
{
|
|
|
|
out.insert(pname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!actsAsBonusSourceOnly())
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CBonusSystemNode *child : children)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
out.insert(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getRedChildren(TNodes &out)
|
|
|
|
{
|
|
|
|
FOREACH_PARENT(pname)
|
|
|
|
{
|
|
|
|
if(!pname->actsAsBonusSourceOnly())
|
|
|
|
{
|
|
|
|
out.insert(pname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(actsAsBonusSourceOnly())
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(CBonusSystemNode *child : children)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
out.insert(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : exportedBonuses)
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator)
|
|
|
|
descendant->propagateBonus(b);
|
|
|
|
|
|
|
|
FOREACH_RED_PARENT(parent)
|
|
|
|
parent->newRedDescendant(descendant);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : exportedBonuses)
|
2011-02-21 06:13:00 +02:00
|
|
|
if(b->propagator)
|
|
|
|
descendant->unpropagateBonus(b);
|
|
|
|
|
|
|
|
FOREACH_RED_PARENT(parent)
|
|
|
|
parent->removedRedDescendant(descendant);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getRedAncestors(TNodes &out)
|
|
|
|
{
|
|
|
|
getRedParents(out);
|
|
|
|
FOREACH_RED_PARENT(p)
|
|
|
|
p->getRedAncestors(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::getRedDescendants(TNodes &out)
|
|
|
|
{
|
|
|
|
getRedChildren(out);
|
|
|
|
FOREACH_RED_CHILD(c)
|
|
|
|
c->getRedChildren(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::battleTurnPassed()
|
|
|
|
{
|
2015-11-09 14:48:07 +02:00
|
|
|
updateBonuses(Bonus::NTurns);
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
2011-02-23 05:57:45 +02:00
|
|
|
void CBonusSystemNode::exportBonus(Bonus * b)
|
|
|
|
{
|
|
|
|
if(b->propagator)
|
|
|
|
propagateBonus(b);
|
|
|
|
else
|
|
|
|
bonuses.push_back(b);
|
2011-07-13 21:39:02 +03:00
|
|
|
|
2015-04-11 07:13:28 +02:00
|
|
|
CBonusSystemNode::treeHasChanged();
|
2011-02-23 05:57:45 +02:00
|
|
|
}
|
|
|
|
|
2011-03-01 12:19:05 +02:00
|
|
|
void CBonusSystemNode::exportBonuses()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(Bonus *b : exportedBonuses)
|
2011-03-01 12:19:05 +02:00
|
|
|
exportBonus(b);
|
|
|
|
}
|
|
|
|
|
2013-02-02 22:28:39 +03:00
|
|
|
CBonusSystemNode::ENodeTypes CBonusSystemNode::getNodeType() const
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
return nodeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BonusList& CBonusSystemNode::getBonusList() const
|
|
|
|
{
|
|
|
|
return bonuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TNodesVector& CBonusSystemNode::getParentNodes() const
|
|
|
|
{
|
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TNodesVector& CBonusSystemNode::getChildrenNodes() const
|
|
|
|
{
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2013-02-02 22:28:39 +03:00
|
|
|
void CBonusSystemNode::setNodeType(CBonusSystemNode::ENodeTypes type)
|
2011-07-13 21:39:02 +03:00
|
|
|
{
|
|
|
|
nodeType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
BonusList& CBonusSystemNode::getExportedBonusList()
|
|
|
|
{
|
|
|
|
return exportedBonuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& CBonusSystemNode::getDescription() const
|
|
|
|
{
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::setDescription(const std::string &description)
|
|
|
|
{
|
|
|
|
this->description = description;
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
void CBonusSystemNode::limitBonuses(const BonusList &allBonuses, BonusList &out) const
|
|
|
|
{
|
|
|
|
assert(&allBonuses != &out); //todo should it work in-place?
|
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
BonusList undecided = allBonuses,
|
2012-03-06 19:59:55 +03:00
|
|
|
&accepted = out;
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
int undecidedCount = undecided.size();
|
|
|
|
for(int i = 0; i < undecided.size(); i++)
|
|
|
|
{
|
|
|
|
Bonus *b = undecided[i];
|
|
|
|
BonusLimitationContext context = {b, *this, out};
|
2013-01-20 15:06:18 +03:00
|
|
|
int decision = b->limiter ? b->limiter->limit(context) : ILimiter::ACCEPT; //bonuses without limiters will be accepted by default
|
2012-03-06 19:59:55 +03:00
|
|
|
if(decision == ILimiter::DISCARD)
|
|
|
|
{
|
|
|
|
undecided.erase(i);
|
2013-02-13 22:35:43 +03:00
|
|
|
i--; continue;
|
2012-03-06 19:59:55 +03:00
|
|
|
}
|
|
|
|
else if(decision == ILimiter::ACCEPT)
|
|
|
|
{
|
|
|
|
accepted.push_back(b);
|
|
|
|
undecided.erase(i);
|
2013-02-13 22:35:43 +03:00
|
|
|
i--; continue;
|
2012-03-06 19:59:55 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
assert(decision == ILimiter::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
|
|
|
|
{
|
2015-12-29 04:43:33 +02:00
|
|
|
auto ret = std::make_shared<BonusList>();
|
2012-03-06 19:59:55 +03:00
|
|
|
limitBonuses(allBonuses, *ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBonusSystemNode::treeHasChanged()
|
|
|
|
{
|
|
|
|
treeChanged++;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
int NBonus::getCount(const CBonusSystemNode *obj, Bonus::BonusSource from, int id)
|
2010-04-03 06:33:46 +03:00
|
|
|
{
|
|
|
|
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)
|
2013-02-13 22:35:43 +03:00
|
|
|
return SpellID(sid).toSpell();
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Bonus::Description() const
|
|
|
|
{
|
|
|
|
std::ostringstream str;
|
2016-08-18 14:03:20 +02:00
|
|
|
|
2015-09-04 18:38:42 +02:00
|
|
|
if(description.empty())
|
|
|
|
switch(source)
|
|
|
|
{
|
|
|
|
case ARTIFACT:
|
|
|
|
str << VLC->arth->artifacts[sid]->Name();
|
|
|
|
break;;
|
|
|
|
case SPELL_EFFECT:
|
|
|
|
str << SpellID(sid).toSpell()->name;
|
|
|
|
break;
|
|
|
|
case CREATURE_ABILITY:
|
|
|
|
str << VLC->creh->creatures[sid]->namePl;
|
|
|
|
break;
|
|
|
|
case SECONDARY_SKILL:
|
|
|
|
str << VLC->generaltexth->skillName[sid]/* << " secondary skill"*/;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//todo: handle all possible sources
|
2016-08-18 14:03:20 +02:00
|
|
|
str << "Unknown";
|
2015-09-04 18:38:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str << description;
|
2016-08-18 14:03:20 +02:00
|
|
|
|
2015-09-04 18:38:42 +02:00
|
|
|
if(val != 0)
|
|
|
|
str << " " << std::showpos << val;
|
2016-08-18 14:03:20 +02:00
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
Bonus::Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
|
2011-02-20 20:32:39 +02:00
|
|
|
: duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
|
2010-07-13 08:25:40 +03:00
|
|
|
{
|
|
|
|
additionalInfo = -1;
|
|
|
|
turnsRemain = 0;
|
|
|
|
valType = ADDITIVE_VALUE;
|
|
|
|
effectRange = NO_LIMIT;
|
|
|
|
boost::algorithm::trim(description);
|
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
Bonus::Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ValueType ValType /*= ADDITIVE_VALUE*/)
|
2011-02-20 20:32:39 +02:00
|
|
|
: duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
|
2010-07-13 08:25:40 +03:00
|
|
|
{
|
|
|
|
additionalInfo = -1;
|
|
|
|
turnsRemain = 0;
|
|
|
|
effectRange = NO_LIMIT;
|
|
|
|
}
|
|
|
|
|
2016-08-18 14:03:20 +02:00
|
|
|
Bonus::Bonus()
|
2010-07-13 08:25:40 +03:00
|
|
|
{
|
2013-03-14 20:21:16 +03:00
|
|
|
duration = PERMANENT;
|
|
|
|
turnsRemain = 0;
|
|
|
|
type = NONE;
|
2010-07-13 08:25:40 +03:00
|
|
|
subtype = -1;
|
|
|
|
additionalInfo = -1;
|
2016-08-18 14:03:20 +02:00
|
|
|
|
2010-07-13 08:25:40 +03:00
|
|
|
valType = ADDITIVE_VALUE;
|
|
|
|
effectRange = NO_LIMIT;
|
2013-03-14 20:21:16 +03:00
|
|
|
val = 0;
|
2013-04-21 15:49:26 +03:00
|
|
|
source = OTHER;
|
2016-08-18 14:03:20 +02:00
|
|
|
sid = 0;
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Bonus::~Bonus()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
Bonus * Bonus::addPropagator(TPropagatorPtr Propagator)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
propagator = Propagator;
|
2010-11-20 02:03:31 +02:00
|
|
|
return this;
|
2010-07-13 08:25:40 +03:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
namespace Selector
|
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
DLL_LINKAGE CSelectFieldEqual<Bonus::BonusType> type(&Bonus::type);
|
|
|
|
DLL_LINKAGE CSelectFieldEqual<TBonusSubtype> subtype(&Bonus::subtype);
|
|
|
|
DLL_LINKAGE CSelectFieldEqual<si32> info(&Bonus::additionalInfo);
|
|
|
|
DLL_LINKAGE CSelectFieldEqual<Bonus::BonusSource> sourceType(&Bonus::source);
|
|
|
|
DLL_LINKAGE CSelectFieldEqual<Bonus::LimitEffect> effectRange(&Bonus::effectRange);
|
2011-12-14 00:23:17 +03:00
|
|
|
DLL_LINKAGE CWillLastTurns turns;
|
2015-11-09 14:01:58 +02:00
|
|
|
DLL_LINKAGE CWillLastDays days;
|
2013-07-02 15:08:30 +03:00
|
|
|
DLL_LINKAGE CSelectFieldAny<Bonus::LimitEffect> anyRange(&Bonus::effectRange);
|
2010-05-02 21:20:26 +03:00
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
CSelector DLL_LINKAGE typeSubtype(Bonus::BonusType Type, TBonusSubtype Subtype)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return type(Type).And(subtype(Subtype));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
CSelector DLL_LINKAGE typeSubtypeInfo(Bonus::BonusType type, TBonusSubtype subtype, si32 info)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return CSelectFieldEqual<Bonus::BonusType>(&Bonus::type)(type)
|
|
|
|
.And(CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype)(subtype))
|
|
|
|
.And(CSelectFieldEqual<si32>(&Bonus::additionalInfo)(info));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
CSelector DLL_LINKAGE source(Bonus::BonusSource source, ui32 sourceID)
|
2010-05-02 21:20:26 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source)
|
|
|
|
.And(CSelectFieldEqual<ui32>(&Bonus::sid)(sourceID));
|
2010-05-02 21:20:26 +03:00
|
|
|
}
|
|
|
|
|
2013-02-02 01:04:25 +03:00
|
|
|
CSelector DLL_LINKAGE sourceTypeSel(Bonus::BonusSource source)
|
2010-08-31 13:22:21 +03:00
|
|
|
{
|
2013-07-02 15:08:30 +03:00
|
|
|
return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source);
|
2010-08-31 13:22:21 +03:00
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
bool DLL_LINKAGE matchesType(const CSelector &sel, Bonus::BonusType 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
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
bool DLL_LINKAGE matchesTypeSubtype(const CSelector &sel, Bonus::BonusType type, TBonusSubtype subtype)
|
2010-05-14 05:18:37 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2011-02-21 06:13:00 +02:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
bool DLL_LINKAGE positiveSpellEffects(const Bonus *b)
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
if(b->source == Bonus::SPELL_EFFECT)
|
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
CSpell *sp = SpellID(b->sid).toSpell();
|
2012-02-17 00:19:07 +03:00
|
|
|
return sp->isPositive();
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
return false; //not a spell effect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CStack * retreiveStackBattle(const CBonusSystemNode *node)
|
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
switch(node->getNodeType())
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
|
|
|
return static_cast<const CStack*>(node);
|
|
|
|
default:
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
|
2011-02-12 18:12:48 +02:00
|
|
|
const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
|
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
switch(node->getNodeType())
|
2011-02-12 18:12:48 +02:00
|
|
|
{
|
|
|
|
case CBonusSystemNode::STACK_INSTANCE:
|
|
|
|
return (static_cast<const CStackInstance *>(node));
|
|
|
|
case CBonusSystemNode::STACK_BATTLE:
|
|
|
|
return (static_cast<const CStack*>(node))->base;
|
|
|
|
default:
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-02-12 18:12:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-20 02:03:31 +02:00
|
|
|
const CCreature * retrieveCreature(const CBonusSystemNode *node)
|
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
switch(node->getNodeType())
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
|
|
|
case CBonusSystemNode::CREATURE:
|
|
|
|
return (static_cast<const CCreature *>(node));
|
|
|
|
default:
|
2011-02-12 18:12:48 +02:00
|
|
|
const CStackInstance *csi = retreiveStackInstance(node);
|
|
|
|
if(csi)
|
|
|
|
return csi->type;
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i = 0; i < bonusList.size(); i++)
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2011-06-25 16:53:15 +03:00
|
|
|
Bonus *b = bonusList[i];
|
|
|
|
out << "Bonus " << i << "\n" << *b << std::endl;
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-02-16 17:03:47 +03:00
|
|
|
for(auto i = bonusNameMap.cbegin(); i != bonusNameMap.cend(); i++)
|
2010-07-12 13:20:25 +03:00
|
|
|
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);
|
2011-02-20 20:32:39 +02:00
|
|
|
printField(sid);
|
2010-07-12 13:20:25 +03:00
|
|
|
printField(additionalInfo);
|
|
|
|
printField(turnsRemain);
|
|
|
|
printField(valType);
|
|
|
|
printField(effectRange);
|
|
|
|
#undef printField
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2013-01-20 15:06:18 +03:00
|
|
|
|
2013-01-16 16:02:01 +03:00
|
|
|
Bonus * Bonus::addLimiter(TLimiterPtr Limiter)
|
|
|
|
{
|
2013-01-20 15:06:18 +03:00
|
|
|
if (limiter)
|
2013-01-16 16:02:01 +03:00
|
|
|
{
|
2014-03-23 15:59:03 +03:00
|
|
|
//If we already have limiter list, retrieve it
|
2013-01-20 15:06:18 +03:00
|
|
|
auto limiterList = std::dynamic_pointer_cast<LimiterList>(limiter);
|
|
|
|
if(!limiterList)
|
|
|
|
{
|
|
|
|
//Create a new limiter list with old limiter and the new one will be pushed later
|
2015-12-29 04:43:33 +02:00
|
|
|
limiterList = std::make_shared<LimiterList>();
|
2013-01-20 15:06:18 +03:00
|
|
|
limiterList->add(limiter);
|
|
|
|
limiter = limiterList;
|
|
|
|
}
|
|
|
|
|
|
|
|
limiterList->add(Limiter);
|
2013-01-16 16:02:01 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-19 20:38:37 +03:00
|
|
|
limiter = Limiter;
|
2013-01-16 16:02:01 +03:00
|
|
|
}
|
|
|
|
return this;
|
2012-12-13 13:49:12 +03:00
|
|
|
}
|
|
|
|
|
2010-07-12 13:20:25 +03:00
|
|
|
ILimiter::~ILimiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
int ILimiter::limit(const BonusLimitationContext &context) const /*return true to drop the bonus */
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
int CCreatureTypeLimiter::limit(const BonusLimitationContext &context) const
|
2010-07-12 13:20:25 +03:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
const CCreature *c = retrieveCreature(&context.node);
|
2011-02-22 11:47:25 +02:00
|
|
|
if(!c)
|
|
|
|
return true;
|
2011-02-12 18:12:48 +02:00
|
|
|
return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
|
2014-05-21 12:02:20 +03:00
|
|
|
//drop bonus if it's not our creature and (we don`t check upgrades or its not our upgrade)
|
2010-07-12 13:20:25 +03:00
|
|
|
}
|
2011-02-12 18:12:48 +02:00
|
|
|
|
2013-02-02 22:28:39 +03:00
|
|
|
CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, bool IncludeUpgrades /*= true*/)
|
2010-07-12 13:20:25 +03:00
|
|
|
:creature(&Creature), includeUpgrades(IncludeUpgrades)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCreatureTypeLimiter::CCreatureTypeLimiter()
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
creature = nullptr;
|
2010-07-12 13:20:25 +03:00
|
|
|
includeUpgrades = false;
|
2010-08-06 13:46:40 +03:00
|
|
|
}
|
|
|
|
|
2013-02-11 02:24:57 +03:00
|
|
|
void CCreatureTypeLimiter::setCreature (CreatureID id)
|
2013-01-18 14:49:15 +03:00
|
|
|
{
|
|
|
|
creature = VLC->creh->creatures[id];
|
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus )
|
2010-08-06 13:46:40 +03:00
|
|
|
: type(bonus), subtype(0), isSubtypeRelevant(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-16 17:03:47 +03:00
|
|
|
HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus, TBonusSubtype _subtype )
|
2010-08-06 13:46:40 +03:00
|
|
|
: type(bonus), subtype(_subtype), isSubtypeRelevant(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
int HasAnotherBonusLimiter::limit(const BonusLimitationContext &context) const
|
2010-08-06 13:46:40 +03:00
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
CSelector mySelector = isSubtypeRelevant
|
|
|
|
? Selector::typeSubtype(type, subtype)
|
2012-03-06 19:59:55 +03:00
|
|
|
: Selector::type(type);
|
|
|
|
|
|
|
|
//if we have a bonus of required type accepted, limiter should accept also this bonus
|
|
|
|
if(context.alreadyAccepted.getFirst(mySelector))
|
|
|
|
return ACCEPT;
|
|
|
|
|
|
|
|
//do not accept for now but it may change if more bonuses gets included
|
|
|
|
return NOT_SURE;
|
2010-08-06 13:46:40 +03:00
|
|
|
}
|
2010-11-19 00:22:51 +02:00
|
|
|
|
|
|
|
IPropagator::~IPropagator()
|
|
|
|
{
|
|
|
|
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
// CBonusSystemNode * IPropagator::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
|
|
|
|
// {
|
|
|
|
// return source;
|
|
|
|
// }
|
|
|
|
|
|
|
|
bool IPropagator::shouldBeAttached(CBonusSystemNode *dest)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CBonusSystemNode * CPropagatorNodeType::getDestNode(CBonusSystemNode *source, CBonusSystemNode *redParent, CBonusSystemNode *redChild)
|
|
|
|
// {
|
2013-06-26 14:18:27 +03:00
|
|
|
// return nullptr;
|
2011-02-21 06:13:00 +02:00
|
|
|
// }
|
|
|
|
|
|
|
|
CPropagatorNodeType::CPropagatorNodeType()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-02 22:28:39 +03:00
|
|
|
CPropagatorNodeType::CPropagatorNodeType(int NodeType)
|
2011-02-21 06:13:00 +02:00
|
|
|
: nodeType(NodeType)
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
|
|
|
|
{
|
2011-07-13 21:39:02 +03:00
|
|
|
return nodeType == dest->getNodeType();
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
|
2010-11-20 02:03:31 +02:00
|
|
|
: terrainType(TerrainType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
int CreatureNativeTerrainLimiter::limit(const BonusLimitationContext &context) const
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
const CCreature *c = retrieveCreature(&context.node);
|
2012-05-18 23:50:16 +03:00
|
|
|
return !c || !c->isItNativeTerrain(terrainType); //drop bonus for non-creatures or non-native residents
|
2011-02-21 06:13:00 +02:00
|
|
|
//TODO neutral creatues
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
|
|
|
|
: faction(Faction)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureFactionLimiter::CreatureFactionLimiter()
|
|
|
|
{
|
|
|
|
}
|
2012-03-06 19:59:55 +03:00
|
|
|
|
|
|
|
int CreatureFactionLimiter::limit(const BonusLimitationContext &context) const
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
const CCreature *c = retrieveCreature(&context.node);
|
2010-11-20 02:03:31 +02:00
|
|
|
return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureAlignmentLimiter::CreatureAlignmentLimiter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
|
|
|
|
: alignment(Alignment)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
int CreatureAlignmentLimiter::limit(const BonusLimitationContext &context) const
|
2010-11-20 02:03:31 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
const CCreature *c = retrieveCreature(&context.node);
|
2013-02-13 22:35:43 +03:00
|
|
|
if(!c)
|
2010-11-20 02:03:31 +02:00
|
|
|
return true;
|
|
|
|
switch(alignment)
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
case EAlignment::GOOD:
|
2010-11-20 02:03:31 +02:00
|
|
|
return !c->isGood(); //if not good -> return true (drop bonus)
|
2011-12-14 00:23:17 +03:00
|
|
|
case EAlignment::NEUTRAL:
|
2010-11-20 02:03:31 +02:00
|
|
|
return c->isEvil() || c->isGood();
|
2011-12-14 00:23:17 +03:00
|
|
|
case EAlignment::EVIL:
|
2010-11-20 02:03:31 +02:00
|
|
|
return !c->isEvil();
|
|
|
|
default:
|
2016-03-12 03:41:27 +02:00
|
|
|
logBonus->warnStream() << "Warning: illegal alignment in limiter!";
|
|
|
|
return true;
|
2010-11-20 02:03:31 +02:00
|
|
|
}
|
2011-02-11 10:20:26 +02:00
|
|
|
}
|
|
|
|
|
2011-02-12 18:12:48 +02:00
|
|
|
RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
|
|
|
|
:minRank(Min), maxRank(Max)
|
2011-02-11 10:20:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:13:00 +02:00
|
|
|
RankRangeLimiter::RankRangeLimiter()
|
|
|
|
{
|
|
|
|
minRank = maxRank = -1;
|
|
|
|
}
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
int RankRangeLimiter::limit(const BonusLimitationContext &context) const
|
2011-02-11 10:20:26 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
const CStackInstance *csi = retreiveStackInstance(&context.node);
|
2011-02-12 18:12:48 +02:00
|
|
|
if(csi)
|
2012-08-20 19:10:50 +03:00
|
|
|
{
|
2013-02-05 22:56:28 +03:00
|
|
|
if (csi->getNodeType() == CBonusSystemNode::COMMANDER) //no stack exp bonuses for commander creatures
|
2012-08-20 19:10:50 +03:00
|
|
|
return true;
|
2011-02-12 18:12:48 +02:00
|
|
|
return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
|
2012-08-20 19:10:50 +03:00
|
|
|
}
|
2011-02-12 18:12:48 +02:00
|
|
|
return true;
|
2011-02-21 06:13:00 +02:00
|
|
|
}
|
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
int StackOwnerLimiter::limit(const BonusLimitationContext &context) const
|
2011-02-21 06:13:00 +02:00
|
|
|
{
|
2012-03-06 19:59:55 +03:00
|
|
|
const CStack *s = retreiveStackBattle(&context.node);
|
2011-02-21 06:13:00 +02:00
|
|
|
if(s)
|
|
|
|
return s->owner != owner;
|
|
|
|
|
2012-03-06 19:59:55 +03:00
|
|
|
const CStackInstance *csi = retreiveStackInstance(&context.node);
|
2011-02-21 06:13:00 +02:00
|
|
|
if(csi && csi->armyObj)
|
|
|
|
return csi->armyObj->tempOwner != owner;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StackOwnerLimiter::StackOwnerLimiter()
|
|
|
|
: owner(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
StackOwnerLimiter::StackOwnerLimiter(PlayerColor Owner)
|
2011-02-21 06:13:00 +02:00
|
|
|
: owner(Owner)
|
|
|
|
{
|
2011-05-30 22:20:14 +03:00
|
|
|
}
|
2013-02-13 22:35:43 +03:00
|
|
|
// int Bonus::limit(const BonusLimitationContext &context) const
|
|
|
|
// 1162 {
|
|
|
|
// 1163 if (limiter)
|
|
|
|
// 1164 return limiter->callNext(context);
|
|
|
|
// 1165 else
|
|
|
|
// 1166 return ILimiter::ACCEPT; //accept if there's no limiter
|
|
|
|
// 1167 }
|
|
|
|
//1168
|
2013-01-20 15:06:18 +03:00
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
int LimiterList::limit( const BonusLimitationContext &context ) const
|
2013-01-20 15:06:18 +03:00
|
|
|
{
|
|
|
|
bool wasntSure = false;
|
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto limiter : limiters)
|
2013-01-20 15:06:18 +03:00
|
|
|
{
|
|
|
|
auto result = limiter->limit(context);
|
|
|
|
if(result == ILimiter::DISCARD)
|
|
|
|
return result;
|
|
|
|
if(result == ILimiter::NOT_SURE)
|
|
|
|
wasntSure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasntSure ? ILimiter::NOT_SURE : ILimiter::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimiterList::add( TLimiterPtr limiter )
|
|
|
|
{
|
|
|
|
limiters.push_back(limiter);
|
|
|
|
}
|