Sentry turrets - Part 4: The sentry turret and its primary systems (#35123)

* Initial commit

* Removed mention of StationAiTurretComponent (for now)

* Prep for moving out of draft

* Fixing merge conflict

* Re-added new net frequencies to AI turrets

* Removed turret control content

* Removed unintended change

* Final tweaks

* Fixed incorrect file name

* Improvement to fire mode handling

* Addressed review comments

* Updated how turret wire panel auto-closing is handled

* Ranged NPCs no longer waste shots on stunned targets

* Fixed bug in tracking broken state

* Addressed review comments

* Bug fix

* Removed unnecessary event call
This commit is contained in:
chromiumboy
2025-03-29 12:55:58 -05:00
committed by GitHub
parent 587afe7598
commit dfd3e36a0a
24 changed files with 1005 additions and 30 deletions

View File

@@ -296,7 +296,7 @@ namespace Content.Shared.Damage
DamageChanged(uid, component, new DamageSpecifier());
}
public void SetDamageModifierSetId(EntityUid uid, string damageModifierSetId, DamageableComponent? comp = null)
public void SetDamageModifierSetId(EntityUid uid, string? damageModifierSetId, DamageableComponent? comp = null)
{
if (!_damageableQuery.Resolve(uid, ref comp))
return;

View File

@@ -0,0 +1,161 @@
using Content.Shared.Damage.Prototypes;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.Turrets;
/// <summary>
/// Attached to turrets that can be toggled between an inactive and active state
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(fieldDeltas: true), AutoGenerateComponentPause]
[Access(typeof(SharedDeployableTurretSystem))]
public sealed partial class DeployableTurretComponent : Component
{
/// <summary>
/// Whether the turret is toggled 'on' or 'off'
/// </summary>
[DataField, AutoNetworkedField]
public bool Enabled = false;
/// <summary>
/// The current state of the turret. Used to inform the device network.
/// </summary>
[DataField, AutoNetworkedField]
public DeployableTurretState CurrentState = DeployableTurretState.Retracted;
/// <summary>
/// The visual state of the turret. Used on the client-side.
/// </summary>
[DataField]
public DeployableTurretState VisualState = DeployableTurretState.Retracted;
/// <summary>
/// The physics fixture that will have its collisions disabled when the turret is retracted.
/// </summary>
[DataField]
public string? DeployedFixture = "turret";
/// <summary>
/// When retracted, the following damage modifier set will be applied to the turret.
/// </summary>
[DataField]
public ProtoId<DamageModifierSetPrototype>? RetractedDamageModifierSetId;
/// <summary>
/// When deployed, the following damage modifier set will be applied to the turret.
/// </summary>
[DataField]
public ProtoId<DamageModifierSetPrototype>? DeployedDamageModifierSetId;
#region: Sound data
/// <summary>
/// Sound to play when denied access to the turret.
/// </summary>
[DataField]
public SoundSpecifier AccessDeniedSound = new SoundPathSpecifier("/Audio/Machines/custom_deny.ogg");
/// <summary>
/// Sound to play when the turret deploys.
/// </summary>
[DataField]
public SoundSpecifier DeploymentSound = new SoundPathSpecifier("/Audio/Machines/blastdoor.ogg");
/// <summary>
/// Sound to play when the turret retracts.
/// </summary>
[DataField]
public SoundSpecifier RetractionSound = new SoundPathSpecifier("/Audio/Machines/blastdoor.ogg");
#endregion
#region: Animation data
/// <summary>
/// The length of the deployment animation (in seconds)
/// </summary>
[DataField]
public float DeploymentLength = 1.19f;
/// <summary>
/// The length of the retraction animation (in seconds)
/// </summary>
[DataField]
public float RetractionLength = 1.19f;
/// <summary>
/// The time that the current animation should complete (in seconds)
/// </summary>
[DataField, AutoPausedField]
public TimeSpan AnimationCompletionTime = TimeSpan.Zero;
/// <summary>
/// The animation used when turret activates
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public object DeploymentAnimation = default!;
/// <summary>
/// The animation used when turret deactivates
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public object RetractionAnimation = default!;
/// <summary>
/// The key used to index the animation played when turning the turret on/off.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public const string AnimationKey = "deployable_turret_animation";
#endregion
#region: Visual state data
/// <summary>
/// The visual state to use when the turret is deployed.
/// </summary>
[DataField]
public string DeployedState = "cover_open";
/// <summary>
/// The visual state to use when the turret is not deployed.
/// </summary>
[DataField]
public string RetractedState = "cover_closed";
/// <summary>
/// Used to build the deployment animation when the component is initialized.
/// </summary>
[DataField]
public string DeployingState = "cover_opening";
/// <summary>
/// Used to build the retraction animation when the component is initialized.
/// </summary>
[DataField]
public string RetractingState = "cover_closing";
#endregion
}
[Serializable, NetSerializable]
public enum DeployableTurretVisuals : byte
{
Turret,
Weapon,
Broken,
}
[Serializable, NetSerializable]
public enum DeployableTurretState : byte
{
Retracted = 0,
Deployed = (1 << 0),
Retracting = (1 << 1),
Deploying = (1 << 1) | Deployed,
Firing = (1 << 2) | Deployed,
Disabled = (1 << 3),
Broken = (1 << 4),
}

