diff --git a/src/common/Util/UtilMath.cpp b/src/common/Util/UtilMath.cpp index 6a162d64..4d7410b5 100644 --- a/src/common/Util/UtilMath.cpp +++ b/src/common/Util/UtilMath.cpp @@ -143,3 +143,23 @@ float Util::trunc( float value, uint8_t digitsToRemain ) return std::floor( value * factor ) / factor; } + +float Util::length( const FFXIVARR_POSITION3& vec ) { + return std::sqrt( vec.x * vec.x + vec.y * vec.y + vec.z * vec.z ); +} + +FFXIVARR_POSITION3 Util::normalize( const FFXIVARR_POSITION3& vec ) { + float len = length( vec ); + if( len == 0 ) return FFXIVARR_POSITION3(); + return FFXIVARR_POSITION3{ vec.x / len, vec.y / len, vec.z / len }; +} + +float Util::dot( const FFXIVARR_POSITION3& vec1, const FFXIVARR_POSITION3& vec2 ) +{ + return vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z; +} + +FFXIVARR_POSITION3 Util::projectY( const FFXIVARR_POSITION3& vec ) +{ + return FFXIVARR_POSITION3{ vec.x, 0, vec.z }; +} \ No newline at end of file diff --git a/src/common/Util/UtilMath.h b/src/common/Util/UtilMath.h index 1f6ddd50..6ec655c6 100644 --- a/src/common/Util/UtilMath.h +++ b/src/common/Util/UtilMath.h @@ -44,6 +44,14 @@ namespace Sapphire::Common::Util FFXIVARR_POSITION3 transform( const FFXIVARR_POSITION3& vector, const Matrix33& matrix ); float eulerToDirection( const FFXIVARR_POSITION3& euler ); + + float length( const FFXIVARR_POSITION3& vec ); + + FFXIVARR_POSITION3 normalize( const FFXIVARR_POSITION3& vec ); + + float dot( const FFXIVARR_POSITION3& vec1, const FFXIVARR_POSITION3& vec2 ); + + FFXIVARR_POSITION3 projectY( const FFXIVARR_POSITION3& vec ); } #endif diff --git a/src/common/Vector3.cpp b/src/common/Vector3.cpp index a98c4fde..7f2bb394 100644 --- a/src/common/Vector3.cpp +++ b/src/common/Vector3.cpp @@ -7,8 +7,17 @@ inline bool FFXIVARR_POSITION3::operator == ( const FFXIVARR_POSITION3& target ) return x == target.x && y == target.y && z == target.z; } +FFXIVARR_POSITION3 FFXIVARR_POSITION3::operator - ( const FFXIVARR_POSITION3& target ) const +{ + return FFXIVARR_POSITION3{ x - target.x, y - target.y, z - target.z }; +} inline bool Vector3::operator == ( const Vector3& target ) const { return x == target.x && y == target.y && z == target.z && reserve == target.reserve; -} \ No newline at end of file +} + +Vector3 Vector3::operator - ( const Vector3& target ) const +{ + return Vector3{ x - target.x, y - target.y, z - target.z }; +} diff --git a/src/common/Vector3.h b/src/common/Vector3.h index 1658aa76..3ca47061 100644 --- a/src/common/Vector3.h +++ b/src/common/Vector3.h @@ -8,6 +8,7 @@ namespace Sapphire::Common float y; float z; inline bool operator == ( const FFXIVARR_POSITION3& target ) const; + FFXIVARR_POSITION3 operator - ( const FFXIVARR_POSITION3& target ) const; }; struct Vector3 @@ -17,6 +18,7 @@ namespace Sapphire::Common float z; float reserve; inline bool operator == ( const Vector3& target ) const; + Vector3 operator - ( const Vector3& target ) const; }; struct Matrix33 diff --git a/src/world/Actor/Chara.cpp b/src/world/Actor/Chara.cpp index 2d6abf28..9593b045 100644 --- a/src/world/Actor/Chara.cpp +++ b/src/world/Actor/Chara.cpp @@ -813,6 +813,26 @@ float Chara::getModifier( Common::ParamModifier paramModifier ) const return result; } +// Compute forward direction based on rotation angle (assuming rotation around Z axis) +FFXIVARR_POSITION3 Chara::getForwardVector() const { + return Common::Util::normalize( FFXIVARR_POSITION3{ std::sin( getRot() ), 0, std::cos( getRot() ) } ); +} + +// Function to check if actor is facing target +bool Chara::isFacingTarget( const Chara& other, float threshold ) +{ + auto toActor = Common::Util::normalize( Common::Util::projectY( other.getPos() - getPos() ) ); + + auto forward = getForwardVector(); + + float dot = Common::Util::dot( forward, toActor ); + + // The threshold is used to determine how closely the actors need to be facing each other + // 1.0 means they need to be perfectly facing each other + // Lower values allow for some deviation + return dot >= threshold; +} + void Chara::onTick() { uint32_t thisTickDmg = 0; diff --git a/src/world/Actor/Chara.h b/src/world/Actor/Chara.h index 8ee157de..212ea1de 100644 --- a/src/world/Actor/Chara.h +++ b/src/world/Actor/Chara.h @@ -249,6 +249,10 @@ namespace Sapphire::Entity virtual void update( uint64_t tickCount ); + Common::FFXIVARR_POSITION3 getForwardVector() const; + + bool isFacingTarget( const Chara& other, float threshold = 0.95f ); + World::Action::ActionPtr getCurrentAction() const; void setCurrentAction( World::Action::ActionPtr pAction ); diff --git a/src/world/Manager/DebugCommandMgr.cpp b/src/world/Manager/DebugCommandMgr.cpp index 2442c679..aaf583be 100644 --- a/src/world/Manager/DebugCommandMgr.cpp +++ b/src/world/Manager/DebugCommandMgr.cpp @@ -80,6 +80,7 @@ DebugCommandMgr::DebugCommandMgr() registerCommand( "cf", &DebugCommandMgr::contentFinder, "Content-Finder", 1 ); registerCommand( "ew", &DebugCommandMgr::easyWarp, "Easy warping", 1 ); registerCommand( "reload", &DebugCommandMgr::hotReload, "Reloads a resource", 1 ); + registerCommand( "facing", &DebugCommandMgr::facing, "Checks if you are facing an actor", 1 ); } // clear all loaded commands @@ -1481,4 +1482,43 @@ void DebugCommandMgr::hotReload( char* data, Sapphire::Entity::Player& player, s { PlayerMgr::sendDebug( player, "Unknown sub command." ); } +} + +void DebugCommandMgr::facing( char* data, Sapphire::Entity::Player& player, std::shared_ptr< DebugCommand > command ) +{ + std::string subCommand; + std::string params = ""; + + // check if the command has parameters + std::string tmpCommand = std::string( data + command->getName().length() + 1 ); + + std::size_t pos = tmpCommand.find_first_of( ' ' ); + + if( pos != std::string::npos ) + // command has parameters, grab the first part + subCommand = tmpCommand.substr( 0, pos ); + else + // no subcommand given + subCommand = tmpCommand; + + if( command->getName().length() + 1 + pos + 1 < strlen( data ) ) + params = std::string( data + command->getName().length() + 1 + pos + 1 ); + + Logger::debug( "[{0}] subCommand: {1} params: {2}", player.getId(), subCommand, params ); + + float threshold = 0.95f; + sscanf( params.c_str(), "%f", &threshold ); + + if( player.getTargetId() != 0 ) + { + auto target = player.lookupTargetById( player.getTargetId() ); + + if( !target ) + return; + + if( auto bnpc = target->getAsBNpc() ) + { + PlayerMgr::sendDebug( player, "Player facing target {0}: {1}", bnpc->getLayoutId(), player.isFacingTarget( *bnpc->getAsChara(), threshold ) ); + } + } } \ No newline at end of file diff --git a/src/world/Manager/DebugCommandMgr.h b/src/world/Manager/DebugCommandMgr.h index 084b99b6..57552896 100644 --- a/src/world/Manager/DebugCommandMgr.h +++ b/src/world/Manager/DebugCommandMgr.h @@ -65,6 +65,8 @@ namespace Sapphire::World::Manager void hotReload( char* data, Sapphire::Entity::Player& player, std::shared_ptr< DebugCommand > command ); + void facing( char* data, Sapphire::Entity::Player& player, std::shared_ptr< DebugCommand > command ); + }; } \ No newline at end of file