Delta-v/Content.Shared/Repairable/RepairableSystem.cs

160 lines
6.5 KiB
C#

using Content.Shared.Administration.Logs;
using Content.Shared.Damage.Components;
using Content.Shared.Damage.Systems;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Tools.Components; // Monolith - Nanite Applicators
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, RepairDoAfterEvent>(OnRepairDoAfter);
}
private void OnRepairDoAfter(Entity<RepairableComponent> ent, ref RepairDoAfterEvent args)
{
if (args.Cancelled)
return;
if (!TryComp(ent.Owner, out DamageableComponent? damageable))
return;
var totalDamage = _damageableSystem.GetTotalDamage((ent.Owner, damageable));
if (totalDamage == 0)
return;
if (ent.Comp.DamageValue != null)
RepairSomeDamage((ent, damageable), ent.Comp.DamageValue.Value, args.User);
else if (ent.Comp.Damage != null)
RepairSomeDamage((ent, damageable), ent.Comp.Damage, args.User);
else
RepairAllDamage((ent, damageable), args.User);
totalDamage = _damageableSystem.GetTotalDamage((ent.Owner, damageable));
args.Repeat = ent.Comp.AutoDoAfter && totalDamage > 0;
args.Args.Event.Repeat = args.Repeat;
args.Handled = true;
if (!args.Repeat)
{
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);
}
}
/// <summary>
/// Repairs some damage of a entity.
/// The healed amount will be evenly distributed among all damage types the entity has.
/// If one of the damage types of the entity is too low. it will heal that completly and distribute the excess healing among the other damage types
/// </summary>
/// <param name="ent">entity to be repaired</param>
/// <param name="damageAmount">how much damage to repair (value have to be negative to repair)</param>
/// <param name="user">who is doing the repair</param>
private void RepairSomeDamage(Entity<DamageableComponent?> ent, float damageAmount, EntityUid user)
{
var damageChanged = _damageableSystem.HealEvenly(ent.Owner, damageAmount, origin: user);
_adminLogger.Add(LogType.Healed, $"{ToPrettyString(user):user} repaired {ToPrettyString(ent.Owner):target} by {damageChanged.GetTotal()}");
}
/// <summary>
/// Repairs some damage of a entity
/// </summary>
/// <param name="ent">entity to be repaired</param>
/// <param name="damageAmount">how much damage to repair (values have to be negative to repair)</param>
/// <param name="user">who is doing the repair</param>
private void RepairSomeDamage(Entity<DamageableComponent?> ent, Damage.DamageSpecifier damageAmount, EntityUid user)
{
var damageChanged = _damageableSystem.ChangeDamage(ent.Owner, damageAmount, true, false, origin: user);
_adminLogger.Add(LogType.Healed, $"{ToPrettyString(user):user} repaired {ToPrettyString(ent.Owner):target} by {damageChanged.GetTotal()}");
}
/// <summary>
/// Repairs all damage of a entity
/// </summary>
/// <param name="ent">entity to be repaired</param>
/// <param name="user">who is doing the repair</param>
private void RepairAllDamage(Entity<DamageableComponent?> ent, EntityUid user)
{
_damageableSystem.ClearAllDamage(ent);
_adminLogger.Add(LogType.Healed, $"{ToPrettyString(user):user} repaired {ToPrettyString(ent.Owner):target} back to full health");
}
private void Repair(Entity<RepairableComponent> ent, ref InteractUsingEvent args)
{
if (args.Handled)
return;
// Only try repair the target if it is damaged
if (_damageableSystem.GetTotalDamage(ent.Owner) == 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;
}
// BEGIN DeltaV - Scale Repair Time with Damage
// If its a self-repair, ignore it since they've already been penalized.
if (args.User != args.Target && TryComp<DamageableComponent>(args.Target, out var damageComp))
{
var totalDamage = damageComp.Damage.GetTotal();
// TODO: Scale with the destructible threshold if DestructibleSystem ever gets more prediction added.
// For now, just scale up the delay per 100 damage, or reduce the delay if its less.
delay *= Math.Clamp((float)totalDamage / 100.0f, 0.5f, 3.0f);
}
// END DeltaV
// BEGIN Monolith - Nanite Applicators
if (!TryComp<ToolComponent>(args.Used, out var tool))
return;
// Run the repairing doafter
foreach (var quality in ent.Comp.QualityNeeded)
{
if (_toolSystem.HasQuality(args.Used, quality, tool))
args.Handled = _toolSystem.UseTool(args.Used, args.User, ent.Owner, delay, quality, new RepairDoAfterEvent(), ent.Comp.FuelCost);
}
// END Monolith
}
}
/// <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);
/// <summary>
/// Do after event started when you try to fix a entity with RepairableComponent.
/// This doafter is repeated if the entity has <see cref="AutoDoAfter"> set to true and not all damage was fixed yet.
/// </summary>
[Serializable, NetSerializable]
public sealed partial class RepairDoAfterEvent : SimpleDoAfterEvent;