Skip to content

Commit

Permalink
Added some experimental lag fixes to surgery. Might have crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
gluesniffler authored and TokenStyle committed Nov 3, 2024
1 parent 018ed00 commit de95d85
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 74 deletions.
75 changes: 26 additions & 49 deletions Content.Client/Backmen/Surgery/SurgeryBui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
using Content.Shared.Backmen.Surgery;
using Content.Shared.Body.Components;
using Content.Shared.Body.Part;
using Content.Shared.Rotation;
using Content.Shared.Standing;
using Content.Client.Hands.Systems;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using Robust.Shared.Timing;
using Robust.Client.Timing;
using static Robust.Client.UserInterface.Control;

namespace Content.Client.Backmen.Surgery;
Expand All @@ -19,34 +23,42 @@ public sealed class SurgeryBui : BoundUserInterface
[Dependency] private readonly IEntityManager _entities = default!;
[Dependency] private readonly IPlayerManager _player = default!;

[Dependency] private readonly IClientGameTiming _gameTiming = default!;
[Dependency] private readonly IGameTiming _timing = default!;

private readonly SurgerySystem _system;

private readonly HandsSystem _hands;
[ViewVariables]
private SurgeryWindow? _window;

private EntityUid? _part;
private bool _isBody = false;
private (EntityUid Ent, EntProtoId Proto)? _surgery;
private readonly List<EntProtoId> _previousSurgeries = new();

private DateTime _lastRefresh = DateTime.UtcNow;
private (string handName, EntityUid item) _throttling = ("", new EntityUid());
public SurgeryBui(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
_system = _entities.System<SurgerySystem>();
_hands = _entities.System<HandsSystem>();

_system.OnStep += RefreshUI;
_hands.OnPlayerItemAdded += OnPlayerItemAdded;
}

protected override void Open()
private void OnPlayerItemAdded(string handName, EntityUid item)
{
//Logger.Debug("Attempting to open");
_system.OnRefresh += () =>
{
UpdateDisabledPanel();
RefreshUI();
};
if (_throttling.handName.Equals(handName)
&& _throttling.item.Equals(item)
&& DateTime.UtcNow - _lastRefresh < TimeSpan.FromSeconds(0.2)
|| !_timing.IsFirstTimePredicted
|| _window == null
|| !_window.IsOpen)
return;

if (State is SurgeryBuiState s)
Update(s);
_throttling = (handName, item);
_lastRefresh = DateTime.UtcNow;
RefreshUI();
}

protected override void UpdateState(BoundUserInterfaceState state)
Expand All @@ -62,7 +74,7 @@ protected override void Dispose(bool disposing)
if (disposing)
_window?.Dispose();

_system.OnRefresh -= RefreshUI;
_system.OnStep -= RefreshUI;
}

private void Update(SurgeryBuiState state)
Expand All @@ -71,7 +83,7 @@ private void Update(SurgeryBuiState state)
if (!_entities.TryGetComponent<SurgeryTargetComponent>(_player.LocalEntity, out var surgeryTargetComp)
|| !surgeryTargetComp.CanOperate)
return;
//Logger.Debug("Passed check");

if (_window == null)
{
_window = new SurgeryWindow();
Expand Down Expand Up @@ -126,7 +138,6 @@ State is not SurgeryBuiState s ||
_window.Surgeries.DisposeAllChildren();
_window.Steps.DisposeAllChildren();
_window.Parts.DisposeAllChildren();

View(ViewType.Parts);

var oldSurgery = _surgery;
Expand Down Expand Up @@ -194,16 +205,8 @@ int GetScore(BodyPartType? partType)


if (!_window.IsOpen)
{
//Logger.Debug("Attempting to open");
_window.OpenCentered();
}
else
{
//Logger.Debug("Attempting to refresh");
RefreshUI();
UpdateDisabledPanel();
}
}

