Skip to content

Commit

Permalink
Some MobState Refactoring (space-wizards#1382)
Browse files Browse the repository at this point in the history
# Description

This PR does *some* refactoring of MobState in preparation for adding a
more in-depth SoftCritical system, mostly adding code support for the
new threshold existing in the first place. Additionally, the
MobStateComponent now also includes several DataFields that allow more
precise fine tuning of what all mobs are allowed to do in each state, as
opposed to hardcoding a majority of these interactions.

The actual SoftCritical state isn't currently used by anything, and so
is a largely vestigial feature. It would have to be added individually
to mobs, such as the BaseMobSpeciesOrganic when the time comes to
actually add this feature.

# Changelog

:cl:
- add: Lots of refactoring of the Mob State Systerm in preparation for
implementing a much more detailed Soft Critical state.

---------

Signed-off-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: DEATHB4DEFEAT <77995199+DEATHB4DEFEAT@users.noreply.github.com>
  • Loading branch information
VMSolidus and DEATHB4DEFEAT authored Jan 2, 2025
1 parent 5767be1 commit 1e9cb1c
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 55 deletions.
98 changes: 98 additions & 0 deletions Content.Server/Traits/TraitSystem.Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ public sealed partial class TraitModifyMobThresholds : TraitFunction
[DataField, AlwaysPushInheritance]
public int CritThresholdModifier;

[DataField, AlwaysPushInheritance]
public int SoftCritThresholdModifier;

[DataField, AlwaysPushInheritance]
public int DeadThresholdModifier;

Expand All @@ -415,6 +418,13 @@ public override void OnPlayerSpawn(EntityUid uid,
thresholdSystem.SetMobStateThreshold(uid, critThreshold + CritThresholdModifier, MobState.Critical);
}

if (SoftCritThresholdModifier != 0)
{
var softCritThreshold = thresholdSystem.GetThresholdForState(uid, MobState.SoftCritical, threshold);
if (softCritThreshold != 0)
thresholdSystem.SetMobStateThreshold(uid, softCritThreshold + SoftCritThresholdModifier, MobState.SoftCritical);
}

if (DeadThresholdModifier != 0)
{
var deadThreshold = thresholdSystem.GetThresholdForState(uid, MobState.Dead, threshold);
Expand All @@ -424,6 +434,94 @@ public override void OnPlayerSpawn(EntityUid uid,
}
}

[UsedImplicitly]
public sealed partial class TraitModifyMobState : TraitFunction
{
// Three-State Booleans my beloved.
// :faridabirb.png:

[DataField, AlwaysPushInheritance]
public bool? AllowMovementWhileCrit;

[DataField, AlwaysPushInheritance]
public bool? AllowMovementWhileSoftCrit;

[DataField, AlwaysPushInheritance]
public bool? AllowMovementWhileDead;

[DataField, AlwaysPushInheritance]
public bool? AllowTalkingWhileCrit;

[DataField, AlwaysPushInheritance]
public bool? AllowTalkingWhileSoftCrit;

[DataField, AlwaysPushInheritance]
public bool? AllowTalkingWhileDead;

[DataField, AlwaysPushInheritance]
public bool? DownWhenCrit;

[DataField, AlwaysPushInheritance]
public bool? DownWhenSoftCrit;

[DataField, AlwaysPushInheritance]
public bool? DownWhenDead;

[DataField, AlwaysPushInheritance]
public bool? AllowHandInteractWhileCrit;

[DataField, AlwaysPushInheritance]
public bool? AllowHandInteractWhileSoftCrit;

[DataField, AlwaysPushInheritance]
public bool? AllowHandInteractWhileDead;

public override void OnPlayerSpawn(EntityUid uid,
IComponentFactory factory,
IEntityManager entityManager,
ISerializationManager serializationManager)
{
if (!entityManager.TryGetComponent<MobStateComponent>(uid, out var mobStateComponent))
return;

if (AllowMovementWhileCrit is not null)
mobStateComponent.AllowMovementWhileCrit = AllowMovementWhileCrit.Value;

if (AllowMovementWhileSoftCrit is not null)
mobStateComponent.AllowHandInteractWhileSoftCrit = AllowMovementWhileSoftCrit.Value;

if (AllowMovementWhileDead is not null)
mobStateComponent.AllowMovementWhileDead = AllowMovementWhileDead.Value;

if (AllowTalkingWhileCrit is not null)
mobStateComponent.AllowTalkingWhileCrit = AllowTalkingWhileCrit.Value;

if (AllowTalkingWhileSoftCrit is not null)
mobStateComponent.AllowTalkingWhileSoftCrit = AllowTalkingWhileSoftCrit.Value;

if (AllowTalkingWhileDead is not null)
mobStateComponent.AllowTalkingWhileDead = AllowTalkingWhileDead.Value;

if (DownWhenCrit is not null)
mobStateComponent.DownWhenCrit = DownWhenCrit.Value;

if (DownWhenSoftCrit is not null)
mobStateComponent.DownWhenSoftCrit = DownWhenSoftCrit.Value;

if (DownWhenDead is not null)
mobStateComponent.DownWhenDead = DownWhenDead.Value;

if (AllowHandInteractWhileCrit is not null)
mobStateComponent.AllowHandInteractWhileCrit = AllowHandInteractWhileCrit.Value;

if (AllowHandInteractWhileSoftCrit is not null)
mobStateComponent.AllowHandInteractWhileSoftCrit = AllowHandInteractWhileSoftCrit.Value;

if (AllowHandInteractWhileDead is not null)
mobStateComponent.AllowHandInteractWhileDead = AllowHandInteractWhileDead.Value;
}
}

[UsedImplicitly]
public sealed partial class TraitModifyStamina : TraitFunction
{
Expand Down
126 changes: 99 additions & 27 deletions Content.Shared/Mobs/Components/MobStateComponent.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,105 @@
using Content.Shared.Damage;
using Content.Shared.Mobs.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;

namespace Content.Shared.Mobs.Components
namespace Content.Shared.Mobs.Components;

/// <summary>
/// When attached to an <see cref="DamageableComponent"/>,
/// this component will handle critical and death behaviors for mobs.
/// Additionally, it handles sending effects to clients
/// (such as blur effect for unconsciousness) and managing the health HUD.
/// </summary>
[RegisterComponent]
[NetworkedComponent]
[AutoGenerateComponentState]
public sealed partial class MobStateComponent : Component
{
/// <summary>
/// When attached to an <see cref="DamageableComponent"/>,
/// this component will handle critical and death behaviors for mobs.
/// Additionally, it handles sending effects to clients
/// (such as blur effect for unconsciousness) and managing the health HUD.
/// </summary>
[RegisterComponent]
[NetworkedComponent]
[AutoGenerateComponentState]
[Access(typeof(MobStateSystem), typeof(MobThresholdSystem))]
public sealed partial class MobStateComponent : Component
{
//default mobstate is always the lowest state level
[AutoNetworkedField, ViewVariables]
public MobState CurrentState { get; set; } = MobState.Alive;

[DataField]
[AutoNetworkedField]
public HashSet<MobState> AllowedStates = new()
{
MobState.Alive,
MobState.Critical,
MobState.Dead
};
}
/// Whether this mob will be allowed to issue movement commands when in the Critical MobState.
/// </summary>
[DataField]
public bool AllowMovementWhileCrit;

/// <summary>
/// Whether this mob will be allowed to issue movement commands when in the Soft-Crit MobState.
/// </summary>
[DataField]
public bool AllowMovementWhileSoftCrit;

/// <summary>
/// Whether this mob will be allowed to issue movement commands when in the Dead MobState.
/// This is provided for completeness sake, and *probably* shouldn't be used by default.
/// </summary>
[DataField]
public bool AllowMovementWhileDead;

/// <summary>
/// Whether this mob will be allowed to talk while in the Critical MobState.
/// </summary>
[DataField]
public bool AllowTalkingWhileCrit = true;

/// <summary>
/// Whether this mob will be allowed to talk while in the SoftCritical MobState.
/// </summary>
[DataField]
public bool AllowTalkingWhileSoftCrit = true;

/// <summary>
/// Whether this mob will be allowed to talk while in the Dead MobState.
/// This is provided for completeness sake, and *probably* shouldn't be used by default.
/// </summary>
[DataField]
public bool AllowTalkingWhileDead;

/// <summary>
/// Whether this mob is forced to be downed when entering the Critical MobState.
/// </summary>
[DataField]
public bool DownWhenCrit = true;

/// <summary>
/// Whether this mob is forced to be downed when entering the SoftCritical MobState.
/// </summary>
[DataField]
public bool DownWhenSoftCrit = true;

/// <summary>
/// Whether this mob is forced to be downed when entering the Dead MobState.
/// </summary>
[DataField]
public bool DownWhenDead = true;

/// <summary>
/// Whether this mob is allowed to perform hand interactions while in the Critical MobState.
/// </summary>
[DataField]
public bool AllowHandInteractWhileCrit;

/// <summary>
/// Whether this mob is allowed to perform hand interactions while in the SoftCritical MobState.
/// </summary>
[DataField]
public bool AllowHandInteractWhileSoftCrit;

/// <summary>
/// Whether this mob is allowed to perform hand interactions while in the Dead MobState.
/// This is provided for completeness sake, and *probably* shouldn't be used by default.
/// </summary>
[DataField]
public bool AllowHandInteractWhileDead;

//default mobstate is always the lowest state level
[AutoNetworkedField, ViewVariables]
public MobState CurrentState { get; set; } = MobState.Alive;

[DataField]
[AutoNetworkedField]
public HashSet<MobState> AllowedStates = new()
{
MobState.Alive,
MobState.Critical,
MobState.SoftCritical,
MobState.Dead
};
}
13 changes: 7 additions & 6 deletions Content.Shared/Mobs/Components/MobThresholdsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ namespace Content.Shared.Mobs.Components;
[Access(typeof(MobThresholdSystem))]
public sealed partial class MobThresholdsComponent : Component
{
[DataField("thresholds", required: true)]
[DataField(required: true)]
public SortedDictionary<FixedPoint2, MobState> Thresholds = new();

[DataField("triggersAlerts")]
[DataField]
public bool TriggersAlerts = true;

[DataField("currentThresholdState")]
[DataField]
public MobState CurrentThresholdState;

/// <summary>
/// The health alert that should be displayed for player controlled entities.
/// Used for alternate health alerts (silicons, for example)
/// </summary>
[DataField("stateAlertDict")]
[DataField]
public Dictionary<MobState, ProtoId<AlertPrototype>> StateAlertDict = new()
{
{MobState.Alive, "HumanHealth"},
{MobState.Critical, "HumanCrit"},
{MobState.SoftCritical, "HumanCrit"},
{MobState.Dead, "HumanDead"},
};

Expand All @@ -38,13 +39,13 @@ public sealed partial class MobThresholdsComponent : Component
/// <summary>
/// Whether or not this entity should display damage overlays (robots don't feel pain, black out etc.)
/// </summary>
[DataField("showOverlays")]
[DataField]
public bool ShowOverlays = true;

/// <summary>
/// Whether or not this entity can be revived out of a dead state.
/// </summary>
[DataField("allowRevives")]
[DataField]
public bool AllowRevives;
}

Expand Down
3 changes: 2 additions & 1 deletion Content.Shared/Mobs/MobState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public enum MobState : byte
Invalid = 0,
Alive = 1,
Critical = 2,
Dead = 3
SoftCritical = 3,
Dead = 4,
}

/// <summary>
Expand Down
Loading

0 comments on commit 1e9cb1c

Please sign in to comment.