]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Gun auto state handlers (#15186)
authorKara <lunarautomaton6@gmail.com>
Fri, 14 Apr 2023 01:08:56 +0000 (20:08 -0500)
committerGitHub <noreply@github.com>
Fri, 14 Apr 2023 01:08:56 +0000 (18:08 -0700)
* battery auto state

* basic entity autostate

* ballistic autostate

* flyby

* cartridge ammo

* gun

* Revert "battery auto state"

This reverts commit 35b7d62f303fddb0edd9eb7a922e3c26b7a5f7fb.

* silly

Content.Shared/Weapons/Ranged/Components/AmmoComponent.cs
Content.Shared/Weapons/Ranged/Components/BallisticAmmoProviderComponent.cs
Content.Shared/Weapons/Ranged/Components/BasicEntityAmmoProviderComponent.cs
Content.Shared/Weapons/Ranged/Components/FlyBySoundComponent.cs
Content.Shared/Weapons/Ranged/Components/GunComponent.cs
Content.Shared/Weapons/Ranged/Systems/SharedFlyBySoundSystem.cs
Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.Ballistic.cs
Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.BasicEntity.cs
Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.Cartridges.cs
Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs

index 3b87beba1ab018904fd9e698fa8c0616fdf2a539..075192a76f7f3a549f75f02797bd22452c80afee 100644 (file)
@@ -20,13 +20,14 @@ public class AmmoComponent : Component, IShootable
 /// <summary>
 /// Spawns another prototype to be shot instead of itself.
 /// </summary>
-[RegisterComponent, NetworkedComponent, ComponentReference(typeof(AmmoComponent))]
-public sealed class CartridgeAmmoComponent : AmmoComponent
+[RegisterComponent, NetworkedComponent, ComponentReference(typeof(AmmoComponent)), AutoGenerateComponentState]
+public sealed partial class CartridgeAmmoComponent : AmmoComponent
 {
     [ViewVariables(VVAccess.ReadWrite), DataField("proto", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
     public string Prototype = default!;
 
     [ViewVariables(VVAccess.ReadWrite), DataField("spent")]
+    [AutoNetworkedField]
     public bool Spent = false;
 
     /// <summary>
index 0fb661aaaaf48681fbf0cd0217feee17ac0437f1..9c4b9045ccea37a655483cc226080be023ce5c41 100644 (file)
@@ -7,8 +7,8 @@ using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototy
 
 namespace Content.Shared.Weapons.Ranged.Components;
 
-[RegisterComponent, NetworkedComponent]
-public sealed class BallisticAmmoProviderComponent : Component
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class BallisticAmmoProviderComponent : Component
 {
     [ViewVariables(VVAccess.ReadWrite), DataField("soundRack")]
     public SoundSpecifier? SoundRack = new SoundPathSpecifier("/Audio/Weapons/Guns/Cock/smg_cock.ogg");
@@ -23,6 +23,7 @@ public sealed class BallisticAmmoProviderComponent : Component
     public int Capacity = 30;
 
     [ViewVariables(VVAccess.ReadWrite), DataField("unspawnedCount")]
+    [AutoNetworkedField]
     public int UnspawnedCount;
 
     [ViewVariables(VVAccess.ReadWrite), DataField("whitelist")]
@@ -32,6 +33,7 @@ public sealed class BallisticAmmoProviderComponent : Component
 
     // TODO: Make this use stacks when the typeserializer is done.
     [DataField("entities")]
+    [AutoNetworkedField(true)]
     public List<EntityUid> Entities = new();
 
     /// <summary>
@@ -44,6 +46,7 @@ public sealed class BallisticAmmoProviderComponent : Component
     /// Is the gun ready to shoot; if AutoCycle is true then this will always stay true and not need to be manually done.
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite), DataField("cycled")]
+    [AutoNetworkedField]
     public bool Cycled = true;
 
     /// <summary>
index d032b075559e6e48e784d3833d887e82b56c4f0b..d0c6c4487e494206ba24e4e2002f21960fa95173 100644 (file)
@@ -1,4 +1,5 @@
-using Robust.Shared.Prototypes;
+using Robust.Shared.GameStates;
+using Robust.Shared.Prototypes;
 using Robust.Shared.Serialization;
 using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
 
@@ -8,8 +9,8 @@ namespace Content.Shared.Weapons.Ranged.Components;
 ///     Simply provides a certain capacity of entities that cannot be reloaded through normal means and have
 ///     no special behavior like cycling, magazine
 /// </summary>
-[RegisterComponent]
-public sealed class BasicEntityAmmoProviderComponent : AmmoProviderComponent
+[RegisterComponent, AutoGenerateComponentState]
+public sealed partial class BasicEntityAmmoProviderComponent : AmmoProviderComponent
 {
     [ViewVariables(VVAccess.ReadWrite)]
     [DataField("proto", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
@@ -20,6 +21,7 @@ public sealed class BasicEntityAmmoProviderComponent : AmmoProviderComponent
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite)]
     [DataField("capacity")]
+    [AutoNetworkedField]
     public int? Capacity = null;
 
     /// <summary>
@@ -27,18 +29,6 @@ public sealed class BasicEntityAmmoProviderComponent : AmmoProviderComponent
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite)]
     [DataField("count")]
+    [AutoNetworkedField]
     public int? Count = null;
 }
-
-[Serializable, NetSerializable]
-public sealed class BasicEntityAmmoProviderComponentState : ComponentState
-{
-    public int? Capacity;
-    public int? Count;
-
-    public BasicEntityAmmoProviderComponentState(int? capacity, int? count)
-    {
-        Capacity = capacity;
-        Count = count;
-    }
-}
index 9375027470e9366ab4ff17cb0afb4580355eef2c..a915a8c1a564c05fb8d3fe285463118b0d4f2b62 100644 (file)
@@ -6,8 +6,8 @@ namespace Content.Shared.Weapons.Ranged.Components;
 /// <summary>
 /// Plays a sound when its non-hard fixture collides with a player.
 /// </summary>
-[RegisterComponent, NetworkedComponent]
-public sealed class FlyBySoundComponent : Component
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class FlyBySoundComponent : Component
 {
     /// <summary>
     /// Probability that the sound plays
@@ -16,10 +16,13 @@ public sealed class FlyBySoundComponent : Component
     public float Prob = 0.10f;
 
     [ViewVariables(VVAccess.ReadWrite), DataField("sound")]
+    [AutoNetworkedField]
     public SoundSpecifier Sound = new SoundCollectionSpecifier("BulletMiss")
     {
         Params = AudioParams.Default,
     };
 
-    [DataField("range")] public float Range = 1.5f;
+    [DataField("range")]
+    [AutoNetworkedField]
+    public float Range = 1.5f;
 }
index 061798e86e56b3ecda7e3f13440005de693a8fc2..07f84f325227f5061846eec1134dfc1009eaf789 100644 (file)
@@ -7,7 +7,8 @@ using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
 namespace Content.Shared.Weapons.Ranged.Components;
 
 [RegisterComponent, NetworkedComponent, Virtual]
-public class GunComponent : Component
+[AutoGenerateComponentState]
+public partial class GunComponent : Component
 {
     #region Sound
 
@@ -40,6 +41,7 @@ public class GunComponent : Component
     /// What the current spread is for shooting. This gets changed every time the gun fires.
     /// </summary>
     [DataField("currentAngle")]
+    [AutoNetworkedField]
     public Angle CurrentAngle;
 
     /// <summary>
@@ -58,12 +60,14 @@ public class GunComponent : Component
     /// The maximum angle allowed for <see cref="CurrentAngle"/>
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite), DataField("maxAngle")]
+    [AutoNetworkedField]
     public Angle MaxAngle = Angle.FromDegrees(2);
 
     /// <summary>
     /// The minimum angle allowed for <see cref="CurrentAngle"/>
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite), DataField("minAngle")]
+    [AutoNetworkedField]
     public Angle MinAngle = Angle.FromDegrees(1);
 
     #endregion
