2024-04-24 21:31:45 -04:00
|
|
|
|
using Content.Shared.Dataset;
|
2024-06-06 02:37:49 -04:00
|
|
|
|
using Content.Shared.Random.Helpers;
|
2022-09-14 03:13:22 -04:00
|
|
|
|
using JetBrains.Annotations;
|
2022-05-02 13:51:03 -07:00
|
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
|
using Robust.Shared.Random;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.RandomMetadata;
|
|
|
|
|
|
|
|
|
|
|
|
public sealed class RandomMetadataSystem : EntitySystem
|
|
|
|
|
|
{
|
|
|
|
|
|
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
|
|
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2023-08-28 11:20:31 +02:00
|
|
|
|
[Dependency] private readonly MetaDataSystem _metaData = default!;
|
2022-05-02 13:51:03 -07:00
|
|
|
|
|
2025-04-05 16:56:22 +01:00
|
|
|
|
private List<string> _outputSegments = new();
|
|
|
|
|
|
|
2022-05-02 13:51:03 -07:00
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<RandomMetadataComponent, MapInitEvent>(OnMapInit);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This is done on map init so that map-placed entities have it randomized each time the map loads, for fun.
|
|
|
|
|
|
private void OnMapInit(EntityUid uid, RandomMetadataComponent component, MapInitEvent args)
|
|
|
|
|
|
{
|
|
|
|
|
|
var meta = MetaData(uid);
|
|
|
|
|
|
|
2022-09-14 03:13:22 -04:00
|
|
|
|
if (component.NameSegments != null)
|
2022-05-02 13:51:03 -07:00
|
|
|
|
{
|
2023-08-28 11:20:31 +02:00
|
|
|
|
_metaData.SetEntityName(uid, GetRandomFromSegments(component.NameSegments, component.NameSeparator), meta);
|
2022-05-02 13:51:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-14 03:13:22 -04:00
|
|
|
|
if (component.DescriptionSegments != null)
|
2022-05-02 13:51:03 -07:00
|
|
|
|
{
|
2023-08-28 11:20:31 +02:00
|
|
|
|
_metaData.SetEntityDescription(uid,
|
|
|
|
|
|
GetRandomFromSegments(component.DescriptionSegments, component.DescriptionSeparator), meta);
|
2022-05-02 13:51:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-09-14 03:13:22 -04:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Generates a random string from segments and a separator.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="segments">The segments that it will be generated from</param>
|
|
|
|
|
|
/// <param name="separator">The separator that will be inbetween each segment</param>
|
|
|
|
|
|
/// <returns>The newly generated string</returns>
|
|
|
|
|
|
[PublicAPI]
|
2025-04-05 16:56:22 +01:00
|
|
|
|
public string GetRandomFromSegments(List<ProtoId<LocalizedDatasetPrototype>> segments, string? separator)
|
2022-09-14 03:13:22 -04:00
|
|
|
|
{
|
2025-04-05 16:56:22 +01:00
|
|
|
|
_outputSegments.Clear();
|
2022-09-14 03:13:22 -04:00
|
|
|
|
foreach (var segment in segments)
|
|
|
|
|
|
{
|
2025-04-05 16:56:22 +01:00
|
|
|
|
var localizedProto = _prototype.Index(segment);
|
|
|
|
|
|
_outputSegments.Add(_random.Pick(localizedProto));
|
2022-09-14 03:13:22 -04:00
|
|
|
|
}
|
2025-04-05 16:56:22 +01:00
|
|
|
|
return string.Join(separator, _outputSegments);
|
2022-09-14 03:13:22 -04:00
|
|
|
|
}
|
2022-05-02 13:51:03 -07:00
|
|
|
|
}
|