private void AddStep(EntProtoId stepId, NetEntity netPart, EntProtoId surgeryId)
Expand Down Expand Up @@ -308,7 +311,6 @@ private void OnPartPressed(NetEntity netPart, List<EntProtoId> surgeryIds)
private void RefreshUI()
{
if (_window == null
|| !_timing.IsFirstTimePredicted
|| !_window.IsOpen
|| _part == null
|| !_entities.HasComponent<SurgeryComponent>(_surgery?.Ent)
Expand All @@ -317,7 +319,7 @@ private void RefreshUI()
{
return;
}

Logger.Debug($"Running RefreshUI on {Owner}");
var next = _system.GetNextStep(Owner, _part.Value, _surgery.Value.Ent);
var i = 0;
foreach (var child in _window.Steps.Children)
Expand Down Expand Up @@ -355,9 +357,6 @@ private void RefreshUI()
else
{
stepButton.Button.Modulate = Color.White;
// GOD THIS NEEDS A REWRITE SO BADLY, IT UPDATES ON EVERY SINGLE TICK
// THEN RUNS CANPERFORMSTEP WHICH CALLS A SHITLOAD OF EVENTS
// DID THEY NOT FUCKING PLAYTEST THIS???
if (_player.LocalEntity is { } player
&& status == StepStatus.Next
&& !_system.CanPerformStep(player, Owner, _part.Value, stepButton.Step, false, out var popup, out var reason, out _))
Expand Down Expand Up @@ -387,28 +386,6 @@ private void RefreshUI()
stepButton.Set(stepName, texture);
i++;
}

UpdateDisabledPanel();
}

private void UpdateDisabledPanel()
{
if (_window == null)
return;

if (_system.IsLyingDown(Owner))
{
_window.DisabledPanel.Visible = false;
_window.DisabledPanel.MouseFilter = MouseFilterMode.Ignore;
return;
}

_window.DisabledPanel.Visible = true;

var text = new FormattedMessage();
text.AddMarkup($"[color=red][font size=16]{Loc.GetString("surgery-ui-window-steps-error-laying")}[/font][/color]");
_window.DisabledLabel.SetMessage(text);
_window.DisabledPanel.MouseFilter = MouseFilterMode.Stop;
}

private void View(ViewType type)
Expand Down
14 changes: 11 additions & 3 deletions Content.Client/Backmen/Surgery/SurgerySystem.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using Content.Shared.Backmen.Surgery;
using Content.Shared.Medical.Surgery;

namespace Content.Client.Backmen.Surgery;

public sealed class SurgerySystem : SharedSurgerySystem
{
public event Action? OnRefresh;
public event Action? OnStep;

public override void Update(float frameTime)
public override void Initialize()
{
OnRefresh?.Invoke();
base.Initialize();

SubscribeNetworkEvent<SurgeryUiRefreshEvent>(OnRefresh);
}

private void OnRefresh(SurgeryUiRefreshEvent ev)
{
OnStep?.Invoke();
}
}
7 changes: 0 additions & 7 deletions Content.Client/Backmen/Surgery/SurgeryWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,4 @@
<BoxContainer Name="Steps" Access="Public" Orientation="Vertical" Visible="False" />
</ScrollContainer>
</BoxContainer>
<PanelContainer Name="DisabledPanel" Access="Public" HorizontalExpand="True"
VerticalExpand="True" Visible="False">
<PanelContainer.PanelOverride>
<graphics:StyleBoxFlat BackgroundColor="#000000BF" />
</PanelContainer.PanelOverride>
<RichTextLabel Name="DisabledLabel" Access="Public" HorizontalAlignment="Center" />
</PanelContainer>
</controls:SurgeryWindow>
13 changes: 13 additions & 0 deletions Content.Client/Body/Systems/BodySystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
using Content.Shared.Body.Systems;
using Content.Shared.Body.Part;
using Robust.Client.GameObjects;

namespace Content.Client.Body.Systems;

public sealed class BodySystem : SharedBodySystem
{
protected override void UpdateAppearance(EntityUid uid, BodyPartAppearanceComponent component)
{
if (TryComp(uid, out SpriteComponent? sprite))
{
if (component.Color != null)
{
//TODO a few things need to be adjusted before this is ready to be used - also need to find a way to update the player sprite
//sprite.Color = component.Color.Value;
}
}
}
}
19 changes: 18 additions & 1 deletion Content.Server/Backmen/Surgery/SurgerySystem.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using Content.Shared.Bed.Sleep;
using Content.Shared.Body.Components;
using Content.Server.Body.Systems;
using Content.Shared.Body.Organ;
using Content.Shared.Body.Part;
using Content.Server.Chat.Systems;
using Content.Server.Popups;
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;
using Content.Shared.Interaction;
using Content.Shared.Inventory;
using Content.Shared.Medical.Surgery.Steps;
using Content.Shared.Medical.Surgery.Conditions;
using Content.Shared.Medical.Surgery.Effects.Step;
using Content.Server.Atmos.Rotting;
Expand All @@ -14,6 +18,7 @@
using Content.Shared.Prototypes;
using Robust.Server.GameObjects;
using Robust.Shared.Configuration;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using System.Linq;
Expand Down Expand Up @@ -75,6 +80,18 @@ protected override void RefreshUI(EntityUid body)
}
//Logger.Debug($"Setting UI state with {surgeries}, {body} and {SurgeryUIKey.Key}");
_ui.SetUiState(body, SurgeryUIKey.Key, new SurgeryBuiState(surgeries));
/*
Reason we do this is because when applying a BUI State, it rolls back the state on the entity temporarily,
which just so happens to occur right as we're checking for step completion, so we end up with the UI
not updating at all until you change tools or reopen the window.
*/

