Revert "Weapon Reflection Movement Mechanic (#27219)" (#29326)

* Revert "Weapon Reflection Movement Mechanic (#27219)"

This reverts commit b90373356e.

# Conflicts:
#	Content.Shared/Alert/AlertType.cs
#	Content.Shared/Weapons/Reflect/ReflectSystem.cs

* Add myself to codeowners

* Add myself to codeowners

* Also the alerts
This commit is contained in:
metalgearsloth
2024-06-23 12:16:08 +10:00
committed by GitHub
parent 089cdcf777
commit 0546c9bf64
13 changed files with 105 additions and 274 deletions

View File

@@ -3,18 +3,16 @@ using System.Numerics;
using Content.Shared.Administration.Logs;
using Content.Shared.Alert;
using Content.Shared.Audio;
using Content.Shared.Damage.Components;
using Content.Shared.Database;
using Content.Shared.Gravity;
using Content.Shared.Hands;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.Popups;
using Content.Shared.Projectiles;
using Content.Shared.Standing;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Network;
using Robust.Shared.Physics.Components;
@@ -38,134 +36,73 @@ public sealed class ReflectSystem : EntitySystem
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!;
[Dependency] private readonly StandingStateSystem _standing = default!;
[Dependency] private readonly AlertsSystem _alerts = default!;
[ValidatePrototypeId<AlertPrototype>]
private const string DeflectingAlert = "Deflecting";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ReflectComponent, ProjectileReflectAttemptEvent>(OnObjectReflectProjectileAttempt);
SubscribeLocalEvent<ReflectComponent, HitScanReflectAttemptEvent>(OnObjectReflectHitscanAttempt);
SubscribeLocalEvent<ReflectComponent, ProjectileReflectAttemptEvent>(OnReflectCollide);
SubscribeLocalEvent<ReflectComponent, HitScanReflectAttemptEvent>(OnReflectHitscan);
SubscribeLocalEvent<ReflectComponent, GotEquippedEvent>(OnReflectEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedEvent>(OnReflectUnequipped);
SubscribeLocalEvent<ReflectComponent, GotEquippedHandEvent>(OnReflectHandEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedHandEvent>(OnReflectHandUnequipped);
SubscribeLocalEvent<ReflectComponent, ItemToggledEvent>(OnToggleReflect);
SubscribeLocalEvent<ReflectUserComponent, ProjectileReflectAttemptEvent>(OnUserProjectileReflectAttempt);
SubscribeLocalEvent<ReflectUserComponent, HitScanReflectAttemptEvent>(OnUserHitscanReflectAttempt);
SubscribeLocalEvent<ReflectUserComponent, ProjectileReflectAttemptEvent>(OnReflectUserCollide);
SubscribeLocalEvent<ReflectUserComponent, HitScanReflectAttemptEvent>(OnReflectUserHitscan);
}
private void OnUserHitscanReflectAttempt(Entity<ReflectUserComponent> user, ref HitScanReflectAttemptEvent args)
private void OnReflectUserHitscan(EntityUid uid, ReflectUserComponent component, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected)
return;
if (!UserCanReflect(user, out var bestReflectorUid))
return;
if (!TryReflectHitscan(user.Owner, bestReflectorUid.Value, args.Shooter, args.SourceItem, args.Direction, out var dir))
return;
args.Direction = dir.Value;
args.Reflected = true;
}
private void OnUserProjectileReflectAttempt(Entity<ReflectUserComponent> user, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryComp<ReflectiveComponent>(args.ProjUid, out var reflectiveComponent))
return;
if (!UserCanReflect(user, out var bestReflectorUid, (args.ProjUid, reflectiveComponent)))
return;
if (!TryReflectProjectile(user, bestReflectorUid.Value, (args.ProjUid, args.Component)))
return;
args.Cancelled = true;
}
private void OnObjectReflectHitscanAttempt(Entity<ReflectComponent> obj, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected || (obj.Comp.Reflects & args.Reflective) == 0x0)
return;
if (!TryReflectHitscan(obj, obj, args.Shooter, args.SourceItem, args.Direction, out var dir))
return;
args.Direction = dir.Value;
args.Reflected = true;
}
private void OnObjectReflectProjectileAttempt(Entity<ReflectComponent> obj, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
if (!TryReflectProjectile(obj, obj, (args.ProjUid, args.Component)))
return;
args.Cancelled = true;
}
/// <summary>
/// Can a user reflect something that's hit them? Returns true if so, and the best reflector available in the user's equipment.
/// </summary>
private bool UserCanReflect(Entity<ReflectUserComponent> user, [NotNullWhen(true)] out Entity<ReflectComponent>? bestReflector, Entity<ReflectiveComponent>? projectile = null)
{
bestReflector = null;
foreach (var entityUid in _inventorySystem.GetHandOrInventoryEntities(user.Owner, SlotFlags.WITHOUT_POCKET))
foreach (var ent in _inventorySystem.GetHandOrInventoryEntities(uid, SlotFlags.All & ~SlotFlags.POCKET))
{
if (!TryComp<ReflectComponent>(entityUid, out var comp))
if (!TryReflectHitscan(uid, ent, args.Shooter, args.SourceItem, args.Direction, out var dir))
continue;
if (!comp.Enabled)
args.Direction = dir.Value;
args.Reflected = true;
break;
}
}
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;
if (bestReflector != null && bestReflector.Value.Comp.ReflectProb >= comp.ReflectProb)
continue;
args.Cancelled = true;
break;
}
}
if (projectile != null && (comp.Reflects & projectile.Value.Comp.Reflective) == 0x0)
continue;
private void OnReflectCollide(EntityUid uid, ReflectComponent component, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
bestReflector = (entityUid, comp);
if (TryReflectProjectile(uid, uid, args.ProjUid, reflect: component))
args.Cancelled = true;
}
private bool TryReflectProjectile(EntityUid user, EntityUid reflector, EntityUid projectile, ProjectileComponent? projectileComp = null, ReflectComponent? reflect = null)
{
if (!Resolve(reflector, ref reflect, false) ||
!reflect.Enabled ||
!TryComp<ReflectiveComponent>(projectile, out var reflective) ||
(reflect.Reflects & reflective.Reflective) == 0x0 ||
!_random.Prob(reflect.ReflectProb) ||
!TryComp<PhysicsComponent>(projectile, out var physics))
{
return false;
}
return bestReflector != null;
}
private bool TryReflectProjectile(EntityUid user, Entity<ReflectComponent> reflector, Entity<ProjectileComponent> projectile)
{
if (
// Is it on?
!reflector.Comp.Enabled ||
// Is the projectile deflectable?
!TryComp<ReflectiveComponent>(projectile, out var reflective) ||
// Does the deflector deflect the type of projecitle?
(reflector.Comp.Reflects & reflective.Reflective) == 0x0 ||
// Is the projectile correctly set up with physics?
!TryComp<PhysicsComponent>(projectile, out var physics) ||
// If the user of the reflector is a mob with stamina, is it capable of deflecting?
TryComp<StaminaComponent>(user, out var staminaComponent) && staminaComponent.Critical ||
_standing.IsDown(reflector)
)
return false;
// If this dice roll fails, the shot isn't deflected
if (!_random.Prob(GetReflectChance(reflector)))
return false;
// Below handles what happens after being deflected.
var rotation = _random.NextAngle(-reflector.Comp.Spread / 2, reflector.Comp.Spread / 2).Opposite();
var rotation = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2).Opposite();
var existingVelocity = _physics.GetMapLinearVelocity(projectile, component: physics);
var relativeVelocity = existingVelocity - _physics.GetMapLinearVelocity(user);
var newVelocity = rotation.RotateVec(relativeVelocity);
@@ -182,52 +119,63 @@ public sealed class ReflectSystem : EntitySystem
if (_netManager.IsServer)
{
_popup.PopupEntity(Loc.GetString("reflect-shot"), user);
_audio.PlayPvs(reflector.Comp.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
_audio.PlayPvs(reflect.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
}
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectile.Comp.Weapon)} shot by {projectile.Comp.Shooter}");
if (Resolve(projectile, ref projectileComp, false))
{
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectileComp.Weapon)} shot by {projectileComp.Shooter}");
projectile.Comp.Shooter = user;
projectile.Comp.Weapon = user;
Dirty(projectile);
projectileComp.Shooter = user;
projectileComp.Weapon = user;
Dirty(projectile, projectileComp);
}
else
{
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)}");
}
return true;
}
private void OnReflectHitscan(EntityUid uid, ReflectComponent component, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected ||
(component.Reflects & args.Reflective) == 0x0)
{
return;
}
if (TryReflectHitscan(uid, uid, args.Shooter, args.SourceItem, args.Direction, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
}
private bool TryReflectHitscan(
EntityUid user,
Entity<ReflectComponent> reflector,
EntityUid reflector,
EntityUid? shooter,
EntityUid shotSource,
Vector2 direction,
[NotNullWhen(true)] out Vector2? newDirection)
{
if (
// Is the reflector enabled?
!reflector.Comp.Enabled ||
// If the user is a mob with stamina, is it capable of deflecting?
TryComp<StaminaComponent>(user, out var staminaComponent) && staminaComponent.Critical ||
_standing.IsDown(user))
if (!TryComp<ReflectComponent>(reflector, out var reflect) ||
!reflect.Enabled ||
!_random.Prob(reflect.ReflectProb))
{
newDirection = null;
return false;
}
// If this dice roll fails, the shot is not deflected.
if (!_random.Prob(GetReflectChance(reflector)))
{
newDirection = null;
return false;
}
// Below handles what happens after being deflected.
if (_netManager.IsServer)
{
_popup.PopupEntity(Loc.GetString("reflect-shot"), user);
_audio.PlayPvs(reflector.Comp.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
_audio.PlayPvs(reflect.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
}
var spread = _random.NextAngle(-reflector.Comp.Spread / 2, reflector.Comp.Spread / 2);
var spread = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2);
newDirection = -spread.RotateVec(direction);
if (shooter != null)
@@ -238,106 +186,52 @@ public sealed class ReflectSystem : EntitySystem
return true;
}
private float GetReflectChance(Entity<ReflectComponent> reflector)
{
/*
* The rules of deflection are as follows:
* If you innately reflect things via magic, biology etc., you always have a full chance.
* If you are standing up and standing still, you're prepared to deflect and have full chance.
* If you have velocity, your deflection chance depends on your velocity, clamped.
* If you are floating, your chance is the minimum value possible.
*/
if (reflector.Comp.Innate)
return reflector.Comp.ReflectProb;
if (_gravity.IsWeightless(reflector))
return reflector.Comp.MinReflectProb;
if (!TryComp<PhysicsComponent>(reflector, out var reflectorPhysics))
return reflector.Comp.ReflectProb;
return MathHelper.Lerp(
reflector.Comp.MinReflectProb,
reflector.Comp.ReflectProb,
// Inverse progression between velocities fed in as progression between probabilities. We go high -> low so the output here needs to be _inverted_.
1 - Math.Clamp((reflectorPhysics.LinearVelocity.Length() - reflector.Comp.VelocityBeforeNotMaxProb) / (reflector.Comp.VelocityBeforeMinProb - reflector.Comp.VelocityBeforeNotMaxProb), 0, 1)
);
}
private void OnReflectEquipped(Entity<ReflectComponent> reflector, ref GotEquippedEvent args)
private void OnReflectEquipped(EntityUid uid, ReflectComponent component, GotEquippedEvent args)
{
if (_gameTiming.ApplyingState)
return;
EnsureComp<ReflectUserComponent>(args.Equipee);
if (reflector.Comp.Enabled)
EnableAlert(args.Equipee);
}
private void OnReflectUnequipped(Entity<ReflectComponent> reflector, ref GotUnequippedEvent args)
private void OnReflectUnequipped(EntityUid uid, ReflectComponent comp, GotUnequippedEvent args)
{
RefreshReflectUser(args.Equipee);
}
private void OnReflectHandEquipped(Entity<ReflectComponent> reflector, ref GotEquippedHandEvent args)
private void OnReflectHandEquipped(EntityUid uid, ReflectComponent component, GotEquippedHandEvent args)
{
if (_gameTiming.ApplyingState)
return;
EnsureComp<ReflectUserComponent>(args.User);
if (reflector.Comp.Enabled)
EnableAlert(args.User);
}
private void OnReflectHandUnequipped(Entity<ReflectComponent> reflector, ref GotUnequippedHandEvent args)
private void OnReflectHandUnequipped(EntityUid uid, ReflectComponent component, GotUnequippedHandEvent args)
{
RefreshReflectUser(args.User);
}
private void OnToggleReflect(Entity<ReflectComponent> reflector, ref ItemToggledEvent args)
private void OnToggleReflect(EntityUid uid, ReflectComponent comp, ref ItemToggledEvent args)
{
reflector.Comp.Enabled = args.Activated;
Dirty(reflector);
if (args.User == null)
return;
if (reflector.Comp.Enabled)
EnableAlert(args.User.Value);
else
DisableAlert(args.User.Value);
comp.Enabled = args.Activated;
Dirty(uid, comp);
}
/// <summary>
/// Refreshes whether someone has reflection potential, so we can raise directed events on them.
/// 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.WITHOUT_POCKET))
foreach (var ent in _inventorySystem.GetHandOrInventoryEntities(user, SlotFlags.All & ~SlotFlags.POCKET))
{
if (!HasComp<ReflectComponent>(ent))
continue;
EnsureComp<ReflectUserComponent>(user);
EnableAlert(user);
return;
}
RemCompDeferred<ReflectUserComponent>(user);
DisableAlert(user);
}
private void EnableAlert(EntityUid alertee)
{
_alerts.ShowAlert(alertee, DeflectingAlert);
}
private void DisableAlert(EntityUid alertee)
{
_alerts.ClearAlert(alertee, DeflectingAlert);
}
}