1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-28 07:07:45 +00:00
sapphire/src/world/Action/EffectBuilder.cpp

236 lines
7.9 KiB
C++
Raw Normal View History

2019-07-25 22:46:10 +10:00
#include "EffectBuilder.h"
#include "EffectResult.h"
2019-07-26 20:28:01 +10:00
#include <Actor/Player.h>
2019-07-25 22:46:10 +10:00
#include <Network/PacketWrappers/EffectPacket.h>
#include <Network/PacketWrappers/EffectPacket1.h>
2019-07-25 22:46:10 +10:00
#include <Territory/Territory.h>
#include <Util/Util.h>
#include <Util/UtilMath.h>
#include <Logging/Logger.h>
#include <Manager/TerritoryMgr.h>
2023-02-17 22:46:02 +01:00
#include <Manager/MgrUtil.h>
#include <Service.h>
2019-07-25 22:46:10 +10:00
#include <Manager/TaskMgr.h>
#include <Task/ActionIntegrityTask.h>
2019-07-25 22:46:10 +10:00
using namespace Sapphire;
using namespace Sapphire::World::Action;
2023-02-17 22:46:02 +01:00
using namespace Sapphire::World::Manager;
2019-07-25 22:46:10 +10:00
using namespace Sapphire::Network::Packets;
using namespace Sapphire::Network::Packets::WorldPackets::Server;
2019-07-25 22:46:10 +10:00
EffectBuilder::EffectBuilder( Entity::CharaPtr source, uint32_t actionId, uint16_t requestId ) :
2019-07-25 22:46:10 +10:00
m_sourceChara( std::move( source ) ),
2019-07-26 20:28:01 +10:00
m_actionId( actionId ),
m_requestId( requestId )
2019-07-25 22:46:10 +10:00
{
}
uint64_t EffectBuilder::getResultDelayMs()
2019-07-25 22:46:10 +10:00
{
// todo: actually figure this retarded shit out
return Common::Util::getTimeMs() + 850;
2019-07-25 22:46:10 +10:00
}
void EffectBuilder::addResultToActor( Entity::CharaPtr& chara, EffectResultPtr result )
2019-07-25 22:46:10 +10:00
{
auto it = m_actorEffectsMap.find( chara->getId() );
if( it == m_actorEffectsMap.end() )
2019-07-25 22:46:10 +10:00
{
2020-01-06 17:52:45 +09:00
// create a new one
auto resultList = std::vector< EffectResultPtr >();
2019-07-25 22:46:10 +10:00
resultList.push_back( std::move( result ) );
2019-07-25 22:46:10 +10:00
m_actorEffectsMap[ chara->getId() ] = resultList;
2020-01-06 17:52:45 +09:00
return;
2019-07-25 22:46:10 +10:00
}
it->second.push_back( std::move( result ) );
2019-07-25 22:46:10 +10:00
}
2020-01-05 20:49:50 +09:00
void EffectBuilder::heal( Entity::CharaPtr& effectTarget, Entity::CharaPtr& healingTarget, uint32_t amount, Common::ActionHitSeverityType severity, Common::ActionEffectResultFlag flag )
2019-07-25 22:46:10 +10:00
{
2020-01-05 20:49:50 +09:00
EffectResultPtr nextResult = make_EffectResult( healingTarget, getResultDelayMs() );
nextResult->heal( amount, severity, flag );
addResultToActor( effectTarget, nextResult );
2020-01-05 17:09:27 +09:00
}
2020-01-05 20:49:50 +09:00
void EffectBuilder::restoreMP( Entity::CharaPtr& target, Entity::CharaPtr& restoringTarget, uint32_t amount, Common::ActionEffectResultFlag flag )
2020-01-05 17:09:27 +09:00
{
2020-01-05 20:49:50 +09:00
EffectResultPtr nextResult = make_EffectResult( restoringTarget, getResultDelayMs() ); // restore mp source actor
nextResult->restoreMP( amount, flag );
addResultToActor( target, nextResult );
2020-01-05 17:09:27 +09:00
}
2020-01-05 20:49:50 +09:00
void EffectBuilder::damage( Entity::CharaPtr& effectTarget, Entity::CharaPtr& damagingTarget, uint32_t amount, Common::ActionHitSeverityType severity, Common::ActionEffectResultFlag flag )
2020-01-05 17:09:27 +09:00
{
2020-01-05 20:49:50 +09:00
EffectResultPtr nextResult = make_EffectResult( damagingTarget, getResultDelayMs() );
nextResult->damage( amount, severity, flag );
addResultToActor( damagingTarget, nextResult );
2020-01-05 17:09:27 +09:00
}
void EffectBuilder::startCombo( Entity::CharaPtr& target, uint16_t actionId )
{
EffectResultPtr nextResult = make_EffectResult( target, 0 );
nextResult->startCombo( actionId );
addResultToActor( target, nextResult );
2020-01-05 17:09:27 +09:00
}
2020-01-05 20:49:50 +09:00
void EffectBuilder::comboSucceed( Entity::CharaPtr& target )
2020-01-05 17:09:27 +09:00
{
EffectResultPtr nextResult = make_EffectResult( target, 0 );
2020-01-05 20:49:50 +09:00
nextResult->comboSucceed();
addResultToActor( target, nextResult );
2019-07-25 22:46:10 +10:00
}
2020-01-06 19:25:01 +09:00
void EffectBuilder::applyStatusEffect( Entity::CharaPtr& target, uint16_t statusId, uint8_t param )
{
EffectResultPtr nextResult = make_EffectResult( target, 0 );
nextResult->applyStatusEffect( statusId, param );
addResultToActor( target, nextResult );
2020-01-06 19:25:01 +09:00
}
2020-01-23 22:36:01 +09:00
void EffectBuilder::mount( Entity::CharaPtr& target, uint16_t mountId )
{
EffectResultPtr nextResult = make_EffectResult( target, getResultDelayMs() );
nextResult->mount( mountId );
addResultToActor( target, nextResult );
2020-01-23 22:36:01 +09:00
}
void EffectBuilder::buildAndSendPackets( const std::vector< Entity::CharaPtr >& targetList )
2019-07-25 22:46:10 +10:00
{
2019-07-25 23:21:42 +10:00
Logger::debug( "EffectBuilder result: " );
Logger::debug( "Targets afflicted: {}", targetList.size() );
2020-01-05 17:09:27 +09:00
2020-01-06 04:29:45 +09:00
do // we want to send at least one packet even nothing is hit so other players can see
2020-01-05 17:09:27 +09:00
{
auto packet = buildNextEffectPacket( targetList );
2023-02-17 22:46:02 +01:00
server().queueForPlayers( m_sourceChara->getInRangePlayerIds( true ), packet );
2020-01-05 17:09:27 +09:00
}
while( !m_actorEffectsMap.empty() );
2020-01-05 17:09:27 +09:00
}
std::shared_ptr< FFXIVPacketBase > EffectBuilder::buildNextEffectPacket( const std::vector< Entity::CharaPtr >& targetList )
2020-01-05 17:09:27 +09:00
{
auto remainingTargetCount = targetList.size();
auto& teriMgr = Common::Service< Sapphire::World::Manager::TerritoryMgr >::ref();
auto zone = teriMgr.getTerritoryByGuId( m_sourceChara->getTerritoryId() );
auto resultId = zone->getNextEffectResultId();
2020-01-05 17:09:27 +09:00
if( remainingTargetCount > 1 ) // use AoeEffect packets
{
auto effectPacket = std::make_shared< EffectPacket >( m_sourceChara->getId(), m_actionId );
effectPacket->setRotation( Common::Util::floatToUInt16Rot( m_sourceChara->getRot() ) );
effectPacket->setRequestId( m_requestId );
2023-02-20 11:24:02 +01:00
effectPacket->setResultId( resultId );
effectPacket->setTargetActor( targetList[ 0 ]->getId() );
2020-01-05 17:09:27 +09:00
uint8_t targetIndex = 0;
for( auto it = m_actorEffectsMap.begin(); it != m_actorEffectsMap.end(); )
2020-01-05 17:09:27 +09:00
{
// get all effect results for an actor
auto actorResultList = it->second;
2020-01-05 17:09:27 +09:00
for( auto i = 0; i < actorResultList.size(); ++i )
2020-01-05 17:09:27 +09:00
{
auto result = actorResultList.data()[ i ];
auto effect = result->getCalcResultParam();
// if effect result is a source/caster effect
if( result->getTarget() == m_sourceChara )
{
effectPacket->addSourceEffect( effect );
}
else
{
effectPacket->addTargetEffect( effect, result->getTarget()->getId() );
auto& taskMgr = Common::Service< World::Manager::TaskMgr >::ref();
taskMgr.queueTask( Sapphire::World::makeActionIntegrityTask( resultId, result->getTarget(), 1000 ) );
}
zone->addEffectResult( std::move( result ) );
2020-01-05 17:09:27 +09:00
}
actorResultList.clear();
it = m_actorEffectsMap.erase( it );
2020-01-05 17:09:27 +09:00
targetIndex++;
if( targetIndex == 15 )
2020-01-05 17:09:27 +09:00
break;
}
return effectPacket;
2020-01-05 17:09:27 +09:00
}
else if( remainingTargetCount == 1 ) // use Effect for single target
2019-07-25 22:46:10 +10:00
{
Logger::debug( " - id: {}", targetList[0]->getId() );
Logger::debug( "------------------------------------------" );
2019-07-26 21:58:13 +10:00
auto effectPacket = std::make_shared< EffectPacket1 >( m_sourceChara->getId(), targetList[ 0 ]->getId(), m_actionId );
2019-07-25 22:46:10 +10:00
effectPacket->setRotation( Common::Util::floatToUInt16Rot( m_sourceChara->getRot() ) );
effectPacket->setRequestId( m_requestId );
2023-02-20 11:24:02 +01:00
effectPacket->setResultId( resultId );
2019-07-25 22:46:10 +10:00
for( auto it = m_actorEffectsMap.begin(); it != m_actorEffectsMap.end(); )
2020-01-05 17:09:27 +09:00
{
// get all effect results for an actor
auto actorResultList = it->second;
2019-07-25 22:46:10 +10:00
for( auto i = 0; i < actorResultList.size(); ++i )
{
auto result = actorResultList.data()[ i ];
auto effect = result->getCalcResultParam();
// if effect result is a source/caster effect
if( result->getTarget() == m_sourceChara )
{
effectPacket->addSourceEffect( effect );
}
else
{
effectPacket->addTargetEffect( effect );
auto& taskMgr = Common::Service< World::Manager::TaskMgr >::ref();
taskMgr.queueTask( Sapphire::World::makeActionIntegrityTask( resultId, result->getTarget(), 1000 ) );
}
zone->addEffectResult( std::move( result ) );
}
2019-07-25 22:46:10 +10:00
actorResultList.clear();
it = m_actorEffectsMap.erase( it );
}
m_actorEffectsMap.clear();
2020-01-06 04:29:45 +09:00
return effectPacket;
}
else // nothing is hit, this only happens when using aoe and AoeEffect8 is used on retail
{
auto effectPacket = makeZonePacket< FFXIVIpcActionResult1 >( m_sourceChara->getId() );
effectPacket->data().ActionKey = m_actionId;
effectPacket->data().Action = static_cast< uint16_t >( m_actionId );
effectPacket->data().Target = m_sourceChara->getId();
effectPacket->data().MainTarget = static_cast< uint64_t >( m_sourceChara->getId() );
effectPacket->data().DirTarget = Common::Util::floatToUInt16Rot( m_sourceChara->getRot() );
effectPacket->data().Flag = Common::ActionEffectDisplayType::HideActionName;
effectPacket->data().RequestId = m_requestId;
effectPacket->data().ResultId = resultId;
2020-01-06 04:29:45 +09:00
m_actorEffectsMap.clear();
2020-01-05 17:09:27 +09:00
return effectPacket;
}
2019-07-25 22:46:10 +10:00
}