Audible emotes (#12708)

Co-authored-by: Visne <39844191+Visne@users.noreply.github.com>
This commit is contained in:
Alex Evgrashin
2023-01-25 17:29:41 +01:00
committed by GitHub
parent 7ec896543f
commit ef452b38a9
45 changed files with 794 additions and 169 deletions

View File

@@ -1,41 +1,53 @@
using Content.Server.Humanoid;
using Content.Server.Speech.EntitySystems;
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Chat.Prototypes;
using Content.Shared.Humanoid;
using Robust.Shared.Audio;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Dictionary;
namespace Content.Server.Speech.Components;
/// <summary>
/// Component required for entities to be able to scream.
/// Component required for entities to be able to do vocal emotions.
/// </summary>
[RegisterComponent]
[Access(typeof(VocalSystem))]
public sealed class VocalComponent : Component
{
[DataField("maleScream")]
public SoundSpecifier MaleScream = new SoundCollectionSpecifier("MaleScreams");
/// <summary>
/// Emote sounds prototype id for each sex (not gender).
/// Entities without <see cref="HumanoidComponent"/> considered to be <see cref="Sex.Unsexed"/>.
/// </summary>
[DataField("sounds", customTypeSerializer: typeof(PrototypeIdValueDictionarySerializer<Sex, EmoteSoundsPrototype>))]
public Dictionary<Sex, string>? Sounds;
[DataField("femaleScream")]
public SoundSpecifier FemaleScream = new SoundCollectionSpecifier("FemaleScreams");
[DataField("unsexedScream")]
public SoundSpecifier UnsexedScream = new SoundCollectionSpecifier("MaleScreams");
[DataField("screamId", customTypeSerializer: typeof(PrototypeIdSerializer<EmotePrototype>))]
public string ScreamId = "Scream";
[DataField("wilhelm")]
public SoundSpecifier Wilhelm = new SoundPathSpecifier("/Audio/Voice/Human/wilhelm_scream.ogg");
[DataField("audioParams")]
public AudioParams AudioParams = AudioParams.Default.WithVolume(4f);
[DataField("wilhelmProbability")]
public float WilhelmProbability = 0.01f;
public const float Variation = 0.125f;
[DataField("screamActionId", customTypeSerializer: typeof(PrototypeIdSerializer<InstantActionPrototype>))]
public string ScreamActionId = "Scream";
[DataField("actionId", customTypeSerializer:typeof(PrototypeIdSerializer<InstantActionPrototype>))]
public string ActionId = "Scream";
[DataField("screamAction")]
public InstantAction? ScreamAction;
[DataField("action")] // must be a data-field to properly save cooldown when saving game state.
public InstantAction? ScreamAction = null;
/// <summary>
/// Currently loaded emote sounds prototype, based on entity sex.
/// Null if no valid prototype for entity sex was found.
/// </summary>
[ViewVariables]
public EmoteSoundsPrototype? EmoteSounds = null;
}
public sealed class ScreamActionEvent : InstantActionEvent { };
public sealed class ScreamActionEvent : InstantActionEvent
{
}

View File

@@ -0,0 +1,105 @@
using Content.Server.Actions;
using Content.Server.Chat.Systems;
using Content.Server.Humanoid;
using Content.Server.Speech.Components;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Chat.Prototypes;
using Content.Shared.Humanoid;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Speech.EntitySystems;
public sealed class VocalSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly ChatSystem _chat = default!;
[Dependency] private readonly ActionsSystem _actions = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<VocalComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<VocalComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<VocalComponent, SexChangedEvent>(OnSexChanged);
SubscribeLocalEvent<VocalComponent, EmoteEvent>(OnEmote);
SubscribeLocalEvent<VocalComponent, ScreamActionEvent>(OnScreamAction);
}
private void OnMapInit(EntityUid uid, VocalComponent component, MapInitEvent args)
{
// try to add scream action when vocal comp added
if (_proto.TryIndex(component.ScreamActionId, out InstantActionPrototype? proto))
{
component.ScreamAction = new InstantAction(proto);
_actions.AddAction(uid, component.ScreamAction, null);
}
LoadSounds(uid, component);
}
private void OnShutdown(EntityUid uid, VocalComponent component, ComponentShutdown args)
{
// remove scream action when component removed
if (component.ScreamAction != null)
{
_actions.RemoveAction(uid, component.ScreamAction);
}
}
private void OnSexChanged(EntityUid uid, VocalComponent component, SexChangedEvent args)
{
LoadSounds(uid, component);
}
private void OnEmote(EntityUid uid, VocalComponent component, ref EmoteEvent args)
{
if (args.Handled || !args.Emote.Category.HasFlag(EmoteCategory.Vocal))
return;
// snowflake case for wilhelm scream easter egg
if (args.Emote.ID == component.ScreamId)
{
args.Handled = TryPlayScreamSound(uid, component);
return;
}
// just play regular sound based on emote proto
args.Handled = _chat.TryPlayEmoteSound(uid, component.EmoteSounds, args.Emote);
}
private void OnScreamAction(EntityUid uid, VocalComponent component, ScreamActionEvent args)
{
if (args.Handled)
return;
_chat.TryEmoteWithChat(uid, component.ScreamActionId);
args.Handled = true;
}
private bool TryPlayScreamSound(EntityUid uid, VocalComponent component)
{
if (_random.Prob(component.WilhelmProbability))
{
_audio.PlayPvs(component.Wilhelm, uid, component.Wilhelm.Params);
return true;
}
return _chat.TryPlayEmoteSound(uid, component.EmoteSounds, component.ScreamId);
}
private void LoadSounds(EntityUid uid, VocalComponent component, Sex? sex = null)
{
if (component.Sounds == null)
return;
sex ??= CompOrNull<HumanoidAppearanceComponent>(uid)?.Sex ?? Sex.Unsexed;
if (!component.Sounds.TryGetValue(sex.Value, out var protoId))
return;
_proto.TryIndex(protoId, out component.EmoteSounds);
}
}

