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 sealed class GotEquippedHandEvent : EquippedHandEvent
{ {
public GotEquippedHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { } 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 sealed class GotUnequippedHandEvent : UnequippedHandEvent
{ {
public GotUnequippedHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { } 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 sealed class DidEquipHandEvent : EquippedHandEvent
{ {
public DidEquipHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { } 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 sealed class DidUnequipHandEvent : UnequippedHandEvent
{ {
public DidUnequipHandEvent(EntityUid user, EntityUid unequipped, Hand hand) : base(user, unequipped, hand) { } 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 sealed class DidEquipEvent : EquippedEventBase
{ {
public DidEquipEvent(EntityUid equipee, EntityUid equipment, SlotDefinition slotDefinition) : base(equipee, equipment, slotDefinition) 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 sealed class GotEquippedEvent : EquippedEventBase
{ {
public GotEquippedEvent(EntityUid equipee, EntityUid equipment, SlotDefinition slotDefinition) : base(equipee, equipment, slotDefinition) public GotEquippedEvent(EntityUid equipee, EntityUid equipment, SlotDefinition slotDefinition) : base(equipee, equipment, slotDefinition)

View File

@@ -1,11 +1,43 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using Content.Shared.Hands.Components;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Shared.Inventory; namespace Content.Shared.Inventory;
public partial class InventorySystem 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> /// <summary>
/// Returns the definition of the inventory slot that the given entity is currently in.. /// Returns the definition of the inventory slot that the given entity is currently in..
/// </summary> /// </summary>

View File

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

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 Heat: 0.4 # this technically means it protects against fires pretty well? -heat is just for lasers and stuff, not atmos temperature
- type: Reflect - type: Reflect
reflectProb: 1 reflectProb: 1
reflects:
- Energy
- type: entity - type: entity
parent: ClothingOuterBaseLarge parent: ClothingOuterBaseLarge