Rework the ChemMaster's output handling (#11207)
* Fix doc comment on FitsInDispenserComponent It's clearly intended to be a doc comment, but wasn't. * Allow the ChemMaster to accept canisters and bottles * Give the ChemMaster an output container slot * Tweak ChemMaster UI layout * Make more ChemMaster UI tweaks * Update ChemMaster SpinBox max handling * Rework the ChemMaster * Apply suggestions from code review Co-authored-by: Flipp Syder <76629141+vulppine@users.noreply.github.com> * Implement PR feedback * Switch ChemMaster to a tabbed UI layout * Rename Amount to Dosage for clarity * Replace Amount with Dosage in messages * Clarify dose in UI Co-authored-by: Flipp Syder <76629141+vulppine@users.noreply.github.com>
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Server.Chemistry.Components;
|
||||
using Content.Server.Labels.Components;
|
||||
using Content.Server.Popups;
|
||||
using Content.Server.Storage.Components;
|
||||
using Content.Server.Storage.EntitySystems;
|
||||
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;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
|
||||
namespace Content.Server.Chemistry.EntitySystems
|
||||
@@ -28,7 +32,9 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
||||
[Dependency] private readonly StorageSystem _storageSystem = default!;
|
||||
|
||||
private const string PillPrototypeId = "Pill";
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -44,33 +50,26 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterSetPillTypeMessage>(OnSetPillTypeMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterReagentAmountButtonMessage>(OnReagentButtonMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterCreatePillsMessage>(OnCreatePillsMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterCreateBottlesMessage>(OnCreateBottlesMessage);
|
||||
SubscribeLocalEvent<ChemMasterComponent, ChemMasterOutputToBottleMessage>(OnOutputToBottleMessage);
|
||||
}
|
||||
|
||||
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);
|
||||
var inputContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
var outputContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
|
||||
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
|
||||
);
|
||||
chemMaster.Mode, dispenserName,
|
||||
BuildInputContainerInfo(inputContainer), BuildOutputContainerInfo(outputContainer),
|
||||
bufferReagents, bufferCurrentVolume,
|
||||
chemMaster.PillType, chemMaster.PillDosageLimit, updateLabel);
|
||||
|
||||
_userInterfaceSystem.TrySetUiState(chemMaster.Owner, ChemMasterUiKey.Key, state);
|
||||
}
|
||||
|
||||
@@ -120,7 +119,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
|
||||
private void TransferReagents(ChemMasterComponent chemMaster, string reagentId, FixedPoint2 amount, bool fromBuffer)
|
||||
{
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.ContainerSlotName);
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
if (container is null ||
|
||||
!_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution) ||
|
||||
!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
@@ -156,7 +155,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
}
|
||||
else
|
||||
{
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.ContainerSlotName);
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
if (container is not null &&
|
||||
_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution))
|
||||
{
|
||||
@@ -171,79 +170,172 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
|
||||
private void OnCreatePillsMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterCreatePillsMessage message)
|
||||
{
|
||||
var user = message.Session.AttachedEntity;
|
||||
var maybeContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
if (maybeContainer is not { Valid: true } container
|
||||
|| !TryComp(container, out ServerStorageComponent? storage)
|
||||
|| storage.Storage is null)
|
||||
{
|
||||
return; // output can't fit pills
|
||||
}
|
||||
|
||||
// Ensure the number is valid.
|
||||
if (message.Number == 0 || message.Number > storage.StorageCapacityMax - storage.StorageUsed)
|
||||
return;
|
||||
|
||||
// Ensure the amount is valid.
|
||||
if (message.Amount == 0 || message.Amount > chemMaster.PillProductionLimit)
|
||||
if (message.Dosage == 0 || message.Dosage > chemMaster.PillDosageLimit)
|
||||
return;
|
||||
|
||||
CreatePillsOrBottles(chemMaster, pills: true, message.Amount, message.Label, message.Session.AttachedEntity);
|
||||
var needed = message.Dosage * message.Number;
|
||||
if (!WithdrawFromBuffer(chemMaster, needed, user, out var withdrawal))
|
||||
return;
|
||||
|
||||
Label(container, message.Label);
|
||||
|
||||
for (var i = 0; i < message.Number; i++)
|
||||
{
|
||||
var item = Spawn(PillPrototypeId, Transform(container).Coordinates);
|
||||
DebugTools.Assert(_storageSystem.Insert(container, item, storage));
|
||||
Label(item, message.Label);
|
||||
|
||||
var itemSolution = _solutionContainerSystem.EnsureSolution(item, SharedChemMaster.PillSolutionName);
|
||||
|
||||
DebugTools.Assert(_solutionContainerSystem.TryAddSolution(
|
||||
item, itemSolution, withdrawal.SplitSolution(message.Dosage)));
|
||||
|
||||
if (TryComp<SpriteComponent>(item, out var spriteComp))
|
||||
spriteComp.LayerSetState(0, "pill" + (chemMaster.PillType + 1));
|
||||
}
|
||||
|
||||
UpdateUiState(chemMaster);
|
||||
ClickSound(chemMaster);
|
||||
}
|
||||
|
||||
private void OnCreateBottlesMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterCreateBottlesMessage message)
|
||||
private void OnOutputToBottleMessage(
|
||||
EntityUid uid, ChemMasterComponent chemMaster, ChemMasterOutputToBottleMessage message)
|
||||
{
|
||||
var user = message.Session.AttachedEntity;
|
||||
var maybeContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
if (maybeContainer is not { Valid: true } container
|
||||
|| !_solutionContainerSystem.TryGetSolution(
|
||||
container, SharedChemMaster.BottleSolutionName, out var solution))
|
||||
{
|
||||
return; // output can't fit reagents
|
||||
}
|
||||
|
||||
// Ensure the amount is valid.
|
||||
if (message.Amount == 0 || message.Amount > chemMaster.BottleProductionLimit)
|
||||
if (message.Dosage == 0 || message.Dosage > solution.AvailableVolume)
|
||||
return;
|
||||
|
||||
if (!WithdrawFromBuffer(chemMaster, message.Dosage, user, out var withdrawal))
|
||||
return;
|
||||
|
||||
CreatePillsOrBottles(chemMaster, pills: false, message.Amount, message.Label, message.Session.AttachedEntity);
|
||||
Label(container, message.Label);
|
||||
DebugTools.Assert(_solutionContainerSystem.TryAddSolution(
|
||||
container, solution, withdrawal));
|
||||
|
||||
UpdateUiState(chemMaster);
|
||||
ClickSound(chemMaster);
|
||||
}
|
||||
|
||||
private void CreatePillsOrBottles(ChemMasterComponent chemMaster, bool pills, FixedPoint2 amount, string label, EntityUid? user)
|
||||
private bool WithdrawFromBuffer(
|
||||
IComponent chemMaster,
|
||||
FixedPoint2 neededVolume, EntityUid? user,
|
||||
[NotNullWhen(returnValue: true)] out Solution? outputSolution)
|
||||
{
|
||||
if (!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
return;
|
||||
outputSolution = null;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetSolution(
|
||||
chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var solution))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var filter = user.HasValue ? Filter.Entities(user.Value) : Filter.Empty();
|
||||
if (bufferSolution.TotalVolume == 0)
|
||||
if (solution.TotalVolume == 0)
|
||||
{
|
||||
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-empty-text"), filter);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
var individualVolume = FixedPoint2.Min(bufferSolution.TotalVolume / amount, FixedPoint2.New(pills ? 50 : 30));
|
||||
if (individualVolume < FixedPoint2.New(1))
|
||||
// ReSharper disable once InvertIf
|
||||
if (neededVolume > solution.CurrentVolume)
|
||||
{
|
||||
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-low-text"), filter);
|
||||
return false;
|
||||
}
|
||||
|
||||
outputSolution = solution.SplitSolution(neededVolume);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Label(EntityUid ent, string label)
|
||||
{
|
||||
if (string.IsNullOrEmpty(label))
|
||||
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);
|
||||
var labelComponent = EnsureComp<LabelComponent>(ent);
|
||||
|
||||
labelComponent.OriginalName = Name(ent);
|
||||
var val = Name(ent) + $" ({label})";
|
||||
MetaData(ent).EntityName = val;
|
||||
labelComponent.CurrentLabel = label;
|
||||
}
|
||||
|
||||
private void ClickSound(ChemMasterComponent chemMaster)
|
||||
{
|
||||
_audioSystem.Play(chemMaster.ClickSound, Filter.Pvs(chemMaster.Owner), chemMaster.Owner, AudioParams.Default.WithVolume(-2f));
|
||||
}
|
||||
|
||||
private ContainerInfo? BuildInputContainerInfo(EntityUid? container)
|
||||
{
|
||||
if (container is not { Valid: true })
|
||||
return null;
|
||||
|
||||
if (!TryComp(container, out FitsInDispenserComponent? fits)
|
||||
|| !_solutionContainerSystem.TryGetSolution(container.Value, fits.Solution, out var solution))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return BuildContainerInfo(Name(container.Value), solution);
|
||||
}
|
||||
|
||||
private ContainerInfo? BuildOutputContainerInfo(EntityUid? container)
|
||||
{
|
||||
if (container is not { Valid: true })
|
||||
return null;
|
||||
|
||||
var name = Name(container.Value);
|
||||
{
|
||||
if (_solutionContainerSystem.TryGetSolution(
|
||||
container.Value, SharedChemMaster.BottleSolutionName, out var solution))
|
||||
{
|
||||
return BuildContainerInfo(name, solution);
|
||||
}
|
||||
}
|
||||
|
||||
if (!TryComp(container, out ServerStorageComponent? storage))
|
||||
return null;
|
||||
|
||||
var pills = storage.Storage?.ContainedEntities.Select(pill =>
|
||||
{
|
||||
_solutionContainerSystem.TryGetSolution(pill, SharedChemMaster.PillSolutionName, out var solution);
|
||||
var quantity = solution?.CurrentVolume ?? FixedPoint2.Zero;
|
||||
return (Name(pill), quantity);
|
||||
}).ToList();
|
||||
|
||||
return pills is null
|
||||
? null
|
||||
: new ContainerInfo(name, false, storage.StorageUsed, storage.StorageCapacityMax, pills);
|
||||
}
|
||||
|
||||
private static ContainerInfo BuildContainerInfo(string name, Solution solution)
|
||||
{
|
||||
var reagents = solution.Contents
|
||||
.Select(reagent => (reagent.ReagentId, reagent.Quantity)).ToList();
|
||||
|
||||
return new ContainerInfo(name, true, solution.CurrentVolume, solution.MaxVolume, reagents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user