Files
OldThink/Content.Server/Chemistry/EntitySystems/ChemMasterSystem.cs

369 lines
16 KiB
C#
Raw Normal View History

ECS verbs and update context menu (#4594) * Functioning ECS verbs Currently only ID card console works. * Changed verb types and allow ID card insertions * Verb GUI sorting and verb networking * More networking, and shared components * Clientside verbs work now. * Verb enums changed to bitmask flags * Verb Categories redo * Fix range check * GasTank Verb * Remove unnecessary bodypart verb * Buckle Verb * buckle & unbuckle verbs * Updated range checks * Item cabinet verbs * Add range user override * construction verb * Chemistry machine verbs * Climb Verb * Generalise pulled entity verbs * ViewVariables Verb * rejuvenate, delete, sentient, control verbs * Outfit verb * inrangeunoccluded and tubedirection verbs * attach-to verbs * remove unused verbs and move VV * Rename DebugVerbSystem * Ghost role and pointing verbs * Remove global verbs * Allow verbs to raise events * Changing categories and simplifying debug verbs * Add rotate and flip verbs * fix rejuvenate test * redo context menu * new Add Gas debug verb * Add Set Temperature debug verb * Uncuff verb * Disposal unit verbs * Add pickup verb * lock/unlock verb * Remove verb type, add specific verb events * rename verb messages -> events * Context menu displays verbs by interaction type * Updated context menu HandleMove previously, checked if entities moved 1 tile from click location. Now checks if entities moved out of view. Now you can actually right-click interact with yourself while walking! * Misc Verb menu GUI changes * Fix non-human/ghost verbs * Update types and categories * Allow non-ghost/human to open context menu * configuration verb * tagger verb * Morgue Verbs * Medical Scanner Verbs * Fix solution refactor merge issues * Fix context menu in-view check * Remove prepare GUI * Redo verb restrictions * Fix context menu UI * Disposal Verbs * Spill verb * Light verb * Hand Held light verb * power cell verbs * storage verbs and adding names to insert/eject * Pulling verb * Close context menu on verb execution * Strip verb * AmmoBox verb * fix pull verb * gun barrel verbs revolver verb energy weapon verbs Bolt action verb * Magazine gun barrel verbs * Add charger verbs * PDA verbs * Transfer amount verb * Add reagent verb * make alt-click use ECS verbs * Delete old verb files * Magboot verb * finalising tweaks * context menu visibility changes * code cleanup * Update AdminAddReagentUI.cs * Remove HasFlag * Consistent verb keys * Remove Linq, add comment * Fix in-inventory check * Update GUI text alignment and padding * Added close-menu option * Changed some "interaction" verbs to "activation" * Remove verb keys, use sorted sets * fix master merge * update some verb text * Undo Changes Remove some new verbs that can be added later undid some .ftl bugfixes, can and should be done separately * fix merge * Undo file rename * fix merge * Misc Cleanup * remove contraction * Fix keybinding issue * fix comment * merge fix * fix merge * fix merge * fix merge * fix merge * fix open-close verbs * adjust uncuff verb * fix merge and undo the renaming of SharedPullableComponent to PullableComponent. I'm tired of all of those merge conflicts
2021-10-05 14:29:03 +11:00
using Content.Server.Chemistry.Components;
using Content.Server.Chemistry.Containers.EntitySystems;
using Content.Server.Labels;
2022-09-06 07:06:47 +02:00
using Content.Server.Popups;
using Content.Server.Storage.EntitySystems;
2022-12-15 12:33:27 -06:00
using Content.Shared.Administration.Logs;
2022-09-06 07:06:47 +02:00
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Reagent;
2022-09-06 07:06:47 +02:00
using Content.Shared.Containers.ItemSlots;
2022-12-15 12:33:27 -06:00
using Content.Shared.Database;
2022-09-06 07:06:47 +02:00
using Content.Shared.FixedPoint;
2023-09-11 21:20:46 +10:00
using Content.Shared.Storage;
using JetBrains.Annotations;
using Robust.Server.Audio;
2022-09-06 07:06:47 +02:00
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
{
2022-09-06 07:06:47 +02:00
/// <summary>
/// Contains all the server-side logic for ChemMasters.
/// <seealso cref="ChemMasterComponent"/>
/// </summary>
[UsedImplicitly]
2022-09-06 07:06:47 +02:00
public sealed class ChemMasterSystem : EntitySystem
{
2022-09-06 07:06:47 +02:00
[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!;
2022-12-15 12:33:27 -06:00
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
2022-10-04 02:57:32 +02:00
[ValidatePrototypeId<EntityPrototype>]
private const string PillPrototypeId = "Pill";
2022-09-06 07:06:47 +02:00
public override void Initialize()
{
base.Initialize();
2022-09-06 07:06:47 +02:00
SubscribeLocalEvent<ChemMasterComponent, ComponentStartup>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, SolutionContainerChangedEvent>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, EntInsertedIntoContainerMessage>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, EntRemovedFromContainerMessage>(SubscribeUpdateUiState);
SubscribeLocalEvent<ChemMasterComponent, BoundUIOpenedEvent>(SubscribeUpdateUiState);
2022-09-06 07:06:47 +02:00
SubscribeLocalEvent<ChemMasterComponent, ChemMasterSetModeMessage>(OnSetModeMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterSetPillTypeMessage>(OnSetPillTypeMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterReagentAmountButtonMessage>(OnReagentButtonMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterCreatePillsMessage>(OnCreatePillsMessage);
SubscribeLocalEvent<ChemMasterComponent, ChemMasterOutputToBottleMessage>(OnOutputToBottleMessage);
2022-09-06 07:06:47 +02:00
}
private void SubscribeUpdateUiState<T>(Entity<ChemMasterComponent> ent, ref T ev)
2022-09-06 07:06:47 +02:00
{
UpdateUiState(ent);
}
private void UpdateUiState(Entity<ChemMasterComponent> ent, bool updateLabel = false)
{
var (owner, chemMaster) = ent;
if (!_solutionContainerSystem.TryGetSolution(owner, SharedChemMaster.BufferSolutionName, out _, out var bufferSolution))
2022-09-06 07:06:47 +02:00
return;
var inputContainer = _itemSlotsSystem.GetItemOrNull(owner, SharedChemMaster.InputSlotName);
var outputContainer = _itemSlotsSystem.GetItemOrNull(owner, SharedChemMaster.OutputSlotName);
2022-09-06 07:06:47 +02:00
var bufferReagents = bufferSolution.Contents;
2023-01-12 16:41:40 +13:00
var bufferCurrentVolume = bufferSolution.Volume;
2022-09-06 07:06:47 +02:00
var state = new ChemMasterBoundUserInterfaceState(
2022-10-04 02:57:32 +02:00
chemMaster.Mode, BuildInputContainerInfo(inputContainer), BuildOutputContainerInfo(outputContainer),
bufferReagents, bufferCurrentVolume, chemMaster.PillType, chemMaster.PillDosageLimit, updateLabel);
_userInterfaceSystem.TrySetUiState(owner, ChemMasterUiKey.Key, state);
2022-09-06 07:06:47 +02:00
}
private void OnSetModeMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterSetModeMessage message)
2022-09-06 07:06:47 +02:00
{
// Ensure the mode is valid, either Transfer or Discard.
if (!Enum.IsDefined(typeof(ChemMasterMode), message.ChemMasterMode))
return;
chemMaster.Comp.Mode = message.ChemMasterMode;
2022-09-06 07:06:47 +02:00
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private void OnSetPillTypeMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterSetPillTypeMessage message)
2022-09-06 07:06:47 +02:00
{
// Ensure valid pill type. There are 20 pills selectable, 0-19.
if (message.PillType > SharedChemMaster.PillTypes - 1)
return;
chemMaster.Comp.PillType = message.PillType;
2022-09-06 07:06:47 +02:00
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private void OnReagentButtonMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterReagentAmountButtonMessage message)
2022-09-06 07:06:47 +02:00
{
// Ensure the amount corresponds to one of the reagent amount buttons.
if (!Enum.IsDefined(typeof(ChemMasterReagentAmount), message.Amount))
return;
switch (chemMaster.Comp.Mode)
2022-09-06 07:06:47 +02:00
{
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)
2022-09-06 07:06:47 +02:00
{
var container = _itemSlotsSystem.GetItemOrNull(chemMaster, SharedChemMaster.InputSlotName);
2022-09-06 07:06:47 +02:00
if (container is null ||
!_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSoln, out var containerSolution) ||
!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out _, out var bufferSolution))
2022-09-06 07:06:47 +02:00
{
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 _);
2022-09-06 07:06:47 +02:00
}
else // Container to buffer
{
amount = FixedPoint2.Min(amount, containerSolution.GetReagentQuantity(id));
_solutionContainerSystem.RemoveReagent(containerSoln.Value, id, amount);
bufferSolution.AddReagent(id, amount);
2022-09-06 07:06:47 +02:00
}
UpdateUiState(chemMaster, updateLabel: true);
}
private void DiscardReagents(Entity<ChemMasterComponent> chemMaster, ReagentId id, FixedPoint2 amount, bool fromBuffer)
2022-09-06 07:06:47 +02:00
{
if (fromBuffer)
{
if (_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out _, out var bufferSolution))
bufferSolution.RemoveReagent(id, amount, preserveOrder: true);
2022-09-06 07:06:47 +02:00
else
return;
}
else
{
var container = _itemSlotsSystem.GetItemOrNull(chemMaster, SharedChemMaster.InputSlotName);
2022-09-06 07:06:47 +02:00
if (container is not null &&
_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution, out _))
2022-09-06 07:06:47 +02:00
{
_solutionContainerSystem.RemoveReagent(containerSolution.Value, id, amount);
2022-09-06 07:06:47 +02:00
}
else
return;
}
UpdateUiState(chemMaster, updateLabel: fromBuffer);
}
private void OnCreatePillsMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterCreatePillsMessage message)
2022-09-06 07:06:47 +02:00
{
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
}
2022-10-04 02:57:32 +02:00
// Ensure the number is valid.
if (message.Number == 0 || !_storageSystem.HasSpace((container, storage)))
return;
2022-10-04 02:57:32 +02:00
2022-09-06 07:06:47 +02:00
// 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))
2022-09-06 07:06:47 +02:00
return;
2022-10-04 02:57:32 +02:00
_labelSystem.Label(container, message.Label);
2022-10-04 02:57:32 +02:00
for (var i = 0; i < message.Number; i++)
{
var item = Spawn(PillPrototypeId, Transform(container).Coordinates);
2023-09-12 22:34:04 +10:00
_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));
2022-10-04 02:57:32 +02:00
var pill = EnsureComp<PillComponent>(item);
pill.PillType = chemMaster.Comp.PillType;
Dirty(item, pill);
2022-12-15 12:33:27 -06:00
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)}");
2022-12-15 12:33:27 -06:00
}
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)}");
2022-12-15 12:33:27 -06:00
}
}
2022-09-06 07:06:47 +02:00
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private void OnOutputToBottleMessage(Entity<ChemMasterComponent> chemMaster, ref ChemMasterOutputToBottleMessage message)
2022-09-06 07:06:47 +02:00
{
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
}
2022-09-06 07:06:47 +02:00
// Ensure the amount is valid.
if (message.Dosage == 0 || message.Dosage > solution.AvailableVolume)
2022-09-06 07:06:47 +02:00
return;
2022-10-04 02:57:32 +02:00
// 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);
2022-09-06 07:06:47 +02:00
2022-12-15 12:33:27 -06:00
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)}");
}
2022-09-06 07:06:47 +02:00
UpdateUiState(chemMaster);
ClickSound(chemMaster);
}
private bool WithdrawFromBuffer(
Entity<ChemMasterComponent> chemMaster,
FixedPoint2 neededVolume, EntityUid? user,
[NotNullWhen(returnValue: true)] out Solution? outputSolution)
2022-09-06 07:06:47 +02:00
{
outputSolution = null;
2022-10-04 02:57:32 +02:00
if (!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out _, out var solution))
{
return false;
}
2022-09-06 07:06:47 +02:00
2023-01-12 16:41:40 +13:00
if (solution.Volume == 0)
2022-09-06 07:06:47 +02:00
{
if (user.HasValue)
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-empty-text"), user.Value);
return false;
2022-09-06 07:06:47 +02:00
}
// ReSharper disable once InvertIf
2023-01-12 16:41:40 +13:00
if (neededVolume > solution.Volume)
2022-09-06 07:06:47 +02:00
{
if (user.HasValue)
_popupSystem.PopupCursor(Loc.GetString("chem-master-window-buffer-low-text"), user.Value);
return false;
2022-09-06 07:06:47 +02:00
}
outputSolution = solution.SplitSolution(neededVolume);
return true;
}
private void ClickSound(Entity<ChemMasterComponent> chemMaster)
{
_audioSystem.PlayPvs(chemMaster.Comp.ClickSound, chemMaster, AudioParams.Default.WithVolume(-2f));
}
2022-10-04 02:57:32 +02:00
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))
2022-09-06 07:06:47 +02:00
{
return null;
}
2022-09-06 07:06:47 +02:00
return BuildContainerInfo(Name(container.Value), solution);
}
2022-09-06 07:06:47 +02:00
private ContainerInfo? BuildOutputContainerInfo(EntityUid? container)
{
if (container is not { Valid: true })
return null;
2022-09-06 07:06:47 +02:00
var name = Name(container.Value);
{
if (_solutionContainerSystem.TryGetSolution(
container.Value, SharedChemMaster.BottleSolutionName, out _, out var solution))
2022-09-06 07:06:47 +02:00
{
return BuildContainerInfo(name, solution);
2022-09-06 07:06:47 +02:00
}
}
2023-10-26 18:46:22 +11:00
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);
2023-01-12 16:41:40 +13:00
var quantity = solution?.Volume ?? FixedPoint2.Zero;
return (Name(pill), quantity);
2023-01-12 16:41:40 +13:00
})).ToList();
return new ContainerInfo(name, _storageSystem.GetCumulativeItemAreas((container.Value, storage)), storage.Grid.GetArea())
{
Entities = pills
};
2022-09-06 07:06:47 +02:00
}
private static ContainerInfo BuildContainerInfo(string name, Solution solution)
2022-09-06 07:06:47 +02:00
{
return new ContainerInfo(name, solution.Volume, solution.MaxVolume)
{
Reagents = solution.Contents
};
}
}
}