2017-07-20 06:08:49 +02:00
|
|
|
/*
|
|
|
|
* Sacrifice.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 "Sacrifice.h"
|
|
|
|
#include "Registry.h"
|
|
|
|
#include "../ISpellMechanics.h"
|
|
|
|
|
|
|
|
#include "../../NetPacks.h"
|
|
|
|
#include "../../battle/IBattleState.h"
|
|
|
|
#include "../../battle/CBattleInfoCallback.h"
|
|
|
|
#include "../../battle/Unit.h"
|
|
|
|
#include "../../serializer/JsonSerializeFormat.h"
|
|
|
|
|
|
|
|
|
|
|
|
static const std::string EFFECT_NAME = "core:sacrifice";
|
|
|
|
|
|
|
|
namespace spells
|
|
|
|
{
|
|
|
|
namespace effects
|
|
|
|
{
|
|
|
|
|
|
|
|
VCMI_REGISTER_SPELL_EFFECT(Sacrifice, EFFECT_NAME);
|
|
|
|
|
|
|
|
Sacrifice::Sacrifice()
|
|
|
|
: Heal()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Sacrifice::~Sacrifice() = default;
|
|
|
|
|
|
|
|
void Sacrifice::adjustTargetTypes(std::vector<TargetType> & types) const
|
|
|
|
{
|
|
|
|
if(!types.empty())
|
|
|
|
{
|
|
|
|
if(types[0] != AimType::CREATURE)
|
|
|
|
{
|
|
|
|
types.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(types.size() == 1)
|
|
|
|
{
|
|
|
|
types.push_back(AimType::CREATURE);
|
|
|
|
}
|
|
|
|
else if(types.size() > 1)
|
|
|
|
{
|
|
|
|
if(types[1] != AimType::CREATURE)
|
|
|
|
types.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sacrifice::applicable(Problem & problem, const Mechanics * m) const
|
|
|
|
{
|
|
|
|
auto mainFilter = std::bind(&UnitEffect::getStackFilter, this, m, true, _1);
|
|
|
|
auto predicate = std::bind(&UnitEffect::eraseByImmunityFilter, this, m, _1);
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto targets = m->battle()->battleGetUnitsIf(mainFilter);
|
2017-07-20 06:08:49 +02:00
|
|
|
vstd::erase_if(targets, predicate);
|
|
|
|
|
|
|
|
bool targetExists = false;
|
|
|
|
bool targetToSacrificeExists = false;
|
|
|
|
|
|
|
|
for(auto & target : targets)
|
|
|
|
{
|
|
|
|
if(target->alive())
|
|
|
|
targetToSacrificeExists = true;
|
|
|
|
else if(target->isDead())
|
|
|
|
targetExists = true;
|
|
|
|
|
|
|
|
if(targetExists && targetToSacrificeExists)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(targetExists && targetToSacrificeExists))
|
|
|
|
{
|
|
|
|
MetaString text;
|
|
|
|
text.addTxt(MetaString::GENERAL_TXT, 185);
|
|
|
|
problem.add(std::move(text), Problem::NORMAL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sacrifice::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
|
|
|
//TODO: support for multiple targets?
|
|
|
|
|
|
|
|
if(target.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
EffectTarget healTarget;
|
|
|
|
healTarget.emplace_back(target.front());
|
|
|
|
|
|
|
|
if(!Heal::applicable(problem, m, healTarget))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(target.size() == 2)
|
|
|
|
{
|
|
|
|
auto victim = target.at(1).unitValue;
|
|
|
|
if(!victim)
|
|
|
|
return false;
|
2019-03-20 21:58:15 +02:00
|
|
|
|
2019-03-29 13:10:26 +02:00
|
|
|
return victim->alive() && getStackFilter(m, false, victim) && isReceptive(m, victim);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
void Sacrifice::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if(target.size() != 2)
|
|
|
|
{
|
|
|
|
logGlobal->error("Sacrifice effect requires 2 targets");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const battle::Unit * victim = target.back().unitValue;
|
|
|
|
|
|
|
|
if(!victim)
|
|
|
|
{
|
|
|
|
logGlobal->error("No unit to Sacrifice");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget healTarget;
|
|
|
|
healTarget.emplace_back(target.front());
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
Heal::apply(calculateHealEffectValue(m, victim), server, m, healTarget);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
BattleUnitsChanged removeUnits;
|
|
|
|
removeUnits.changedStacks.emplace_back(victim->unitId(), UnitChanges::EOperation::REMOVE);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
server->apply(&removeUnits);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sacrifice::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
|
|
|
|
{
|
|
|
|
return unit->isValidTarget(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget Sacrifice::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
EffectTarget res = Heal::transformTarget(m, aimPoint, spellTarget);
|
|
|
|
|
|
|
|
//ignore spell range for now, arbitrary range support requires redesign
|
|
|
|
res.resize(1);
|
|
|
|
|
|
|
|
//add victim
|
|
|
|
if(aimPoint.size() >= 2)
|
|
|
|
{
|
|
|
|
auto victim = aimPoint.at(1).unitValue;
|
|
|
|
if(victim && getStackFilter(m, false, victim) && isReceptive(m, victim))
|
|
|
|
res.emplace_back(victim);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t Sacrifice::calculateHealEffectValue(const Mechanics * m, const battle::Unit * victim) const
|
|
|
|
{
|
|
|
|
return (m->getEffectPower() + victim->MaxHealth() + m->calculateRawEffectValue(0, 1)) * victim->getCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|