Predict RepairableSystem (#38886)

* commit

* Update SharedRepairableSystem.cs

* compo

* final touches.

* Update RepairableComponent.cs

* Update RepairableSystem.cs

* Update Content.Shared/Repairable/RepairableSystem.cs

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
Kyle Tyo
2025-07-09 22:39:34 -04:00
committed by GitHub
parent 59633f6dc5
commit 326712faca
7 changed files with 142 additions and 144 deletions

View File

@@ -2,7 +2,6 @@ using Content.Server.Administration.Logs;
using Content.Server.Fluids.EntitySystems; using Content.Server.Fluids.EntitySystems;
using Content.Server.Ghost; using Content.Server.Ghost;
using Content.Server.Popups; using Content.Server.Popups;
using Content.Server.Repairable;
using Content.Server.Stack; using Content.Server.Stack;
using Content.Server.Wires; using Content.Server.Wires;
using Content.Shared.Body.Systems; using Content.Shared.Body.Systems;
@@ -19,6 +18,7 @@ using Content.Shared.Materials;
using Content.Shared.Mind; using Content.Shared.Mind;
using Content.Shared.Nutrition.EntitySystems; using Content.Shared.Nutrition.EntitySystems;
using Content.Shared.Power; using Content.Shared.Power;
using Content.Shared.Repairable;
using Content.Shared.Stacks; using Content.Shared.Stacks;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.Player; using Robust.Shared.Player;

View File

@@ -1,42 +0,0 @@
using Content.Shared.Damage;
using Content.Shared.Tools;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Repairable
{
[RegisterComponent]
public sealed partial class RepairableComponent : Component
{
/// <summary>
/// All the damage to change information is stored in this <see cref="DamageSpecifier"/>.
/// </summary>
/// <remarks>
/// If this data-field is specified, it will change damage by this amount instead of setting all damage to 0.
/// in order to heal/repair the damage values have to be negative.
/// </remarks>
[DataField]
public DamageSpecifier? Damage;
[DataField]
public int FuelCost = 5;
[DataField]
public ProtoId<ToolQualityPrototype> QualityNeeded = "Welding";
[DataField]
public int DoAfterDelay = 1;
/// <summary>
/// A multiplier that will be applied to the above if an entity is repairing themselves.
/// </summary>
[DataField]
public float SelfRepairPenalty = 3f;
/// <summary>
/// Whether or not an entity is allowed to repair itself.
/// </summary>
[DataField]
public bool AllowSelfRepair = true;
}
}

View File

@@ -1,87 +0,0 @@
using Content.Server.Administration.Logs;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Repairable;
using SharedToolSystem = Content.Shared.Tools.Systems.SharedToolSystem;
namespace Content.Server.Repairable
{
public sealed class RepairableSystem : SharedRepairableSystem
{
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly IAdminLogManager _adminLogger= default!;
public override void Initialize()
{
SubscribeLocalEvent<RepairableComponent, InteractUsingEvent>(Repair);
SubscribeLocalEvent<RepairableComponent, RepairFinishedEvent>(OnRepairFinished);
}
private void OnRepairFinished(EntityUid uid, RepairableComponent component, RepairFinishedEvent args)
{
if (args.Cancelled)
return;
if (!TryComp(uid, out DamageableComponent? damageable) || damageable.TotalDamage == 0)
return;
if (component.Damage != null)
{
var damageChanged = _damageableSystem.TryChangeDamage(uid, component.Damage, true, false, origin: args.User);
_adminLogger.Add(LogType.Healed, $"{ToPrettyString(args.User):user} repaired {ToPrettyString(uid):target} by {damageChanged?.GetTotal()}");
}
else
{
// Repair all damage
_damageableSystem.SetAllDamage(uid, damageable, 0);
_adminLogger.Add(LogType.Healed, $"{ToPrettyString(args.User):user} repaired {ToPrettyString(uid):target} back to full health");
}
var str = Loc.GetString("comp-repairable-repair",
("target", uid),
("tool", args.Used!));
_popup.PopupEntity(str, uid, args.User);
var ev = new RepairedEvent((uid, component), args.User);
RaiseLocalEvent(uid, ref ev);
}
public async void Repair(EntityUid uid, RepairableComponent component, InteractUsingEvent args)
{
if (args.Handled)
return;
// Only try repair the target if it is damaged
if (!TryComp<DamageableComponent>(uid, out var damageable) || damageable.TotalDamage == 0)
return;
float delay = component.DoAfterDelay;
// Add a penalty to how long it takes if the user is repairing itself
if (args.User == args.Target)
{
if (!component.AllowSelfRepair)
return;
delay *= component.SelfRepairPenalty;
}
// Run the repairing doafter
args.Handled = _toolSystem.UseTool(args.Used, args.User, uid, delay, component.QualityNeeded, new RepairFinishedEvent(), component.FuelCost);
}
}
/// <summary>
/// Event raised on an entity when its successfully repaired.
/// </summary>
/// <param name="Ent"></param>
/// <param name="User"></param>
[ByRefEvent]
public readonly record struct RepairedEvent(Entity<RepairableComponent> Ent, EntityUid User);
}

View File

@@ -3,7 +3,6 @@ using Content.Server.DeviceNetwork.Systems;
using Content.Server.NPC.HTN; using Content.Server.NPC.HTN;
using Content.Server.NPC.HTN.PrimitiveTasks.Operators.Combat.Ranged; using Content.Server.NPC.HTN.PrimitiveTasks.Operators.Combat.Ranged;
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Server.Repairable;
using Content.Server.TurretController; using Content.Server.TurretController;
using Content.Shared.Access; using Content.Shared.Access;
using Content.Shared.Destructible; using Content.Shared.Destructible;
@@ -11,6 +10,7 @@ using Content.Shared.DeviceNetwork;
using Content.Shared.DeviceNetwork.Components; using Content.Shared.DeviceNetwork.Components;
using Content.Shared.DeviceNetwork.Events; using Content.Shared.DeviceNetwork.Events;
using Content.Shared.Power; using Content.Shared.Power;
using Content.Shared.Repairable;
using Content.Shared.Turrets; using Content.Shared.Turrets;
using Content.Shared.Weapons.Ranged.Components; using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events; using Content.Shared.Weapons.Ranged.Events;

View File

@@ -0,0 +1,53 @@
using Content.Shared.Damage;
using Content.Shared.Tools;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.Repairable;
/// <summary>
/// Use this component to mark a device as repairable.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class RepairableComponent : Component
{
/// <summary>
/// All the damage to change information is stored in this <see cref="DamageSpecifier"/>.
/// </summary>
/// <remarks>
/// If this data-field is specified, it will change damage by this amount instead of setting all damage to 0.
/// in order to heal/repair the damage values have to be negative.
/// </remarks>
[DataField, AutoNetworkedField]
public DamageSpecifier? Damage;
/// <summary>
/// Cost of fuel used to repair this device.
/// </summary>
[DataField, AutoNetworkedField]
public int FuelCost = 5;
/// <summary>
/// Tool quality necessary to repair this device.
/// </summary>
[DataField, AutoNetworkedField]
public ProtoId<ToolQualityPrototype> QualityNeeded = "Welding";
/// <summary>
/// The base tool use delay (seconds). This will be modified by the tool's quality
/// </summary>
[DataField, AutoNetworkedField]
public int DoAfterDelay = 1;
/// <summary>
/// A multiplier that will be applied to the above if an entity is repairing themselves.
/// </summary>
[DataField, AutoNetworkedField]
public float SelfRepairPenalty = 3f;
/// <summary>
/// Whether an entity is allowed to repair itself.
/// </summary>
[DataField, AutoNetworkedField]
public bool AllowSelfRepair = true;
}

View File

@@ -0,0 +1,87 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Tools.Systems;
using Robust.Shared.Serialization;
namespace Content.Shared.Repairable;
public sealed partial class RepairableSystem : EntitySystem
{
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
public override void Initialize()
{
SubscribeLocalEvent<RepairableComponent, InteractUsingEvent>(Repair);
SubscribeLocalEvent<RepairableComponent, RepairFinishedEvent>(OnRepairFinished);
}
private void OnRepairFinished(Entity<RepairableComponent> ent, ref RepairFinishedEvent args)
{
if (args.Cancelled)
return;
if (!TryComp(ent.Owner, out DamageableComponent? damageable) || damageable.TotalDamage == 0)
return;
if (ent.Comp.Damage != null)
{
var damageChanged = _damageableSystem.TryChangeDamage(ent.Owner, ent.Comp.Damage, true, false, origin: args.User);
_adminLogger.Add(LogType.Healed, $"{ToPrettyString(args.User):user} repaired {ToPrettyString(ent.Owner):target} by {damageChanged?.GetTotal()}");
}
else
{
// Repair all damage
_damageableSystem.SetAllDamage(ent.Owner, damageable, 0);
_adminLogger.Add(LogType.Healed, $"{ToPrettyString(args.User):user} repaired {ToPrettyString(ent.Owner):target} back to full health");
}
var str = Loc.GetString("comp-repairable-repair", ("target", ent.Owner), ("tool", args.Used!));
_popup.PopupClient(str, ent.Owner, args.User);
var ev = new RepairedEvent(ent, args.User);
RaiseLocalEvent(ent.Owner, ref ev);
}
private void Repair(Entity<RepairableComponent> ent, ref InteractUsingEvent args)
{
if (args.Handled)
return;
// Only try repair the target if it is damaged
if (!TryComp<DamageableComponent>(ent.Owner, out var damageable) || damageable.TotalDamage == 0)
return;
float delay = ent.Comp.DoAfterDelay;
// Add a penalty to how long it takes if the user is repairing itself
if (args.User == args.Target)
{
if (!ent.Comp.AllowSelfRepair)
return;
delay *= ent.Comp.SelfRepairPenalty;
}
// Run the repairing doafter
args.Handled = _toolSystem.UseTool(args.Used, args.User, ent.Owner, delay, ent.Comp.QualityNeeded, new RepairFinishedEvent(), ent.Comp.FuelCost);
}
}
/// <summary>
/// Event raised on an entity when its successfully repaired.
/// </summary>
/// <param name="Ent"></param>
/// <param name="User"></param>
[ByRefEvent]
public readonly record struct RepairedEvent(Entity<RepairableComponent> Ent, EntityUid User);
[Serializable, NetSerializable]
public sealed partial class RepairFinishedEvent : SimpleDoAfterEvent;

View File

@@ -1,13 +0,0 @@
using Content.Shared.DoAfter;
using Robust.Shared.Serialization;
namespace Content.Shared.Repairable;
public abstract partial class SharedRepairableSystem : EntitySystem
{
[Serializable, NetSerializable]
protected sealed partial class RepairFinishedEvent : SimpleDoAfterEvent
{
}
}