Files
tbd-station-14/Content.Server/Vocalization/Systems/VocalizationSystem.cs
Crude Oil 9ebf6a24c4 Parroting Parrots part 1: Help maints! SQUAWK! Maints! (#38243)
* parrots have ears. add poly

* high tech parrot functionality

* adjust times

* add accent to radio message

* don't spam everything all at once probably

* learn about the existence of prob(float)

* actually use Prob(float) correctly

* newline

* add pet spawner for poly

* move chance to talk on radio to component

* missing comment

* minor edits and doc additions

* the reviewerrrrrrr

* parrot can't learn when crit or dead

* increase default memory

* rename poly to polly

* crude way to ignore whispers. chatcode please

* This is Polly. It is set to broadcast over the engineering frequency

* add missing initialize

* add displacement map for parrot ears

* review comments - Errant

* minor things

* large rework

* fix attempting to talk when entity has no channels

* use list of active radios again to track channels

* fix bad return, some comments

* fix long learn cooldown

* minor adjustments

* use FromMinutes

* the voices told me to make these changes

* remove default reassignment

* Review changes

* remove polly's accent

* decouple radio stuff from parrotsystem

* minor stuff

* split vocalization and parroting

* minor review work

* re-add missing check

* add admin verb for clearing parrot messages

* minor action icon update

* oops

* increase icon number text size

* Admin erase parrot messages associated with players

* part 1 beck review

* add whitelist and blacklist for parrots

* Downgrade missing component error to warning

* Add comment

* add some missing comments

* Remove active radio entity tracking, use all inventory slots

* Minor changes

* small review stuff

* review radio stuff

* swap ears displacement to invisible death displacement

* remove syncsprite

* vscode why do yo have to hurt my feelings

* review changes

* use checkboth
2025-07-09 12:04:57 -07:00

112 lines
4.5 KiB
C#

using Content.Server.Chat.Systems;
using Content.Server.Vocalization.Components;
using Content.Shared.ActionBlocker;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.Vocalization.Systems;
/// <summary>
/// VocalizationSystem raises VocalizeEvents to make entities speak at certain intervals
/// This is used in combination with systems like ParrotMemorySystem to randomly say messages from memory,
/// or can be used by other systems to speak pre-set messages
/// </summary>
public sealed partial class VocalizationSystem : EntitySystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly ChatSystem _chat = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IRobustRandom _random = default!;
/// <summary>
/// Try speaking by raising a TryVocalizeEvent
/// This event is passed to systems adding a message to it and setting it to handled
/// </summary>
private void TrySpeak(Entity<VocalizerComponent> entity)
{
var tryVocalizeEvent = new TryVocalizeEvent();
RaiseLocalEvent(entity.Owner, ref tryVocalizeEvent);
// if the event was never handled, return
// this happens if there are no components that trigger systems to add a message to this event
if (!tryVocalizeEvent.Handled)
return;
// if the event's message is null for whatever reason, return.
// this would mean a system didn't set the message properly but did set the event to handled
if (tryVocalizeEvent.Message is not { } message)
return;
Speak(entity, message);
}
/// <summary>
/// Actually say something.
/// </summary>
private void Speak(Entity<VocalizerComponent> entity, string message)
{
// raise a VocalizeEvent
// this can be handled by other systems to speak using a method other than local chat
var vocalizeEvent = new VocalizeEvent(message);
RaiseLocalEvent(entity.Owner, ref vocalizeEvent);
// if the event is handled, don't try speaking
if (vocalizeEvent.Handled)
return;
// default to local chat if no other system handles the event
// first check if the entity can speak
if (!_actionBlocker.CanSpeak(entity))
return;
// send the message
_chat.TrySendInGameICMessage(entity, message, InGameICChatType.Speak, ChatTransmitRange.Normal);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
// get current game time for delay
var currentGameTime = _gameTiming.CurTime;
// query to get all entities with a VocalizeComponent
var query = EntityQueryEnumerator<VocalizerComponent>();
while (query.MoveNext(out var uid, out var vocalizer))
{
// go to next entity if it is too early for this one to speak
if (currentGameTime < vocalizer.NextVocalizeInterval)
continue;
// set a new time for the speak interval, regardless of whether speaking works
var randomSpeakInterval = _random.Next(vocalizer.MinVocalizeInterval, vocalizer.MaxVocalizeInterval);
vocalizer.NextVocalizeInterval += randomSpeakInterval;
// if an admin updates the speak interval to be immediate, this loop will spam messages until the
// nextspeakinterval catches up with the current game time. Prevent this from happening
if (vocalizer.NextVocalizeInterval < _gameTiming.CurTime)
vocalizer.NextVocalizeInterval = _gameTiming.CurTime + randomSpeakInterval;
// try to speak
TrySpeak((uid, vocalizer));
}
}
}
/// <summary>
/// Fired when the entity wants to try vocalizing, but doesn't have a message yet
/// </summary>
/// <param name="Message">Message to send, this is null when the event is just fired and should be set by a system</param>
/// <param name="Handled">Whether the message was handled by a system</param>
[ByRefEvent]
public record struct TryVocalizeEvent(string? Message = null, bool Handled = false);
/// <summary>
/// Fired when the entity wants to vocalize and has a message. Allows for interception by other systems if the
/// vocalization needs to be done some other way
/// </summary>
/// <param name="Message">Message to send</param>
/// <param name="Handled">Whether the message was handled by a system</param>
[ByRefEvent]
public record struct VocalizeEvent(string Message, bool Handled = false);