2020-08-22 22:29:20 +02:00
|
|
|
using System;
|
2019-11-13 17:37:46 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Access.Components;
|
2021-10-22 05:31:07 +03:00
|
|
|
using Content.Server.Access.Systems;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Advertise;
|
2021-09-26 15:18:45 +02:00
|
|
|
using Content.Server.Popups;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Power.Components;
|
|
|
|
|
using Content.Server.UserInterface;
|
2021-07-04 18:11:52 +02:00
|
|
|
using Content.Server.WireHacking;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Acts;
|
|
|
|
|
using Content.Shared.Examine;
|
|
|
|
|
using Content.Shared.Interaction;
|
2021-07-10 17:35:33 +02:00
|
|
|
using Content.Shared.Sound;
|
2019-08-14 10:49:28 +02:00
|
|
|
using Content.Shared.VendingMachines;
|
2019-11-13 17:37:46 -05:00
|
|
|
using Robust.Server.GameObjects;
|
2020-08-13 22:39:23 +10:00
|
|
|
using Robust.Shared.Audio;
|
2019-08-14 10:49:28 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2020-12-08 07:42:43 +01:00
|
|
|
using Robust.Shared.Localization;
|
2021-03-21 09:12:03 -07:00
|
|
|
using Robust.Shared.Player;
|
2019-08-14 10:49:28 +02:00
|
|
|
using Robust.Shared.Prototypes;
|
2019-11-13 17:37:46 -05:00
|
|
|
using Robust.Shared.Random;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2019-08-14 10:49:28 +02:00
|
|
|
using Robust.Shared.Utility;
|
2020-08-24 20:47:17 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2021-06-09 22:19:39 +02:00
|
|
|
using static Content.Shared.Wires.SharedWiresComponent;
|
2019-08-14 10:49:28 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.VendingMachines
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
[ComponentReference(typeof(IActivate))]
|
2021-08-19 22:23:02 -07:00
|
|
|
public class VendingMachineComponent : SharedVendingMachineComponent, IActivate, IBreakAct, IWires
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2020-09-02 01:30:03 +02:00
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
2020-08-22 22:29:20 +02:00
|
|
|
|
|
|
|
|
private bool _ejecting;
|
2019-08-14 10:49:28 +02:00
|
|
|
private TimeSpan _animationDuration = TimeSpan.Zero;
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("pack")]
|
|
|
|
|
private string _packPrototypeId = string.Empty;
|
2020-08-22 22:29:20 +02:00
|
|
|
private string _spriteName = "";
|
|
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
private bool Powered => !Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) || receiver.Powered;
|
2020-08-22 22:29:20 +02:00
|
|
|
private bool _broken;
|
2019-08-14 10:49:28 +02:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("soundVend")]
|
|
|
|
|
// Grabbed from: https://github.com/discordia-space/CEV-Eris/blob/f702afa271136d093ddeb415423240a2ceb212f0/sound/machines/vending_drop.ogg
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _soundVend = new SoundPathSpecifier("/Audio/Machines/machine_vend.ogg");
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("soundDeny")]
|
|
|
|
|
// Yoinked from: https://github.com/discordia-space/CEV-Eris/blob/35bbad6764b14e15c03a816e3e89aa1751660ba9/sound/machines/Custom_deny.ogg
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _soundDeny = new SoundPathSpecifier("/Audio/Machines/custom_deny.ogg");
|
2019-08-14 10:49:28 +02:00
|
|
|
|
2020-08-24 20:47:17 +02:00
|
|
|
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(VendingMachineUiKey.Key);
|
2020-08-13 22:39:23 +10:00
|
|
|
|
2021-09-23 18:02:22 +02:00
|
|
|
public bool Broken => _broken;
|
|
|
|
|
|
2021-02-04 17:44:49 +01:00
|
|
|
void IActivate.Activate(ActivateEventArgs eventArgs)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2021-05-12 13:42:18 +02:00
|
|
|
if(!eventArgs.User.TryGetComponent(out ActorComponent? actor))
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-21 15:37:22 -04:00
|
|
|
if (!Powered)
|
|
|
|
|
return;
|
2019-08-14 10:49:28 +02:00
|
|
|
|
2019-09-01 22:15:34 +02:00
|
|
|
var wires = Owner.GetComponent<WiresComponent>();
|
2019-09-18 22:12:36 +02:00
|
|
|
if (wires.IsPanelOpen)
|
2019-09-01 22:15:34 +02:00
|
|
|
{
|
2021-05-12 13:42:18 +02:00
|
|
|
wires.OpenInterface(actor.PlayerSession);
|
2019-09-01 22:15:34 +02:00
|
|
|
} else
|
|
|
|
|
{
|
2021-05-12 13:42:18 +02:00
|
|
|
UserInterface?.Toggle(actor.PlayerSession);
|
2019-09-01 22:15:34 +02:00
|
|
|
}
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeFromPrototype()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_packPrototypeId)) { return; }
|
2021-02-23 22:26:59 +01:00
|
|
|
if (!_prototypeManager.TryIndex(_packPrototypeId, out VendingMachineInventoryPrototype? packPrototype))
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Owner.Name = packPrototype.Name;
|
|
|
|
|
_animationDuration = TimeSpan.FromSeconds(packPrototype.AnimationDuration);
|
|
|
|
|
_spriteName = packPrototype.SpriteName;
|
|
|
|
|
if (!string.IsNullOrEmpty(_spriteName))
|
|
|
|
|
{
|
|
|
|
|
var spriteComponent = Owner.GetComponent<SpriteComponent>();
|
2021-07-15 18:30:50 +00:00
|
|
|
const string vendingMachineRSIPath = "Structures/Machines/VendingMachines/{0}.rsi";
|
2019-08-14 10:49:28 +02:00
|
|
|
spriteComponent.BaseRSIPath = string.Format(vendingMachineRSIPath, _spriteName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var inventory = new List<VendingMachineInventoryEntry>();
|
|
|
|
|
foreach(var (id, amount) in packPrototype.StartingInventory)
|
|
|
|
|
{
|
|
|
|
|
inventory.Add(new VendingMachineInventoryEntry(id, amount));
|
|
|
|
|
}
|
|
|
|
|
Inventory = inventory;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-08-22 22:29:20 +02:00
|
|
|
|
|
|
|
|
if (UserInterface != null)
|
|
|
|
|
{
|
|
|
|
|
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
if (Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver))
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
|
|
|
|
TrySetVisualState(receiver.Powered ? VendingMachineVisualState.Normal : VendingMachineVisualState.Off);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:49:28 +02:00
|
|
|
InitializeFromPrototype();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 18:10:40 +02:00
|
|
|
[Obsolete("Component Messages are deprecated, use Entity Events instead.")]
|
2021-01-03 09:13:01 -06:00
|
|
|
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning disable 618
|
2021-01-03 09:13:01 -06:00
|
|
|
base.HandleMessage(message, component);
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning restore 618
|
2021-01-03 09:13:01 -06:00
|
|
|
switch (message)
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
2021-01-03 09:13:01 -06:00
|
|
|
case PowerChangedMessage powerChanged:
|
|
|
|
|
UpdatePower(powerChanged);
|
|
|
|
|
break;
|
2020-08-22 22:29:20 +02:00
|
|
|
}
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-03 09:13:01 -06:00
|
|
|
private void UpdatePower(PowerChangedMessage args)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
var state = args.Powered ? VendingMachineVisualState.Normal : VendingMachineVisualState.Off;
|
|
|
|
|
TrySetVisualState(state);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 22:00:38 +02:00
|
|
|
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2020-03-21 15:37:22 -04:00
|
|
|
if (!Powered)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-08-27 22:00:38 +02:00
|
|
|
var message = serverMsg.Message;
|
2019-08-14 10:49:28 +02:00
|
|
|
switch (message)
|
|
|
|
|
{
|
|
|
|
|
case VendingMachineEjectMessage msg:
|
2020-11-02 12:33:12 +01:00
|
|
|
TryEject(msg.ID, serverMsg.Session.AttachedEntity);
|
2019-08-14 10:49:28 +02:00
|
|
|
break;
|
2020-08-22 22:29:20 +02:00
|
|
|
case InventorySyncRequestMessage _:
|
|
|
|
|
UserInterface?.SendMessage(new VendingMachineInventoryMessage(Inventory));
|
2019-08-14 10:49:28 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TryEject(string id)
|
|
|
|
|
{
|
|
|
|
|
if (_ejecting || _broken)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 22:29:20 +02:00
|
|
|
var entry = Inventory.Find(x => x.ID == id);
|
2019-08-14 10:49:28 +02:00
|
|
|
if (entry == null)
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessageEveryone(Loc.GetString("vending-machine-component-try-eject-invalid-item"));
|
2020-12-08 07:42:43 +01:00
|
|
|
Deny();
|
2019-08-14 10:49:28 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (entry.Amount <= 0)
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessageEveryone(Loc.GetString("vending-machine-component-try-eject-out-of-stock"));
|
2020-12-08 07:42:43 +01:00
|
|
|
Deny();
|
2019-08-14 10:49:28 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ejecting = true;
|
|
|
|
|
entry.Amount--;
|
2020-08-22 22:29:20 +02:00
|
|
|
UserInterface?.SendMessage(new VendingMachineInventoryMessage(Inventory));
|
2019-08-14 10:49:28 +02:00
|
|
|
TrySetVisualState(VendingMachineVisualState.Eject);
|
|
|
|
|
|
2020-10-30 05:02:49 +01:00
|
|
|
Owner.SpawnTimer(_animationDuration, () =>
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
_ejecting = false;
|
2020-02-27 00:30:58 -03:00
|
|
|
TrySetVisualState(VendingMachineVisualState.Normal);
|
2020-09-06 16:11:53 +02:00
|
|
|
Owner.EntityManager.SpawnEntity(id, Owner.Transform.Coordinates);
|
2019-08-14 10:49:28 +02:00
|
|
|
});
|
2020-08-13 22:39:23 +10:00
|
|
|
|
2021-07-31 19:52:33 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), _soundVend.GetSound(), Owner, AudioParams.Default.WithVolume(-2f));
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-02 12:33:12 +01:00
|
|
|
private void TryEject(string id, IEntity? sender)
|
|
|
|
|
{
|
|
|
|
|
if (Owner.TryGetComponent<AccessReader>(out var accessReader))
|
|
|
|
|
{
|
2021-10-22 05:31:07 +03:00
|
|
|
var accessSystem = EntitySystem.Get<AccessReaderSystem>();
|
|
|
|
|
if (sender == null || !accessSystem.IsAllowed(accessReader, sender.Uid))
|
2020-11-02 12:33:12 +01:00
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessageEveryone(Loc.GetString("vending-machine-component-try-eject-access-denied"));
|
2020-12-08 07:42:43 +01:00
|
|
|
Deny();
|
2020-11-02 12:33:12 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TryEject(id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 07:42:43 +01:00
|
|
|
private void Deny()
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(Owner), _soundDeny.GetSound(), Owner, AudioParams.Default.WithVolume(-2f));
|
2020-12-08 07:42:43 +01:00
|
|
|
|
|
|
|
|
// Play the Deny animation
|
2019-08-14 10:49:28 +02:00
|
|
|
TrySetVisualState(VendingMachineVisualState.Deny);
|
|
|
|
|
//TODO: This duration should be a distinct value specific to the deny animation
|
2020-10-30 05:02:49 +01:00
|
|
|
Owner.SpawnTimer(_animationDuration, () =>
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
TrySetVisualState(VendingMachineVisualState.Normal);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TrySetVisualState(VendingMachineVisualState state)
|
|
|
|
|
{
|
|
|
|
|
var finalState = state;
|
|
|
|
|
if (_broken)
|
|
|
|
|
{
|
|
|
|
|
finalState = VendingMachineVisualState.Broken;
|
2020-08-22 22:29:20 +02:00
|
|
|
}
|
|
|
|
|
else if (_ejecting)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
finalState = VendingMachineVisualState.Eject;
|
2020-08-22 22:29:20 +02:00
|
|
|
}
|
|
|
|
|
else if (!Powered)
|
2019-08-14 10:49:28 +02:00
|
|
|
{
|
|
|
|
|
finalState = VendingMachineVisualState.Off;
|
|
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
|
|
|
|
|
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
|
|
|
|
{
|
|
|
|
|
appearance.SetData(VendingMachineVisuals.VisualState, finalState);
|
|
|
|
|
}
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnBreak(BreakageEventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
_broken = true;
|
|
|
|
|
TrySetVisualState(VendingMachineVisualState.Broken);
|
|
|
|
|
}
|
2019-09-01 22:15:34 +02:00
|
|
|
|
|
|
|
|
public enum Wires
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shoots a random item when pulsed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
Shoot
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IWires.RegisterWires(WiresComponent.WiresBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
builder.CreateWire(Wires.Shoot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IWires.WiresUpdate(WiresUpdateEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
var identifier = (Wires) args.Identifier;
|
|
|
|
|
if (identifier == Wires.Shoot && args.Action == WiresAction.Pulse)
|
|
|
|
|
{
|
|
|
|
|
EjectRandom();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ejects a random item if present.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void EjectRandom()
|
|
|
|
|
{
|
|
|
|
|
var availableItems = Inventory.Where(x => x.Amount > 0).ToList();
|
|
|
|
|
if (availableItems.Count <= 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
TryEject(_random.Pick(availableItems).ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class WiresUpdateEventArgs : EventArgs
|
|
|
|
|
{
|
|
|
|
|
public readonly object Identifier;
|
|
|
|
|
public readonly WiresAction Action;
|
|
|
|
|
|
|
|
|
|
public WiresUpdateEventArgs(object identifier, WiresAction action)
|
|
|
|
|
{
|
|
|
|
|
Identifier = identifier;
|
|
|
|
|
Action = action;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IWires
|
|
|
|
|
{
|
|
|
|
|
void RegisterWires(WiresComponent.WiresBuilder builder);
|
|
|
|
|
void WiresUpdate(WiresUpdateEventArgs args);
|
|
|
|
|
|
2019-08-14 10:49:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|