Reflection refactor (#19253)

This commit is contained in:
metalgearsloth
2023-08-25 12:48:27 +10:00
committed by GitHub
parent 67580e08bf
commit ed81d1a4da
7 changed files with 146 additions and 74 deletions

View File

@@ -206,21 +206,33 @@ namespace Content.Shared.Hands
}
}
/// <summary>
/// Raised directed on an entity when it is equipped into hands.
/// </summary>
public sealed class GotEquippedHandEvent : EquippedHandEvent
{
public GotEquippedHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { }
}
/// <summary>
/// Raised directed on an entity when it is unequipped from hands.
/// </summary>
public sealed class GotUnequippedHandEvent : UnequippedHandEvent
{
public GotUnequippedHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { }
}
/// <summary>
/// Raised directed on a user when it picks something up.
/// </summary>
public sealed class DidEquipHandEvent : EquippedHandEvent
{
public DidEquipHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { }
}
/// <summary>
/// Raised directed on a user when something leaves its hands.
/// </summary>
public sealed class DidUnequipHandEvent : UnequippedHandEvent
{
public DidUnequipHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { }

View File

@@ -37,6 +37,9 @@ public abstract class EquippedEventBase : EntityEventArgs
}
}
/// <summary>
/// Raised directed on an equipee when something is equipped.
/// </summary>
public sealed class DidEquipEvent : EquippedEventBase
{
public DidEquipEvent(EntityUid equipee, EntityUid equipment, SlotDefinition slotDefinition) : base(equipee, equipment, slotDefinition)
@@ -44,6 +47,9 @@ public sealed class DidEquipEvent : EquippedEventBase
}
}
/// <summary>
/// Raised directed on equipment when it's equipped to an equipee
/// </summary>
public sealed class GotEquippedEvent : EquippedEventBase
{
public GotEquippedEvent(EntityUid equipee, EntityUid equipment, SlotDefinition slotDefinition) : base(equipee, equipment, slotDefinition)

View File

@@ -1,11 +1,43 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Hands.Components;
using Robust.Shared.Prototypes;
namespace Content.Shared.Inventory;
public partial class InventorySystem
{
/// <summary>
/// Yields all entities in hands or inventory slots with the specific flags.
/// </summary>
public IEnumerable<EntityUid> GetHandOrInventoryEntities(EntityUid user, SlotFlags flags = SlotFlags.All)
{
if (TryComp<HandsComponent>(user, out var handsComp))
{
foreach (var hand in handsComp.Hands.Values)
{
if (hand.HeldEntity == null)
continue;
yield return hand.HeldEntity.Value;
}
}
if (TryComp<InventoryComponent>(user, out var inventoryComp))
{
var slotEnumerator = new ContainerSlotEnumerator(user, inventoryComp.TemplateId,
_prototypeManager, this, flags);
while (slotEnumerator.MoveNext(out var slot))
{
if (slot.ContainedEntity == null)
continue;
yield return slot.ContainedEntity.Value;
}
}
}
/// <summary>
/// Returns the definition of the inventory slot that the given entity is currently in..
/// </summary>

View File

@@ -1,3 +1,5 @@
using Content.Shared.Hands.Components;
namespace Content.Shared.Inventory;
public partial class InventorySystem

View File

@@ -0,0 +1,13 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Weapons.Reflect;
/// <summary>
/// Added to an entity if it equips a reflection item in a hand slot or into its clothing.
/// Reflection events will then be relayed.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class ReflectUserComponent : Component
{
}

View File

@@ -3,16 +3,19 @@ using System.Numerics;
using Content.Shared.Administration.Logs;
using Content.Shared.Audio;
using Content.Shared.Database;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Weapons.Ranged.Events;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Item;
using Robust.Shared.Physics.Components;
using Content.Shared.Popups;
using Content.Shared.Projectiles;
using Content.Shared.Weapons.Ranged.Components;
using Robust.Shared.Network;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Shared.Weapons.Reflect;
@@ -23,6 +26,7 @@ namespace Content.Shared.Weapons.Reflect;
public abstract class SharedReflectSystem : EntitySystem
{
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
@@ -35,37 +39,55 @@ public abstract class SharedReflectSystem : EntitySystem
{
base.Initialize();
SubscribeLocalEvent<HandsComponent, ProjectileReflectAttemptEvent>(OnHandReflectProjectile);
SubscribeLocalEvent<HandsComponent, HitScanReflectAttemptEvent>(OnHandsReflectHitscan);
SubscribeLocalEvent<ReflectComponent, ProjectileReflectAttemptEvent>(OnReflectCollide);
SubscribeLocalEvent<ReflectComponent, HitScanReflectAttemptEvent>(OnReflectHitscan);
SubscribeLocalEvent<ReflectUserComponent, ProjectileReflectAttemptEvent>(OnReflectUserCollide);
SubscribeLocalEvent<ReflectUserComponent, HitScanReflectAttemptEvent>(OnReflectUserHitscan);
SubscribeLocalEvent<ReflectComponent, GotEquippedEvent>(OnReflectEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedEvent>(OnReflectUnequipped);
SubscribeLocalEvent<ReflectComponent, GotEquippedHandEvent>(OnReflectHandEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedHandEvent>(OnReflectHandUnequipped);
}
private void OnReflectUserHitscan(EntityUid uid, ReflectUserComponent component, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected)
return;
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;
}
}
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;
args.Cancelled = true;
break;
}
}
private void OnReflectCollide(EntityUid uid, ReflectComponent component, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
{
return;
}
if (TryReflectProjectile(uid, args.ProjUid, reflect: component))
if (TryReflectProjectile(uid, uid, args.ProjUid, reflect: component))
args.Cancelled = true;
}
private void OnHandReflectProjectile(EntityUid uid, HandsComponent hands, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
if (hands.ActiveHandEntity != null && TryReflectProjectile(hands.ActiveHandEntity.Value, args.ProjUid))
args.Cancelled = true;
}
private bool TryReflectProjectile(EntityUid reflector, EntityUid projectile, ProjectileComponent? projectileComp = null, ReflectComponent? reflect = null)
private bool TryReflectProjectile(EntityUid user, EntityUid reflector, EntityUid projectile, ProjectileComponent? projectileComp = null, ReflectComponent? reflect = null)
{
if (!Resolve(reflector, ref reflect, false) ||
!reflect.Enabled ||
@@ -79,7 +101,7 @@ public abstract class SharedReflectSystem : EntitySystem
var rotation = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2).Opposite();
var existingVelocity = _physics.GetMapLinearVelocity(projectile, component: physics);
var relativeVelocity = existingVelocity - _physics.GetMapLinearVelocity(reflector);
var relativeVelocity = existingVelocity - _physics.GetMapLinearVelocity(user);
var newVelocity = rotation.RotateVec(relativeVelocity);
// Have the velocity in world terms above so need to convert it back to local.
@@ -93,38 +115,26 @@ public abstract class SharedReflectSystem : EntitySystem
if (_netManager.IsServer)
{
_popup.PopupEntity(Loc.GetString("reflect-shot"), reflector);
_audio.PlayPvs(reflect.SoundOnReflect, reflector, AudioHelpers.WithVariation(0.05f, _random));
_popup.PopupEntity(Loc.GetString("reflect-shot"), user);
_audio.PlayPvs(reflect.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
}
if (Resolve(projectile, ref projectileComp, false))
{
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(reflector)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectileComp.Weapon)} shot by {projectileComp.Shooter}");
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectileComp.Weapon)} shot by {projectileComp.Shooter}");
projectileComp.Shooter = reflector;
projectileComp.Weapon = reflector;
Dirty(projectileComp);
projectileComp.Shooter = user;
projectileComp.Weapon = user;
Dirty(projectile, projectileComp);
}
else
{
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(reflector)} reflected {ToPrettyString(projectile)}");
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)}");
}
return true;
}
private void OnHandsReflectHitscan(EntityUid uid, HandsComponent hands, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected || hands.ActiveHandEntity == null)
return;
if (TryReflectHitscan(hands.ActiveHandEntity.Value, args.Shooter, args.SourceItem, args.Direction, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
}
private void OnReflectHitscan(EntityUid uid, ReflectComponent component, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected ||
@@ -133,14 +143,19 @@ public abstract class SharedReflectSystem : EntitySystem
return;
}
if (TryReflectHitscan(uid, args.Shooter, args.SourceItem, args.Direction, out var dir))
if (TryReflectHitscan(uid, uid, args.Shooter, args.SourceItem, args.Direction, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
}
private bool TryReflectHitscan(EntityUid reflector, EntityUid? shooter, EntityUid shotSource, Vector2 direction,
private bool TryReflectHitscan(
EntityUid user,
EntityUid reflector,
EntityUid? shooter,
EntityUid shotSource,
Vector2 direction,
[NotNullWhen(true)] out Vector2? newDirection)
{
if (!TryComp<ReflectComponent>(reflector, out var reflect) ||
@@ -153,65 +168,55 @@ public abstract class SharedReflectSystem : EntitySystem
if (_netManager.IsServer)
{
_popup.PopupEntity(Loc.GetString("reflect-shot"), reflector);
_audio.PlayPvs(reflect.SoundOnReflect, reflector, AudioHelpers.WithVariation(0.05f, _random));
_popup.PopupEntity(Loc.GetString("reflect-shot"), user);
_audio.PlayPvs(reflect.SoundOnReflect, user, AudioHelpers.WithVariation(0.05f, _random));
}
var spread = _random.NextAngle(-reflect.Spread / 2, reflect.Spread / 2);
newDirection = -spread.RotateVec(direction);
if (shooter != null)
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(reflector)} reflected hitscan from {ToPrettyString(shotSource)} shot by {ToPrettyString(shooter.Value)}");
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)} shot by {ToPrettyString(shooter.Value)}");
else
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(reflector)} reflected hitscan from {ToPrettyString(shotSource)}");
_adminLogger.Add(LogType.HitScanHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected hitscan from {ToPrettyString(shotSource)}");
return true;
}
private void OnReflectEquipped(EntityUid uid, ReflectComponent comp, GotEquippedEvent args)
private void OnReflectEquipped(EntityUid uid, ReflectComponent component, GotEquippedEvent args)
{
if (!TryComp(args.Equipee, out ReflectComponent? reflection))
return;
if (args.Slot == "pocket1" || args.Slot == "pocket2")
return;
reflection.Enabled = comp.Enabled;
// reflection probability should be: (1 - old probability) * newly-equipped item probability + old probability
// example: if entity has .25 reflection and newly-equipped item has .7, entity should have (1 - .25) * .7 + .25 = .775
reflection.ReflectProb += (1 - reflection.ReflectProb) * comp.ReflectProb;
EnsureComp<ReflectUserComponent>(args.Equipee);
}
private void OnReflectUnequipped(EntityUid uid, ReflectComponent comp, GotUnequippedEvent args)
{
if (!TryComp(args.Equipee, out ReflectComponent? reflection))
return;
if (!_inventorySystem.TryGetSlots(args.Equipee, out var slotDef))
return;
// you could recalculate reflectprob with new = (old - component) / (1 - component), but component=1 introduces loss
// still need to either maintain a counter or loop through all slots to determine reflection.enabled anyway?
float newProb = 1;
var reflecting = false;
foreach (var slot in slotDef)
{
if (!_inventorySystem.TryGetSlotEntity(args.Equipee, slot.Name, out var slotEnt))
continue;
if (!TryComp(slotEnt, out ReflectComponent? refcomp))
continue;
reflecting = true;
var prob = refcomp.ReflectProb;
newProb -= newProb * prob;
RefreshReflectUser(args.Equipee);
}
reflection.ReflectProb = 1 - newProb;
reflection.Enabled = reflecting;
private void OnReflectHandEquipped(EntityUid uid, ReflectComponent component, GotEquippedHandEvent args)
{
EnsureComp<ReflectUserComponent>(args.User);
}
private void OnReflectHandUnequipped(EntityUid uid, ReflectComponent component, GotUnequippedHandEvent args)
{
RefreshReflectUser(args.User);
}
/// <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))
{
if (!HasComp<ReflectComponent>(ent))
continue;
EnsureComp<ReflectUserComponent>(user);
return;
}
RemCompDeferred<ReflectUserComponent>(user);
}
}

View File

@@ -96,6 +96,8 @@
Heat: 0.4 # this technically means it protects against fires pretty well? -heat is just for lasers and stuff, not atmos temperature
- type: Reflect
reflectProb: 1
reflects:
- Energy
- type: entity
parent: ClothingOuterBaseLarge