2021-03-05 01:08:38 +01:00
|
|
|
#nullable enable
|
2021-02-11 01:13:03 -08:00
|
|
|
using System;
|
2020-09-21 12:47:52 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Log;
|
2021-03-15 13:25:18 +01:00
|
|
|
using Robust.Shared.Prototypes;
|
2020-09-21 12:47:52 +02:00
|
|
|
using Robust.Shared.Random;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager;
|
|
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-03-26 20:55:14 +01:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
2020-09-21 12:47:52 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Storage.Components
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2021-03-05 01:08:38 +01:00
|
|
|
public sealed class StorageFillComponent : Component, IMapInit
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
|
|
|
|
public override string Name => "StorageFill";
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("contents")]
|
|
|
|
|
private List<StorageFillEntry> _contents = new();
|
2020-09-21 12:47:52 +02:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
public IReadOnlyList<StorageFillEntry> Contents => _contents;
|
2020-09-21 12:47:52 +02:00
|
|
|
|
|
|
|
|
void IMapInit.MapInit()
|
|
|
|
|
{
|
|
|
|
|
if (_contents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
if (!Owner.TryGetComponent(out IStorageComponent? storage))
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
|
|
|
|
Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({Owner})");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-05 01:08:38 +01:00
|
|
|
|
2020-09-21 12:47:52 +02:00
|
|
|
var random = IoCManager.Resolve<IRobustRandom>();
|
|
|
|
|
|
|
|
|
|
var alreadySpawnedGroups = new List<string>();
|
|
|
|
|
foreach (var storageItem in _contents)
|
|
|
|
|
{
|
2021-05-21 21:30:03 +00:00
|
|
|
if (string.IsNullOrEmpty(storageItem.PrototypeId)) continue;
|
2020-09-25 13:49:59 +02:00
|
|
|
if (!string.IsNullOrEmpty(storageItem.GroupId) && alreadySpawnedGroups.Contains(storageItem.GroupId)) continue;
|
2020-09-21 12:47:52 +02:00
|
|
|
|
|
|
|
|
if (storageItem.SpawnProbability != 1f &&
|
|
|
|
|
!random.Prob(storageItem.SpawnProbability))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 21:28:06 +02:00
|
|
|
for (var i = 0; i < storageItem.Amount; i++)
|
|
|
|
|
{
|
2021-05-21 21:30:03 +00:00
|
|
|
storage.Insert(Owner.EntityManager.SpawnEntity(storageItem.PrototypeId, Owner.Transform.Coordinates));
|
2020-10-05 21:28:06 +02:00
|
|
|
}
|
2020-09-25 13:49:59 +02:00
|
|
|
if (!string.IsNullOrEmpty(storageItem.GroupId)) alreadySpawnedGroups.Add(storageItem.GroupId);
|
2020-09-21 12:47:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataDefinition]
|
|
|
|
|
public struct StorageFillEntry : IPopulateDefaultValues
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
2021-05-21 21:30:03 +00:00
|
|
|
[DataField("id", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
|
|
|
|
|
public string? PrototypeId;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
[DataField("prob")]
|
2020-09-21 12:47:52 +02:00
|
|
|
public float SpawnProbability;
|
2021-05-21 11:56:15 -07:00
|
|
|
/// <summary>
|
2021-05-21 21:30:03 +00:00
|
|
|
/// The probability that an item will spawn. Takes decimal form so 0.05 is 5%, 0.50 is 50% etc.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataField("orGroup")]
|
|
|
|
|
public string GroupId;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// orGroup signifies to pick between entities designated with an ID.
|
2021-05-21 11:56:15 -07:00
|
|
|
///
|
|
|
|
|
/// <example>
|
2021-05-21 21:30:03 +00:00
|
|
|
/// <para>To define an orGroup in a StorageFill component you
|
|
|
|
|
/// need to add it to the entities you want to choose between and
|
|
|
|
|
/// add a prob field. In this example there is a 50% chance the storage
|
|
|
|
|
/// spawns with Y or Z.
|
2021-06-09 22:19:39 +02:00
|
|
|
///
|
2021-05-21 11:56:15 -07:00
|
|
|
/// </para>
|
|
|
|
|
/// <code>
|
|
|
|
|
/// - type: StorageFill
|
2021-06-09 22:19:39 +02:00
|
|
|
/// contents:
|
2021-05-21 11:56:15 -07:00
|
|
|
/// - name: X
|
|
|
|
|
/// - name: Y
|
2021-05-21 21:30:03 +00:00
|
|
|
/// prob: 0.50
|
2021-05-21 11:56:15 -07:00
|
|
|
/// orGroup: YOrZ
|
|
|
|
|
/// - name: Z
|
|
|
|
|
/// orGroup: YOrZ
|
|
|
|
|
/// </code>
|
2021-06-09 22:19:39 +02:00
|
|
|
/// </example>
|
2021-05-21 11:56:15 -07:00
|
|
|
/// </summary>
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("amount")]
|
2020-09-21 12:47:52 +02:00
|
|
|
public int Amount;
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
public void PopulateDefaultValues()
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
Amount = 1;
|
|
|
|
|
SpawnProbability = 1;
|
2020-09-21 12:47:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|