From 96c17505aeb2e77ea0a1b3c35717f1517c99d3a2 Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Sat, 1 Oct 2016 09:07:18 +0300 Subject: [PATCH] get rid of IBonusBearer::getSpellBonuses & Selector::anyRange --- client/battle/CBattleInterface.cpp | 11 ++++++----- client/windows/CCreatureWindow.cpp | 20 ++++++++++---------- lib/BattleState.cpp | 10 +++++++++- lib/HeroBonus.cpp | 13 ------------- lib/HeroBonus.h | 21 --------------------- lib/spells/CreatureSpellMechanics.cpp | 14 +------------- 6 files changed, 26 insertions(+), 63 deletions(-) diff --git a/client/battle/CBattleInterface.cpp b/client/battle/CBattleInterface.cpp index 7dce73f77..81ca2e59e 100644 --- a/client/battle/CBattleInterface.cpp +++ b/client/battle/CBattleInterface.cpp @@ -3206,12 +3206,12 @@ void CBattleInterface::showAliveStacks(SDL_Surface * to, std::vector int + auto getEffectsPositivness = [&](const std::vector & activeSpells) -> int { int pos = 0; - for(auto & spellId : stack->activeSpells()) + for(const auto & spellId : activeSpells) { - pos += CGI->spellh->objects[ spellId ]->positiveness; + pos += CGI->spellh->objects.at(spellId)->positiveness; } return pos; }; @@ -3243,8 +3243,9 @@ void CBattleInterface::showAliveStacks(SDL_Surface * to, std::vectorgetSpellBonuses()->empty()) - amountBG = getAmountBoxBackground(getEffectsPositivness(stack)); + std::vector activeSpells = stack->activeSpells(); + if(!activeSpells.empty()) + amountBG = getAmountBoxBackground(getEffectsPositivness(activeSpells)); SDL_Rect temp_rect = genRect(amountBG->h, amountBG->w, creAnims[stack->ID]->pos.x + xAdd, creAnims[stack->ID]->pos.y + yAdd); SDL_BlitSurface(amountBG, nullptr, to, &temp_rect); diff --git a/client/windows/CCreatureWindow.cpp b/client/windows/CCreatureWindow.cpp index 18274ee50..70674e607 100644 --- a/client/windows/CCreatureWindow.cpp +++ b/client/windows/CCreatureWindow.cpp @@ -324,14 +324,14 @@ void CStackWindow::CWindowSection::createActiveSpells() for(si32 effect : spells) { const CSpell * sp = CGI->spellh->objects[effect]; - + std::string spellText; - + //not all effects have graphics (for eg. Acid Breath) //for modded spells iconEffect is added to SpellInt.def - const bool hasGraphics = (effect < SpellID::THUNDERBOLT) || (effect >= SpellID::AFTER_LAST); - - if (hasGraphics) + const bool hasGraphics = (effect < SpellID::THUNDERBOLT) || (effect >= SpellID::AFTER_LAST); + + if (hasGraphics) { spellText = CGI->generaltexth->allTexts[610]; //"%s, duration: %d rounds." boost::replace_first (spellText, "%s", sp->name); @@ -582,7 +582,7 @@ void CStackWindow::CWindowSection::createButtonPanel() } if(LOCPLINT->cb->getResourceAmount().canAfford(totalCost)) - { + { LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[207], onUpgrade, nullptr, true, resComps); } else @@ -751,7 +751,7 @@ void CStackWindow::initSections() void CStackWindow::initBonusesList() { BonusList output, input; - input = *(info->stackNode->getBonuses(CSelector(Bonus::Permanent).And(Selector::anyRange()))); + input = *(info->stackNode->getBonuses(CSelector(Bonus::Permanent), Selector::all)); while (!input.empty()) { @@ -840,7 +840,7 @@ CStackWindow::CStackWindow(const CStackInstance * stack, bool popup): info->creature = stack->type; info->creatureCount = stack->count; info->popupWindow = popup; - info->owner = dynamic_cast (stack->armyObj); + info->owner = dynamic_cast (stack->armyObj); init(); } @@ -870,7 +870,7 @@ CStackWindow::CStackWindow(const CCommanderInstance * commander, bool popup): info->commander = commander; info->creatureCount = 1; info->popupWindow = popup; - info->owner = dynamic_cast (commander->armyObj); + info->owner = dynamic_cast (commander->armyObj); init(); } @@ -885,7 +885,7 @@ CStackWindow::CStackWindow(const CCommanderInstance * commander, std::vectorlevelupInfo = StackWindowInfo::CommanderLevelInfo(); info->levelupInfo->skills = skills; info->levelupInfo->callback = callback; - info->owner = dynamic_cast (commander->armyObj); + info->owner = dynamic_cast (commander->armyObj); init(); } diff --git a/lib/BattleState.cpp b/lib/BattleState.cpp index f923d6961..09fd59bef 100644 --- a/lib/BattleState.cpp +++ b/lib/BattleState.cpp @@ -973,7 +973,15 @@ std::vector CStack::activeSpells() const { std::vector ret; - TBonusListPtr spellEffects = getSpellBonuses(); + std::stringstream cachingStr; + cachingStr << "!type_" << Bonus::NONE << "source_" << Bonus::SPELL_EFFECT; + CSelector selector = Selector::sourceType(Bonus::SPELL_EFFECT) + .And(CSelector([](const Bonus *b)->bool + { + return b->type != Bonus::NONE; + })); + + TBonusListPtr spellEffects = getBonuses(selector, Selector::all, cachingStr.str()); for(const std::shared_ptr it : *spellEffects) { if (!vstd::contains(ret, it->sid)) //do not duplicate spells with multiple effects diff --git a/lib/HeroBonus.cpp b/lib/HeroBonus.cpp index fe1e43354..81294c29b 100644 --- a/lib/HeroBonus.cpp +++ b/lib/HeroBonus.cpp @@ -487,18 +487,6 @@ bool IBonusBearer::isLiving() const //TODO: theoreticaly there exists "LIVING" b .Or(Selector::type(Bonus::SIEGE_WEAPON)), cachingStr.str()); } -const TBonusListPtr IBonusBearer::getSpellBonuses() const -{ - std::stringstream cachingStr; - cachingStr << "!type_" << Bonus::NONE << "source_" << Bonus::SPELL_EFFECT; - CSelector selector = Selector::sourceType(Bonus::SPELL_EFFECT) - .And(CSelector([](const Bonus *b)->bool - { - return b->type != Bonus::NONE; - })); - return getBonuses(selector, Selector::anyRange(), cachingStr.str()); -} - const std::shared_ptr IBonusBearer::getBonus(const CSelector &selector) const { auto bonuses = getAllBonuses(Selector::all, Selector::all); @@ -1159,7 +1147,6 @@ namespace Selector DLL_LINKAGE CSelectFieldEqual effectRange(&Bonus::effectRange); DLL_LINKAGE CWillLastTurns turns; DLL_LINKAGE CWillLastDays days; - DLL_LINKAGE CSelectFieldAny anyRange(&Bonus::effectRange); CSelector DLL_LINKAGE typeSubtype(Bonus::BonusType Type, TBonusSubtype Subtype) { diff --git a/lib/HeroBonus.h b/lib/HeroBonus.h index 9e3d01bb2..39641792c 100644 --- a/lib/HeroBonus.h +++ b/lib/HeroBonus.h @@ -607,7 +607,6 @@ public: si32 manaLimit() const; //maximum mana value for this hero (basically 10*knowledge) int getPrimSkillLevel(PrimarySkill::PrimarySkill id) const; - const TBonusListPtr getSpellBonuses() const; }; class DLL_LINKAGE CBonusSystemNode : public IBonusBearer, public boost::noncopyable @@ -757,25 +756,6 @@ public: } }; -template -class CSelectFieldAny //allows to ignore value of certain field, that is to accept any value -{ - T Bonus::*ptr; -public: - CSelectFieldAny(T Bonus::*Ptr) - : ptr(Ptr) - { - } - bool operator()(const Bonus *bonus) const - { - return true; - } - CSelectFieldAny& operator()() - { - return *this; - } -}; - template //can be same, needed for subtype field class CSelectFieldEqualOrEvery { @@ -979,7 +959,6 @@ namespace Selector extern DLL_LINKAGE CSelectFieldEqual effectRange; extern DLL_LINKAGE CWillLastTurns turns; extern DLL_LINKAGE CWillLastDays days; - extern DLL_LINKAGE CSelectFieldAny anyRange; CSelector DLL_LINKAGE typeSubtype(Bonus::BonusType Type, TBonusSubtype Subtype); CSelector DLL_LINKAGE typeSubtypeInfo(Bonus::BonusType type, TBonusSubtype subtype, si32 info); diff --git a/lib/spells/CreatureSpellMechanics.cpp b/lib/spells/CreatureSpellMechanics.cpp index fab76f28d..98d54f4a1 100644 --- a/lib/spells/CreatureSpellMechanics.cpp +++ b/lib/spells/CreatureSpellMechanics.cpp @@ -88,20 +88,8 @@ void DispellHelpfulMechanics::applyBattle(BattleInfo * battle, const BattleSpell ESpellCastProblem::ESpellCastProblem DispellHelpfulMechanics::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const { - TBonusListPtr spellBon = obj->getSpellBonuses(); - bool hasPositiveSpell = false; - for(const std::shared_ptr b : *spellBon) - { - if(SpellID(b->sid).toSpell()->isPositive()) - { - hasPositiveSpell = true; - break; - } - } - if(!hasPositiveSpell) - { + if(!obj->hasBonus(Selector::positiveSpellEffects, Selector::all, "Selector::positiveSpellEffects")) return ESpellCastProblem::NO_SPELLS_TO_DISPEL; - } //use default algorithm only if there is no mechanics-related problem return DefaultSpellMechanics::isImmuneByStack(caster,obj);