SharedProjectileSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System.Numerics;
  2. using Content.Shared._RMC14.Weapons.Ranged.Prediction;
  3. using Content.Shared.Administration.Logs;
  4. using Content.Shared.Camera;
  5. using Content.Shared.CombatMode.Pacification;
  6. using Content.Shared.Damage;
  7. using Content.Shared.Database;
  8. using Content.Shared.DoAfter;
  9. using Content.Shared.Effects;
  10. using Content.Shared.Hands.EntitySystems;
  11. using Content.Shared.Interaction;
  12. using Content.Shared.Throwing;
  13. using Content.Shared.Weapons.Ranged.Systems;
  14. using Robust.Shared.Audio.Systems;
  15. using Robust.Shared.Map;
  16. using Robust.Shared.Network;
  17. using Robust.Shared.Physics;
  18. using Robust.Shared.Physics.Components;
  19. using Robust.Shared.Physics.Events;
  20. using Robust.Shared.Physics.Systems;
  21. using Robust.Shared.Player;
  22. using Robust.Shared.Serialization;
  23. namespace Content.Shared.Projectiles;
  24. public abstract partial class SharedProjectileSystem : EntitySystem
  25. {
  26. public const string ProjectileFixture = "projectile";
  27. [Dependency] private readonly INetManager _netManager = default!;
  28. [Dependency] private readonly SharedAudioSystem _audio = default!;
  29. [Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
  30. [Dependency] private readonly SharedHandsSystem _hands = default!;
  31. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  32. [Dependency] private readonly SharedTransformSystem _transform = default!;
  33. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  34. [Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
  35. [Dependency] private readonly DamageableSystem _damageableSystem = default!;
  36. [Dependency] private readonly SharedGunSystem _guns = default!;
  37. [Dependency] private readonly SharedCameraRecoilSystem _sharedCameraRecoil = default!;
  38. public override void Initialize()
  39. {
  40. base.Initialize();
  41. SubscribeLocalEvent<ProjectileComponent, StartCollideEvent>(OnStartCollide);
  42. SubscribeLocalEvent<ProjectileComponent, PreventCollideEvent>(PreventCollision);
  43. SubscribeLocalEvent<EmbeddableProjectileComponent, ProjectileHitEvent>(OnEmbedProjectileHit);
  44. SubscribeLocalEvent<EmbeddableProjectileComponent, ThrowDoHitEvent>(OnEmbedThrowDoHit);
  45. SubscribeLocalEvent<EmbeddableProjectileComponent, ActivateInWorldEvent>(OnEmbedActivate);
  46. SubscribeLocalEvent<EmbeddableProjectileComponent, RemoveEmbeddedProjectileEvent>(OnEmbedRemove);
  47. }
  48. private void OnStartCollide(EntityUid uid, ProjectileComponent component, ref StartCollideEvent args)
  49. {
  50. // This is so entities that shouldn't get a collision are ignored.
  51. if (args.OurFixtureId != ProjectileFixture || !args.OtherFixture.Hard
  52. || component.DamagedEntity || component is { Weapon: null, OnlyCollideWhenShot: true })
  53. return;
  54. ProjectileCollide((uid, component, args.OurBody), args.OtherEntity);
  55. }
  56. public void ProjectileCollide(Entity<ProjectileComponent, PhysicsComponent> projectile, EntityUid target, bool predicted = false)
  57. {
  58. var (uid, component, ourBody) = projectile;
  59. if (projectile.Comp1.DamagedEntity)
  60. {
  61. if (_netManager.IsServer && component.DeleteOnCollide)
  62. QueueDel(uid);
  63. return;
  64. }
  65. // it's here so this check is only done once before possible hit
  66. var attemptEv = new ProjectileReflectAttemptEvent(uid, component, false);
  67. RaiseLocalEvent(target, ref attemptEv);
  68. if (attemptEv.Cancelled)
  69. {
  70. SetShooter(uid, component, target);
  71. return;
  72. }
  73. var ev = new ProjectileHitEvent(component.Damage, target, component.Shooter);
  74. RaiseLocalEvent(uid, ref ev);
  75. if (ev.Handled)
  76. return;
  77. var coordinates = Transform(projectile).Coordinates;
  78. var otherName = ToPrettyString(target);
  79. var direction = ourBody.LinearVelocity.Normalized();
  80. var modifiedDamage = _netManager.IsServer
  81. ? _damageableSystem.TryChangeDamage(target,
  82. ev.Damage,
  83. component.IgnoreResistances,
  84. origin: component.Shooter)
  85. : new DamageSpecifier(ev.Damage);
  86. var deleted = Deleted(target);
  87. var filter = Filter.Pvs(coordinates, entityMan: EntityManager);
  88. if (_guns.GunPrediction && TryComp(projectile, out PredictedProjectileServerComponent? serverProjectile))
  89. filter = filter.RemovePlayer(serverProjectile.Shooter);
  90. if (modifiedDamage is not null && (EntityManager.EntityExists(component.Shooter) || EntityManager.EntityExists(component.Weapon)))
  91. {
  92. if (modifiedDamage.AnyPositive() && !deleted)
  93. {
  94. _color.RaiseEffect(Color.Red, new List<EntityUid> { target }, filter);
  95. }
  96. var shooterOrWeapon = EntityManager.EntityExists(component.Shooter) ? component.Shooter!.Value : component.Weapon!.Value;
  97. _adminLogger.Add(LogType.BulletHit,
  98. HasComp<ActorComponent>(target) ? LogImpact.Extreme : LogImpact.High,
  99. $"Projectile {ToPrettyString(uid):projectile} shot by {ToPrettyString(shooterOrWeapon):source} hit {otherName:target} and dealt {modifiedDamage.GetTotal():damage} damage");
  100. }
  101. if (!deleted)
  102. {
  103. _guns.PlayImpactSound(target, modifiedDamage, component.SoundHit, component.ForceSound, filter, projectile);
  104. _sharedCameraRecoil.KickCamera(target, direction);
  105. }
  106. component.DamagedEntity = true;
  107. Dirty(uid, component);
  108. if (!predicted && component.DeleteOnCollide && (_netManager.IsServer || IsClientSide(uid)))
  109. QueueDel(uid);
  110. else if (_netManager.IsServer && component.DeleteOnCollide)
  111. {
  112. var predictedComp = EnsureComp<PredictedProjectileHitComponent>(uid);
  113. predictedComp.Origin = _transform.GetMoverCoordinates(coordinates);
  114. var targetCoords = _transform.GetMoverCoordinates(target);
  115. if (predictedComp.Origin.TryDistance(EntityManager, _transform, targetCoords, out var distance))
  116. predictedComp.Distance = distance;
  117. Dirty(uid, predictedComp);
  118. }
  119. if ((_netManager.IsServer || IsClientSide(uid)) && component.ImpactEffect != null)
  120. {
  121. var impactEffectEv = new ImpactEffectEvent(component.ImpactEffect, GetNetCoordinates(coordinates));
  122. if (_netManager.IsServer)
  123. RaiseNetworkEvent(impactEffectEv, filter);
  124. else
  125. RaiseLocalEvent(impactEffectEv);
  126. }
  127. }
  128. private void OnEmbedActivate(EntityUid uid, EmbeddableProjectileComponent component, ActivateInWorldEvent args)
  129. {
  130. // Nuh uh
  131. if (component.RemovalTime == null)
  132. return;
  133. if (args.Handled || !args.Complex || !TryComp<PhysicsComponent>(uid, out var physics) || physics.BodyType != BodyType.Static)
  134. return;
  135. args.Handled = true;
  136. _doAfter.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, component.RemovalTime.Value,
  137. new RemoveEmbeddedProjectileEvent(), eventTarget: uid, target: uid));
  138. }
  139. private void OnEmbedRemove(EntityUid uid, EmbeddableProjectileComponent component, RemoveEmbeddedProjectileEvent args)
  140. {
  141. // Whacky prediction issues.
  142. if (args.Cancelled || _netManager.IsClient)
  143. return;
  144. if (component.DeleteOnRemove)
  145. {
  146. QueueDel(uid);
  147. return;
  148. }
  149. var xform = Transform(uid);
  150. TryComp<PhysicsComponent>(uid, out var physics);
  151. _physics.SetBodyType(uid, BodyType.Dynamic, body: physics, xform: xform);
  152. _transform.AttachToGridOrMap(uid, xform);
  153. // Reset whether the projectile has damaged anything if it successfully was removed
  154. if (TryComp<ProjectileComponent>(uid, out var projectile))
  155. {
  156. projectile.Shooter = null;
  157. projectile.Weapon = null;
  158. projectile.DamagedEntity = false;
  159. }
  160. // Land it just coz uhhh yeah
  161. var landEv = new LandEvent(args.User, true);
  162. RaiseLocalEvent(uid, ref landEv);
  163. _physics.WakeBody(uid, body: physics);
  164. // try place it in the user's hand
  165. _hands.TryPickupAnyHand(args.User, uid);
  166. }
  167. private void OnEmbedThrowDoHit(EntityUid uid, EmbeddableProjectileComponent component, ThrowDoHitEvent args)
  168. {
  169. if (!component.EmbedOnThrow)
  170. return;
  171. Embed(uid, args.Target, null, component);
  172. }
  173. private void OnEmbedProjectileHit(EntityUid uid, EmbeddableProjectileComponent component, ref ProjectileHitEvent args)
  174. {
  175. Embed(uid, args.Target, args.Shooter, component);
  176. // Raise a specific event for projectiles.
  177. if (TryComp(uid, out ProjectileComponent? projectile))
  178. {
  179. var ev = new ProjectileEmbedEvent(projectile.Shooter!.Value, projectile.Weapon!.Value, args.Target);
  180. RaiseLocalEvent(uid, ref ev);
  181. }
  182. }
  183. private void Embed(EntityUid uid, EntityUid target, EntityUid? user, EmbeddableProjectileComponent component)
  184. {
  185. TryComp<PhysicsComponent>(uid, out var physics);
  186. _physics.SetLinearVelocity(uid, Vector2.Zero, body: physics);
  187. _physics.SetBodyType(uid, BodyType.Static, body: physics);
  188. var xform = Transform(uid);
  189. _transform.SetParent(uid, xform, target);
  190. if (component.Offset != Vector2.Zero)
  191. {
  192. var rotation = xform.LocalRotation;
  193. if (TryComp<ThrowingAngleComponent>(uid, out var throwingAngleComp))
  194. rotation += throwingAngleComp.Angle;
  195. _transform.SetLocalPosition(uid, xform.LocalPosition + rotation.RotateVec(component.Offset),
  196. xform);
  197. }
  198. _audio.PlayPredicted(component.Sound, uid, null);
  199. var ev = new EmbedEvent(user, target);
  200. RaiseLocalEvent(uid, ref ev);
  201. }
  202. private void PreventCollision(EntityUid uid, ProjectileComponent component, ref PreventCollideEvent args)
  203. {
  204. if (component.IgnoreShooter && (args.OtherEntity == component.Shooter || args.OtherEntity == component.Weapon))
  205. {
  206. args.Cancelled = true;
  207. }
  208. }
  209. public void SetShooter(EntityUid id, ProjectileComponent component, EntityUid? shooterId = null)
  210. {
  211. if (component.Shooter == shooterId || shooterId == null)
  212. return;
  213. component.Shooter = shooterId;
  214. Dirty(id, component);
  215. }
  216. [Serializable, NetSerializable]
  217. private sealed partial class RemoveEmbeddedProjectileEvent : DoAfterEvent
  218. {
  219. public override DoAfterEvent Clone() => this;
  220. }
  221. }
  222. [Serializable, NetSerializable]
  223. public sealed class ImpactEffectEvent : EntityEventArgs
  224. {
  225. public string Prototype;
  226. public NetCoordinates Coordinates;
  227. public ImpactEffectEvent(string prototype, NetCoordinates coordinates)
  228. {
  229. Prototype = prototype;
  230. Coordinates = coordinates;
  231. }
  232. }
  233. /// <summary>
  234. /// Raised when an entity is just about to be hit with a projectile but can reflect it
  235. /// </summary>
  236. [ByRefEvent]
  237. public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled);
  238. /// <summary>
  239. /// Raised when a projectile hits an entity
  240. /// </summary>
  241. [ByRefEvent]
  242. public record struct ProjectileHitEvent(DamageSpecifier Damage, EntityUid Target, EntityUid? Shooter = null, bool Handled = false);