Added auto label for ChemMaster (#5596)

This commit is contained in:
Spartak
2021-11-28 18:25:23 -08:00
committed by GitHub
parent 47186a6dec
commit 30c87ca6b2
6 changed files with 137 additions and 65 deletions

View File

@@ -24,6 +24,8 @@ using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
using Robust.Shared.Log;
using Content.Server.Labels.Components;
namespace Content.Server.Chemistry.Components
{
@@ -40,7 +42,10 @@ namespace Content.Server.Chemistry.Components
{
[ViewVariables]
private uint _pillType = 1;
[ViewVariables]
private string _label = "";
[ViewVariables]
private bool _bufferModeTransfer = true;
@@ -66,7 +71,7 @@ namespace Content.Server.Chemistry.Components
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
@@ -102,9 +107,7 @@ namespace Content.Server.Chemistry.Components
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
{
if (obj.Session.AttachedEntity == null)
{
return;
}
var msg = (UiActionMessage) obj.Message;
var needsPower = msg.Action switch
@@ -138,7 +141,8 @@ namespace Content.Server.Chemistry.Components
break;
case UiAction.CreatePills:
case UiAction.CreateBottles:
TryCreatePackage(obj.Session.AttachedEntity, msg.Action, msg.PillAmount, msg.BottleAmount);
_label = msg.Label;
TryCreatePackage(obj.Session.AttachedEntity, msg.Action, msg.Label, msg.PillAmount, msg.BottleAmount);
break;
default:
throw new ArgumentOutOfRangeException();
@@ -182,13 +186,13 @@ namespace Content.Server.Chemistry.Components
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker.Uid, fits.Solution, out var beakerSolution))
{
return new ChemMasterBoundUserInterfaceState(Powered, false, FixedPoint2.New(0), FixedPoint2.New(0),
"", Owner.Name, new List<Solution.ReagentQuantity>(), BufferSolution.Contents, _bufferModeTransfer,
"", _label, Owner.Name, new List<Solution.ReagentQuantity>(), BufferSolution.Contents, _bufferModeTransfer,
BufferSolution.TotalVolume, _pillType);
}
return new ChemMasterBoundUserInterfaceState(Powered, true, beakerSolution.CurrentVolume,
beakerSolution.MaxVolume,
beaker.Name, Owner.Name, beakerSolution.Contents, BufferSolution.Contents, _bufferModeTransfer,
beaker.Name, _label, Owner.Name, beakerSolution.Contents, BufferSolution.Contents, _bufferModeTransfer,
BufferSolution.TotalVolume, _pillType);
}
@@ -260,26 +264,51 @@ namespace Content.Server.Chemistry.Components
}
}
}
_label = GenerateLabel();
UpdateUserInterface();
}
private void TryCreatePackage(IEntity user, UiAction action, int pillAmount, int bottleAmount)
/// <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(IEntity user, UiAction action, string label, int pillAmount, int bottleAmount)
{
if (BufferSolution.TotalVolume == 0)
{
user.PopupMessageCursor(Loc.GetString("chem-master-window-buffer-empty-text"));
return;
}
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 = Owner.EntityManager.SpawnEntity("ChemistryEmptyBottle01", Owner.Transform.Coordinates);
//Adding label
LabelComponent labelComponent = bottle.EnsureComponent<LabelComponent>();
labelComponent.OriginalName = bottle.Name;
bottle.Name += $" ({label})";
labelComponent.CurrentLabel = label;
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
var bottleSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(bottle.Uid, "drink");
@@ -306,15 +335,23 @@ namespace Content.Server.Chemistry.Components
{
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 = Owner.EntityManager.SpawnEntity("pill", Owner.Transform.Coordinates);
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
//Adding label
LabelComponent labelComponent = pill.EnsureComponent<LabelComponent>();
labelComponent.OriginalName = pill.Name;
pill.Name += $" ({label})";
labelComponent.CurrentLabel = label;
var bufferSolution = BufferSolution.SplitSolution(actualVolume);
var pillSolution = EntitySystem.Get<SolutionContainerSystem>().EnsureSolution(pill.Uid, "food");
EntitySystem.Get<SolutionContainerSystem>().TryAddSolution(pill.Uid, pillSolution, bufferSolution);
@@ -343,6 +380,9 @@ namespace Content.Server.Chemistry.Components
}
}
if (_bufferSolution?.Contents.Count == 0)
_label = "";
UpdateUserInterface();
}