View File

@@ -0,0 +1,167 @@
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Timing;
using Content.Shared.Verbs;
using Content.Shared.Weapons.Ranged.Events;
using Content.Shared.Wires;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Shared.Turrets;
public abstract partial class SharedDeployableTurretSystem : EntitySystem
{
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly UseDelaySystem _useDelay = default!;
[Dependency] private readonly AccessReaderSystem _accessReader = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedWiresSystem _wires = default!;
[Dependency] private readonly IGameTiming _timing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DeployableTurretComponent, ActivateInWorldEvent>(OnActivate);
SubscribeLocalEvent<DeployableTurretComponent, AttemptChangePanelEvent>(OnAttemptChangeWirePanelWire);
SubscribeLocalEvent<DeployableTurretComponent, GetVerbsEvent<Verb>>(OnGetVerb);
}
private void OnGetVerb(Entity<DeployableTurretComponent> ent, ref GetVerbsEvent<Verb> args)
{
if (!args.CanAccess || !args.CanInteract || !args.CanComplexInteract)
return;
if (!_accessReader.IsAllowed(args.User, ent))
return;
var user = args.User;
var verb = new Verb
{
Priority = 1,
Text = ent.Comp.Enabled ? Loc.GetString("deployable-turret-component-deactivate") : Loc.GetString("deployable-turret-component-activate"),
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/Spare/poweronoff.svg.192dpi.png")),
Disabled = !HasAmmo(ent),
Impact = LogImpact.Low,
Act = () => { TryToggleState(ent, user); }
};
args.Verbs.Add(verb);
}
private void OnActivate(Entity<DeployableTurretComponent> ent, ref ActivateInWorldEvent args)
{
if (TryComp(ent, out UseDelayComponent? useDelay) && !_useDelay.TryResetDelay((ent, useDelay), true))
return;
if (!_accessReader.IsAllowed(args.User, ent))
{
_popup.PopupClient(Loc.GetString("deployable-turret-component-access-denied"), ent, args.User);
_audio.PlayPredicted(ent.Comp.AccessDeniedSound, ent, args.User);
return;
}
TryToggleState(ent, args.User);
}
private void OnAttemptChangeWirePanelWire(Entity<DeployableTurretComponent> ent, ref AttemptChangePanelEvent args)
{
if (!ent.Comp.Enabled || args.Cancelled)
return;
_popup.PopupClient(Loc.GetString("deployable-turret-component-cannot-access-wires"), ent, args.User);
args.Cancelled = true;
}
public bool TryToggleState(Entity<DeployableTurretComponent> ent, EntityUid? user = null)
{
return TrySetState(ent, !ent.Comp.Enabled, user);
}
public bool TrySetState(Entity<DeployableTurretComponent> ent, bool enabled, EntityUid? user = null)
{
if (enabled && ent.Comp.CurrentState == DeployableTurretState.Broken)
{
if (user != null)
_popup.PopupClient(Loc.GetString("deployable-turret-component-is-broken"), ent, user.Value);
return false;
}
if (enabled && !HasAmmo(ent))
{
if (user != null)
_popup.PopupClient(Loc.GetString("deployable-turret-component-no-ammo"), ent, user.Value);
return false;
}
SetState(ent, enabled, user);
return true;
}
protected virtual void SetState(Entity<DeployableTurretComponent> ent, bool enabled, EntityUid? user = null)
{
if (ent.Comp.Enabled == enabled)
return;
// Hide the wires panel UI on activation
if (enabled && TryComp<WiresPanelComponent>(ent, out var wires) && wires.Open)
{
_wires.TogglePanel(ent, wires, false);
_audio.PlayPredicted(wires.ScrewdriverCloseSound, ent, user);
}
// Determine how much time is remaining in the current animation and the one next in queue
// We track this so that when a turret is toggled on/off, we can wait for all queued animations
// to end before the turret's HTN is reactivated
var animTimeRemaining = MathF.Max((float)(ent.Comp.AnimationCompletionTime - _timing.CurTime).TotalSeconds, 0f);
var animTimeNext = enabled ? ent.Comp.DeploymentLength : ent.Comp.RetractionLength;
ent.Comp.AnimationCompletionTime = _timing.CurTime + TimeSpan.FromSeconds(animTimeNext + animTimeRemaining);
// Change the turret's damage modifiers
if (TryComp<DamageableComponent>(ent, out var damageable))
{
var damageSetID = enabled ? ent.Comp.DeployedDamageModifierSetId : ent.Comp.RetractedDamageModifierSetId;
_damageable.SetDamageModifierSetId(ent, damageSetID, damageable);
}
// Change the turret's fixtures
if (ent.Comp.DeployedFixture != null &&
TryComp(ent, out FixturesComponent? fixtures) &&
fixtures.Fixtures.TryGetValue(ent.Comp.DeployedFixture, out var fixture))
{
_physics.SetHard(ent, fixture, enabled);
}
// Play pop up message
var msg = enabled ? "deployable-turret-component-activating" : "deployable-turret-component-deactivating";
_popup.PopupClient(Loc.GetString(msg), ent, user);
// Update enabled state
ent.Comp.Enabled = enabled;
DirtyField(ent, ent.Comp, "Enabled");
}
public bool HasAmmo(Entity<DeployableTurretComponent> ent)
{
var ammoCountEv = new GetAmmoCountEvent();
RaiseLocalEvent(ent, ref ammoCountEv);
return ammoCountEv.Count > 0;
}
}

