Files
wwdpublic/Content.Shared/Mobs/Components/MobStateComponent.cs
RedFoxIV daf4f66414 "Proper" "Softcrit" "Support" (#1545)
# Description

Implements the softcrit functionality.
Similiar to critical state but spessmen will be able to communicate and
crawl around, but not pick up items.
Also supports configuring what is and isn't allowed in different
MobStates (per mob prototype): you can enable picking up items while in
softcrit so people can pick up their lasgun and continue shooting after
taking a 40x46mm to their ass cheeks from the guest nukies while being
dragged to safety.

![escape-from-tarkov-raid](https://github.com/user-attachments/assets/7f31702d-5677-4daf-a13d-8a9525fd3f9f)

<details> <summary><h1>Technical details</h1></summary>
New prototype type: "mobStateParams" (`MobStateParametersPrototype`)
Used to specify what can and can't be done when in a certain mobstate.
Of note that they are not actually bound to any `MobState` by
themselves. To assign a params prototype to a mobstate, use
`InitMobStateParams` in `MobStateComponent`.
It has to be a prototype because if I just did something akin to
`Dictionary<MobState, Dictionary<string, bool>>`, you'd have to check
the parent and copy every flag besides the one you wish to modify. That
is, if I understand how the prototype system works correctly, which I
frankly doubt. <!-- Working on softcrit made me hate prototypes. -->

MobStateComponent now has:
- `Dictionary<string, string> InitMobStateParams`, for storing "mobstate
- parameter prototype" pairs. `<string, string>` because it has to be
editable via mob prototypes. Named "mobStateParams" for mob prototypes.
- `public Dictionary<MobState, MobStateParametersPrototype>
MobStateParams` for actually storing the params for each state
- `public Dictionary<MobState, MobStateParametersOverride>
MobStateParamsOverrides` for storing overrides.
`MobStateParametersOverride` is a struct which mirrors all
`MobStateParametersPrototype`'s fields, except they're all nullable.
This is meant for code which wants to temporarily override some setting,
like a spell which allows dead people to talk. This is not the best
solution, but it should do at first. A better option would be tracking
each change separately, instead of hoping different systems overriding
the same flag will play nicely with eachother.
- a shitton of getter methods

TraitModifyMobState now has:
- `public Dictionary<string, string> Params` to specify a new prototype
to use.
- Important note: All values of `MobStateParametersPrototype` are
nullable, which is a hack to support `TraitModifyMobState`. This trait
takes one `MobStateParametersPrototype` per mobstate and applies all of
its non-null values. This way, a params prototype can be created which
will only have `pointing: true` and the trait can apply it (e.g. to
critstate, so we can spam pointing while dying like it's a game of turbo
dota)
- The above is why that wall of getters exists: They check the relevant
override struct, then the relevant prototype. If both are null, they
default to false (0f for floats.) The only exception is
OxyDamageOverlay, because it's used both for oxy damage overlay (if
null) and as a vision-limiting black void in crit..

MobStateSystem now has:
- a bunch of new "IsSomething"/"CanDoSomething" methods to check the
various flags, alongside rewritten old ones.
-
![image](https://github.com/user-attachments/assets/33a6b296-c12c-4311-9abe-90ca4288e871)
lookin ahh predicate factory

</details>
---

# TODO

done:
- [x] Make proper use of `MobStateSystem.IsIncapacitated()`.
done: some checks were changed, some left as they did what was (more or
less) intended.
<details>Previous `IsIncapacitated()` implementation simply checked if
person was in crit or dead. Now there is a `IsIncapacitated` flag in the
parameters, but it's heavily underutilized. I may need some help on this
one, since I don't know where would be a good place to check for it and
I absolutely will not just scour the entire build in search for them.
</details>

- [x] Separate force-dropping items from being downed
done: dropItemsOnEntering bool field. If true, will drop items upon
entering linked mobstate.
- [x] Don't drop items if `ForceDown` is true but `PickingUp` is also
true.
done: dropItemsOnEntering bool field. If true, will drop items upon
entering linked mobstate.
- [x] Actually check what are "conscious attempts" are used for
done: whether or not mob is conscious. Renamed the bool field
accordingly.
- [x] Look into adding a way to make people choke "slowly" in softcrit
as opposed to choking at "regular speed" in crit. Make that into a param
option? Make that into a float so the speed can be finetuned?
done: `BreathingMultiplier` float field added.
<details>
1f is regular breathing, 0.25 is "quarter-breathing". Air taken is
multiplied by `BreathingMultiplier` and suffocation damage taken (that
is dealt by RespiratorSystem, not all oxy damage) is multiplied by
`1-BreathingMultiplier`.
</details>

- [x] make sure the serializer actually does its job
done: it doesn't. Removed.
- [x] Make an option to prohibit using radio headsets while in softcrit
done: Requires Incapacitated parameter to be false to be able to use
headset radio.
- [x] Make sure it at least compiles

not done:
- [ ] probably move some other stuff to Params if it makes sense. Same
thing as with `IsIncapacitated` though: I kinda don't want to, at least
for now.

---

<details><summary><h1>No media</h1></summary>
<p>

:p

</p>
</details>

---

# Changelog

🆑
- add: Soft critical state. Crawl to safety, or to your doom - whatever
is closer.

---------

Signed-off-by: RedFoxIV <38788538+RedFoxIV@users.noreply.github.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>

(cherry picked from commit 9a357c1774f1a783844a07b5414f504ca574d84c)
2025-02-15 00:12:50 +03:00

200 lines
10 KiB
C#

using Content.Shared.Damage;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Markdown.Mapping;
using Robust.Shared.Serialization.Markdown.Sequence;
using Robust.Shared.Serialization.Markdown.Validation;
using Robust.Shared.Serialization.Markdown.Value;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Array;
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.CompilerServices;
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
{
[DataField("mobStateParams")]
public Dictionary<string, string> InitMobStateParams = new()
{
{"Alive", "AliveDefault" },
{"SoftCritical", "SoftCriticalDefault" },
{"Critical", "CriticalDefault" },
{"Dead", "DeadDefault" }
};
[AutoNetworkedField, ViewVariables]
public Dictionary<MobState, MobStateParametersPrototype> MobStateParams = new();
/// <summary>
/// Use this to modify mobstate parameters without actually overwrithing them.
/// </summary>
[AutoNetworkedField, ViewVariables]
public Dictionary<MobState, MobStateParametersOverride> MobStateParamsOverrides = new()
{
{ MobState.Alive, new() },
{ MobState.SoftCritical, new() },
{ MobState.Critical, new() },
{ MobState.Dead, new() }
};
[ViewVariables]
public MobStateParametersPrototype CurrentStateParams => MobStateParams[CurrentState];
[ViewVariables]
public MobStateParametersOverride CurrentStateOverrides => MobStateParamsOverrides[CurrentState];
//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.SoftCritical,
MobState.Critical,
MobState.Dead
};
#region terraria wall of getters boss
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private MobStateParametersOverride GetOverride(MobState? State = null) { MobStateParamsOverrides.TryGetValue(State ?? CurrentState, out var value); return value; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private MobStateParametersPrototype? GetParams(MobState? State = null) { MobStateParams.TryGetValue(State ?? CurrentState, out var value); return value; }
// the "?." and "?? false" at the end is because at round restard MobStateParams apparently can be wiped,
// but stuff that relies on it will still run, and I don't know why. Cool.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanMove(MobState? State = null) => GetOverride(State).Moving ?? GetParams(State)?.Moving ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanTalk(MobState? State = null) => GetOverride(State).Talking ?? GetParams(State)?.Talking ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanEmote(MobState? State = null) => GetOverride(State).Emoting ?? GetParams(State)?.Emoting ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanThrow(MobState? State = null) => GetOverride(State).Throwing ?? GetParams(State)?.Throwing ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanPickUp(MobState? State = null) => GetOverride(State).PickingUp ?? GetParams(State)?.PickingUp ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanPull(MobState? State = null) => GetOverride(State).Pulling ?? GetParams(State)?.Pulling ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanAttack(MobState? State = null) => GetOverride(State).Attacking ?? GetParams(State)?.Attacking ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanUse(MobState? State = null) => GetOverride(State).Using ?? GetParams(State)?.Using ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanPoint(MobState? State = null) =>GetOverride(State).Pointing ?? GetParams(State)?.Pointing ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsConscious(MobState? State = null) => GetOverride(State).IsConscious ?? GetParams(State)?.IsConscious ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsDowned(MobState? State = null) => GetOverride(State).ForceDown ?? GetParams(State)?.ForceDown ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ShouldDropItems(MobState? State = null) => GetOverride(State).DropItemsOnEntering ?? GetParams(State)?.DropItemsOnEntering ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsIncapacitated(MobState? State = null) => GetOverride(State).Incapacitated ?? GetParams(State)?.Incapacitated ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsThreatening(MobState? State = null) => GetOverride(State).Threatening ?? GetParams(State)?.Threatening ?? true; // :)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanEquipSelf(MobState? State = null) => GetOverride(State).CanEquipSelf ?? GetParams(State)?.CanEquipSelf ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanUnequipSelf(MobState? State = null) => GetOverride(State).CanUnequipSelf ?? GetParams(State)?.CanUnequipSelf ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanEquipOther(MobState? State = null) => GetOverride(State).CanEquipOther ?? GetParams(State)?.CanEquipOther ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool CanUnequipOther(MobState? State = null) => GetOverride(State).CanUnequipOther ?? GetParams(State)?.CanUnequipOther ?? false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float? GetOxyDamageOverlay(MobState? State = null) => GetOverride(State).OxyDamageOverlay ?? GetParams(State)?.OxyDamageOverlay;
/// <summary>
/// Not clamped, but you really should, in case someone decides to be funny with the prototypes.
/// [0,1].
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetBreathingMultiplier(MobState? State = null) => GetOverride(State).BreathingMultiplier ?? GetParams(State)?.BreathingMultiplier ?? 1f;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetStrippingTimeMultiplier(MobState? State = null) => GetOverride(State).StrippingTimeMultiplier ?? GetParams(State)?.StrippingTimeMultiplier ?? 1f;
#endregion
}
[NetSerializable, Serializable]
public struct MobStateParametersOverride
{
[DataField]
public bool? Moving, Talking, Emoting,
Throwing, PickingUp, Pulling, Attacking, Using, Pointing,
IsConscious,
CanEquipSelf, CanUnequipSelf, CanEquipOther, CanUnequipOther,
ForceDown, DropItemsOnEntering, Incapacitated, Threatening;
[DataField]
public float? OxyDamageOverlay, BreathingMultiplier, StrippingTimeMultiplier;
}
[Prototype("mobStateParams")]
[Serializable] // do i need to put netserializable on it?
public sealed partial class MobStateParametersPrototype : IPrototype, IInheritingPrototype
{
[IdDataField]
public string ID { get; } = default!;
[ParentDataField(typeof(AbstractPrototypeIdArraySerializer<MobStateParametersPrototype>))]
public string[]? Parents { get; private set; }
[AbstractDataField]
[NeverPushInheritance]
public bool Abstract { get; }
[DataField]
public bool? Moving, Talking, Emoting,
Throwing, PickingUp, Pulling, Attacking, Using, Pointing,
IsConscious,
CanEquipSelf, CanUnequipSelf, CanEquipOther, CanUnequipOther,
ForceDown, DropItemsOnEntering, Incapacitated, Threatening;
[DataField]
public float? OxyDamageOverlay, BreathingMultiplier, StrippingTimeMultiplier;
public void MergeWith(MobStateParametersPrototype other)
{
this.Moving = other.Moving ?? this.Moving;
this.Talking = other.Talking ?? this.Talking;
this.Emoting = other.Emoting ?? this.Emoting;
this.Throwing = other.Throwing ?? this.Throwing;
this.PickingUp = other.PickingUp ?? this.PickingUp;
this.Pulling = other.Pulling ?? this.Pulling;
this.Attacking = other.Attacking ?? this.Attacking;
this.Using = other.Using ?? this.Using;
this.Pointing = other.Pointing ?? this.Pointing;
this.IsConscious = other.IsConscious ?? this.IsConscious;
this.CanEquipSelf = other.CanEquipSelf ?? this.CanEquipSelf;
this.CanEquipOther = other.CanEquipOther ?? this.CanEquipOther;
this.CanUnequipSelf = other.CanUnequipSelf ?? this.CanUnequipSelf;
this.CanUnequipOther = other.CanUnequipOther ?? this.CanUnequipOther;
this.ForceDown = other.ForceDown ?? this.ForceDown;
this.Incapacitated = other.Incapacitated ?? this.Incapacitated;
this.Threatening = other.Threatening ?? this.Threatening;
this.DropItemsOnEntering = other.DropItemsOnEntering ?? this.DropItemsOnEntering;
this.BreathingMultiplier = other.BreathingMultiplier ?? this.BreathingMultiplier;
this.OxyDamageOverlay = other.OxyDamageOverlay ?? this.OxyDamageOverlay;
this.StrippingTimeMultiplier = other.StrippingTimeMultiplier ?? this.StrippingTimeMultiplier;
}
}