View File

@@ -1,101 +0,0 @@
using Content.Server.Humanoid;
using Content.Server.Popups;
using Content.Server.Speech.Components;
using Content.Shared.ActionBlocker;
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Humanoid;
using Content.Shared.Popups;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Speech;
/// <summary>
/// Fer Screamin
/// </summary>
/// <remarks>
/// Or I guess other vocalizations, like laughing. If fun is ever legalized on the station.
/// </remarks>
public sealed class VocalSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<VocalComponent, ScreamActionEvent>(OnActionPerform);
SubscribeLocalEvent<VocalComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<VocalComponent, ComponentShutdown>(OnShutdown);
}
private void OnMapInit(EntityUid uid, VocalComponent component, MapInitEvent args)
{
if (component.ScreamAction == null
&& _proto.TryIndex(component.ActionId, out InstantActionPrototype? act))
{
component.ScreamAction = new(act);
}
if (component.ScreamAction != null)
_actions.AddAction(uid, component.ScreamAction, null);
}
private void OnShutdown(EntityUid uid, VocalComponent component, ComponentShutdown args)
{
if (component.ScreamAction != null)
_actions.RemoveAction(uid, component.ScreamAction);
}
private void OnActionPerform(EntityUid uid, VocalComponent component, ScreamActionEvent args)
{
if (args.Handled)
return;
args.Handled = TryScream(uid, component);
}
public bool TryScream(EntityUid uid, VocalComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return false;
if (!_blocker.CanSpeak(uid))
return false;
var sex = CompOrNull<HumanoidAppearanceComponent>(uid)?.Sex ?? Sex.Unsexed;
if (_random.Prob(component.WilhelmProbability))
{
SoundSystem.Play(component.Wilhelm.GetSound(), Filter.Pvs(uid), uid, component.AudioParams);
return true;
}
var scale = (float) _random.NextGaussian(1, VocalComponent.Variation);
var pitchedParams = component.AudioParams.WithPitchScale(scale);
switch (sex)
{
case Sex.Male:
SoundSystem.Play(component.MaleScream.GetSound(), Filter.Pvs(uid), uid, pitchedParams);
break;
case Sex.Female:
SoundSystem.Play(component.FemaleScream.GetSound(), Filter.Pvs(uid), uid, pitchedParams);
break;
default:
SoundSystem.Play(component.UnsexedScream.GetSound(), Filter.Pvs(uid), uid, pitchedParams);
break;
}
_popupSystem.PopupEntity(Loc.GetString("scream-action-popup"), uid, PopupType.Medium);
return true;
}
}