Files
OldThink/Content.Server/Radio/EntitySystems/RadioSystem.cs

155 lines
6.5 KiB
C#
Raw Normal View History

2022-12-24 14:50:34 -06:00
using Content.Server.Administration.Logs;
2022-11-15 17:09:27 +13:00
using Content.Server.Chat.Systems;
2021-06-09 22:19:39 +02:00
using Content.Server.Radio.Components;
2022-11-15 17:09:27 +13:00
using Content.Server.VoiceMask;
using Content.Server.Popups;
2022-11-15 17:09:27 +13:00
using Content.Shared.Chat;
2022-12-24 14:50:34 -06:00
using Content.Shared.Database;
using Content.Shared.Radio;
2022-11-15 17:09:27 +13:00
using Robust.Server.GameObjects;
using Robust.Shared.Network;
2022-11-23 00:52:19 +13:00
using Robust.Shared.Replays;
2022-11-15 17:09:27 +13:00
using Robust.Shared.Utility;
using Content.Shared.Popups;
2023-03-24 03:02:41 +03:00
using Robust.Shared.Map;
using Content.Shared.Radio.Components;
using Content.Server.Power.Components;
using Robust.Shared.Random;
2022-11-15 17:09:27 +13:00
namespace Content.Server.Radio.EntitySystems;
/// <summary>
2022-11-23 00:52:19 +13:00
/// This system handles intrinsic radios and the general process of converting radio messages into chat messages.
2022-11-15 17:09:27 +13:00
/// </summary>
public sealed class RadioSystem : EntitySystem
{
2022-11-15 17:09:27 +13:00
[Dependency] private readonly INetManager _netMan = default!;
2022-11-23 00:52:19 +13:00
[Dependency] private readonly IReplayRecordingManager _replay = default!;
2022-12-24 14:50:34 -06:00
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly IRobustRandom _random = default!;
2023-03-24 03:02:41 +03:00
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly ChatSystem _chat = default!;
2022-11-15 17:09:27 +13:00
// set used to prevent radio feedback loops.
private readonly HashSet<string> _messages = new();
public override void Initialize()
{
2022-11-15 17:09:27 +13:00
base.Initialize();
SubscribeLocalEvent<IntrinsicRadioReceiverComponent, RadioReceiveEvent>(OnIntrinsicReceive);
SubscribeLocalEvent<IntrinsicRadioTransmitterComponent, EntitySpokeEvent>(OnIntrinsicSpeak);
}
Headsets (#2023) * add headset component * add basic headset logic * fix formatting in listening component, add dependency to headset * test function for headset * implement headset as listener * ANNIHILATES ListeningComponent, refactor of radio/listener sys * basic headset functionality * rename RadioComponent to HandheldRadioComponent * change channel to list of channels * basic headset implementation complete * message now always excludes ';' * add radio color; state channel freq. and source name * undocumented game breaking bug commit (DO NOT RESEARCH) actually just changes frequency from 1457 (what signalers are set to by default) to 1459, the actual frequency for common * Add more sprites * Reorganizes * Added job headsets * Adds headset as an ignored component * Jobs now spawn with headsets * remove system.tracing * Catchup commits * Add headset property serialization * Turn GetChannels into a property * ListenRange property and serializatioon * Adjust interfaces * Address reviews * Cleanup * Address reviews * Update rsi * Fix licenses and copyright * Fix missing textures * Merge fixes * Move headset textures from objects/devices to clothing/ears * Fix rsi state names and add equipped states * Fix headsets not working * Add missing brackets to channel number in chat * heck * Fix broken rsi * Fix radio id and names * Put quotes around headset messages * Fix method names * Fix handheld radios * Fix capitalization when using radio channels and trim * Remove unnecessary dependency * Indent that * Separate this part * Goodbye icons * Implement IActivate in HandheldRadioComponent * Add examine tooltip to radios and headsets * Rename IListen methods Co-authored-by: Bright <nsmoak10@yahoo.com> Co-authored-by: Swept <jamesurquhartwebb@gmail.com> Co-authored-by: Bright0 <55061890+Bright0@users.noreply.github.com>
2020-10-07 14:02:12 +02:00
2022-11-15 17:09:27 +13:00
private void OnIntrinsicSpeak(EntityUid uid, IntrinsicRadioTransmitterComponent component, EntitySpokeEvent args)
{
if (args.Channel != null && component.Channels.Contains(args.Channel.ID))
2022-04-08 17:17:25 -04:00
{
2023-03-24 03:02:41 +03:00
SendRadioMessage(uid, args.Message, args.Channel, uid);
2022-11-15 17:09:27 +13:00
args.Channel = null; // prevent duplicate messages from other listeners.
}
2022-11-15 17:09:27 +13:00
}
2023-03-24 03:02:41 +03:00
private void OnIntrinsicReceive(EntityUid uid, IntrinsicRadioReceiverComponent component, ref RadioReceiveEvent args)
2022-11-15 17:09:27 +13:00
{
if (TryComp(uid, out ActorComponent? actor))
_netMan.ServerSendMessage(args.ChatMsg, actor.PlayerSession.ConnectedClient);
}
2023-03-24 03:02:41 +03:00
/// <summary>
/// Send radio message to all active radio listeners
/// </summary>
/// <param name="messageSource">Entity that spoke the message</param>
/// <param name="radioSource">Entity that picked up the message and will send it, e.g. headset</param>
public void SendRadioMessage(EntityUid messageSource, string message, RadioChannelPrototype channel, EntityUid radioSource)
2022-11-15 17:09:27 +13:00
{
// TODO if radios ever garble / modify messages, feedback-prevention needs to be handled better than this.
if (!_messages.Add(message))
return;
2023-03-24 03:02:41 +03:00
var name = TryComp(messageSource, out VoiceMaskComponent? mask) && mask.Enabled
? mask.VoiceName
2023-03-24 03:02:41 +03:00
: MetaData(messageSource).EntityName;
2022-04-08 17:17:25 -04:00
2022-11-15 17:09:27 +13:00
name = FormattedMessage.EscapeText(name);
var speech = _chat.GetSpeechVerb(messageSource, message);
var wrappedMessage = Loc.GetString(speech.Bold ? "chat-radio-message-wrap-bold" : "chat-radio-message-wrap",
("color", channel.Color),
("fontType", speech.FontId),
("fontSize", speech.FontSize),
("verb", Loc.GetString(_random.Pick(speech.SpeechVerbStrings))),
("channel", $"\\[{channel.LocalizedName}\\]"),
("name", name),
("message", FormattedMessage.EscapeText(message)));
2022-11-15 17:09:27 +13:00
// most radios are relayed to chat, so lets parse the chat message beforehand
2022-11-23 00:52:19 +13:00
var chat = new ChatMessage(
ChatChannel.Radio,
message,
wrappedMessage,
2022-11-23 00:52:19 +13:00
EntityUid.Invalid);
var chatMsg = new MsgChatMessage { Message = chat };
2023-03-24 03:02:41 +03:00
var ev = new RadioReceiveEvent(message, messageSource, channel, chatMsg);
2022-04-08 17:17:25 -04:00
2023-04-14 22:50:19 +03:00
var sendAttemptEv = new RadioSendAttemptEvent(channel, radioSource);
RaiseLocalEvent(ref sendAttemptEv);
RaiseLocalEvent(radioSource, ref sendAttemptEv);
2023-04-14 22:50:19 +03:00
var canSend = !sendAttemptEv.Cancelled;
2023-03-24 03:02:41 +03:00
var sourceMapId = Transform(radioSource).MapID;
var hasActiveServer = HasActiveServer(sourceMapId, channel.ID);
var hasMicro = HasComp<RadioMicrophoneComponent>(radioSource);
2022-11-15 17:09:27 +13:00
2023-03-24 03:02:41 +03:00
var speakerQuery = GetEntityQuery<RadioSpeakerComponent>();
var radioQuery = EntityQueryEnumerator<ActiveRadioComponent, TransformComponent>();
2023-04-14 22:50:19 +03:00
while (canSend && radioQuery.MoveNext(out var receiver, out var radio, out var transform))
Headsets (#2023) * add headset component * add basic headset logic * fix formatting in listening component, add dependency to headset * test function for headset * implement headset as listener * ANNIHILATES ListeningComponent, refactor of radio/listener sys * basic headset functionality * rename RadioComponent to HandheldRadioComponent * change channel to list of channels * basic headset implementation complete * message now always excludes ';' * add radio color; state channel freq. and source name * undocumented game breaking bug commit (DO NOT RESEARCH) actually just changes frequency from 1457 (what signalers are set to by default) to 1459, the actual frequency for common * Add more sprites * Reorganizes * Added job headsets * Adds headset as an ignored component * Jobs now spawn with headsets * remove system.tracing * Catchup commits * Add headset property serialization * Turn GetChannels into a property * ListenRange property and serializatioon * Adjust interfaces * Address reviews * Cleanup * Address reviews * Update rsi * Fix licenses and copyright * Fix missing textures * Merge fixes * Move headset textures from objects/devices to clothing/ears * Fix rsi state names and add equipped states * Fix headsets not working * Add missing brackets to channel number in chat * heck * Fix broken rsi * Fix radio id and names * Put quotes around headset messages * Fix method names * Fix handheld radios * Fix capitalization when using radio channels and trim * Remove unnecessary dependency * Indent that * Separate this part * Goodbye icons * Implement IActivate in HandheldRadioComponent * Add examine tooltip to radios and headsets * Rename IListen methods Co-authored-by: Bright <nsmoak10@yahoo.com> Co-authored-by: Swept <jamesurquhartwebb@gmail.com> Co-authored-by: Bright0 <55061890+Bright0@users.noreply.github.com>
2020-10-07 14:02:12 +02:00
{
if (!radio.Channels.Contains(channel.ID) || (TryComp<IntercomComponent>(receiver, out var intercom) && !intercom.SupportedChannels.Contains(channel.ID)))
2022-11-15 17:09:27 +13:00
continue;
if (!channel.LongRange && transform.MapID != sourceMapId && !radio.GlobalReceive)
2023-03-24 03:02:41 +03:00
continue;
// don't need telecom server for long range channels or handheld radios and intercoms
var needServer = !channel.LongRange && (!hasMicro || !speakerQuery.HasComponent(receiver));
if (needServer && !hasActiveServer)
continue;
2023-03-24 03:02:41 +03:00
// check if message can be sent to specific receiver
var attemptEv = new RadioReceiveAttemptEvent(channel, radioSource, receiver);
RaiseLocalEvent(ref attemptEv);
RaiseLocalEvent(receiver, ref attemptEv);
2022-11-15 17:09:27 +13:00
if (attemptEv.Cancelled)
continue;
2023-03-24 03:02:41 +03:00
// send the message
RaiseLocalEvent(receiver, ref ev);
}
2022-11-15 17:09:27 +13:00
2023-03-24 03:02:41 +03:00
if (name != Name(messageSource))
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Radio message from {ToPrettyString(messageSource):user} as {name} on {channel.LocalizedName}: {message}");
else
2023-03-24 03:02:41 +03:00
_adminLogger.Add(LogType.Chat, LogImpact.Low, $"Radio message from {ToPrettyString(messageSource):user} on {channel.LocalizedName}: {message}");
2022-12-24 14:50:34 -06:00
_replay.RecordServerMessage(chat);
2022-11-15 17:09:27 +13:00
_messages.Remove(message);
}
2023-03-24 03:02:41 +03:00
/// <inheritdoc cref="TelecomServerComponent"/>
private bool HasActiveServer(MapId mapId, string channelId)
{
var servers = EntityQuery<TelecomServerComponent, EncryptionKeyHolderComponent, ApcPowerReceiverComponent, TransformComponent>();
foreach (var (_, keys, power, transform) in servers)
{
if (transform.MapID == mapId &&
power.Powered &&
keys.Channels.Contains(channelId))
{
return true;
}
}
return false;
}
}