Files
OldThink/Content.Server/AME/Components/AMEControllerComponent.cs

339 lines
11 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
2021-06-09 22:19:39 +02:00
using Content.Server.Hands.Components;
using Content.Server.NodeContainer;
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.AME;
2022-03-17 20:13:31 +13:00
using Content.Shared.Hands.EntitySystems;
2021-06-09 22:19:39 +02:00
using Content.Shared.Interaction;
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
using Content.Shared.Item;
2021-09-26 15:18:45 +02:00
using Content.Shared.Popups;
using Content.Shared.Sound;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.Player;
2021-06-09 22:19:39 +02:00
namespace Content.Server.AME.Components
{
[RegisterComponent]
[ComponentReference(typeof(IInteractUsing))]
public sealed class AMEControllerComponent : SharedAMEControllerComponent, IInteractUsing
{
2021-12-05 18:09:01 +01:00
[Dependency] private readonly IEntityManager _entities = default!;
2022-03-17 20:13:31 +13:00
[Dependency] private readonly IEntitySystemManager _sysMan = default!;
2021-12-05 18:09:01 +01:00
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(AMEControllerUiKey.Key);
private bool _injecting;
[ViewVariables] public bool Injecting => _injecting;
[ViewVariables] public int InjectionAmount;
private AppearanceComponent? _appearance;
private PowerSupplierComponent? _powerSupplier;
[DataField("clickSound")] private SoundSpecifier _clickSound = new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg");
[DataField("injectSound")] private SoundSpecifier _injectSound = new SoundPathSpecifier("/Audio/Effects/bang.ogg");
2021-12-05 18:09:01 +01:00
private bool Powered => !_entities.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
[ViewVariables]
private int _stability = 100;
private ContainerSlot _jarSlot = default!;
[ViewVariables] private bool HasJar => _jarSlot.ContainedEntity != null;
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
}
2021-12-05 18:09:01 +01:00
_entities.TryGetComponent(Owner, out _appearance);
2021-12-05 18:09:01 +01:00
_entities.TryGetComponent(Owner, out _powerSupplier);
_injecting = false;
InjectionAmount = 2;
_jarSlot = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-fuelJarContainer");
}
[Obsolete("Component Messages are deprecated, use Entity Events instead.")]
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
#pragma warning disable 618
base.HandleMessage(message, component);
#pragma warning restore 618
switch (message)
{
case PowerChangedMessage powerChanged:
OnPowerChanged(powerChanged);
break;
}
}
internal void OnUpdate(float frameTime)
{
if (!_injecting)
{
return;
}
var group = GetAMENodeGroup();
if (group == null)
{
return;
}
2021-12-05 18:09:01 +01:00
if (_jarSlot.ContainedEntity is not {Valid: true} jar)
return;
2021-12-05 18:09:01 +01:00
_entities.TryGetComponent<AMEFuelContainerComponent?>(jar, out var fuelJar);
if (fuelJar != null && _powerSupplier != null)
{
var availableInject = fuelJar.FuelAmount >= InjectionAmount ? InjectionAmount : fuelJar.FuelAmount;
_powerSupplier.MaxSupply = group.InjectFuel(availableInject, out var overloading);
fuelJar.FuelAmount -= availableInject;
InjectSound(overloading);
UpdateUserInterface();
}
_stability = group.GetTotalStability();
UpdateDisplay(_stability);
if (_stability <= 0) { group.ExplodeCores(); }
}
private void OnPowerChanged(PowerChangedMessage e)
{
UpdateUserInterface();
}
// Used to update core count
public void OnAMENodeGroupUpdate()
{
UpdateUserInterface();
}
private AMEControllerBoundUserInterfaceState GetUserInterfaceState()
{
2021-12-05 18:09:01 +01:00
if (_jarSlot.ContainedEntity is not {Valid: true} jar)
{
return new AMEControllerBoundUserInterfaceState(Powered, IsMasterController(), false, HasJar, 0, InjectionAmount, GetCoreCount());
}
2021-12-05 18:09:01 +01:00
var jarComponent = _entities.GetComponent<AMEFuelContainerComponent>(jar);
return new AMEControllerBoundUserInterfaceState(Powered, IsMasterController(), _injecting, HasJar, jarComponent.FuelAmount, InjectionAmount, GetCoreCount());
}
/// <summary>
/// Checks whether the player entity is able to use the controller.
/// </summary>
/// <param name="playerEntity">The player entity.</param>
/// <returns>Returns true if the entity can use the controller, and false if it cannot.</returns>
2021-12-05 18:09:01 +01:00
private bool PlayerCanUseController(EntityUid playerEntity, bool needsPower = true)
{
//Need player entity to check if they are still able to use the dispenser
2021-12-05 18:09:01 +01:00
if (playerEntity == default)
return false;
//Check if device is powered
if (needsPower && !Powered)
return false;
return true;
}
private void UpdateUserInterface()
{
var state = GetUserInterfaceState();
UserInterface?.SetState(state);
}
/// <summary>
/// Handles ui messages from the client. For things such as button presses
/// which interact with the world and require server action.
/// </summary>
/// <param name="obj">A user interface message from the client.</param>
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
2021-12-05 18:09:01 +01:00
if (obj.Session.AttachedEntity is not {Valid: true} player)
{
return;
}
var msg = (UiButtonPressedMessage) obj.Message;
var needsPower = msg.Button switch
{
UiButton.Eject => false,
_ => true,
};
2021-12-05 18:09:01 +01:00
if (!PlayerCanUseController(player, needsPower))
return;
switch (msg.Button)
{
case UiButton.Eject:
2021-12-05 18:09:01 +01:00
TryEject(player);
break;
case UiButton.ToggleInjection:
ToggleInjection();
break;
case UiButton.IncreaseFuel:
InjectionAmount += 2;
break;
case UiButton.DecreaseFuel:
InjectionAmount = InjectionAmount > 0 ? InjectionAmount -= 2 : 0;
break;
}
GetAMENodeGroup()?.UpdateCoreVisuals();
UpdateUserInterface();
ClickSound();
}
2021-12-05 18:09:01 +01:00
private void TryEject(EntityUid user)
{
if (!HasJar || _injecting)
return;
2021-12-05 18:09:01 +01:00
if (_jarSlot.ContainedEntity is not {Valid: true} jar)
return;
_jarSlot.Remove(jar);
UpdateUserInterface();
2022-03-17 20:13:31 +13:00
_sysMan.GetEntitySystem<SharedHandsSystem>().PickupOrDrop(user, jar);
}
private void ToggleInjection()
{
if (!_injecting)
{
_appearance?.SetData(AMEControllerVisuals.DisplayState, "on");
}
else
{
_appearance?.SetData(AMEControllerVisuals.DisplayState, "off");
if (_powerSupplier != null)
{
_powerSupplier.MaxSupply = 0;
}
}
_injecting = !_injecting;
UpdateUserInterface();
}
private void UpdateDisplay(int stability)
{
if (_appearance == null) { return; }
_appearance.TryGetData<string>(AMEControllerVisuals.DisplayState, out var state);
var newState = "on";
if (stability < 50) { newState = "critical"; }
if (stability < 10) { newState = "fuck"; }
if (state != newState)
{
_appearance?.SetData(AMEControllerVisuals.DisplayState, newState);
}
}
private AMENodeGroup? GetAMENodeGroup()
{
2021-12-05 18:09:01 +01:00
_entities.TryGetComponent(Owner, out NodeContainerComponent? nodeContainer);
var engineNodeGroup = nodeContainer?.Nodes.Values
.Select(node => node.NodeGroup)
.OfType<AMENodeGroup>()
.FirstOrDefault();
return engineNodeGroup;
}
private bool IsMasterController()
{
if (GetAMENodeGroup()?.MasterController == this)
{
return true;
}
return false;
}
private int GetCoreCount()
{
var coreCount = 0;
var group = GetAMENodeGroup();
if (group != null)
{
coreCount = group.CoreCount;
}
return coreCount;
}
private void ClickSound()
{
SoundSystem.Play(Filter.Pvs(Owner), _clickSound.GetSound(), Owner, AudioParams.Default.WithVolume(-2f));
}
private void InjectSound(bool overloading)
{
SoundSystem.Play(Filter.Pvs(Owner), _injectSound.GetSound(), Owner, AudioParams.Default.WithVolume(overloading ? 10f : 0f));
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs args)
{
2021-12-05 18:09:01 +01:00
if (!_entities.TryGetComponent(args.User, out HandsComponent? hands))
{
Owner.PopupMessage(args.User, Loc.GetString("ame-controller-component-interact-using-no-hands-text"));
return true;
}
2022-03-17 20:13:31 +13:00
if (hands.ActiveHandEntity == null)
{
Owner.PopupMessage(args.User, Loc.GetString("ame-controller-component-interact-using-nothing-in-hands-text"));
return false;
}
2022-03-17 20:13:31 +13:00
var activeHandEntity = hands.ActiveHandEntity;
2021-12-05 18:09:01 +01:00
if (_entities.HasComponent<AMEFuelContainerComponent?>(activeHandEntity))
{
if (HasJar)
{
Owner.PopupMessage(args.User, Loc.GetString("ame-controller-component-interact-using-already-has-jar"));
}
else
{
2022-03-17 20:13:31 +13:00
_jarSlot.Insert(activeHandEntity.Value);
Owner.PopupMessage(args.User, Loc.GetString("ame-controller-component-interact-using-success"));
UpdateUserInterface();
}
}
else
{
Owner.PopupMessage(args.User, Loc.GetString("ame-controller-component-interact-using-fail"));
}
return true;
}
}
}