2025-07-14 00:35:47 -04:00
|
|
|
|
using Content.Server.Damage.Components;
|
2022-01-29 08:21:38 +03:00
|
|
|
|
using Content.Server.Destructible;
|
|
|
|
|
|
using Content.Shared.Damage;
|
|
|
|
|
|
using Content.Shared.Examine;
|
|
|
|
|
|
using Content.Shared.Rounding;
|
|
|
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Damage.Systems;
|
|
|
|
|
|
|
2022-02-16 00:23:23 -07:00
|
|
|
|
public sealed class ExaminableDamageSystem : EntitySystem
|
2022-01-29 08:21:38 +03:00
|
|
|
|
{
|
2025-07-14 00:35:47 -04:00
|
|
|
|
[Dependency] private readonly DestructibleSystem _destructible = default!;
|
2022-01-29 08:21:38 +03:00
|
|
|
|
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
|
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
SubscribeLocalEvent<ExaminableDamageComponent, ExaminedEvent>(OnExamine);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-14 00:35:47 -04:00
|
|
|
|
private void OnExamine(Entity<ExaminableDamageComponent> ent, ref ExaminedEvent args)
|
2022-01-29 08:21:38 +03:00
|
|
|
|
{
|
2025-09-09 18:17:56 +02:00
|
|
|
|
if (!_prototype.Resolve(ent.Comp.Messages, out var proto) || proto.Values.Count == 0)
|
2022-01-29 08:21:38 +03:00
|
|
|
|
return;
|
|
|
|
|
|
|
2025-07-14 00:35:47 -04:00
|
|
|
|
var percent = GetDamagePercent(ent);
|
|
|
|
|
|
var level = ContentHelpers.RoundToNearestLevels(percent, 1, proto.Values.Count - 1);
|
|
|
|
|
|
var msg = Loc.GetString(proto.Values[level]);
|
|
|
|
|
|
args.PushMarkup(msg, -99);
|
2022-01-29 08:21:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-14 00:35:47 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns a value between 0 and 1 representing how damaged the entity is,
|
|
|
|
|
|
/// where 0 is undamaged and 1 is fully damaged.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>How damaged the entity is from 0 to 1</returns>
|
|
|
|
|
|
private float GetDamagePercent(Entity<ExaminableDamageComponent> ent)
|
2022-01-29 08:21:38 +03:00
|
|
|
|
{
|
2025-07-14 00:35:47 -04:00
|
|
|
|
if (!TryComp<DamageableComponent>(ent, out var damageable))
|
2022-01-29 08:21:38 +03:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
2025-07-14 00:35:47 -04:00
|
|
|
|
var damage = damageable.TotalDamage;
|
|
|
|
|
|
var damageThreshold = _destructible.DestroyedAt(ent);
|
2022-01-29 08:21:38 +03:00
|
|
|
|
|
2025-07-14 00:35:47 -04:00
|
|
|
|
if (damageThreshold == 0)
|
2022-01-29 08:21:38 +03:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
2025-07-14 00:35:47 -04:00
|
|
|
|
return (damage / damageThreshold).Float();
|
2022-01-29 08:21:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|