2023-03-16 00:49:40 +02:00
|
|
|
/*
|
|
|
|
* Moat.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 "Moat.h"
|
|
|
|
|
|
|
|
#include "Registry.h"
|
|
|
|
#include "../ISpellMechanics.h"
|
|
|
|
|
2024-07-21 12:49:40 +02:00
|
|
|
#include "../../entities/building/CBuilding.h"
|
2023-03-16 00:49:40 +02:00
|
|
|
#include "../../mapObjects/CGTownInstance.h"
|
2023-04-30 15:52:48 +02:00
|
|
|
#include "../../bonuses/Limiters.h"
|
2023-03-16 00:49:40 +02:00
|
|
|
#include "../../battle/IBattleState.h"
|
|
|
|
#include "../../battle/CBattleInfoCallback.h"
|
2024-02-14 15:48:06 +02:00
|
|
|
#include "../../json/JsonBonus.h"
|
2023-03-16 00:49:40 +02:00
|
|
|
#include "../../serializer/JsonSerializeFormat.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../../networkPacks/PacksForClient.h"
|
|
|
|
#include "../../networkPacks/PacksForClientBattle.h"
|
2023-03-16 00:49:40 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
namespace spells
|
|
|
|
{
|
|
|
|
namespace effects
|
|
|
|
{
|
|
|
|
|
2023-03-20 13:02:09 +02:00
|
|
|
static void serializeMoatHexes(JsonSerializeFormat & handler, const std::string & fieldName, std::vector<std::vector<BattleHex>> & moatHexes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
JsonArraySerializer outer = handler.enterArray(fieldName);
|
|
|
|
outer.syncSize(moatHexes, JsonNode::JsonType::DATA_VECTOR);
|
|
|
|
|
|
|
|
for(size_t outerIndex = 0; outerIndex < outer.size(); outerIndex++)
|
|
|
|
{
|
|
|
|
JsonArraySerializer inner = outer.enterArray(outerIndex);
|
|
|
|
inner.syncSize(moatHexes.at(outerIndex), JsonNode::JsonType::DATA_INTEGER);
|
|
|
|
|
|
|
|
for(size_t innerIndex = 0; innerIndex < inner.size(); innerIndex++)
|
|
|
|
inner.serializeInt(innerIndex, moatHexes.at(outerIndex).at(innerIndex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 00:49:40 +02:00
|
|
|
void Moat::serializeJsonEffect(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
handler.serializeBool("hidden", hidden);
|
|
|
|
handler.serializeBool("trap", trap);
|
|
|
|
handler.serializeBool("removeOnTrigger", removeOnTrigger);
|
|
|
|
handler.serializeBool("dispellable", dispellable);
|
2023-03-19 02:16:02 +02:00
|
|
|
handler.serializeInt("moatDamage", moatDamage);
|
2023-03-20 13:02:09 +02:00
|
|
|
serializeMoatHexes(handler, "moatHexes", moatHexes);
|
2023-03-19 02:16:02 +02:00
|
|
|
handler.serializeId("triggerAbility", triggerAbility, SpellID::NONE);
|
2023-03-16 00:49:40 +02:00
|
|
|
handler.serializeStruct("defender", sideOptions); //Moats are defender only
|
2023-03-29 16:23:12 +02:00
|
|
|
|
|
|
|
assert(!handler.saving);
|
|
|
|
{
|
|
|
|
auto guard = handler.enterStruct("bonus");
|
|
|
|
const JsonNode & data = handler.getCurrent();
|
|
|
|
|
|
|
|
for(const auto & p : data.Struct())
|
|
|
|
{
|
|
|
|
//TODO: support JsonSerializeFormat in Bonus
|
|
|
|
auto guard = handler.enterStruct(p.first);
|
|
|
|
const JsonNode & bonusNode = handler.getCurrent();
|
|
|
|
auto b = JsonUtils::parseBonus(bonusNode);
|
|
|
|
bonus.push_back(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Moat::convertBonus(const Mechanics * m, std::vector<Bonus> & converted) const
|
|
|
|
{
|
|
|
|
|
|
|
|
for(const auto & b : bonus)
|
|
|
|
{
|
|
|
|
Bonus nb(*b);
|
|
|
|
|
|
|
|
//Moat battlefield effect is always permanent
|
2023-05-01 00:20:01 +02:00
|
|
|
nb.duration = BonusDuration::ONE_BATTLE;
|
2023-03-29 16:23:12 +02:00
|
|
|
|
|
|
|
if(m->battle()->battleGetDefendedTown() && m->battle()->battleGetSiegeLevel() >= CGTownInstance::CITADEL)
|
|
|
|
{
|
2023-10-21 13:50:42 +02:00
|
|
|
nb.sid = BonusSourceID(m->battle()->battleGetDefendedTown()->town->buildings.at(BuildingID::CITADEL)->getUniqueTypeID());
|
2023-05-01 00:20:01 +02:00
|
|
|
nb.source = BonusSource::TOWN_STRUCTURE;
|
2023-03-29 16:23:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-21 13:50:42 +02:00
|
|
|
nb.sid = BonusSourceID(m->getSpellId()); //for all
|
2023-05-01 00:20:01 +02:00
|
|
|
nb.source = BonusSource::SPELL_EFFECT;//for all
|
2023-03-29 16:23:12 +02:00
|
|
|
}
|
|
|
|
std::set<BattleHex> flatMoatHexes;
|
|
|
|
|
|
|
|
for(const auto & moatPatch : moatHexes)
|
|
|
|
flatMoatHexes.insert(moatPatch.begin(), moatPatch.end());
|
|
|
|
|
|
|
|
nb.limiter = std::make_shared<UnitOnHexLimiter>(std::move(flatMoatHexes));
|
|
|
|
converted.push_back(nb);
|
|
|
|
}
|
2023-03-16 00:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Moat::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
|
|
|
assert(m->isMassive());
|
|
|
|
assert(m->battle()->battleGetDefendedTown());
|
|
|
|
if(m->isMassive() && m->battle()->battleGetSiegeLevel() >= CGTownInstance::CITADEL)
|
|
|
|
{
|
|
|
|
EffectTarget moat;
|
|
|
|
placeObstacles(server, m, moat);
|
2023-03-29 16:23:12 +02:00
|
|
|
|
|
|
|
std::vector<Bonus> converted;
|
|
|
|
convertBonus(m, converted);
|
|
|
|
for(auto & b : converted)
|
|
|
|
{
|
|
|
|
GiveBonus gb(GiveBonus::ETarget::BATTLE);
|
2023-11-06 18:27:16 +02:00
|
|
|
gb.id = m->battle()->getBattle()->getBattleID();
|
2023-03-29 16:23:12 +02:00
|
|
|
gb.bonus = b;
|
|
|
|
server->apply(&gb);
|
|
|
|
}
|
2023-03-16 00:49:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Moat::placeObstacles(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
|
|
|
assert(m->battle()->battleGetDefendedTown());
|
|
|
|
assert(m->casterSide == BattleSide::DEFENDER); // Moats are always cast by defender
|
|
|
|
|
|
|
|
BattleObstaclesChanged pack;
|
2023-09-08 15:35:43 +02:00
|
|
|
pack.battleID = m->battle()->getBattle()->getBattleID();
|
2023-03-16 00:49:40 +02:00
|
|
|
|
|
|
|
auto all = m->battle()->battleGetAllObstacles(BattlePerspective::ALL_KNOWING);
|
|
|
|
|
|
|
|
int obstacleIdToGive = 1;
|
|
|
|
for(auto & one : all)
|
|
|
|
if(one->uniqueID >= obstacleIdToGive)
|
|
|
|
obstacleIdToGive = one->uniqueID + 1;
|
|
|
|
|
2023-03-20 13:02:09 +02:00
|
|
|
for(const auto & destination : moatHexes) //Moat hexes can be different obstacles
|
2023-03-16 00:49:40 +02:00
|
|
|
{
|
|
|
|
SpellCreatedObstacle obstacle;
|
|
|
|
obstacle.uniqueID = obstacleIdToGive++;
|
2023-03-20 13:02:09 +02:00
|
|
|
obstacle.pos = destination.at(0);
|
2023-03-16 00:49:40 +02:00
|
|
|
obstacle.obstacleType = dispellable ? CObstacleInstance::SPELL_CREATED : CObstacleInstance::MOAT;
|
2023-03-30 23:49:51 +02:00
|
|
|
obstacle.ID = m->getSpellIndex();
|
2023-03-16 00:49:40 +02:00
|
|
|
|
|
|
|
obstacle.turnsRemaining = -1; //Moat cannot be expired
|
|
|
|
obstacle.casterSpellPower = m->getEffectPower();
|
|
|
|
obstacle.spellLevel = m->getEffectLevel(); //todo: level of indirect effect should be also configurable
|
|
|
|
obstacle.casterSide = BattleSide::DEFENDER; // Moats are always cast by defender
|
2023-03-19 02:16:02 +02:00
|
|
|
obstacle.minimalDamage = moatDamage; // Minimal moat damage
|
2023-03-16 00:49:40 +02:00
|
|
|
obstacle.hidden = hidden;
|
|
|
|
obstacle.passable = true; //Moats always passable
|
2023-03-30 23:49:51 +02:00
|
|
|
obstacle.trigger = triggerAbility;
|
2023-03-16 00:49:40 +02:00
|
|
|
obstacle.trap = trap;
|
|
|
|
obstacle.removeOnTrigger = removeOnTrigger;
|
|
|
|
obstacle.nativeVisible = false; //Moats is invisible for native terrain
|
2023-03-20 10:59:43 +02:00
|
|
|
obstacle.appearSound = sideOptions.appearSound; //For dispellable moats
|
|
|
|
obstacle.appearAnimation = sideOptions.appearAnimation; //For dispellable moats
|
2023-03-16 00:49:40 +02:00
|
|
|
obstacle.animation = sideOptions.animation;
|
2023-03-20 13:02:09 +02:00
|
|
|
obstacle.customSize.insert(obstacle.customSize.end(),destination.cbegin(), destination.cend());
|
2023-03-16 00:49:40 +02:00
|
|
|
obstacle.animationYOffset = sideOptions.offsetY;
|
|
|
|
pack.changes.emplace_back();
|
|
|
|
obstacle.toInfo(pack.changes.back());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!pack.changes.empty())
|
|
|
|
server->apply(&pack);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|