Skip to content

Commit

Permalink
Merge pull request #962 from arieshi255/actions-fixup
Browse files Browse the repository at this point in the history
[3.x] Fix StatusEffect flag application from exd and add isFacingTarget function
  • Loading branch information
hkAlice authored Jun 26, 2024
2 parents 52dfd3a + 0acff69 commit 88ae073
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/common/Util/UtilMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
8 changes: 8 additions & 0 deletions src/common/Util/UtilMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 10 additions & 1 deletion src/common/Vector3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Vector3 Vector3::operator - ( const Vector3& target ) const
{
return Vector3{ x - target.x, y - target.y, z - target.z };
}
2 changes: 2 additions & 0 deletions src/common/Vector3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/world/Actor/Chara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/world/Actor/Chara.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
40 changes: 40 additions & 0 deletions src/world/Manager/DebugCommandMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ) );
}
}
}
2 changes: 2 additions & 0 deletions src/world/Manager/DebugCommandMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

};

}
21 changes: 14 additions & 7 deletions src/world/StatusEffect/StatusEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ Sapphire::StatusEffect::StatusEffect::StatusEffect( uint32_t id, Entity::CharaPt
Util::eraseAll( m_name, ')' );

m_flag |= entry->data().Category;
m_flag |= static_cast< uint32_t >( entry->data().Forever ) << static_cast< uint32_t >( Common::StatusEffectFlag::Permanent );
m_flag |= static_cast< uint32_t >( entry->data().CanOff ) << static_cast< uint32_t >( Common::StatusEffectFlag::CanStatusOff );
m_flag |= static_cast< uint32_t >( entry->data().NotAction ) << static_cast< uint32_t >( Common::StatusEffectFlag::LockActions );
m_flag |= static_cast< uint32_t >( entry->data().NotControl ) << static_cast< uint32_t >( Common::StatusEffectFlag::LockControl );
m_flag |= static_cast< uint32_t >( entry->data().NotMove ) << static_cast< uint32_t >( Common::StatusEffectFlag::LockMovement );
m_flag |= static_cast< uint32_t >( entry->data().NotLookAt ) << static_cast< uint32_t >( Common::StatusEffectFlag::IsGaze );
m_flag |= static_cast< uint32_t >( entry->data().FcAction ) << static_cast< uint32_t >( Common::StatusEffectFlag::FcBuff );
if( entry->data().Forever )
m_flag |= static_cast< uint32_t >( Common::StatusEffectFlag::Permanent );
if( entry->data().CanOff )
m_flag |= static_cast< uint32_t >( Common::StatusEffectFlag::CanStatusOff );
if( entry->data().NotAction )
m_flag |= static_cast< uint32_t >( Common::StatusEffectFlag::LockActions );
if( entry->data().NotControl )
m_flag |= static_cast< uint32_t >( Common::StatusEffectFlag::LockControl );
if( entry->data().NotMove )
m_flag |= static_cast< uint32_t >( Common::StatusEffectFlag::LockMovement );
if( entry->data().NotLookAt )
m_flag |= static_cast< uint32_t >( Common::StatusEffectFlag::IsGaze );
if( entry->data().FcAction )
m_flag |= static_cast< uint32_t >( Common::StatusEffectFlag::FcBuff );
}


Expand Down

0 comments on commit 88ae073

Please sign in to comment.