Fixing MobState issues (#13465)

Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
Fixes https://github.com/space-wizards/space-station-14/issues/13461
This commit is contained in:
Jezithyr
2023-01-14 01:32:57 -08:00
committed by GitHub
parent cbb37e434b
commit f42bbb226a
4 changed files with 36 additions and 32 deletions

View File

@@ -16,6 +16,12 @@ public abstract class AlertsSystem : EntitySystem
: null;
}
public short GetSeverityRange(AlertType alertType)
{
var minSeverity = _typeToAlert[alertType].MinSeverity;
return (short)MathF.Min(minSeverity,_typeToAlert[alertType].MaxSeverity - minSeverity);
}
public short GetMaxSeverity(AlertType alertType)
{
return _typeToAlert[alertType].MaxSeverity;

View File

@@ -24,6 +24,7 @@ public partial class MobStateSystem
SubscribeLocalEvent<MobStateComponent, BeforeGettingStrippedEvent>(OnGettingStripped);
SubscribeLocalEvent<MobStateComponent, ChangeDirectionAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, UseAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, AttackAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, InteractionAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, ThrowAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, SpeakAttemptEvent>(CheckAct);

View File

@@ -5,9 +5,6 @@ using Content.Shared.Damage;
using Content.Shared.FixedPoint;
using Content.Shared.Mobs.Components;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Mobs.Systems;
public sealed class MobThresholdSystem : EntitySystem
@@ -17,8 +14,8 @@ public sealed class MobThresholdSystem : EntitySystem
public override void Initialize()
{
SubscribeLocalEvent<MobThresholdsComponent, MapInitEvent>(MobThresholdMapInit);
SubscribeLocalEvent<MobThresholdsComponent, ComponentShutdown>(MobThresholdShutdown);
SubscribeLocalEvent<MobThresholdsComponent, ComponentStartup>(MobThresholdStartup);
SubscribeLocalEvent<MobThresholdsComponent, DamageChangedEvent>(OnDamaged);
SubscribeLocalEvent<MobThresholdsComponent, ComponentGetState>(OnGetComponentState);
SubscribeLocalEvent<MobThresholdsComponent, ComponentHandleState>(OnHandleComponentState);
@@ -246,6 +243,10 @@ public sealed class MobThresholdSystem : EntitySystem
return;
CheckThresholds(target, mobState, threshold, damageable);
var ev = new MobThresholdChecked(target, mobState, threshold, damageable);
RaiseLocalEvent(target, ref ev, true);
UpdateAlerts(target, mobState.CurrentState, threshold, damageable);
}
#endregion
@@ -255,29 +256,24 @@ public sealed class MobThresholdSystem : EntitySystem
private void CheckThresholds(EntityUid target, MobStateComponent mobStateComponent,
MobThresholdsComponent thresholdsComponent, DamageableComponent damageableComponent)
{
foreach (var (threshold, mobState) in thresholdsComponent.Thresholds)
foreach (var (threshold, mobState) in thresholdsComponent.Thresholds.Reverse())
{
if (damageableComponent.TotalDamage < threshold)
continue;
TriggerThreshold(target, thresholdsComponent.CurrentThresholdState, mobState, mobStateComponent,
thresholdsComponent);
TriggerThreshold(target, mobState, mobStateComponent, thresholdsComponent);
break;
}
var ev = new MobThresholdChecked(target, mobStateComponent, thresholdsComponent, damageableComponent);
RaiseLocalEvent(target, ref ev, true);
UpdateAlerts(target, mobStateComponent.CurrentState, thresholdsComponent, damageableComponent);
}
private void TriggerThreshold(
EntityUid target,
MobState oldState,
MobState newState,
MobStateComponent? mobState = null,
MobThresholdsComponent? thresholds = null)
{
if (oldState == newState ||
!Resolve(target, ref mobState, ref thresholds))
if (!Resolve(target, ref mobState, ref thresholds) ||
mobState.CurrentState == newState)
{
return;
}
@@ -305,10 +301,12 @@ public sealed class MobThresholdSystem : EntitySystem
var severity = _alerts.GetMinSeverity(AlertType.HumanHealth);
if (TryGetIncapPercentage(target, damageable.TotalDamage, out var percentage))
{
severity = (short) MathF.Floor(percentage.Value.Float() *
_alerts.GetMaxSeverity(AlertType.HumanHealth));
}
severity = (short) MathF.Floor(percentage.Value.Float() *
_alerts.GetSeverityRange(AlertType.HumanHealth));
severity += _alerts.GetMinSeverity(AlertType.HumanHealth);
}
_alerts.ShowAlert(target, AlertType.HumanHealth, severity);
break;
}
@@ -328,10 +326,14 @@ public sealed class MobThresholdSystem : EntitySystem
}
}
private void OnDamaged(EntityUid target, MobThresholdsComponent mobThresholdsComponent, DamageChangedEvent args)
private void OnDamaged(EntityUid target, MobThresholdsComponent thresholds, DamageChangedEvent args)
{
var mobStateComp = EnsureComp<MobStateComponent>(target);
CheckThresholds(target, mobStateComp, mobThresholdsComponent, args.Damageable);
if (!TryComp<MobStateComponent>(target, out var mobState))
return;
CheckThresholds(target, mobState, thresholds, args.Damageable);
var ev = new MobThresholdChecked(target, mobState, thresholds, args.Damageable);
RaiseLocalEvent(target, ref ev, true);
UpdateAlerts(target, mobState.CurrentState, thresholds, args.Damageable);
}
private void OnHandleComponentState(EntityUid target, MobThresholdsComponent component,
@@ -350,19 +352,14 @@ public sealed class MobThresholdSystem : EntitySystem
new Dictionary<FixedPoint2, MobState>(component.Thresholds));
}
private void MobThresholdMapInit(EntityUid target, MobThresholdsComponent component, MapInitEvent args)
private void MobThresholdStartup(EntityUid target, MobThresholdsComponent thresholds, ComponentStartup args)
{
// TODO remove when body sim is implemented
EnsureComp<MobStateComponent>(target);
EnsureComp<DamageableComponent>(target);
if (!component.Thresholds.TryFirstOrNull(out var newState))
if (!TryComp<MobStateComponent>(target, out var mobState) || !TryComp<DamageableComponent>(target, out var damageable))
return;
component.CurrentThresholdState = newState.Value.Value;
TriggerThreshold(target, MobState.Invalid, newState.Value.Value, thresholds: component);
UpdateAlerts(target, newState.Value.Value, component);
CheckThresholds(target, mobState, thresholds, damageable);
var ev = new MobThresholdChecked(target, mobState, thresholds, damageable);
RaiseLocalEvent(target, ref ev, true);
UpdateAlerts(target, mobState.CurrentState, thresholds, damageable);
}
private void MobThresholdShutdown(EntityUid target, MobThresholdsComponent component, ComponentShutdown args)

View File

@@ -178,7 +178,7 @@
state: health7
name: alerts-health-name
description: alerts-health-desc
minSeverity: 0
minSeverity: 1
maxSeverity: 6
- type: alert