Skip to content

Commit

Permalink
Remove wireless surgery (#2785)
Browse files Browse the repository at this point in the history
* Remove wireless surgery

* Change surgery window title to Surgery

* Remove todo

* Check only tools with a window open
  • Loading branch information
DrSmugleaf authored Dec 20, 2020
1 parent a3fdcd3 commit 0172613
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SurgeryWindow : SS14Window

public SurgeryWindow()
{
Title = Loc.GetString("Select surgery target...");
Title = Loc.GetString("Surgery");
RectClipContent = true;

var vSplitContainer = new VBoxContainer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Content.Server.GameObjects.Components.Body.Surgery.Messages
{
public class SurgeryWindowCloseMessage
{
public SurgeryWindowCloseMessage(SurgeryToolComponent tool)
{
Tool = tool;
}

public SurgeryToolComponent Tool { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Robust.Shared.GameObjects;

namespace Content.Server.GameObjects.Components.Body.Surgery.Messages
{
public class SurgeryWindowOpenMessage : ComponentMessage
{
public SurgeryWindowOpenMessage(SurgeryToolComponent tool)
{
Tool = tool;
}

public SurgeryToolComponent Tool { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Content.Server.GameObjects.Components.Body.Surgery.Messages;
using Content.Server.Utility;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Body;
Expand Down Expand Up @@ -37,18 +38,18 @@ public class SurgeryToolComponent : Component, ISurgeon, IAfterInteract

private float _baseOperateTime;

private IBody? _bodyCache;

private ISurgeon.MechanismRequestCallback? _callbackCache;

private int _idHash;

private IEntity? _performerCache;

private SurgeryType _surgeryType;

[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(SurgeryUIKey.Key);

public IBody? BodyCache { get; private set; }

public IEntity? PerformerCache { get; private set; }

async Task IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{
if (eventArgs.Target == null)
Expand All @@ -62,11 +63,6 @@ async Task IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
}

CloseAllSurgeryUIs();
_optionsCache.Clear();

_performerCache = null;
_bodyCache = null;
_callbackCache = null;

// Attempt surgery on a body by sending a list of operable parts for the client to choose from
if (eventArgs.Target.TryGetComponent(out IBody? body))
Expand All @@ -88,8 +84,8 @@ async Task IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{
OpenSurgeryUI(actor.playerSession);
UpdateSurgeryUIBodyPartRequest(actor.playerSession, toSend);
_performerCache = eventArgs.User; // Also, cache the data.
_bodyCache = body;
PerformerCache = eventArgs.User; // Also, cache the data.
BodyCache = body;
}
else // If surgery cannot be performed, show message saying so.
{
Expand All @@ -99,7 +95,7 @@ async Task IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
else if (eventArgs.Target.TryGetComponent<IBodyPart>(out var part))
{
// Attempt surgery on a DroppedBodyPart - there's only one possible target so no need for selection UI
_performerCache = eventArgs.User;
PerformerCache = eventArgs.User;

// If surgery can be performed...
if (!part.SurgeryCheck(_surgeryType))
Expand Down Expand Up @@ -132,10 +128,10 @@ public void RequestMechanism(IEnumerable<IMechanism> options, ISurgeon.Mechanism
toSend.Add(mechanism.Name, _idHash++);
}

if (_optionsCache.Count > 0 && _performerCache != null)
if (_optionsCache.Count > 0 && PerformerCache != null)
{
OpenSurgeryUI(_performerCache.GetComponent<BasicActorComponent>().playerSession);
UpdateSurgeryUIMechanismRequest(_performerCache.GetComponent<BasicActorComponent>().playerSession,
OpenSurgeryUI(PerformerCache.GetComponent<BasicActorComponent>().playerSession);
UpdateSurgeryUIMechanismRequest(PerformerCache.GetComponent<BasicActorComponent>().playerSession,
toSend);
_callbackCache = callback;
}
Expand All @@ -146,6 +142,14 @@ public void RequestMechanism(IEnumerable<IMechanism> options, ISurgeon.Mechanism
}
}

public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);

serializer.DataField(ref _surgeryType, "surgeryType", SurgeryType.Incision);
serializer.DataField(ref _baseOperateTime, "baseOperateTime", 5);
}

public override void Initialize()
{
base.Initialize();
Expand All @@ -156,10 +160,14 @@ public override void Initialize()
}
}

// TODO BODY add checks to close UI if user walks too far away from tool or target.
private void OpenSurgeryUI(IPlayerSession session)
{
UserInterface?.Open(session);

var message = new SurgeryWindowOpenMessage(this);

SendMessage(message);
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
}

private void UpdateSurgeryUIBodyPartRequest(IPlayerSession session, Dictionary<string, int> options)
Expand All @@ -172,14 +180,25 @@ private void UpdateSurgeryUIMechanismRequest(IPlayerSession session, Dictionary<
UserInterface?.SendMessage(new RequestMechanismSurgeryUIMessage(options), session);
}

private void ClearUIData()
{
_optionsCache.Clear();

PerformerCache = null;
BodyCache = null;
_callbackCache = null;
}

private void CloseSurgeryUI(IPlayerSession session)
{
UserInterface?.Close(session);
ClearUIData();
}

private void CloseAllSurgeryUIs()
public void CloseAllSurgeryUIs()
{
UserInterface?.CloseAll();
ClearUIData();
}

private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage message)
Expand All @@ -201,16 +220,16 @@ private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage mes
/// </summary>
private void HandleReceiveBodyPart(int key)
{
if (_performerCache == null ||
!_performerCache.TryGetComponent(out IActorComponent? actor))
if (PerformerCache == null ||
!PerformerCache.TryGetComponent(out IActorComponent? actor))
{
return;
}

CloseSurgeryUI(actor.playerSession);
// 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 == null)
BodyCache == null)
{
NotUsefulAnymorePopup();
return;
Expand All @@ -219,7 +238,7 @@ private void HandleReceiveBodyPart(int key)
var target = (IBodyPart) targetObject!;

// TODO BODY Reconsider
if (!target.AttemptSurgery(_surgeryType, _bodyCache, this, _performerCache))
if (!target.AttemptSurgery(_surgeryType, BodyCache, this, PerformerCache))
{
NotUsefulAnymorePopup();
}
Expand All @@ -233,8 +252,8 @@ private void HandleReceiveMechanism(int key)
{
// 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) ||
_performerCache == null ||
!_performerCache.TryGetComponent(out IActorComponent? actor))
PerformerCache == null ||
!PerformerCache.TryGetComponent(out IActorComponent? actor))
{
NotUsefulAnymorePopup();
return;
Expand All @@ -243,27 +262,19 @@ private void HandleReceiveMechanism(int key)
var target = targetObject as MechanismComponent;

CloseSurgeryUI(actor.playerSession);
_callbackCache?.Invoke(target, _bodyCache, this, _performerCache);
_callbackCache?.Invoke(target, BodyCache, this, PerformerCache);
}

private void NotUsefulPopup()
{
_bodyCache?.Owner.PopupMessage(_performerCache,
BodyCache?.Owner.PopupMessage(PerformerCache,
Loc.GetString("You see no useful way to use {0:theName}.", Owner));
}

private void NotUsefulAnymorePopup()
{
_bodyCache?.Owner.PopupMessage(_performerCache,
BodyCache?.Owner.PopupMessage(PerformerCache,
Loc.GetString("You see no useful way to use {0:theName} anymore.", Owner));
}

public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);

serializer.DataField(ref _surgeryType, "surgeryType", SurgeryType.Incision);
serializer.DataField(ref _baseOperateTime, "baseOperateTime", 5);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Body.Surgery;
using Content.Server.GameObjects.Components.Body.Surgery.Messages;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.GameTicking;
using Content.Shared.Utility;
using JetBrains.Annotations;
using Robust.Shared.GameObjects.Systems;

namespace Content.Server.GameObjects.EntitySystems.Body.Surgery
{
[UsedImplicitly]
public class SurgeryToolSystem : EntitySystem, IResettingEntitySystem
{
private readonly HashSet<SurgeryToolComponent> _openSurgeryUIs = new();

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<SurgeryWindowOpenMessage>(OnSurgeryWindowOpen);
SubscribeLocalEvent<SurgeryWindowCloseMessage>(OnSurgeryWindowClose);
}

public void Reset()
{
_openSurgeryUIs.Clear();
}

private void OnSurgeryWindowOpen(SurgeryWindowOpenMessage ev)
{
_openSurgeryUIs.Add(ev.Tool);
}

private void OnSurgeryWindowClose(SurgeryWindowCloseMessage ev)
{
_openSurgeryUIs.Remove(ev.Tool);
}

public override void Update(float frameTime)
{
base.Update(frameTime);

foreach (var tool in _openSurgeryUIs)
{
if (tool.PerformerCache == null)
{
continue;
}

if (tool.BodyCache == null)
{
continue;
}

if (!ActionBlockerSystem.CanInteract(tool.PerformerCache) ||
!tool.PerformerCache.InRangeUnobstructed(tool.BodyCache))
{
tool.CloseAllSurgeryUIs();
}
}
}
}
}

0 comments on commit 0172613

Please sign in to comment.