From 1bbc01f04fa895c20e1b10a38e7930e34a30bf04 Mon Sep 17 00:00:00 2001 From: NotAdam Date: Mon, 28 Jan 2019 19:16:44 +1100 Subject: [PATCH] manage timer vars and call onTick from Chara update instead of derived classes --- src/world/Actor/BNpc.cpp | 20 +++++++++++++------- src/world/Actor/BNpc.h | 3 ++- src/world/Actor/Chara.cpp | 12 ++++++++++++ src/world/Actor/Chara.h | 6 +++--- src/world/Actor/Player.cpp | 9 +-------- src/world/Actor/PlayerEvent.cpp | 3 +++ 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/world/Actor/BNpc.cpp b/src/world/Actor/BNpc.cpp index 039374b7..faff1d45 100644 --- a/src/world/Actor/BNpc.cpp +++ b/src/world/Actor/BNpc.cpp @@ -88,7 +88,7 @@ Sapphire::Entity::BNpc::BNpc( uint32_t id, BNpcTemplatePtr pTemplate, float posX memcpy( m_customize, pTemplate->getCustomize(), sizeof( m_customize ) ); memcpy( m_modelEquip, pTemplate->getModelEquip(), sizeof( m_modelEquip ) ); - m_lastTickTime = Util::getTimeMs(); + m_lastTickTime = 0; } Sapphire::Entity::BNpc::~BNpc() @@ -177,7 +177,7 @@ void Sapphire::Entity::BNpc::step() // This is probably not a good way to do it but works fine for now float angle = Util::calcAngFrom( getPos().x, getPos().z, stepPos.x, stepPos.z ) + PI; - auto delta = static_cast< float >( Util::getTimeMs() - m_lastTickTime ) / 1000.f; + auto delta = static_cast< float >( Util::getTimeMs() - m_lastUpdate ) / 1000.f; float speed = 7.5f * delta; @@ -388,6 +388,14 @@ void Sapphire::Entity::BNpc::deaggro( Sapphire::Entity::CharaPtr pChara ) } } +void Sapphire::Entity::BNpc::onTick() +{ + if( m_state == BNpcState::Retreat ) + { + regainHp(); + } +} + void Sapphire::Entity::BNpc::update( int64_t currTime ) { const uint8_t minActorDistance = 4; @@ -406,9 +414,6 @@ void Sapphire::Entity::BNpc::update( int64_t currTime ) { setInvincibilityType( InvincibilityType::InvincibilityIgnoreDamage ); - if( std::difftime( currTime, m_lastTickTime ) > 3000 ) - regainHp( currTime ); - if( moveTo( m_spawnPos ) ) { setInvincibilityType( InvincibilityType::InvincibilityNone ); @@ -511,10 +516,11 @@ void Sapphire::Entity::BNpc::update( int64_t currTime ) } } - m_lastTickTime = currTime; + + Chara::update( currTime ); } -void Sapphire::Entity::BNpc::regainHp( int64_t currTime ) +void Sapphire::Entity::BNpc::regainHp() { if( this->m_hp < this->getMaxHp() ) { diff --git a/src/world/Actor/BNpc.h b/src/world/Actor/BNpc.h index fd2e1844..40b6cae4 100644 --- a/src/world/Actor/BNpc.h +++ b/src/world/Actor/BNpc.h @@ -82,6 +82,7 @@ namespace Sapphire::Entity void deaggro( CharaPtr pChara ); void update( int64_t currTime ) override; + void onTick() override; void onActionHostile( CharaPtr pSource ) override; @@ -90,7 +91,7 @@ namespace Sapphire::Entity uint32_t getTimeOfDeath() const; void setTimeOfDeath( uint32_t timeOfDeath ); - void regainHp( int64_t currTime ); + void regainHp(); void checkAggro( uint32_t range ); diff --git a/src/world/Actor/Chara.cpp b/src/world/Actor/Chara.cpp index 6e2272cc..3c888b35 100644 --- a/src/world/Actor/Chara.cpp +++ b/src/world/Actor/Chara.cpp @@ -280,6 +280,18 @@ bool Sapphire::Entity::Chara::checkAction() } +void Sapphire::Entity::Chara::update( int64_t currTime ) +{ + if( std::difftime( currTime, m_lastTickTime ) > 3000 ) + { + onTick(); + + m_lastTickTime = currTime; + } + + m_lastUpdate = currTime; +} + /*! Change the current target and propagate to in range players diff --git a/src/world/Actor/Chara.h b/src/world/Actor/Chara.h index 1adf919b..896404b5 100644 --- a/src/world/Actor/Chara.h +++ b/src/world/Actor/Chara.h @@ -68,11 +68,11 @@ namespace Sapphire::Entity protected: char m_name[34]; /*! Last tick time for the actor ( in ms ) */ - uint64_t m_lastTickTime; + int64_t m_lastTickTime; /*! Last time the actor performed an autoAttack ( in ms ) */ uint64_t m_lastAttack; /*! Last time the actor was updated ( in ms ) */ - uint64_t m_lastUpdate; + int64_t m_lastUpdate; /*! Current stance of the actor */ Common::Stance m_currentStance; /*! Current staus of the actor */ @@ -237,7 +237,7 @@ namespace Sapphire::Entity virtual bool checkAction(); - virtual void update( int64_t currTime ) {}; + virtual void update( int64_t currTime ); Action::ActionPtr getCurrentAction() const; diff --git a/src/world/Actor/Player.cpp b/src/world/Actor/Player.cpp index db5e56fb..f7e2c317 100644 --- a/src/world/Actor/Player.cpp +++ b/src/world/Actor/Player.cpp @@ -1043,7 +1043,6 @@ void Sapphire::Entity::Player::unsetStateFlag( Common::PlayerStateFlag flag ) void Sapphire::Entity::Player::update( int64_t currTime ) { - // a zoning is pending, lets do it if( m_queuedZoneing && ( currTime - m_queuedZoneing->m_queueTime ) > 800 ) { @@ -1115,13 +1114,7 @@ void Sapphire::Entity::Player::update( int64_t currTime ) } } - if( ( currTime - m_lastTickTime ) > 3000 ) - { - // add 3 seconds to total play time - m_playTime += 3; - m_lastTickTime = currTime; - onTick(); - } + Chara::update( currTime ); } void Sapphire::Entity::Player::onMobKill( uint16_t nameId ) diff --git a/src/world/Actor/PlayerEvent.cpp b/src/world/Actor/PlayerEvent.cpp index 089cc5c0..6b0865eb 100644 --- a/src/world/Actor/PlayerEvent.cpp +++ b/src/world/Actor/PlayerEvent.cpp @@ -355,6 +355,9 @@ void Sapphire::Entity::Player::onDeath() void Sapphire::Entity::Player::onTick() { + // add 3 seconds to total play time + m_playTime += 3; + bool sendUpdate = false; if( !isAlive() || !isLoadingComplete() )