Files
wwdpublic/Content.Server/Radio/EntitySystems/HeadsetSystem.cs
FoxxoTrystan 82edb60ac6 Languages (#43)
Resolves https://github.com/Simple-Station/Einstein-Engines/issues/37

# Description

This PR adds languages. Every entity who can speak now speaks a specific
language (or Universal, for entities that are not supposed to speak,
which is understood by everyone). Other entities who do not understand
this language will see gibberish (it's possible to learn how certain
induvidual words are spelled. But the spelling changes between rounds).
This means that certain creatures, like xenos, cats, vulps, can
communicate within their species in their own languages. Similarly, it
means that xenos, cats and other things cannot understand GalacticCommon
speakers without a translator or cognization.

An entity may be able to speak multiple languages, or understand a
language but be unable to speak it.

Thi PR was orignally made for Frontier but is now being ported and will
be maintain here.
Orignal PR:
https://github.com/new-frontiers-14/frontier-station-14/pull/671

This PR was made orignally by Mnemotechnician and FoxxoTrystan.

---

# TODO

- [x] Language System. (Check Frontier PR for all the compleated todo
list)
- [x] Port PR from Frontier.
- [x] QOL Changes.
- [x] Missing Default Languages. (Missing default langauges for some
roundstart species)
- [x] Animals Languages.

---

<details><summary><h1>Media</h1></summary>
<p>


![image](https://github.com/Simple-Station/Einstein-Engines/assets/45297731/fc43efd9-612e-4a6d-8ed6-90a26d315c6f)

![image](https://github.com/Simple-Station/Einstein-Engines/assets/45297731/b86616a3-d5fb-408d-865e-90d09096b6d7)

![image](https://github.com/Simple-Station/Einstein-Engines/assets/45297731/ab1e8581-522d-4e7e-95e8-f62575bc5039)

</p>
</details>

---

# Changelog

🆑 FoxxoTrystan / Mnemotechnician
- add: All species can now bring their own cultures and languages

---------

Signed-off-by: Mnemotechnican <69920617+Mnemotechnician@users.noreply.github.com>
Signed-off-by: FoxxoTrystan <45297731+FoxxoTrystan@users.noreply.github.com>
Co-authored-by: fox <daytimer253@gmail.com>
Co-authored-by: Mnemotechnican <69920617+Mnemotechnician@users.noreply.github.com>
Co-authored-by: Pspritechologist <81725545+Pspritechologist@users.noreply.github.com>
Co-authored-by: Lincoln McQueen <lincoln.mcqueen@gmail.com>
Co-authored-by: Arkyfloof <Marvinlinke08@gmail.com>
Co-authored-by: reese1243 <ber23027@byui.edu>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: Eagle-0 <114363363+Eagle-0@users.noreply.github.com>
Co-authored-by: BlitzDev <145472107+Reese1243@users.noreply.github.com>
Co-authored-by: Arkyfloof <161242062+Arkyfloof@users.noreply.github.com>
Co-authored-by: Danger Revolution! <142105406+DangerRevolution@users.noreply.github.com>
Co-authored-by: DEATHB4DEFEAT <77995199+DEATHB4DEFEAT@users.noreply.github.com>
2024-06-10 16:48:48 -04:00

127 lines
4.4 KiB
C#

using Content.Server.Chat.Systems;
using Content.Server.Emp;
using Content.Server.Language;
using Content.Server.Radio.Components;
using Content.Server.Speech;
using Content.Shared.Chat;
using Content.Shared.Inventory.Events;
using Content.Shared.Radio;
using Content.Shared.Radio.Components;
using Content.Shared.Radio.EntitySystems;
using Robust.Shared.Network;
using Robust.Shared.Player;
namespace Content.Server.Radio.EntitySystems;
public sealed class HeadsetSystem : SharedHeadsetSystem
{
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly RadioSystem _radio = default!;
[Dependency] private readonly LanguageSystem _language = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HeadsetComponent, RadioReceiveEvent>(OnHeadsetReceive);
SubscribeLocalEvent<HeadsetComponent, EncryptionChannelsChangedEvent>(OnKeysChanged);
SubscribeLocalEvent<WearingHeadsetComponent, EntitySpokeEvent>(OnSpeak);
SubscribeLocalEvent<HeadsetComponent, EmpPulseEvent>(OnEmpPulse);
}
private void OnKeysChanged(EntityUid uid, HeadsetComponent component, EncryptionChannelsChangedEvent args)
{
UpdateRadioChannels(uid, component, args.Component);
}
private void UpdateRadioChannels(EntityUid uid, HeadsetComponent headset, EncryptionKeyHolderComponent? keyHolder = null)
{
// make sure to not add ActiveRadioComponent when headset is being deleted
if (!headset.Enabled || MetaData(uid).EntityLifeStage >= EntityLifeStage.Terminating)
return;
if (!Resolve(uid, ref keyHolder))
return;
if (keyHolder.Channels.Count == 0)
RemComp<ActiveRadioComponent>(uid);
else
EnsureComp<ActiveRadioComponent>(uid).Channels = new(keyHolder.Channels);
}
private void OnSpeak(EntityUid uid, WearingHeadsetComponent component, EntitySpokeEvent args)
{
if (args.Channel != null
&& TryComp(component.Headset, out EncryptionKeyHolderComponent? keys)
&& keys.Channels.Contains(args.Channel.ID))
{
_radio.SendRadioMessage(uid, args.Message, args.Channel, component.Headset);
args.Channel = null; // prevent duplicate messages from other listeners.
}
}
protected override void OnGotEquipped(EntityUid uid, HeadsetComponent component, GotEquippedEvent args)
{
base.OnGotEquipped(uid, component, args);
if (component.IsEquipped && component.Enabled)
{
EnsureComp<WearingHeadsetComponent>(args.Equipee).Headset = uid;
UpdateRadioChannels(uid, component);
}
}
protected override void OnGotUnequipped(EntityUid uid, HeadsetComponent component, GotUnequippedEvent args)
{
base.OnGotUnequipped(uid, component, args);
component.IsEquipped = false;
RemComp<ActiveRadioComponent>(uid);
RemComp<WearingHeadsetComponent>(args.Equipee);
}
public void SetEnabled(EntityUid uid, bool value, HeadsetComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (component.Enabled == value)
return;
if (!value)
{
RemCompDeferred<ActiveRadioComponent>(uid);
if (component.IsEquipped)
RemCompDeferred<WearingHeadsetComponent>(Transform(uid).ParentUid);
}
else if (component.IsEquipped)
{
EnsureComp<WearingHeadsetComponent>(Transform(uid).ParentUid).Headset = uid;
UpdateRadioChannels(uid, component);
}
}
private void OnHeadsetReceive(EntityUid uid, HeadsetComponent component, ref RadioReceiveEvent args)
{
var parent = Transform(uid).ParentUid;
if (TryComp(parent, out ActorComponent? actor))
{
var canUnderstand = _language.CanUnderstand(parent, args.Language);
var msg = new MsgChatMessage
{
Message = canUnderstand ? args.OriginalChatMsg : args.LanguageObfuscatedChatMsg
};
_netMan.ServerSendMessage(msg, actor.PlayerSession.Channel);
}
}
private void OnEmpPulse(EntityUid uid, HeadsetComponent component, ref EmpPulseEvent args)
{
if (component.Enabled)
{
args.Affected = true;
args.Disabled = true;
}
}
}