1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-28 15:17:46 +00:00
sapphire/src/world/Actor/BNpc.cpp

1066 lines
27 KiB
C++
Raw Normal View History

#include <Util/Util.h>
#include <Util/UtilMath.h>
#include <Network/PacketContainer.h>
#include <Exd/ExdData.h>
#include <utility>
#include <Network/CommonActorControl.h>
#include <Network/PacketWrappers/EffectPacket1.h>
2019-01-23 22:37:55 +01:00
#include <Logging/Logger.h>
#include "Forwards.h"
#include "Action/Action.h"
2019-07-21 22:33:33 +10:00
#include "Territory/Territory.h"
#include "Network/GameConnection.h"
#include "Network/PacketWrappers/ActorControlPacket.h"
#include "Network/PacketWrappers/ActorControlSelfPacket.h"
#include "Network/PacketWrappers/ActorControlTargetPacket.h"
#include "Network/PacketWrappers/NpcSpawnPacket.h"
#include "Network/PacketWrappers/MoveActorPacket.h"
#include "Network/Util/PacketUtil.h"
2023-03-06 10:12:29 +01:00
#include "Navi/NaviProvider.h"
#include "Math/CalcStats.h"
#include "WorldServer.h"
#include "Session.h"
#include "Chara.h"
#include "BNpc.h"
#include "Common.h"
#include <Manager/TerritoryMgr.h>
#include <Manager/RNGMgr.h>
#include <Manager/PlayerMgr.h>
2022-01-20 22:42:26 +01:00
#include <Manager/TaskMgr.h>
#include <Manager/MgrUtil.h>
#include <Script/ScriptMgr.h>
2022-01-20 22:42:26 +01:00
#include <Task/RemoveBNpcTask.h>
2022-01-21 00:42:40 +01:00
#include <Task/FadeBNpcTask.h>
2022-01-24 02:39:21 -06:00
#include <Task/DelayedEmnityTask.h>
#include <Task/ActionIntegrityTask.h>
2020-03-01 01:00:57 +11:00
#include <Service.h>
using namespace Sapphire;
using namespace Sapphire::Common;
using namespace Sapphire::Entity;
using namespace Sapphire::Network::Packets;
using namespace Sapphire::Network::Packets::WorldPackets::Server;
using namespace Sapphire::Network::ActorControl;
using namespace Sapphire::World::Manager;
BNpc::BNpc() : Npc( ObjKind::BattleNpc )
{
}
BNpc::BNpc( uint32_t id, std::shared_ptr< Common::BNPCInstanceObject > pInfo, const Territory& zone ) : Npc( ObjKind::BattleNpc )
{
m_id = id;
m_pInfo = pInfo;
m_layoutId = pInfo->instanceId;
m_aggressionMode = pInfo->ActiveType;
m_displayFlags = 0;
m_weaponMain = 0;
m_weaponSub = 0;
m_pose = 0;
m_bNpcNameId = pInfo->NameId;
m_bNpcBaseId = pInfo->BaseId;
m_pos.x = pInfo->x;
m_pos.y = pInfo->y;
m_pos.z = pInfo->z;
m_rot = pInfo->rotation;
m_level = pInfo->Level <= 0 ? 1 : pInfo->Level;
m_invincibilityType = InvincibilityNone;
m_currentStance = Common::Stance::Passive;
m_boundInstanceId = pInfo->BoundInstanceID;
m_flags = 0;
m_rank = pInfo->BNPCRankId;
if( pInfo->WanderingRange == 0 || pInfo->BoundInstanceID != 0 )
setFlag( Immobile );
// Striking Dummy
if( pInfo->NameId == 541 )
m_invincibilityType = Common::InvincibilityRefill;
auto& exdData = Common::Service< Data::ExdData >::ref();
2022-01-27 21:24:54 +01:00
auto bNpcBaseData = exdData.getRow< Excel::BNpcBase >( m_bNpcBaseId );
if( !bNpcBaseData )
{
Logger::debug( "BNpcBase#{0} not found in exd data!", m_bNpcBaseId );
return;
}
m_modelChara = bNpcBaseData->data().Model;
m_enemyType = bNpcBaseData->data().Battalion;
m_class = ClassJob::Gladiator;
2023-01-17 08:28:06 +01:00
m_territoryTypeId = zone.getTerritoryTypeId();
m_territoryId = zone.getGuId();
m_spawnPos = m_pos;
m_timeOfDeath = 0;
m_targetId = Common::INVALID_GAME_OBJECT_ID64;
m_maxHp = 500;
m_maxMp = 200;
m_hp = m_maxHp;
m_mp = 200;
if( m_level <= BnpcBaseHp.size() )
m_maxHp = BnpcBaseHp[ m_level - 1 ];
m_state = BNpcState::Idle;
m_status = ActorStatus::Idle;
memset( m_customize, 0, sizeof( m_customize ) );
memset( m_modelEquip, 0, sizeof( m_modelEquip ) );
m_radius = bNpcBaseData->data().Scale;
if( bNpcBaseData->data().Customize != 0 )
{
2022-01-27 21:24:54 +01:00
auto bnpcCustom = exdData.getRow< Excel::BNpcCustomize >( bNpcBaseData->data().Customize );
if( bnpcCustom )
{
memcpy( m_customize, reinterpret_cast< char* >( &bnpcCustom->data() ), sizeof( m_customize ) );
}
}
if( bNpcBaseData->data().Equipment != 0 )
{
2022-01-27 21:24:54 +01:00
auto bnpcEquip = exdData.getRow< Excel::NpcEquip >( bNpcBaseData->data().Equipment );
if( bnpcEquip )
{
m_weaponMain = bnpcEquip->data().WeaponModel;
m_weaponSub = bnpcEquip->data().SubWeaponModel;
memcpy( m_modelEquip, reinterpret_cast< char* >( bnpcEquip->data().Equip ), sizeof( m_modelEquip ) );
}
}
2022-01-27 21:24:54 +01:00
auto modelChara = exdData.getRow< Excel::ModelChara >( bNpcBaseData->data().Model );
if( modelChara )
{
2022-01-27 21:24:54 +01:00
auto modelSkeleton = exdData.getRow< Excel::ModelSkeleton >( modelChara->data().SkeletonId );
if( modelSkeleton )
{
m_radius *= modelSkeleton->data().Radius;
}
}
// todo: is this actually good?
m_naviTargetReachedDistance = m_radius * 2;
calculateStats();
if( m_bnpcType == BNpcType::Friendly )
m_maxHp *= 5;
}
BNpc::BNpc( uint32_t id, std::shared_ptr< Common::BNPCInstanceObject > pInfo, const Territory& zone, uint32_t hp, Common::BNpcType type ) :
Npc( ObjKind::BattleNpc )
{
m_id = id;
m_pInfo = pInfo;
m_layoutId = pInfo->instanceId;
m_aggressionMode = pInfo->ActiveType;
m_displayFlags = 0;
m_weaponMain = 0;
m_weaponSub = 0;
m_pose = 0;
m_bNpcNameId = pInfo->NameId;
m_bNpcBaseId = pInfo->BaseId;
m_pos.x = pInfo->x;
m_pos.y = pInfo->y;
m_pos.z = pInfo->z;
m_rot = pInfo->rotation;
m_level = pInfo->Level <= 0 ? 1 : pInfo->Level;
m_invincibilityType = InvincibilityNone;
m_currentStance = Common::Stance::Passive;
m_boundInstanceId = pInfo->BoundInstanceID;
2019-04-22 00:16:39 +02:00
m_flags = 0;
m_rank = pInfo->BNPCRankId;
2023-01-17 08:28:06 +01:00
m_territoryTypeId = zone.getTerritoryTypeId();
m_territoryId = zone.getGuId();
if( pInfo->WanderingRange == 0 || pInfo->BoundInstanceID != 0 )
setFlag( Immobile );
auto& exdData = Common::Service< Data::ExdData >::ref();
2022-01-27 21:24:54 +01:00
auto bNpcBaseData = exdData.getRow< Excel::BNpcBase >( m_bNpcBaseId );
if( !bNpcBaseData )
{
Logger::debug( "BNpcBase#{0} not found in exd data!", m_bNpcBaseId );
return;
}
m_modelChara = bNpcBaseData->data().Model;
m_enemyType = bNpcBaseData->data().Battalion;
m_class = ClassJob::Gladiator;
m_spawnPos = m_pos;
2019-01-23 22:37:55 +01:00
m_timeOfDeath = 0;
m_targetId = Common::INVALID_GAME_OBJECT_ID64;
2019-01-23 22:37:55 +01:00
m_maxHp = hp;
m_maxMp = 200;
m_hp = m_maxHp;
m_mp = 200;
m_state = BNpcState::Idle;
2019-01-20 23:49:43 +01:00
m_status = ActorStatus::Idle;
m_bnpcType = type;
memset( m_customize, 0, sizeof( m_customize ) );
memset( m_modelEquip, 0, sizeof( m_modelEquip ) );
2019-01-31 22:49:04 +11:00
m_radius = bNpcBaseData->data().Scale;
if( bNpcBaseData->data().Customize != 0 )
{
2022-01-27 21:24:54 +01:00
auto bnpcCustom = exdData.getRow< Excel::BNpcCustomize >( bNpcBaseData->data().Customize );
if( bnpcCustom )
{
memcpy( m_customize, reinterpret_cast< char* >( &bnpcCustom->data() ), sizeof( m_customize ) );
}
}
2019-01-31 22:49:04 +11:00
if( bNpcBaseData->data().Equipment != 0 )
{
2022-01-27 21:24:54 +01:00
auto bnpcEquip = exdData.getRow< Excel::NpcEquip >( bNpcBaseData->data().Equipment );
if( bnpcEquip )
{
m_weaponMain = bnpcEquip->data().WeaponModel;
m_weaponSub = bnpcEquip->data().SubWeaponModel;
memcpy( m_modelEquip, reinterpret_cast< char* >( bnpcEquip->data().Equip ), sizeof( m_modelEquip ) );
}
}
2022-01-27 21:24:54 +01:00
auto modelChara = exdData.getRow< Excel::ModelChara >( bNpcBaseData->data().Model );
2019-04-19 23:01:27 +10:00
if( modelChara )
{
2022-01-27 21:24:54 +01:00
auto modelSkeleton = exdData.getRow< Excel::ModelSkeleton >( modelChara->data().ModelType );
2019-04-19 23:01:27 +10:00
if( modelSkeleton )
m_radius *= modelSkeleton->data().Radius;
2019-04-19 23:01:27 +10:00
}
2019-01-31 22:49:04 +11:00
// todo: is this actually good?
m_naviTargetReachedDistance = m_radius;
calculateStats();
}
BNpc::~BNpc() = default;
2019-01-31 22:49:04 +11:00
uint8_t BNpc::getAggressionMode() const
{
return m_aggressionMode;
}
float BNpc::getNaviTargetReachedDistance() const
2019-01-31 22:49:04 +11:00
{
return m_naviTargetReachedDistance;
}
uint8_t BNpc::getEnemyType() const
{
return m_enemyType;
}
uint64_t BNpc::getWeaponMain() const
{
return m_weaponMain;
}
uint64_t BNpc::getWeaponSub() const
{
return m_weaponSub;
}
uint16_t BNpc::getModelChara() const
{
return m_modelChara;
}
uint8_t BNpc::getLevel() const
{
return m_level;
}
uint32_t BNpc::getBNpcBaseId() const
{
return m_bNpcBaseId;
}
uint32_t BNpc::getBNpcNameId() const
{
return m_bNpcNameId;
}
void BNpc::spawn( PlayerPtr pTarget )
{
m_lastRoamTargetReached = Util::getTimeSeconds();
auto& server = Common::Service< World::WorldServer >::ref();
server.queueForPlayer( pTarget->getCharacterId(), std::make_shared< NpcSpawnPacket >( *this, *pTarget ) );
2018-09-26 03:32:43 -04:00
}
void BNpc::despawn( PlayerPtr pTarget )
{
pTarget->freePlayerSpawnId( getId() );
Network::Util::Packet::sendActorControl( *pTarget, WarpStart, 4, getId(), 1 );
}
BNpcState BNpc::getState() const
{
return m_state;
}
void BNpc::setState( BNpcState state )
{
m_state = state;
}
bool BNpc::moveTo( const FFXIVARR_POSITION3& pos )
{
auto& teriMgr = Common::Service< World::Manager::TerritoryMgr >::ref();
auto pZone = teriMgr.getTerritoryByGuId( getTerritoryId() );
2019-01-23 17:07:40 +01:00
auto pNaviProvider = pZone->getNaviProvider();
if( !pNaviProvider )
2019-01-21 15:15:28 +01:00
{
Logger::error( "No NaviProvider for zone#{0} - {1}", pZone->getGuId(), pZone->getInternalName() );
return false;
}
2019-01-23 19:23:49 +01:00
2019-04-19 00:39:42 +02:00
auto pos1 = pNaviProvider->getMovePos( *this );
auto distance = Util::distance( pos1, pos );
2019-04-19 00:39:42 +02:00
if( distance < getNaviTargetReachedDistance() )
{
// Reached destination
face( pos );
2019-04-19 14:04:38 +02:00
setPos( pos1 );
sendPositionUpdate();
pNaviProvider->updateAgentPosition( *this );
return true;
}
pZone->updateActorPosition( *this );
face( pos );
if( distance > 2.0f )
face( { ( pos.x - pos1.x ) + pos.x, 1.0f, ( pos.z - pos1.z ) + pos.z } );
else
face( pos );
setPos( pos1 );
sendPositionUpdate();
return false;
}
bool BNpc::moveTo( const Chara& targetChara )
2019-04-20 00:11:00 +02:00
{
auto& teriMgr = Common::Service< World::Manager::TerritoryMgr >::ref();
auto pZone = teriMgr.getTerritoryByGuId( getTerritoryId() );
auto pNaviProvider = pZone->getNaviProvider();
2019-04-20 00:11:00 +02:00
if( !pNaviProvider )
{
Logger::error( "No NaviProvider for zone#{0} - {1}", pZone->getGuId(), pZone->getInternalName() );
2019-04-20 00:11:00 +02:00
return false;
}
auto pos1 = pNaviProvider->getMovePos( *this );
auto distance = Util::distance( pos1, targetChara.getPos() );
2019-04-20 00:11:00 +02:00
if( distance <= ( getNaviTargetReachedDistance() + targetChara.getRadius() ) )
2019-04-20 00:11:00 +02:00
{
// Reached destination
face( targetChara.getPos() );
2019-04-20 00:11:00 +02:00
setPos( pos1 );
sendPositionUpdate();
pNaviProvider->resetMoveTarget( *this );
2019-04-20 00:11:00 +02:00
pNaviProvider->updateAgentPosition( *this );
return true;
}
pZone->updateActorPosition( *this );
if( distance > 2.0f )
face( { ( pos1.x - getPos().x ) + pos1.x, 1.0f, ( pos1.z - getPos().z ) + pos1.z } );
else
face( targetChara.getPos() );
2019-04-20 00:11:00 +02:00
setPos( pos1 );
sendPositionUpdate();
return false;
}
void BNpc::sendPositionUpdate()
{
uint8_t animationType = 2;
if( m_state == BNpcState::Combat || m_state == BNpcState::Retreat )
animationType = 0;
auto movePacket = std::make_shared< MoveActorPacket >( *getAsChara(), 0x3A, animationType, 0, 0x5A / 4 );
server().queueForPlayers( getInRangePlayerIds(), movePacket );
}
void BNpc::hateListClear()
{
for( auto& listEntry : m_hateList )
{
if( isInRangeSet( listEntry->m_pChara ) )
deaggro( listEntry->m_pChara );
}
m_hateList.clear();
}
uint32_t BNpc::hateListGetValue( const Sapphire::Entity::CharaPtr& pChara )
{
for( const auto& listEntry : m_hateList )
{
if( listEntry->m_pChara == pChara )
{
return listEntry->m_hateAmount;
}
}
return 0;
}
uint32_t BNpc::hateListGetHighestValue()
{
auto it = m_hateList.begin();
uint32_t maxHate = 0;
std::shared_ptr< HateListEntry > entry;
for( ; it != m_hateList.end(); ++it )
{
if( ( *it )->m_hateAmount > maxHate )
{
maxHate = ( *it )->m_hateAmount;
entry = *it;
}
}
if( entry && maxHate != 0 )
return entry->m_hateAmount;
return 0;
}
CharaPtr BNpc::hateListGetHighest()
{
auto it = m_hateList.begin();
uint32_t maxHate = 0;
std::shared_ptr< HateListEntry > entry;
for( ; it != m_hateList.end(); ++it )
{
if( ( *it )->m_hateAmount > maxHate )
{
maxHate = ( *it )->m_hateAmount;
entry = *it;
}
}
if( entry && maxHate != 0 )
return entry->m_pChara;
return nullptr;
}
void BNpc::hateListAdd( const CharaPtr& pChara, int32_t hateAmount )
{
auto hateEntry = std::make_shared< HateListEntry >();
hateEntry->m_hateAmount = static_cast< uint32_t >( hateAmount );
hateEntry->m_pChara = pChara;
m_hateList.insert( hateEntry );
if( pChara->isPlayer() )
{
auto pPlayer = pChara->getAsPlayer();
pPlayer->hateListAdd( *this );
}
}
void BNpc::hateListAddDelayed( const CharaPtr& pChara, int32_t hateAmount )
2022-01-24 02:39:21 -06:00
{
auto& taskMgr = Common::Service< World::Manager::TaskMgr >::ref();
auto delayedEmnityTask = std::make_shared< World::DelayedEmnityTask >( 5000, getAsBNpc(), pChara, hateAmount );
2022-01-24 02:39:21 -06:00
taskMgr.queueTask( delayedEmnityTask );
}
void BNpc::hateListUpdate( const CharaPtr& pChara, int32_t hateAmount )
{
bool hasEntry = false;
2022-01-19 17:55:29 +01:00
for( const auto& listEntry : m_hateList )
{
if( listEntry->m_pChara == pChara )
{
listEntry->m_hateAmount += static_cast< uint32_t >( hateAmount );
hasEntry = true;
break;
}
}
if( !hasEntry )
{
auto hateEntry = std::make_shared< HateListEntry >();
hateEntry->m_hateAmount = static_cast< uint32_t >( hateAmount );
hateEntry->m_pChara = pChara;
m_hateList.insert( hateEntry );
}
for( const auto& listEntry : m_hateList )
{
// update entire hatelist for all players who are on aggro with this bnpc
if( pChara->isPlayer() )
{
auto pPlayer = pChara->getAsPlayer();
Network::Util::Packet::sendHateList( *pPlayer );
}
}
}
void BNpc::hateListRemove( const CharaPtr& pChara )
{
2022-01-19 17:55:29 +01:00
for( const auto& listEntry : m_hateList )
{
if( listEntry->m_pChara == pChara )
{
m_hateList.erase( listEntry );
if( pChara->isPlayer() )
{
PlayerPtr tmpPlayer = pChara->getAsPlayer();
tmpPlayer->onMobDeaggro( *this );
}
return;
}
}
}
uint32_t BNpc::getTriggerOwnerId() const
2022-01-18 08:03:49 +01:00
{
return m_triggerOwnerId;
}
void BNpc::setTriggerOwnerId( uint32_t triggerOwnerId )
2022-01-18 08:03:49 +01:00
{
m_triggerOwnerId = triggerOwnerId;
}
bool BNpc::hateListHasActor( const Sapphire::Entity::CharaPtr& pChara )
{
2022-01-21 08:41:48 +01:00
return std::any_of( m_hateList.begin(), m_hateList.end(),
[ pChara ]( const auto& entry ) { return entry->m_pChara == pChara; } );
}
void BNpc::aggro( const Sapphire::Entity::CharaPtr& pChara )
{
2020-03-01 01:00:57 +11:00
auto& pRNGMgr = Common::Service< World::Manager::RNGMgr >::ref();
auto variation = static_cast< uint32_t >( pRNGMgr.getRandGenerator< float >( 500, 1000 ).next() );
m_lastAttack = Util::getTimeMs() + variation;
setStance( Stance::Active );
m_state = BNpcState::Combat;
Network::Util::Packet::sendActorControl( getInRangePlayerIds(), getId(), SetBattle, 1 );
changeTarget( pChara->getId() );
if( pChara->isPlayer() )
{
PlayerPtr tmpPlayer = pChara->getAsPlayer();
tmpPlayer->onMobAggro( *getAsBNpc() );
}
}
void BNpc::deaggro( const CharaPtr& pChara )
{
if( !hateListHasActor( pChara ) )
hateListRemove( pChara );
if( pChara->isPlayer() )
{
PlayerPtr tmpPlayer = pChara->getAsPlayer();
Network::Util::Packet::sendActorControl( getInRangePlayerIds(), getId(), ToggleWeapon, 0, 1, 1 );
Network::Util::Packet::sendActorControl( getInRangePlayerIds(), getId(), SetBattle );
tmpPlayer->onMobDeaggro( *this );
if( getTriggerOwnerId() == pChara->getId() )
{
auto& scriptMgr = Common::Service< Scripting::ScriptMgr >::ref();
auto bnpc = *getAsBNpc();
scriptMgr.onTriggerOwnerDeaggro( *tmpPlayer, bnpc );
}
}
}
void BNpc::onTick()
{
Chara::onTick();
if( m_state == BNpcState::Retreat )
{
2023-02-25 15:31:57 +01:00
restHp();
}
}
void BNpc::update( uint64_t tickCount )
{
auto& teriMgr = Common::Service< World::Manager::TerritoryMgr >::ref();
auto pZone = teriMgr.getTerritoryByGuId( getTerritoryId() );
2019-01-23 21:10:53 +01:00
const uint8_t maxDistanceToOrigin = 40;
const uint32_t roamTick = 20;
auto pNaviProvider = pZone->getNaviProvider();
if( !pNaviProvider )
return;
switch( m_state )
{
2019-01-23 22:37:55 +01:00
case BNpcState::Dead:
case BNpcState::JustDied:
2019-01-21 02:42:47 +01:00
return;
case BNpcState::Retreat:
{
setInvincibilityType( InvincibilityType::InvincibilityIgnoreDamage );
2019-04-19 14:50:00 +02:00
if( pNaviProvider )
pNaviProvider->setMoveTarget( *this, m_spawnPos );
if( moveTo( m_spawnPos ) )
{
setInvincibilityType( InvincibilityType::InvincibilityNone );
// retail doesn't seem to roam straight after retreating
// todo: perhaps requires more investigation?
m_lastRoamTargetReached = Util::getTimeSeconds();
// resetHp
setHp( getMaxHp() );
m_state = BNpcState::Idle;
setOwner( nullptr );
}
}
break;
case BNpcState::Roaming:
{
2019-04-19 00:39:42 +02:00
if( pNaviProvider )
2019-04-20 00:11:00 +02:00
pNaviProvider->setMoveTarget( *this, m_roamPos );
2019-04-19 00:39:42 +02:00
if( moveTo( m_roamPos ) )
{
m_lastRoamTargetReached = Util::getTimeSeconds();
m_state = BNpcState::Idle;
}
checkAggro();
}
break;
case BNpcState::Idle:
{
auto pHatedActor = hateListGetHighest();
if( pHatedActor )
aggro( pHatedActor );
2019-04-19 14:19:14 +02:00
if( pNaviProvider->syncPosToChara( *this ) )
sendPositionUpdate();
if( !hasFlag( Immobile ) && ( Util::getTimeSeconds() - m_lastRoamTargetReached > roamTick ) )
{
if( !pNaviProvider )
{
m_lastRoamTargetReached = Util::getTimeSeconds();
break;
}
if( m_pInfo->WanderingRange != 0 && getEnemyType() != 0 )
{
m_roamPos = pNaviProvider->findRandomPositionInCircle( m_spawnPos, m_pInfo->WanderingRange );
}
else
{
m_roamPos = m_spawnPos;
}
m_state = BNpcState::Roaming;
}
checkAggro();
2022-02-16 15:11:10 -03:00
break;
}
case BNpcState::Combat:
{
auto pHatedActor = hateListGetHighest();
if( !pHatedActor )
return;
pNaviProvider->updateAgentParameters( *this );
auto distanceOrig = Util::distance( getPos().x, getPos().y, getPos().z, m_spawnPos.x, m_spawnPos.y, m_spawnPos.z );
if( pHatedActor && !pHatedActor->isAlive() )
{
hateListRemove( pHatedActor );
pHatedActor = hateListGetHighest();
2019-01-21 02:42:47 +01:00
}
if( pHatedActor )
{
auto distance = Util::distance( getPos().x, getPos().y, getPos().z,
2019-04-20 15:42:48 +02:00
pHatedActor->getPos().x, pHatedActor->getPos().y, pHatedActor->getPos().z );
if( !hasFlag( NoDeaggro ) && ( ( distanceOrig > maxDistanceToOrigin ) || distance > 30.0f ) )
{
hateListClear();
changeTarget( INVALID_GAME_OBJECT_ID64 );
setStance( Stance::Passive );
setOwner( nullptr );
m_state = BNpcState::Retreat;
break;
}
if( distance > ( getNaviTargetReachedDistance() + pHatedActor->getRadius() ) )
{
2019-04-20 00:11:00 +02:00
if( hasFlag( Immobile ) )
break;
2019-04-19 14:50:00 +02:00
if( pNaviProvider )
pNaviProvider->setMoveTarget( *this, pHatedActor->getPos() );
2019-04-22 00:16:39 +02:00
2019-04-20 00:11:00 +02:00
moveTo( *pHatedActor );
}
2019-04-20 23:15:58 +02:00
if( pNaviProvider->syncPosToChara( *this ) )
sendPositionUpdate();
if( distance < ( getNaviTargetReachedDistance() + pHatedActor->getRadius() ) )
{
if( !hasFlag( TurningDisabled ) && face( pHatedActor->getPos() ) )
sendPositionUpdate();
// in combat range. ATTACK!
autoAttack( pHatedActor );
}
}
2019-01-21 02:42:47 +01:00
else
{
changeTarget( INVALID_GAME_OBJECT_ID64 );
setStance( Stance::Passive );
//setOwner( nullptr );
m_state = BNpcState::Retreat;
pNaviProvider->updateAgentParameters( *this );
}
}
2022-02-16 15:11:10 -03:00
break;
}
Chara::update( tickCount );
}
void BNpc::restHp()
{
2023-02-25 15:31:57 +01:00
if( m_hp < getMaxHp() )
{
2022-01-21 08:41:48 +01:00
auto addHp = static_cast< uint32_t >( getMaxHp() * 0.1f + 1 );
2023-02-25 15:31:57 +01:00
if( m_hp + addHp < getMaxHp() )
m_hp += addHp;
else
2023-02-25 15:31:57 +01:00
m_hp = getMaxHp();
}
2023-02-25 15:31:57 +01:00
sendHudParam();
}
void BNpc::onActionHostile( CharaPtr pSource )
{
if( !hateListGetHighest() )
aggro( pSource );
hateListUpdate( pSource, 1 );
if( !m_pOwner )
setOwner( pSource );
}
void BNpc::onDeath()
{
2022-01-21 08:41:48 +01:00
auto& playerMgr = Common::Service< World::Manager::PlayerMgr >::ref();
2022-02-16 15:11:10 -03:00
auto& taskMgr = Common::Service< World::Manager::TaskMgr >::ref();
2019-01-30 23:48:09 +01:00
setTargetId( INVALID_GAME_OBJECT_ID64 );
m_currentStance = Stance::Passive;
m_state = BNpcState::Dead;
2019-01-23 22:37:55 +01:00
m_timeOfDeath = Util::getTimeSeconds();
setOwner( nullptr );
2022-01-21 08:41:48 +01:00
taskMgr.queueTask( World::makeFadeBNpcTask( 10000, getAsBNpc() ) );
taskMgr.queueTask( World::makeRemoveBNpcTask( 12000, getAsBNpc() ) );
2022-01-19 17:22:56 +01:00
auto& exdData = Common::Service< Data::ExdData >::ref();
2022-01-27 21:24:54 +01:00
auto paramGrowthInfo = exdData.getRow< Excel::ParamGrow >( m_level );
2022-01-19 17:22:56 +01:00
2022-02-16 15:11:10 -03:00
for( const auto& pHateEntry : m_hateList )
{
// TODO: handle drops
auto pPlayer = pHateEntry->m_pChara->getAsPlayer();
if( pPlayer )
{
2022-02-23 08:36:23 +01:00
playerMgr.onMobKill( *pPlayer, *this );
2022-01-19 17:22:56 +01:00
pPlayer->gainExp( paramGrowthInfo->data().BaseExp );
}
}
2022-02-16 15:11:10 -03:00
hateListClear();
}
2019-01-23 22:37:55 +01:00
uint32_t BNpc::getTimeOfDeath() const
2019-01-23 22:37:55 +01:00
{
return m_timeOfDeath;
}
void BNpc::setTimeOfDeath( uint32_t timeOfDeath )
2019-01-23 22:37:55 +01:00
{
m_timeOfDeath = timeOfDeath;
}
void BNpc::checkAggro()
{
// passive mobs should ignore players unless aggro'd
if( m_aggressionMode == 1 )
return;
CharaPtr pClosestChara = getClosestChara();
if( pClosestChara && pClosestChara->isAlive() && ( getEnemyType() != 0 && pClosestChara->isPlayer() ) )
{
// will use this range if chara level is lower than bnpc, otherwise diminishing equation applies
float range = 14.f;
if( pClosestChara->getLevel() > m_level )
{
auto levelDiff = std::abs( pClosestChara->getLevel() - this->getLevel() );
if( levelDiff >= 10 )
range = 0.f;
else
2023-01-17 08:28:06 +01:00
range = std::max< float >( 0.f, range - std::pow( 1.53f, static_cast< float >( levelDiff ) * 0.6f ) );
}
2022-01-21 08:41:48 +01:00
auto distance = Util::distance( getPos(), pClosestChara->getPos() );
if( distance < range )
{
aggro( pClosestChara );
}
}
else if( pClosestChara && pClosestChara->isAlive() && ( getEnemyType() == 0 && pClosestChara->isBattleNpc() ) )
{
if( getBNpcType() == Common::BNpcType::Friendly )
{
if( pClosestChara->getAsBNpc()->getBNpcType() == Common::BNpcType::Friendly )
return;
}
if( getEnemyType() == 0 && pClosestChara->getAsBNpc()->getEnemyType() == 0 )
return;
2019-01-31 23:19:25 +11:00
// will use this range if chara level is lower than bnpc, otherwise diminishing equation applies
float range = 14.f;
if( pClosestChara->getLevel() > m_level )
{
auto levelDiff = std::abs( pClosestChara->getLevel() - this->getLevel() );
if( levelDiff >= 10 )
range = 0.f;
else
2022-01-19 17:55:29 +01:00
range = std::max< float >( 0.f, range - std::pow( 1.53f, static_cast< float >( levelDiff ) * 0.6f ) );
}
2022-01-21 08:41:48 +01:00
auto distance = Util::distance( getPos(), pClosestChara->getPos() );
if( distance < range )
{
aggro( pClosestChara );
}
}
2019-01-31 22:49:04 +11:00
}
void BNpc::setOwner( const CharaPtr& m_pChara )
{
m_pOwner = m_pChara;
2022-01-21 08:41:48 +01:00
auto targetId = static_cast< uint32_t >( INVALID_GAME_OBJECT_ID );
if( m_pChara != nullptr )
2022-01-21 08:41:48 +01:00
targetId = m_pChara->getId();
auto setOwnerPacket = makeZonePacket< FFXIVIpcFirstAttack >( getId() );
setOwnerPacket->data().Type = 0x01;
setOwnerPacket->data().Id = targetId;
server().queueForPlayers( getInRangePlayerIds(), setOwnerPacket );
if( m_pChara && m_pChara->isPlayer() )
Network::Util::Packet::sendActorControl( *m_pChara->getAsPlayer(), SetHateLetter, 1, getId(), 0 );
}
void BNpc::setLevelId( uint32_t levelId )
{
m_levelId = levelId;
}
uint32_t BNpc::getLevelId() const
{
return m_levelId;
}
bool BNpc::hasFlag( uint32_t flag ) const
{
return m_flags & flag;
}
void BNpc::setFlag( uint32_t flag )
{
m_flags |= flag;
}
void BNpc::autoAttack( CharaPtr pTarget )
{
auto& teriMgr = Common::Service< World::Manager::TerritoryMgr >::ref();
auto pZone = teriMgr.getTerritoryByGuId( getTerritoryId() );
uint64_t tick = Util::getTimeMs();
// todo: this needs to use the auto attack delay for the equipped weapon
if( ( tick - m_lastAttack ) > 2500 )
{
pTarget->onActionHostile( getAsChara() );
m_lastAttack = tick;
srand( static_cast< uint32_t >( tick ) );
auto damage = Math::CalcStats::calcAutoAttackDamage( *this );
//damage.first = 1;
auto effectPacket = std::make_shared< EffectPacket1 >( getId(), pTarget->getId(), 7 );
effectPacket->setRotation( Util::floatToUInt16Rot( getRot() ) );
Common::CalcResultParam effectEntry{};
effectEntry.Value = static_cast< int16_t >( damage.first );
effectEntry.Type = ActionEffectType::CALC_RESULT_TYPE_DAMAGE_HP;
effectEntry.Arg0 = 3;
effectEntry.Arg1 = 7;
auto resultId = pZone->getNextEffectResultId();
2023-02-20 11:24:02 +01:00
effectPacket->setResultId( resultId );
effectPacket->addTargetEffect( effectEntry );
server().queueForPlayers( getInRangePlayerIds(), effectPacket );
2020-01-07 19:08:13 +09:00
pTarget->takeDamage( static_cast< uint16_t >( damage.first ) );
auto& taskMgr = Common::Service< World::Manager::TaskMgr >::ref();
taskMgr.queueTask( Sapphire::World::makeActionIntegrityTask( resultId, pTarget, 500 ) );
}
}
void BNpc::calculateStats()
{
2022-01-19 17:55:29 +01:00
auto level = getLevel();
auto job = static_cast< uint8_t >( getClass() );
auto& exdData = Common::Service< Data::ExdData >::ref();
2022-01-27 21:24:54 +01:00
auto classInfo = exdData.getRow< Excel::ClassJob >( job );
auto paramGrowthInfo = exdData.getRow< Excel::ParamGrow >( level );
float base = Math::CalcStats::calculateBaseStat( *this );
auto str = static_cast< uint32_t >( base * ( static_cast< float >( classInfo->data().STR ) / 100 ) );
auto dex = static_cast< uint32_t >( base * ( static_cast< float >( classInfo->data().DEX ) / 100 ) );
auto vit = static_cast< uint32_t >( base * ( static_cast< float >( classInfo->data().VIT ) / 100 ) );
auto inte = static_cast< uint32_t >( base * ( static_cast< float >( classInfo->data().INT_ ) / 100 ) );
auto mnd = static_cast< uint32_t >( base * ( static_cast< float >( classInfo->data().MND ) / 100 ) );
auto pie = static_cast< uint32_t >( base * ( static_cast< float >( classInfo->data().PIE ) / 100 ) );
setStatValue( BaseParam::Strength, str );
setStatValue( BaseParam::Dexterity, dex );
setStatValue( BaseParam::Vitality, vit );
setStatValue( BaseParam::Intelligence, inte );
setStatValue( BaseParam::Mind, mnd );
setStatValue( BaseParam::Piety, pie );
auto determination = static_cast< uint32_t >( base );
auto skillSpeed = static_cast< uint32_t >( paramGrowthInfo->data().ParamBase );
auto spellSpeed = static_cast< uint32_t >( paramGrowthInfo->data().ParamBase );
auto accuracy = static_cast< uint32_t >( paramGrowthInfo->data().ParamBase );
auto critHitRate = static_cast< uint32_t >( paramGrowthInfo->data().ParamBase );
setStatValue( BaseParam::Determination, determination );
setStatValue( BaseParam::SkillSpeed, skillSpeed );
setStatValue( BaseParam::SpellSpeed, spellSpeed );
setStatValue( BaseParam::CriticalHit, critHitRate );
setStatValue( BaseParam::AttackPower, str );
setStatValue( BaseParam::AttackMagicPotency, inte );
setStatValue( BaseParam::HealingMagicPotency, mnd );
}
uint32_t BNpc::getRank() const
{
return m_rank;
}
uint32_t BNpc::getBoundInstanceId() const
{
return m_boundInstanceId;
}
BNpcType BNpc::getBNpcType() const
{
return m_bnpcType;
}
uint32_t BNpc::getLayoutId() const
{
return m_layoutId;
}
void BNpc::init()
{
m_maxHp = Math::CalcStats::calculateMaxHp( *getAsChara() );
m_hp = m_maxHp;
}