1
0

SharedConveyorController.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System.Numerics;
  2. using Content.Shared.Conveyor;
  3. using Content.Shared.Gravity;
  4. using Content.Shared.Magic;
  5. using Content.Shared.Movement.Systems;
  6. using Robust.Shared.Collections;
  7. using Robust.Shared.Map;
  8. using Robust.Shared.Map.Components;
  9. using Robust.Shared.Physics;
  10. using Robust.Shared.Physics.Components;
  11. using Robust.Shared.Physics.Controllers;
  12. using Robust.Shared.Physics.Events;
  13. using Robust.Shared.Physics.Systems;
  14. using Robust.Shared.Utility;
  15. namespace Content.Shared.Physics.Controllers;
  16. public abstract class SharedConveyorController : VirtualController
  17. {
  18. [Dependency] protected readonly IMapManager MapManager = default!;
  19. [Dependency] protected readonly EntityLookupSystem Lookup = default!;
  20. [Dependency] private readonly SharedMapSystem _maps = default!;
  21. [Dependency] protected readonly SharedPhysicsSystem Physics = default!;
  22. [Dependency] private readonly SharedGravitySystem _gravity = default!;
  23. protected const string ConveyorFixture = "conveyor";
  24. private EntityQuery<MapGridComponent> _gridQuery;
  25. private EntityQuery<TransformComponent> _xformQuery;
  26. private ValueList<EntityUid> _ents = new();
  27. private HashSet<Entity<ConveyorComponent>> _conveyors = new();
  28. public override void Initialize()
  29. {
  30. _gridQuery = GetEntityQuery<MapGridComponent>();
  31. _xformQuery = GetEntityQuery<TransformComponent>();
  32. UpdatesAfter.Add(typeof(SharedMoverController));
  33. SubscribeLocalEvent<ConveyorComponent, StartCollideEvent>(OnConveyorStartCollide);
  34. SubscribeLocalEvent<ConveyorComponent, EndCollideEvent>(OnConveyorEndCollide);
  35. base.Initialize();
  36. }
  37. private void OnConveyorStartCollide(EntityUid uid, ConveyorComponent component, ref StartCollideEvent args)
  38. {
  39. var otherUid = args.OtherEntity;
  40. if (!args.OtherFixture.Hard || args.OtherBody.BodyType == BodyType.Static || component.State == ConveyorState.Off)
  41. return;
  42. var conveyed = EnsureComp<ConveyedComponent>(otherUid);
  43. if (conveyed.Colliding.Contains(uid))
  44. return;
  45. conveyed.Colliding.Add(uid);
  46. Dirty(otherUid, conveyed);
  47. }
  48. private void OnConveyorEndCollide(Entity<ConveyorComponent> ent, ref EndCollideEvent args)
  49. {
  50. if (!TryComp(args.OtherEntity, out ConveyedComponent? conveyed))
  51. return;
  52. if (!conveyed.Colliding.Remove(ent.Owner))
  53. return;
  54. Dirty(args.OtherEntity, conveyed);
  55. }
  56. public override void UpdateBeforeSolve(bool prediction, float frameTime)
  57. {
  58. base.UpdateBeforeSolve(prediction, frameTime);
  59. var query = EntityQueryEnumerator<ConveyedComponent, TransformComponent, PhysicsComponent>();
  60. _ents.Clear();
  61. while (query.MoveNext(out var uid, out var comp, out var xform, out var physics))
  62. {
  63. if (TryConvey((uid, comp, physics, xform), prediction, frameTime))
  64. continue;
  65. _ents.Add(uid);
  66. }
  67. foreach (var ent in _ents)
  68. {
  69. RemComp<ConveyedComponent>(ent);
  70. }
  71. }
  72. private bool TryConvey(Entity<ConveyedComponent, PhysicsComponent, TransformComponent> entity, bool prediction, float frameTime)
  73. {
  74. var physics = entity.Comp2;
  75. var xform = entity.Comp3;
  76. var contacting = entity.Comp1.Colliding.Count > 0;
  77. if (!contacting)
  78. return false;
  79. // Client moment
  80. if (!physics.Predict && prediction)
  81. return true;
  82. if (physics.BodyType == BodyType.Static)
  83. return false;
  84. if (!_gridQuery.TryComp(xform.GridUid, out var grid))
  85. return true;
  86. var gridTile = _maps.TileIndicesFor(xform.GridUid.Value, grid, xform.Coordinates);
  87. _conveyors.Clear();
  88. // Check for any conveyors on the attached tile.
  89. Lookup.GetLocalEntitiesIntersecting(xform.GridUid.Value, gridTile, _conveyors);
  90. DebugTools.Assert(_conveyors.Count <= 1);
  91. // No more conveyors.
  92. if (_conveyors.Count == 0)
  93. return true;
  94. if (physics.BodyStatus == BodyStatus.InAir ||
  95. _gravity.IsWeightless(entity, physics, xform))
  96. {
  97. return true;
  98. }
  99. Entity<ConveyorComponent> bestConveyor = default;
  100. var bestSpeed = 0f;
  101. foreach (var conveyor in _conveyors)
  102. {
  103. if (conveyor.Comp.Speed > bestSpeed && CanRun(conveyor))
  104. {
  105. bestSpeed = conveyor.Comp.Speed;
  106. bestConveyor = conveyor;
  107. }
  108. }
  109. if (bestSpeed == 0f || bestConveyor == default)
  110. return true;
  111. var comp = bestConveyor.Comp!;
  112. var conveyorXform = _xformQuery.GetComponent(bestConveyor.Owner);
  113. var conveyorPos = conveyorXform.LocalPosition;
  114. var conveyorRot = conveyorXform.LocalRotation;
  115. conveyorRot += bestConveyor.Comp!.Angle;
  116. if (comp.State == ConveyorState.Reverse)
  117. conveyorRot += MathF.PI;
  118. var direction = conveyorRot.ToWorldVec();
  119. var localPos = xform.LocalPosition;
  120. var itemRelative = conveyorPos - localPos;
  121. localPos += Convey(direction, bestSpeed, frameTime, itemRelative);
  122. TransformSystem.SetLocalPosition(entity, localPos, xform);
  123. // Force it awake for collisionwake reasons.
  124. Physics.SetAwake((entity, physics), true);
  125. Physics.SetSleepTime(physics, 0f);
  126. return true;
  127. }
  128. private static Vector2 Convey(Vector2 direction, float speed, float frameTime, Vector2 itemRelative)
  129. {
  130. if (speed == 0 || direction.Length() == 0)
  131. return Vector2.Zero;
  132. /*
  133. * Basic idea: if the item is not in the middle of the conveyor in the direction that the conveyor is running,
  134. * move the item towards the middle. Otherwise, move the item along the direction. This lets conveyors pick up
  135. * items that are not perfectly aligned in the middle, and also makes corner cuts work.
  136. *
  137. * We do this by computing the projection of 'itemRelative' on 'direction', yielding a vector 'p' in the direction
  138. * of 'direction'. We also compute the rejection 'r'. If the magnitude of 'r' is not (near) zero, then the item
  139. * is not on the centerline.
  140. */
  141. var p = direction * (Vector2.Dot(itemRelative, direction) / Vector2.Dot(direction, direction));
  142. var r = itemRelative - p;
  143. if (r.Length() < 0.1)
  144. {
  145. var velocity = direction * speed;
  146. return velocity * frameTime;
  147. }
  148. else
  149. {
  150. // Give a slight nudge in the direction of the conveyor to prevent
  151. // to collidable objects (e.g. crates) on the locker from getting stuck
  152. // pushing each other when rounding a corner.
  153. var velocity = (r + direction*0.2f).Normalized() * speed;
  154. return velocity * frameTime;
  155. }
  156. }
  157. public bool CanRun(ConveyorComponent component)
  158. {
  159. return component.State != ConveyorState.Off && component.Powered;
  160. }
  161. }