Add strange pills, RandomFillSolutionComponent (#15067)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Random;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.Chemistry.Components.SolutionManager;
|
||||
|
||||
/// <summary>
|
||||
/// Fills a solution container randomly using a weighted random prototype
|
||||
/// </summary>
|
||||
[RegisterComponent, Access(typeof(SolutionRandomFillSystem))]
|
||||
public sealed class RandomFillSolutionComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Solution name which to add reagents to.
|
||||
/// </summary>
|
||||
[DataField("solution")]
|
||||
public string Solution { get; set; } = "default";
|
||||
|
||||
/// <summary>
|
||||
/// Weighted random prototype Id. Used to pick reagent.
|
||||
/// </summary>
|
||||
[DataField("weightedRandomId", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomPrototype>))]
|
||||
public string WeightedRandomId { get; set; } = "default";
|
||||
|
||||
/// <summary>
|
||||
/// Amount of reagent to add.
|
||||
/// </summary>
|
||||
[DataField("quantity")]
|
||||
public FixedPoint2 Quantity { get; set; } = 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Content.Server.Chemistry.Components.SolutionManager;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Content.Shared.Random;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Chemistry.EntitySystems;
|
||||
|
||||
public sealed class SolutionRandomFillSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!;
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<RandomFillSolutionComponent, MapInitEvent>(OnRandomSolutionFillMapInit);
|
||||
}
|
||||
|
||||
public void OnRandomSolutionFillMapInit(EntityUid uid, RandomFillSolutionComponent component, MapInitEvent args)
|
||||
{
|
||||
var target = _solutionsSystem.EnsureSolution(uid, component.Solution);
|
||||
var reagent = _proto.Index<WeightedRandomPrototype>(component.WeightedRandomId).Pick(_random);
|
||||
|
||||
if (!_proto.TryIndex<ReagentPrototype>(reagent, out ReagentPrototype? reagentProto))
|
||||
{
|
||||
Logger.Error(
|
||||
$"Tried to add invalid reagent Id {reagent} using SolutionRandomFill.");
|
||||
return;
|
||||
}
|
||||
|
||||
target.AddReagent(reagent, component.Quantity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user