Files
OldThink/Content.Server/Chemistry/EntitySystems/ChemMasterSystem.cs
Remuchi 467eb84d43 [WIP] [Tweak] Ограничение раздатчиков реагентов и прочих жидкостей. (#498)
* Reapply "Add limited-reagent dispensers (#23907)"

This reverts commit 531becd592.

# Conflicts:
#	Content.Client/Chemistry/UI/ReagentDispenserWindow.xaml.cs
#	Content.Server/Chemistry/EntitySystems/ReagentDispenserSystem.cs
#	Content.Shared/Chemistry/SharedReagentDispenser.cs
#	Content.Shared/Containers/ItemSlot/ItemSlotsComponent.cs
#	Resources/Prototypes/Catalog/ReagentDispensers/beverage.yml
#	Resources/Prototypes/Entities/Objects/Specific/chemical-containers.yml
#	Resources/Prototypes/Entities/Structures/Dispensers/chem.yml

* Reapply "Fix comp.Owner (#24206)"

This reverts commit 2d2405ff93.

* Reapply "Use old reagent dispenser beaker slot ID (#24209)"

This reverts commit 388424e2f3.

* Add a container display to dispenser UI (#25391)

* Implemented contents display for dispenser UI

* Update Content.Client/Chemistry/UI/ReagentDispenserWindow.xaml.cs

Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>

* Resolve the netent into a euid first

---------

Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>

* Add prediction to hand labeler labels (#25869)

Added prediction to labels

* scoopable ash and foam, solution transfer prediction (#25832)

* move SolutionTransfer to shared and predict as much as possible

* fully move OpenableSystem to shared now that SolutionTransfer is

* fix imports for everything

* doc for solution transfer system

* trolling

* add scoopable system

* make ash and foam scoopable

* untroll

* untroll real

* make clickable it work

* troll

* the scooping room

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
# Conflicts:
#	Resources/Prototypes/Entities/Effects/chemistry_effects.yml

* Reagent dispenser UI (#27831)

* reagent dispenser: fancy window

* reagent dispenser: dispense button grid

* reagent dispenser: rearrange containers & info

* reagent dispenser: remove `reagent-dispenser-window-container-label`

* reagent dispenser: add `Scrollcontainer` on right side

* reagent dispenser: get rid of pointless actions

* reagent dispenser: cleanup actions and `inventory` field on bound ui state

* reagent dispenser: cool reagent cards & finishing touches

* reagent dispenser: final cleanup and formatting

* reagent dispenser: `ButtonGrid` and `ReagentDispenserSetDispenseAmountMessage` refactor

* reagent dispenser: cleanup code & address minor concerns

* reagent dispenser: text in reagent cards no longer clips

* reagent dispenser: oh wait i forgot to change this and thats why the builds keep failing probably

* reagent dispenser mayybe this

* reagent dispenser: remove `using FastAccessors;`
# Conflicts:
#	Content.Client/Chemistry/UI/ReagentDispenserWindow.xaml.cs

* Reagent dispenser UI (Again) (#27958)

* reagent dispenser: fancy window

* reagent dispenser: dispense button grid

* reagent dispenser: rearrange containers & info

* reagent dispenser: remove `reagent-dispenser-window-container-label`

* reagent dispenser: add `Scrollcontainer` on right side

* reagent dispenser: get rid of pointless actions

* reagent dispenser: cleanup actions and `inventory` field on bound ui state

* reagent dispenser: cool reagent cards & finishing touches

* reagent dispenser: final cleanup and formatting

* reagent dispenser: `ButtonGrid` and `ReagentDispenserSetDispenseAmountMessage` refactor

* reagent dispenser: cleanup code & address minor concerns

* reagent dispenser: text in reagent cards no longer clips

* reagent dispenser: oh wait i forgot to change this and thats why the builds keep failing probably

* reagent dispenser mayybe this

* reagent dispenser: remove `using FastAccessors;`

* delete unused classes

* disable reagent button when container is empty

* Make things a bit bigger

* remove obsolete text color override

* пару фиксов поверх

* переводики хихиххи

* еще переводы

* feat: химические картриджи

---------

Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: Brandon Li <48413902+aspiringLich@users.noreply.github.com>
2024-08-04 09:36:11 +03:00

368 lines
16 KiB
C#

using Content.Server.Chemistry.Components;
using Content.Server.Chemistry.Containers.EntitySystems;
using Content.Server.Labels;
using Content.Server.Popups;
using Content.Server.Storage.EntitySystems;
using Content.Shared.Administration.Logs;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Database;
using Content.Shared.FixedPoint;
using Content.Shared.Storage;
using JetBrains.Annotations;
using Robust.Server.Audio;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Content.Server.Chemistry.EntitySystems
{
/// <summary>
/// Contains all the server-side logic for ChemMasters.
/// <seealso cref="ChemMasterComponent"/>
/// </summary>
[UsedImplicitly]
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 StorageSystem _storageSystem = default!;
[Dependency] private readonly LabelSystem _labelSystem = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[ValidatePrototypeId<EntityPrototype>]
private const string PillPrototypeId = "Pill";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ChemMasterComponent, ComponentStartup>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, SolutionContainerChangedEvent>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, EntInsertedIntoContainerMessage>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, EntRemovedFromContainerMessage>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, BoundUIOpenedEvent>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterSetModeMessage>(OnSetModeMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterSetPillTypeMessage>(OnSetPillTypeMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterReagentAmountButtonMessage>(OnReagentButtonMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterCreatePillsMessage>(OnCreatePillsMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterOutputToBottleMessage>(OnOutputToBottleMessage);
}
private void SubscribeUpdateUiState<T>(Entity<ChemMasterComponent> ent, ref T ev)
{
UpdateUiState(ent);
}
public void UpdateUiState(Entity<ChemMasterComponent> ent, bool updateLabel = false) // WD EDIT
{
var (owner, chemMaster) = ent;
if (!_solutionContainerSystem.TryGetSolution(owner, SharedChemMaster.BufferSolutionName, out _, out var bufferSolution))
return;
var inputContainer = _itemSlotsSystem.GetItemOrNull(owner, SharedChemMaster.InputSlotName);
var outputContainer = _itemSlotsSystem.GetItemOrNull(owner, SharedChemMaster.OutputSlotName);
var bufferReagents = bufferSolution.Contents;
var bufferCurrentVolume = bufferSolution.Volume;
var state = new ChemMasterBoundUserInterfaceState(
chemMaster.Mode, BuildInputContainerInfo(inputContainer), BuildOutputContainerInfo(outputContainer),
bufferReagents, bufferCurrentVolume, chemMaster.PillType, chemMaster.PillDosageLimit, updateLabel);
_userInterfaceSystem.TrySetUiState(owner, ChemMasterUiKey.Key, state);
}
private void OnSetModeMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterSetModeMessage message)
{
// Ensure the mode is valid, either Transfer or Discard.
if (!Enum.IsDefined(typeof(ChemMasterMode), message.ChemMasterMode))
return;
chemMaster.Comp.Mode = message.ChemMasterMode;
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private void OnSetPillTypeMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterSetPillTypeMessage message)
{
// Ensure valid pill type. There are 20 pills selectable, 0-19.
if (message.PillType > SharedChemMaster.PillTypes - 1)
return;
chemMaster.Comp.PillType = message.PillType;
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private void OnReagentButtonMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterReagentAmountButtonMessage message)
{
// Ensure the amount corresponds to one of the reagent amount buttons.
if (!Enum.IsDefined(typeof(ChemMasterReagentAmount), message.Amount))
return;
switch (chemMaster.Comp.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(Entity<ChemMasterComponent> chemMaster, ReagentId id, FixedPoint2 amount, bool fromBuffer)
{
var container = _itemSlotsSystem.GetItemOrNull(chemMaster, SharedChemMaster.InputSlotName);
if (container is null ||
!_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSoln, out var containerSolution) ||
!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out _, out var bufferSolution))
{
return;
}
if (fromBuffer) // Buffer to container
{
amount = FixedPoint2.Min(amount, containerSolution.AvailableVolume);
amount = bufferSolution.RemoveReagent(id, amount, preserveOrder: true);
_solutionContainerSystem.TryAddReagent(containerSoln.Value, id, amount, out var _);
}
else // Container to buffer
{
amount = FixedPoint2.Min(amount, containerSolution.GetReagentQuantity(id));
_solutionContainerSystem.RemoveReagent(containerSoln.Value, id, amount);
bufferSolution.AddReagent(id, amount);
}
UpdateUiState(chemMaster, updateLabel: true);
}
private void DiscardReagents(Entity<ChemMasterComponent> chemMaster, ReagentId id, FixedPoint2 amount, bool fromBuffer)
{
if (fromBuffer)
{
if (_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out _, out var bufferSolution))
bufferSolution.RemoveReagent(id, amount, preserveOrder: true);
else
return;
}
else
{
var container = _itemSlotsSystem.GetItemOrNull(chemMaster, SharedChemMaster.InputSlotName);
if (container is not null &&
_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution, out _))
{
_solutionContainerSystem.RemoveReagent(containerSolution.Value, id, amount);
}
else
return;
}
UpdateUiState(chemMaster, updateLabel: fromBuffer);
}
private void OnCreatePillsMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterCreatePillsMessage message)
{
var user = message.Session.AttachedEntity;
var maybeContainer = _itemSlotsSystem.GetItemOrNull(chemMaster, SharedChemMaster.OutputSlotName);
if (maybeContainer is not { Valid: true } container
|| !TryComp(container, out StorageComponent? storage))
{
return; // output can't fit pills
}
// Ensure the number is valid.
if (message.Number == 0 || !_storageSystem.HasSpace((container, storage)))
return;
// Ensure the amount is valid.
if (message.Dosage == 0 || message.Dosage > chemMaster.Comp.PillDosageLimit)
return;
// Ensure label length is within the character limit.
if (message.Label.Length > SharedChemMaster.LabelMaxLength)
return;
var needed = message.Dosage * message.Number;
if (!WithdrawFromBuffer(chemMaster, needed, user, out var withdrawal))
return;
_labelSystem.Label(container, message.Label);
for (var i = 0; i < message.Number; i++)
{
var item = Spawn(PillPrototypeId, Transform(container).Coordinates);
_storageSystem.Insert(container, item, out _, user: user, storage);
_labelSystem.Label(item, message.Label);
var itemSolution = _solutionContainerSystem.EnsureSolutionEntity(item, SharedChemMaster.PillSolutionName, message.Dosage, null, out _);
_solutionContainerSystem.TryAddSolution(itemSolution, withdrawal.SplitSolution(message.Dosage));
var pill = EnsureComp<PillComponent>(item);
pill.PillType = chemMaster.Comp.PillType;
Dirty(item, pill);
if (user.HasValue)
{
// Log pill creation by a user
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(user.Value):user} printed {ToPrettyString(item):pill} {SolutionContainerSystem.ToPrettyString(itemSolution.Comp.Solution)}");
}
else
{
// Log pill creation by magic? This should never happen... right?
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"Unknown printed {ToPrettyString(item):pill} {SolutionContainerSystem.ToPrettyString(itemSolution.Comp.Solution)}");
}
}
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private void OnOutputToBottleMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterOutputToBottleMessage message)
{
var user = message.Session.AttachedEntity;
var maybeContainer = _itemSlotsSystem.GetItemOrNull(chemMaster, SharedChemMaster.OutputSlotName);
if (maybeContainer is not { Valid: true } container
|| !_solutionContainerSystem.TryGetSolution(container, SharedChemMaster.BottleSolutionName, out var soln, out var solution))
{
return; // output can't fit reagents
}
// Ensure the amount is valid.
if (message.Dosage == 0 || message.Dosage > solution.AvailableVolume)
return;
// Ensure label length is within the character limit.
if (message.Label.Length > SharedChemMaster.LabelMaxLength)
return;
if (!WithdrawFromBuffer(chemMaster, message.Dosage, user, out var withdrawal))
return;
_labelSystem.Label(container, message.Label);
_solutionContainerSystem.TryAddSolution(soln.Value, withdrawal);
if (user.HasValue)
{
// Log bottle creation by a user
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(user.Value):user} bottled {ToPrettyString(container):bottle} {SolutionContainerSystem.ToPrettyString(solution)}");
}
else
{
// Log bottle creation by magic? This should never happen... right?
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"Unknown bottled {ToPrettyString(container):bottle} {SolutionContainerSystem.ToPrettyString(solution)}");
}
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private bool WithdrawFromBuffer(
Entity<ChemMasterComponent> chemMaster,
FixedPoint2 neededVolume, EntityUid? user,
[NotNullWhen(returnValue: true)] out Solution? outputSolution)
{
outputSolution = null;
if (!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out _, out var solution))
{
return false;
}
if (solution.Volume == 0)
{
if (user.HasValue)
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-empty-text"), user.Value);
return false;
}
// ReSharper disable once InvertIf
if (neededVolume > solution.Volume)
{
if (user.HasValue)
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-low-text"), user.Value);
return false;
}
outputSolution = solution.SplitSolution(neededVolume);
return true;
}
private void ClickSound(Entity<ChemMasterComponent> chemMaster)
{
_audioSystem.PlayPvs(chemMaster.Comp.ClickSound, chemMaster, 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 _, 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 _, out var solution))
{
return BuildContainerInfo(name, solution);
}
}
if (!TryComp(container, out StorageComponent? storage))
return null;
var pills = storage.Container.ContainedEntities.Select((Func<EntityUid, (string, FixedPoint2 quantity)>) (pill =>
{
_solutionContainerSystem.TryGetSolution(pill, SharedChemMaster.PillSolutionName, out _, out var solution);
var quantity = solution?.Volume ?? FixedPoint2.Zero;
return (Name(pill), quantity);
})).ToList();
return new ContainerInfo(name, _storageSystem.GetCumulativeItemAreas((container.Value, storage)), storage.Grid.GetArea())
{
Entities = pills
};
}
private static ContainerInfo BuildContainerInfo(string name, Solution solution)
{
return new ContainerInfo(name, solution.Volume, solution.MaxVolume)
{
Reagents = solution.Contents
};
}
}
}