@@ -78,12 +82,14 @@ public class GunComponent : Component
     /// Used for tracking semi-auto / burst
     /// </summary>
     [ViewVariables]
+    [AutoNetworkedField]
     public int ShotCounter = 0;
 
     /// <summary>
     /// How many times it shoots per second.
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite), DataField("fireRate")]
+    [AutoNetworkedField]
     public float FireRate = 8f;
 
     /// <summary>
@@ -97,18 +103,21 @@ public class GunComponent : Component
     /// Can be set multiple times in a single tick due to guns firing faster than a single tick time.
     /// </summary>
     [DataField("nextFire", customTypeSerializer:typeof(TimeOffsetSerializer))]
+    [AutoNetworkedField]
     public TimeSpan NextFire = TimeSpan.Zero;
 
     /// <summary>
     /// What firemodes can be selected.
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite), DataField("availableModes")]
+    [AutoNetworkedField]
     public SelectiveFire AvailableModes = SelectiveFire.SemiAuto;
 
     /// <summary>
     /// What firemode is currently selected.
     /// </summary>
     [ViewVariables(VVAccess.ReadWrite), DataField("selectedMode")]
+    [AutoNetworkedField]
     public SelectiveFire SelectedMode = SelectiveFire.SemiAuto;
 
     [DataField("selectModeAction")]
