DisposableSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Disposal.Tube;
  3. using Content.Server.Disposal.Tube.Components;
  4. using Content.Server.Disposal.Unit.Components;
  5. using Content.Shared.Body.Components;
  6. using Content.Shared.Damage;
  7. using Content.Shared.Item;
  8. using Content.Shared.Throwing;
  9. using Robust.Shared.Audio.Systems;
  10. using Robust.Shared.Containers;
  11. using Robust.Shared.Map.Components;
  12. using Robust.Shared.Physics.Components;
  13. using Robust.Shared.Physics.Systems;
  14. namespace Content.Server.Disposal.Unit.EntitySystems
  15. {
  16. public sealed class DisposableSystem : EntitySystem
  17. {
  18. [Dependency] private readonly ThrowingSystem _throwing = default!;
  19. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  20. [Dependency] private readonly DamageableSystem _damageable = default!;
  21. [Dependency] private readonly DisposalUnitSystem _disposalUnitSystem = default!;
  22. [Dependency] private readonly DisposalTubeSystem _disposalTubeSystem = default!;
  23. [Dependency] private readonly SharedAudioSystem _audio = default!;
  24. [Dependency] private readonly SharedContainerSystem _containerSystem = default!;
  25. [Dependency] private readonly SharedMapSystem _maps = default!;
  26. [Dependency] private readonly SharedPhysicsSystem _physicsSystem = default!;
  27. [Dependency] private readonly SharedTransformSystem _xformSystem = default!;
  28. private EntityQuery<DisposalTubeComponent> _disposalTubeQuery;
  29. private EntityQuery<DisposalUnitComponent> _disposalUnitQuery;
  30. private EntityQuery<MetaDataComponent> _metaQuery;
  31. private EntityQuery<PhysicsComponent> _physicsQuery;
  32. private EntityQuery<TransformComponent> _xformQuery;
  33. public override void Initialize()
  34. {
  35. base.Initialize();
  36. _disposalTubeQuery = GetEntityQuery<DisposalTubeComponent>();
  37. _disposalUnitQuery = GetEntityQuery<DisposalUnitComponent>();
  38. _metaQuery = GetEntityQuery<MetaDataComponent>();
  39. _physicsQuery = GetEntityQuery<PhysicsComponent>();
  40. _xformQuery = GetEntityQuery<TransformComponent>();
  41. SubscribeLocalEvent<DisposalHolderComponent, ComponentStartup>(OnComponentStartup);
  42. }
  43. private void OnComponentStartup(EntityUid uid, DisposalHolderComponent holder, ComponentStartup args)
  44. {
  45. holder.Container = _containerSystem.EnsureContainer<Container>(uid, nameof(DisposalHolderComponent));
  46. }
  47. public bool TryInsert(EntityUid uid, EntityUid toInsert, DisposalHolderComponent? holder = null)
  48. {
  49. if (!Resolve(uid, ref holder))
  50. return false;
  51. if (!CanInsert(uid, toInsert, holder))
  52. return false;
  53. if (!_containerSystem.Insert(toInsert, holder.Container))
  54. return false;
  55. if (_physicsQuery.TryGetComponent(toInsert, out var physBody))
  56. _physicsSystem.SetCanCollide(toInsert, false, body: physBody);
  57. return true;
  58. }
  59. private bool CanInsert(EntityUid uid, EntityUid toInsert, DisposalHolderComponent? holder = null)
  60. {
  61. if (!Resolve(uid, ref holder))
  62. return false;
  63. if (!_containerSystem.CanInsert(toInsert, holder.Container))
  64. {
  65. return false;
  66. }
  67. return HasComp<ItemComponent>(toInsert) ||
  68. HasComp<BodyComponent>(toInsert);
  69. }
  70. public void ExitDisposals(EntityUid uid, DisposalHolderComponent? holder = null, TransformComponent? holderTransform = null)
  71. {
  72. if (Terminating(uid))
  73. return;
  74. if (!Resolve(uid, ref holder, ref holderTransform))
  75. return;
  76. if (holder.IsExitingDisposals)
  77. {
  78. Log.Error("Tried exiting disposals twice. This should never happen.");
  79. return;
  80. }
  81. holder.IsExitingDisposals = true;
  82. // Check for a disposal unit to throw them into and then eject them from it.
  83. // *This ejection also makes the target not collide with the unit.*
  84. // *This is on purpose.*
  85. EntityUid? disposalId = null;
  86. DisposalUnitComponent? duc = null;
  87. var gridUid = holderTransform.GridUid;
  88. if (TryComp<MapGridComponent>(gridUid, out var grid))
  89. {
  90. foreach (var contentUid in _maps.GetLocal(gridUid.Value, grid, holderTransform.Coordinates))
  91. {
  92. if (_disposalUnitQuery.TryGetComponent(contentUid, out duc))
  93. {
  94. disposalId = contentUid;
  95. break;
  96. }
  97. }
  98. }
  99. // We're purposely iterating over all the holder's children
  100. // because the holder might have something teleported into it,
  101. // outside the usual container insertion logic.
  102. var children = holderTransform.ChildEnumerator;
  103. while (children.MoveNext(out var entity))
  104. {
  105. RemComp<BeingDisposedComponent>(entity);
  106. var meta = _metaQuery.GetComponent(entity);
  107. if (holder.Container.Contains(entity))
  108. _containerSystem.Remove((entity, null, meta), holder.Container, reparent: false, force: true);
  109. var xform = _xformQuery.GetComponent(entity);
  110. if (xform.ParentUid != uid)
  111. continue;
  112. if (duc != null)
  113. _containerSystem.Insert((entity, xform, meta), duc.Container);
  114. else
  115. {
  116. _xformSystem.AttachToGridOrMap(entity, xform);
  117. var direction = holder.CurrentDirection == Direction.Invalid ? holder.PreviousDirection : holder.CurrentDirection;
  118. if (direction != Direction.Invalid && _xformQuery.TryGetComponent(gridUid, out var gridXform))
  119. {
  120. var directionAngle = direction.ToAngle();
  121. directionAngle += _xformSystem.GetWorldRotation(gridXform);
  122. _throwing.TryThrow(entity, directionAngle.ToWorldVec() * 3f, 10f);
  123. }
  124. }
  125. }
  126. if (disposalId != null && duc != null)
  127. {
  128. _disposalUnitSystem.TryEjectContents(disposalId.Value, duc);
  129. }
  130. if (_atmosphereSystem.GetContainingMixture(uid, false, true) is { } environment)
  131. {
  132. _atmosphereSystem.Merge(environment, holder.Air);
  133. holder.Air.Clear();
  134. }
  135. EntityManager.DeleteEntity(uid);
  136. }
  137. // Note: This function will cause an ExitDisposals on any failure that does not make an ExitDisposals impossible.
  138. public bool EnterTube(EntityUid holderUid, EntityUid toUid, DisposalHolderComponent? holder = null, TransformComponent? holderTransform = null, DisposalTubeComponent? to = null, TransformComponent? toTransform = null)
  139. {
  140. if (!Resolve(holderUid, ref holder, ref holderTransform))
  141. return false;
  142. if (holder.IsExitingDisposals)
  143. {
  144. Log.Error("Tried entering tube after exiting disposals. This should never happen.");
  145. return false;
  146. }
  147. if (!Resolve(toUid, ref to, ref toTransform))
  148. {
  149. ExitDisposals(holderUid, holder, holderTransform);
  150. return false;
  151. }
  152. foreach (var ent in holder.Container.ContainedEntities)
  153. {
  154. var comp = EnsureComp<BeingDisposedComponent>(ent);
  155. comp.Holder = holderUid;
  156. }
  157. // Insert into next tube
  158. if (!_containerSystem.Insert(holderUid, to.Contents))
  159. {
  160. ExitDisposals(holderUid, holder, holderTransform);
  161. return false;
  162. }
  163. if (holder.CurrentTube != null)
  164. {
  165. holder.PreviousTube = holder.CurrentTube;
  166. holder.PreviousDirection = holder.CurrentDirection;
  167. }
  168. holder.CurrentTube = toUid;
  169. var ev = new GetDisposalsNextDirectionEvent(holder);
  170. RaiseLocalEvent(toUid, ref ev);
  171. holder.CurrentDirection = ev.Next;
  172. holder.StartingTime = 0.1f;
  173. holder.TimeLeft = 0.1f;
  174. // Logger.InfoS("c.s.disposal.holder", $"Disposals dir {holder.CurrentDirection}");
  175. // Invalid direction = exit now!
  176. if (holder.CurrentDirection == Direction.Invalid)
  177. {
  178. ExitDisposals(holderUid, holder, holderTransform);
  179. return false;
  180. }
  181. // damage entities on turns and play sound
  182. if (holder.CurrentDirection != holder.PreviousDirection)
  183. {
  184. foreach (var ent in holder.Container.ContainedEntities)
  185. {
  186. _damageable.TryChangeDamage(ent, to.DamageOnTurn);
  187. }
  188. _audio.PlayPvs(to.ClangSound, toUid);
  189. }
  190. return true;
  191. }
  192. public override void Update(float frameTime)
  193. {
  194. var query = EntityQueryEnumerator<DisposalHolderComponent>();
  195. while (query.MoveNext(out var uid, out var holder))
  196. {
  197. UpdateComp(uid, holder, frameTime);
  198. }
  199. }
  200. private void UpdateComp(EntityUid uid, DisposalHolderComponent holder, float frameTime)
  201. {
  202. while (frameTime > 0)
  203. {
  204. var time = frameTime;
  205. if (time > holder.TimeLeft)
  206. {
  207. time = holder.TimeLeft;
  208. }
  209. holder.TimeLeft -= time;
  210. frameTime -= time;
  211. if (!EntityManager.EntityExists(holder.CurrentTube))
  212. {
  213. ExitDisposals(uid, holder);
  214. break;
  215. }
  216. var currentTube = holder.CurrentTube!.Value;
  217. if (holder.TimeLeft > 0)
  218. {
  219. var progress = 1 - holder.TimeLeft / holder.StartingTime;
  220. var origin = _xformQuery.GetComponent(currentTube).Coordinates;
  221. var destination = holder.CurrentDirection.ToVec();
  222. var newPosition = destination * progress;
  223. // This is some supreme shit code.
  224. _xformSystem.SetCoordinates(uid, origin.Offset(newPosition).WithEntityId(currentTube));
  225. continue;
  226. }
  227. // Past this point, we are performing inter-tube transfer!
  228. // Remove current tube content
  229. _containerSystem.Remove(uid, _disposalTubeQuery.GetComponent(currentTube).Contents, reparent: false, force: true);
  230. // Find next tube
  231. var nextTube = _disposalTubeSystem.NextTubeFor(currentTube, holder.CurrentDirection);
  232. if (!EntityManager.EntityExists(nextTube))
  233. {
  234. ExitDisposals(uid, holder);
  235. break;
  236. }
  237. // Perform remainder of entry process
  238. if (!EnterTube(uid, nextTube!.Value, holder))
  239. {
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. }