почти важно (#135)
* Add data field for id card microwave behaviour (#28087) * Move replacement chance from ReplacementAccentComponent to ReplacementAccentPrototype (#28049) Move replacement chance to ReplacementAccentPrototype * Resolves Bible summon message being sent to all users (#28104) * Changed PopupEntity overload used to ensure message is only sent to user * Updated uid for PopupEntity call * Updating _popupSystem.PopupEntity call in AttemptSummon * Random book story generator refactor (#28082) * Randomized book overhaul * Fix prototype names * Improved setting paper content * Praise Ratvar * Fix activatable UI popup message spam (#28123) Fixed activatable UI popup message spam * Modify battery assert to avoid floating point errors (#28007) * Update component query benchmarks (#27967) * Add more component query benchmarks. * Rename benchmark * Use non-generic `TryComp()` for metadata & transform (#28133) * sleeper agent appear later into the round and only once (#28160) --------- Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com> Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com> Co-authored-by: double_b <40827162+benjamin-burges@users.noreply.github.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Co-authored-by: Killerqu00 <47712032+Killerqu00@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Robust.Shared.Collections;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Shared.StoryGen;
|
||||
|
||||
/// <summary>
|
||||
/// Provides functionality to generate a story from a <see cref="StoryTemplatePrototype"/>.
|
||||
/// </summary>
|
||||
public sealed partial class StoryGeneratorSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Tries to generate a random story using the given template, picking a random word from the referenced
|
||||
/// datasets for each variable and passing them into the localization system with template.
|
||||
/// If <paramref name="seed"/> is specified, the randomizer will be seeded with it for consistent story generation;
|
||||
/// otherwise the variables will be randomized.
|
||||
/// Fails if the template prototype cannot be loaded.
|
||||
/// </summary>
|
||||
/// <returns>true if the template was loaded, otherwise false.</returns>
|
||||
public bool TryGenerateStoryFromTemplate(ProtoId<StoryTemplatePrototype> template, [NotNullWhen(true)] out string? story, int? seed = null)
|
||||
{
|
||||
// Get the story template prototype from the ID
|
||||
if (!_protoMan.TryIndex(template, out var templateProto))
|
||||
{
|
||||
story = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// If given a seed, use it
|
||||
if (seed != null)
|
||||
_random.SetSeed(seed.Value);
|
||||
|
||||
// Pick values for all of the variables in the template
|
||||
var variables = new ValueList<(string, object)>(templateProto.Variables.Count);
|
||||
foreach (var (name, list) in templateProto.Variables)
|
||||
{
|
||||
// Get the prototype for the world list dataset
|
||||
if (!_protoMan.TryIndex(list, out var listProto))
|
||||
continue; // Missed one, but keep going with the rest of the story
|
||||
|
||||
// Pick a random word from the dataset and localize it
|
||||
var chosenWord = Loc.GetString(_random.Pick(listProto.Values));
|
||||
variables.Add((name, chosenWord));
|
||||
}
|
||||
|
||||
// Pass the variables to the localization system and build the story
|
||||
story = Loc.GetString(templateProto.LocId, variables.ToArray());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
33
Content.Shared/StoryGen/Prototypes/StoryTemplatePrototype.cs
Normal file
33
Content.Shared/StoryGen/Prototypes/StoryTemplatePrototype.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Content.Shared.Dataset;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.StoryGen;
|
||||
|
||||
/// <summary>
|
||||
/// Prototype for a story template that can be filled in with words chosen from <see cref="DatasetPrototype"/>s.
|
||||
/// </summary>
|
||||
[Serializable, Prototype("storyTemplate")]
|
||||
public sealed partial class StoryTemplatePrototype : IPrototype
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifier for this prototype instance.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[IdDataField]
|
||||
public string ID { get; private set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Localization ID of the Fluent string that forms the structure of this story.
|
||||
/// </summary>
|
||||
[DataField(required: true)]
|
||||
public LocId LocId { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Dictionary containing the name of each variable to pass to the template and the ID of the
|
||||
/// <see cref="DatasetPrototype"/> from which a random entry will be selected as its value.
|
||||
/// For example, <c>name: book_character</c> will pick a random entry from the book_character
|
||||
/// dataset which can then be used in the template by <c>{$name}</c>.
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public Dictionary<string, ProtoId<DatasetPrototype>> Variables { get; } = default!;
|
||||
}
|
||||
Reference in New Issue
Block a user