1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/lib/spells/effects/Summon.cpp
AlexVinS 0b70baa95e Spells configuration version 2 (effect-based)
* Indirect spell effects loading
* Json serializer improvements
* spell->canBeCastAt do not allow useless cast for any spell
* Added proxy caster class for spell-created obstacles
* Handle damage from spell-created obstacles inside mechanics
* Experimental GameState integration/regression tests
* Ignore mod settings and load only "vcmi" mod when running tests
* fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests)
* Huge improvements of BattleAI regarding spell casts
* AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells.
* Possible fix for https://bugs.vcmi.eu/view.php?id=1811
* CStack factored out to several classes
* [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional
* [Battle] Allowed BattleAction have multiple destinations
* [Spells] Converted limit|immunity to target condition
* [Spells] Use partial configuration reload for backward compatibility handling
* [Tests] Started tests for CUnitState
* Partial fixes of fire shield effect
* [Battle] Do HP calculations in 64 bits
* [BattleAI] Use threading for spell cast evaluation
* [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state)
* Implemented https://bugs.vcmi.eu/view.php?id=2811
* plug rare freeze when hypnotized unit shots vertically
* Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense
* [BattleAI] Try to not waste a cast if battle is actually won already
* Extended JsonSerializeFormat API
* fixed https://bugs.vcmi.eu/view.php?id=2847
* Any unit effect can be now chained (not only damage like Chain Lightning)
** only damage effect for now actually uses "chainFactor"
* Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2018-02-08 11:37:21 +03:00

181 lines
4.3 KiB
C++

/*
* Summon.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 "Summon.h"
#include "Registry.h"
#include "../ISpellMechanics.h"
#include "../../battle/CBattleInfoCallback.h"
#include "../../battle/Unit.h"
#include "../../NetPacks.h"
#include "../../serializer/JsonSerializeFormat.h"
#include "../../CHeroHandler.h"
#include "../../mapObjects/CGHeroInstance.h"
static const std::string EFFECT_NAME = "core:summon";
namespace spells
{
namespace effects
{
VCMI_REGISTER_SPELL_EFFECT(Summon, EFFECT_NAME);
Summon::Summon()
: Effect(),
creature(),
permanent(false),
exclusive(true)
{
}
Summon::~Summon() = default;
void Summon::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
{
//no hexes affected
}
void Summon::adjustTargetTypes(std::vector<TargetType> & types) const
{
//any target type allowed
}
bool Summon::applicable(Problem & problem, const Mechanics * m) const
{
if(!exclusive)
return true;
//check if there are summoned elementals of other type
auto otherSummoned = m->cb->battleGetUnitsIf([m, this](const battle::Unit * unit)
{
return (unit->unitOwner() == m->getCasterColor())
&& (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
&& (!unit->isClone())
&& (unit->creatureId() != creature);
});
if(!otherSummoned.empty())
{
auto elemental = otherSummoned.front();
MetaString text;
text.addTxt(MetaString::GENERAL_TXT, 538);
auto caster = dynamic_cast<const CGHeroInstance *>(m->caster);
if(caster)
{
text.addReplacement(caster->name);
text.addReplacement(MetaString::CRE_PL_NAMES, elemental->creatureIndex());
if(caster->type->sex)
text.addReplacement(MetaString::GENERAL_TXT, 540);
else
text.addReplacement(MetaString::GENERAL_TXT, 539);
}
problem.add(std::move(text), Problem::NORMAL);
return false;
}
return true;
}
void Summon::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
{
//new feature - percentage bonus
auto amount = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));
if(amount < 1)
{
battleState->complain("Summoning didn't summon any!");
return;
}
BattleUnitsChanged pack;
for(auto & dest : target)
{
if(dest.unitValue)
{
const battle::Unit * summoned = dest.unitValue;
std::shared_ptr<battle::Unit> state = summoned->acquire();
int64_t healthValue = amount * summoned->MaxHealth();
state->heal(healthValue, EHealLevel::OVERHEAL, (permanent ? EHealPower::PERMANENT : EHealPower::ONE_BATTLE));
pack.changedStacks.emplace_back(summoned->unitId(), UnitChanges::EOperation::RESET_STATE);
state->save(pack.changedStacks.back().data);
}
else
{
battle::UnitInfo info;
info.id = m->cb->battleNextUnitId();
info.count = amount;
info.type = creature;
info.side = m->casterSide;
info.position = dest.hexValue;
info.summoned = !permanent;
pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
info.save(pack.changedStacks.back().data);
}
}
if(!pack.changedStacks.empty())
battleState->apply(&pack);
}
EffectTarget Summon::filterTarget(const Mechanics * m, const EffectTarget & target) const
{
return target;
}
void Summon::serializeJsonEffect(JsonSerializeFormat & handler)
{
handler.serializeId("id", creature, CreatureID());
handler.serializeBool("permanent", permanent, false);
handler.serializeBool("exclusive", exclusive, true);
}
EffectTarget Summon::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
{
auto sameSummoned = m->cb->battleGetUnitsIf([m, this](const battle::Unit * unit)
{
return (unit->unitOwner() == m->getCasterColor())
&& (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
&& (!unit->isClone())
&& (unit->creatureId() == creature)
&& (unit->alive());
});
EffectTarget effectTarget;
if(sameSummoned.empty())
{
BattleHex hex = m->cb->getAvaliableHex(creature, m->casterSide);
if(!hex.isValid())
logGlobal->error("No free space to summon creature!");
else
effectTarget.emplace_back(hex);
}
else
{
effectTarget.emplace_back(sameSummoned.front());
}
return effectTarget;
}
}
}