using Content.Shared.Dataset; using JetBrains.Annotations; 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!; [Dependency] private readonly MetaDataSystem _metaData = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(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); if (component.NameSegments != null) { _metaData.SetEntityName(uid, GetRandomFromSegments(component.NameSegments, component.NameSeparator), meta); } if (component.DescriptionSegments != null) { _metaData.SetEntityDescription(uid, GetRandomFromSegments(component.DescriptionSegments, component.DescriptionSeparator), meta); } } /// /// Generates a random string from segments and a separator. /// /// The segments that it will be generated from /// The separator that will be inbetween each segment /// The newly generated string [PublicAPI] public string GetRandomFromSegments(List segments, string? separator) { var outputSegments = new List(); foreach (var segment in segments) { outputSegments.Add(_prototype.TryIndex(segment, out var proto) ? Loc.GetString(_random.Pick(proto.Values)) : Loc.GetString(segment)); } return string.Join(separator, outputSegments); } }