Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step Trigger Minor Refactor #884

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions Content.Server/Mousetrap/MousetrapSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
using Content.Server.Explosion.EntitySystems;
using Content.Server.Popups;
using Content.Shared.Interaction.Events;
using Content.Shared.Inventory;
using Content.Shared.Mousetrap;
using Content.Shared.StepTrigger;
using Content.Shared.StepTrigger.Systems;
using Robust.Server.GameObjects;
using Robust.Shared.Physics.Components;
using Robust.Shared.Player;

namespace Content.Server.Mousetrap;

Expand Down Expand Up @@ -44,15 +40,16 @@ private void OnStepTriggerAttempt(EntityUid uid, MousetrapComponent component, r

private void BeforeDamageOnTrigger(EntityUid uid, MousetrapComponent component, BeforeDamageUserOnTriggerEvent args)
{
if (TryComp(args.Tripper, out PhysicsComponent? physics) && physics.Mass != 0)
{
// The idea here is inverse,
// Small - big damage,
// Large - small damage
// yes i punched numbers into a calculator until the graph looked right
var scaledDamage = -50 * Math.Atan(physics.Mass - component.MassBalance) + (25 * Math.PI);
args.Damage *= scaledDamage;
}
if (!TryComp<PhysicsComponent>(args.Tripper, out var physics)
|| physics.Mass is 0)
return;

// The idea here is inverse,
// Small - big damage,
// Large - small damage
// yes i punched numbers into a calculator until the graph looked right
VMSolidus marked this conversation as resolved.
Show resolved Hide resolved
var scaledDamage = -50 * MathF.Atan(physics.Mass - component.MassBalance) + 25 * MathF.PI;
args.Damage *= scaledDamage;
}

private void OnTrigger(EntityUid uid, MousetrapComponent component, TriggerEvent args)
Expand All @@ -64,9 +61,7 @@ private void OnTrigger(EntityUid uid, MousetrapComponent component, TriggerEvent
private void UpdateVisuals(EntityUid uid, MousetrapComponent? mousetrap = null, AppearanceComponent? appearance = null)
{
if (!Resolve(uid, ref mousetrap, ref appearance, false))
{
return;
}

_appearance.SetData(uid, MousetrapVisuals.Visual,
mousetrap.IsActive ? MousetrapVisuals.Armed : MousetrapVisuals.Unarmed, appearance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Robust.Shared.GameStates;

namespace Content.Shared.StepTrigger.Components;

/// <summary>
/// This component marks an entity as being immune to all step triggers.
/// For example, a Felinid or Harpy being so low density, that they don't set off landmines.
/// </summary>
/// <remarks>
/// This is the "Earliest Possible Exit" method, and therefore isn't possible to un-cancel.
/// It will prevent ALL step trigger events from firing. Therefore there may sometimes be unintended consequences to this.
/// Consider using a subscription to StepTriggerAttemptEvent if you wish to be more selective.
/// </remarks>
[RegisterComponent, NetworkedComponent]
public sealed partial class StepTriggerImmuneComponent : Component { }
74 changes: 21 additions & 53 deletions Content.Shared/StepTrigger/Systems/StepTriggerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,16 @@ public override void Update(float frameTime)
while (enumerator.MoveNext(out var uid, out var active, out var trigger, out var transform))
{
if (!Update(uid, trigger, transform, query))
{
continue;
}

RemCompDeferred(uid, active);
}
}

private bool Update(EntityUid uid, StepTriggerComponent component, TransformComponent transform, EntityQuery<PhysicsComponent> query)
{
if (!component.Active ||
component.Colliding.Count == 0)
{
if (!component.Active || component.Colliding.Count == 0)
return true;
}

if (component.Blacklist != null && TryComp<MapGridComponent>(transform.GridUid, out var grid))
{
Expand All @@ -67,17 +62,13 @@ private bool Update(EntityUid uid, StepTriggerComponent component, TransformComp
if (ent == uid)
continue;

if (component.Blacklist.IsValid(ent.Value, EntityManager) == true)
{
if (component.Blacklist.IsValid(ent.Value, EntityManager))
return false;
}
}
}

foreach (var otherUid in component.Colliding)
{
UpdateColliding(uid, component, transform, otherUid, query);
}

return false;
}
Expand All @@ -95,9 +86,8 @@ private void UpdateColliding(EntityUid uid, StepTriggerComponent component, Tran
if (!ourAabb.Intersects(otherAabb))
{
if (component.CurrentlySteppedOn.Remove(otherUid))
{
Dirty(uid, component);
}

return;
}

Expand All @@ -109,9 +99,7 @@ private void UpdateColliding(EntityUid uid, StepTriggerComponent component, Tran
|| component.CurrentlySteppedOn.Contains(otherUid)
|| ratio < component.IntersectRatio
|| !CanTrigger(uid, otherUid, component))
{
return;
}

if (component.StepOn)
{
Expand All @@ -130,7 +118,9 @@ private void UpdateColliding(EntityUid uid, StepTriggerComponent component, Tran

private bool CanTrigger(EntityUid uid, EntityUid otherUid, StepTriggerComponent component)
{
if (!component.Active || component.CurrentlySteppedOn.Contains(otherUid))
if (HasComp<StepTriggerImmuneComponent>(otherUid)
|| !component.Active
|| component.CurrentlySteppedOn.Contains(otherUid))
return false;

// Can't trigger if we don't ignore weightless entities
Expand All @@ -141,7 +131,6 @@ private bool CanTrigger(EntityUid uid, EntityUid otherUid, StepTriggerComponent
return false;

var msg = new StepTriggerAttemptEvent { Source = uid, Tripper = otherUid };

RaiseLocalEvent(uid, ref msg);

return msg.Continue && !msg.Cancelled;
Expand All @@ -151,18 +140,14 @@ private void OnStartCollide(EntityUid uid, StepTriggerComponent component, ref S
{
var otherUid = args.OtherEntity;

if (!args.OtherFixture.Hard)
return;

if (!CanTrigger(uid, otherUid, component))
if (!args.OtherFixture.Hard
|| !CanTrigger(uid, otherUid, component))
return;

EnsureComp<StepTriggerActiveComponent>(uid);

if (component.Colliding.Add(otherUid))
{
Dirty(uid, component);
}
}

private void OnEndCollide(EntityUid uid, StepTriggerComponent component, ref EndCollideEvent args)
Expand All @@ -182,29 +167,21 @@ private void OnEndCollide(EntityUid uid, StepTriggerComponent component, ref End
}

if (component.Colliding.Count == 0)
{
RemCompDeferred<StepTriggerActiveComponent>(uid);
}
}

private void TriggerHandleState(EntityUid uid, StepTriggerComponent component, ref AfterAutoHandleStateEvent args)
{
if (component.Colliding.Count > 0)
{
EnsureComp<StepTriggerActiveComponent>(uid);
}
else
{
RemCompDeferred<StepTriggerActiveComponent>(uid);
}
}

public void SetIntersectRatio(EntityUid uid, float ratio, StepTriggerComponent? component = null)
{
if (!Resolve(uid, ref component))
return;

if (MathHelper.CloseToPercent(component.IntersectRatio, ratio))
if (!Resolve(uid, ref component)
|| MathHelper.CloseToPercent(component.IntersectRatio, ratio))
return;

component.IntersectRatio = ratio;
Expand All @@ -213,10 +190,8 @@ public void SetIntersectRatio(EntityUid uid, float ratio, StepTriggerComponent?

public void SetRequiredTriggerSpeed(EntityUid uid, float speed, StepTriggerComponent? component = null)
{
if (!Resolve(uid, ref component))
return;

if (MathHelper.CloseToPercent(component.RequiredTriggeredSpeed, speed))
if (!Resolve(uid, ref component)
|| MathHelper.CloseToPercent(component.RequiredTriggeredSpeed, speed))
return;

component.RequiredTriggeredSpeed = speed;
Expand All @@ -225,37 +200,30 @@ public void SetRequiredTriggerSpeed(EntityUid uid, float speed, StepTriggerCompo

public void SetActive(EntityUid uid, bool active, StepTriggerComponent? component = null)
{
if (!Resolve(uid, ref component))
return;

if (active == component.Active)
if (!Resolve(uid, ref component)
|| active == component.Active)
return;

component.Active = active;
Dirty(uid, component);
}
}

/// <summary>
/// Raised at the beginning of a step trigger, and before entering the checks.
/// Allows for entities to end the steptrigger early via args.Cancelled.
/// </summary>
[ByRefEvent]
public struct StepTriggerAttemptEvent
{
public EntityUid Source;
public EntityUid Tripper;
public bool Continue;
/// <summary>
/// Set by systems which wish to cancel the step trigger event, regardless of event ordering.
/// </summary>
public bool Cancelled;
}
public record struct StepTriggerAttemptEvent(EntityUid Source, EntityUid Tripper, bool Continue, bool Cancelled);

/// <summary>
/// Raised when an entity stands on a steptrigger initially (assuming it has both on and off states).
/// Raised when an entity stands on a steptrigger initially (assuming it has both on and off states).
/// </summary>
[ByRefEvent]
public readonly record struct StepTriggeredOnEvent(EntityUid Source, EntityUid Tripper);

/// <summary>
/// Raised when an entity leaves a steptrigger if it has on and off states OR when an entity intersects a steptrigger.
/// Raised when an entity leaves a steptrigger if it has on and off states OR when an entity intersects a steptrigger.
/// </summary>
[ByRefEvent]
public readonly record struct StepTriggeredOffEvent(EntityUid Source, EntityUid Tripper);
5 changes: 5 additions & 0 deletions Resources/Locale/en-US/traits/traits.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,8 @@ trait-description-NaturalTelepath =
whether or not you possess any notable psychic powers. This offers all of the same benefits and
drawbacks of Latent Psychic, except that you are guaranteed to start with full Telepathy. You may
still gain powers as normal for a Latent Psychic.

trait-name-TrapAvoider = Trap Avoider
trait-description-TrapAvoider =
You possess a preturnatural sense of traps, and will unconsciously avoid them. You will never trigger
floor traps, such as land mines, tripwires, mouse traps(If you're small enough), etc.
VMSolidus marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Mobs/Species/harpy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
understands:
- GalacticCommon
- SolCommon
- type: StepTriggerImmune

- type: entity
save: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
stealth: Subtle
stripTimeReduction: 0
stripTimeMultiplier: 0.667
- type: StepTriggerImmune

- type: entity
save: false
Expand Down
13 changes: 13 additions & 0 deletions Resources/Prototypes/Traits/skills.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,16 @@
- ForensicMantis
- Chaplain
- Librarian

- type: trait
id: TrapAvoider
category: Physical
points: -3
components:
- type: StepTriggerImmune
requirements:
- !type:CharacterSpeciesRequirement
inverted: true
species:
- Felinid
- Harpy
VMSolidus marked this conversation as resolved.
Show resolved Hide resolved
Loading