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; : 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) public short GetMaxSeverity(AlertType alertType)
{ {
return _typeToAlert[alertType].MaxSeverity; return _typeToAlert[alertType].MaxSeverity;

View File

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

View File

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

View File

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