ChemMaster ECS (#11052)
This commit is contained in:
@@ -1,365 +1,30 @@
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Server.Labels.Components;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using Content.Shared.Popups;
|
||||
using Robust.Server.GameObjects;
|
||||
using Content.Shared.Chemistry;
|
||||
using Robust.Shared.Audio;
|
||||
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Chemistry.Components
|
||||
{
|
||||
/// <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.
|
||||
/// An industrial grade chemical manipulator with pill and bottle production included.
|
||||
/// <seealso cref="ChemMasterSystem"/>
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedChemMasterComponent))]
|
||||
public sealed class ChemMasterComponent : SharedChemMasterComponent
|
||||
[Access(typeof(ChemMasterSystem))]
|
||||
public sealed class ChemMasterComponent : Component
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entities = default!;
|
||||
[Dependency] private readonly IEntitySystemManager _sysMan = default!;
|
||||
[DataField("pillType"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public uint PillType = 0;
|
||||
|
||||
[ViewVariables]
|
||||
private uint _pillType = 1;
|
||||
[DataField("mode"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public ChemMasterMode Mode = ChemMasterMode.Transfer;
|
||||
|
||||
[ViewVariables]
|
||||
private string _label = "";
|
||||
[DataField("pillProductionLimit", required: true), ViewVariables(VVAccess.ReadWrite)]
|
||||
public uint PillProductionLimit;
|
||||
|
||||
[ViewVariables]
|
||||
private bool _bufferModeTransfer = true;
|
||||
[DataField("bottleProductionLimit", required: true), ViewVariables(VVAccess.ReadWrite)]
|
||||
public uint BottleProductionLimit;
|
||||
|
||||
[ViewVariables]
|
||||
private bool Powered => !_entities.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
|
||||
|
||||
[ViewVariables]
|
||||
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");
|
||||
|
||||
|
||||
/// <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()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
|
||||
}
|
||||
|
||||
_bufferSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(Owner, SolutionName);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (obj.Session.AttachedEntity is not {Valid: true} player)
|
||||
return;
|
||||
|
||||
if (obj.Message is not UiActionMessage msg)
|
||||
return;
|
||||
|
||||
if (!PlayerCanUseChemMaster(player, true))
|
||||
return;
|
||||
|
||||
switch (msg.Action)
|
||||
{
|
||||
case UiAction.ChemButton:
|
||||
if (!_bufferModeTransfer)
|
||||
{
|
||||
if (msg.IsBuffer)
|
||||
{
|
||||
DiscardReagent(msg.Id, msg.Amount, BufferSolution);
|
||||
}
|
||||
else if (BeakerSlot.HasItem &&
|
||||
BeakerSlot.Item is {Valid: true} beaker &&
|
||||
_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) &&
|
||||
EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var beakerSolution))
|
||||
{
|
||||
DiscardReagent(msg.Id, msg.Amount, beakerSolution);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TransferReagent(msg.Id, msg.Amount, msg.IsBuffer);
|
||||
}
|
||||
break;
|
||||
case UiAction.Transfer:
|
||||
_bufferModeTransfer = true;
|
||||
UpdateUserInterface();
|
||||
break;
|
||||
case UiAction.Discard:
|
||||
_bufferModeTransfer = false;
|
||||
UpdateUserInterface();
|
||||
break;
|
||||
case UiAction.SetPillType:
|
||||
_pillType = msg.PillType;
|
||||
UpdateUserInterface();
|
||||
break;
|
||||
case UiAction.CreatePills:
|
||||
case UiAction.CreateBottles:
|
||||
_label = msg.Label;
|
||||
TryCreatePackage(player, msg.Action, msg.Label, msg.PillAmount, msg.BottleAmount);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
UpdateUserInterface();
|
||||
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>
|
||||
/// <returns>Returns true if the entity can use the chem master, and false if it cannot.</returns>
|
||||
private bool PlayerCanUseChemMaster(EntityUid playerEntity, bool needsPower = true)
|
||||
{
|
||||
//Need player entity to check if they are still able to use the chem master
|
||||
if (playerEntity == default)
|
||||
return false;
|
||||
|
||||
//Check if device is powered
|
||||
if (needsPower && !Powered)
|
||||
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()
|
||||
{
|
||||
if (BeakerSlot.Item is not {Valid: true} beaker ||
|
||||
!_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) ||
|
||||
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var beakerSolution))
|
||||
{
|
||||
return new ChemMasterBoundUserInterfaceState(Powered, false, FixedPoint2.New(0), FixedPoint2.New(0),
|
||||
"", _label, _entities.GetComponent<MetaDataComponent>(Owner).EntityName, new List<Solution.ReagentQuantity>(), BufferSolution.Contents, _bufferModeTransfer,
|
||||
BufferSolution.TotalVolume, _pillType);
|
||||
}
|
||||
|
||||
return new ChemMasterBoundUserInterfaceState(Powered, true, beakerSolution.CurrentVolume,
|
||||
beakerSolution.MaxVolume,
|
||||
_entities.GetComponent<MetaDataComponent>(beaker).EntityName, _label, _entities.GetComponent<MetaDataComponent>(Owner).EntityName, beakerSolution.Contents, BufferSolution.Contents, _bufferModeTransfer,
|
||||
BufferSolution.TotalVolume, _pillType);
|
||||
}
|
||||
|
||||
public void UpdateUserInterface()
|
||||
{
|
||||
if (!Initialized) return;
|
||||
|
||||
var state = GetUserInterfaceState();
|
||||
UserInterface?.SetState(state);
|
||||
}
|
||||
|
||||
private void DiscardReagent(string id, FixedPoint2 amount, Solution solution)
|
||||
{
|
||||
foreach (var reagent in solution.Contents)
|
||||
{
|
||||
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
|
||||
{
|
||||
actualAmount = reagent.Quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
actualAmount = FixedPoint2.Min(reagent.Quantity, amount);
|
||||
}
|
||||
solution.RemoveReagent(id, actualAmount);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void TransferReagent(string id, FixedPoint2 amount, bool isBuffer)
|
||||
{
|
||||
if (!BeakerSlot.HasItem ||
|
||||
BeakerSlot.Item is not {Valid: true} beaker ||
|
||||
!_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) ||
|
||||
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var beakerSolution))
|
||||
return;
|
||||
|
||||
if (isBuffer)
|
||||
{
|
||||
foreach (var reagent in BufferSolution.Contents)
|
||||
{
|
||||
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
|
||||
{
|
||||
actualAmount = FixedPoint2.Min(reagent.Quantity, beakerSolution.AvailableVolume);
|
||||
}
|
||||
else
|
||||
{
|
||||
actualAmount = FixedPoint2.Min(reagent.Quantity, amount, beakerSolution.AvailableVolume);
|
||||
}
|
||||
|
||||
|
||||
BufferSolution.RemoveReagent(id, actualAmount);
|
||||
EntitySystem.Get<SolutionContainerSystem>()
|
||||
.TryAddReagent(beaker, beakerSolution, id, actualAmount, out var _);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var reagent in beakerSolution.Contents)
|
||||
{
|
||||
if (reagent.ReagentId == id)
|
||||
{
|
||||
FixedPoint2 actualAmount;
|
||||
if (amount == FixedPoint2.New(-1))
|
||||
{
|
||||
actualAmount = reagent.Quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
actualAmount = FixedPoint2.Min(reagent.Quantity, amount);
|
||||
}
|
||||
|
||||
EntitySystem.Get<SolutionContainerSystem>().TryRemoveReagent(beaker, beakerSolution, id, actualAmount);
|
||||
BufferSolution.AddReagent(id, actualAmount);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
_label = GenerateLabel();
|
||||
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;
|
||||
}
|
||||
|
||||
private void TryCreatePackage(EntityUid user, UiAction action, string label, int pillAmount, int bottleAmount)
|
||||
{
|
||||
if (BufferSolution.TotalVolume == 0)
|
||||
{
|
||||
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-empty-text"));
|
||||
return;
|
||||
}
|
||||
|
||||
var handSys = _sysMan.GetEntitySystem<SharedHandsSystem>();
|
||||
var solSys = _sysMan.GetEntitySystem<SolutionContainerSystem>();
|
||||
|
||||
pillAmount = Math.Clamp(pillAmount, 0, MaxEntitySpawns);
|
||||
bottleAmount = Math.Clamp(bottleAmount, 0, MaxEntitySpawns);
|
||||
|
||||
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));
|
||||
for (int i = 0; i < bottleAmount; i++)
|
||||
{
|
||||
var bottle = _entities.SpawnEntity("ChemistryEmptyBottle01", _entities.GetComponent<TransformComponent>(Owner).Coordinates);
|
||||
|
||||
//Adding label
|
||||
LabelComponent labelComponent = bottle.EnsureComponent<LabelComponent>();
|
||||
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);
|
||||
var bottleSolution = solSys.EnsureSolution(bottle, "drink");
|
||||
|
||||
solSys.TryAddSolution(bottle, bottleSolution, bufferSolution);
|
||||
|
||||
//Try to give them the bottle
|
||||
handSys.PickupOrDrop(user, bottle);
|
||||
}
|
||||
}
|
||||
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));
|
||||
for (int i = 0; i < pillAmount; i++)
|
||||
{
|
||||
var pill = _entities.SpawnEntity("Pill", _entities.GetComponent<TransformComponent>(Owner).Coordinates);
|
||||
|
||||
//Adding label
|
||||
LabelComponent labelComponent = pill.EnsureComponent<LabelComponent>();
|
||||
labelComponent.OriginalName = _entities.GetComponent<MetaDataComponent>(pill).EntityName;
|
||||
string val = _entities.GetComponent<MetaDataComponent>(pill).EntityName + $" ({label})";
|
||||
_entities.GetComponent<MetaDataComponent>(pill).EntityName = val;
|
||||
labelComponent.CurrentLabel = label;
|
||||
|
||||
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
|
||||
var pillSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(pill, "food");
|
||||
solSys.TryAddSolution(pill, pillSolution, bufferSolution);
|
||||
|
||||
//Change pill Sprite component state
|
||||
if (!_entities.TryGetComponent(pill, out SpriteComponent? sprite))
|
||||
{
|
||||
return;
|
||||
}
|
||||
sprite?.LayerSetState(0, "pill" + _pillType);
|
||||
|
||||
//Try to give them the bottle
|
||||
handSys.PickupOrDrop(user, pill);
|
||||
}
|
||||
}
|
||||
|
||||
if (_bufferSolution?.Contents.Count == 0)
|
||||
_label = "";
|
||||
|
||||
UpdateUserInterface();
|
||||
}
|
||||
|
||||
private void ClickSound()
|
||||
{
|
||||
SoundSystem.Play(_clickSound.GetSound(), Filter.Pvs(Owner), Owner, AudioParams.Default.WithVolume(-2f));
|
||||
}
|
||||
[DataField("clickSound"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public SoundSpecifier ClickSound = new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,249 @@
|
||||
using Content.Server.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.EntitySystems;
|
||||
using Content.Server.Labels.Components;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.Chemistry;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Containers.ItemSlots;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Hands.EntitySystems;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
|
||||
namespace Content.Server.Chemistry.EntitySystems
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Contains all the server-side logic for ChemMasters.
|
||||
/// <seealso cref="ChemMasterComponent"/>
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public sealed class ChemMasterSystem : SharedChemMasterSystem
|
||||
public sealed class ChemMasterSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly AudioSystem _audioSystem = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ChemMasterComponent, ComponentStartup>((_, comp, _) => comp.UpdateUserInterface());
|
||||
SubscribeLocalEvent<ChemMasterComponent, SolutionChangedEvent>((_, comp, _) => comp.UpdateUserInterface());
|
||||
SubscribeLocalEvent<ChemMasterComponent, EntInsertedIntoContainerMessage>((_, comp, _) => comp.UpdateUserInterface());
|
||||
SubscribeLocalEvent<ChemMasterComponent, EntRemovedFromContainerMessage>((_, comp, _) => comp.UpdateUserInterface());
|
||||
|
||||
SubscribeLocalEvent<ChemMasterComponent, ComponentStartup>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ChemMasterComponent, SolutionChangedEvent>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ChemMasterComponent, EntInsertedIntoContainerMessage>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ChemMasterComponent, EntRemovedFromContainerMessage>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ChemMasterComponent, BoundUIOpenedEvent>((_, comp, _) => UpdateUiState(comp));
|
||||
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterSetModeMessage>(OnSetModeMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterSetPillTypeMessage>(OnSetPillTypeMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterReagentAmountButtonMessage>(OnReagentButtonMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterCreatePillsMessage>(OnCreatePillsMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterCreateBottlesMessage>(OnCreateBottlesMessage);
|
||||
}
|
||||
|
||||
private void UpdateUiState(ChemMasterComponent chemMaster, bool updateLabel = false)
|
||||
{
|
||||
if (!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
return;
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.ContainerSlotName);
|
||||
|
||||
Solution? containerSolution = null;
|
||||
|
||||
if (container.HasValue && container.Value.Valid)
|
||||
{
|
||||
TryComp(container, out FitsInDispenserComponent? fits);
|
||||
_solutionContainerSystem.TryGetSolution(container.Value, fits!.Solution, out containerSolution);
|
||||
}
|
||||
|
||||
var containerName = container is null ? null : Name(container.Value);
|
||||
var dispenserName = Name(chemMaster.Owner);
|
||||
var bufferReagents = bufferSolution.Contents;
|
||||
var bufferCurrentVolume = bufferSolution.CurrentVolume;
|
||||
|
||||
var state = new ChemMasterBoundUserInterfaceState(
|
||||
containerSolution?.CurrentVolume, containerSolution?.MaxVolume, containerName, dispenserName,
|
||||
containerSolution?.Contents, bufferReagents, chemMaster.Mode, bufferCurrentVolume, chemMaster.PillType,
|
||||
chemMaster.PillProductionLimit, chemMaster.BottleProductionLimit, updateLabel
|
||||
);
|
||||
_userInterfaceSystem.TrySetUiState(chemMaster.Owner, ChemMasterUiKey.Key, state);
|
||||
}
|
||||
|
||||
private void OnSetModeMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterSetModeMessage message)
|
||||
{
|
||||
// Ensure the mode is valid, either Transfer or Discard.
|
||||
if (!Enum.IsDefined(typeof(ChemMasterMode), message.ChemMasterMode))
|
||||
return;
|
||||
|
||||
chemMaster.Mode = message.ChemMasterMode;
|
||||
UpdateUiState(chemMaster);
|
||||
ClickSound(chemMaster);
|
||||
}
|
||||
|
||||
private void OnSetPillTypeMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterSetPillTypeMessage message)
|
||||
{
|
||||
// Ensure valid pill type. There are 20 pills selectable, 0-19.
|
||||
if (message.PillType > SharedChemMaster.PillTypes - 1)
|
||||
return;
|
||||
|
||||
chemMaster.PillType = message.PillType;
|
||||
UpdateUiState(chemMaster);
|
||||
ClickSound(chemMaster);
|
||||
}
|
||||
|
||||
private void OnReagentButtonMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterReagentAmountButtonMessage message)
|
||||
{
|
||||
// Ensure the amount corresponds to one of the reagent amount buttons.
|
||||
if (!Enum.IsDefined(typeof(ChemMasterReagentAmount), message.Amount))
|
||||
return;
|
||||
|
||||
switch (chemMaster.Mode)
|
||||
{
|
||||
case ChemMasterMode.Transfer:
|
||||
TransferReagents(chemMaster, message.ReagentId, message.Amount.GetFixedPoint(), message.FromBuffer);
|
||||
break;
|
||||
case ChemMasterMode.Discard:
|
||||
DiscardReagents(chemMaster, message.ReagentId, message.Amount.GetFixedPoint(), message.FromBuffer);
|
||||
break;
|
||||
default:
|
||||
// Invalid mode.
|
||||
return;
|
||||
}
|
||||
|
||||
ClickSound(chemMaster);
|
||||
}
|
||||
|
||||
private void TransferReagents(ChemMasterComponent chemMaster, string reagentId, FixedPoint2 amount, bool fromBuffer)
|
||||
{
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.ContainerSlotName);
|
||||
if (container is null ||
|
||||
!_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution) ||
|
||||
!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (fromBuffer) // Buffer to container
|
||||
{
|
||||
amount = FixedPoint2.Min(amount, containerSolution.AvailableVolume);
|
||||
amount = bufferSolution.RemoveReagent(reagentId, amount);
|
||||
_solutionContainerSystem.TryAddReagent(container.Value, containerSolution, reagentId, amount, out var _);
|
||||
}
|
||||
else // Container to buffer
|
||||
{
|
||||
amount = FixedPoint2.Min(amount, containerSolution.GetReagentQuantity(reagentId));
|
||||
_solutionContainerSystem.TryRemoveReagent(container.Value, containerSolution, reagentId, amount);
|
||||
bufferSolution.AddReagent(reagentId, amount);
|
||||
}
|
||||
|
||||
UpdateUiState(chemMaster, updateLabel: true);
|
||||
}
|
||||
|
||||
private void DiscardReagents(ChemMasterComponent chemMaster, string reagentId, FixedPoint2 amount, bool fromBuffer)
|
||||
{
|
||||
|
||||
if (fromBuffer)
|
||||
{
|
||||
if (_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
bufferSolution.RemoveReagent(reagentId, amount);
|
||||
else
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.ContainerSlotName);
|
||||
if (container is not null &&
|
||||
_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution))
|
||||
{
|
||||
_solutionContainerSystem.TryRemoveReagent(container.Value, containerSolution, reagentId, amount);
|
||||
}
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateUiState(chemMaster, updateLabel: fromBuffer);
|
||||
}
|
||||
|
||||
private void OnCreatePillsMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterCreatePillsMessage message)
|
||||
{
|
||||
// Ensure the amount is valid.
|
||||
if (message.Amount == 0 || message.Amount > chemMaster.PillProductionLimit)
|
||||
return;
|
||||
|
||||
CreatePillsOrBottles(chemMaster, pills: true, message.Amount, message.Label, message.Session.AttachedEntity);
|
||||
UpdateUiState(chemMaster);
|
||||
ClickSound(chemMaster);
|
||||
}
|
||||
|
||||
private void OnCreateBottlesMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterCreateBottlesMessage message)
|
||||
{
|
||||
// Ensure the amount is valid.
|
||||
if (message.Amount == 0 || message.Amount > chemMaster.BottleProductionLimit)
|
||||
return;
|
||||
|
||||
CreatePillsOrBottles(chemMaster, pills: false, message.Amount, message.Label, message.Session.AttachedEntity);
|
||||
UpdateUiState(chemMaster);
|
||||
ClickSound(chemMaster);
|
||||
}
|
||||
|
||||
private void CreatePillsOrBottles(ChemMasterComponent chemMaster, bool pills, FixedPoint2 amount, string label, EntityUid? user)
|
||||
{
|
||||
if (!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
return;
|
||||
|
||||
var filter = user.HasValue ? Filter.Entities(user.Value) : Filter.Empty();
|
||||
if (bufferSolution.TotalVolume == 0)
|
||||
{
|
||||
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-empty-text"), filter);
|
||||
return;
|
||||
}
|
||||
|
||||
var individualVolume = FixedPoint2.Min(bufferSolution.TotalVolume / amount, FixedPoint2.New(pills ? 50 : 30));
|
||||
if (individualVolume < FixedPoint2.New(1))
|
||||
{
|
||||
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-low-text"), filter);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < amount; i++)
|
||||
{
|
||||
var item = Spawn(pills ? "Pill" : "ChemistryEmptyBottle01", Transform(chemMaster.Owner).Coordinates);
|
||||
|
||||
if (label != "")
|
||||
{
|
||||
var labelComponent = EnsureComp<LabelComponent>(item);
|
||||
labelComponent.OriginalName = Name(item);
|
||||
string val = Name(item) + $" ({label})";
|
||||
Comp<MetaDataComponent>(item).EntityName = val;
|
||||
labelComponent.CurrentLabel = label;
|
||||
}
|
||||
|
||||
var solution = bufferSolution.SplitSolution(individualVolume);
|
||||
var itemSolution = _solutionContainerSystem.EnsureSolution(item,
|
||||
pills ? SharedChemMaster.PillSolutionName : SharedChemMaster.BottleSolutionName);
|
||||
_solutionContainerSystem.TryAddSolution(item, itemSolution, solution);
|
||||
|
||||
if (pills)
|
||||
{
|
||||
if (TryComp<SpriteComponent>(item, out var spriteComp))
|
||||
spriteComp.LayerSetState(0, "pill" + (chemMaster.PillType + 1));
|
||||
}
|
||||
|
||||
if (user.HasValue)
|
||||
_handsSystem.PickupOrDrop(user, item);
|
||||
}
|
||||
|
||||
UpdateUiState(chemMaster);
|
||||
}
|
||||
|
||||
private void ClickSound(ChemMasterComponent chemMaster)
|
||||
{
|
||||
_audioSystem.Play(chemMaster.ClickSound, Filter.Pvs(chemMaster.Owner), chemMaster.Owner, AudioParams.Default.WithVolume(-2f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
using Content.Server.Chemistry.Components;
|
||||
using Content.Server.Power.Components;
|
||||
|
||||
namespace Content.Server.Chemistry.EntitySystems;
|
||||
|
||||
public sealed partial class ChemistrySystem
|
||||
{
|
||||
private void InitializeChemMaster()
|
||||
{
|
||||
SubscribeLocalEvent<ChemMasterComponent, PowerChangedEvent>(OnChemMasterPowerChange);
|
||||
}
|
||||
|
||||
private static void OnChemMasterPowerChange(EntityUid uid, ChemMasterComponent component, PowerChangedEvent args)
|
||||
{
|
||||
component.UpdateUserInterface();
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ public sealed partial class ChemistrySystem : EntitySystem
|
||||
public override void Initialize()
|
||||
{
|
||||
// Why ChemMaster duplicates reagentdispenser nobody knows.
|
||||
InitializeChemMaster();
|
||||
InitializeHypospray();
|
||||
InitializeInjector();
|
||||
InitializeReagentDispenser();
|
||||
|
||||
Reference in New Issue
Block a user