* A big hecking chemistry-related refactor. Changed SolutionContainerCaps. It now describes "stock" behavior for interacting with solutions that is pre-implemented by SolutionContainerComponent. As such things like syringes do not check it anymore (on themselves) to see "can we remove reagent from ourselves". That's assumed by it... being a syringe. SolutionContainerCaps now has different flags more accurately describing possible reagent interaction behaviors. ISolutionInteractionsComponent is the interface that describes the common behaviors like "what happens when injected with a syringe". This is implemented by SolutionContainerComponent but could be implemented by other classes. One notable example that drove me to making this interface was the /vg/station circuit imprinter which splits reagent poured in into its two reservoir beakers. Having this interface allows us to do this "proxying" behavior hack-free. (the hacks in /vg/ code were somewhat dirty...). PourableComponent has been replaced SolutionTransferComponent. It now describes both give-and-take behavior for the common reagent containers. This is in line with /vg/'s /obj/item/weapon/reagent_containers architecture. "Taking" in this context is ONLY from reagent tanks like fuel tanks. Oh, should I mention that fuel tanks and such have a proper component now? They do. Because of this behavioral change, reagent tanks DO NOT have Pourable anymore. Removing from reagent tanks is now in the hands of the item used on them. Welders and fire extinguishers now have code for removing from them. This sounds bad at first but remember that all have quite unique behavior related to this: Welders cause explosions if lit and can ONLY be fueled at fuel tanks. Extinguishers can be filled at any tank, etc... The code for this is also simpler due to ISolutionInteractionsComponent now so... IAfterInteract now works like IInteractUsing with the Priority levels and "return true to block further handlers" behavior. This was necessary to make extinguishers prioritize taking from tanks over spraying. Explicitly coded interactions like welders refueling also means they refuse instantly to full now, which they didn't before. And it plays the sound. Etc... Probably more stuff I'm forgetting. * Review improvements.
165 lines
5.5 KiB
C#
165 lines
5.5 KiB
C#
#nullable enable
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Content.Server.Utility;
|
|
using Content.Shared.GameObjects.Components.Body;
|
|
using Content.Shared.GameObjects.Components.Body.Mechanism;
|
|
using Content.Shared.GameObjects.Components.Body.Part;
|
|
using Content.Shared.GameObjects.Components.Body.Surgery;
|
|
using Content.Shared.Interfaces;
|
|
using Content.Shared.Interfaces.GameObjects.Components;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Server.GameObjects.Components.UserInterface;
|
|
using Robust.Server.Interfaces.GameObjects;
|
|
using Robust.Server.Interfaces.Player;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.Localization;
|
|
using Robust.Shared.Utility;
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
namespace Content.Server.GameObjects.Components.Body
|
|
{
|
|
[RegisterComponent]
|
|
[ComponentReference(typeof(SharedMechanismComponent))]
|
|
[ComponentReference(typeof(IMechanism))]
|
|
public class MechanismComponent : SharedMechanismComponent, IAfterInteract
|
|
{
|
|
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SurgeryUIKey.Key);
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
if (UserInterface != null)
|
|
{
|
|
UserInterface.OnReceiveMessage += OnUIMessage;
|
|
}
|
|
}
|
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
|
{
|
|
if (eventArgs.Target == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CloseAllSurgeryUIs();
|
|
OptionsCache.Clear();
|
|
PerformerCache = null;
|
|
BodyCache = null;
|
|
|
|
if (eventArgs.Target.TryGetComponent(out IBody? body))
|
|
{
|
|
SendBodyPartListToUser(eventArgs, body);
|
|
}
|
|
else if (eventArgs.Target.TryGetComponent<IBodyPart>(out var part))
|
|
{
|
|
DebugTools.AssertNotNull(part);
|
|
|
|
if (!part.TryAddMechanism(this))
|
|
{
|
|
eventArgs.Target.PopupMessage(eventArgs.User, Loc.GetString("You can't fit it in!"));
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void SendBodyPartListToUser(AfterInteractEventArgs eventArgs, IBody body)
|
|
{
|
|
// Create dictionary to send to client (text to be shown : data sent back if selected)
|
|
var toSend = new Dictionary<string, int>();
|
|
|
|
foreach (var (key, value) in body.Parts)
|
|
{
|
|
// For each limb in the target, add it to our cache if it is a valid option.
|
|
if (value.CanAddMechanism(this))
|
|
{
|
|
OptionsCache.Add(IdHash, value);
|
|
toSend.Add(key + ": " + value.Name, IdHash++);
|
|
}
|
|
}
|
|
|
|
if (OptionsCache.Count > 0 &&
|
|
eventArgs.User.TryGetComponent(out IActorComponent? actor))
|
|
{
|
|
OpenSurgeryUI(actor.playerSession);
|
|
UpdateSurgeryUIBodyPartRequest(actor.playerSession, toSend);
|
|
PerformerCache = eventArgs.User;
|
|
BodyCache = body;
|
|
}
|
|
else // If surgery cannot be performed, show message saying so.
|
|
{
|
|
eventArgs.Target.PopupMessage(eventArgs.User,
|
|
Loc.GetString("You see no way to install the {0}.", Owner.Name));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called after the client chooses from a list of possible BodyParts that can be operated on.
|
|
/// </summary>
|
|
private void HandleReceiveBodyPart(int key)
|
|
{
|
|
if (PerformerCache == null ||
|
|
!PerformerCache.TryGetComponent(out IActorComponent? actor))
|
|
{
|
|
return;
|
|
}
|
|
|
|
CloseSurgeryUI(actor.playerSession);
|
|
|
|
if (BodyCache == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// TODO: sanity checks to see whether user is in range, user is still able-bodied, target is still the same, etc etc
|
|
if (!OptionsCache.TryGetValue(key, out var targetObject))
|
|
{
|
|
BodyCache.Owner.PopupMessage(PerformerCache,
|
|
Loc.GetString("You see no useful way to use the {0} anymore.", Owner.Name));
|
|
return;
|
|
}
|
|
|
|
var target = (IBodyPart) targetObject;
|
|
var message = target.TryAddMechanism(this)
|
|
? Loc.GetString("You jam {0:theName} inside {1:them}.", Owner, PerformerCache)
|
|
: Loc.GetString("You can't fit it in!");
|
|
|
|
BodyCache.Owner.PopupMessage(PerformerCache, message);
|
|
|
|
// TODO: {1:theName}
|
|
}
|
|
|
|
private void OpenSurgeryUI(IPlayerSession session)
|
|
{
|
|
UserInterface?.Open(session);
|
|
}
|
|
|
|
private void UpdateSurgeryUIBodyPartRequest(IPlayerSession session, Dictionary<string, int> options)
|
|
{
|
|
UserInterface?.SendMessage(new RequestBodyPartSurgeryUIMessage(options), session);
|
|
}
|
|
|
|
private void CloseSurgeryUI(IPlayerSession session)
|
|
{
|
|
UserInterface?.Close(session);
|
|
}
|
|
|
|
private void CloseAllSurgeryUIs()
|
|
{
|
|
UserInterface?.CloseAll();
|
|
}
|
|
|
|
private void OnUIMessage(ServerBoundUserInterfaceMessage message)
|
|
{
|
|
switch (message.Message)
|
|
{
|
|
case ReceiveBodyPartSurgeryUIMessage msg:
|
|
HandleReceiveBodyPart(msg.SelectedOptionId);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|