index c2fd397e42598fb67b99f425eca905cf9d34165a..6aec5e9db3d9144260fc6d9abaf7ce063f304f5c 100644 (file)
@@ -20,8 +20,6 @@ public abstract class SharedFlyBySoundSystem : EntitySystem
     public override void Initialize()
     {
         base.Initialize();
-        SubscribeLocalEvent<FlyBySoundComponent, ComponentGetState>(OnGetState);
-        SubscribeLocalEvent<FlyBySoundComponent, ComponentHandleState>(OnHandleState);
         SubscribeLocalEvent<FlyBySoundComponent, ComponentStartup>(OnStartup);
         SubscribeLocalEvent<FlyBySoundComponent, ComponentShutdown>(OnShutdown);
     }
@@ -46,28 +44,4 @@ public abstract class SharedFlyBySoundSystem : EntitySystem
 
         _fixtures.DestroyFixture(uid, FlyByFixture, body: body);
     }
-
-    private void OnHandleState(EntityUid uid, FlyBySoundComponent component, ref ComponentHandleState args)
-    {
-        if (args.Current is not FlyBySoundComponentState state) return;
-
-        component.Sound = state.Sound;
-        component.Range = state.Range;
-    }
-
-    private void OnGetState(EntityUid uid, FlyBySoundComponent component, ref ComponentGetState args)
-    {
-        args.State = new FlyBySoundComponentState()
-        {
-            Sound = component.Sound,
-            Range = component.Range,
-        };
-    }
-
-    [Serializable, NetSerializable]
-    private sealed class FlyBySoundComponentState : ComponentState
-    {
-        public SoundSpecifier Sound = default!;
-        public float Range;
-    }
 }
index 2b5b330fb50e06999be1b5fb60c17a0e9b8f2bea..b529a978faa185951fa0a7fbefcc152d12373403 100644 (file)
@@ -19,8 +19,6 @@ public abstract partial class SharedGunSystem
         SubscribeLocalEvent<BallisticAmmoProviderComponent, MapInitEvent>(OnBallisticMapInit);
         SubscribeLocalEvent<BallisticAmmoProviderComponent, TakeAmmoEvent>(OnBallisticTakeAmmo);
         SubscribeLocalEvent<BallisticAmmoProviderComponent, GetAmmoCountEvent>(OnBallisticAmmoCount);
-        SubscribeLocalEvent<BallisticAmmoProviderComponent, ComponentGetState>(OnBallisticGetState);
-        SubscribeLocalEvent<BallisticAmmoProviderComponent, ComponentHandleState>(OnBallisticHandleState);
 
         SubscribeLocalEvent<BallisticAmmoProviderComponent, ExaminedEvent>(OnBallisticExamine);
         SubscribeLocalEvent<BallisticAmmoProviderComponent, GetVerbsEvent<Verb>>(OnBallisticVerb);
