Skip to content

Commit 0207f51

Browse files
Merge pull request #33 from EmoGarbage404/feat/radio-falloff
radio falloff
2 parents 886d855 + ff1fb08 commit 0207f51

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

Content.Server/Radio/EntitySystems/RadioSystem.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
using Robust.Shared.Random;
1515
using Robust.Shared.Replays;
1616
using Robust.Shared.Utility;
17+
// ES START
18+
using Content.Server._ES.Radio;
19+
// ES END
1720

1821
namespace Content.Server.Radio.EntitySystems;
1922

@@ -28,6 +31,9 @@ public sealed class RadioSystem : EntitySystem
2831
[Dependency] private readonly IPrototypeManager _prototype = default!;
2932
[Dependency] private readonly IRobustRandom _random = default!;
3033
[Dependency] private readonly ChatSystem _chat = default!;
34+
// ES START
35+
[Dependency] private readonly ESRadioSystem _esRadio = default!;
36+
// ES END
3137

3238
// set used to prevent radio feedback loops.
3339
private readonly HashSet<string> _messages = new();
@@ -145,6 +151,26 @@ public void SendRadioMessage(EntityUid messageSource, string message, RadioChann
145151
RaiseLocalEvent(receiver, ref attemptEv);
146152
if (attemptEv.Cancelled)
147153
continue;
154+
// ES START
155+
var distortedMessage = _esRadio.DistortMessage(radioSource, receiver, FormattedMessage.EscapeText(message));
156+
var distortedWrappedMessage = Loc.GetString(speech.Bold ? "chat-radio-message-wrap-bold" : "chat-radio-message-wrap",
157+
("color", channel.Color),
158+
("fontType", speech.FontId),
159+
("fontSize", speech.FontSize),
160+
("verb", Loc.GetString(_random.Pick(speech.SpeechVerbStrings))),
161+
("channel", $"\\[{channel.LocalizedName}\\]"),
162+
("name", name),
163+
("message", distortedMessage));
164+
165+
chat = new ChatMessage(
166+
ChatChannel.Radio,
167+
distortedMessage,
168+
distortedWrappedMessage,
169+
NetEntity.Invalid,
170+
null);
171+
chatMsg = new MsgChatMessage { Message = chat };
172+
ev = new RadioReceiveEvent(message, messageSource, channel, radioSource, chatMsg);
173+
// ES END
148174

149175
// send the message
150176
RaiseLocalEvent(receiver, ref ev);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Content.Shared.Dataset;
2+
using Robust.Shared.Prototypes;
3+
4+
namespace Content.Server._ES.Radio.Components;
5+
6+
[RegisterComponent]
7+
[Access(typeof(ESRadioSystem))]
8+
public sealed partial class ESRadioFalloffComponent : Component
9+
{
10+
[DataField]
11+
public float MaxClearSendDistance = 15f;
12+
13+
[DataField]
14+
public float MaxSendDistance = 50f;
15+
16+
[DataField]
17+
public ProtoId<LocalizedDatasetPrototype> FalloffInterjectionDataset = "ESRadioFalloffInterjections";
18+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Text;
2+
using Content.Server._ES.Radio.Components;
3+
using Content.Server.Radio;
4+
using Robust.Server.GameObjects;
5+
using Robust.Shared.Prototypes;
6+
using Robust.Shared.Random;
7+
8+
namespace Content.Server._ES.Radio;
9+
10+
public sealed class ESRadioSystem : EntitySystem
11+
{
12+
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
13+
[Dependency] private readonly IRobustRandom _random = default!;
14+
[Dependency] private readonly TransformSystem _transform = default!;
15+
16+
/// <inheritdoc/>
17+
public override void Initialize()
18+
{
19+
SubscribeLocalEvent<ESRadioFalloffComponent, RadioReceiveAttemptEvent>(OnFalloffReceiveAttempt);
20+
}
21+
22+
private void OnFalloffReceiveAttempt(Entity<ESRadioFalloffComponent> ent, ref RadioReceiveAttemptEvent args)
23+
{
24+
if (args.Cancelled)
25+
return;
26+
27+
if (_transform.InRange(args.RadioSource, args.RadioReceiver, ent.Comp.MaxSendDistance))
28+
return;
29+
args.Cancelled = true;
30+
}
31+
32+
public string DistortMessage(Entity<ESRadioFalloffComponent?> receiver, EntityUid sender, string message)
33+
{
34+
var msg = message;
35+
36+
if (!Resolve(receiver, ref receiver.Comp, false))
37+
return msg;
38+
39+
var receiverXform = Transform(receiver);
40+
var senderXform = Transform(sender);
41+
42+
if (!receiverXform.Coordinates.TryDistance(EntityManager, _transform, senderXform.Coordinates, out var distance))
43+
return msg;
44+
45+
var distortion = (distance - receiver.Comp.MaxClearSendDistance) / (receiver.Comp.MaxSendDistance - receiver.Comp.MaxClearSendDistance);
46+
if (distortion < 0) // only distort if we're in the range.
47+
return msg;
48+
49+
var muffleChance = MathHelper.Lerp(0.05f, 0.4f, distortion);
50+
51+
var outputMsg = new StringBuilder();
52+
foreach (var letter in msg.AsSpan())
53+
{
54+
if (!char.IsLetterOrDigit(letter))
55+
{
56+
outputMsg.Append(letter);
57+
continue;
58+
}
59+
60+
if (_random.Prob(muffleChance))
61+
outputMsg.Append('~');
62+
else
63+
outputMsg.Append(letter);
64+
}
65+
66+
msg = outputMsg.ToString();
67+
outputMsg.Clear();
68+
69+
var interjectionChance = Math.Clamp(MathHelper.Lerp(-0.1f, 0.5f, distortion), 0, 1);
70+
var interjection = _prototypeManager.Index(receiver.Comp.FalloffInterjectionDataset);
71+
foreach (var word in msg.Split(' '))
72+
{
73+
if (_random.Prob(interjectionChance))
74+
{
75+
outputMsg.Append(Loc.GetString(_random.Pick(interjection.Values)));
76+
outputMsg.Append(' ');
77+
}
78+
79+
outputMsg.Append(word);
80+
outputMsg.Append(' ');
81+
}
82+
83+
return outputMsg.ToString().TrimEnd();
84+
}
85+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
es-radio-falloff-interjection-1 = [italic]--BZZT--[/italic]
2+
es-radio-falloff-interjection-2 = [italic]--KRSSH--[/italic]
3+
es-radio-falloff-interjection-3 = [italic]--CRACKLE--[/italic]
4+
es-radio-falloff-interjection-4 = [italic]--PSSSHT--[/italic]
5+
es-radio-falloff-interjection-5 = [italic]--PZZAT--[/italic]

Resources/Prototypes/Entities/Clothing/Ears/headsets.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
- type: GuideHelp
2727
guides:
2828
- Radio
29+
# ES START
30+
- type: ESRadioFalloff
31+
# ES END
2932

3033
- type: entity
3134
parent: ClothingHeadset
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- type: localizedDataset
2+
id: ESRadioFalloffInterjections
3+
values:
4+
prefix: es-radio-falloff-interjection-
5+
count: 5

0 commit comments

Comments
 (0)