Files
wwdpublic/Content.Server/Speech/Systems/RandomBarkSystem.cs
Mnemotechnican 4d222b98b3 Random Bark Revamp (#1003)
# Description
This improves the random barks by:
- Fully moving the random barks to localization files, with the ability
to re-use existing types
- Adding many new (english) random bark types
- Making the random bark system add a random punctuation mark at the end
of a random bark
- Adjusting some existing random bark configs

This also reparents MobSecurityDog to MobCorgi and MobArcticFox to
MobFox because for some reason delta-v decided to copy the original mobs
instead of using inheritance??? Either way, laika and siobhan will now
also have random barks, can be carried, etc.

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


![image](https://github.com/user-attachments/assets/2f429d8d-fa3a-4ac1-8f04-0eefa2541de3)

![image](https://github.com/user-attachments/assets/84a2bf32-07a5-4b7b-9afc-d077333ee620)

![image](https://github.com/user-attachments/assets/b4b099c3-4c59-444b-b2f8-f13d11d93f30)

![image](https://github.com/user-attachments/assets/f9d8de0d-06ff-4f9a-8358-e5dd38e9571d)


</p>
</details>

# Changelog
🆑
- add: Animals now have more unique things to say when not controlled by
a player.

---------

Signed-off-by: Mnemotechnican <69920617+Mnemotechnician@users.noreply.github.com>
Co-authored-by: FoxxoTrystan <45297731+FoxxoTrystan@users.noreply.github.com>
2024-10-19 13:41:34 +07:00

95 lines
3.3 KiB
C#

using Content.Server.Chat.Systems;
using Content.Shared.Mind.Components;
using Robust.Shared.Random;
using Content.Server.Speech.Components;
using Content.Shared.Chat;
namespace Content.Server.Speech.Systems;
public sealed class RandomBarkSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ChatSystem _chat = default!;
private static readonly string[] AddedPunctuation = [".", "...", "!", "..!", "!!"];
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RandomBarkComponent, MapInitEvent>(OnInit);
}
private void OnInit(Entity<RandomBarkComponent> ent, ref MapInitEvent args)
{
ent.Comp.BarkAccumulator = _random.NextFloat(ent.Comp.MinTime, ent.Comp.MaxTime) * ent.Comp.BarkMultiplier;
ent.Comp.BarkLocaleCount ??= GetBarkLocaleCount(ent);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<RandomBarkComponent>();
while (query.MoveNext(out var uid, out var barker))
{
barker.BarkAccumulator -= frameTime;
if (barker.BarkAccumulator > 0)
continue;
barker.BarkAccumulator = _random.NextFloat(barker.MinTime, barker.MaxTime) * barker.BarkMultiplier;
if (TryComp<MindContainerComponent>(uid, out var actComp) && actComp.HasMind
|| GetNextBark((uid, barker)) is not { } bark)
continue;
_chat.TrySendInGameICMessage(uid, bark, InGameICChatType.Speak, barker.ChatLog ? ChatTransmitRange.Normal : ChatTransmitRange.HideChat);
}
}
/// <summary>
/// Tries to get the next bark for the given entity. Returns null if it fails.
/// </summary>
public string? GetNextBark(Entity<RandomBarkComponent> ent)
{
var count = GetBarkLocaleCount(ent);
if (count <= 0)
return null;
var index = _random.Next(0, count) + 1;
if (!Loc.TryGetString($"bark-{ent.Comp.BarkType}-{index}", out var bark))
{
Log.Error($"Could not find bark with index {index} and type {ent.Comp.BarkType} for entity {ent.Owner}.");
return null;
}
// If the last char of the string is an alphanumeric one, then add a random punctuation mark
if (bark.Length > 0 && char.IsLetterOrDigit(bark[^1]))
bark += _random.Pick(AddedPunctuation);
return bark;
}
private int GetBarkLocaleCount(Entity<RandomBarkComponent> ent)
{
if (ent.Comp.BarkLocaleCount is { } localeCount)
return localeCount;
// All the error logging should cause certain integration tests to fail should someone setup randombark incorrectly
if (!Loc.TryGetString($"bark-{ent.Comp.BarkType}-count", out var localeCountStr))
{
Log.Error($"Entity {ent.Owner} has a bark type {ent.Comp.BarkType} which does not have a respective bark count locale.");
return 0;
}
if (!int.TryParse(localeCountStr, out localeCount) || localeCount < 0)
{
Log.Error($"Entity {ent.Owner} has a bark type {ent.Comp.BarkType} whose respective bark count locale is not a valid number.");
return 0;
}
return localeCount;
}
}