2017-07-20 06:08:49 +02:00
|
|
|
/*
|
|
|
|
* 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"
|
2023-08-31 17:45:52 +02:00
|
|
|
#include "../../battle/BattleInfo.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../../battle/Unit.h"
|
|
|
|
#include "../../serializer/JsonSerializeFormat.h"
|
2018-02-20 07:08:34 +02:00
|
|
|
#include "../../CCreatureHandler.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../../mapObjects/CGHeroInstance.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../../networkPacks/PacksForClientBattle.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
namespace spells
|
|
|
|
{
|
|
|
|
namespace effects
|
|
|
|
{
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-12-09 18:08:22 +02:00
|
|
|
if (creature == CreatureID::NONE)
|
|
|
|
{
|
|
|
|
logMod->error("Attempt to summon non-existing creature!");
|
|
|
|
return m->adaptGenericProblem(problem);
|
|
|
|
}
|
|
|
|
|
2024-04-07 17:37:17 +02:00
|
|
|
if (summonedCreatureAmount(m) == 0)
|
|
|
|
{
|
|
|
|
logMod->debug("Attempt to summon zero creatures!");
|
|
|
|
return m->adaptGenericProblem(problem);
|
|
|
|
}
|
|
|
|
|
2018-02-20 07:08:34 +02:00
|
|
|
if(exclusive)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2018-02-20 07:08:34 +02:00
|
|
|
//check if there are summoned creatures of other type
|
2017-07-20 06:08:49 +02:00
|
|
|
|
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 otherSummoned = m->battle()->battleGetUnitsIf([m, this](const battle::Unit * unit)
|
2018-02-20 07:08:34 +02:00
|
|
|
{
|
|
|
|
return (unit->unitOwner() == m->getCasterColor())
|
|
|
|
&& (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
|
|
|
|
&& (!unit->isClone())
|
|
|
|
&& (unit->creatureId() != creature);
|
|
|
|
});
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2018-02-20 07:08:34 +02:00
|
|
|
if(!otherSummoned.empty())
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2023-02-07 00:40:01 +02:00
|
|
|
const auto *elemental = otherSummoned.front();
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2018-02-20 07:08:34 +02:00
|
|
|
MetaString text;
|
2023-06-18 11:18:25 +02:00
|
|
|
text.appendLocalString(EMetaText::GENERAL_TXT, 538);
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-02-07 00:40:01 +02:00
|
|
|
const auto *caster = dynamic_cast<const CGHeroInstance *>(m->caster);
|
2018-02-20 07:08:34 +02:00
|
|
|
if(caster)
|
|
|
|
{
|
2023-06-18 11:18:25 +02:00
|
|
|
text.replaceRawString(caster->getNameTranslated());
|
2018-02-20 07:08:34 +02:00
|
|
|
|
2023-11-02 22:01:49 +02:00
|
|
|
text.replaceNamePlural(elemental->creatureId());
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2024-10-05 21:37:52 +02:00
|
|
|
if(caster->gender == EHeroGender::FEMALE)
|
2023-06-18 11:18:25 +02:00
|
|
|
text.replaceLocalString(EMetaText::GENERAL_TXT, 540);
|
2018-02-20 07:08:34 +02:00
|
|
|
else
|
2023-06-18 11:18:25 +02:00
|
|
|
text.replaceLocalString(EMetaText::GENERAL_TXT, 539);
|
2018-02-20 07:08:34 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
problem.add(std::move(text), Problem::NORMAL);
|
|
|
|
return false;
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-07 17:37:17 +02:00
|
|
|
int32_t Summon::summonedCreatureHealth(const Mechanics * m, const battle::Unit * unit) const
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2024-04-07 17:37:17 +02:00
|
|
|
auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));
|
|
|
|
|
|
|
|
if(summonByHealth)
|
|
|
|
return valueWithBonus;
|
|
|
|
else
|
|
|
|
return valueWithBonus * unit->getMaxHealth();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Summon::summonedCreatureAmount(const Mechanics * m) const
|
|
|
|
{
|
|
|
|
auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));
|
|
|
|
|
|
|
|
if(summonByHealth)
|
|
|
|
{
|
|
|
|
const auto *creatureType = creature.toEntity(m->creatures());
|
|
|
|
auto creatureMaxHealth = creatureType->getMaxHealth();
|
|
|
|
return valueWithBonus / creatureMaxHealth;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return valueWithBonus;
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2024-04-07 17:37:17 +02:00
|
|
|
void Summon::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
BattleUnitsChanged pack;
|
2023-08-31 17:45:52 +02:00
|
|
|
pack.battleID = m->battle()->getBattle()->getBattleID();
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2023-02-07 00:40:01 +02:00
|
|
|
for(const auto & dest : target)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
if(dest.unitValue)
|
|
|
|
{
|
|
|
|
const battle::Unit * summoned = dest.unitValue;
|
|
|
|
std::shared_ptr<battle::Unit> state = summoned->acquire();
|
2024-04-07 17:37:17 +02:00
|
|
|
int64_t healthValue = summonedCreatureHealth(m, summoned);
|
2017-07-20 06:08:49 +02:00
|
|
|
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
|
|
|
|
{
|
2024-04-07 17:37:17 +02:00
|
|
|
int32_t amount = summonedCreatureAmount(m);
|
2018-02-20 07:08:34 +02:00
|
|
|
|
|
|
|
if(amount < 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
|
|
|
server->complain("Summoning didn't summon any!");
|
2018-02-20 07:08:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
battle::UnitInfo info;
|
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
|
|
|
info.id = m->battle()->battleNextUnitId();
|
2017-07-20 06:08:49 +02:00
|
|
|
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())
|
2024-10-04 20:59:51 +02:00
|
|
|
server->apply(pack);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-02-20 07:08:34 +02:00
|
|
|
handler.serializeBool("summonByHealth", summonByHealth, false);
|
2018-03-03 19:37:49 +02:00
|
|
|
handler.serializeBool("summonSameUnit", summonSameUnit, false);
|
2017-07-20 06:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget Summon::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
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 sameSummoned = m->battle()->battleGetUnitsIf([m, this](const battle::Unit * unit)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
return (unit->unitOwner() == m->getCasterColor())
|
|
|
|
&& (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
|
|
|
|
&& (!unit->isClone())
|
|
|
|
&& (unit->creatureId() == creature)
|
|
|
|
&& (unit->alive());
|
|
|
|
});
|
|
|
|
|
|
|
|
EffectTarget effectTarget;
|
|
|
|
|
2018-03-03 19:37:49 +02:00
|
|
|
if(sameSummoned.empty() || !summonSameUnit)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
2024-06-24 03:23:26 +02:00
|
|
|
BattleHex hex = m->battle()->getAvailableHex(creature, m->casterSide);
|
2017-07-20 06:08:49 +02:00
|
|
|
if(!hex.isValid())
|
|
|
|
logGlobal->error("No free space to summon creature!");
|
|
|
|
else
|
|
|
|
effectTarget.emplace_back(hex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
effectTarget.emplace_back(sameSummoned.front());
|
|
|
|
}
|
|
|
|
|
|
|
|
return effectTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|