Re-organize all projects (#4166)
This commit is contained in:
@@ -0,0 +1,393 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Weapon.Ranged.Ammunition.Components;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.NetIDs;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Weapon.Ranged.Barrels.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Shotguns mostly
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class BoltActionBarrelComponent : ServerRangedBarrelComponent, IMapInit, IExamine
|
||||
{
|
||||
// Originally I had this logic shared with PumpBarrel and used a couple of variables to control things
|
||||
// but it felt a lot messier to play around with, especially when adding verbs
|
||||
|
||||
public override string Name => "BoltActionBarrel";
|
||||
public override uint? NetID => ContentNetIDs.BOLTACTION_BARREL;
|
||||
|
||||
public override int ShotsLeft
|
||||
{
|
||||
get
|
||||
{
|
||||
var chamberCount = _chamberContainer.ContainedEntity != null ? 1 : 0;
|
||||
return chamberCount + _spawnedAmmo.Count + _unspawnedCount;
|
||||
}
|
||||
}
|
||||
public override int Capacity => _capacity;
|
||||
[DataField("capacity")]
|
||||
private int _capacity = 6;
|
||||
|
||||
private ContainerSlot _chamberContainer = default!;
|
||||
private Stack<IEntity> _spawnedAmmo = default!;
|
||||
private Container _ammoContainer = default!;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("caliber")]
|
||||
private BallisticCaliber _caliber = BallisticCaliber.Unspecified;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("fillPrototype")]
|
||||
private string? _fillPrototype;
|
||||
[ViewVariables]
|
||||
private int _unspawnedCount;
|
||||
|
||||
public bool BoltOpen
|
||||
{
|
||||
get => _boltOpen;
|
||||
set
|
||||
{
|
||||
if (_boltOpen == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (value)
|
||||
{
|
||||
TryEjectChamber();
|
||||
if (_soundBoltOpen != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundBoltOpen, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TryFeedChamber();
|
||||
if (_soundBoltClosed != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundBoltClosed, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
}
|
||||
|
||||
_boltOpen = value;
|
||||
UpdateAppearance();
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
private bool _boltOpen;
|
||||
[DataField("autoCycle")]
|
||||
private bool _autoCycle;
|
||||
|
||||
private AppearanceComponent? _appearanceComponent;
|
||||
|
||||
// Sounds
|
||||
[DataField("soundCycle")]
|
||||
private string _soundCycle = "/Audio/Weapons/Guns/Cock/sf_rifle_cock.ogg";
|
||||
[DataField("soundBoltOpen")]
|
||||
private string _soundBoltOpen = "/Audio/Weapons/Guns/Bolt/rifle_bolt_open.ogg";
|
||||
[DataField("soundBoltClosed")]
|
||||
private string _soundBoltClosed = "/Audio/Weapons/Guns/Bolt/rifle_bolt_closed.ogg";
|
||||
[DataField("soundInsert")]
|
||||
private string _soundInsert = "/Audio/Weapons/Guns/MagIn/bullet_insert.ogg";
|
||||
|
||||
void IMapInit.MapInit()
|
||||
{
|
||||
if (_fillPrototype != null)
|
||||
{
|
||||
_unspawnedCount += Capacity;
|
||||
if (_unspawnedCount > 0)
|
||||
{
|
||||
_unspawnedCount--;
|
||||
var chamberEntity = Owner.EntityManager.SpawnEntity(_fillPrototype, Owner.Transform.Coordinates);
|
||||
_chamberContainer.Insert(chamberEntity);
|
||||
}
|
||||
}
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
(int, int)? count = (ShotsLeft, Capacity);
|
||||
var chamberedExists = _chamberContainer.ContainedEntity != null;
|
||||
// (Is one chambered?, is the bullet spend)
|
||||
var chamber = (chamberedExists, false);
|
||||
|
||||
if (chamberedExists && _chamberContainer.ContainedEntity!.TryGetComponent<AmmoComponent>(out var ammo))
|
||||
{
|
||||
chamber.Item2 = ammo.Spent;
|
||||
}
|
||||
|
||||
return new BoltActionBarrelComponentState(
|
||||
chamber,
|
||||
FireRateSelector,
|
||||
count,
|
||||
SoundGunshot);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
// TODO: Add existing ammo support on revolvers
|
||||
base.Initialize();
|
||||
_spawnedAmmo = new Stack<IEntity>(_capacity - 1);
|
||||
_ammoContainer = ContainerHelpers.EnsureContainer<Container>(Owner, $"{Name}-ammo-container", out var existing);
|
||||
|
||||
if (existing)
|
||||
{
|
||||
foreach (var entity in _ammoContainer.ContainedEntities)
|
||||
{
|
||||
_spawnedAmmo.Push(entity);
|
||||
_unspawnedCount--;
|
||||
}
|
||||
}
|
||||
|
||||
_chamberContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-chamber-container");
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearanceComponent))
|
||||
{
|
||||
_appearanceComponent = appearanceComponent;
|
||||
}
|
||||
|
||||
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, true);
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
_appearanceComponent?.SetData(BarrelBoltVisuals.BoltOpen, BoltOpen);
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoCount, ShotsLeft);
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoMax, Capacity);
|
||||
}
|
||||
|
||||
public override IEntity? PeekAmmo()
|
||||
{
|
||||
return _chamberContainer.ContainedEntity;
|
||||
}
|
||||
|
||||
public override IEntity? TakeProjectile(EntityCoordinates spawnAt)
|
||||
{
|
||||
var chamberEntity = _chamberContainer.ContainedEntity;
|
||||
if (_autoCycle)
|
||||
{
|
||||
Cycle();
|
||||
}
|
||||
else
|
||||
{
|
||||
Dirty();
|
||||
}
|
||||
|
||||
return chamberEntity?.GetComponentOrNull<AmmoComponent>()?.TakeBullet(spawnAt);
|
||||
}
|
||||
|
||||
protected override bool WeaponCanFire()
|
||||
{
|
||||
if (!base.WeaponCanFire())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return !BoltOpen && _chamberContainer.ContainedEntity != null;
|
||||
}
|
||||
|
||||
private void Cycle(bool manual = false)
|
||||
{
|
||||
TryEjectChamber();
|
||||
TryFeedChamber();
|
||||
|
||||
if (_chamberContainer.ContainedEntity == null && manual)
|
||||
{
|
||||
BoltOpen = true;
|
||||
if (Owner.TryGetContainer(out var container))
|
||||
{
|
||||
Owner.PopupMessage(container.Owner, Loc.GetString("Bolt opened"));
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_soundCycle))
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundCycle, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
}
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public bool TryInsertBullet(IEntity user, IEntity ammo)
|
||||
{
|
||||
if (!ammo.TryGetComponent(out AmmoComponent? ammoComponent))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ammoComponent.Caliber != _caliber)
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("Wrong caliber"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!BoltOpen)
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("Bolt isn't open"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_chamberContainer.ContainedEntity == null)
|
||||
{
|
||||
_chamberContainer.Insert(ammo);
|
||||
if (_soundInsert != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundInsert, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_ammoContainer.ContainedEntities.Count < Capacity - 1)
|
||||
{
|
||||
_ammoContainer.Insert(ammo);
|
||||
_spawnedAmmo.Push(ammo);
|
||||
if (_soundInsert != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundInsert, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return true;
|
||||
}
|
||||
|
||||
Owner.PopupMessage(user, Loc.GetString("No room"));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override bool UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
if (BoltOpen)
|
||||
{
|
||||
BoltOpen = false;
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Bolt closed"));
|
||||
return true;
|
||||
}
|
||||
|
||||
Cycle(true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
return TryInsertBullet(eventArgs.User, eventArgs.Using);
|
||||
}
|
||||
|
||||
private bool TryEjectChamber()
|
||||
{
|
||||
var chamberedEntity = _chamberContainer.ContainedEntity;
|
||||
if (chamberedEntity != null)
|
||||
{
|
||||
if (!_chamberContainer.Remove(chamberedEntity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!chamberedEntity.GetComponent<AmmoComponent>().Caseless)
|
||||
{
|
||||
EjectCasing(chamberedEntity);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryFeedChamber()
|
||||
{
|
||||
if (_chamberContainer.ContainedEntity != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_spawnedAmmo.TryPop(out var next))
|
||||
{
|
||||
_ammoContainer.Remove(next);
|
||||
_chamberContainer.Insert(next);
|
||||
return true;
|
||||
}
|
||||
else if (_unspawnedCount > 0)
|
||||
{
|
||||
_unspawnedCount--;
|
||||
var ammoEntity = Owner.EntityManager.SpawnEntity(_fillPrototype, Owner.Transform.Coordinates);
|
||||
_chamberContainer.Insert(ammoEntity);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public override void Examine(FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
base.Examine(message, inDetailsRange);
|
||||
|
||||
message.AddMarkup(Loc.GetString("\nIt uses [color=white]{0}[/color] ammo.", _caliber));
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class OpenBoltVerb : Verb<BoltActionBarrelComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, BoltActionBarrelComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString("Open bolt");
|
||||
data.Visibility = component.BoltOpen ? VerbVisibility.Invisible : VerbVisibility.Visible;
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, BoltActionBarrelComponent component)
|
||||
{
|
||||
component.BoltOpen = true;
|
||||
}
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class CloseBoltVerb : Verb<BoltActionBarrelComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, BoltActionBarrelComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString("Close bolt");
|
||||
data.Visibility = component.BoltOpen ? VerbVisibility.Visible : VerbVisibility.Invisible;
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, BoltActionBarrelComponent component)
|
||||
{
|
||||
component.BoltOpen = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Weapon.Ranged.Ammunition.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.NetIDs;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Weapon.Ranged.Barrels.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Bolt-action rifles
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class PumpBarrelComponent : ServerRangedBarrelComponent, IMapInit, ISerializationHooks
|
||||
{
|
||||
public override string Name => "PumpBarrel";
|
||||
public override uint? NetID => ContentNetIDs.PUMP_BARREL;
|
||||
|
||||
public override int ShotsLeft
|
||||
{
|
||||
get
|
||||
{
|
||||
var chamberCount = _chamberContainer.ContainedEntity != null ? 1 : 0;
|
||||
return chamberCount + _spawnedAmmo.Count + _unspawnedCount;
|
||||
}
|
||||
}
|
||||
|
||||
private const int DefaultCapacity = 6;
|
||||
[DataField("capacity")]
|
||||
public override int Capacity { get; } = DefaultCapacity;
|
||||
|
||||
// Even a point having a chamber? I guess it makes some of the below code cleaner
|
||||
private ContainerSlot _chamberContainer = default!;
|
||||
private Stack<IEntity> _spawnedAmmo = new (DefaultCapacity-1);
|
||||
private Container _ammoContainer = default!;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("caliber")]
|
||||
private BallisticCaliber _caliber = BallisticCaliber.Unspecified;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("fillPrototype")]
|
||||
private string? _fillPrototype;
|
||||
[ViewVariables]
|
||||
private int _unspawnedCount;
|
||||
|
||||
[DataField("manualCycle")]
|
||||
private bool _manualCycle = true;
|
||||
|
||||
private AppearanceComponent? _appearanceComponent;
|
||||
|
||||
// Sounds
|
||||
[DataField("soundCycle")]
|
||||
private string _soundCycle = "/Audio/Weapons/Guns/Cock/sf_rifle_cock.ogg";
|
||||
|
||||
[DataField("soundInsert")]
|
||||
private string _soundInsert = "/Audio/Weapons/Guns/MagIn/bullet_insert.ogg";
|
||||
|
||||
void IMapInit.MapInit()
|
||||
{
|
||||
if (_fillPrototype != null)
|
||||
{
|
||||
_unspawnedCount += Capacity - 1;
|
||||
}
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
(int, int)? count = (ShotsLeft, Capacity);
|
||||
var chamberedExists = _chamberContainer.ContainedEntity != null;
|
||||
// (Is one chambered?, is the bullet spend)
|
||||
var chamber = (chamberedExists, false);
|
||||
|
||||
if (chamberedExists && _chamberContainer.ContainedEntity!.TryGetComponent<AmmoComponent>(out var ammo))
|
||||
{
|
||||
chamber.Item2 = ammo.Spent;
|
||||
}
|
||||
return new PumpBarrelComponentState(
|
||||
chamber,
|
||||
FireRateSelector,
|
||||
count,
|
||||
SoundGunshot);
|
||||
}
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
_spawnedAmmo = new Stack<IEntity>(Capacity - 1);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_ammoContainer =
|
||||
ContainerHelpers.EnsureContainer<Container>(Owner, $"{Name}-ammo-container", out var existing);
|
||||
|
||||
if (existing)
|
||||
{
|
||||
foreach (var entity in _ammoContainer.ContainedEntities)
|
||||
{
|
||||
_spawnedAmmo.Push(entity);
|
||||
_unspawnedCount--;
|
||||
}
|
||||
}
|
||||
|
||||
_chamberContainer =
|
||||
ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-chamber-container", out existing);
|
||||
if (existing)
|
||||
{
|
||||
_unspawnedCount--;
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearanceComponent))
|
||||
{
|
||||
_appearanceComponent = appearanceComponent;
|
||||
}
|
||||
|
||||
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, true);
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoCount, ShotsLeft);
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoMax, Capacity);
|
||||
}
|
||||
|
||||
public override IEntity? PeekAmmo()
|
||||
{
|
||||
return _chamberContainer.ContainedEntity;
|
||||
}
|
||||
|
||||
public override IEntity? TakeProjectile(EntityCoordinates spawnAt)
|
||||
{
|
||||
var chamberEntity = _chamberContainer.ContainedEntity;
|
||||
if (!_manualCycle)
|
||||
{
|
||||
Cycle();
|
||||
}
|
||||
else
|
||||
{
|
||||
Dirty();
|
||||
}
|
||||
|
||||
return chamberEntity?.GetComponentOrNull<AmmoComponent>()?.TakeBullet(spawnAt);
|
||||
}
|
||||
|
||||
private void Cycle(bool manual = false)
|
||||
{
|
||||
var chamberedEntity = _chamberContainer.ContainedEntity;
|
||||
if (chamberedEntity != null)
|
||||
{
|
||||
_chamberContainer.Remove(chamberedEntity);
|
||||
var ammoComponent = chamberedEntity.GetComponent<AmmoComponent>();
|
||||
if (!ammoComponent.Caseless)
|
||||
{
|
||||
EjectCasing(chamberedEntity);
|
||||
}
|
||||
}
|
||||
|
||||
if (_spawnedAmmo.TryPop(out var next))
|
||||
{
|
||||
_ammoContainer.Remove(next);
|
||||
_chamberContainer.Insert(next);
|
||||
}
|
||||
|
||||
if (_unspawnedCount > 0)
|
||||
{
|
||||
_unspawnedCount--;
|
||||
var ammoEntity = Owner.EntityManager.SpawnEntity(_fillPrototype, Owner.Transform.Coordinates);
|
||||
_chamberContainer.Insert(ammoEntity);
|
||||
}
|
||||
|
||||
if (manual)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_soundCycle))
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundCycle, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
}
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public bool TryInsertBullet(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.Using.TryGetComponent(out AmmoComponent? ammoComponent))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ammoComponent.Caliber != _caliber)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Wrong caliber"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_ammoContainer.ContainedEntities.Count < Capacity - 1)
|
||||
{
|
||||
_ammoContainer.Insert(eventArgs.Using);
|
||||
_spawnedAmmo.Push(eventArgs.Using);
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
if (_soundInsert != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundInsert, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("No room"));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override bool UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
Cycle(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
return TryInsertBullet(eventArgs);
|
||||
}
|
||||
|
||||
public override void Examine(FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
base.Examine(message, inDetailsRange);
|
||||
|
||||
message.AddMarkup(Loc.GetString("\nIt uses [color=white]{0}[/color] ammo.", _caliber));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Weapon.Ranged.Ammunition.Components;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.NetIDs;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Weapon.Ranged.Barrels.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class RevolverBarrelComponent : ServerRangedBarrelComponent, ISerializationHooks
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override string Name => "RevolverBarrel";
|
||||
public override uint? NetID => ContentNetIDs.REVOLVER_BARREL;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("caliber")]
|
||||
private BallisticCaliber _caliber = BallisticCaliber.Unspecified;
|
||||
|
||||
private Container _ammoContainer = default!;
|
||||
|
||||
[ViewVariables]
|
||||
private int _currentSlot;
|
||||
|
||||
public override int Capacity => _ammoSlots.Length;
|
||||
|
||||
[DataField("capacity")]
|
||||
private int _serializedCapacity = 6;
|
||||
|
||||
[DataField("ammoSlots", readOnly: true)]
|
||||
private IEntity?[] _ammoSlots = Array.Empty<IEntity?>();
|
||||
|
||||
public override int ShotsLeft => _ammoContainer.ContainedEntities.Count;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("fillPrototype")]
|
||||
private string? _fillPrototype;
|
||||
|
||||
[ViewVariables]
|
||||
private int _unspawnedCount;
|
||||
|
||||
// Sounds
|
||||
[DataField("soundEject")]
|
||||
private string _soundEject = "/Audio/Weapons/Guns/MagOut/revolver_magout.ogg";
|
||||
|
||||
[DataField("soundInsert")]
|
||||
private string _soundInsert = "/Audio/Weapons/Guns/MagIn/revolver_magin.ogg";
|
||||
|
||||
[DataField("soundSpin")]
|
||||
private string _soundSpin = "/Audio/Weapons/Guns/Misc/revolver_spin.ogg";
|
||||
|
||||
void ISerializationHooks.BeforeSerialization()
|
||||
{
|
||||
_serializedCapacity = _ammoSlots.Length;
|
||||
}
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
_ammoSlots = new IEntity[_serializedCapacity];
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
var slotsSpent = new bool?[Capacity];
|
||||
for (var i = 0; i < Capacity; i++)
|
||||
{
|
||||
slotsSpent[i] = null;
|
||||
var ammoEntity = _ammoSlots[i];
|
||||
if (ammoEntity != null && ammoEntity.TryGetComponent(out AmmoComponent? ammo))
|
||||
{
|
||||
slotsSpent[i] = ammo.Spent;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: make yaml var to not sent currentSlot/UI? (for russian roulette)
|
||||
return new RevolverBarrelComponentState(
|
||||
_currentSlot,
|
||||
FireRateSelector,
|
||||
slotsSpent,
|
||||
SoundGunshot);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_unspawnedCount = Capacity;
|
||||
int idx = 0;
|
||||
_ammoContainer = ContainerHelpers.EnsureContainer<Container>(Owner, $"{Name}-ammoContainer", out var existing);
|
||||
if (existing)
|
||||
{
|
||||
foreach (var entity in _ammoContainer.ContainedEntities)
|
||||
{
|
||||
_unspawnedCount--;
|
||||
_ammoSlots[idx] = entity;
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < _unspawnedCount; i++)
|
||||
{
|
||||
var entity = Owner.EntityManager.SpawnEntity(_fillPrototype, Owner.Transform.Coordinates);
|
||||
_ammoSlots[idx] = entity;
|
||||
_ammoContainer.Insert(entity);
|
||||
idx++;
|
||||
}
|
||||
|
||||
UpdateAppearance();
|
||||
Dirty();
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Placeholder, at this stage it's just here for the RPG
|
||||
appearance.SetData(MagazineBarrelVisuals.MagLoaded, ShotsLeft > 0);
|
||||
appearance.SetData(AmmoVisuals.AmmoCount, ShotsLeft);
|
||||
appearance.SetData(AmmoVisuals.AmmoMax, Capacity);
|
||||
}
|
||||
|
||||
public bool TryInsertBullet(IEntity user, IEntity entity)
|
||||
{
|
||||
if (!entity.TryGetComponent(out AmmoComponent? ammoComponent))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ammoComponent.Caliber != _caliber)
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("Wrong caliber"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Functions like a stack
|
||||
// These are inserted in reverse order but then when fired Cycle will go through in order
|
||||
// The reason we don't just use an actual stack is because spin can select a random slot to point at
|
||||
for (var i = _ammoSlots.Length - 1; i >= 0; i--)
|
||||
{
|
||||
var slot = _ammoSlots[i];
|
||||
if (slot == null)
|
||||
{
|
||||
_currentSlot = i;
|
||||
_ammoSlots[i] = entity;
|
||||
_ammoContainer.Insert(entity);
|
||||
if (_soundInsert != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundInsert, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Owner.PopupMessage(user, Loc.GetString("Ammo full"));
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Cycle()
|
||||
{
|
||||
// Move up a slot
|
||||
_currentSlot = (_currentSlot + 1) % _ammoSlots.Length;
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Russian Roulette
|
||||
/// </summary>
|
||||
public void Spin()
|
||||
{
|
||||
var random = _random.Next(_ammoSlots.Length - 1);
|
||||
_currentSlot = random;
|
||||
if (!string.IsNullOrEmpty(_soundSpin))
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundSpin, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public override IEntity? PeekAmmo()
|
||||
{
|
||||
return _ammoSlots[_currentSlot];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes a projectile out if possible
|
||||
/// IEnumerable just to make supporting shotguns saner
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public override IEntity? TakeProjectile(EntityCoordinates spawnAt)
|
||||
{
|
||||
var ammo = _ammoSlots[_currentSlot];
|
||||
IEntity? bullet = null;
|
||||
if (ammo != null)
|
||||
{
|
||||
var ammoComponent = ammo.GetComponent<AmmoComponent>();
|
||||
bullet = ammoComponent.TakeBullet(spawnAt);
|
||||
if (ammoComponent.Caseless)
|
||||
{
|
||||
_ammoSlots[_currentSlot] = null;
|
||||
_ammoContainer.Remove(ammo);
|
||||
}
|
||||
}
|
||||
Cycle();
|
||||
UpdateAppearance();
|
||||
return bullet;
|
||||
}
|
||||
|
||||
private void EjectAllSlots()
|
||||
{
|
||||
for (var i = 0; i < _ammoSlots.Length; i++)
|
||||
{
|
||||
var entity = _ammoSlots[i];
|
||||
if (entity == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_ammoContainer.Remove(entity);
|
||||
EjectCasing(entity);
|
||||
_ammoSlots[i] = null;
|
||||
}
|
||||
|
||||
if (_ammoContainer.ContainedEntities.Count > 0)
|
||||
{
|
||||
if (_soundEject != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundEject, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-1));
|
||||
}
|
||||
}
|
||||
|
||||
// May as well point back at the end?
|
||||
_currentSlot = _ammoSlots.Length - 1;
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Eject all casings
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public override bool UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
EjectAllSlots();
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
return TryInsertBullet(eventArgs.User, eventArgs.Using);
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class SpinRevolverVerb : Verb<RevolverBarrelComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, RevolverBarrelComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString("Spin");
|
||||
if (component.Capacity <= 1)
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Visibility = component.ShotsLeft > 0 ? VerbVisibility.Visible : VerbVisibility.Disabled;
|
||||
data.IconTexture = "/Textures/Interface/VerbIcons/refresh.svg.192dpi.png";
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, RevolverBarrelComponent component)
|
||||
{
|
||||
component.Spin();
|
||||
component.Owner.PopupMessage(user, Loc.GetString("Spun the cylinder"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Battery.Components;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Items;
|
||||
using Content.Server.Projectiles.Components;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.NetIDs;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Weapon.Ranged.Barrels.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class ServerBatteryBarrelComponent : ServerRangedBarrelComponent
|
||||
{
|
||||
public override string Name => "BatteryBarrel";
|
||||
public override uint? NetID => ContentNetIDs.BATTERY_BARREL;
|
||||
|
||||
// The minimum change we need before we can fire
|
||||
[DataField("lowerChargeLimit")]
|
||||
[ViewVariables] private float _lowerChargeLimit = 10;
|
||||
[DataField("fireCost")]
|
||||
[ViewVariables] private int _baseFireCost = 300;
|
||||
// What gets fired
|
||||
[DataField("ammoPrototype")]
|
||||
[ViewVariables] private string? _ammoPrototype;
|
||||
|
||||
[ViewVariables] public IEntity? PowerCellEntity => _powerCellContainer.ContainedEntity;
|
||||
public BatteryComponent? PowerCell => _powerCellContainer.ContainedEntity?.GetComponentOrNull<BatteryComponent>();
|
||||
private ContainerSlot _powerCellContainer = default!;
|
||||
private ContainerSlot _ammoContainer = default!;
|
||||
[DataField("powerCellPrototype")]
|
||||
private string? _powerCellPrototype = default;
|
||||
[DataField("powerCellRemovable")]
|
||||
[ViewVariables] private bool _powerCellRemovable = default;
|
||||
|
||||
public override int ShotsLeft
|
||||
{
|
||||
get
|
||||
{
|
||||
var powerCell = _powerCellContainer.ContainedEntity;
|
||||
|
||||
if (powerCell == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) Math.Ceiling(powerCell.GetComponent<BatteryComponent>().CurrentCharge / _baseFireCost);
|
||||
}
|
||||
}
|
||||
|
||||
public override int Capacity
|
||||
{
|
||||
get
|
||||
{
|
||||
var powerCell = _powerCellContainer.ContainedEntity;
|
||||
|
||||
if (powerCell == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) Math.Ceiling((float) (powerCell.GetComponent<BatteryComponent>().MaxCharge / _baseFireCost));
|
||||
}
|
||||
}
|
||||
|
||||
private AppearanceComponent? _appearanceComponent;
|
||||
|
||||
// Sounds
|
||||
[DataField("soundPowerCellInsert")]
|
||||
private string? _soundPowerCellInsert = default;
|
||||
[DataField("soundPowerCellEject")]
|
||||
private string? _soundPowerCellEject = default;
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
(int, int)? count = (ShotsLeft, Capacity);
|
||||
|
||||
return new BatteryBarrelComponentState(
|
||||
FireRateSelector,
|
||||
count);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_powerCellContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-powercell-container", out var existing);
|
||||
if (!existing && _powerCellPrototype != null)
|
||||
{
|
||||
var powerCellEntity = Owner.EntityManager.SpawnEntity(_powerCellPrototype, Owner.Transform.Coordinates);
|
||||
_powerCellContainer.Insert(powerCellEntity);
|
||||
}
|
||||
|
||||
if (_ammoPrototype != null)
|
||||
{
|
||||
_ammoContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-ammo-container");
|
||||
}
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearanceComponent))
|
||||
{
|
||||
_appearanceComponent = appearanceComponent;
|
||||
}
|
||||
Dirty();
|
||||
}
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public void UpdateAppearance()
|
||||
{
|
||||
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, _powerCellContainer.ContainedEntity != null);
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoCount, ShotsLeft);
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoMax, Capacity);
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public override IEntity PeekAmmo()
|
||||
{
|
||||
// Spawn a dummy entity because it's easier to work with I guess
|
||||
// This will get re-used for the projectile
|
||||
var ammo = _ammoContainer.ContainedEntity;
|
||||
if (ammo == null)
|
||||
{
|
||||
ammo = Owner.EntityManager.SpawnEntity(_ammoPrototype, Owner.Transform.Coordinates);
|
||||
_ammoContainer.Insert(ammo);
|
||||
}
|
||||
|
||||
return ammo;
|
||||
}
|
||||
|
||||
public override IEntity? TakeProjectile(EntityCoordinates spawnAt)
|
||||
{
|
||||
var powerCellEntity = _powerCellContainer.ContainedEntity;
|
||||
|
||||
if (powerCellEntity == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var capacitor = powerCellEntity.GetComponent<BatteryComponent>();
|
||||
if (capacitor.CurrentCharge < _lowerChargeLimit)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Can fire confirmed
|
||||
// Multiply the entity's damage / whatever by the percentage of charge the shot has.
|
||||
IEntity entity;
|
||||
var chargeChange = Math.Min(capacitor.CurrentCharge, _baseFireCost);
|
||||
if (capacitor.UseCharge(chargeChange) < _lowerChargeLimit)
|
||||
{
|
||||
// Handling of funny exploding cells.
|
||||
return null;
|
||||
}
|
||||
var energyRatio = chargeChange / _baseFireCost;
|
||||
|
||||
if (_ammoContainer.ContainedEntity != null)
|
||||
{
|
||||
entity = _ammoContainer.ContainedEntity;
|
||||
_ammoContainer.Remove(entity);
|
||||
entity.Transform.Coordinates = spawnAt;
|
||||
}
|
||||
else
|
||||
{
|
||||
entity = Owner.EntityManager.SpawnEntity(_ammoPrototype, spawnAt);
|
||||
}
|
||||
|
||||
if (entity.TryGetComponent(out ProjectileComponent? projectileComponent))
|
||||
{
|
||||
if (energyRatio < 1.0)
|
||||
{
|
||||
var newDamages = new Dictionary<DamageType, int>(projectileComponent.Damages.Count);
|
||||
foreach (var (damageType, damage) in projectileComponent.Damages)
|
||||
{
|
||||
newDamages.Add(damageType, (int) (damage * energyRatio));
|
||||
}
|
||||
|
||||
projectileComponent.Damages = newDamages;
|
||||
}
|
||||
} else if (entity.TryGetComponent(out HitscanComponent? hitscanComponent))
|
||||
{
|
||||
hitscanComponent.Damage *= energyRatio;
|
||||
hitscanComponent.ColorModifier = energyRatio;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Ammo doesn't have hitscan or projectile?");
|
||||
}
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public bool TryInsertPowerCell(IEntity entity)
|
||||
{
|
||||
if (_powerCellContainer.ContainedEntity != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!entity.HasComponent<BatteryComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_soundPowerCellInsert != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundPowerCellInsert, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
|
||||
_powerCellContainer.Insert(entity);
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override bool UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
if (!_powerCellRemovable)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (PowerCellEntity == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return TryEjectCell(eventArgs.User);
|
||||
}
|
||||
|
||||
private bool TryEjectCell(IEntity user)
|
||||
{
|
||||
if (PowerCell == null || !_powerCellRemovable)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!user.TryGetComponent(out HandsComponent? hands))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var cell = PowerCell;
|
||||
if (!_powerCellContainer.Remove(cell.Owner))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
|
||||
if (!hands.PutInHand(cell.Owner.GetComponent<ItemComponent>()))
|
||||
{
|
||||
cell.Owner.Transform.Coordinates = user.Transform.Coordinates;
|
||||
}
|
||||
|
||||
if (_soundPowerCellEject != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundPowerCellEject, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.Using.HasComponent<BatteryComponent>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return TryInsertPowerCell(eventArgs.Using);
|
||||
}
|
||||
|
||||
[Verb]
|
||||
public sealed class EjectCellVerb : Verb<ServerBatteryBarrelComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, ServerBatteryBarrelComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user) || !component._powerCellRemovable)
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
if (component.PowerCell == null)
|
||||
{
|
||||
data.Text = Loc.GetString("No cell");
|
||||
data.Visibility = VerbVisibility.Disabled;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.Text = Loc.GetString("Eject cell");
|
||||
data.IconTexture = "/Textures/Interface/VerbIcons/eject.svg.192dpi.png";
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, ServerBatteryBarrelComponent component)
|
||||
{
|
||||
component.TryEjectCell(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,539 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Items;
|
||||
using Content.Server.Weapon.Ranged.Ammunition.Components;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.NetIDs;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Weapons.Ranged;
|
||||
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Weapon.Ranged.Barrels.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class ServerMagazineBarrelComponent : ServerRangedBarrelComponent, IExamine
|
||||
{
|
||||
public override string Name => "MagazineBarrel";
|
||||
public override uint? NetID => ContentNetIDs.MAGAZINE_BARREL;
|
||||
|
||||
[ViewVariables]
|
||||
private ContainerSlot _chamberContainer = default!;
|
||||
[ViewVariables] public bool HasMagazine => _magazineContainer.ContainedEntity != null;
|
||||
private ContainerSlot _magazineContainer = default!;
|
||||
|
||||
[ViewVariables] public MagazineType MagazineTypes => _magazineTypes;
|
||||
[DataField("magazineTypes")]
|
||||
private MagazineType _magazineTypes = default;
|
||||
[ViewVariables] public BallisticCaliber Caliber => _caliber;
|
||||
[DataField("caliber")]
|
||||
private BallisticCaliber _caliber = BallisticCaliber.Unspecified;
|
||||
|
||||
public override int ShotsLeft
|
||||
{
|
||||
get
|
||||
{
|
||||
var count = 0;
|
||||
if (_chamberContainer.ContainedEntity != null)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
var magazine = _magazineContainer.ContainedEntity;
|
||||
if (magazine != null)
|
||||
{
|
||||
count += magazine.GetComponent<RangedMagazineComponent>().ShotsLeft;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
public override int Capacity
|
||||
{
|
||||
get
|
||||
{
|
||||
// Chamber
|
||||
var count = 1;
|
||||
var magazine = _magazineContainer.ContainedEntity;
|
||||
if (magazine != null)
|
||||
{
|
||||
count += magazine.GetComponent<RangedMagazineComponent>().Capacity;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
[DataField("magFillPrototype")]
|
||||
private string? _magFillPrototype;
|
||||
|
||||
public bool BoltOpen
|
||||
{
|
||||
get => _boltOpen;
|
||||
set
|
||||
{
|
||||
if (_boltOpen == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (value)
|
||||
{
|
||||
TryEjectChamber();
|
||||
if (_soundBoltOpen != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundBoltOpen, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TryFeedChamber();
|
||||
if (_soundBoltClosed != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundBoltClosed, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
}
|
||||
|
||||
_boltOpen = value;
|
||||
UpdateAppearance();
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
private bool _boltOpen = true;
|
||||
|
||||
[DataField("autoEjectMag")]
|
||||
private bool _autoEjectMag;
|
||||
// If the bolt needs to be open before we can insert / remove the mag (i.e. for LMGs)
|
||||
public bool MagNeedsOpenBolt => _magNeedsOpenBolt;
|
||||
[DataField("magNeedsOpenBolt")]
|
||||
private bool _magNeedsOpenBolt = default;
|
||||
|
||||
private AppearanceComponent? _appearanceComponent;
|
||||
|
||||
// Sounds
|
||||
[DataField("soundBoltOpen")]
|
||||
private string? _soundBoltOpen = default;
|
||||
[DataField("soundBoltClosed")]
|
||||
private string? _soundBoltClosed = default;
|
||||
[DataField("soundRack")]
|
||||
private string? _soundRack = default;
|
||||
[DataField("soundMagInsert")]
|
||||
private string? _soundMagInsert = default;
|
||||
[DataField("soundMagEject")]
|
||||
private string? _soundMagEject = default;
|
||||
[DataField("soundAutoEject")]
|
||||
private string _soundAutoEject = "/Audio/Weapons/Guns/EmptyAlarm/smg_empty_alarm.ogg";
|
||||
|
||||
private List<MagazineType> GetMagazineTypes()
|
||||
{
|
||||
var types = new List<MagazineType>();
|
||||
|
||||
foreach (MagazineType mag in Enum.GetValues(typeof(MagazineType)))
|
||||
{
|
||||
if ((_magazineTypes & mag) != 0)
|
||||
{
|
||||
types.Add(mag);
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
(int, int)? count = null;
|
||||
var magazine = _magazineContainer.ContainedEntity;
|
||||
if (magazine != null && magazine.TryGetComponent(out RangedMagazineComponent? rangedMagazineComponent))
|
||||
{
|
||||
count = (rangedMagazineComponent.ShotsLeft, rangedMagazineComponent.Capacity);
|
||||
}
|
||||
|
||||
return new MagazineBarrelComponentState(
|
||||
_chamberContainer.ContainedEntity != null,
|
||||
FireRateSelector,
|
||||
count,
|
||||
SoundGunshot);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearanceComponent))
|
||||
{
|
||||
_appearanceComponent = appearanceComponent;
|
||||
}
|
||||
|
||||
_chamberContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-chamber");
|
||||
_magazineContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-magazine", out var existing);
|
||||
|
||||
if (!existing && _magFillPrototype != null)
|
||||
{
|
||||
var magEntity = Owner.EntityManager.SpawnEntity(_magFillPrototype, Owner.Transform.Coordinates);
|
||||
_magazineContainer.Insert(magEntity);
|
||||
}
|
||||
Dirty();
|
||||
}
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public override IEntity? PeekAmmo()
|
||||
{
|
||||
return BoltOpen ? null : _chamberContainer.ContainedEntity;
|
||||
}
|
||||
|
||||
public override IEntity? TakeProjectile(EntityCoordinates spawnAt)
|
||||
{
|
||||
if (BoltOpen)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var entity = _chamberContainer.ContainedEntity;
|
||||
|
||||
Cycle();
|
||||
return entity?.GetComponent<AmmoComponent>().TakeBullet(spawnAt);
|
||||
}
|
||||
|
||||
private void Cycle(bool manual = false)
|
||||
{
|
||||
if (BoltOpen)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TryEjectChamber();
|
||||
|
||||
TryFeedChamber();
|
||||
|
||||
if (_chamberContainer.ContainedEntity == null && !BoltOpen)
|
||||
{
|
||||
if (_soundBoltOpen != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundBoltOpen, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-5));
|
||||
}
|
||||
|
||||
if (Owner.TryGetContainer(out var container))
|
||||
{
|
||||
Owner.PopupMessage(container.Owner, Loc.GetString("Bolt open"));
|
||||
}
|
||||
BoltOpen = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (manual)
|
||||
{
|
||||
if (_soundRack != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundRack, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
}
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
private void UpdateAppearance()
|
||||
{
|
||||
_appearanceComponent?.SetData(BarrelBoltVisuals.BoltOpen, BoltOpen);
|
||||
_appearanceComponent?.SetData(MagazineBarrelVisuals.MagLoaded, _magazineContainer.ContainedEntity != null);
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoCount, ShotsLeft);
|
||||
_appearanceComponent?.SetData(AmmoVisuals.AmmoMax, Capacity);
|
||||
}
|
||||
|
||||
public override bool UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
// Behavior:
|
||||
// If bolt open just close it
|
||||
// If bolt closed then cycle
|
||||
// If we cycle then get next round
|
||||
// If no more round then open bolt
|
||||
|
||||
if (BoltOpen)
|
||||
{
|
||||
if (_soundBoltClosed != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundBoltClosed, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-5));
|
||||
}
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Bolt closed"));
|
||||
BoltOpen = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Could play a rack-slide specific sound here if you're so inclined (if the chamber is empty but rounds are available)
|
||||
|
||||
Cycle(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryEjectChamber()
|
||||
{
|
||||
var chamberEntity = _chamberContainer.ContainedEntity;
|
||||
if (chamberEntity != null)
|
||||
{
|
||||
if (!_chamberContainer.Remove(chamberEntity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var ammoComponent = chamberEntity.GetComponent<AmmoComponent>();
|
||||
if (!ammoComponent.Caseless)
|
||||
{
|
||||
EjectCasing(chamberEntity);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryFeedChamber()
|
||||
{
|
||||
if (_chamberContainer.ContainedEntity != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try and pull a round from the magazine to replace the chamber if possible
|
||||
var magazine = _magazineContainer.ContainedEntity;
|
||||
var nextRound = magazine?.GetComponent<RangedMagazineComponent>().TakeAmmo();
|
||||
|
||||
if (nextRound == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_chamberContainer.Insert(nextRound);
|
||||
|
||||
if (_autoEjectMag && magazine != null && magazine.GetComponent<RangedMagazineComponent>().ShotsLeft == 0)
|
||||
{
|
||||
if (_soundAutoEject != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundAutoEject, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
|
||||
_magazineContainer.Remove(magazine);
|
||||
SendNetworkMessage(new MagazineAutoEjectMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void RemoveMagazine(IEntity user)
|
||||
{
|
||||
var mag = _magazineContainer.ContainedEntity;
|
||||
|
||||
if (mag == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (MagNeedsOpenBolt && !BoltOpen)
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("Bolt needs to be open"));
|
||||
return;
|
||||
}
|
||||
|
||||
_magazineContainer.Remove(mag);
|
||||
if (_soundMagEject != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundMagEject, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
|
||||
if (user.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
handsComponent.PutInHandOrDrop(mag.GetComponent<ItemComponent>());
|
||||
}
|
||||
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
}
|
||||
|
||||
public override async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
// Insert magazine
|
||||
if (eventArgs.Using.TryGetComponent(out RangedMagazineComponent? magazineComponent))
|
||||
{
|
||||
if ((MagazineTypes & magazineComponent.MagazineType) == 0)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Wrong magazine type"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (magazineComponent.Caliber != _caliber)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Wrong caliber"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_magNeedsOpenBolt && !BoltOpen)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Need to open bolt first"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_magazineContainer.ContainedEntity == null)
|
||||
{
|
||||
if (_soundMagInsert != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(Owner), _soundMagInsert, Owner.Transform.Coordinates, AudioParams.Default.WithVolume(-2));
|
||||
}
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Magazine inserted"));
|
||||
_magazineContainer.Insert(eventArgs.Using);
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return true;
|
||||
}
|
||||
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Already holding a magazine"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Insert 1 ammo
|
||||
if (eventArgs.Using.TryGetComponent(out AmmoComponent? ammoComponent))
|
||||
{
|
||||
if (!BoltOpen)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Cannot insert ammo while bolt is closed"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ammoComponent.Caliber != _caliber)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Wrong caliber"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_chamberContainer.ContainedEntity == null)
|
||||
{
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Ammo inserted"));
|
||||
_chamberContainer.Insert(eventArgs.Using);
|
||||
Dirty();
|
||||
UpdateAppearance();
|
||||
return true;
|
||||
}
|
||||
|
||||
Owner.PopupMessage(eventArgs.User, Loc.GetString("Chamber full"));
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override void Examine(FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
base.Examine(message, inDetailsRange);
|
||||
|
||||
message.AddMarkup(Loc.GetString("\nIt uses [color=white]{0}[/color] ammo.", Caliber));
|
||||
|
||||
foreach (var magazineType in GetMagazineTypes())
|
||||
{
|
||||
message.AddMarkup(Loc.GetString("\nIt accepts [color=white]{0}[/color] magazines.", magazineType));
|
||||
}
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class EjectMagazineVerb : Verb<ServerMagazineBarrelComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, ServerMagazineBarrelComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString("Eject magazine");
|
||||
data.IconTexture = "/Textures/Interface/VerbIcons/eject.svg.192dpi.png";
|
||||
if (component.MagNeedsOpenBolt)
|
||||
{
|
||||
data.Visibility = component.HasMagazine && component.BoltOpen
|
||||
? VerbVisibility.Visible
|
||||
: VerbVisibility.Disabled;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Visibility = component.HasMagazine ? VerbVisibility.Visible : VerbVisibility.Disabled;
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, ServerMagazineBarrelComponent component)
|
||||
{
|
||||
component.RemoveMagazine(user);
|
||||
}
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class OpenBoltVerb : Verb<ServerMagazineBarrelComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, ServerMagazineBarrelComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString("Open bolt");
|
||||
data.Visibility = component.BoltOpen ? VerbVisibility.Invisible : VerbVisibility.Visible;
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, ServerMagazineBarrelComponent component)
|
||||
{
|
||||
component.BoltOpen = true;
|
||||
}
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class CloseBoltVerb : Verb<ServerMagazineBarrelComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, ServerMagazineBarrelComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString("Close bolt");
|
||||
data.Visibility = component.BoltOpen ? VerbVisibility.Visible : VerbVisibility.Invisible;
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, ServerMagazineBarrelComponent component)
|
||||
{
|
||||
component.BoltOpen = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum MagazineType
|
||||
{
|
||||
Unspecified = 0,
|
||||
LPistol = 1 << 0, // Placeholder?
|
||||
Pistol = 1 << 1,
|
||||
HCPistol = 1 << 2,
|
||||
Smg = 1 << 3,
|
||||
SmgTopMounted = 1 << 4,
|
||||
Rifle = 1 << 5,
|
||||
IH = 1 << 6, // Placeholder?
|
||||
Box = 1 << 7,
|
||||
Pan = 1 << 8,
|
||||
Dart = 1 << 9, // Placeholder
|
||||
CalicoTopMounted = 1 << 10,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Camera;
|
||||
using Content.Server.Projectiles.Components;
|
||||
using Content.Server.Weapon.Ranged.Ammunition.Components;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Damage.Components;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Weapons.Ranged.Components;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Physics.Broadphase;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Weapon.Ranged.Barrels.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// All of the ranged weapon components inherit from this to share mechanics like shooting etc.
|
||||
/// Only difference between them is how they retrieve a projectile to shoot (battery, magazine, etc.)
|
||||
/// </summary>
|
||||
public abstract class ServerRangedBarrelComponent : SharedRangedBarrelComponent, IUse, IInteractUsing, IExamine, ISerializationHooks
|
||||
{
|
||||
// There's still some of py01 and PJB's work left over, especially in underlying shooting logic,
|
||||
// it's just when I re-organised it changed me as the contributor
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
public override FireRateSelector FireRateSelector => _fireRateSelector;
|
||||
|
||||
[DataField("currentSelector")]
|
||||
private FireRateSelector _fireRateSelector = FireRateSelector.Safety;
|
||||
|
||||
public override FireRateSelector AllRateSelectors => _fireRateSelector;
|
||||
|
||||
[DataField("allSelectors")]
|
||||
private FireRateSelector _allRateSelectors;
|
||||
|
||||
[DataField("fireRate")]
|
||||
public override float FireRate { get; } = 2f;
|
||||
|
||||
// _lastFire is when we actually fired (so if we hold the button then recoil doesn't build up if we're not firing)
|
||||
private TimeSpan _lastFire;
|
||||
|
||||
public abstract IEntity? PeekAmmo();
|
||||
public abstract IEntity? TakeProjectile(EntityCoordinates spawnAt);
|
||||
|
||||
// Recoil / spray control
|
||||
[DataField("minAngle")]
|
||||
private float _minAngleDegrees;
|
||||
|
||||
public Angle MinAngle { get; private set; }
|
||||
|
||||
[DataField("maxAngle")]
|
||||
private float _maxAngleDegrees = 45;
|
||||
|
||||
public Angle MaxAngle { get; private set; }
|
||||
|
||||
private Angle _currentAngle = Angle.Zero;
|
||||
|
||||
[DataField("angleDecay")]
|
||||
private float _angleDecayDegrees = 20;
|
||||
|
||||
/// <summary>
|
||||
/// How slowly the angle's theta decays per second in radians
|
||||
/// </summary>
|
||||
public float AngleDecay { get; private set; }
|
||||
|
||||
[DataField("angleIncrease")]
|
||||
private float? _angleIncreaseDegrees;
|
||||
|
||||
/// <summary>
|
||||
/// How quickly the angle's theta builds for every shot fired in radians
|
||||
/// </summary>
|
||||
public float AngleIncrease { get; private set; }
|
||||
|
||||
// Multiplies the ammo spread to get the final spread of each pellet
|
||||
[DataField("ammoSpreadRatio")]
|
||||
public float SpreadRatio { get; private set; }
|
||||
|
||||
[DataField("canMuzzleFlash")]
|
||||
public bool CanMuzzleFlash { get; } = true;
|
||||
|
||||
// Sounds
|
||||
[DataField("soundGunshot")]
|
||||
public string? SoundGunshot { get; set; }
|
||||
|
||||
[DataField("soundEmpty")]
|
||||
public string SoundEmpty { get; } = "/Audio/Weapons/Guns/Empty/empty.ogg";
|
||||
|
||||
void ISerializationHooks.BeforeSerialization()
|
||||
{
|
||||
_minAngleDegrees = (float) (MinAngle.Degrees * 2);
|
||||
_maxAngleDegrees = (float) (MaxAngle.Degrees * 2);
|
||||
_angleIncreaseDegrees = MathF.Round(AngleIncrease / ((float) Math.PI / 180f), 2);
|
||||
AngleDecay = MathF.Round(AngleDecay / ((float) Math.PI / 180f), 2);
|
||||
}
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
// This hard-to-read area's dealing with recoil
|
||||
// Use degrees in yaml as it's easier to read compared to "0.0125f"
|
||||
MinAngle = Angle.FromDegrees(_minAngleDegrees / 2f);
|
||||
|
||||
// Random doubles it as it's +/- so uhh we'll just half it here for readability
|
||||
MaxAngle = Angle.FromDegrees(_maxAngleDegrees / 2f);
|
||||
|
||||
_angleIncreaseDegrees ??= 40 / FireRate;
|
||||
AngleIncrease = _angleIncreaseDegrees.Value * (float) Math.PI / 180f;
|
||||
|
||||
AngleDecay = _angleDecayDegrees * (float) Math.PI / 180f;
|
||||
|
||||
// For simplicity we'll enforce it this way; ammo determines max spread
|
||||
if (SpreadRatio > 1.0f)
|
||||
{
|
||||
Logger.Error("SpreadRatio must be <= 1.0f for guns");
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnAdd()
|
||||
{
|
||||
base.OnAdd();
|
||||
|
||||
Owner.EnsureComponentWarn(out ServerRangedWeaponComponent rangedWeaponComponent);
|
||||
|
||||
rangedWeaponComponent.Barrel ??= this;
|
||||
rangedWeaponComponent.FireHandler += Fire;
|
||||
rangedWeaponComponent.WeaponCanFireHandler += WeaponCanFire;
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
base.OnRemove();
|
||||
if (Owner.TryGetComponent(out ServerRangedWeaponComponent? rangedWeaponComponent))
|
||||
{
|
||||
rangedWeaponComponent.Barrel = null;
|
||||
rangedWeaponComponent.FireHandler -= Fire;
|
||||
rangedWeaponComponent.WeaponCanFireHandler -= WeaponCanFire;
|
||||
}
|
||||
}
|
||||
|
||||
private Angle GetRecoilAngle(Angle direction)
|
||||
{
|
||||
var currentTime = _gameTiming.CurTime;
|
||||
var timeSinceLastFire = (currentTime - _lastFire).TotalSeconds;
|
||||
var newTheta = MathHelper.Clamp(_currentAngle.Theta + AngleIncrease - AngleDecay * timeSinceLastFire, MinAngle.Theta, MaxAngle.Theta);
|
||||
_currentAngle = new Angle(newTheta);
|
||||
|
||||
var random = (_robustRandom.NextDouble() - 0.5) * 2;
|
||||
var angle = Angle.FromDegrees(direction.Degrees + _currentAngle.Degrees * random);
|
||||
return angle;
|
||||
}
|
||||
|
||||
public abstract bool UseEntity(UseEntityEventArgs eventArgs);
|
||||
|
||||
public abstract Task<bool> InteractUsing(InteractUsingEventArgs eventArgs);
|
||||
|
||||
public void ChangeFireSelector(FireRateSelector rateSelector)
|
||||
{
|
||||
if ((rateSelector & AllRateSelectors) != 0)
|
||||
{
|
||||
_fireRateSelector = rateSelector;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
protected virtual bool WeaponCanFire()
|
||||
{
|
||||
// If the ServerRangedWeaponComponent gets re-done probably need to add the checks here
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires a round of ammo out of the weapon.
|
||||
/// </summary>
|
||||
/// <param name="shooter">Entity that is operating the weapon, usually the player.</param>
|
||||
/// <param name="targetPos">Target position on the map to shoot at.</param>
|
||||
private void Fire(IEntity shooter, Vector2 targetPos)
|
||||
{
|
||||
if (ShotsLeft == 0)
|
||||
{
|
||||
if (SoundEmpty != null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Broadcast(), SoundEmpty, Owner.Transform.Coordinates);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var ammo = PeekAmmo();
|
||||
var projectile = TakeProjectile(shooter.Transform.Coordinates);
|
||||
if (projectile == null)
|
||||
{
|
||||
SoundSystem.Play(Filter.Broadcast(), SoundEmpty, Owner.Transform.Coordinates);
|
||||
return;
|
||||
}
|
||||
|
||||
// At this point firing is confirmed
|
||||
var direction = (targetPos - shooter.Transform.WorldPosition).ToAngle();
|
||||
var angle = GetRecoilAngle(direction);
|
||||
// This should really be client-side but for now we'll just leave it here
|
||||
if (shooter.TryGetComponent(out CameraRecoilComponent? recoilComponent))
|
||||
{
|
||||
recoilComponent.Kick(-angle.ToVec() * 0.15f);
|
||||
}
|
||||
|
||||
|
||||
// This section probably needs tweaking so there can be caseless hitscan etc.
|
||||
if (projectile.TryGetComponent(out HitscanComponent? hitscan))
|
||||
{
|
||||
FireHitscan(shooter, hitscan, angle);
|
||||
}
|
||||
else if (projectile.HasComponent<ProjectileComponent>() &&
|
||||
ammo != null &&
|
||||
ammo.TryGetComponent(out AmmoComponent? ammoComponent))
|
||||
{
|
||||
FireProjectiles(shooter, projectile, ammoComponent.ProjectilesFired, ammoComponent.EvenSpreadAngle, angle, ammoComponent.Velocity, ammo);
|
||||
|
||||
if (CanMuzzleFlash)
|
||||
{
|
||||
ammoComponent.MuzzleFlash(Owner, angle);
|
||||
}
|
||||
|
||||
if (ammoComponent.Caseless)
|
||||
{
|
||||
ammo.Delete();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Invalid types
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(SoundGunshot))
|
||||
{
|
||||
SoundSystem.Play(Filter.Broadcast(), SoundGunshot, Owner.Transform.Coordinates);
|
||||
}
|
||||
|
||||
_lastFire = _gameTiming.CurTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops a single cartridge / shell
|
||||
/// Made as a static function just because multiple places need it
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="playSound"></param>
|
||||
/// <param name="robustRandom"></param>
|
||||
/// <param name="prototypeManager"></param>
|
||||
/// <param name="ejectDirections"></param>
|
||||
public static void EjectCasing(
|
||||
IEntity entity,
|
||||
bool playSound = true,
|
||||
IRobustRandom? robustRandom = null,
|
||||
IPrototypeManager? prototypeManager = null,
|
||||
Direction[]? ejectDirections = null)
|
||||
{
|
||||
robustRandom ??= IoCManager.Resolve<IRobustRandom>();
|
||||
ejectDirections ??= new[]
|
||||
{Direction.East, Direction.North, Direction.NorthWest, Direction.South, Direction.SouthEast, Direction.West};
|
||||
|
||||
const float ejectOffset = 1.8f;
|
||||
var ammo = entity.GetComponent<AmmoComponent>();
|
||||
var offsetPos = ((robustRandom.NextFloat() - 0.5f) * ejectOffset, (robustRandom.NextFloat() - 0.5f) * ejectOffset);
|
||||
entity.Transform.Coordinates = entity.Transform.Coordinates.Offset(offsetPos);
|
||||
entity.Transform.LocalRotation = robustRandom.Pick(ejectDirections).ToAngle();
|
||||
|
||||
if (ammo.SoundCollectionEject == null || !playSound)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
prototypeManager ??= IoCManager.Resolve<IPrototypeManager>();
|
||||
|
||||
var soundCollection = prototypeManager.Index<SoundCollectionPrototype>(ammo.SoundCollectionEject);
|
||||
var randomFile = robustRandom.Pick(soundCollection.PickFiles);
|
||||
SoundSystem.Play(Filter.Broadcast(), randomFile, entity.Transform.Coordinates, AudioParams.Default.WithVolume(-1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops multiple cartridges / shells on the floor
|
||||
/// Wraps EjectCasing to make it less toxic for bulk ejections
|
||||
/// </summary>
|
||||
/// <param name="entities"></param>
|
||||
public static void EjectCasings(IEnumerable<IEntity> entities)
|
||||
{
|
||||
var robustRandom = IoCManager.Resolve<IRobustRandom>();
|
||||
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
|
||||
var ejectDirections = new[] {Direction.East, Direction.North, Direction.NorthWest, Direction.South, Direction.SouthEast, Direction.West};
|
||||
var soundPlayCount = 0;
|
||||
var playSound = true;
|
||||
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
EjectCasing(entity, playSound, robustRandom, prototypeManager, ejectDirections);
|
||||
soundPlayCount++;
|
||||
if (soundPlayCount > 3)
|
||||
{
|
||||
playSound = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region Firing
|
||||
/// <summary>
|
||||
/// Handles firing one or many projectiles
|
||||
/// </summary>
|
||||
private void FireProjectiles(IEntity shooter, IEntity baseProjectile, int count, float evenSpreadAngle, Angle angle, float velocity, IEntity ammo)
|
||||
{
|
||||
List<Angle>? sprayAngleChange = null;
|
||||
if (count > 1)
|
||||
{
|
||||
evenSpreadAngle *= SpreadRatio;
|
||||
sprayAngleChange = Linspace(-evenSpreadAngle / 2, evenSpreadAngle / 2, count);
|
||||
}
|
||||
|
||||
var firedProjectiles = new List<IEntity>();
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
IEntity projectile;
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
projectile = baseProjectile;
|
||||
}
|
||||
else
|
||||
{
|
||||
projectile =
|
||||
Owner.EntityManager.SpawnEntity(baseProjectile.Prototype?.ID, baseProjectile.Transform.Coordinates);
|
||||
}
|
||||
firedProjectiles.Add(projectile);
|
||||
|
||||
Angle projectileAngle;
|
||||
|
||||
if (sprayAngleChange != null)
|
||||
{
|
||||
projectileAngle = angle + sprayAngleChange[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
projectileAngle = angle;
|
||||
}
|
||||
|
||||
var physics = projectile.GetComponent<IPhysBody>();
|
||||
physics.BodyStatus = BodyStatus.InAir;
|
||||
|
||||
var projectileComponent = projectile.GetComponent<ProjectileComponent>();
|
||||
projectileComponent.IgnoreEntity(shooter);
|
||||
|
||||
// FIXME: Work around issue where inserting and removing an entity from a container,
|
||||
// then setting its linear velocity in the same tick resets velocity back to zero.
|
||||
// See SharedBroadPhaseSystem.HandleContainerInsert()... It sets Awake to false, which causes this.
|
||||
projectile.SpawnTimer(TimeSpan.FromMilliseconds(25), () =>
|
||||
{
|
||||
projectile
|
||||
.GetComponent<IPhysBody>()
|
||||
.LinearVelocity = projectileAngle.ToVec() * velocity;
|
||||
});
|
||||
|
||||
|
||||
projectile.Transform.LocalRotation = projectileAngle + MathHelper.PiOver2;
|
||||
}
|
||||
ammo.SendMessage(this, new BarrelFiredMessage(firedProjectiles));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of numbers that form a set of equal intervals between the start and end value. Used to calculate shotgun spread angles.
|
||||
/// </summary>
|
||||
private List<Angle> Linspace(double start, double end, int intervals)
|
||||
{
|
||||
DebugTools.Assert(intervals > 1);
|
||||
|
||||
var linspace = new List<Angle>(intervals);
|
||||
|
||||
for (var i = 0; i <= intervals - 1; i++)
|
||||
{
|
||||
linspace.Add(Angle.FromDegrees(start + (end - start) * i / (intervals - 1)));
|
||||
}
|
||||
return linspace;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires hitscan entities and then displays their effects
|
||||
/// </summary>
|
||||
private void FireHitscan(IEntity shooter, HitscanComponent hitscan, Angle angle)
|
||||
{
|
||||
var ray = new CollisionRay(Owner.Transform.Coordinates.ToMapPos(Owner.EntityManager), angle.ToVec(), (int) hitscan.CollisionMask);
|
||||
var physicsManager = EntitySystem.Get<SharedBroadPhaseSystem>();
|
||||
var rayCastResults = physicsManager.IntersectRay(Owner.Transform.MapID, ray, hitscan.MaxLength, shooter, false).ToList();
|
||||
|
||||
if (rayCastResults.Count >= 1)
|
||||
{
|
||||
var result = rayCastResults[0];
|
||||
var distance = result.Distance;
|
||||
hitscan.FireEffects(shooter, distance, angle, result.HitEntity);
|
||||
|
||||
if (!result.HitEntity.TryGetComponent(out IDamageableComponent? damageable))
|
||||
return;
|
||||
|
||||
damageable.ChangeDamage(hitscan.DamageType, (int)Math.Round(hitscan.Damage, MidpointRounding.AwayFromZero), false, Owner);
|
||||
//I used Math.Round over Convert.toInt32, as toInt32 always rounds to
|
||||
//even numbers if halfway between two numbers, rather than rounding to nearest
|
||||
}
|
||||
else
|
||||
{
|
||||
hitscan.FireEffects(shooter, hitscan.MaxLength, angle);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public virtual void Examine(FormattedMessage message, bool inDetailsRange)
|
||||
{
|
||||
var fireRateMessage = Loc.GetString(FireRateSelector switch
|
||||
{
|
||||
FireRateSelector.Safety => "Its safety is enabled.",
|
||||
FireRateSelector.Single => "It's in single fire mode.",
|
||||
FireRateSelector.Automatic => "It's in automatic fire mode.",
|
||||
_ => throw new IndexOutOfRangeException()
|
||||
});
|
||||
|
||||
message.AddText(fireRateMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public class BarrelFiredMessage : ComponentMessage
|
||||
{
|
||||
public readonly List<IEntity> FiredProjectiles;
|
||||
|
||||
public BarrelFiredMessage(List<IEntity> firedProjectiles)
|
||||
{
|
||||
FiredProjectiles = firedProjectiles;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user