1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588 |
- /*
- * 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
- *
- */
- #include "StdInc.h"
- #include "HeroBonus.h"
- #include "VCMI_Lib.h"
- #include "CSpellHandler.h"
- #include "CCreatureHandler.h"
- #include "CCreatureSet.h"
- #include "CHeroHandler.h"
- #include "CGeneralTextHandler.h"
- #include "BattleState.h"
- #include "CArtHandler.h"
- #include "GameConstants.h"
- #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)
- #define BONUS_NAME(x) { #x, Bonus::x },
- const std::map<std::string, Bonus::BonusType> bonusNameMap = { BONUS_LIST };
- #undef BONUS_NAME
- #define BONUS_VALUE(x) { #x, Bonus::x },
- const std::map<std::string, Bonus::ValueType> bonusValueMap = { BONUS_VALUE_LIST };
- #undef BONUS_VALUE
- #define BONUS_SOURCE(x) { #x, Bonus::x },
- const std::map<std::string, Bonus::BonusSource> bonusSourceMap = { BONUS_SOURCE_LIST };
- #undef BONUS_SOURCE
- #define BONUS_ITEM(x) { #x, Bonus::x },
- const std::map<std::string, ui16> bonusDurationMap =
- {
- 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)
- BONUS_ITEM(COMMANDER_KILLED)
- };
- const std::map<std::string, Bonus::LimitEffect> bonusLimitEffect =
- {
- BONUS_ITEM(NO_LIMIT)
- BONUS_ITEM(ONLY_DISTANCE_FIGHT)
- BONUS_ITEM(ONLY_MELEE_FIGHT)
- BONUS_ITEM(ONLY_ENEMY_ARMY)
- };
- const std::map<std::string, TLimiterPtr> bonusLimiterMap =
- {
- {"SHOOTER_ONLY", make_shared<HasAnotherBonusLimiter>(Bonus::SHOOTER)},
- {"DRAGON_NATURE", make_shared<HasAnotherBonusLimiter>(Bonus::DRAGON_NATURE)},
- {"IS_UNDEAD", make_shared<HasAnotherBonusLimiter>(Bonus::UNDEAD)}
- };
- const std::map<std::string, TPropagatorPtr> bonusPropagatorMap =
- {
- {"BATTLE_WIDE", make_shared<CPropagatorNodeType>(CBonusSystemNode::BATTLE)},
- {"VISITED_TOWN_AND_VISITOR", make_shared<CPropagatorNodeType>(CBonusSystemNode::TOWN_AND_VISITOR)},
- {"PLAYER_PROPAGATOR", make_shared<CPropagatorNodeType>(CBonusSystemNode::PLAYER)},
- {"HERO", make_shared<CPropagatorNodeType>(CBonusSystemNode::HERO)},
- {"TEAM_PROPAGATOR", make_shared<CPropagatorNodeType>(CBonusSystemNode::TEAM)}, //untested
- {"GLOBAL_EFFECT", make_shared<CPropagatorNodeType>(CBonusSystemNode::GLOBAL_EFFECTS)}
- }; //untested
- #define BONUS_LOG_LINE(x) logBonus->traceStream() << x
- int CBonusSystemNode::treeChanged = 1;
- const bool CBonusSystemNode::cachingEnabled = true;
- 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
- {
- int base = 0;
- int percentToBase = 0;
- int percentToAll = 0;
- int additive = 0;
- int indepMax = 0;
- bool hasIndepMax = false;
- int indepMin = 0;
- bool hasIndepMin = false;
- for (auto & elem : bonuses)
- {
- Bonus *b = elem;
- switch(b->valType)
- {
- case Bonus::BASE_NUMBER:
- base += b->val;
- break;
- case Bonus::PERCENT_TO_ALL:
- percentToAll += b->val;
- break;
- case Bonus::PERCENT_TO_BASE:
- percentToBase += b->val;
- break;
- case Bonus::ADDITIVE_VALUE:
- additive += b->val;
- break;
- case Bonus::INDEPENDENT_MAX:
- if (!hasIndepMax)
- {
- indepMax = b->val;
- hasIndepMax = true;
- }
- else
- {
- vstd::amax(indepMax, b->val);
- }
- break;
- case Bonus::INDEPENDENT_MIN:
- if (!hasIndepMin)
- {
- indepMin = b->val;
- hasIndepMin = true;
- }
- else
- {
- vstd::amin(indepMin, b->val);
- }
- break;
- }
- }
- int modifiedBase = base + (base * percentToBase) / 100;
- modifiedBase += additive;
- int valFirst = (modifiedBase * (100 + percentToAll)) / 100;
- if(hasIndepMin && hasIndepMax)
- assert(indepMin < indepMax);
- const int notIndepBonuses = boost::count_if(bonuses, [](const Bonus *b)
- {
- return b->valType != Bonus::INDEPENDENT_MAX && b->valType != Bonus::INDEPENDENT_MIN;
- });
- if (hasIndepMax)
- {
- if(notIndepBonuses)
- vstd::amax(valFirst, indepMax);
- else
- valFirst = indepMax;
- }
- if (hasIndepMin)
- {
- if(notIndepBonuses)
- vstd::amin(valFirst, indepMin);
- else
- valFirst = indepMin;
- }
- return valFirst;
- }
- const Bonus * BonusList::getFirst(const CSelector &selector) const
- {
- for (auto & elem : bonuses)
- {
- const Bonus *b = elem;
- if(selector(b))
- return &*b;
- }
- return nullptr;
- }
- Bonus * BonusList::getFirst(const CSelector &select)
- {
- for (auto & elem : bonuses)
- {
- Bonus *b = elem;
- if(select(b))
- return &*b;
- }
- return nullptr;
- }
- void BonusList::getModifiersWDescr(TModDescr &out) const
- {
- for (auto & elem : bonuses)
- {
- Bonus *b = elem;
- out.push_back(std::make_pair(b->val, b->Description()));
- }
- }
- void BonusList::getBonuses(BonusList & out, const CSelector &selector) const
- {
- // for(Bonus *i : *this)
- // if(selector(i) && i->effectRange == Bonus::NO_LIMIT)
- // out.push_back(i);
- getBonuses(out, selector, nullptr);
- }
- void BonusList::getBonuses(BonusList & out, const CSelector &selector, const CSelector &limit) const
- {
- for (auto & elem : bonuses)
- {
- Bonus *b = elem;
- //add matching bonuses that matches limit predicate or have NO_LIMIT if no given predicate
- if(selector(b) && ((!limit && b->effectRange == Bonus::NO_LIMIT) || ((bool)limit && limit(b))))
- out.push_back(b);
- }
- }
- void BonusList::getAllBonuses(BonusList &out) const
- {
- for(Bonus *b : bonuses)
- out.push_back(b);
- }
- int BonusList::valOfBonuses(const CSelector &select) const
- {
- BonusList ret;
- CSelector limit = nullptr;
- getBonuses(ret, select, limit);
- ret.eliminateDuplicates();
- return ret.totalValue();
- }
- // void BonusList::limit(const CBonusSystemNode &node)
- // {
- // remove_if(std::bind(&CBonusSystemNode::isLimitedOnUs, std::ref(node), _1));
- // }
- void BonusList::eliminateDuplicates()
- {
- sort( bonuses.begin(), bonuses.end() );
- bonuses.erase( unique( bonuses.begin(), bonuses.end() ), bonuses.end() );
- }
- void BonusList::push_back(Bonus* const &x)
- {
- bonuses.push_back(x);
- if (belongsToTree)
- CBonusSystemNode::incrementTreeChangedNum();
- }
- std::vector<Bonus*>::iterator BonusList::erase(const int position)
- {
- if (belongsToTree)
- CBonusSystemNode::incrementTreeChangedNum();
- return bonuses.erase(bonuses.begin() + position);
- }
- void BonusList::clear()
- {
- bonuses.clear();
- if (belongsToTree)
- CBonusSystemNode::incrementTreeChangedNum();
- }
- std::vector<BonusList*>::size_type BonusList::operator-=(Bonus* const &i)
- {
- auto itr = std::find(bonuses.begin(), bonuses.end(), i);
- if(itr == bonuses.end())
- return false;
- bonuses.erase(itr);
- if (belongsToTree)
- CBonusSystemNode::incrementTreeChangedNum();
- return true;
- }
- void BonusList::resize(std::vector<Bonus*>::size_type sz, Bonus* c )
- {
- bonuses.resize(sz, c);
- if (belongsToTree)
- CBonusSystemNode::incrementTreeChangedNum();
- }
- void BonusList::insert(std::vector<Bonus*>::iterator position, std::vector<Bonus*>::size_type n, Bonus* const &x)
- {
- bonuses.insert(position, n, x);
- if (belongsToTree)
- CBonusSystemNode::incrementTreeChangedNum();
- }
- int IBonusBearer::valOfBonuses(Bonus::BonusType type, const CSelector &selector) const
- {
- return valOfBonuses(Selector::type(type).And(selector));
- }
- int IBonusBearer::valOfBonuses(Bonus::BonusType type, int subtype /*= -1*/) const
- {
- std::stringstream cachingStr;
- cachingStr << "type_" << type << "s_" << subtype;
- CSelector s = Selector::type(type);
- if(subtype != -1)
- s = s.And(Selector::subtype(subtype));
- return valOfBonuses(s, cachingStr.str());
- }
- int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
- {
- CSelector limit = nullptr;
- TBonusListPtr hlp = getAllBonuses(selector, limit, nullptr, cachingStr);
- return hlp->totalValue();
- }
- bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr /*= ""*/) const
- {
- return getBonuses(selector, cachingStr)->size() > 0;
- }
- bool IBonusBearer::hasBonusOfType(Bonus::BonusType type, int subtype /*= -1*/) const
- {
- std::stringstream cachingStr;
- cachingStr << "type_" << type << "s_" << subtype;
- CSelector s = Selector::type(type);
- if(subtype != -1)
- s = s.And(Selector::subtype(subtype));
- return hasBonus(s, cachingStr.str());
- }
- void IBonusBearer::getModifiersWDescr(TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */) const
- {
- std::stringstream cachingStr;
- cachingStr << "type_" << type << "s_" << subtype;
- getModifiersWDescr(out, subtype != -1 ? Selector::typeSubtype(type, subtype) : Selector::type(type), cachingStr.str());
- }
- void IBonusBearer::getModifiersWDescr(TModDescr &out, const CSelector &selector, const std::string &cachingStr /* =""*/) const
- {
- getBonuses(selector, cachingStr)->getModifiersWDescr(out);
- }
- int IBonusBearer::getBonusesCount(Bonus::BonusSource from, int id) const
- {
- std::stringstream cachingStr;
- cachingStr << "source_" << from << "id_" << id;
- return getBonusesCount(Selector::source(from, id), cachingStr.str());
- }
- int IBonusBearer::getBonusesCount(const CSelector &selector, const std::string &cachingStr /* =""*/) const
- {
- return getBonuses(selector, cachingStr)->size();
- }
- const TBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr /*= ""*/) const
- {
- return getAllBonuses(selector, nullptr, nullptr, cachingStr);
- }
- const TBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSelector &limit, const std::string &cachingStr /*= ""*/) const
- {
- return getAllBonuses(selector, limit, nullptr, cachingStr);
- }
- bool IBonusBearer::hasBonusFrom(Bonus::BonusSource source, ui32 sourceID) const
- {
- std::stringstream cachingStr;
- cachingStr << "source_" << source << "id_" << sourceID;
- return hasBonus(Selector::source(source,sourceID), cachingStr.str());
- }
- int IBonusBearer::MoraleVal() const
- {
- if(hasBonusOfType(Bonus::NON_LIVING) || hasBonusOfType(Bonus::UNDEAD) ||
- hasBonusOfType(Bonus::NO_MORALE) || hasBonusOfType(Bonus::SIEGE_WEAPON))
- return 0;
- int ret = valOfBonuses(Bonus::MORALE);
- if(hasBonusOfType(Bonus::SELF_MORALE)) //eg. minotaur
- vstd::amax(ret, +1);
- return vstd::abetween(ret, -3, +3);
- }
- int IBonusBearer::LuckVal() const
- {
- if(hasBonusOfType(Bonus::NO_LUCK))
- return 0;
- int ret = valOfBonuses(Bonus::LUCK);
- if(hasBonusOfType(Bonus::SELF_LUCK)) //eg. halfling
- vstd::amax(ret, +1);
- return vstd::abetween(ret, -3, +3);
- }
- si32 IBonusBearer::Attack() const
- {
- si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
- if (double frenzyPower = valOfBonuses(Bonus::IN_FRENZY)) //frenzy for attacker
- {
- ret += (frenzyPower/100) * (double)Defense(false);
- }
- vstd::amax(ret, 0);
- return ret;
- }
- si32 IBonusBearer::Defense(bool withFrenzy /*= true*/) const
- {
- si32 ret = valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
- if(withFrenzy && hasBonusOfType(Bonus::IN_FRENZY)) //frenzy for defender
- {
- return 0;
- }
- vstd::amax(ret, 0);
- return ret;
- }
- ui32 IBonusBearer::MaxHealth() const
- {
- return std::max(1, valOfBonuses(Bonus::STACK_HEALTH)); //never 0
- }
- ui32 IBonusBearer::getMinDamage() const
- {
- std::stringstream cachingStr;
- cachingStr << "type_" << Bonus::CREATURE_DAMAGE << "s_0Otype_" << Bonus::CREATURE_DAMAGE << "s_1";
- return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 1)), cachingStr.str());
- }
- ui32 IBonusBearer::getMaxDamage() const
- {
- std::stringstream cachingStr;
- cachingStr << "type_" << Bonus::CREATURE_DAMAGE << "s_0Otype_" << Bonus::CREATURE_DAMAGE << "s_2";
- return valOfBonuses(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 0).Or(Selector::typeSubtype(Bonus::CREATURE_DAMAGE, 2)), cachingStr.str());
- }
- si32 IBonusBearer::manaLimit() const
- {
- return si32(getPrimSkillLevel(PrimarySkill::KNOWLEDGE)
- * (100.0 + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::INTELLIGENCE))
- / 10.0);
- }
- int IBonusBearer::getPrimSkillLevel(PrimarySkill::PrimarySkill id) const
- {
- int ret = 0;
- if(id == PrimarySkill::ATTACK)
- ret = Attack();
- else if(id == PrimarySkill::DEFENSE)
- ret = Defense();
- else
- ret = valOfBonuses(Bonus::PRIMARY_SKILL, id);
- vstd::amax(ret, id/2); //minimal value is 0 for attack and defense and 1 for spell power and knowledge
- return ret;
- }
- si32 IBonusBearer::magicResistance() const
- {
- return valOfBonuses(Bonus::MAGIC_RESISTANCE);
- }
- ui32 IBonusBearer::Speed( int turn /*= 0*/ , bool useBind /* = false*/) const
- {
- //war machines cannot move
- if(hasBonus(Selector::type(Bonus::SIEGE_WEAPON).And(Selector::turns(turn))))
- {
- return 0;
- }
- //bind effect check - doesn't influence stack initiative
- if (useBind && getEffect (SpellID::BIND))
- {
- return 0;
- }
- return valOfBonuses(Selector::type(Bonus::STACKS_SPEED).And(Selector::turns(turn)));
- }
- bool IBonusBearer::isLiving() const //TODO: theoreticaly there exists "LIVING" bonus in stack experience documentation
- {
- std::stringstream cachingStr;
- cachingStr << "type_" << Bonus::UNDEAD << "s_-1Otype_" << Bonus::NON_LIVING << "s_-11type_" << Bonus::SIEGE_WEAPON; //I don't really get what string labels mean?
- return !hasBonus(Selector::type(Bonus::UNDEAD)
- .Or(Selector::type(Bonus::NON_LIVING))
- .Or(Selector::type(Bonus::SIEGE_WEAPON)), cachingStr.str());
- }
- const TBonusListPtr IBonusBearer::getSpellBonuses() const
- {
- std::stringstream cachingStr;
- cachingStr << "source_" << Bonus::SPELL_EFFECT;
- return getBonuses(Selector::sourceType(Bonus::SPELL_EFFECT), Selector::anyRange(), cachingStr.str());
- }
- const Bonus * IBonusBearer::getEffect(ui16 id, int turn /*= 0*/) const
- {
- //TODO should check only local bonuses?
- auto bonuses = getAllBonuses();
- for(const Bonus *it : *bonuses)
- {
- if(it->source == Bonus::SPELL_EFFECT && it->sid == id)
- {
- if(!turn || it->turnsRemain > turn)
- return &(*it);
- }
- }
- return nullptr;
- }
- ui8 IBonusBearer::howManyEffectsSet(ui16 id) const
- {
- //TODO should check only local bonuses?
- ui8 ret = 0;
- auto bonuses = getAllBonuses();
- for(const Bonus *it : *bonuses)
- {
- 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)
- {
- Bonus *ret = bonuses.getFirst(selector);
- if(ret)
- return ret;
- FOREACH_PARENT(pname)
- {
- ret = pname->getBonusLocalFirst(selector);
- if (ret)
- return ret;
- }
- return nullptr;
- }
- const Bonus * CBonusSystemNode::getBonusLocalFirst( const CSelector &selector ) const
- {
- return (const_cast<CBonusSystemNode*>(this))->getBonusLocalFirst(selector);
- }
- void CBonusSystemNode::getParents(TCNodes &out) const /*retreives list of parent nodes (nodes to inherit bonuses from) */
- {
- for (auto & elem : parents)
- {
- const CBonusSystemNode *parent = elem;
- out.insert(parent);
- }
- }
- void CBonusSystemNode::getParents(TNodes &out)
- {
- for (auto & elem : parents)
- {
- const CBonusSystemNode *parent = elem;
- out.insert(const_cast<CBonusSystemNode*>(parent));
- }
- }
- void CBonusSystemNode::getBonusesRec(BonusList &out, const CSelector &selector, const CSelector &limit) const
- {
- 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);
- }
- const TBonusListPtr CBonusSystemNode::getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= nullptr*/, const std::string &cachingStr /*= ""*/) const
- {
- bool limitOnUs = (!root || root == this); //caching won't work when we want to limit bonuses against an external node
- if (CBonusSystemNode::cachingEnabled && limitOnUs)
- {
- // Exclusive access for one thread
- static boost::mutex m;
- boost::mutex::scoped_lock lock(m);
- // If the bonus system tree changes(state of a single node or the relations to each other) then
- // cache all bonus objects. Selector objects doesn't matter.
- if (cachedLast != treeChanged)
- {
- cachedBonuses.clear();
- cachedRequests.clear();
- BonusList allBonuses;
- getAllBonusesRec(allBonuses);
- allBonuses.eliminateDuplicates();
- limitBonuses(allBonuses, cachedBonuses);
- cachedLast = treeChanged;
- }
- // If a bonus system request comes with a caching string then look up in the map if there are any
- // pre-calculated bonus results. Limiters can't be cached so they have to be calculated.
- if (cachingStr != "")
- {
- auto it = cachedRequests.find(cachingStr);
- if(it != cachedRequests.end())
- {
- //Cached list contains bonuses for our query with applied limiters
- return it->second;
- }
- }
- //We still don't have the bonuses (didn't returned them from cache)
- //Perform bonus selection
- auto ret = make_shared<BonusList>();
- cachedBonuses.getBonuses(*ret, selector, limit);
- // Save the results in the cache
- if(cachingStr != "")
- cachedRequests[cachingStr] = ret;
- return ret;
- }
- else
- {
- return getAllBonusesWithoutCaching(selector, limit, root);
- }
- }
- const TBonusListPtr CBonusSystemNode::getAllBonusesWithoutCaching(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root /*= nullptr*/) const
- {
- auto ret = make_shared<BonusList>();
- // 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);
- }
- else if(root)
- {
- //We want to limit our query against an external node. We get all its bonuses,
- // add the ones we're considering and see if they're cut out by limiters
- BonusList rootBonuses, limitedRootBonuses;
- getAllBonusesRec(rootBonuses);
- for(Bonus *b : beforeLimiting)
- rootBonuses.push_back(b);
- rootBonuses.eliminateDuplicates();
- root->limitBonuses(rootBonuses, limitedRootBonuses);
- for(Bonus *b : beforeLimiting)
- if(vstd::contains(limitedRootBonuses, b))
- afterLimiting.push_back(b);
- afterLimiting.getBonuses(*ret, selector, limit);
- }
- else
- beforeLimiting.getBonuses(*ret, selector, limit);
- return ret;
- }
- CBonusSystemNode::CBonusSystemNode() : bonuses(true), exportedBonuses(true), nodeType(UNKNOWN), cachedLast(0)
- {
- }
- CBonusSystemNode::~CBonusSystemNode()
- {
- detachFromAll();
- if(children.size())
- {
- logBonus->warnStream() << "Warning: an orphaned child!";
- while(children.size())
- children.front()->detachFrom(this);
- }
- for(Bonus *b : exportedBonuses)
- delete b;
- }
- void CBonusSystemNode::attachTo(CBonusSystemNode *parent)
- {
- assert(!vstd::contains(parents, parent));
- parents.push_back(parent);
- if(parent->actsAsBonusSourceOnly())
- parent->newRedDescendant(this);
- else
- newRedDescendant(parent);
- parent->newChildAttached(this);
- CBonusSystemNode::treeChanged++;
- }
- void CBonusSystemNode::detachFrom(CBonusSystemNode *parent)
- {
- assert(vstd::contains(parents, parent));
- if(parent->actsAsBonusSourceOnly())
- parent->removedRedDescendant(this);
- else
- removedRedDescendant(parent);
- parents -= parent;
- parent->childDetached(this);
- CBonusSystemNode::treeChanged++;
- }
- void CBonusSystemNode::popBonuses(const CSelector &s)
- {
- BonusList bl;
- exportedBonuses.getBonuses(bl, s);
- for(Bonus *b : bl)
- removeBonus(b);
- for(CBonusSystemNode *child : children)
- child->popBonuses(s);
- }
- // void CBonusSystemNode::addNewBonus(const Bonus &b)
- // {
- // addNewBonus(new Bonus(b));
- // }
- void CBonusSystemNode::addNewBonus(Bonus *b)
- {
- assert(!vstd::contains(exportedBonuses,b));
- exportedBonuses.push_back(b);
- exportBonus(b);
- CBonusSystemNode::treeChanged++;
- }
- void CBonusSystemNode::accumulateBonus(Bonus &b)
- {
- Bonus *bonus = exportedBonuses.getFirst(Selector::typeSubtype(b.type, b.subtype)); //only local bonuses are interesting //TODO: what about value type?
- if(bonus)
- bonus->val += b.val;
- else
- addNewBonus(new Bonus(b)); //duplicate needed, original may get destroyed
- }
- void CBonusSystemNode::removeBonus(Bonus *b)
- {
- exportedBonuses -= b;
- if(b->propagator)
- unpropagateBonus(b);
- else
- bonuses -= b;
- vstd::clear_pointer(b);
- CBonusSystemNode::treeChanged++;
- }
- bool CBonusSystemNode::actsAsBonusSourceOnly() const
- {
- switch(nodeType)
- {
- case CREATURE:
- case ARTIFACT:
- case ARTIFACT_INSTANCE:
- return true;
- default:
- return false;
- }
- }
- void CBonusSystemNode::propagateBonus(Bonus * b)
- {
- if(b->propagator->shouldBeAttached(this))
- {
- bonuses.push_back(b);
- BONUS_LOG_LINE("#$# " << b->Description() << " #propagated to# " << nodeName());
- }
- FOREACH_RED_CHILD(child)
- child->propagateBonus(b);
- }
- void CBonusSystemNode::unpropagateBonus(Bonus * b)
- {
- if(b->propagator->shouldBeAttached(this))
- {
- bonuses -= b;
- while(vstd::contains(bonuses, b))
- {
- logBonus->errorStream() << "Bonus was duplicated (" << b->Description() << ") at " << nodeName();
- bonuses -= b;
- }
- BONUS_LOG_LINE("#$#" << b->Description() << " #is no longer propagated to# " << nodeName());
- }
- FOREACH_RED_CHILD(child)
- child->unpropagateBonus(b);
- }
- void CBonusSystemNode::newChildAttached(CBonusSystemNode *child)
- {
- assert(!vstd::contains(children, child));
- children.push_back(child);
- //BONUS_LOG_LINE(child->nodeName() << " #attached to# " << nodeName());
- }
- void CBonusSystemNode::childDetached(CBonusSystemNode *child)
- {
- if (vstd::contains(children, child))
- children -= child;
- else
- {
- logBonus->errorStream() << std::string("Error!" + child->nodeName() + " #cannot be detached from# " + nodeName());
- assert(0);
- }
-
- }
- void CBonusSystemNode::detachFromAll()
- {
- while(parents.size())
- detachFrom(parents.front());
- }
- bool CBonusSystemNode::isIndependentNode() const
- {
- return parents.empty() && children.empty();
- }
- std::string CBonusSystemNode::nodeName() const
- {
- return description.size()
- ? description
- : std::string("Bonus system node of type ") + typeid(*this).name();
- }
- void CBonusSystemNode::deserializationFix()
- {
- exportBonuses();
- }
- void CBonusSystemNode::getRedParents(TNodes &out)
- {
- FOREACH_PARENT(pname)
- {
- if(pname->actsAsBonusSourceOnly())
- {
- out.insert(pname);
- }
- }
- if(!actsAsBonusSourceOnly())
- {
- for(CBonusSystemNode *child : children)
- {
- out.insert(child);
- }
- }
- }
- void CBonusSystemNode::getRedChildren(TNodes &out)
- {
- FOREACH_PARENT(pname)
- {
- if(!pname->actsAsBonusSourceOnly())
- {
- out.insert(pname);
- }
- }
- if(actsAsBonusSourceOnly())
- {
- for(CBonusSystemNode *child : children)
- {
- out.insert(child);
- }
- }
- }
- void CBonusSystemNode::newRedDescendant(CBonusSystemNode *descendant)
- {
- for(Bonus *b : exportedBonuses)
- if(b->propagator)
- descendant->propagateBonus(b);
- FOREACH_RED_PARENT(parent)
- parent->newRedDescendant(descendant);
- }
- void CBonusSystemNode::removedRedDescendant(CBonusSystemNode *descendant)
- {
- for(Bonus *b : exportedBonuses)
- 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()
- {
- BonusList bonusesCpy = exportedBonuses; //copy, because removing bonuses invalidates iters
- for (auto & elem : bonusesCpy)
- {
- Bonus *b = elem;
- if(b->duration & Bonus::N_TURNS)
- {
- b->turnsRemain--;
- if(b->turnsRemain <= 0)
- removeBonus(b);
- }
- }
- }
- void CBonusSystemNode::exportBonus(Bonus * b)
- {
- if(b->propagator)
- propagateBonus(b);
- else
- bonuses.push_back(b);
- CBonusSystemNode::treeChanged++;
- }
- void CBonusSystemNode::exportBonuses()
- {
- for(Bonus *b : exportedBonuses)
- exportBonus(b);
- }
- CBonusSystemNode::ENodeTypes CBonusSystemNode::getNodeType() const
- {
- return nodeType;
- }
- BonusList& CBonusSystemNode::getBonusList()
- {
- return bonuses;
- }
- const BonusList& CBonusSystemNode::getBonusList() const
- {
- return bonuses;
- }
- const TNodesVector& CBonusSystemNode::getParentNodes() const
- {
- return parents;
- }
- const TNodesVector& CBonusSystemNode::getChildrenNodes() const
- {
- return children;
- }
- void CBonusSystemNode::setNodeType(CBonusSystemNode::ENodeTypes type)
- {
- nodeType = type;
- }
- BonusList& CBonusSystemNode::getExportedBonusList()
- {
- return exportedBonuses;
- }
- const std::string& CBonusSystemNode::getDescription() const
- {
- return description;
- }
- void CBonusSystemNode::setDescription(const std::string &description)
- {
- this->description = description;
- }
- void CBonusSystemNode::incrementTreeChangedNum()
- {
- treeChanged++;
- }
- void CBonusSystemNode::limitBonuses(const BonusList &allBonuses, BonusList &out) const
- {
- assert(&allBonuses != &out); //todo should it work in-place?
- BonusList undecided = allBonuses,
- &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};
- int decision = b->limiter ? b->limiter->limit(context) : ILimiter::ACCEPT; //bonuses without limiters will be accepted by default
- if(decision == ILimiter::DISCARD)
- {
- undecided.erase(i);
- i--; continue;
- }
- else if(decision == ILimiter::ACCEPT)
- {
- accepted.push_back(b);
- undecided.erase(i);
- i--; continue;
- }
- 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
- {
- auto ret = make_shared<BonusList>();
- limitBonuses(allBonuses, *ret);
- return ret;
- }
- void CBonusSystemNode::treeHasChanged()
- {
- treeChanged++;
- }
- int NBonus::valOf(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
- {
- if(obj)
- return obj->valOfBonuses(type, subtype);
- return 0;
- }
- bool NBonus::hasOfType(const CBonusSystemNode *obj, Bonus::BonusType type, int subtype /*= -1*/)
- {
- if(obj)
- return obj->hasBonusOfType(type, subtype);
- return false;
- }
- void NBonus::getModifiersWDescr(const CBonusSystemNode *obj, TModDescr &out, Bonus::BonusType type, int subtype /*= -1 */)
- {
- if(obj)
- return obj->getModifiersWDescr(out, type, subtype);
- }
- int NBonus::getCount(const CBonusSystemNode *obj, Bonus::BonusSource from, int id)
- {
- if(obj)
- return obj->getBonusesCount(from, id);
- return 0;
- }
- const CSpell * Bonus::sourceSpell() const
- {
- if(source == SPELL_EFFECT)
- return SpellID(sid).toSpell();
- return nullptr;
- }
- std::string Bonus::Description() const
- {
- if(description.size())
- return description;
- std::ostringstream str;
- str << std::showpos << val << " ";
- 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;
- }
- return str.str();
- }
- Bonus::Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype/*=-1*/)
- : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), description(Desc)
- {
- additionalInfo = -1;
- turnsRemain = 0;
- valType = ADDITIVE_VALUE;
- effectRange = NO_LIMIT;
- boost::algorithm::trim(description);
- }
- Bonus::Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype/*=-1*/, ValueType ValType /*= ADDITIVE_VALUE*/)
- : duration(Dur), type(Type), subtype(Subtype), source(Src), val(Val), sid(ID), valType(ValType)
- {
- additionalInfo = -1;
- turnsRemain = 0;
- effectRange = NO_LIMIT;
- }
- Bonus::Bonus()
- {
- duration = PERMANENT;
- turnsRemain = 0;
- type = NONE;
- subtype = -1;
- additionalInfo = -1;
-
- valType = ADDITIVE_VALUE;
- effectRange = NO_LIMIT;
- val = 0;
- source = OTHER;
- }
- Bonus::~Bonus()
- {
- }
- Bonus * Bonus::addPropagator(TPropagatorPtr Propagator)
- {
- propagator = Propagator;
- return this;
- }
- namespace Selector
- {
- 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<ui16> duration(&Bonus::duration);
- DLL_LINKAGE CSelectFieldEqual<Bonus::BonusSource> sourceType(&Bonus::source);
- DLL_LINKAGE CSelectFieldEqual<Bonus::LimitEffect> effectRange(&Bonus::effectRange);
- DLL_LINKAGE CWillLastTurns turns;
- DLL_LINKAGE CSelectFieldAny<Bonus::LimitEffect> anyRange(&Bonus::effectRange);
- CSelector DLL_LINKAGE typeSubtype(Bonus::BonusType Type, TBonusSubtype Subtype)
- {
- return type(Type).And(subtype(Subtype));
- }
- CSelector DLL_LINKAGE typeSubtypeInfo(Bonus::BonusType type, TBonusSubtype subtype, si32 info)
- {
- return CSelectFieldEqual<Bonus::BonusType>(&Bonus::type)(type)
- .And(CSelectFieldEqual<TBonusSubtype>(&Bonus::subtype)(subtype))
- .And(CSelectFieldEqual<si32>(&Bonus::additionalInfo)(info));
- }
- CSelector DLL_LINKAGE source(Bonus::BonusSource source, ui32 sourceID)
- {
- return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source)
- .And(CSelectFieldEqual<ui32>(&Bonus::sid)(sourceID));
- }
- CSelector DLL_EXPORT durationType(ui16 duration)
- {
- return CSelectFieldEqual<ui16>(&Bonus::duration)(duration);
- }
- CSelector DLL_LINKAGE sourceTypeSel(Bonus::BonusSource source)
- {
- return CSelectFieldEqual<Bonus::BonusSource>(&Bonus::source)(source);
- }
- bool DLL_LINKAGE matchesType(const CSelector &sel, Bonus::BonusType type)
- {
- Bonus dummy;
- dummy.type = type;
- return sel(&dummy);
- }
- bool DLL_LINKAGE matchesTypeSubtype(const CSelector &sel, Bonus::BonusType type, TBonusSubtype subtype)
- {
- Bonus dummy;
- dummy.type = type;
- dummy.subtype = subtype;
- return sel(&dummy);
- }
- bool DLL_LINKAGE positiveSpellEffects(const Bonus *b)
- {
- if(b->source == Bonus::SPELL_EFFECT)
- {
- CSpell *sp = SpellID(b->sid).toSpell();
- return sp->isPositive();
- }
- return false; //not a spell effect
- }
- }
- const CStack * retreiveStackBattle(const CBonusSystemNode *node)
- {
- switch(node->getNodeType())
- {
- case CBonusSystemNode::STACK_BATTLE:
- return static_cast<const CStack*>(node);
- default:
- return nullptr;
- }
- }
- const CStackInstance * retreiveStackInstance(const CBonusSystemNode *node)
- {
- switch(node->getNodeType())
- {
- case CBonusSystemNode::STACK_INSTANCE:
- return (static_cast<const CStackInstance *>(node));
- case CBonusSystemNode::STACK_BATTLE:
- return (static_cast<const CStack*>(node))->base;
- default:
- return nullptr;
- }
- }
- const CCreature * retrieveCreature(const CBonusSystemNode *node)
- {
- switch(node->getNodeType())
- {
- case CBonusSystemNode::CREATURE:
- return (static_cast<const CCreature *>(node));
- default:
- const CStackInstance *csi = retreiveStackInstance(node);
- if(csi)
- return csi->type;
- return nullptr;
- }
- }
- DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const BonusList &bonusList)
- {
- for (ui32 i = 0; i < bonusList.size(); i++)
- {
- Bonus *b = bonusList[i];
- out << "Bonus " << i << "\n" << *b << std::endl;
- }
- return out;
- }
- DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
- {
- for(auto i = bonusNameMap.cbegin(); i != bonusNameMap.cend(); 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(sid);
- printField(additionalInfo);
- printField(turnsRemain);
- printField(valType);
- printField(effectRange);
- #undef printField
- return out;
- }
- Bonus * Bonus::addLimiter(TLimiterPtr Limiter)
- {
- if (limiter)
- {
- //If we already have limiter list, retrieve it
- 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
- limiterList = make_shared<LimiterList>();
- limiterList->add(limiter);
- limiter = limiterList;
- }
- limiterList->add(Limiter);
- }
- else
- {
- limiter = Limiter;
- }
- return this;
- }
- ILimiter::~ILimiter()
- {
- }
- int ILimiter::limit(const BonusLimitationContext &context) const /*return true to drop the bonus */
- {
- return false;
- }
- int CCreatureTypeLimiter::limit(const BonusLimitationContext &context) const
- {
- const CCreature *c = retrieveCreature(&context.node);
- if(!c)
- return true;
- return c != creature && (!includeUpgrades || !creature->isMyUpgrade(c));
- //drop bonus if it's not our creature and (we don`t check upgrades or its not our upgrade)
- }
- CCreatureTypeLimiter::CCreatureTypeLimiter(const CCreature &Creature, bool IncludeUpgrades /*= true*/)
- :creature(&Creature), includeUpgrades(IncludeUpgrades)
- {
- }
- CCreatureTypeLimiter::CCreatureTypeLimiter()
- {
- creature = nullptr;
- includeUpgrades = false;
- }
- void CCreatureTypeLimiter::setCreature (CreatureID id)
- {
- creature = VLC->creh->creatures[id];
- }
- HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus )
- : type(bonus), subtype(0), isSubtypeRelevant(false)
- {
- }
- HasAnotherBonusLimiter::HasAnotherBonusLimiter( Bonus::BonusType bonus, TBonusSubtype _subtype )
- : type(bonus), subtype(_subtype), isSubtypeRelevant(true)
- {
- }
- int HasAnotherBonusLimiter::limit(const BonusLimitationContext &context) const
- {
- CSelector mySelector = isSubtypeRelevant
- ? Selector::typeSubtype(type, subtype)
- : 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;
- }
- IPropagator::~IPropagator()
- {
- }
- // 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)
- // {
- // return nullptr;
- // }
- CPropagatorNodeType::CPropagatorNodeType()
- {
- }
- CPropagatorNodeType::CPropagatorNodeType(int NodeType)
- : nodeType(NodeType)
- {
- }
- bool CPropagatorNodeType::shouldBeAttached(CBonusSystemNode *dest)
- {
- return nodeType == dest->getNodeType();
- }
- CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter(int TerrainType)
- : terrainType(TerrainType)
- {
- }
- CreatureNativeTerrainLimiter::CreatureNativeTerrainLimiter()
- {
- }
- int CreatureNativeTerrainLimiter::limit(const BonusLimitationContext &context) const
- {
- const CCreature *c = retrieveCreature(&context.node);
- return !c || !c->isItNativeTerrain(terrainType); //drop bonus for non-creatures or non-native residents
- //TODO neutral creatues
- }
- CreatureFactionLimiter::CreatureFactionLimiter(int Faction)
- : faction(Faction)
- {
- }
- CreatureFactionLimiter::CreatureFactionLimiter()
- {
- }
- int CreatureFactionLimiter::limit(const BonusLimitationContext &context) const
- {
- const CCreature *c = retrieveCreature(&context.node);
- return !c || c->faction != faction; //drop bonus for non-creatures or non-native residents
- }
- CreatureAlignmentLimiter::CreatureAlignmentLimiter()
- {
- }
- CreatureAlignmentLimiter::CreatureAlignmentLimiter(si8 Alignment)
- : alignment(Alignment)
- {
- }
- int CreatureAlignmentLimiter::limit(const BonusLimitationContext &context) const
- {
- const CCreature *c = retrieveCreature(&context.node);
- if(!c)
- return true;
- switch(alignment)
- {
- case EAlignment::GOOD:
- return !c->isGood(); //if not good -> return true (drop bonus)
- case EAlignment::NEUTRAL:
- return c->isEvil() || c->isGood();
- case EAlignment::EVIL:
- return !c->isEvil();
- default:
- logBonus->warnStream() << "Warning: illegal alignment in limiter!";
- return true;
- }
- }
- RankRangeLimiter::RankRangeLimiter(ui8 Min, ui8 Max)
- :minRank(Min), maxRank(Max)
- {
- }
- RankRangeLimiter::RankRangeLimiter()
- {
- minRank = maxRank = -1;
- }
- int RankRangeLimiter::limit(const BonusLimitationContext &context) const
- {
- const CStackInstance *csi = retreiveStackInstance(&context.node);
- if(csi)
- {
- if (csi->getNodeType() == CBonusSystemNode::COMMANDER) //no stack exp bonuses for commander creatures
- return true;
- return csi->getExpRank() < minRank || csi->getExpRank() > maxRank;
- }
- return true;
- }
- int StackOwnerLimiter::limit(const BonusLimitationContext &context) const
- {
- const CStack *s = retreiveStackBattle(&context.node);
- if(s)
- return s->owner != owner;
- const CStackInstance *csi = retreiveStackInstance(&context.node);
- if(csi && csi->armyObj)
- return csi->armyObj->tempOwner != owner;
- return true;
- }
- StackOwnerLimiter::StackOwnerLimiter()
- : owner(-1)
- {
- }
- StackOwnerLimiter::StackOwnerLimiter(PlayerColor Owner)
- : owner(Owner)
- {
- }
- // 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
- int LimiterList::limit( const BonusLimitationContext &context ) const
- {
- bool wasntSure = false;
- for(auto limiter : limiters)
- {
- 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);
- }
|