CatStation/Content.Shared/Humanoid/HumanoidProfileSystem.cs
Falcon 9805fe3956
Refactor voice to separate it from sex (#40593)
* feat: separate sex from voice

* wip: tried to make it use voice collection directly

* fix: yaml

* fix: migrations

* fix: ensure valid now validates correctly

* fix: unisex mobs wouldn't have voice

* chore: fix RT version

* refactor: clean up code into subfunctions

* refactor: swap name and protoId in voice

also rename sounds to voice

* chore: add comment

* fix: fallback sounds for non-humanoid characters

* fix: tests

* fix: tests

* refactor: change requests

* refactor: convert sheepers

* refactor: move Voice to SpeciesPrototype

* chore: add tests

* fix: test IoC error

* refactor: use null coalesing in ConvertProfiles

* refactor: move vocal sex association to speciesPrototype

* style: remove space from new()

* refactor: use name from emoteSoundsPrototype for UI

* refactor: no more null Voice on profile

* refactor: make DefaultVoice a static readonly property

* fix: add default voice to HumanoidProfileComponent

* chore: remove repeated vocal component from species yaml

It's inherited from BaseSpeciesMob

* style: correct comments for refactor

* fix: add missing locale for FemaleVulpkanin

* refactor: simplify logic based off surounding code

* test: add test to make sure that sexes are properly enumerated for the voices

* fix: add back urist sounds

* style: clarify comments again

* style: last thing im changing before I sleep

* refactor: battling programming semantics

* refactor: Name on EmoteSoundsPrototype.cs to VoiceSelectorName for clarity

* revert: isDirty change

* Fix HumanoidProfileTest.EnsureValidLoading.
    The profile's voice wasnt being updated, thus the voiceChanged event was never raised and that meant that the voice component had old EmoteSounds

* oops

* refactor: do the easy change requests

* refactor: use an array for indexing sex conversion for performance

* fix master

* mistakes were made

* chore: redo migrations

---------

Co-authored-by: AJCM-git <ajcm_mail@disroot.org>
2026-06-30 23:46:27 -04:00

86 lines
2.9 KiB
C#

using Content.Shared.Examine;
using Content.Shared.Humanoid.Prototypes;
using Content.Shared.IdentityManagement;
using Content.Shared.Preferences;
using Robust.Shared.GameObjects.Components.Localization;
using Robust.Shared.Prototypes;
namespace Content.Shared.Humanoid;
public sealed partial class HumanoidProfileSystem : EntitySystem
{
[Dependency] private GrammarSystem _grammar = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HumanoidProfileComponent, ExaminedEvent>(OnExamined);
}
public void ApplyProfileTo(Entity<HumanoidProfileComponent?> ent, HumanoidCharacterProfile profile)
{
if (!Resolve(ent, ref ent.Comp))
return;
ent.Comp.Gender = profile.Gender;
ent.Comp.Age = profile.Age;
ent.Comp.Species = profile.Species;
ent.Comp.Voice = profile.Voice;
ent.Comp.Sex = profile.Sex;
Dirty(ent);
var voiceChanged = new VoiceChangedEvent(ent.Comp.Voice, profile.Voice);
RaiseLocalEvent(ent, ref voiceChanged);
if (TryComp<GrammarComponent>(ent, out var grammar))
{
_grammar.SetGender((ent, grammar), profile.Gender);
}
}
private void OnExamined(Entity<HumanoidProfileComponent> ent, ref ExaminedEvent args)
{
var identity = Identity.Entity(ent, EntityManager);
var species = GetSpeciesRepresentation(ent.Comp.Species).ToLower();
var age = GetAgeRepresentation(ent.Comp.Species, ent.Comp.Age);
args.PushText(Loc.GetString("humanoid-appearance-component-examine", ("user", identity), ("age", age), ("species", species)));
}
/// <summary>
/// Takes ID of the species prototype, returns UI-friendly name of the species.
/// </summary>
public string GetSpeciesRepresentation(ProtoId<SpeciesPrototype> species)
{
if (ProtoMan.TryIndex(species, out var speciesPrototype))
return Loc.GetString(speciesPrototype.Name);
Log.Error("Tried to get representation of unknown species: {speciesId}");
return Loc.GetString("humanoid-appearance-component-unknown-species");
}
/// <summary>
/// Takes ID of the species prototype and an age, returns an approximate description
/// </summary>
public string GetAgeRepresentation(ProtoId<SpeciesPrototype> species, int age)
{
if (!ProtoMan.TryIndex(species, out var speciesPrototype))
{
Log.Error("Tried to get age representation of species that couldn't be indexed: " + species);
return Loc.GetString("identity-age-young");
}
if (age < speciesPrototype.YoungAge)
{
return Loc.GetString("identity-age-young");
}
if (age < speciesPrototype.OldAge)
{
return Loc.GetString("identity-age-middle-aged");
}
return Loc.GetString("identity-age-old");
}
}