Files
wwdpublic/Content.Server/RandomMetadata/RandomMetadataSystem.cs
VMSolidus 08822e34ba Cherry-Pick Antag Refactor (#734)
This cherry-pick's Wizden's Antag Refactor, which is needed for all
future antag updates, as well as for me to cherry-pick over Cultists(Who
are going to need some editing to fit the antag refactor), Changelings,
Heretics, and Wizards. I actually selected the White-Dream-Public
version of the Antag Refactor, due to it having commits made tailored to
our repository, so it comes prepackaged with all the changes necessary
for our repo-specific content.

https://github.com/frosty-dev/ss14-wwdp/pull/10

Signed-off-by: Timemaster99 <57200767+Timemaster99@users.noreply.github.com>
Co-authored-by: ThereDrD <88589686+ThereDrD0@users.noreply.github.com>
Co-authored-by: Jeff <velcroboy333@hotmail.com>
Co-authored-by: Timemaster99 <57200767+Timemaster99@users.noreply.github.com>
Co-authored-by: Timemaster99 <elijahrobot@gmail.com>
Co-authored-by: luckywill339@gmail.com <luckywill339@gmail.com>
Co-authored-by: Danger Revolution! <142105406+DangerRevolution@users.noreply.github.com>
Co-authored-by: Azzy <azzydev@icloud.com>
2024-08-15 10:11:48 -07:00

60 lines
2.2 KiB
C#

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<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);
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);
}
}
/// <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]
public string GetRandomFromSegments(List<string> segments, string? separator)
{
var outputSegments = new List<string>();
foreach (var segment in segments)
{
if (_prototype.TryIndex<DatasetPrototype>(segment, out var proto))
outputSegments.Add(_random.Pick(proto.Values));
else if (Loc.TryGetString(segment, out var localizedSegment))
outputSegments.Add(localizedSegment);
else
outputSegments.Add(segment);
}
return string.Join(separator, outputSegments);
}
}