Save seed data in components and remove the seed-database (#7499)

This commit is contained in:
Leon Friedrich
2022-04-16 17:32:35 +12:00
committed by GitHub
parent 98e7c84dad
commit 6997bd83b2
16 changed files with 225 additions and 232 deletions

View File

@@ -87,7 +87,7 @@ namespace Content.Server.Botany.Components
public float WeedCoefficient { get; set; } = 1f;
[ViewVariables(VVAccess.ReadWrite)]
public SeedPrototype? Seed { get; set; }
public SeedData? Seed { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public bool ImproperHeat { get; set; }
@@ -613,15 +613,14 @@ namespace Content.Server.Botany.Components
appearanceComponent.SetData(PlantHolderVisuals.HarvestLight, Harvest);
}
public void CheckForDivergence(bool modified)
/// <summary>
/// Check if the currently contained seed is unique. If it is not, clone it so that we have a unique seed.
/// Necessary to avoid modifying global seeds.
/// </summary>
public void EnsureUniqueSeed()
{
// Make sure we're not modifying a "global" seed.
// If this seed is not in the global seed list, then no products of this line have been harvested yet.
// It is then safe to assume it's restricted to this tray.
if (Seed == null) return;
var plantSystem = EntitySystem.Get<BotanySystem>();
if (plantSystem.Seeds.ContainsKey(Seed.Uid))
Seed = Seed.Diverge(modified);
if (Seed != null && !Seed.Unique)
Seed = Seed.Clone();
}
public void ForceUpdateByExternalCause()

View File

@@ -1,7 +1,5 @@
using Content.Server.Botany.Systems;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Botany.Components;
@@ -11,5 +9,15 @@ public sealed class ProduceComponent : Component
{
[DataField("targetSolution")] public string SolutionName { get; set; } = "food";
[DataField("seed", required: true)] public string SeedName = default!;
/// <summary>
/// Seed data used to create a <see cref="SeedComponent"/> when this produce has its seeds extracted.
/// </summary>
[DataField("seed")]
public SeedData? Seed;
/// <summary>
/// Seed data used to create a <see cref="SeedComponent"/> when this produce has its seeds extracted.
/// </summary>
[DataField("seedId", customTypeSerializer: typeof(PrototypeIdSerializer<SeedPrototype>))]
public readonly string? SeedId;
}

View File

@@ -1,7 +1,4 @@
using Content.Server.Botany.Systems;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Botany.Components
@@ -9,7 +6,18 @@ namespace Content.Server.Botany.Components
[RegisterComponent, Friend(typeof(BotanySystem))]
public sealed class SeedComponent : Component
{
[DataField("seed", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<SeedPrototype>))]
public string SeedName = default!;
/// <summary>
/// Seed data containing information about the plant type & properties that this seed can grow seed. If
/// null, will instead attempt to get data from a seed prototype, if one is defined. See <see
/// cref="SeedId"/>.
/// </summary>
[DataField("seed")]
public SeedData? Seed;
/// <summary>
/// Name of a base seed prototype that is used if <see cref="Seed"/> is null.
/// </summary>
[DataField("seedId", customTypeSerializer:typeof(PrototypeIdSerializer<SeedPrototype>))]
public readonly string? SeedId;
}
}

View File