View File

@@ -43,3 +43,9 @@ public sealed partial class BatteryWeaponFireMode
[DataField]
public float FireCost = 100;
}
[Serializable, NetSerializable]
public enum BatteryWeaponFireModeVisuals : byte
{
State
}

View File

@@ -1,7 +1,8 @@
using System.Linq;
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Popups;
using Content.Shared.Verbs;
using Content.Shared.Weapons.Ranged.Components;
@@ -14,12 +15,14 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly AccessReaderSystem _accessReaderSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BatteryWeaponFireModesComponent, ActivateInWorldEvent>(OnInteractHandEvent);
SubscribeLocalEvent<BatteryWeaponFireModesComponent, UseInHandEvent>(OnUseInHandEvent);
SubscribeLocalEvent<BatteryWeaponFireModesComponent, GetVerbsEvent<Verb>>(OnGetVerb);
SubscribeLocalEvent<BatteryWeaponFireModesComponent, ExaminedEvent>(OnExamined);
}
@@ -44,12 +47,15 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
private void OnGetVerb(EntityUid uid, BatteryWeaponFireModesComponent component, GetVerbsEvent<Verb> args)
{
if (!args.CanAccess || !args.CanInteract || args.Hands == null)
if (!args.CanAccess || !args.CanInteract || !args.CanComplexInteract)
return;
if (component.FireModes.Count < 2)
return;
if (!_accessReaderSystem.IsAllowed(args.User, uid))
return;
for (var i = 0; i < component.FireModes.Count; i++)
{
var fireMode = component.FireModes[i];
@@ -62,11 +68,11 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
Category = VerbCategory.SelectType,
Text = entProto.Name,
Disabled = i == component.CurrentFireMode,
Impact = LogImpact.Low,
Impact = LogImpact.Medium,
DoContactInteraction = true,
Act = () =>
{
SetFireMode(uid, component, index, args.User);
TrySetFireMode(uid, component, index, args.User);
}
};
@@ -74,24 +80,31 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
}
}
private void OnInteractHandEvent(EntityUid uid, BatteryWeaponFireModesComponent component, ActivateInWorldEvent args)
private void OnUseInHandEvent(EntityUid uid, BatteryWeaponFireModesComponent component, UseInHandEvent args)
{
if (!args.Complex)
return;
if (component.FireModes.Count < 2)
return;
CycleFireMode(uid, component, args.User);
TryCycleFireMode(uid, component, args.User);
}
private void CycleFireMode(EntityUid uid, BatteryWeaponFireModesComponent component, EntityUid user)
public void TryCycleFireMode(EntityUid uid, BatteryWeaponFireModesComponent component, EntityUid? user = null)
{
if (component.FireModes.Count < 2)
return;
var index = (component.CurrentFireMode + 1) % component.FireModes.Count;
TrySetFireMode(uid, component, index, user);
}
public bool TrySetFireMode(EntityUid uid, BatteryWeaponFireModesComponent component, int index, EntityUid? user = null)
{
if (index < 0 || index >= component.FireModes.Count)
return false;
if (user != null && !_accessReaderSystem.IsAllowed(user.Value, uid))
return false;
SetFireMode(uid, component, index, user);
return true;
}
private void SetFireMode(EntityUid uid, BatteryWeaponFireModesComponent component, int index, EntityUid? user = null)
@@ -100,26 +113,30 @@ public sealed class BatteryWeaponFireModesSystem : EntitySystem
component.CurrentFireMode = index;
Dirty(uid, component);
if (_prototypeManager.TryIndex<EntityPrototype>(fireMode.Prototype, out var prototype))
{
if (TryComp<AppearanceComponent>(uid, out var appearance))
_appearanceSystem.SetData(uid, BatteryWeaponFireModeVisuals.State, prototype.ID, appearance);
if (user != null)
_popupSystem.PopupClient(Loc.GetString("gun-set-fire-mode", ("mode", prototype.Name)), uid, user.Value);
}
if (TryComp(uid, out ProjectileBatteryAmmoProviderComponent? projectileBatteryAmmoProviderComponent))
{
if (!_prototypeManager.TryIndex<EntityPrototype>(fireMode.Prototype, out var prototype))
return;
// TODO: Have this get the info directly from the batteryComponent when power is moved to shared.
var OldFireCost = projectileBatteryAmmoProviderComponent.FireCost;
projectileBatteryAmmoProviderComponent.Prototype = fireMode.Prototype;
projectileBatteryAmmoProviderComponent.FireCost = fireMode.FireCost;
float FireCostDiff = (float)fireMode.FireCost / (float)OldFireCost;
projectileBatteryAmmoProviderComponent.Shots = (int)Math.Round(projectileBatteryAmmoProviderComponent.Shots/FireCostDiff);
projectileBatteryAmmoProviderComponent.Capacity = (int)Math.Round(projectileBatteryAmmoProviderComponent.Capacity/FireCostDiff);
projectileBatteryAmmoProviderComponent.Shots = (int)Math.Round(projectileBatteryAmmoProviderComponent.Shots / FireCostDiff);
projectileBatteryAmmoProviderComponent.Capacity = (int)Math.Round(projectileBatteryAmmoProviderComponent.Capacity / FireCostDiff);
Dirty(uid, projectileBatteryAmmoProviderComponent);
var updateClientAmmoEvent = new UpdateClientAmmoEvent();
RaiseLocalEvent(uid, ref updateClientAmmoEvent);
if (user != null)
{
_popupSystem.PopupClient(Loc.GetString("gun-set-fire-mode", ("mode", prototype.Name)), uid, user.Value);
}
}
}
}