2021-03-21 09:12:03 -07:00
|
|
|
using System;
|
2020-08-18 14:39:08 +02:00
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Weapon.Ranged.Ammunition.Components;
|
|
|
|
|
using Content.Shared.Interaction;
|
2021-09-26 15:18:45 +02:00
|
|
|
using Content.Shared.Popups;
|
2021-07-10 17:35:33 +02:00
|
|
|
using Content.Shared.Sound;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Weapons.Ranged.Barrels.Components;
|
2020-06-22 05:47:15 +10:00
|
|
|
using Robust.Shared.Audio;
|
2021-03-01 15:24:46 -08:00
|
|
|
using Robust.Shared.Containers;
|
2020-06-22 05:47:15 +10:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-07-12 01:32:10 -07:00
|
|
|
using Robust.Shared.GameStates;
|
2020-06-22 05:47:15 +10:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Localization;
|
2020-06-24 20:00:31 +02:00
|
|
|
using Robust.Shared.Map;
|
2021-03-21 09:12:03 -07:00
|
|
|
using Robust.Shared.Player;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Random;
|
2020-06-22 05:47:15 +10:00
|
|
|
using Robust.Shared.Serialization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-09-08 13:30:22 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2020-06-22 05:47:15 +10:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Weapon.Ranged.Barrels.Components
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2021-07-12 01:32:10 -07:00
|
|
|
[NetworkedComponent()]
|
2021-12-05 16:23:47 +13:00
|
|
|
public sealed class RevolverBarrelComponent : ServerRangedBarrelComponent, IUse, IInteractUsing, ISerializationHooks
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
2020-09-02 01:30:03 +02:00
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
|
|
|
|
2020-06-22 05:47:15 +10:00
|
|
|
public override string Name => "RevolverBarrel";
|
2020-08-24 13:13:26 +02:00
|
|
|
|
2020-09-08 13:30:22 +02:00
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("caliber")]
|
|
|
|
|
private BallisticCaliber _caliber = BallisticCaliber.Unspecified;
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
private Container _ammoContainer = default!;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
2020-09-08 13:30:22 +02:00
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
private int _currentSlot;
|
|
|
|
|
|
2020-06-22 05:47:15 +10:00
|
|
|
public override int Capacity => _ammoSlots.Length;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
[DataField("capacity")]
|
|
|
|
|
private int _serializedCapacity = 6;
|
|
|
|
|
|
|
|
|
|
[DataField("ammoSlots", readOnly: true)]
|
2021-12-05 18:09:01 +01:00
|
|
|
private EntityUid[] _ammoSlots = Array.Empty<EntityUid>();
|
2020-06-22 05:47:15 +10:00
|
|
|
|
|
|
|
|
public override int ShotsLeft => _ammoContainer.ContainedEntities.Count;
|
|
|
|
|
|
2020-09-08 13:30:22 +02:00
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("fillPrototype")]
|
2021-03-16 15:50:20 +01:00
|
|
|
private string? _fillPrototype;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
2020-09-14 00:04:00 +12:00
|
|
|
[ViewVariables]
|
2020-06-25 01:29:40 +10:00
|
|
|
private int _unspawnedCount;
|
2020-06-22 05:47:15 +10:00
|
|
|
|
|
|
|
|
// Sounds
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("soundEject")]
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _soundEject = new SoundPathSpecifier("/Audio/Weapons/Guns/MagOut/revolver_magout.ogg");
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
[DataField("soundInsert")]
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _soundInsert = new SoundPathSpecifier("/Audio/Weapons/Guns/MagIn/revolver_magin.ogg");
|
2020-06-22 05:47:15 +10:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("soundSpin")]
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _soundSpin = new SoundPathSpecifier("/Audio/Weapons/Guns/Misc/revolver_spin.ogg");
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
void ISerializationHooks.BeforeSerialization()
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
_serializedCapacity = _ammoSlots.Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ISerializationHooks.AfterDeserialization()
|
|
|
|
|
{
|
2021-12-06 00:52:58 +01:00
|
|
|
_ammoSlots = new EntityUid[_serializedCapacity];
|
2020-06-22 05:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
2021-11-30 15:20:38 +01:00
|
|
|
public override ComponentState GetComponentState()
|
2020-08-24 13:13:26 +02:00
|
|
|
{
|
|
|
|
|
var slotsSpent = new bool?[Capacity];
|
|
|
|
|
for (var i = 0; i < Capacity; i++)
|
|
|
|
|
{
|
|
|
|
|
slotsSpent[i] = null;
|
2021-03-16 15:50:20 +01:00
|
|
|
var ammoEntity = _ammoSlots[i];
|
2021-12-23 15:46:43 +11:00
|
|
|
if (ammoEntity != default && Entities.TryGetComponent(ammoEntity, out AmmoComponent? ammo))
|
2020-08-24 13:13:26 +02:00
|
|
|
{
|
|
|
|
|
slotsSpent[i] = ammo.Spent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: make yaml var to not sent currentSlot/UI? (for russian roulette)
|
|
|
|
|
return new RevolverBarrelComponentState(
|
|
|
|
|
_currentSlot,
|
|
|
|
|
FireRateSelector,
|
|
|
|
|
slotsSpent,
|
2021-07-10 17:35:33 +02:00
|
|
|
SoundGunshot.GetSound());
|
2020-08-24 13:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-06-25 01:29:40 +10:00
|
|
|
_unspawnedCount = Capacity;
|
|
|
|
|
int idx = 0;
|
2021-03-01 15:24:46 -08:00
|
|
|
_ammoContainer = ContainerHelpers.EnsureContainer<Container>(Owner, $"{Name}-ammoContainer", out var existing);
|
2020-06-25 01:29:40 +10:00
|
|
|
if (existing)
|
|
|
|
|
{
|
|
|
|
|
foreach (var entity in _ammoContainer.ContainedEntities)
|
|
|
|
|
{
|
|
|
|
|
_unspawnedCount--;
|
|
|
|
|
_ammoSlots[idx] = entity;
|
|
|
|
|
idx++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < _unspawnedCount; i++)
|
|
|
|
|
{
|
2021-12-23 15:46:43 +11:00
|
|
|
var entity = Entities.SpawnEntity(_fillPrototype, Entities.GetComponent<TransformComponent>(Owner).Coordinates);
|
2020-06-25 01:29:40 +10:00
|
|
|
_ammoSlots[idx] = entity;
|
|
|
|
|
_ammoContainer.Insert(entity);
|
|
|
|
|
idx++;
|
|
|
|
|
}
|
2020-06-22 05:47:15 +10:00
|
|
|
|
2020-08-27 08:28:50 -06:00
|
|
|
UpdateAppearance();
|
2020-08-24 13:13:26 +02:00
|
|
|
Dirty();
|
2020-06-22 05:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateAppearance()
|
|
|
|
|
{
|
2021-12-23 15:46:43 +11:00
|
|
|
if (!Entities.TryGetComponent(Owner, out AppearanceComponent? appearance))
|
2021-03-05 01:08:38 +01:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-22 05:47:15 +10:00
|
|
|
// Placeholder, at this stage it's just here for the RPG
|
2021-03-05 01:08:38 +01:00
|
|
|
appearance.SetData(MagazineBarrelVisuals.MagLoaded, ShotsLeft > 0);
|
|
|
|
|
appearance.SetData(AmmoVisuals.AmmoCount, ShotsLeft);
|
|
|
|
|
appearance.SetData(AmmoVisuals.AmmoMax, Capacity);
|
2020-06-22 05:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public bool TryInsertBullet(EntityUid user, EntityUid entity)
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
2021-12-23 15:46:43 +11:00
|
|
|
if (!Entities.TryGetComponent(entity, out AmmoComponent? ammoComponent))
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-22 04:10:44 +02:00
|
|
|
|
2020-06-22 05:47:15 +10:00
|
|
|
if (ammoComponent.Caliber != _caliber)
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessage(user, Loc.GetString("revolver-barrel-component-try-inser-bullet-wrong-caliber"));
|
2020-06-22 05:47:15 +10:00
|
|
|
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];
|
2021-12-06 00:52:58 +01:00
|
|
|
if (slot == default)
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
_currentSlot = i;
|
|
|
|
|
_ammoSlots[i] = entity;
|
|
|
|
|
_ammoContainer.Insert(entity);
|
2021-08-18 23:36:57 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), _soundInsert.GetSound(), Owner, AudioParams.Default.WithVolume(-2));
|
2020-06-22 05:47:15 +10:00
|
|
|
|
2020-08-24 13:13:26 +02:00
|
|
|
Dirty();
|
2020-06-22 05:47:15 +10:00
|
|
|
UpdateAppearance();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessage(user, Loc.GetString("revolver-barrel-component-try-inser-bullet-ammo-full"));
|
2020-06-22 05:47:15 +10:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Cycle()
|
|
|
|
|
{
|
|
|
|
|
// Move up a slot
|
|
|
|
|
_currentSlot = (_currentSlot + 1) % _ammoSlots.Length;
|
2020-08-24 13:13:26 +02:00
|
|
|
Dirty();
|
2020-06-22 05:47:15 +10:00
|
|
|
UpdateAppearance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Russian Roulette
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Spin()
|
|
|
|
|
{
|
2020-09-02 01:30:03 +02:00
|
|
|
var random = _random.Next(_ammoSlots.Length - 1);
|
2020-06-22 05:47:15 +10:00
|
|
|
_currentSlot = random;
|
2021-08-18 23:36:57 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), _soundSpin.GetSound(), Owner, AudioParams.Default.WithVolume(-2));
|
2020-08-24 13:13:26 +02:00
|
|
|
Dirty();
|
2020-06-22 05:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
2021-12-06 00:52:58 +01:00
|
|
|
public override EntityUid? PeekAmmo()
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
return _ammoSlots[_currentSlot];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Takes a projectile out if possible
|
|
|
|
|
/// IEnumerable just to make supporting shotguns saner
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
2021-12-06 00:52:58 +01:00
|
|
|
public override EntityUid? TakeProjectile(EntityCoordinates spawnAt)
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
var ammo = _ammoSlots[_currentSlot];
|
2021-12-06 00:52:58 +01:00
|
|
|
EntityUid? bullet = null;
|
|
|
|
|
if (ammo != default)
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
2021-12-23 15:46:43 +11:00
|
|
|
var ammoComponent = Entities.GetComponent<AmmoComponent>(ammo);
|
|
|
|
|
bullet = EntitySystem.Get<GunSystem>().TakeBullet(ammoComponent, spawnAt);
|
2020-06-22 05:47:15 +10:00
|
|
|
if (ammoComponent.Caseless)
|
|
|
|
|
{
|
2021-12-06 00:52:58 +01:00
|
|
|
_ammoSlots[_currentSlot] = default;
|
2020-06-22 05:47:15 +10:00
|
|
|
_ammoContainer.Remove(ammo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Cycle();
|
|
|
|
|
UpdateAppearance();
|
|
|
|
|
return bullet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EjectAllSlots()
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < _ammoSlots.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var entity = _ammoSlots[i];
|
2021-12-06 00:52:58 +01:00
|
|
|
if (entity == default)
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ammoContainer.Remove(entity);
|
|
|
|
|
EjectCasing(entity);
|
2021-12-06 00:52:58 +01:00
|
|
|
_ammoSlots[i] = default;
|
2020-06-22 05:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_ammoContainer.ContainedEntities.Count > 0)
|
|
|
|
|
{
|
2021-08-18 23:36:57 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), _soundEject.GetSound(), Owner, AudioParams.Default.WithVolume(-1));
|
2020-06-22 05:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// May as well point back at the end?
|
|
|
|
|
_currentSlot = _ammoSlots.Length - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Eject all casings
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="eventArgs"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
2021-12-20 15:20:27 +01:00
|
|
|
public bool UseEntity(UseEntityEventArgs eventArgs)
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
EjectAllSlots();
|
2020-08-24 13:13:26 +02:00
|
|
|
Dirty();
|
2020-06-22 05:47:15 +10:00
|
|
|
UpdateAppearance();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:20:27 +01:00
|
|
|
public async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
|
2020-06-22 05:47:15 +10:00
|
|
|
{
|
|
|
|
|
return TryInsertBullet(eventArgs.User, eventArgs.Using);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|