2023-04-07 11:21:12 -07:00
using System.Diagnostics.CodeAnalysis ;
2023-07-08 14:08:32 +10:00
using System.Numerics ;
2023-06-01 00:57:31 -05:00
using Content.Shared.Administration.Logs ;
2023-04-02 16:48:32 +03:00
using Content.Shared.Audio ;
2023-06-01 00:57:31 -05:00
using Content.Shared.Database ;
2023-08-25 12:48:27 +10:00
using Content.Shared.Hands ;
2023-07-30 18:07:45 -07:00
using Content.Shared.Inventory ;
using Content.Shared.Inventory.Events ;
2024-01-03 17:24:02 +11:00
using Content.Shared.Item.ItemToggle.Components ;
2023-04-07 11:21:12 -07:00
using Content.Shared.Popups ;
2023-05-15 15:21:05 +10:00
using Content.Shared.Projectiles ;
using Content.Shared.Weapons.Ranged.Components ;
2023-09-11 02:04:02 -07:00
using Content.Shared.Weapons.Ranged.Events ;
2023-11-27 22:12:34 +11:00
using Robust.Shared.Audio ;
using Robust.Shared.Audio.Systems ;
2023-05-15 15:21:05 +10:00
using Robust.Shared.Network ;
2023-09-11 02:04:02 -07:00
using Robust.Shared.Physics.Components ;
2023-04-07 11:21:12 -07:00
using Robust.Shared.Physics.Systems ;
using Robust.Shared.Random ;
2023-09-11 02:04:02 -07:00
using Robust.Shared.Timing ;
2023-04-02 16:48:32 +03:00
namespace Content.Shared.Weapons.Reflect ;
/// <summary>
/// This handles reflecting projectiles and hitscan shots.
/// </summary>
2024-01-03 17:24:02 +11:00
public sealed class ReflectSystem : EntitySystem
2023-04-02 16:48:32 +03:00
{
2023-05-15 15:21:05 +10:00
[Dependency] private readonly INetManager _netManager = default ! ;
2023-04-02 16:48:32 +03:00
[Dependency] private readonly IRobustRandom _random = default ! ;
2023-06-01 00:57:31 -05:00
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default ! ;
2023-09-11 02:04:02 -07:00
[Dependency] private readonly IGameTiming _gameTiming = default ! ;
2023-04-02 16:48:32 +03:00
[Dependency] private readonly SharedPopupSystem _popup = default ! ;
[Dependency] private readonly SharedPhysicsSystem _physics = default ! ;
[Dependency] private readonly SharedAudioSystem _audio = default ! ;
[Dependency] private readonly SharedTransformSystem _transform = default ! ;
2023-07-30 18:07:45 -07:00
[Dependency] private readonly InventorySystem _inventorySystem = default ! ;
2023-04-02 16:48:32 +03:00
public override void Initialize ( )
{
base . Initialize ( ) ;
2023-07-30 18:07:45 -07:00
2023-08-06 16:44:41 +03:00
SubscribeLocalEvent < ReflectComponent , ProjectileReflectAttemptEvent > ( OnReflectCollide ) ;
2023-05-15 15:21:05 +10:00
SubscribeLocalEvent < ReflectComponent , HitScanReflectAttemptEvent > ( OnReflectHitscan ) ;
2023-07-30 18:07:45 -07:00
SubscribeLocalEvent < ReflectComponent , GotEquippedEvent > ( OnReflectEquipped ) ;
SubscribeLocalEvent < ReflectComponent , GotUnequippedEvent > ( OnReflectUnequipped ) ;
2023-08-25 12:48:27 +10:00
SubscribeLocalEvent < ReflectComponent , GotEquippedHandEvent > ( OnReflectHandEquipped ) ;
SubscribeLocalEvent < ReflectComponent , GotUnequippedHandEvent > ( OnReflectHandUnequipped ) ;
2024-01-03 17:24:02 +11:00
SubscribeLocalEvent < ReflectComponent , ItemToggledEvent > ( OnToggleReflect ) ;
SubscribeLocalEvent < ReflectUserComponent , ProjectileReflectAttemptEvent > ( OnReflectUserCollide ) ;
SubscribeLocalEvent < ReflectUserComponent , HitScanReflectAttemptEvent > ( OnReflectUserHitscan ) ;
2023-04-02 16:48:32 +03:00
}
2023-08-25 12:48:27 +10:00
private void OnReflectUserHitscan ( EntityUid uid , ReflectUserComponent component , ref HitScanReflectAttemptEvent args )
2023-04-02 16:48:32 +03:00
{
2023-08-25 12:48:27 +10:00
if ( args . Reflected )
2023-04-19 20:02:30 +10:00
return ;
2023-08-25 12:48:27 +10:00
foreach ( var ent in _inventorySystem . GetHandOrInventoryEntities ( uid , SlotFlags . All & ~ SlotFlags . POCKET ) )
{
if ( ! TryReflectHitscan ( uid , ent , args . Shooter , args . SourceItem , args . Direction , out var dir ) )
continue ;
args . Direction = dir . Value ;
args . Reflected = true ;
break ;
2023-05-15 15:21:05 +10:00
}
2023-08-25 12:48:27 +10:00
}
private void OnReflectUserCollide ( EntityUid uid , ReflectUserComponent component , ref ProjectileReflectAttemptEvent args )
{
foreach ( var ent in _inventorySystem . GetHandOrInventoryEntities ( uid , SlotFlags . All & ~ SlotFlags . POCKET ) )
{
if ( ! TryReflectProjectile ( uid , ent , args . ProjUid ) )
continue ;
2023-04-19 20:02:30 +10:00
2023-05-15 15:21:05 +10:00
args . Cancelled = true ;
2023-08-25 12:48:27 +10:00
break ;
}
2023-04-02 16:48:32 +03:00
}
2023-08-25 12:48:27 +10:00
private void OnReflectCollide ( EntityUid uid , ReflectComponent component , ref ProjectileReflectAttemptEvent args )
2023-04-02 16:48:32 +03:00
{
if ( args . Cancelled )
return ;
2023-04-19 20:02:30 +10:00
2023-08-25 12:48:27 +10:00
if ( TryReflectProjectile ( uid , uid , args . ProjUid , reflect : component ) )
2023-04-02 16:48:32 +03:00
args . Cancelled = true ;
}
2023-04-19 20:02:30 +10:00
2023-08-25 12:48:27 +10:00
private bool TryReflectProjectile ( EntityUid user , EntityUid reflector , EntityUid projectile , ProjectileComponent ? projectileComp = null , ReflectComponent ? reflect = null )
2023-04-02 16:48:32 +03:00
{
2023-05-15 15:21:05 +10:00
if ( ! Resolve ( reflector , ref reflect , false ) | |
2023-04-19 20:02:30 +10:00
! reflect . Enabled | |
2023-05-15 15:21:05 +10:00
! TryComp < ReflectiveComponent > ( projectile , out var reflective ) | |
( reflect . Reflects & reflective . Reflective ) = = 0x0 | |
2023-04-19 20:02:30 +10:00
! _random . Prob ( reflect . ReflectProb ) | |
! TryComp < PhysicsComponent > ( projectile , out var physics ) )
2023-04-02 16:48:32 +03:00
{
2023-04-19 20:02:30 +10:00
return false ;
}
2023-04-02 16:48:32 +03:00
2023-04-19 20:02:30 +10:00
var rotation = _random . NextAngle ( - reflect . Spread / 2 , reflect . Spread / 2 ) . Opposite ( ) ;
var existingVelocity = _physics . GetMapLinearVelocity ( projectile , component : physics ) ;
2023-08-25 12:48:27 +10:00
var relativeVelocity = existingVelocity - _physics . GetMapLinearVelocity ( user ) ;
2023-04-19 20:02:30 +10:00
var newVelocity = rotation . RotateVec ( relativeVelocity ) ;
2023-04-02 16:48:32 +03:00
2023-04-19 20:02:30 +10:00
// Have the velocity in world terms above so need to convert it back to local.
var difference = newVelocity - existingVelocity ;
2023-04-02 16:48:32 +03:00
2023-04-19 20:02:30 +10:00
_physics . SetLinearVelocity ( projectile , physics . LinearVelocity + difference , body : physics ) ;
var locRot = Transform ( projectile ) . LocalRotation ;
var newRot = rotation . RotateVec ( locRot . ToVec ( ) ) ;
_transform . SetLocalRotation ( projectile , newRot . ToAngle ( ) ) ;
2023-05-15 15:21:05 +10:00
if ( _netManager . IsServer )
{
2023-08-25 12:48:27 +10:00
_popup . PopupEntity ( Loc . GetString ( "reflect-shot" ) , user ) ;
_audio . PlayPvs ( reflect . SoundOnReflect , user , AudioHelpers . WithVariation ( 0.05f , _random ) ) ;
2023-05-15 15:21:05 +10:00
}
if ( Resolve ( projectile , ref projectileComp , false ) )
{
2023-12-22 02:26:08 -05:00
_adminLogger . Add ( LogType . BulletHit , LogImpact . Medium , $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectileComp.Weapon)} shot by {projectileComp.Shooter}" ) ;
2023-06-01 00:57:31 -05:00
2023-08-25 12:48:27 +10:00
projectileComp . Shooter = user ;
projectileComp . Weapon = user ;
Dirty ( projectile , projectileComp ) ;
2023-05-15 15:21:05 +10:00
}
2023-06-01 00:57:31 -05:00
else
{
2023-08-25 12:48:27 +10:00
_adminLogger . Add ( LogType . BulletHit , LogImpact . Medium , $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)}" ) ;
2023-06-01 00:57:31 -05:00
}
2023-05-15 15:21:05 +10:00
2023-04-19 20:02:30 +10:00
return true ;
2023-04-02 16:48:32 +03:00
}
2023-05-15 15:21:05 +10:00
private void OnReflectHitscan ( EntityUid uid , ReflectComponent component , ref HitScanReflectAttemptEvent args )
{
if ( args . Reflected | |
( component . Reflects & args . Reflective ) = = 0x0 )
{
2023-04-02 16:48:32 +03:00
return ;
2023-05-15 15:21:05 +10:00
}
2023-04-19 20:02:30 +10:00
2023-08-25 12:48:27 +10:00
if ( TryReflectHitscan ( uid , uid , args . Shooter , args . SourceItem , args . Direction , out var dir ) )
2023-04-02 16:48:32 +03:00
{
args . Direction = dir . Value ;
args . Reflected = true ;
}
}
2023-08-25 12:48:27 +10:00
private bool TryReflectHitscan (
EntityUid user ,
EntityUid reflector ,
EntityUid ? shooter ,
EntityUid shotSource ,
Vector2 direction ,
2023-05-15 15:21:05 +10:00
[NotNullWhen(true)] out Vector2 ? newDirection )
2023-04-02 16:48:32 +03:00
{
2023-05-15 15:21:05 +10:00
if ( ! TryComp < ReflectComponent > ( reflector , out var reflect ) | |
! reflect . Enabled | |
! _random . Prob ( reflect . ReflectProb ) )
{
newDirection = null ;
return false ;
}
if ( _netManager . IsServer )
2023-04-02 16:48:32 +03:00
{
2023-08-25 12:48:27 +10:00
_popup . PopupEntity ( Loc . GetString ( "reflect-shot" ) , user ) ;
_audio . PlayPvs ( reflect . SoundOnReflect , user , AudioHelpers . WithVariation ( 0.05f , _random ) ) ;
2023-04-02 16:48:32 +03:00
}
2023-04-19 20:02:30 +10:00
2023-05-15 15:21:05 +10:00
var spread = _random . NextAngle ( - reflect . Spread / 2 , reflect . Spread / 2 ) ;
newDirection = - spread . RotateVec ( direction ) ;
2023-06-01 00:57:31 -05:00
if ( shooter ! = null )
2023-08-25 12:48:27 +10:00
_adminLogger . Add ( LogType . HitScanHit , LogImpact . Medium , $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)} shot by {ToPrettyString(shooter.Value)}" ) ;
2023-06-01 00:57:31 -05:00
else
2023-08-25 12:48:27 +10:00
_adminLogger . Add ( LogType . HitScanHit , LogImpact . Medium , $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)}" ) ;
2023-06-01 00:57:31 -05:00
2023-05-15 15:21:05 +10:00
return true ;
2023-04-02 16:48:32 +03:00
}
2023-07-30 18:07:45 -07:00
2023-08-25 12:48:27 +10:00
private void OnReflectEquipped ( EntityUid uid , ReflectComponent component , GotEquippedEvent args )
2023-07-30 18:07:45 -07:00
{
2023-09-11 02:04:02 -07:00
if ( _gameTiming . ApplyingState )
return ;
2023-08-25 12:48:27 +10:00
EnsureComp < ReflectUserComponent > ( args . Equipee ) ;
2023-07-30 18:07:45 -07:00
}
private void OnReflectUnequipped ( EntityUid uid , ReflectComponent comp , GotUnequippedEvent args )
{
2023-08-25 12:48:27 +10:00
RefreshReflectUser ( args . Equipee ) ;
}
2023-07-30 18:07:45 -07:00
2023-08-25 12:48:27 +10:00
private void OnReflectHandEquipped ( EntityUid uid , ReflectComponent component , GotEquippedHandEvent args )
{
2023-09-11 02:04:02 -07:00
if ( _gameTiming . ApplyingState )
return ;
2023-08-25 12:48:27 +10:00
EnsureComp < ReflectUserComponent > ( args . User ) ;
}
2023-07-30 18:07:45 -07:00
2023-08-25 12:48:27 +10:00
private void OnReflectHandUnequipped ( EntityUid uid , ReflectComponent component , GotUnequippedHandEvent args )
{
RefreshReflectUser ( args . User ) ;
}
2023-07-30 18:07:45 -07:00
2024-01-03 17:24:02 +11:00
private void OnToggleReflect ( EntityUid uid , ReflectComponent comp , ref ItemToggledEvent args )
{
comp . Enabled = args . Activated ;
Dirty ( uid , comp ) ;
}
2023-08-25 12:48:27 +10:00
/// <summary>
/// Refreshes whether someone has reflection potential so we can raise directed events on them.
/// </summary>
private void RefreshReflectUser ( EntityUid user )
{
foreach ( var ent in _inventorySystem . GetHandOrInventoryEntities ( user , SlotFlags . All & ~ SlotFlags . POCKET ) )
2023-07-30 18:07:45 -07:00
{
2023-08-25 12:48:27 +10:00
if ( ! HasComp < ReflectComponent > ( ent ) )
2023-07-30 18:07:45 -07:00
continue ;
2023-08-25 12:48:27 +10:00
EnsureComp < ReflectUserComponent > ( user ) ;
return ;
2023-07-30 18:07:45 -07:00
}
2023-08-25 12:48:27 +10:00
RemCompDeferred < ReflectUserComponent > ( user ) ;
2023-07-30 18:07:45 -07:00
}
2023-04-02 16:48:32 +03:00
}