1
0

SharedProjectileSystem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. [Dependency] private readonly ILogManager _log = default!;
  42. private ISawmill _sawmill = default!;
  43. public override void Initialize()
  44. {
  45. base.Initialize();
  46. SubscribeLocalEvent<ProjectileComponent, StartCollideEvent>(OnStartCollide);
  47. SubscribeLocalEvent<ProjectileComponent, PreventCollideEvent>(PreventCollision);
  48. SubscribeLocalEvent<EmbeddableProjectileComponent, ProjectileHitEvent>(OnEmbedProjectileHit);
  49. SubscribeLocalEvent<EmbeddableProjectileComponent, ThrowDoHitEvent>(OnEmbedThrowDoHit);
  50. SubscribeLocalEvent<EmbeddableProjectileComponent, ActivateInWorldEvent>(OnEmbedActivate);
  51. SubscribeLocalEvent<EmbeddableProjectileComponent, RemoveEmbeddedProjectileEvent>(OnEmbedRemove);
  52. _sawmill = _log.GetSawmill("projectile");
  53. }
  54. private void OnStartCollide(EntityUid uid, ProjectileComponent component, ref StartCollideEvent args)
  55. {
  56. // This is so entities that shouldn't get a collision are ignored.
  57. if (args.OurFixtureId != ProjectileFixture || !args.OtherFixture.Hard
  58. || component.DamagedEntity || component is { Weapon: null, OnlyCollideWhenShot: true })
  59. return;
  60. ProjectileCollide((uid, component, args.OurBody), args.OtherEntity);
  61. }
  62. public void ProjectileCollide(Entity<ProjectileComponent, PhysicsComponent> projectile, EntityUid target, bool predicted = false)
  63. {
  64. var (uid, component, ourBody) = projectile;
  65. if (projectile.Comp1.DamagedEntity)
  66. {
  67. if (_netManager.IsServer && component.DeleteOnCollide)
  68. QueueDel(uid);
  69. return;
  70. }
  71. // it's here so this check is only done once before possible hit
  72. var attemptEv = new ProjectileReflectAttemptEvent(uid, component, false);
  73. RaiseLocalEvent(target, ref attemptEv);
  74. if (attemptEv.Cancelled)
  75. {
  76. SetShooter(uid, component, target);
  77. return;
  78. }
  79. var ev = new ProjectileHitEvent(component.Damage, target, component.Shooter);
  80. RaiseLocalEvent(uid, ref ev);
  81. if (ev.Handled)
  82. return;
  83. var coordinates = Transform(projectile).Coordinates;
  84. var otherName = ToPrettyString(target);
  85. var direction = ourBody.LinearVelocity.Normalized();
  86. var modifiedDamage = _netManager.IsServer
  87. ? _damageableSystem.TryChangeDamage(target,
  88. ev.Damage,
  89. component.IgnoreResistances,
  90. origin: component.Shooter)
  91. : new DamageSpecifier(ev.Damage);
  92. var deleted = Deleted(target);
  93. var filter = Filter.Pvs(coordinates, entityMan: EntityManager);
  94. if (_guns.GunPrediction && TryComp(projectile, out PredictedProjectileServerComponent? serverProjectile))
  95. filter = filter.RemovePlayer(serverProjectile.Shooter);
  96. if (modifiedDamage is not null && (EntityManager.EntityExists(component.Shooter) || EntityManager.EntityExists(component.Weapon)))
  97. {
  98. if (modifiedDamage.AnyPositive() && !deleted)
  99. {
  100. _color.RaiseEffect(Color.Red, new List<EntityUid> { target }, filter);
  101. }
  102. var shooterOrWeapon = EntityManager.EntityExists(component.Shooter) ? component.Shooter!.Value : component.Weapon!.Value;
  103. _adminLogger.Add(LogType.BulletHit,
  104. HasComp<ActorComponent>(target) ? LogImpact.Extreme : LogImpact.High,
  105. $"Projectile {ToPrettyString(uid):projectile} shot by {ToPrettyString(shooterOrWeapon):source} hit {otherName:target} and dealt {modifiedDamage.GetTotal():damage} damage");
  106. }
  107. if (!deleted)
  108. {
  109. _guns.PlayImpactSound(target, modifiedDamage, component.SoundHit, component.ForceSound, filter, projectile);
  110. //_sharedCameraRecoil.KickCamera(target, direction); # this makes people blind or something
  111. }
  112. component.DamagedEntity = true;
  113. Dirty(uid, component);
  114. if (!predicted && component.DeleteOnCollide && (_netManager.IsServer || IsClientSide(uid)))
  115. QueueDel(uid);
  116. else if (_netManager.IsServer && component.DeleteOnCollide)
  117. {
  118. var predictedComp = EnsureComp<PredictedProjectileHitComponent>(uid);
  119. predictedComp.Origin = _transform.GetMoverCoordinates(coordinates);
  120. var targetCoords = _transform.GetMoverCoordinates(target);
  121. if (predictedComp.Origin.TryDistance(EntityManager, _transform, targetCoords, out var distance))
  122. predictedComp.Distance = distance;
  123. Dirty(uid, predictedComp);
  124. }
  125. if ((_netManager.IsServer || IsClientSide(uid)) && component.ImpactEffect != null)
  126. {
  127. var impactEffectEv = new ImpactEffectEvent(component.ImpactEffect, GetNetCoordinates(coordinates));
  128. if (_netManager.IsServer)
  129. RaiseNetworkEvent(impactEffectEv, filter);
  130. else
  131. RaiseLocalEvent(impactEffectEv);
  132. }
  133. }
  134. private void OnEmbedActivate(EntityUid uid, EmbeddableProjectileComponent component, ActivateInWorldEvent args)
  135. {
  136. // Nuh uh
  137. if (component.RemovalTime == null)
  138. return;
  139. if (args.Handled || !args.Complex || !TryComp<PhysicsComponent>(uid, out var physics) || physics.BodyType != BodyType.Static)
  140. return;
  141. args.Handled = true;
  142. _doAfter.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, component.RemovalTime.Value,
  143. new RemoveEmbeddedProjectileEvent(), eventTarget: uid, target: uid));
  144. }
  145. private void OnEmbedRemove(EntityUid uid, EmbeddableProjectileComponent component, RemoveEmbeddedProjectileEvent args)
  146. {
  147. // Whacky prediction issues.
  148. if (args.Cancelled || _netManager.IsClient)
  149. return;
  150. if (component.DeleteOnRemove)
  151. {
  152. QueueDel(uid);
  153. return;
  154. }
  155. var xform = Transform(uid);
  156. TryComp<PhysicsComponent>(uid, out var physics);
  157. _physics.SetBodyType(uid, BodyType.Dynamic, body: physics, xform: xform);
  158. _transform.AttachToGridOrMap(uid, xform);
  159. // Reset whether the projectile has damaged anything if it successfully was removed
  160. if (TryComp<ProjectileComponent>(uid, out var projectile))
  161. {
  162. projectile.Shooter = null;
  163. projectile.Weapon = null;
  164. projectile.DamagedEntity = false;
  165. }
  166. // Land it just coz uhhh yeah
  167. var landEv = new LandEvent(args.User, true);
  168. RaiseLocalEvent(uid, ref landEv);
  169. _physics.WakeBody(uid, body: physics);
  170. // try place it in the user's hand
  171. _hands.TryPickupAnyHand(args.User, uid);
  172. }
  173. private void OnEmbedThrowDoHit(EntityUid uid, EmbeddableProjectileComponent component, ThrowDoHitEvent args)
  174. {
  175. if (!component.EmbedOnThrow)
  176. return;
  177. Embed(uid, args.Target, null, component);
  178. }
  179. private void OnEmbedProjectileHit(EntityUid uid, EmbeddableProjectileComponent component, ref ProjectileHitEvent args)
  180. {
  181. Embed(uid, args.Target, args.Shooter, component);
  182. // Raise a specific event for projectiles.
  183. if (TryComp(uid, out ProjectileComponent? projectile))
  184. {
  185. var ev = new ProjectileEmbedEvent(projectile.Shooter!.Value, projectile.Weapon!.Value, args.Target);
  186. RaiseLocalEvent(uid, ref ev);
  187. }
  188. }
  189. private void Embed(EntityUid uid, EntityUid target, EntityUid? user, EmbeddableProjectileComponent component)
  190. {
  191. TryComp<PhysicsComponent>(uid, out var physics);
  192. _physics.SetLinearVelocity(uid, Vector2.Zero, body: physics);
  193. _physics.SetBodyType(uid, BodyType.Static, body: physics);
  194. var xform = Transform(uid);
  195. _transform.SetParent(uid, xform, target);
  196. if (component.Offset != Vector2.Zero)
  197. {
  198. var rotation = xform.LocalRotation;
  199. if (TryComp<ThrowingAngleComponent>(uid, out var throwingAngleComp))
  200. rotation += throwingAngleComp.Angle;
  201. _transform.SetLocalPosition(uid, xform.LocalPosition + rotation.RotateVec(component.Offset),
  202. xform);
  203. }
  204. _audio.PlayPredicted(component.Sound, uid, null);
  205. var ev = new EmbedEvent(user, target);
  206. RaiseLocalEvent(uid, ref ev);
  207. }
  208. private void PreventCollision(EntityUid uid, ProjectileComponent component, ref PreventCollideEvent args)
  209. {
  210. if (component.IgnoreShooter && (args.OtherEntity == component.Shooter || args.OtherEntity == component.Weapon))
  211. {
  212. args.Cancelled = true;
  213. }
  214. //check for barricade component (percentage of chance to hit/pass over)
  215. if (TryComp(args.OtherEntity, out BarricadeComponent? barricade))
  216. {
  217. var alwaysPassThrough = false;
  218. //_sawmill.Info("Checking barricade...");
  219. if (component.Shooter is { } shooterUid && Exists(shooterUid))
  220. {
  221. // Condition 1: Directions are the same (using cardinal directions).
  222. // Or, if bidirectional, directions can be opposite.
  223. var shooterWorldRotation = _transform.GetWorldRotation(shooterUid);
  224. var barricadeWorldRotation = _transform.GetWorldRotation(args.OtherEntity);
  225. var shooterDir = shooterWorldRotation.GetCardinalDir();
  226. var barricadeDir = barricadeWorldRotation.GetCardinalDir();
  227. bool directionallyAllowed = false;
  228. if (shooterDir == barricadeDir)
  229. {
  230. directionallyAllowed = true;
  231. //_sawmill.Debug("Shooter and barricade facing same cardinal direction.");
  232. }
  233. else if (barricade.Bidirectional)
  234. {
  235. var oppositeBarricadeDir = (Direction)(((int)barricadeDir + 4) % 8);
  236. if (shooterDir == oppositeBarricadeDir)
  237. {
  238. directionallyAllowed = true;
  239. //_sawmill.Debug("Shooter and barricade facing opposite cardinal directions (bidirectional pass).");
  240. }
  241. }
  242. if (directionallyAllowed)
  243. {
  244. // Condition 2: Firer is within 1 tile of the barricade.
  245. var shooterCoords = Transform(shooterUid).Coordinates;
  246. var barricadeCoords = Transform(args.OtherEntity).Coordinates;
  247. if (shooterCoords.TryDistance(EntityManager, barricadeCoords, out var distance) &&
  248. distance <= 1.5f)
  249. {
  250. alwaysPassThrough = true;
  251. }
  252. }
  253. }
  254. if (alwaysPassThrough)
  255. {
  256. args.Cancelled = true;
  257. }
  258. else
  259. {
  260. //_sawmill.Debug("Barricade direction/distance check failed or shooter not valid.");
  261. // Standard barricade blocking logic if the special conditions are not met.
  262. var rando = _random.NextFloat(0.0f, 100.0f);
  263. if (rando >= barricade.Blocking)
  264. {
  265. args.Cancelled = true;
  266. }
  267. else
  268. {
  269. return;
  270. }
  271. }
  272. }
  273. }
  274. public void SetShooter(EntityUid id, ProjectileComponent component, EntityUid? shooterId = null)
  275. {
  276. if (component.Shooter == shooterId || shooterId == null)
  277. return;
  278. component.Shooter = shooterId;
  279. Dirty(id, component);
  280. }
  281. [Serializable, NetSerializable]
  282. private sealed partial class RemoveEmbeddedProjectileEvent : DoAfterEvent
  283. {
  284. public override DoAfterEvent Clone() => this;
  285. }
  286. }
  287. [Serializable, NetSerializable]
  288. public sealed class ImpactEffectEvent : EntityEventArgs
  289. {
  290. public string Prototype;
  291. public NetCoordinates Coordinates;
  292. public ImpactEffectEvent(string prototype, NetCoordinates coordinates)
  293. {
  294. Prototype = prototype;
  295. Coordinates = coordinates;
  296. }
  297. }
  298. /// <summary>
  299. /// Raised when an entity is just about to be hit with a projectile but can reflect it
  300. /// </summary>
  301. [ByRefEvent]
  302. public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled);
  303. /// <summary>
  304. /// Raised when a projectile hits an entity
  305. /// </summary>
  306. [ByRefEvent]
  307. public record struct ProjectileHitEvent(DamageSpecifier Damage, EntityUid Target, EntityUid? Shooter = null, bool Handled = false);