Files

233 lines
8.8 KiB
C#
Raw Permalink Normal View History

using System.Diagnostics.CodeAnalysis;
using System.Numerics;
2023-06-01 00:57:31 -05:00
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
2023-08-25 12:48:27 +10:00
using Content.Shared.Hands;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
item toggling giga rework + full ninja refactor (#28039) * item toggle refactoring and some new systems * add ToggleClothing component/system * unhardcode magboots gravity logic * make magboots and speedboots use ItemToggle and stuff * remove now useless clothing components * update client/server magboots systems * add note to use ItemToggledEvent in ToggleActionEvent doc * refactor PowerCellDraw to use ItemToggle for ui open/close control * add TryUseCharges, refactor charges system * update magboot trigger code * make borg use ItemToggle, network SelectedModule instead of now removed Activated * add AccessToggle for borg * the giga ninja refactor * update ninja yml * update ItemToggle usage for some stuff * fix activatableui requires power * random fixing * yaml fixing * nuke ItemToggleDisarmMalus * make defib use ItemToggle * make things that use power not turn on if missing use charge * pro * fix sound prediction * bruh * proximity detector use ItemToggle * oop * big idiot syndrome * fix ninja spawn rule and make it generic * fix ninja spawn rule yml * move loading profiles into AntagLoadProfileRule * more ninja refactor * ninja yml fixes * the dreaded copy paste ops * remove useless NinjaRuleComponent and ue AntagSelection for greeting * fix invisibility * move IsCompleted to SharedObjectivesSystem * ability fixes * oop fix powercell instantly draining itself * sentient speedboots gaming * make reflect use ItemToggle * fix other test * loadprofilerule moved into its own pr * remove conflict with dragon refactor * remove all GenericAntag code from ninja * ) * probably * remove old enabled * great language bravo vince * GREAT LANGUAGE * who made this language * because it stinks * reparent blood-red magboots to magboots probbbly works * most of the review stuff * hasGrav doesnt mean what i thought it did * make health analyzer use itemtoggle, not fail test * fix mag/speed boots being wacky * UNTROLL * add ItemToggle to the random health analyzers * a * remove unused obsolete borg func * untrolling * :trollface: * fix test * fix * g * untroll --------- Co-authored-by: deltanedas <@deltanedas:kde.org>
2024-07-11 05:55:56 +00:00
using Content.Shared.Item.ItemToggle;
using Content.Shared.Popups;
2023-05-15 15:21:05 +10:00
using Content.Shared.Projectiles;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events;
using Robust.Shared.Audio.Systems;
2023-05-15 15:21:05 +10:00
using Robust.Shared.Network;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Random;
using Content.Shared.Examine;
using Content.Shared.Localizations;
namespace Content.Shared.Weapons.Reflect;
/// <summary>
/// This handles reflecting projectiles and hitscan shots.
/// </summary>
public sealed class ReflectSystem : EntitySystem
{
2023-05-15 15:21:05 +10:00
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
2023-06-01 00:57:31 -05:00
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
item toggling giga rework + full ninja refactor (#28039) * item toggle refactoring and some new systems * add ToggleClothing component/system * unhardcode magboots gravity logic * make magboots and speedboots use ItemToggle and stuff * remove now useless clothing components * update client/server magboots systems * add note to use ItemToggledEvent in ToggleActionEvent doc * refactor PowerCellDraw to use ItemToggle for ui open/close control * add TryUseCharges, refactor charges system * update magboot trigger code * make borg use ItemToggle, network SelectedModule instead of now removed Activated * add AccessToggle for borg * the giga ninja refactor * update ninja yml * update ItemToggle usage for some stuff * fix activatableui requires power * random fixing * yaml fixing * nuke ItemToggleDisarmMalus * make defib use ItemToggle * make things that use power not turn on if missing use charge * pro * fix sound prediction * bruh * proximity detector use ItemToggle * oop * big idiot syndrome * fix ninja spawn rule and make it generic * fix ninja spawn rule yml * move loading profiles into AntagLoadProfileRule * more ninja refactor * ninja yml fixes * the dreaded copy paste ops * remove useless NinjaRuleComponent and ue AntagSelection for greeting * fix invisibility * move IsCompleted to SharedObjectivesSystem * ability fixes * oop fix powercell instantly draining itself * sentient speedboots gaming * make reflect use ItemToggle * fix other test * loadprofilerule moved into its own pr * remove conflict with dragon refactor * remove all GenericAntag code from ninja * ) * probably * remove old enabled * great language bravo vince * GREAT LANGUAGE * who made this language * because it stinks * reparent blood-red magboots to magboots probbbly works * most of the review stuff * hasGrav doesnt mean what i thought it did * make health analyzer use itemtoggle, not fail test * fix mag/speed boots being wacky * UNTROLL * add ItemToggle to the random health analyzers * a * remove unused obsolete borg func * untrolling * :trollface: * fix test * fix * g * untroll --------- Co-authored-by: deltanedas <@deltanedas:kde.org>
2024-07-11 05:55:56 +00:00
[Dependency] private readonly ItemToggleSystem _toggle = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
Subs.SubscribeWithRelay<ReflectComponent, ProjectileReflectAttemptEvent>(OnReflectUserCollide, baseEvent: false);
Subs.SubscribeWithRelay<ReflectComponent, HitScanReflectAttemptEvent>(OnReflectUserHitscan, baseEvent: false);
SubscribeLocalEvent<ReflectComponent, ProjectileReflectAttemptEvent>(OnReflectCollide);
SubscribeLocalEvent<ReflectComponent, HitScanReflectAttemptEvent>(OnReflectHitscan);
SubscribeLocalEvent<ReflectComponent, GotEquippedEvent>(OnReflectEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedEvent>(OnReflectUnequipped);
2023-08-25 12:48:27 +10:00
SubscribeLocalEvent<ReflectComponent, GotEquippedHandEvent>(OnReflectHandEquipped);
SubscribeLocalEvent<ReflectComponent, GotUnequippedHandEvent>(OnReflectHandUnequipped);
SubscribeLocalEvent<ReflectComponent, ExaminedEvent>(OnExamine);
}
private void OnReflectUserCollide(Entity<ReflectComponent> ent, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
if (!ent.Comp.InRightPlace)
return; // only reflect when equipped correctly
if (TryReflectProjectile(ent, ent.Owner, args.ProjUid))
args.Cancelled = true;
}
private void OnReflectUserHitscan(Entity<ReflectComponent> ent, ref HitScanReflectAttemptEvent args)
{
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
if (!ent.Comp.InRightPlace)
return; // only reflect when equipped correctly
if (TryReflectHitscan(ent, ent.Owner, args.Shooter, args.SourceItem, args.Direction, args.Reflective, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
}
private void OnReflectCollide(Entity<ReflectComponent> ent, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;
if (TryReflectProjectile(ent, ent.Owner, args.ProjUid))
args.Cancelled = true;
}
private void OnReflectHitscan(Entity<ReflectComponent> ent, ref HitScanReflectAttemptEvent args)
{
if (args.Reflected)
return;
2023-04-19 20:02:30 +10:00
if (TryReflectHitscan(ent, ent.Owner, args.Shooter, args.SourceItem, args.Direction, args.Reflective, out var dir))
{
args.Direction = dir.Value;
args.Reflected = true;
}
}
private bool TryReflectProjectile(Entity<ReflectComponent> reflector, EntityUid user, Entity<ProjectileComponent?> projectile)
{
if (!TryComp<ReflectiveComponent>(projectile, out var reflective) ||
(reflector.Comp.Reflects & reflective.Reflective) == 0x0 ||
!_toggle.IsActivated(reflector.Owner) ||
!_random.Prob(reflector.Comp.ReflectProb) ||
!TryComp<PhysicsComponent>(projectile, out var physics))
{
2023-04-19 20:02:30 +10:00
return false;
}
var rotation = _random.NextAngle(-reflector.Comp.Spread / 2, reflector.Comp.Spread / 2).Opposite();
2023-04-19 20:02:30 +10:00
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-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-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());
PlayAudioAndPopup(reflector.Comp, user);
2023-05-15 15:21:05 +10:00
if (Resolve(projectile, ref projectile.Comp, false))
{
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)} from {ToPrettyString(projectile.Comp.Weapon)} shot by {projectile.Comp.Shooter}");
2023-06-01 00:57:31 -05:00
projectile.Comp.Shooter = user;
projectile.Comp.Weapon = user;
Dirty(projectile, projectile.Comp);
}
else
{
_adminLogger.Add(LogType.BulletHit, LogImpact.Medium, $"{ToPrettyString(user)} reflected {ToPrettyString(projectile)}");
}
2023-05-15 15:21:05 +10:00
2023-04-19 20:02:30 +10:00
return true;
}
2023-08-25 12:48:27 +10:00
private bool TryReflectHitscan(
Entity<ReflectComponent> reflector,
2023-08-25 12:48:27 +10:00
EntityUid user,
EntityUid? shooter,
EntityUid shotSource,
Vector2 direction,
ReflectType hitscanReflectType,
2023-05-15 15:21:05 +10:00
[NotNullWhen(true)] out Vector2? newDirection)
{
if ((reflector.Comp.Reflects & hitscanReflectType) == 0x0 ||
!_toggle.IsActivated(reflector.Owner) ||
!_random.Prob(reflector.Comp.ReflectProb))
Weapon Reflection Movement Mechanic (#27219) * Weapon Reflection Movement Mechanic Adds a movement mechanic to deflection. Standing still gives you your best chance of deflecting a shot. Moving lowers this to 2/3rds. Sprinting to 1/3rd. This allows for robust players to express better and provides counterplay to someone finding a goober-strong deflection weapon, giving more design space. As part of this PR I've also touched the numbers of a few swords, shields, etc. and modified some descriptions to make them read better. The balance numbers are not remotely final, but as intent: 1. All the sidearm swords (katana, cutlass, captain's sabre) have the same damage. There's no good reason the "ceremonial" blade the captain has doing more damage than a katana. 2. The Captain's Sabre has a 30% reflect chance, dropping to 20% when moving and 10% when sprinting. This one is controversial due to the recent nerf, I suspect: This could easily be 15->10->5? 3. The Energy Katana has a flat 30% reflect chance. 4. The meme Throngler has a 30% reflect chance, dropping to 20% when moving and 10% when sprinting. 5. The E-Sword has a 30% reflect chance, dropping to 20% when moving and 10% when sprinting. 6. The Double E-Sword has a mighty 75% reflect chance, dropping to 50% and then 25%. 7. Both reflective shields - Mirror and Energy - have a 95% deflect chance, dropping to 63% then 31%. * Resolve PR comments. * Weh? * Reign in double esword a tad * Shield nerfs no longer real * Improve Mirror Cult desc * Simple alert for deflection! No art yet. * Added a new icon for deflecting
2024-05-07 19:14:58 +01:00
{
newDirection = null;
return false;
}
PlayAudioAndPopup(reflector.Comp, user);
2023-04-19 20:02:30 +10:00
var spread = _random.NextAngle(-reflector.Comp.Spread / 2, reflector.Comp.Spread / 2);
2023-05-15 15:21:05 +10:00
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;
}
private void PlayAudioAndPopup(ReflectComponent reflect, EntityUid user)
{
// Can probably be changed for prediction
if (_netManager.IsServer)
{
_popup.PopupEntity(Loc.GetString("reflect-shot"), user);
_audio.PlayPvs(reflect.SoundOnReflect, user);
}
2023-08-25 12:48:27 +10:00
}
private void OnReflectEquipped(Entity<ReflectComponent> ent, ref GotEquippedEvent args)
2023-08-25 12:48:27 +10:00
{
ent.Comp.InRightPlace = (ent.Comp.SlotFlags & args.SlotFlags) == args.SlotFlags;
Dirty(ent);
2023-08-25 12:48:27 +10:00
}
private void OnReflectUnequipped(Entity<ReflectComponent> ent, ref GotUnequippedEvent args)
2023-08-25 12:48:27 +10:00
{
ent.Comp.InRightPlace = false;
Dirty(ent);
2023-08-25 12:48:27 +10:00
}
private void OnReflectHandEquipped(Entity<ReflectComponent> ent, ref GotEquippedHandEvent args)
{
ent.Comp.InRightPlace = ent.Comp.ReflectingInHands;
Dirty(ent);
}
private void OnReflectHandUnequipped(Entity<ReflectComponent> ent, ref GotUnequippedHandEvent args)
2023-08-25 12:48:27 +10:00
{
ent.Comp.InRightPlace = false;
Dirty(ent);
}
#region Examine
private void OnExamine(Entity<ReflectComponent> ent, ref ExaminedEvent args)
{
// This isn't examine verb or something just because it looks too much bad.
// Trust me, universal verb for the potential weapons, armor and walls looks awful.
var value = MathF.Round(ent.Comp.ReflectProb * 100, 1);
if (!_toggle.IsActivated(ent.Owner) || value == 0 || ent.Comp.Reflects == ReflectType.None)
return;
var compTypes = ent.Comp.Reflects.ToString().Split(", ");
List<string> typeList = new(compTypes.Length);
for (var i = 0; i < compTypes.Length; i++)
{
var type = Loc.GetString(("reflect-component-" + compTypes[i]).ToLower());
typeList.Add(type);
}
var msg = ContentLocalizationManager.FormatList(typeList);
args.PushMarkup(Loc.GetString("reflect-component-examine", ("value", value), ("type", msg)));
}
#endregion
}