@@ -175,32 +173,6 @@ public abstract partial class SharedGunSystem
 
     protected abstract void Cycle(BallisticAmmoProviderComponent component, MapCoordinates coordinates);
 
-    private void OnBallisticGetState(EntityUid uid, BallisticAmmoProviderComponent component, ref ComponentGetState args)
-    {
-        args.State = new BallisticAmmoProviderComponentState()
-        {
-            UnspawnedCount = component.UnspawnedCount,
-            Entities = component.Entities,
-            Cycled = component.Cycled,
-        };
-    }
-
-    private void OnBallisticHandleState(EntityUid uid, BallisticAmmoProviderComponent component, ref ComponentHandleState args)
-    {
-        if (args.Current is not BallisticAmmoProviderComponentState state)
-            return;
-
-        component.Cycled = state.Cycled;
-        component.UnspawnedCount = state.UnspawnedCount;
-
-        component.Entities.Clear();
-
-        foreach (var ent in state.Entities)
-        {
-            component.Entities.Add(ent);
-        }
-    }
-
     private void OnBallisticInit(EntityUid uid, BallisticAmmoProviderComponent component, ComponentInit args)
     {
         component.Container = Containers.EnsureContainer<Container>(uid, "ballistic-ammo");
@@ -292,12 +264,4 @@ public abstract partial class SharedGunSystem
         Appearance.SetData(uid, AmmoVisuals.AmmoCount, GetBallisticShots(component), appearance);
         Appearance.SetData(uid, AmmoVisuals.AmmoMax, component.Capacity, appearance);
     }
-
-    [Serializable, NetSerializable]
-    private sealed class BallisticAmmoProviderComponentState : ComponentState
-    {
-        public int UnspawnedCount;
-        public List<EntityUid> Entities = default!;
-        public bool Cycled;
-    }
 }
index 58c3f01d16c98bfbde69780cfc452742c321ac02..e530b21e3277b84e42eb1c3dac9de9dc2f54ba6d 100644 (file)
@@ -11,23 +11,6 @@ public abstract partial class SharedGunSystem
         SubscribeLocalEvent<BasicEntityAmmoProviderComponent, ComponentInit>(OnBasicEntityInit);
         SubscribeLocalEvent<BasicEntityAmmoProviderComponent, TakeAmmoEvent>(OnBasicEntityTakeAmmo);
         SubscribeLocalEvent<BasicEntityAmmoProviderComponent, GetAmmoCountEvent>(OnBasicEntityAmmoCount);
-
-        SubscribeLocalEvent<BasicEntityAmmoProviderComponent, ComponentGetState>(OnBasicEntityGetState);
-        SubscribeLocalEvent<BasicEntityAmmoProviderComponent, ComponentHandleState>(OnBasicEntityHandleState);
-    }
-
-    private void OnBasicEntityGetState(EntityUid uid, BasicEntityAmmoProviderComponent component, ref ComponentGetState args)
-    {
-        args.State = new BasicEntityAmmoProviderComponentState(component.Capacity, component.Count);
-    }
-
-    private void OnBasicEntityHandleState(EntityUid uid, BasicEntityAmmoProviderComponent component, ref ComponentHandleState args)
-    {
-        if (args.Current is BasicEntityAmmoProviderComponentState state)
-        {
-            component.Capacity = state.Capacity;
-            component.Count = state.Count;
-        }
     }
 
     private void OnBasicEntityInit(EntityUid uid, BasicEntityAmmoProviderComponent component, ComponentInit args)