@@ -1,25 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Botany.Components;
using Content.Server.Botany.Systems;
using Content.Shared.Atmos;
using Content.Shared.Popups;
using Content.Shared.Random.Helpers;
using Content.Shared.Tag;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
using Robust.Shared.Utility;
namespace Content.Server.Botany;
[Prototype("seed")]
public sealed class SeedPrototype : SeedData, IPrototype
{
[IdDataField] public string ID { get; private init; } = default!;
}
public enum HarvestType : byte
{
NoRepeat,
@@ -65,33 +61,53 @@ public struct SeedChemQuantity
[DataField("PotencyDivisor")] public int PotencyDivisor;
}
[Prototype("seed")]
public sealed class SeedPrototype : IPrototype
// TODO reduce the number of friends to a reasonable level. Requires ECS-ing things like plant holder component.
[Virtual, DataDefinition]
[Friend(typeof(BotanySystem), typeof(PlantHolderSystem), typeof(SeedExtractorSystem), typeof(PlantHolderComponent))]
public class SeedData
{
public const string Prototype = "SeedBase";
[IdDataFieldAttribute] public string ID { get; private init; } = default!;
#region Tracking
/// <summary>
/// The name of this seed. Determines the name of seed packets.
/// </summary>
[DataField("name")] public string Name = string.Empty;
/// <summary>
/// Unique identifier of this seed. Do NOT set this.
/// The noun for this type of seeds. E.g. for fungi this should probably be "spores" instead of "seeds". Also
/// used to determine the name of seed packets.
/// </summary>
public int Uid { get; internal set; } = -1;
[DataField("noun")] public string Noun = "seeds";
#region Tracking
[DataField("name")] public string Name = string.Empty;
[DataField("seedName")] public string SeedName = string.Empty;
[DataField("seedNoun")] public string SeedNoun = "seeds";
/// <summary>
/// Name displayed when examining the hydroponics tray. Describes the actual plant, not the seed itself.
/// </summary>
[DataField("displayName")] public string DisplayName = string.Empty;
[DataField("roundStart")] public bool RoundStart = true;
[DataField("mysterious")] public bool Mysterious;
/// <summary>
/// If true, the properties of this seed cannot be modified.
/// </summary>
[DataField("immutable")] public bool Immutable;
/// <summary>
/// If true, there is only a single reference to this seed and it's properties can be directly modified without
/// needing to clone the seed.
/// </summary>
[ViewVariables]
public bool Unique = false; // seed-prototypes or yaml-defined seeds for entity prototypes will not generally be unique.
#endregion
#region Output
/// <summary>
/// The entity prototype that is spawned when this type of seed is extracted from produce using a seed extractor.
/// </summary>
[DataField("packetPrototype", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public string PacketPrototype = "SeedBase";
/// <summary>
/// The entity prototype this seed spawns when it gets harvested.
/// </summary>
[DataField("productPrototypes", customTypeSerializer: typeof(PrototypeIdListSerializer<EntityPrototype>))]
public List<string> ProductPrototypes = new();
@@ -137,7 +153,13 @@ public sealed class SeedPrototype : IPrototype
[DataField("potency")] public float Potency = 1f;
/// <summary>
/// If true, a sharp tool is required to harvest this plant.
/// </summary>
[DataField("ligneous")] public bool Ligneous;
// No, I'm not removing these.
// if you re-add these, make sure that they get cloned.
//public PlantSpread Spread { get; set; }
//public PlantMutation Mutation { get; set; }
//public float AlterTemperature { get; set; }
@@ -146,8 +168,6 @@ public sealed class SeedPrototype : IPrototype
//public bool Hematophage { get; set; }
//public bool Thorny { get; set; }
//public bool Stinging { get; set; }
[DataField("ligneous")] public bool Ligneous;
// public bool Teleporting { get; set; }
// public PlantJuicy Juicy { get; set; }
@@ -168,17 +188,18 @@ public sealed class SeedPrototype : IPrototype
#endregion
public SeedPrototype Clone()
public SeedData Clone()
{
var newSeed = new SeedPrototype
DebugTools.Assert(!Immutable, "There should be no need to clone an immutable seed.");
var newSeed = new SeedData
{
ID = ID,
Name = Name,
SeedName = SeedName,
SeedNoun = SeedNoun,
RoundStart = RoundStart,
Noun = Noun,
DisplayName = DisplayName,
Mysterious = Mysterious,
PacketPrototype = PacketPrototype,
ProductPrototypes = new List<string>(ProductPrototypes),
Chemicals = new Dictionary<string, SeedChemQuantity>(Chemicals),
ConsumeGasses = new Dictionary<Gas, float>(ConsumeGasses),
@@ -209,14 +230,14 @@ public sealed class SeedPrototype : IPrototype
PlantIconState = PlantIconState,
Bioluminescent = Bioluminescent,
BioluminescentColor = BioluminescentColor,
SplatPrototype = SplatPrototype
SplatPrototype = SplatPrototype,
Ligneous = Ligneous,
// Newly cloned seed is unique. No need to unnecessarily clone if repeatedly modified.
Unique = true,
};
return newSeed;
}
public SeedPrototype Diverge(bool modified)
{
return Clone();
}
}

View File

@@ -1,12 +1,8 @@
using System.Collections.Generic;
using Content.Server.Botany.Components;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Popups;
using Content.Shared.GameTicking;
using Content.Shared.Tag;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
@@ -20,49 +16,13 @@ namespace Content.Server.Botany.Systems
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
private int _nextUid = 0;
private float _timer = 0f;
public readonly Dictionary<int, SeedPrototype> Seeds = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
InitializeSeeds();
PopulateDatabase();
}
private void PopulateDatabase()
{
_nextUid = 0;
Seeds.Clear();
foreach (var seed in _prototypeManager.EnumeratePrototypes<SeedPrototype>())
{
seed.Uid = GetNextSeedUid();
Seeds[seed.Uid] = seed;
}
}
public bool AddSeedToDatabase(SeedPrototype seed)
{
// If it's not -1, it's already in the database. Probably.
if (seed.Uid != -1)
return false;
seed.Uid = GetNextSeedUid();
Seeds[seed.Uid] = seed;
return true;
}
private int GetNextSeedUid()
{
return _nextUid++;
}
public override void Update(float frameTime)
@@ -80,10 +40,5 @@ namespace Content.Server.Botany.Systems
plantHolder.Update();
}
}
public void Reset(RoundRestartCleanupEvent ev)
{
PopulateDatabase();
}
}
}

