123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694 |
- /*
- * BattleSpellMechanics.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 "BattleSpellMechanics.h"
- #include "Problem.h"
- #include "CSpellHandler.h"
- #include "../battle/IBattleState.h"
- #include "../battle/CBattleInfoCallback.h"
- #include "../networkPacks/PacksForClientBattle.h"
- #include "../networkPacks/SetStackEffect.h"
- #include "../CStack.h"
- #include <vstd/RNG.h>
- VCMI_LIB_NAMESPACE_BEGIN
- namespace spells
- {
- namespace SRSLPraserHelpers
- {
- static int XYToHex(int x, int y)
- {
- return x + GameConstants::BFIELD_WIDTH * y;
- }
- static int XYToHex(std::pair<int, int> xy)
- {
- return XYToHex(xy.first, xy.second);
- }
- static int hexToY(int battleFieldPosition)
- {
- return battleFieldPosition/GameConstants::BFIELD_WIDTH;
- }
- static int hexToX(int battleFieldPosition)
- {
- int pos = battleFieldPosition - hexToY(battleFieldPosition) * GameConstants::BFIELD_WIDTH;
- return pos;
- }
- static std::pair<int, int> hexToPair(int battleFieldPosition)
- {
- return std::make_pair(hexToX(battleFieldPosition), hexToY(battleFieldPosition));
- }
- //moves hex by one hex in given direction
- //0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left
- static std::pair<int, int> gotoDir(int x, int y, int direction)
- {
- switch(direction)
- {
- case 0: //top left
- return std::make_pair((y%2) ? x-1 : x, y-1);
- case 1: //top right
- return std::make_pair((y%2) ? x : x+1, y-1);
- case 2: //right
- return std::make_pair(x+1, y);
- case 3: //right bottom
- return std::make_pair((y%2) ? x : x+1, y+1);
- case 4: //left bottom
- return std::make_pair((y%2) ? x-1 : x, y+1);
- case 5: //left
- return std::make_pair(x-1, y);
- default:
- throw std::runtime_error("Disaster: wrong direction in SRSLPraserHelpers::gotoDir!\n");
- }
- }
- static std::pair<int, int> gotoDir(std::pair<int, int> xy, int direction)
- {
- return gotoDir(xy.first, xy.second, direction);
- }
- static bool isGoodHex(std::pair<int, int> xy)
- {
- return xy.first >=0 && xy.first < GameConstants::BFIELD_WIDTH && xy.second >= 0 && xy.second < GameConstants::BFIELD_HEIGHT;
- }
- //helper function for rangeInHexes
- static std::set<ui16> getInRange(unsigned int center, int low, int high)
- {
- std::set<ui16> ret;
- if(low == 0)
- {
- ret.insert(center);
- }
- std::pair<int, int> mainPointForLayer[6]; //A, B, C, D, E, F points
- for(auto & elem : mainPointForLayer)
- elem = hexToPair(center);
- for(int it=1; it<=high; ++it) //it - distance to the center
- {
- for(int b=0; b<6; ++b)
- mainPointForLayer[b] = gotoDir(mainPointForLayer[b], b);
- if(it>=low)
- {
- std::pair<int, int> curHex;
- //adding lines (A-b, B-c, C-d, etc)
- for(int v=0; v<6; ++v)
- {
- curHex = mainPointForLayer[v];
- for(int h=0; h<it; ++h)
- {
- if(isGoodHex(curHex))
- ret.insert(XYToHex(curHex));
- curHex = gotoDir(curHex, (v+2)%6);
- }
- }
- } //if(it>=low)
- }
- return ret;
- }
- }
- BattleSpellMechanics::BattleSpellMechanics(const IBattleCast * event,
- std::shared_ptr<effects::Effects> effects_,
- std::shared_ptr<IReceptiveCheck> targetCondition_):
- BaseMechanics(event),
- effects(std::move(effects_)),
- targetCondition(std::move(targetCondition_))
- {}
- BattleSpellMechanics::~BattleSpellMechanics() = default;
- void BattleSpellMechanics::applyEffects(ServerCallback * server, const Target & targets, bool indirect, bool ignoreImmunity) const
- {
- auto callback = [&](const effects::Effect * effect, bool & stop)
- {
- if(indirect == effect->indirect)
- {
- if(ignoreImmunity)
- {
- effect->apply(server, this, targets);
- }
- else
- {
- EffectTarget filtered = effect->filterTarget(this, targets);
- effect->apply(server, this, filtered);
- }
- }
- };
- effects->forEachEffect(getEffectLevel(), callback);
- }
- bool BattleSpellMechanics::canBeCast(Problem & problem) const
- {
- auto genProblem = battle()->battleCanCastSpell(caster, mode);
- if(genProblem != ESpellCastProblem::OK)
- return adaptProblem(genProblem, problem);
- switch(mode)
- {
- case Mode::HERO:
- {
- const auto * castingHero = dynamic_cast<const CGHeroInstance *>(caster); //todo: unify hero|creature spell cost
- if(!castingHero)
- {
- logGlobal->debug("CSpell::canBeCast: invalid caster");
- genProblem = ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
- }
- else if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
- genProblem = ESpellCastProblem::NO_SPELLBOOK;
- else if(!castingHero->canCastThisSpell(owner))
- genProblem = ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
- else if(castingHero->mana < battle()->battleGetSpellCost(owner, castingHero)) //not enough mana
- genProblem = ESpellCastProblem::NOT_ENOUGH_MANA;
- }
- break;
- }
- if(genProblem != ESpellCastProblem::OK)
- return adaptProblem(genProblem, problem);
- if(!owner->isCombat())
- return adaptProblem(ESpellCastProblem::ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL, problem);
- const PlayerColor player = caster->getCasterOwner();
- const BattleSide side = battle()->playerToSide(player);
- if(side == BattleSide::NONE)
- return adaptProblem(ESpellCastProblem::INVALID, problem);
- //effect like Recanter's Cloak. Blocks also passive casting.
- //TODO: check creature abilities to block
- //TODO: check any possible caster
- if(battle()->battleMaxSpellLevel(side) < getSpellLevel() || battle()->battleMinSpellLevel(side) > getSpellLevel())
- return adaptProblem(ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED, problem);
- return effects->applicable(problem, this);
- }
- bool BattleSpellMechanics::canBeCastAt(const Target & target, Problem & problem) const
- {
- if(!canBeCast(problem))
- return false;
- Target spellTarget = transformSpellTarget(target);
- const battle::Unit * mainTarget = nullptr;
- if(spellTarget.front().unitValue)
- {
- mainTarget = target.front().unitValue;
- }
- else if(spellTarget.front().hexValue.isValid())
- {
- mainTarget = battle()->battleGetUnitByPos(target.front().hexValue, true);
- }
- if (!getSpell()->canCastOnSelf() && !getSpell()->canCastOnlyOnSelf())
- {
- if(mainTarget && mainTarget == caster)
- return false; // can't cast on self
- if(mainTarget && mainTarget->hasBonusOfType(BonusType::INVINCIBLE) && !getSpell()->getPositiveness())
- return false;
- }
- else if(getSpell()->canCastOnlyOnSelf())
- {
- if(mainTarget && mainTarget != caster)
- return false; // can't cast on others
- }
- return effects->applicable(problem, this, target, spellTarget);
- }
- std::vector<const CStack *> BattleSpellMechanics::getAffectedStacks(const Target & target) const
- {
- Target spellTarget = transformSpellTarget(target);
- EffectTarget all;
- effects->forEachEffect(getEffectLevel(), [&all, &target, &spellTarget, this](const effects::Effect * e, bool & stop)
- {
- EffectTarget one = e->transformTarget(this, target, spellTarget);
- vstd::concatenate(all, one);
- });
- std::set<const CStack *> stacks;
- for(const Destination & dest : all)
- {
- if(dest.unitValue && !dest.unitValue->hasBonusOfType(BonusType::INVINCIBLE))
- {
- //FIXME: remove and return battle::Unit
- stacks.insert(battle()->battleGetStackByID(dest.unitValue->unitId(), false));
- }
- }
- std::vector<const CStack *> res;
- std::copy(stacks.begin(), stacks.end(), std::back_inserter(res));
- return res;
- }
- void BattleSpellMechanics::cast(ServerCallback * server, const Target & target)
- {
- BattleSpellCast sc;
- int spellCost = 0;
- sc.side = casterSide;
- sc.spellID = getSpellId();
- sc.battleID = battle()->getBattle()->getBattleID();
- sc.tile = target.at(0).hexValue;
- sc.castByHero = mode == Mode::HERO;
- sc.casterStack = caster->getCasterUnitId();
- sc.manaGained = 0;
- sc.activeCast = false;
- affectedUnits.clear();
- const CGHeroInstance * otherHero = nullptr;
- {
- //check it there is opponent hero
- const BattleSide otherSide = battle()->otherSide(casterSide);
- if(battle()->battleHasHero(otherSide))
- otherHero = battle()->battleGetFightingHero(otherSide);
- }
- //calculate spell cost
- if(mode == Mode::HERO)
- {
- const auto * casterHero = dynamic_cast<const CGHeroInstance *>(caster);
- spellCost = battle()->battleGetSpellCost(owner, casterHero);
- if(nullptr != otherHero) //handle mana channel
- {
- int manaChannel = 0;
- for(const auto * stack : battle()->battleGetAllStacks(true)) //TODO: shouldn't bonus system handle it somehow?
- {
- if(stack->unitOwner() == otherHero->tempOwner)
- {
- vstd::amax(manaChannel, stack->valOfBonuses(BonusType::MANA_CHANNELING));
- }
- }
- sc.manaGained = (manaChannel * spellCost) / 100;
- }
- sc.activeCast = true;
- }
- else if(mode == Mode::CREATURE_ACTIVE || mode == Mode::ENCHANTER)
- {
- spellCost = 1;
- sc.activeCast = true;
- }
- beforeCast(sc, *server->getRNG(), target);
- BattleLogMessage castDescription;
- castDescription.battleID = battle()->getBattle()->getBattleID();
- switch (mode)
- {
- case Mode::CREATURE_ACTIVE:
- case Mode::ENCHANTER:
- case Mode::HERO:
- case Mode::PASSIVE:
- {
- MetaString line;
- caster->getCastDescription(owner, affectedUnits, line);
- if(!line.empty())
- castDescription.lines.push_back(line);
- }
- break;
- default:
- break;
- }
- doRemoveEffects(server, affectedUnits, std::bind(&BattleSpellMechanics::counteringSelector, this, _1));
- for(auto & unit : affectedUnits)
- sc.affectedCres.insert(unit->unitId());
- if(!castDescription.lines.empty())
- server->apply(castDescription);
- server->apply(sc);
- for(auto & p : effectsToApply)
- p.first->apply(server, this, p.second);
- if(sc.activeCast)
- {
- caster->spendMana(server, spellCost);
- if(sc.manaGained > 0)
- {
- assert(otherHero);
- otherHero->spendMana(server, -sc.manaGained);
- }
- }
- // send empty event to client
- // temporary(?) workaround to force animations to trigger
- StacksInjured fakeEvent;
- fakeEvent.battleID = battle()->getBattle()->getBattleID();
- server->apply(fakeEvent);
- }
- void BattleSpellMechanics::beforeCast(BattleSpellCast & sc, vstd::RNG & rng, const Target & target)
- {
- affectedUnits.clear();
- Target spellTarget = transformSpellTarget(target);
- std::vector <const battle::Unit *> resisted;
- auto filterResisted = [&, this](const battle::Unit * unit) -> bool
- {
- if(isNegativeSpell() && isMagicalEffect())
- {
- //magic resistance
- const int prob = std::min(unit->magicResistance(), 100); //probability of resistance in %
- if(rng.nextInt(0, 99) < prob)
- return true;
- }
- return false;
- };
- auto filterUnit = [&](const battle::Unit * unit)
- {
- if(filterResisted(unit))
- resisted.push_back(unit);
- else
- affectedUnits.push_back(unit);
- };
- //prepare targets
- effectsToApply = effects->prepare(this, target, spellTarget);
- std::set<const battle::Unit *> unitTargets = collectTargets();
- //process them
- for(const auto * unit : unitTargets)
- filterUnit(unit);
- //and update targets
- for(auto & p : effectsToApply)
- {
- vstd::erase_if(p.second, [&](const Destination & d)
- {
- if(!d.unitValue)
- return false;
- return vstd::contains(resisted, d.unitValue);
- });
- }
- if(mode == Mode::MAGIC_MIRROR)
- {
- if(caster->getHeroCaster() == nullptr)
- {
- sc.reflectedCres.insert(caster->getCasterUnitId());
- }
- }
- for(const auto * unit : resisted)
- sc.resistedCres.insert(unit->unitId());
- }
- void BattleSpellMechanics::castEval(ServerCallback * server, const Target & target)
- {
- affectedUnits.clear();
- //TODO: evaluate caster updates (mana usage etc.)
- //TODO: evaluate random values
- Target spellTarget = transformSpellTarget(target);
- effectsToApply = effects->prepare(this, target, spellTarget);
- std::set<const battle::Unit *> unitTargets = collectTargets();
- auto selector = std::bind(&BattleSpellMechanics::counteringSelector, this, _1);
- std::copy(std::begin(unitTargets), std::end(unitTargets), std::back_inserter(affectedUnits));
- doRemoveEffects(server, affectedUnits, selector);
- for(auto & p : effectsToApply)
- p.first->apply(server, this, p.second);
- }
- std::set<const battle::Unit *> BattleSpellMechanics::collectTargets() const
- {
- std::set<const battle::Unit *> result;
- for(const auto & p : effectsToApply)
- {
- for(const Destination & d : p.second)
- if(d.unitValue)
- result.insert(d.unitValue);
- }
- return result;
- }
- void BattleSpellMechanics::doRemoveEffects(ServerCallback * server, const battle::Units & targets, const CSelector & selector)
- {
- SetStackEffect sse;
- sse.battleID = battle()->getBattle()->getBattleID();
- for(const auto * unit : targets)
- {
- std::vector<Bonus> buffer;
- auto bl = unit->getBonuses(selector);
- for(const auto & item : *bl)
- buffer.emplace_back(*item);
- if(!buffer.empty())
- sse.toRemove.emplace_back(unit->unitId(), buffer);
- }
- if(!sse.toRemove.empty())
- server->apply(sse);
- }
- bool BattleSpellMechanics::counteringSelector(const Bonus * bonus) const
- {
- if(bonus->source != BonusSource::SPELL_EFFECT)
- return false;
- for(const SpellID & id : owner->counteredSpells)
- {
- if(bonus->sid.as<SpellID>() == id)
- return true;
- }
- return false;
- }
- BattleHexArray BattleSpellMechanics::spellRangeInHexes(BattleHex centralHex) const
- {
- using namespace SRSLPraserHelpers;
- BattleHexArray ret;
- std::vector<int> rng = owner->getLevelInfo(getRangeLevel()).range;
- for(auto & elem : rng)
- {
- std::set<ui16> curLayer = getInRange(centralHex.toInt(), elem, elem);
- //adding obtained hexes
- for(const auto & curLayer_it : curLayer)
- ret.insert(curLayer_it);
- }
- return ret;
- }
- Target BattleSpellMechanics::transformSpellTarget(const Target & aimPoint) const
- {
- Target spellTarget;
- if(aimPoint.empty())
- {
- logGlobal->error("Aimed spell cast with no destination.");
- }
- else
- {
- const Destination & primary = aimPoint.at(0);
- BattleHex aimPointHex = primary.hexValue;
- //transform primary spell target with spell range (if it`s valid), leave anything else to effects
- if(aimPointHex.isValid())
- {
- auto spellRange = spellRangeInHexes(aimPointHex);
- for(const auto & hex : spellRange)
- spellTarget.push_back(Destination(hex));
- }
- }
- if(spellTarget.empty())
- spellTarget.push_back(Destination(BattleHex::INVALID));
- return spellTarget;
- }
- std::vector<AimType> BattleSpellMechanics::getTargetTypes() const
- {
- auto ret = BaseMechanics::getTargetTypes();
- if(!ret.empty())
- {
- effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * e, bool & stop)
- {
- e->adjustTargetTypes(ret);
- stop = ret.empty();
- });
- }
- return ret;
- }
- std::vector<Destination> BattleSpellMechanics::getPossibleDestinations(size_t index, AimType aimType, const Target & current, bool fast) const
- {
- //TODO: BattleSpellMechanics::getPossibleDestinations
- if(index != 0)
- return std::vector<Destination>();
- std::vector<Destination> ret;
- switch(aimType)
- {
- case AimType::CREATURE:
- {
- auto stacks = battle()->battleGetAllStacks();
- for(auto stack : stacks)
- {
- Target tmp = current;
- tmp.emplace_back(stack->getPosition());
- detail::ProblemImpl ignored;
- if(canBeCastAt(tmp, ignored))
- ret.emplace_back(stack->getPosition());
- }
- break;
- }
- case AimType::LOCATION:
- if(fast)
- {
- auto stacks = battle()->battleGetAllStacks();
- BattleHexArray hexesToCheck;
- for(auto stack : stacks)
- {
- hexesToCheck.insert(stack->getPosition());
- hexesToCheck.insert(stack->getPosition().getNeighbouringTiles());
- }
- for(auto hex : hexesToCheck)
- {
- if(hex.isAvailable())
- {
- Target tmp = current;
- tmp.emplace_back(hex);
- detail::ProblemImpl ignored;
- if(canBeCastAt(tmp, ignored))
- ret.emplace_back(hex);
- }
- }
- }
- else
- {
- for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
- {
- BattleHex dest(i);
- if(dest.isAvailable())
- {
- Target tmp = current;
- tmp.emplace_back(dest);
- detail::ProblemImpl ignored;
- if(canBeCastAt(tmp, ignored))
- ret.emplace_back(dest);
- }
- }
- }
- break;
- case AimType::NO_TARGET:
- ret.emplace_back();
- break;
- default:
- break;
- }
- return ret;
- }
- bool BattleSpellMechanics::isReceptive(const battle::Unit * target) const
- {
- return targetCondition->isReceptive(this, target);
- }
- BattleHexArray BattleSpellMechanics::rangeInHexes(BattleHex centralHex) const
- {
- if(isMassive() || !centralHex.isValid())
- return BattleHexArray();
- Target aimPoint;
- aimPoint.push_back(Destination(centralHex));
- Target spellTarget = transformSpellTarget(aimPoint);
- BattleHexArray effectRange;
- effects->forEachEffect(getEffectLevel(), [&](const effects::Effect * effect, bool & stop)
- {
- if(!effect->indirect)
- {
- effect->adjustAffectedHexes(effectRange, this, spellTarget);
- }
- });
- return effectRange;
- }
- const Spell * BattleSpellMechanics::getSpell() const
- {
- return owner;
- }
- }
- VCMI_LIB_NAMESPACE_END
|