index 14ed2df95091d1080c36966a0558ed5648a8b190..b28b4b7508e2c78bcb54a2dab804974f9c29e893 100644 (file)
@@ -6,31 +6,8 @@ namespace Content.Shared.Weapons.Ranged.Systems;
 
 public abstract partial class SharedGunSystem
 {
+    // needed for server system
     protected virtual void InitializeCartridge()
     {
-        SubscribeLocalEvent<CartridgeAmmoComponent, ComponentGetState>(OnCartridgeGetState);
-        SubscribeLocalEvent<CartridgeAmmoComponent, ComponentHandleState>(OnCartridgeHandleState);
-    }
-
-    private void OnCartridgeHandleState(EntityUid uid, CartridgeAmmoComponent component, ref ComponentHandleState args)
-    {
-        if (args.Current is not CartridgeAmmoComponentState state)
-            return;
-
-        component.Spent = state.Spent;
-    }
-
-    private void OnCartridgeGetState(EntityUid uid, CartridgeAmmoComponent component, ref ComponentGetState args)
-    {
-        args.State = new CartridgeAmmoComponentState()
-        {
-            Spent = component.Spent,
-        };
-    }
-
-    [Serializable, NetSerializable]
-    private sealed class CartridgeAmmoComponentState : ComponentState
-    {
-        public bool Spent;
     }
 }
index 13179a24d21e0fa9a1418f0577e64a8d02e3cf4a..fdae26dcc139e460fd29936b0caae0e53b0cb689 100644 (file)
@@ -67,10 +67,8 @@ public abstract partial class SharedGunSystem : EntitySystem
     {
         Sawmill = Logger.GetSawmill("gun");
         Sawmill.Level = LogLevel.Info;
-        SubscribeLocalEvent<GunComponent, ComponentGetState>(OnGetState);
         SubscribeAllEvent<RequestShootEvent>(OnShootRequest);
         SubscribeAllEvent<RequestStopShootEvent>(OnStopShootRequest);
-        SubscribeLocalEvent<GunComponent, ComponentHandleState>(OnHandleState);
         SubscribeLocalEvent<GunComponent, MeleeAttackAttemptEvent>(OnGunMeleeAttempt);
 
         // Ammo providers
@@ -144,37 +142,6 @@ public abstract partial class SharedGunSystem : EntitySystem
         StopShooting(ev.Gun, gun);
     }
 
-    private void OnGetState(EntityUid uid, GunComponent component, ref ComponentGetState args)
-    {
-        args.State = new GunComponentState
-        {
-            FireRate = component.FireRate,
-            CurrentAngle = component.CurrentAngle,
-            MinAngle = component.MinAngle,
-            MaxAngle = component.MaxAngle,
-            NextFire = component.NextFire,
-            ShotCounter = component.ShotCounter,
-            SelectiveFire = component.SelectedMode,
-            AvailableSelectiveFire = component.AvailableModes,
-        };
-    }
-
-    private void OnHandleState(EntityUid uid, GunComponent component, ref ComponentHandleState args)
-    {
-        if (args.Current is not GunComponentState state)
-            return;
-
-        Sawmill.Debug($"Handle state: setting shot count from {component.ShotCounter} to {state.ShotCounter}");
-        component.FireRate = state.FireRate;
-        component.CurrentAngle = state.CurrentAngle;
-        component.MinAngle = state.MinAngle;
-        component.MaxAngle = state.MaxAngle;
-        component.NextFire = state.NextFire;
-        component.ShotCounter = state.ShotCounter;
-        component.SelectedMode = state.SelectiveFire;
-        component.AvailableModes = state.AvailableSelectiveFire;
-    }
-
     public bool CanShoot(GunComponent component)
     {
         if (component.NextFire > Timing.CurTime)
@@ -418,19 +385,6 @@ public abstract partial class SharedGunSystem : EntitySystem
     }
     protected abstract void CreateEffect(EntityUid uid, MuzzleFlashEvent message, EntityUid? user = null);
 
-    [Serializable, NetSerializable]
-    protected sealed class GunComponentState : ComponentState
-    {
-        public Angle CurrentAngle;
-        public Angle MinAngle;
-        public Angle MaxAngle;
-        public TimeSpan NextFire;
-        public float FireRate;
-        public int ShotCounter;
-        public SelectiveFire SelectiveFire;
-        public SelectiveFire AvailableSelectiveFire;
-    }
-
     /// <summary>
     /// Used for animated effects on the client.
     /// </summary>