View File

@@ -1,8 +1,6 @@
using Content.Server.Botany.Components;
using Content.Server.Botany.Components;
using Content.Shared.FixedPoint;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
namespace Content.Server.Botany.Systems;
@@ -10,7 +8,7 @@ public sealed partial class BotanySystem
{
public void ProduceGrown(EntityUid uid, ProduceComponent produce)
{
if (!_prototypeManager.TryIndex<SeedPrototype>(produce.SeedName, out var seed))
if (!TryGetSeed(produce, out var seed))
return;
if (TryComp(uid, out SpriteComponent? sprite))

View File

@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Botany.Components;
using Content.Server.Kitchen.Components;
@@ -26,35 +27,64 @@ public sealed partial class BotanySystem
SubscribeLocalEvent<SeedComponent, ExaminedEvent>(OnExamined);
}
public bool TryGetSeed(SeedComponent comp, [NotNullWhen(true)] out SeedData? seed)
{
if (comp.Seed != null)
{
seed = comp.Seed;
return true;
}
if (comp.SeedId != null
&& _prototypeManager.TryIndex(comp.SeedId, out SeedPrototype? protoSeed))
{
seed = protoSeed;
return true;
}
seed = null;
return false;
}
public bool TryGetSeed(ProduceComponent comp, [NotNullWhen(true)] out SeedData? seed)
{
if (comp.Seed != null)
{
seed = comp.Seed;
return true;
}
if (comp.SeedId != null
&& _prototypeManager.TryIndex(comp.SeedId, out SeedPrototype? protoSeed))
{
seed = protoSeed;
return true;
}
seed = null;
return false;
}
private void OnExamined(EntityUid uid, SeedComponent component, ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
if (!_prototypeManager.TryIndex<SeedPrototype>(component.SeedName, out var seed))
if (!TryGetSeed(component, out var seed))
return;
args.PushMarkup(Loc.GetString($"seed-component-description", ("seedName", seed.DisplayName)));
if (!seed.RoundStart)
{
args.PushMarkup(Loc.GetString($"seed-component-has-variety-tag", ("seedUid", seed.Uid)));
}
else
{
args.PushMarkup(Loc.GetString($"seed-component-plant-yield-text", ("seedYield", seed.Yield)));
args.PushMarkup(Loc.GetString($"seed-component-plant-potency-text", ("seedPotency", seed.Potency)));
}
args.PushMarkup(Loc.GetString($"seed-component-plant-yield-text", ("seedYield", seed.Yield)));
args.PushMarkup(Loc.GetString($"seed-component-plant-potency-text", ("seedPotency", seed.Potency)));
}
#region SeedPrototype prototype stuff
public EntityUid SpawnSeedPacket(SeedPrototype proto, EntityCoordinates transformCoordinates)
public EntityUid SpawnSeedPacket(SeedData proto, EntityCoordinates transformCoordinates)
{
var seed = Spawn(SeedPrototype.Prototype, transformCoordinates);
var seed = Spawn(proto.PacketPrototype, transformCoordinates);
var seedComp = EnsureComp<SeedComponent>(seed);
seedComp.SeedName = proto.ID;
seedComp.Seed = proto;
if (TryComp(seed, out SpriteComponent? sprite))
{
@@ -63,13 +93,13 @@ public sealed partial class BotanySystem
sprite.LayerSetSprite(0, new SpriteSpecifier.Rsi(proto.PlantRsi, "seed"));
}
string val = Loc.GetString("botany-seed-packet-name", ("seedName", proto.SeedName), ("seedNoun", proto.SeedNoun));
string val = Loc.GetString("botany-seed-packet-name", ("seedName", proto.Name), ("seedNoun", proto.Noun));
MetaData(seed).EntityName = val;
return seed;
}
public IEnumerable<EntityUid> AutoHarvest(SeedPrototype proto, EntityCoordinates position, int yieldMod = 1)
public IEnumerable<EntityUid> AutoHarvest(SeedData proto, EntityCoordinates position, int yieldMod = 1)
{
if (position.IsValid(EntityManager) &&
proto.ProductPrototypes.Count > 0)
@@ -78,10 +108,8 @@ public sealed partial class BotanySystem
return Enumerable.Empty<EntityUid>();
}
public IEnumerable<EntityUid> Harvest(SeedPrototype proto, EntityUid user, int yieldMod = 1)
public IEnumerable<EntityUid> Harvest(SeedData proto, EntityUid user, int yieldMod = 1)
{
if (AddSeedToDatabase(proto)) proto.Name = proto.Uid.ToString();
if (proto.ProductPrototypes.Count == 0 || proto.Yield <= 0)
{
_popupSystem.PopupCursor(Loc.GetString("botany-harvest-fail-message"),
@@ -94,16 +122,13 @@ public sealed partial class BotanySystem
return GenerateProduct(proto, Transform(user).Coordinates, yieldMod);
}
public IEnumerable<EntityUid> GenerateProduct(SeedPrototype proto, EntityCoordinates position, int yieldMod = 1)
public IEnumerable<EntityUid> GenerateProduct(SeedData proto, EntityCoordinates position, int yieldMod = 1)
{
var totalYield = 0;
if (proto.Yield > -1)
{
if (yieldMod < 0)
{
yieldMod = 1;
totalYield = proto.Yield;
}
else
totalYield = proto.Yield * yieldMod;
@@ -112,6 +137,9 @@ public sealed partial class BotanySystem
var products = new List<EntityUid>();
if (totalYield > 1 || proto.HarvestRepeat != HarvestType.NoRepeat)
proto.Unique = false;
for (var i = 0; i < totalYield; i++)
{
var product = _robustRandom.Pick(proto.ProductPrototypes);
@@ -122,7 +150,7 @@ public sealed partial class BotanySystem
var produce = EnsureComp<ProduceComponent>(entity);
produce.SeedName = proto.ID;
produce.Seed = proto;
ProduceGrown(entity, produce);
if (TryComp<AppearanceComponent>(entity, out var appearance))
@@ -141,7 +169,7 @@ public sealed partial class BotanySystem
return products;
}
public bool CanHarvest(SeedPrototype proto, EntityUid? held = null)
public bool CanHarvest(SeedData proto, EntityUid? held = null)
{
return !proto.Ligneous || proto.Ligneous && held != null && HasComp<SharpComponent>(held);
}

View File

@@ -98,12 +98,12 @@ namespace Content.Server.Botany.Systems
{
if (component.Seed == null)
{
if (!_prototypeManager.TryIndex<SeedPrototype>(seeds.SeedName, out var seed))
return;
if (!_botanySystem.TryGetSeed(seeds, out var seed))
return ;
_popupSystem.PopupCursor(Loc.GetString("plant-holder-component-plant-success-message",
("seedName", seed.SeedName),
("seedNoun", seed.SeedNoun)), Filter.Entities(args.User));
("seedName", seed.Name),
("seedNoun", seed.Noun)), Filter.Entities(args.User));
component.Seed = seed;
component.Dead = false;
@@ -215,6 +215,7 @@ namespace Content.Server.Botany.Systems
return;
}
component.Seed.Unique = false;
var seed = _botanySystem.SpawnSeedPacket(component.Seed, Transform(args.User).Coordinates);
seed.RandomOffset(0.25f);
_popupSystem.PopupCursor(Loc.GetString("plant-holder-component-take-sample-message",

View File

@@ -1,4 +1,4 @@
using Content.Server.Botany.Components;
using Content.Server.Botany.Components;
using Content.Server.Popups;
using Content.Server.Power.Components;
using Content.Shared.Interaction;
@@ -14,7 +14,6 @@ namespace Content.Server.Botany.Systems;
public sealed class SeedExtractorSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly BotanySystem _botanySystem = default!;
@@ -32,7 +31,7 @@ public sealed class SeedExtractorSystem : EntitySystem
if (TryComp(args.Used, out ProduceComponent? produce))
{
if (!_prototypeManager.TryIndex<SeedPrototype>(produce.SeedName, out var seed))
if (!_botanySystem.TryGetSeed(produce, out var seed))
return;
_popupSystem.PopupCursor(Loc.GetString("seed-extractor-component-interact-message",("name", args.Used)),
@@ -43,6 +42,9 @@ public sealed class SeedExtractorSystem : EntitySystem
var random = _random.Next(component.MinSeeds, component.MaxSeeds);
var coords = Transform(uid).Coordinates;
if (random > 1)
seed.Unique = false;
for (var i = 0; i < random; i++)
{
_botanySystem.SpawnSeedPacket(seed, coords);