Files
wwdpublic/Content.Server/HealthExaminable/HealthExaminableSystem.cs
Angelo Fallaria 22ff27c098 New Trait: Self-Aware (#680)
# Description

**Self-Aware** is a 2-point Mental trait that allows you to precisely
examine your Brute/Burn damage as if using a health analyzer, and
estimate the level of your toxin/airloss damage.

Inspired by the SS13 trait of the same name.

## Media

<details><summary>Expand</summary>

**Trait entry**


![image](https://github.com/user-attachments/assets/8faae16f-7dd5-42cd-8332-c65a0c610429)

**No damage**


![image](https://github.com/user-attachments/assets/72bfa0f4-57fa-4b8d-b974-a604b4030ea6)

**Damaged**


![image](https://github.com/user-attachments/assets/ec6318b4-41dc-4d3b-9aec-8f02333363e7)
</details>

# Changelog

🆑 Skubman
add: Add the Self-Aware trait, a 2-point trait that allows you to
examine your Brute/Burn damage numbers like a health analyzer, and
estimate your toxin/airloss damage.

---------

Signed-off-by: Angelo Fallaria <ba.fallaria@gmail.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
2024-08-06 09:05:58 +01:00

223 lines
7.2 KiB
C#

using Content.Server.Traits.Assorted;
using Content.Shared.Damage;
using Content.Shared.Examine;
using Content.Shared.FixedPoint;
using Content.Shared.IdentityManagement;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Verbs;
using Robust.Shared.Utility;
using System.Linq;
namespace Content.Server.HealthExaminable;
public sealed class HealthExaminableSystem : EntitySystem
{
[Dependency] private readonly ExamineSystemShared _examineSystem = default!;
[Dependency] private readonly MobThresholdSystem _threshold = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HealthExaminableComponent, GetVerbsEvent<ExamineVerb>>(OnGetExamineVerbs);
}
private void OnGetExamineVerbs(EntityUid uid, HealthExaminableComponent component, GetVerbsEvent<ExamineVerb> args)
{
if (!TryComp<DamageableComponent>(uid, out var damage))
return;
var detailsRange = _examineSystem.IsInDetailsRange(args.User, uid);
var verb = new ExamineVerb()
{
Act = () =>
{
FormattedMessage markup;
if (uid == args.User
&& TryComp<SelfAwareComponent>(uid, out var selfAware))
markup = CreateMarkupSelfAware(uid, selfAware, component, damage);
else
markup = CreateMarkup(uid, component, damage);
_examineSystem.SendExamineTooltip(args.User, uid, markup, false, false);
},
Text = Loc.GetString("health-examinable-verb-text"),
Category = VerbCategory.Examine,
Disabled = !detailsRange,
Message = detailsRange ? null : Loc.GetString("health-examinable-verb-disabled"),
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/rejuvenate.svg.192dpi.png"))
};
args.Verbs.Add(verb);
}
private FormattedMessage CreateMarkup(EntityUid uid, HealthExaminableComponent component, DamageableComponent damage)
{
var msg = new FormattedMessage();
var first = true;
var adjustedThresholds = GetAdjustedThresholds(uid, component.Thresholds);
foreach (var type in component.ExaminableTypes)
{
if (!damage.Damage.DamageDict.TryGetValue(type, out var dmg))
continue;
if (dmg == FixedPoint2.Zero)
continue;
FixedPoint2 closest = FixedPoint2.Zero;
string chosenLocStr = string.Empty;
foreach (var threshold in adjustedThresholds)
{
var str = $"health-examinable-{component.LocPrefix}-{type}-{threshold}";
var tempLocStr = Loc.GetString($"health-examinable-{component.LocPrefix}-{type}-{threshold}", ("target", Identity.Entity(uid, EntityManager)));
// i.e., this string doesn't exist, because theres nothing for that threshold
if (tempLocStr == str)
continue;
if (dmg > threshold && threshold > closest)
{
chosenLocStr = tempLocStr;
closest = threshold;
}
}
if (closest == FixedPoint2.Zero)
continue;
if (!first)
{
msg.PushNewline();
}
else
{
first = false;
}
msg.AddMarkup(chosenLocStr);
}
if (msg.IsEmpty)
{
msg.AddMarkup(Loc.GetString($"health-examinable-{component.LocPrefix}-none"));
}
// Anything else want to add on to this?
RaiseLocalEvent(uid, new HealthBeingExaminedEvent(msg, false), true);
return msg;
}
private FormattedMessage CreateMarkupSelfAware(EntityUid target, SelfAwareComponent selfAware, HealthExaminableComponent component, DamageableComponent damage)
{
var msg = new FormattedMessage();
var first = true;
foreach (var type in selfAware.AnalyzableTypes)
{
if (!damage.Damage.DamageDict.TryGetValue(type, out var typeDmgUnrounded))
continue;
var typeDmg = (int) Math.Round(typeDmgUnrounded.Float(), 0);
if (typeDmg <= 0)
continue;
var damageString = Loc.GetString(
"health-examinable-selfaware-type-text",
("damageType", Loc.GetString($"health-examinable-selfaware-type-{type}")),
("amount", typeDmg)
);
if (!first)
msg.PushNewline();
else
first = false;
msg.AddMarkup(damageString);
}
var adjustedThresholds = GetAdjustedThresholds(target, selfAware.Thresholds);
foreach (var group in selfAware.DetectableGroups)
{
if (!damage.DamagePerGroup.TryGetValue(group, out var groupDmg)
|| groupDmg == FixedPoint2.Zero)
continue;
FixedPoint2 closest = FixedPoint2.Zero;
string chosenLocStr = string.Empty;
foreach (var threshold in adjustedThresholds)
{
var locName = $"health-examinable-selfaware-group-{group}-{threshold}";
var locStr = Loc.GetString(locName);
var locDoesNotExist = locStr == locName;
if (locDoesNotExist)
continue;
if (groupDmg > threshold && threshold > closest)
{
chosenLocStr = locStr;
closest = threshold;
}
}
if (closest == FixedPoint2.Zero)
continue;
if (!first)
msg.PushNewline();
else
first = false;
msg.AddMarkup(chosenLocStr);
}
if (msg.IsEmpty)
msg.AddMarkup(Loc.GetString($"health-examinable-selfaware-none"));
// Event listeners can know if the examination is Self-Aware.
RaiseLocalEvent(target, new HealthBeingExaminedEvent(msg, true), true);
return msg;
}
/// <summary>
/// Return thresholds as percentages of an entity's critical threshold.
/// </summary>
private List<FixedPoint2> GetAdjustedThresholds(EntityUid uid, List<FixedPoint2> thresholdPercentages)
{
FixedPoint2 critThreshold = 0;
if (TryComp<MobThresholdsComponent>(uid, out var threshold))
critThreshold = _threshold.GetThresholdForState(uid, Shared.Mobs.MobState.Critical, threshold);
// Fallback to 100 crit threshold if none found
if (critThreshold == 0)
critThreshold = 100;
return thresholdPercentages.Select(percentage => critThreshold * percentage).ToList();
}
}
/// <summary>
/// A class raised on an entity whose health is being examined
/// in order to add special text that is not handled by the
/// damage thresholds.
/// </summary>
public sealed class HealthBeingExaminedEvent
{
public FormattedMessage Message;
public bool IsSelfAware;
public HealthBeingExaminedEvent(FormattedMessage message, bool isSelfAware)
{
Message = message;
IsSelfAware = isSelfAware;
}
}