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

Randomly Fire Dropped Guns #19

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Content.Shared.Throwing;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Systems;
using Robust.Shared.Physics.Components;
using Robust.Shared.Random;

namespace Content.Server.SimpleStation14.Weapons.Ranged.Systems;

public sealed class FireOnDropSystem : EntitySystem
{
[Dependency] private readonly SharedGunSystem _gun = default!;
[Dependency] private readonly IRobustRandom _random = default!;


public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<GunComponent, LandEvent>(HandleLand);
}


private void HandleLand(EntityUid uid, GunComponent component, ref LandEvent args)
{
// TODO: This shouldn't be a hardcoded 10% roll. I wanted to base it off mass, but most items don't seem to care about their mass (most guns had the same).
// Then I wanted to base it off of item size, but the minigun still has a size of 5 at the time of writing, so ¯\_(ツ)_/¯
if (_random.Prob(0.1f))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like to see this be a datafield on GunComponent, so that individual guns can have a sort of "Reliability" set. That way you can have really shitty guns have a very high chance of setting themselves off when fired, and high quality guns having a much lower chance.

_gun.AttemptShoot(uid, uid, component, Transform(uid).Coordinates.Offset(Transform(uid).LocalRotation.ToVec()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was apparently wrong. This is actually failing because LandEvent is operating inside of an Enumeration over an object's physics component. Your code is running fine inside the enumeration, and it will even fire the weapon when it's thrown. However firing a weapon triggers a linearimpulse, which modifies the weapon's physics component, and since said physics component currently resides inside an enumeration, it causes the game to crash on the very next frame. What I think you might need to do is instead add an AfterLandEvent which runs outside of said enumeration. Essentially waiting until it has finished calculating physics for the object before asking it to calculate something new. This definitely isn't the best solution either. I can imagine some situations where you might say, "But I want the gun to be able to bounce off a wall, fire, and then change direction mid bounce because of the shot".

This is just a really odd coincedence it seems of how the game handles throwing an object.

// The gun fires itself (weird), with the target being its own position offset by its rotation as a point vector.
// The result being that it will always fire the direction that all gun sprites point in.
}
}
Loading