Files
crystall-punk-14/Content.Server/Chemistry/Components/ChemMasterComponent.cs

418 lines
17 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
2021-06-09 22:19:39 +02:00
using Content.Server.Hands.Components;
2021-12-05 18:09:01 +01:00
using Content.Server.Labels.Components;
2021-06-09 22:19:39 +02:00
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.Chemistry.Components;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.FixedPoint;
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;
2021-06-09 22:19:39 +02:00
using Content.Shared.Random.Helpers;
using Content.Shared.Sound;
using Robust.Server.GameObjects;
2020-07-17 15:41:19 -05:00
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
2021-12-05 18:09:01 +01:00
using Robust.Shared.IoC;
2020-07-17 15:41:19 -05:00
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
2020-07-17 15:41:19 -05:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.Chemistry.Components
2020-07-17 15:41:19 -05:00
{
/// <summary>
/// Contains all the server-side logic for chem masters. See also <see cref="SharedChemMasterComponent"/>.
/// This includes initializing the component based on prototype data, and sending and receiving messages from the client.
/// Messages sent to the client are used to update update the user interface for a component instance.
/// Messages sent from the client are used to handle ui button presses.
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(SharedChemMasterComponent))]
public sealed class ChemMasterComponent : SharedChemMasterComponent, IActivate
2020-07-17 15:41:19 -05:00
{
2021-12-05 18:09:01 +01:00
[Dependency] private readonly IEntityManager _entities = default!;
[ViewVariables]
private uint _pillType = 1;
[ViewVariables]
private string _label = "";
[ViewVariables]
private bool _bufferModeTransfer = true;
2020-07-17 15:41:19 -05:00
[ViewVariables]
2021-12-05 18:09:01 +01:00
private bool Powered => !_entities.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
[ViewVariables]
2021-12-03 15:53:09 +01:00
private Solution BufferSolution => _bufferSolution ??= EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner, SolutionName);
private Solution? _bufferSolution;
[ViewVariables]
private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ChemMasterUiKey.Key);
[DataField("clickSound")]
private SoundSpecifier _clickSound = new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg");
2020-07-17 15:41:19 -05:00
2020-07-17 15:41:19 -05:00
/// <summary>
/// Called once per instance of this component. Gets references to any other components needed
/// by this component and initializes it's UI and other data.
/// </summary>
protected override void Initialize()
2020-07-17 15:41:19 -05:00
{
base.Initialize();
2021-12-03 11:11:52 +01:00
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
}
2020-07-17 15:41:19 -05:00
2021-12-03 15:53:09 +01:00
_bufferSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner, SolutionName);
2020-07-17 15:41:19 -05:00
}
[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:
OnPowerChanged();
break;
}
}
private void OnPowerChanged()
{
UpdateUserInterface();
}
2020-07-17 15:41:19 -05:00
/// <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 = (UiActionMessage) obj.Message;
var needsPower = msg.Action switch
{
UiAction.Eject => false,
_ => true,
};
2021-12-05 18:09:01 +01:00
if (!PlayerCanUseChemMaster(player, needsPower))
2020-07-17 15:41:19 -05:00
return;
switch (msg.Action)
2020-07-17 15:41:19 -05:00
{
case UiAction.Eject:
2021-12-07 21:54:00 +11:00
EntitySystem.Get<ItemSlotsSystem>().TryEjectToHands(Owner, BeakerSlot, player);
2020-07-17 15:41:19 -05:00
break;
case UiAction.ChemButton:
TransferReagent(msg.Id, msg.Amount, msg.IsBuffer);
2020-07-17 15:41:19 -05:00
break;
case UiAction.Transfer:
_bufferModeTransfer = true;
2020-07-17 15:41:19 -05:00
UpdateUserInterface();
break;
case UiAction.Discard:
_bufferModeTransfer = false;
2020-07-17 15:41:19 -05:00
UpdateUserInterface();
break;
case UiAction.SetPillType:
_pillType = msg.PillType;
UpdateUserInterface();
break;
2020-07-17 15:41:19 -05:00
case UiAction.CreatePills:
case UiAction.CreateBottles:
_label = msg.Label;
2021-12-05 18:09:01 +01:00
TryCreatePackage(player, msg.Action, msg.Label, msg.PillAmount, msg.BottleAmount);
2020-07-17 15:41:19 -05:00
break;
default:
throw new ArgumentOutOfRangeException();
}
UpdateUserInterface();
2020-07-17 15:41:19 -05:00
ClickSound();
}
/// <summary>
/// Checks whether the player entity is able to use the chem master.
/// </summary>
/// <param name="playerEntity">The player entity.</param>
/// <param name="needsPower">whether the device requires power</param>
2020-07-17 15:41:19 -05:00
/// <returns>Returns true if the entity can use the chem master, and false if it cannot.</returns>
2021-12-05 18:09:01 +01:00
private bool PlayerCanUseChemMaster(EntityUid playerEntity, bool needsPower = true)
2020-07-17 15:41:19 -05:00
{
//Need player entity to check if they are still able to use the chem master
2021-12-05 18:09:01 +01:00
if (playerEntity == default)
2020-07-17 15:41:19 -05:00
return false;
2020-07-17 15:41:19 -05:00
//Check if device is powered
if (needsPower && !Powered)
2020-07-17 15:41:19 -05:00
return false;
return true;
}
/// <summary>
/// Gets component data to be used to update the user interface client-side.
/// </summary>
/// <returns>Returns a <see cref="SharedChemMasterComponent.ChemMasterBoundUserInterfaceState"/></returns>
private ChemMasterBoundUserInterfaceState GetUserInterfaceState()
{
2021-12-05 18:09:01 +01:00
if (BeakerSlot.Item is not {Valid: true} beaker ||
!_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) ||
2021-12-03 15:53:09 +01:00
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var beakerSolution))
2020-07-17 15:41:19 -05:00
{
return new ChemMasterBoundUserInterfaceState(Powered, false, FixedPoint2.New(0), FixedPoint2.New(0),
2021-12-05 18:09:01 +01:00
"", _label, _entities.GetComponent<MetaDataComponent>(Owner).EntityName, new List<Solution.ReagentQuantity>(), BufferSolution.Contents, _bufferModeTransfer,
BufferSolution.TotalVolume, _pillType);
2020-07-17 15:41:19 -05:00
}
return new ChemMasterBoundUserInterfaceState(Powered, true, beakerSolution.CurrentVolume,
beakerSolution.MaxVolume,
2021-12-05 18:09:01 +01:00
_entities.GetComponent<MetaDataComponent>(beaker).EntityName, _label, _entities.GetComponent<MetaDataComponent>(Owner).EntityName, beakerSolution.Contents, BufferSolution.Contents, _bufferModeTransfer,
BufferSolution.TotalVolume, _pillType);
2020-07-17 15:41:19 -05:00
}
public void UpdateUserInterface()
2020-07-17 15:41:19 -05:00
{
2021-12-16 23:42:02 +13:00
if (!Initialized) return;
2020-07-17 15:41:19 -05:00
var state = GetUserInterfaceState();
UserInterface?.SetState(state);
2020-07-17 15:41:19 -05:00
}
private void TransferReagent(string id, FixedPoint2 amount, bool isBuffer)
2020-07-17 15:41:19 -05:00
{
2021-12-05 18:09:01 +01:00
if (!BeakerSlot.HasItem && _bufferModeTransfer)
return;
2021-12-05 18:09:01 +01:00
if (BeakerSlot.Item is not {Valid: true} beaker ||
!_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) ||
2021-12-03 15:53:09 +01:00
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var beakerSolution))
return;
2020-07-17 15:41:19 -05:00
if (isBuffer)
{
foreach (var reagent in BufferSolution.Contents)
2020-07-17 15:41:19 -05:00
{
if (reagent.ReagentId == id)
{
FixedPoint2 actualAmount;
if (
amount == FixedPoint2
.New(-1)) //amount is FixedPoint2.New(-1) when the client sends a message requesting to remove all solution from the container
2020-07-17 15:41:19 -05:00
{
actualAmount = FixedPoint2.Min(reagent.Quantity, beakerSolution.AvailableVolume);
2020-07-17 15:41:19 -05:00
}
else
{
actualAmount = FixedPoint2.Min(reagent.Quantity, amount, beakerSolution.AvailableVolume);
2020-07-17 15:41:19 -05:00
}
BufferSolution.RemoveReagent(id, actualAmount);
if (_bufferModeTransfer)
2020-07-17 15:41:19 -05:00
{
EntitySystem.Get<SolutionContainerSystem>()
2021-12-03 15:53:09 +01:00
.TryAddReagent(beaker, beakerSolution, id, actualAmount, out var _);
// beakerSolution.Solution.AddReagent(id, actualAmount);
2020-07-17 15:41:19 -05:00
}
2020-07-17 15:41:19 -05:00
break;
}
}
}
else
{
foreach (var reagent in beakerSolution.Contents)
2020-07-17 15:41:19 -05:00
{
if (reagent.ReagentId == id)
{
FixedPoint2 actualAmount;
if (amount == FixedPoint2.New(-1))
2020-07-17 15:41:19 -05:00
{
actualAmount = reagent.Quantity;
2020-07-17 15:41:19 -05:00
}
else
{
actualAmount = FixedPoint2.Min(reagent.Quantity, amount);
2020-07-17 15:41:19 -05:00
}
2021-12-03 11:11:52 +01:00
2021-12-03 15:53:09 +01:00
EntitySystem.Get<SolutionContainerSystem>().TryRemoveReagent(beaker, beakerSolution, id, actualAmount);
BufferSolution.AddReagent(id, actualAmount);
2020-07-17 15:41:19 -05:00
break;
}
}
}
_label = GenerateLabel();
2020-07-17 15:41:19 -05:00
UpdateUserInterface();
}
/// <summary>
/// Handles label generation depending from solutions and their amount.
/// Label is generated by taking the most significant solution name.
/// </summary>
private string GenerateLabel()
{
if (_bufferSolution == null || _bufferSolution.Contents.Count == 0)
return "";
_bufferSolution.Contents.Sort();
return _bufferSolution.Contents[_bufferSolution.Contents.Count - 1].ReagentId;
}
2021-12-05 18:09:01 +01:00
private void TryCreatePackage(EntityUid user, UiAction action, string label, int pillAmount, int bottleAmount)
2020-07-17 15:41:19 -05:00
{
if (BufferSolution.TotalVolume == 0)
{
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-empty-text"));
return;
}
2020-07-17 15:41:19 -05:00
if (action == UiAction.CreateBottles)
{
var individualVolume = BufferSolution.TotalVolume / FixedPoint2.New(bottleAmount);
if (individualVolume < FixedPoint2.New(1))
{
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-low-text"));
return;
}
var actualVolume = FixedPoint2.Min(individualVolume, FixedPoint2.New(30));
2020-07-17 15:41:19 -05:00
for (int i = 0; i < bottleAmount; i++)
{
2021-12-05 18:09:01 +01:00
var bottle = _entities.SpawnEntity("ChemistryEmptyBottle01", _entities.GetComponent<TransformComponent>(Owner).Coordinates);
2020-07-17 15:41:19 -05:00
//Adding label
LabelComponent labelComponent = bottle.EnsureComponent<LabelComponent>();
2021-12-05 18:09:01 +01:00
labelComponent.OriginalName = _entities.GetComponent<MetaDataComponent>(bottle).EntityName;
string val = _entities.GetComponent<MetaDataComponent>(bottle).EntityName + $" ({label})";
_entities.GetComponent<MetaDataComponent>(bottle).EntityName = val;
labelComponent.CurrentLabel = label;
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
2021-12-03 15:53:09 +01:00
var bottleSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(bottle, "drink");
2020-07-17 15:41:19 -05:00
2021-12-03 15:53:09 +01:00
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(bottle, bottleSolution, bufferSolution);
2020-07-17 15:41:19 -05:00
//Try to give them the bottle
2021-12-05 18:09:01 +01:00
if (_entities.TryGetComponent<HandsComponent?>(user, out var hands) &&
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
_entities.TryGetComponent<SharedItemComponent?>(bottle, out var item))
2020-07-17 15:41:19 -05:00
{
if (hands.CanPutInHand(item))
{
hands.PutInHand(item);
continue;
}
}
//Put it on the floor
2021-12-05 18:09:01 +01:00
_entities.GetComponent<TransformComponent>(bottle).Coordinates = _entities.GetComponent<TransformComponent>(user).Coordinates;
2020-07-17 15:41:19 -05:00
//Give it an offset
bottle.RandomOffset(0.2f);
2020-07-17 15:41:19 -05:00
}
}
else //Pills
{
var individualVolume = BufferSolution.TotalVolume / FixedPoint2.New(pillAmount);
if (individualVolume < FixedPoint2.New(1))
{
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-low-text"));
return;
}
var actualVolume = FixedPoint2.Min(individualVolume, FixedPoint2.New(50));
2020-07-17 15:41:19 -05:00
for (int i = 0; i < pillAmount; i++)
{
var pill = _entities.SpawnEntity("Pill", _entities.GetComponent<TransformComponent>(Owner).Coordinates);
2020-07-17 15:41:19 -05:00
//Adding label
LabelComponent labelComponent = pill.EnsureComponent<LabelComponent>();
2021-12-05 18:09:01 +01:00
labelComponent.OriginalName = _entities.GetComponent<MetaDataComponent>(pill).EntityName;
string val = _entities.GetComponent<MetaDataComponent>(pill).EntityName + $" ({label})";
_entities.GetComponent<MetaDataComponent>(pill).EntityName = val;
labelComponent.CurrentLabel = label;
2020-07-17 15:41:19 -05:00
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
2021-12-03 15:53:09 +01:00
var pillSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(pill, "food");
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(pill, pillSolution, bufferSolution);
2020-07-17 15:41:19 -05:00
//Change pill Sprite component state
2021-12-05 18:09:01 +01:00
if (!_entities.TryGetComponent(pill, out SpriteComponent? sprite))
{
return;
}
sprite?.LayerSetState(0, "pill" + _pillType);
2020-07-17 15:41:19 -05:00
//Try to give them the bottle
2021-12-05 18:09:01 +01:00
if (_entities.TryGetComponent<HandsComponent?>(user, out var hands) &&
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
_entities.TryGetComponent<SharedItemComponent?>(pill, out var item))
2020-07-17 15:41:19 -05:00
{
if (hands.CanPutInHand(item))
{
hands.PutInHand(item);
continue;
}
}
//Put it on the floor
2021-12-05 18:09:01 +01:00
_entities.GetComponent<TransformComponent>(pill).Coordinates = _entities.GetComponent<TransformComponent>(user).Coordinates;
2020-07-17 15:41:19 -05:00
//Give it an offset
2020-09-02 01:23:43 +02:00
pill.RandomOffset(0.2f);
2020-07-17 15:41:19 -05:00
}
}
if (_bufferSolution?.Contents.Count == 0)
_label = "";
2020-07-17 15:41:19 -05:00
UpdateUserInterface();
}
/// <summary>
/// Called when you click the owner entity with an empty hand. Opens the UI client-side if possible.
/// </summary>
/// <param name="args">Data relevant to the event such as the actor which triggered it.</param>
void IActivate.Activate(ActivateEventArgs args)
{
2021-12-05 18:09:01 +01:00
if (!_entities.TryGetComponent(args.User, out ActorComponent? actor))
2020-07-17 15:41:19 -05:00
{
return;
}
2021-12-05 18:09:01 +01:00
if (!_entities.TryGetComponent(args.User, out HandsComponent? hands))
2020-07-17 15:41:19 -05:00
{
Owner.PopupMessage(args.User, Loc.GetString("chem-master-component-activate-no-hands"));
2020-07-17 15:41:19 -05:00
return;
}
var activeHandEntity = hands.GetActiveHandItem?.Owner;
2020-07-17 15:41:19 -05:00
if (activeHandEntity == null)
{
UserInterface?.Open(actor.PlayerSession);
2020-07-17 15:41:19 -05:00
}
}
private void ClickSound()
{
SoundSystem.Play(Filter.Pvs(Owner), _clickSound.GetSound(), Owner, AudioParams.Default.WithVolume(-2f));
2020-07-17 15:41:19 -05:00
}
}
}