AtmosphereSystem.HighPressureDelta.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using Content.Server.Atmos.Components;
  2. using Content.Shared.Atmos;
  3. using Content.Shared.Atmos.Components;
  4. using Content.Shared.Mobs.Components;
  5. using Content.Shared.Physics;
  6. using Robust.Shared.Audio;
  7. using Robust.Shared.Map;
  8. using Robust.Shared.Physics;
  9. using Robust.Shared.Physics.Components;
  10. using Robust.Shared.Random;
  11. using Robust.Shared.Utility;
  12. namespace Content.Server.Atmos.EntitySystems
  13. {
  14. public sealed partial class AtmosphereSystem
  15. {
  16. private const int SpaceWindSoundCooldownCycles = 75;
  17. private int _spaceWindSoundCooldown = 0;
  18. [ViewVariables(VVAccess.ReadWrite)]
  19. public string? SpaceWindSound { get; private set; } = "/Audio/Effects/space_wind.ogg";
  20. private readonly HashSet<Entity<MovedByPressureComponent>> _activePressures = new(8);
  21. private void UpdateHighPressure(float frameTime)
  22. {
  23. var toRemove = new RemQueue<Entity<MovedByPressureComponent>>();
  24. foreach (var ent in _activePressures)
  25. {
  26. var (uid, comp) = ent;
  27. MetaDataComponent? metadata = null;
  28. if (Deleted(uid, metadata))
  29. {
  30. toRemove.Add((uid, comp));
  31. continue;
  32. }
  33. if (Paused(uid, metadata))
  34. continue;
  35. comp.Accumulator += frameTime;
  36. if (comp.Accumulator < 2f)
  37. continue;
  38. // Reset it just for VV reasons even though it doesn't matter
  39. comp.Accumulator = 0f;
  40. toRemove.Add(ent);
  41. if (HasComp<MobStateComponent>(uid) &&
  42. TryComp<PhysicsComponent>(uid, out var body))
  43. {
  44. _physics.SetBodyStatus(uid, body, BodyStatus.OnGround);
  45. }
  46. if (TryComp<FixturesComponent>(uid, out var fixtures))
  47. {
  48. foreach (var (id, fixture) in fixtures.Fixtures)
  49. {
  50. _physics.AddCollisionMask(uid, id, fixture, (int) CollisionGroup.TableLayer, manager: fixtures);
  51. }
  52. }
  53. }
  54. foreach (var comp in toRemove)
  55. {
  56. _activePressures.Remove(comp);
  57. }
  58. }
  59. private void AddMobMovedByPressure(EntityUid uid, MovedByPressureComponent component, PhysicsComponent body)
  60. {
  61. if (!TryComp<FixturesComponent>(uid, out var fixtures))
  62. return;
  63. _physics.SetBodyStatus(uid, body, BodyStatus.InAir);
  64. foreach (var (id, fixture) in fixtures.Fixtures)
  65. {
  66. _physics.RemoveCollisionMask(uid, id, fixture, (int) CollisionGroup.TableLayer, manager: fixtures);
  67. }
  68. // TODO: Make them dynamic type? Ehh but they still want movement so uhh make it non-predicted like weightless?
  69. // idk it's hard.
  70. component.Accumulator = 0f;
  71. _activePressures.Add((uid, component));
  72. }
  73. private void HighPressureMovements(Entity<GridAtmosphereComponent> gridAtmosphere, TileAtmosphere tile, EntityQuery<PhysicsComponent> bodies, EntityQuery<TransformComponent> xforms, EntityQuery<MovedByPressureComponent> pressureQuery, EntityQuery<MetaDataComponent> metas)
  74. {
  75. // TODO ATMOS finish this
  76. // Don't play the space wind sound on tiles that are on fire...
  77. if (tile.PressureDifference > 15 && !tile.Hotspot.Valid)
  78. {
  79. if (_spaceWindSoundCooldown == 0 && !string.IsNullOrEmpty(SpaceWindSound))
  80. {
  81. var coordinates = _mapSystem.ToCenterCoordinates(tile.GridIndex, tile.GridIndices);
  82. _audio.PlayPvs(SpaceWindSound, coordinates, AudioParams.Default.WithVariation(0.125f).WithVolume(MathHelper.Clamp(tile.PressureDifference / 10, 10, 100)));
  83. }
  84. }
  85. if (tile.PressureDifference > 100)
  86. {
  87. // TODO ATMOS Do space wind graphics here!
  88. }
  89. if (_spaceWindSoundCooldown++ > SpaceWindSoundCooldownCycles)
  90. _spaceWindSoundCooldown = 0;
  91. // No atmos yeets, return early.
  92. if (!SpaceWind)
  93. return;
  94. // Used by ExperiencePressureDifference to correct push/throw directions from tile-relative to physics world.
  95. var gridWorldRotation = _transformSystem.GetWorldRotation(gridAtmosphere);
  96. // If we're using monstermos, smooth out the yeet direction to follow the flow
  97. if (MonstermosEqualization)
  98. {
  99. // We step through tiles according to the pressure direction on the current tile.
  100. // The goal is to get a general direction of the airflow in the area.
  101. // 3 is the magic number - enough to go around corners, but not U-turns.
  102. var curTile = tile;
  103. for (var i = 0; i < 3; i++)
  104. {
  105. if (curTile.PressureDirection == AtmosDirection.Invalid
  106. || !curTile.AdjacentBits.IsFlagSet(curTile.PressureDirection))
  107. break;
  108. curTile = curTile.AdjacentTiles[curTile.PressureDirection.ToIndex()]!;
  109. }
  110. if (curTile != tile)
  111. tile.PressureSpecificTarget = curTile;
  112. }
  113. _entSet.Clear();
  114. _lookup.GetLocalEntitiesIntersecting(tile.GridIndex, tile.GridIndices, _entSet, 0f);
  115. foreach (var entity in _entSet)
  116. {
  117. // Ideally containers would have their own EntityQuery internally or something given recursively it may need to slam GetComp<T> anyway.
  118. // Also, don't care about static bodies (but also due to collisionwakestate can't query dynamic directly atm).
  119. if (!bodies.TryGetComponent(entity, out var body) ||
  120. !pressureQuery.TryGetComponent(entity, out var pressure) ||
  121. !pressure.Enabled)
  122. continue;
  123. if (_containers.IsEntityInContainer(entity, metas.GetComponent(entity))) continue;
  124. var pressureMovements = EnsureComp<MovedByPressureComponent>(entity);
  125. if (pressure.LastHighPressureMovementAirCycle < gridAtmosphere.Comp.UpdateCounter)
  126. {
  127. // tl;dr YEET
  128. ExperiencePressureDifference(
  129. (entity, pressureMovements),
  130. gridAtmosphere.Comp.UpdateCounter,
  131. tile.PressureDifference,
  132. tile.PressureDirection, 0,
  133. tile.PressureSpecificTarget != null ? _mapSystem.ToCenterCoordinates(tile.GridIndex, tile.PressureSpecificTarget.GridIndices) : EntityCoordinates.Invalid,
  134. gridWorldRotation,
  135. xforms.GetComponent(entity),
  136. body);
  137. }
  138. }
  139. }
  140. // Called from AtmosphereSystem.LINDA.cs with SpaceWind CVar check handled there.
  141. private void ConsiderPressureDifference(GridAtmosphereComponent gridAtmosphere, TileAtmosphere tile, AtmosDirection differenceDirection, float difference)
  142. {
  143. gridAtmosphere.HighPressureDelta.Add(tile);
  144. if (difference <= tile.PressureDifference)
  145. return;
  146. tile.PressureDifference = difference;
  147. tile.PressureDirection = differenceDirection;
  148. }
  149. public void ExperiencePressureDifference(
  150. Entity<MovedByPressureComponent> ent,
  151. int cycle,
  152. float pressureDifference,
  153. AtmosDirection direction,
  154. float pressureResistanceProbDelta,
  155. EntityCoordinates throwTarget,
  156. Angle gridWorldRotation,
  157. TransformComponent? xform = null,
  158. PhysicsComponent? physics = null)
  159. {
  160. var (uid, component) = ent;
  161. if (!Resolve(uid, ref physics, false))
  162. return;
  163. if (!Resolve(uid, ref xform))
  164. return;
  165. // TODO ATMOS stuns?
  166. var maxForce = MathF.Sqrt(pressureDifference) * 2.25f;
  167. var moveProb = 100f;
  168. if (component.PressureResistance > 0)
  169. moveProb = MathF.Abs((pressureDifference / component.PressureResistance * MovedByPressureComponent.ProbabilityBasePercent) -
  170. MovedByPressureComponent.ProbabilityOffset);
  171. // Can we yeet the thing (due to probability, strength, etc.)
  172. if (moveProb > MovedByPressureComponent.ProbabilityOffset && _random.Prob(MathF.Min(moveProb / 100f, 1f))
  173. && !float.IsPositiveInfinity(component.MoveResist)
  174. && (physics.BodyType != BodyType.Static
  175. && (maxForce >= (component.MoveResist * MovedByPressureComponent.MoveForcePushRatio)))
  176. || (physics.BodyType == BodyType.Static && (maxForce >= (component.MoveResist * MovedByPressureComponent.MoveForceForcePushRatio))))
  177. {
  178. if (HasComp<MobStateComponent>(uid))
  179. {
  180. AddMobMovedByPressure(uid, component, physics);
  181. }
  182. if (maxForce > MovedByPressureComponent.ThrowForce)
  183. {
  184. var moveForce = maxForce;
  185. moveForce /= (throwTarget != EntityCoordinates.Invalid) ? SpaceWindPressureForceDivisorThrow : SpaceWindPressureForceDivisorPush;
  186. moveForce *= MathHelper.Clamp(moveProb, 0, 100);
  187. // Apply a sanity clamp to prevent being thrown through objects.
  188. var maxSafeForceForObject = SpaceWindMaxVelocity * physics.Mass;
  189. moveForce = MathF.Min(moveForce, maxSafeForceForObject);
  190. // Grid-rotation adjusted direction
  191. var dirVec = (direction.ToAngle() + gridWorldRotation).ToWorldVec();
  192. // TODO: Technically these directions won't be correct but uhh I'm just here for optimisations buddy not to fix my old bugs.
  193. if (throwTarget != EntityCoordinates.Invalid)
  194. {
  195. var pos = ((_transformSystem.ToMapCoordinates(throwTarget).Position - _transformSystem.GetWorldPosition(xform)).Normalized() + dirVec).Normalized();
  196. _physics.ApplyLinearImpulse(uid, pos * moveForce, body: physics);
  197. }
  198. else
  199. {
  200. moveForce = MathF.Min(moveForce, SpaceWindMaxPushForce);
  201. _physics.ApplyLinearImpulse(uid, dirVec * moveForce, body: physics);
  202. }
  203. component.LastHighPressureMovementAirCycle = cycle;
  204. }
  205. }
  206. }
  207. }
  208. }