Skip to content

Commit

Permalink
Merge pull request #2 from SS14-Classic/atmos_throws
Browse files Browse the repository at this point in the history
Crazy dangerous atmos optimisations
  • Loading branch information
Peptide90 authored May 3, 2024
2 parents 6df4578 + ba3646e commit 7d2df44
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using Content.Server.Atmos.Components;
using Content.Shared.Atmos;
using Content.Shared.Audio;
using Content.Shared.Mobs.Components;
using Content.Shared.Physics;
using Robust.Shared.Audio;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Utility;

namespace Content.Server.Atmos.EntitySystems
Expand Down Expand Up @@ -51,8 +48,7 @@ private void UpdateHighPressure(float frameTime)
comp.Accumulator = 0f;
toRemove.Add(ent);

if (HasComp<MobStateComponent>(uid) &&
TryComp<PhysicsComponent>(uid, out var body))
if (TryComp<PhysicsComponent>(uid, out var body))
{
_physics.SetBodyStatus(body, BodyStatus.OnGround);
}
Expand All @@ -72,7 +68,7 @@ private void UpdateHighPressure(float frameTime)
}
}

private void AddMobMovedByPressure(EntityUid uid, MovedByPressureComponent component, PhysicsComponent body)
private void AddMovedByPressure(EntityUid uid, MovedByPressureComponent component, PhysicsComponent body)
{
if (!TryComp<FixturesComponent>(uid, out var fixtures))
return;
Expand Down Expand Up @@ -162,7 +158,7 @@ private void HighPressureMovements(Entity<GridAtmosphereComponent> gridAtmospher
(entity, pressureMovements),
gridAtmosphere.Comp.UpdateCounter,
tile.PressureDifference,
tile.PressureDirection, 0,
tile.PressureDirection,
tile.PressureSpecificTarget != null ? _mapSystem.ToCenterCoordinates(tile.GridIndex, tile.PressureSpecificTarget.GridIndices) : EntityCoordinates.Invalid,
gridWorldRotation,
xforms.GetComponent(entity),
Expand All @@ -183,12 +179,13 @@ private void ConsiderPressureDifference(GridAtmosphereComponent gridAtmosphere,
tile.PressureDirection = differenceDirection;
}

//The EE version of this function drops pressureResistanceProbDelta, since it's not needed. If you are for whatever reason calling this function
//And it isn't working, you've probably still got the ResistancePobDelta line included.
public void ExperiencePressureDifference(
Entity<MovedByPressureComponent> ent,
int cycle,
float pressureDifference,
AtmosDirection direction,
float pressureResistanceProbDelta,
EntityCoordinates throwTarget,
Angle gridWorldRotation,
TransformComponent? xform = null,
Expand All @@ -201,50 +198,28 @@ public void ExperiencePressureDifference(
if (!Resolve(uid, ref xform))
return;

// TODO ATMOS stuns?

var maxForce = MathF.Sqrt(pressureDifference) * 2.25f;
var moveProb = 100f;

if (component.PressureResistance > 0)
moveProb = MathF.Abs((pressureDifference / component.PressureResistance * MovedByPressureComponent.ProbabilityBasePercent) -
MovedByPressureComponent.ProbabilityOffset);

// Can we yeet the thing (due to probability, strength, etc.)
if (moveProb > MovedByPressureComponent.ProbabilityOffset && _robustRandom.Prob(MathF.Min(moveProb / 100f, 1f))
&& !float.IsPositiveInfinity(component.MoveResist)
&& (physics.BodyType != BodyType.Static
&& (maxForce >= (component.MoveResist * MovedByPressureComponent.MoveForcePushRatio)))
|| (physics.BodyType == BodyType.Static && (maxForce >= (component.MoveResist * MovedByPressureComponent.MoveForceForcePushRatio))))
if (physics.BodyType != BodyType.Static
&& !float.IsPositiveInfinity(component.MoveResist))
{
if (HasComp<MobStateComponent>(uid))
{
AddMobMovedByPressure(uid, component, physics);
}
var moveForce = pressureDifference * physics.InvMass;

if (maxForce > MovedByPressureComponent.ThrowForce)
if (moveForce > physics.Mass)
{
var moveForce = maxForce;
moveForce /= (throwTarget != EntityCoordinates.Invalid) ? SpaceWindPressureForceDivisorThrow : SpaceWindPressureForceDivisorPush;
moveForce *= MathHelper.Clamp(moveProb, 0, 100);

// Apply a sanity clamp to prevent being thrown through objects.
var maxSafeForceForObject = SpaceWindMaxVelocity * physics.Mass;
moveForce = MathF.Min(moveForce, maxSafeForceForObject);

AddMovedByPressure(uid, component, physics);
// Grid-rotation adjusted direction
var dirVec = (direction.ToAngle() + gridWorldRotation).ToWorldVec();
var maxSafeForceForObject = SpaceWindMaxVelocity * physics.Mass;

// TODO: Technically these directions won't be correct but uhh I'm just here for optimisations buddy not to fix my old bugs.
if (throwTarget != EntityCoordinates.Invalid)
{
var pos = ((throwTarget.ToMap(EntityManager).Position - xform.WorldPosition).Normalized() + dirVec).Normalized();
_physics.ApplyLinearImpulse(uid, pos * moveForce, body: physics);
_physics.ApplyLinearImpulse(uid, pos * Math.Clamp(moveForce, 0, maxSafeForceForObject), body: physics);
}
else
{
moveForce = MathF.Min(moveForce, SpaceWindMaxPushForce);
_physics.ApplyLinearImpulse(uid, dirVec * moveForce, body: physics);
_physics.ApplyLinearImpulse(uid, dirVec * Math.Clamp(moveForce, 0, maxSafeForceForObject), body: physics);
}

component.LastHighPressureMovementAirCycle = cycle;
Expand Down
2 changes: 2 additions & 0 deletions Resources/Prototypes/Entities/Objects/Materials/shards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
damage:
types:
Slash: 2
- type: EmbeddableProjectile
sound: /Audio/Weapons/bladeslice.ogg
- type: Tag
tags:
- Trash
Expand Down
28 changes: 18 additions & 10 deletions Resources/Prototypes/Entities/Objects/Misc/tiles.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- type: entity
parent: BaseItem
id: FloorTileItemBase
description: These could work as a pretty decent throwing weapon.
description: The floor was meant to hold this, not you.
abstract: true
components:
- type: Sprite
Expand All @@ -12,7 +12,9 @@
- type: DamageOtherOnHit
damage:
types:
Blunt: 5
Blunt: 2
- type: EmbeddableProjectile
sound: /Audio/Weapons/star_hit.ogg
- type: Stack
count: 1
- type: Damageable
Expand Down Expand Up @@ -59,6 +61,12 @@
- type: Construction
graph: TileSteel
node: steeltile
- type: DamageOtherOnHit
damage:
types:
Blunt: 5 #Metal floor tiles deal more damage than standard
- type: EmbeddableProjectile
sound: /Audio/Weapons/block_metal1.ogg

- type: entity
name: steel dark checker tile
Expand Down Expand Up @@ -86,7 +94,7 @@

- type: entity
name: steel tile
parent: FloorTileItemBase
parent: FloorTileItemSteel
id: FloorTileItemMetalDiamond
components:
- type: Sprite
Expand Down Expand Up @@ -143,7 +151,7 @@

- type: entity
name: dark tile
parent: FloorTileItemBase
parent: FloorTileItemSteel
id: FloorTileItemDark
components:
- type: Sprite
Expand All @@ -162,7 +170,7 @@

- type: entity
name: techmaint floor
parent: FloorTileItemBase
parent: FloorTileItemSteel
id: FloorTileItemTechmaint
components:
- type: Sprite
Expand All @@ -178,7 +186,7 @@

- type: entity
name: reinforced tile
parent: FloorTileItemBase
parent: FloorTileItemSteel
id: FloorTileItemReinforced
components:
- type: Sprite
Expand Down Expand Up @@ -282,7 +290,7 @@

- type: entity
name: elevator shaft tile
parent: FloorTileItemBase
parent: FloorTileItemSteel
id: FloorTileItemElevatorShaft
components:
- type: Sprite
Expand Down Expand Up @@ -362,7 +370,7 @@

- type: entity
name: dark mining tile
parent: FloorTileItemBase
parent: FloorTileItemSteel
id: FloorTileItemMiningDark
components:
- type: Sprite
Expand All @@ -378,7 +386,7 @@

- type: entity
name: light mining tile
parent: FloorTileItemBase
parent: FloorTileItemSteel
id: FloorTileItemMiningLight
components:
- type: Sprite
Expand Down Expand Up @@ -1082,4 +1090,4 @@
stackType: FloorTileWoodLarge
- type: Construction
graph: TileWoodLarge
node: woodtilelarge
node: woodtilelarge

0 comments on commit 7d2df44

Please sign in to comment.