From 085a184ad3f88dacd1b3ee30aa903ecdae4896e9 Mon Sep 17 00:00:00 2001 From: NotAdam Date: Thu, 31 Jan 2019 17:53:20 +1100 Subject: [PATCH] add deminishing aggro range based on level difference --- src/world/Actor/BNpc.cpp | 28 +++++++++++++++++++++------- src/world/Actor/BNpc.h | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/world/Actor/BNpc.cpp b/src/world/Actor/BNpc.cpp index e5c45cdb..a2bbc9f6 100644 --- a/src/world/Actor/BNpc.cpp +++ b/src/world/Actor/BNpc.cpp @@ -402,7 +402,6 @@ void Sapphire::Entity::BNpc::onTick() void Sapphire::Entity::BNpc::update( int64_t currTime ) { const uint8_t minActorDistance = 4; - const uint8_t aggroRange = 8; const uint8_t maxDistanceToOrigin = 40; const uint32_t roamTick = 20; @@ -441,7 +440,7 @@ void Sapphire::Entity::BNpc::update( int64_t currTime ) m_state = BNpcState::Idle; } - checkAggro( aggroRange ); + checkAggro(); } break; @@ -462,7 +461,7 @@ void Sapphire::Entity::BNpc::update( int64_t currTime ) m_state = BNpcState::Roaming; } - checkAggro( aggroRange ); + checkAggro(); } case BNpcState::Combat: @@ -574,7 +573,7 @@ void Sapphire::Entity::BNpc::setTimeOfDeath( uint32_t timeOfDeath ) m_timeOfDeath = timeOfDeath; } -void Sapphire::Entity::BNpc::checkAggro( uint32_t range ) +void Sapphire::Entity::BNpc::checkAggro() { // passive mobs should ignore players unless aggro'd if( m_aggressionMode == 1 ) @@ -582,14 +581,29 @@ void Sapphire::Entity::BNpc::checkAggro( uint32_t range ) CharaPtr pClosestChara = getClosestChara(); - if( pClosestChara && pClosestChara->isAlive() ) + if( pClosestChara && pClosestChara->isAlive() && pClosestChara->isPlayer() ) { + // will use this range if chara level is lower than bnpc, otherwise deminishing equation applies + float range = 13.f; + + if( pClosestChara->getLevel() > m_level ) + { + auto levelDiff = std::abs( pClosestChara->getLevel() - this->getLevel() ); + range = std::max< float >( 0.f, range - std::pow( 1.53f, levelDiff * 0.6f ) ); + } + + // level difference too great, ignore player + if( range == 0.f ) + return; + auto distance = Util::distance( getPos().x, getPos().y, getPos().z, pClosestChara->getPos().x, pClosestChara->getPos().y, pClosestChara->getPos().z ); - if( distance < range && pClosestChara->isPlayer() ) + if( distance < range ) + { aggro( pClosestChara ); + } } -} +} \ No newline at end of file diff --git a/src/world/Actor/BNpc.h b/src/world/Actor/BNpc.h index 40b6cae4..d873699f 100644 --- a/src/world/Actor/BNpc.h +++ b/src/world/Actor/BNpc.h @@ -93,7 +93,7 @@ namespace Sapphire::Entity void regainHp(); - void checkAggro( uint32_t range ); + void checkAggro(); private: uint32_t m_bNpcBaseId;