SharedProjectileSystem.cs 12 KB

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