| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using Content.Server.Botany.Components;
- using Content.Server.Kitchen.Components;
- using Content.Server.Popups;
- using Content.Shared.Chemistry.EntitySystems;
- using Content.Shared.Botany;
- using Content.Shared.Examine;
- using Content.Shared.Hands.EntitySystems;
- using Content.Shared.Popups;
- using Content.Shared.Random;
- using Content.Shared.Random.Helpers;
- using Robust.Server.GameObjects;
- using Robust.Shared.Map;
- using Robust.Shared.Prototypes;
- using Robust.Shared.Random;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using Content.Shared.Coordinates; // Added for Log
- using Robust.Shared.Utility; // Added for FormattedMessage
- namespace Content.Server.Botany.Systems;
- public sealed partial class BotanySystem : EntitySystem
- {
- [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
- [Dependency] private readonly IRobustRandom _robustRandom = default!;
- [Dependency] private readonly AppearanceSystem _appearance = default!;
- [Dependency] private readonly PopupSystem _popupSystem = default!;
- [Dependency] private readonly SharedHandsSystem _hands = default!;
- [Dependency] private readonly SharedSolutionContainerSystem _solutionContainerSystem = default!;
- [Dependency] private readonly MetaDataSystem _metaData = default!;
- [Dependency] private readonly RandomHelperSystem _randomHelper = default!;
- public override void Initialize()
- {
- base.Initialize();
- 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 (!TryGetSeed(component, out var seed))
- return;
- using (args.PushGroup(nameof(SeedComponent), 1))
- {
- var name = Loc.GetString(seed.DisplayName);
- args.PushMarkup(Loc.GetString($"seed-component-description", ("seedName", name)));
- 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
- /// <summary>
- /// Spawns a new seed packet on the floor at a position, then tries to put it in the user's hands if possible.
- /// </summary>
- public EntityUid SpawnSeedPacket(SeedData proto, EntityCoordinates coords, EntityUid user, float? healthOverride = null)
- {
- var seed = Spawn(proto.PacketPrototype, coords);
- var seedComp = EnsureComp<SeedComponent>(seed);
- seedComp.Seed = proto;
- seedComp.HealthOverride = healthOverride;
- var name = Loc.GetString(proto.Name);
- var noun = Loc.GetString(proto.Noun);
- var val = Loc.GetString("botany-seed-packet-name", ("seedName", name), ("seedNoun", noun));
- _metaData.SetEntityName(seed, val);
- // try to automatically place in user's other hand
- _hands.TryPickupAnyHand(user, seed);
- return seed;
- }
- public IEnumerable<EntityUid> AutoHarvest(SeedData proto, EntityCoordinates position, int yieldMod = 1)
- {
- if (position.IsValid(EntityManager) &&
- proto.ProductPrototypes.Count > 0)
- return GenerateProduct(proto, position, yieldMod);
- return Enumerable.Empty<EntityUid>();
- }
- public IEnumerable<EntityUid> Harvest(SeedData proto, EntityUid user, int yieldMod = 1)
- {
- if (proto.ProductPrototypes.Count == 0 || proto.Yield <= 0)
- {
- _popupSystem.PopupCursor(Loc.GetString("botany-harvest-fail-message"), user, PopupType.Medium);
- return Enumerable.Empty<EntityUid>();
- }
- var name = Loc.GetString(proto.DisplayName);
- _popupSystem.PopupCursor(Loc.GetString("botany-harvest-success-message", ("name", name)), user, PopupType.Medium);
- return GenerateProduct(proto, Transform(user).Coordinates, yieldMod);
- }
- public IEnumerable<EntityUid> GenerateProduct(SeedData proto, EntityCoordinates position, int yieldMod = 1)
- {
- var totalYield = 0;
- if (proto.Yield > -1)
- {
- if (yieldMod < 0)
- totalYield = proto.Yield;
- else
- totalYield = proto.Yield * yieldMod;
- totalYield = Math.Max(1, totalYield);
- }
- var products = new List<EntityUid>();
- // If yield is > 1 OR there's more than one product type OR harvest repeats, it's not unique.
- if (totalYield > 1 || proto.ProductPrototypes.Count > 1 || proto.HarvestRepeat != HarvestType.NoRepeat)
- proto.Unique = false;
- for (var i = 0; i < totalYield; i++)
- {
- // Iterate through ALL product prototypes defined in the seed data
- foreach (var productPrototypeId in proto.ProductPrototypes)
- {
- var entity = Spawn(productPrototypeId, position);
- _randomHelper.RandomOffset(entity, 0.25f);
- products.Add(entity);
- var produce = EnsureComp<ProduceComponent>(entity);
- produce.Seed = proto;
- ProduceGrown(entity, produce); // Apply specific produce logic (like solutions)
- _appearance.SetData(entity, ProduceVisuals.Potency, proto.Potency);
- if (proto.Mysterious)
- {
- var metaData = MetaData(entity);
- _metaData.SetEntityName(entity, metaData.EntityName + "?", metaData);
- _metaData.SetEntityDescription(entity,
- metaData.EntityDescription + " " + Loc.GetString("botany-mysterious-description-addon"), metaData);
- }
- }
- }
- return products;
- }
- public bool CanHarvest(SeedData proto, EntityUid? held = null)
- {
- return !proto.Ligneous || proto.Ligneous && held != null && HasComp<SharpComponent>(held);
- }
- #endregion
- }
|