var actors = _ui.GetActors(body, SurgeryUIKey.Key).ToArray();
if (actors.Length == 0)
return;

var filter = Filter.Entities(actors);
RaiseNetworkEvent(new Shared.Medical.Surgery.SurgeryUiRefreshEvent(GetNetEntity(body)), filter);
}

private void SetDamage(EntityUid body,
Expand Down Expand Up @@ -174,7 +191,7 @@ private void OnStepAffixPartComplete(Entity<SurgeryStepAffixPartEffectComponent>
var ev = new BodyPartEnableChangedEvent(true);
RaiseLocalEvent(targetPart.Id, ref ev);
// This is basically an equalizer, severing a part will badly damage it.
// and affixing it will heal it a bit if its not too badly damaged.
// and affixing it will heal it a bit if it's not too badly damaged.
_body.TryChangeIntegrity(targetPart, targetPart.Component.Integrity - BodyPartComponent.IntegrityAffixPart, false,
_body.GetTargetBodyPart(targetPart.Component.PartType, targetPart.Component.Symmetry), out _);
}
Expand Down
5 changes: 5 additions & 0 deletions Content.Server/Body/Systems/BodySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ public override HashSet<EntityUid> GibPart(

return gibs;
}

protected override void UpdateAppearance(EntityUid uid, BodyPartAppearanceComponent component)
{
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,22 @@ public void TryChangeIntegrity(Entity<BodyPartComponent> partEnt,
out bool severed)
{
severed = false;

if (!_timing.IsFirstTimePredicted || !_queryTargeting.HasComp(partEnt.Comp.Body))
return;

var partIdSlot = GetParentPartAndSlotOrNull(partEnt)?.Slot;
var originalIntegrity = partEnt.Comp.Integrity;
partEnt.Comp.Integrity = Math.Min(BodyPartComponent.MaxIntegrity, partEnt.Comp.Integrity - integrity);

// This will also prevent the torso from being removed.
if (canSever
&& !HasComp<BodyPartReattachedComponent>(partEnt)
&& !partEnt.Comp.Enabled
&& partEnt.Comp.Integrity <= 0
&& partIdSlot is not null)
severed = true;

// This will also prevent the torso from being removed.
if (partEnt.Comp.Enabled
&& partEnt.Comp.Integrity <= BodyPartComponent.CritIntegrity)
{
Expand Down
4 changes: 1 addition & 3 deletions Content.Shared/Backmen/Surgery/SharedSurgerySystem.Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ private void OnToolStep(Entity<SurgeryStepComponent> ent, ref SurgeryStepEvent a
var compType = reg.Component.GetType();
if (HasComp(args.Part, compType))
continue;

AddComp(args.Part, _compFactory.GetComponent(compType));
}
}
Expand Down Expand Up @@ -409,7 +408,7 @@ private void OnSurgeryTargetStepChosen(Entity<SurgeryTargetComponent> ent, ref S
var user = args.Actor;
if (GetEntity(args.Entity) is not { Valid: true } body ||
GetEntity(args.Part) is not { Valid: true } targetPart ||
!IsSurgeryValid(body, targetPart, args.Surgery, args.Step, out var surgery, out var part, out var step))
!IsSurgeryValid(body, targetPart, args.Surgery, args.Step, user, out var surgery, out var part, out var step))
{
return;
}
Expand Down Expand Up @@ -565,7 +564,6 @@ public bool IsStepComplete(EntityUid body, EntityUid part, EntProtoId step, Enti

var ev = new SurgeryStepCompleteCheckEvent(body, part, surgery);
RaiseLocalEvent(stepEnt, ref ev);

return !ev.Cancelled;
}

Expand Down
Loading

0 comments on commit de95d85

Please sign in to comment.