Add department-specific radio channels (#9061)
* Add department-specific radio channels
This commit adds working department-specific radio channels, while
minimizing damage to the current codebase. It is expected that a future
refactor will clean this up a bit.
ChatSystem now has a RadioPrefix() method that recognizes
department-specific channels (e.g. ":e" and ":m") in addition to the
global channel (";"). It strips the prefix from the message and assigns
messages an integer representing the destination channel, if any.
IListen and IRadio now accept optional 'channel' arguments with this
channel in mind.
The ugly is that the integer channel number is hard-coded and also shows
up in chat.
Comms are not modeled at this time. You cannot break comms (yet).
All headsets have channels soldered into them. You cannot change
encryption keys to hop on new channels. Steal a headset instead.
* Remove debugging print
* Convert to prototypes
* Use prototype names in headset prototype
* Adjust list style
* Document prototype fields
* cringe
* some cleanup
* colours
* Remove alphas at least
* cc
Co-authored-by: Kevin Zheng <kevinz5000@gmail.com>
This commit is contained in:
88
Content.Server/Chat/Systems/ChatSystem.Radio.cs
Normal file
88
Content.Server/Chat/Systems/ChatSystem.Radio.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using Content.Server.Headset;
|
||||
using Content.Shared.Radio;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Chat.Systems;
|
||||
|
||||
public sealed partial class ChatSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Cache of the keycodes for faster lookup.
|
||||
/// </summary>
|
||||
private Dictionary<char, RadioChannelPrototype> _keyCodes = new();
|
||||
|
||||
private void InitializeRadio()
|
||||
{
|
||||
_prototypeManager.PrototypesReloaded += OnPrototypeReload;
|
||||
CacheRadios();
|
||||
}
|
||||
|
||||
private void OnPrototypeReload(PrototypesReloadedEventArgs obj)
|
||||
{
|
||||
CacheRadios();
|
||||
}
|
||||
|
||||
private void CacheRadios()
|
||||
{
|
||||
_keyCodes.Clear();
|
||||
|
||||
foreach (var proto in _prototypeManager.EnumeratePrototypes<RadioChannelPrototype>())
|
||||
{
|
||||
_keyCodes.Add(proto.KeyCode, proto);
|
||||
}
|
||||
}
|
||||
|
||||
private void ShutdownRadio()
|
||||
{
|
||||
_prototypeManager.PrototypesReloaded -= OnPrototypeReload;
|
||||
}
|
||||
|
||||
private (string, RadioChannelPrototype?) GetRadioPrefix(EntityUid source, string message)
|
||||
{
|
||||
// TODO: Turn common into a true frequency and support multiple aliases.
|
||||
var channelMessage = message.StartsWith(':') || message.StartsWith('.');
|
||||
var radioMessage = message.StartsWith(';') || channelMessage;
|
||||
if (!radioMessage) return (message, null);
|
||||
|
||||
// Special case for empty messages
|
||||
if (message.Length <= 1)
|
||||
return (string.Empty, null);
|
||||
|
||||
// Look for a prefix indicating a destination radio channel.
|
||||
RadioChannelPrototype? chan;
|
||||
if (channelMessage && message.Length >= 2)
|
||||
{
|
||||
_keyCodes.TryGetValue(message[1], out chan);
|
||||
|
||||
if (chan == null)
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("chat-manager-no-such-channel"), source, Filter.Entities(source));
|
||||
chan = null;
|
||||
}
|
||||
|
||||
// Strip message prefix.
|
||||
message = message[2..].TrimStart();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Remove semicolon
|
||||
message = message[1..].TrimStart();
|
||||
chan = _prototypeManager.Index<RadioChannelPrototype>("Common");
|
||||
}
|
||||
|
||||
if (_inventory.TryGetSlotEntity(source, "ears", out var entityUid) &&
|
||||
TryComp(entityUid, out HeadsetComponent? headset))
|
||||
{
|
||||
headset.RadioRequested = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_popup.PopupEntity(Loc.GetString("chat-manager-no-headset-on-message"), source, Filter.Entities(source));
|
||||
}
|
||||
|
||||
return (message, chan);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user