| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- using System.Numerics;
- using Content.Shared._RMC14.Weapons.Ranged.Prediction;
- using Content.Shared.Administration.Logs;
- using Content.Shared.Camera;
- using Content.Shared.CombatMode.Pacification;
- using Content.Shared.Damage;
- using Content.Shared.Database;
- using Content.Shared.DoAfter;
- using Content.Shared.Effects;
- using Content.Shared.Hands.EntitySystems;
- using Content.Shared.Interaction;
- using Content.Shared.Throwing;
- using Content.Shared.Weapons.Ranged.Systems;
- using Robust.Shared.Audio.Systems;
- using Robust.Shared.Map;
- using Robust.Shared.Network;
- using Robust.Shared.Physics;
- using Robust.Shared.Physics.Components;
- using Robust.Shared.Physics.Events;
- using Robust.Shared.Physics.Systems;
- using Robust.Shared.Player;
- using Robust.Shared.Serialization;
- using Content.Shared.Barricade;
- using Robust.Shared.Random;
- namespace Content.Shared.Projectiles;
- public abstract partial class SharedProjectileSystem : EntitySystem
- {
- public const string ProjectileFixture = "projectile";
- [Dependency] private readonly INetManager _netManager = default!;
- [Dependency] private readonly SharedAudioSystem _audio = default!;
- [Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
- [Dependency] private readonly SharedHandsSystem _hands = default!;
- [Dependency] private readonly SharedPhysicsSystem _physics = default!;
- [Dependency] private readonly SharedTransformSystem _transform = default!;
- [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
- [Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
- [Dependency] private readonly DamageableSystem _damageableSystem = default!;
- [Dependency] private readonly SharedGunSystem _guns = default!;
- [Dependency] private readonly SharedCameraRecoilSystem _sharedCameraRecoil = default!;
- [Dependency] private readonly IRobustRandom _random = default!;
- public override void Initialize()
- {
- base.Initialize();
- SubscribeLocalEvent<ProjectileComponent, StartCollideEvent>(OnStartCollide);
- SubscribeLocalEvent<ProjectileComponent, PreventCollideEvent>(PreventCollision);
- SubscribeLocalEvent<EmbeddableProjectileComponent, ProjectileHitEvent>(OnEmbedProjectileHit);
- SubscribeLocalEvent<EmbeddableProjectileComponent, ThrowDoHitEvent>(OnEmbedThrowDoHit);
- SubscribeLocalEvent<EmbeddableProjectileComponent, ActivateInWorldEvent>(OnEmbedActivate);
- SubscribeLocalEvent<EmbeddableProjectileComponent, RemoveEmbeddedProjectileEvent>(OnEmbedRemove);
- }
- private void OnStartCollide(EntityUid uid, ProjectileComponent component, ref StartCollideEvent args)
- {
- // This is so entities that shouldn't get a collision are ignored.
- if (args.OurFixtureId != ProjectileFixture || !args.OtherFixture.Hard
- || component.DamagedEntity || component is { Weapon: null, OnlyCollideWhenShot: true })
- return;
- ProjectileCollide((uid, component, args.OurBody), args.OtherEntity);
- }
- public void ProjectileCollide(Entity<ProjectileComponent, PhysicsComponent> projectile, EntityUid target, bool predicted = false)
- {
- var (uid, component, ourBody) = projectile;
- if (projectile.Comp1.DamagedEntity)
- {
- if (_netManager.IsServer && component.DeleteOnCollide)
- QueueDel(uid);
- return;
- }
- // it's here so this check is only done once before possible hit
- var attemptEv = new ProjectileReflectAttemptEvent(uid, component, false);
- RaiseLocalEvent(target, ref attemptEv);
- if (attemptEv.Cancelled)
- {
- SetShooter(uid, component, target);
- return;
- }
- var ev = new ProjectileHitEvent(component.Damage, target, component.Shooter);
- RaiseLocalEvent(uid, ref ev);
- if (ev.Handled)
- return;
- var coordinates = Transform(projectile).Coordinates;
- var otherName = ToPrettyString(target);
- var direction = ourBody.LinearVelocity.Normalized();
- var modifiedDamage = _netManager.IsServer
- ? _damageableSystem.TryChangeDamage(target,
- ev.Damage,
- component.IgnoreResistances,
- origin: component.Shooter)
- : new DamageSpecifier(ev.Damage);
- var deleted = Deleted(target);
- var filter = Filter.Pvs(coordinates, entityMan: EntityManager);
- if (_guns.GunPrediction && TryComp(projectile, out PredictedProjectileServerComponent? serverProjectile))
- filter = filter.RemovePlayer(serverProjectile.Shooter);
- if (modifiedDamage is not null && (EntityManager.EntityExists(component.Shooter) || EntityManager.EntityExists(component.Weapon)))
- {
- if (modifiedDamage.AnyPositive() && !deleted)
- {
- _color.RaiseEffect(Color.Red, new List<EntityUid> { target }, filter);
- }
- var shooterOrWeapon = EntityManager.EntityExists(component.Shooter) ? component.Shooter!.Value : component.Weapon!.Value;
- _adminLogger.Add(LogType.BulletHit,
- HasComp<ActorComponent>(target) ? LogImpact.Extreme : LogImpact.High,
- $"Projectile {ToPrettyString(uid):projectile} shot by {ToPrettyString(shooterOrWeapon):source} hit {otherName:target} and dealt {modifiedDamage.GetTotal():damage} damage");
- }
- if (!deleted)
- {
- _guns.PlayImpactSound(target, modifiedDamage, component.SoundHit, component.ForceSound, filter, projectile);
- //_sharedCameraRecoil.KickCamera(target, direction); # this makes people blind or something
- }
- component.DamagedEntity = true;
- Dirty(uid, component);
- if (!predicted && component.DeleteOnCollide && (_netManager.IsServer || IsClientSide(uid)))
- QueueDel(uid);
- else if (_netManager.IsServer && component.DeleteOnCollide)
- {
- var predictedComp = EnsureComp<PredictedProjectileHitComponent>(uid);
- predictedComp.Origin = _transform.GetMoverCoordinates(coordinates);
- var targetCoords = _transform.GetMoverCoordinates(target);
- if (predictedComp.Origin.TryDistance(EntityManager, _transform, targetCoords, out var distance))
- predictedComp.Distance = distance;
- Dirty(uid, predictedComp);
- }
- if ((_netManager.IsServer || IsClientSide(uid)) && component.ImpactEffect != null)
- {
- var impactEffectEv = new ImpactEffectEvent(component.ImpactEffect, GetNetCoordinates(coordinates));
- if (_netManager.IsServer)
- RaiseNetworkEvent(impactEffectEv, filter);
- else
- RaiseLocalEvent(impactEffectEv);
- }
- }
- private void OnEmbedActivate(EntityUid uid, EmbeddableProjectileComponent component, ActivateInWorldEvent args)
- {
- // Nuh uh
- if (component.RemovalTime == null)
- return;
- if (args.Handled || !args.Complex || !TryComp<PhysicsComponent>(uid, out var physics) || physics.BodyType != BodyType.Static)
- return;
- args.Handled = true;
- _doAfter.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, component.RemovalTime.Value,
- new RemoveEmbeddedProjectileEvent(), eventTarget: uid, target: uid));
- }
- private void OnEmbedRemove(EntityUid uid, EmbeddableProjectileComponent component, RemoveEmbeddedProjectileEvent args)
- {
- // Whacky prediction issues.
- if (args.Cancelled || _netManager.IsClient)
- return;
- if (component.DeleteOnRemove)
- {
- QueueDel(uid);
- return;
- }
- var xform = Transform(uid);
- TryComp<PhysicsComponent>(uid, out var physics);
- _physics.SetBodyType(uid, BodyType.Dynamic, body: physics, xform: xform);
- _transform.AttachToGridOrMap(uid, xform);
- // Reset whether the projectile has damaged anything if it successfully was removed
- if (TryComp<ProjectileComponent>(uid, out var projectile))
- {
- projectile.Shooter = null;
- projectile.Weapon = null;
- projectile.DamagedEntity = false;
- }
- // Land it just coz uhhh yeah
- var landEv = new LandEvent(args.User, true);
- RaiseLocalEvent(uid, ref landEv);
- _physics.WakeBody(uid, body: physics);
- // try place it in the user's hand
- _hands.TryPickupAnyHand(args.User, uid);
- }
- private void OnEmbedThrowDoHit(EntityUid uid, EmbeddableProjectileComponent component, ThrowDoHitEvent args)
- {
- if (!component.EmbedOnThrow)
- return;
- Embed(uid, args.Target, null, component);
- }
- private void OnEmbedProjectileHit(EntityUid uid, EmbeddableProjectileComponent component, ref ProjectileHitEvent args)
- {
- Embed(uid, args.Target, args.Shooter, component);
- // Raise a specific event for projectiles.
- if (TryComp(uid, out ProjectileComponent? projectile))
- {
- var ev = new ProjectileEmbedEvent(projectile.Shooter!.Value, projectile.Weapon!.Value, args.Target);
- RaiseLocalEvent(uid, ref ev);
- }
- }
- private void Embed(EntityUid uid, EntityUid target, EntityUid? user, EmbeddableProjectileComponent component)
- {
- TryComp<PhysicsComponent>(uid, out var physics);
- _physics.SetLinearVelocity(uid, Vector2.Zero, body: physics);
- _physics.SetBodyType(uid, BodyType.Static, body: physics);
- var xform = Transform(uid);
- _transform.SetParent(uid, xform, target);
- if (component.Offset != Vector2.Zero)
- {
- var rotation = xform.LocalRotation;
- if (TryComp<ThrowingAngleComponent>(uid, out var throwingAngleComp))
- rotation += throwingAngleComp.Angle;
- _transform.SetLocalPosition(uid, xform.LocalPosition + rotation.RotateVec(component.Offset),
- xform);
- }
- _audio.PlayPredicted(component.Sound, uid, null);
- var ev = new EmbedEvent(user, target);
- RaiseLocalEvent(uid, ref ev);
- }
- private void PreventCollision(EntityUid uid, ProjectileComponent component, ref PreventCollideEvent args)
- {
- if (component.IgnoreShooter && (args.OtherEntity == component.Shooter || args.OtherEntity == component.Weapon))
- {
- args.Cancelled = true;
- }
- //check for barricade component (percentage of chance to hit/pass over)
- if (TryComp(args.OtherEntity, out BarricadeComponent? barricade))
- {
- if (_random.NextFloat(0.0f, 100.0f) <= barricade.Blocking)
- {
- args.Cancelled = true;
- }
- }
- }
- public void SetShooter(EntityUid id, ProjectileComponent component, EntityUid? shooterId = null)
- {
- if (component.Shooter == shooterId || shooterId == null)
- return;
- component.Shooter = shooterId;
- Dirty(id, component);
- }
- [Serializable, NetSerializable]
- private sealed partial class RemoveEmbeddedProjectileEvent : DoAfterEvent
- {
- public override DoAfterEvent Clone() => this;
- }
- }
- [Serializable, NetSerializable]
- public sealed class ImpactEffectEvent : EntityEventArgs
- {
- public string Prototype;
- public NetCoordinates Coordinates;
- public ImpactEffectEvent(string prototype, NetCoordinates coordinates)
- {
- Prototype = prototype;
- Coordinates = coordinates;
- }
- }
- /// <summary>
- /// Raised when an entity is just about to be hit with a projectile but can reflect it
- /// </summary>
- [ByRefEvent]
- public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled);
- /// <summary>
- /// Raised when a projectile hits an entity
- /// </summary>
- [ByRefEvent]
- public record struct ProjectileHitEvent(DamageSpecifier Damage, EntityUid Target, EntityUid? Shooter = null, bool Handled = false);
|