Files
crystall-punk-14/Content.Shared/Lathe/SharedLatheSystem.cs

155 lines
4.9 KiB
C#
Raw Permalink Normal View History

using System.Diagnostics.CodeAnalysis;
using System.Linq;
2023-08-07 17:21:04 +03:00
using Content.Shared.Emag.Systems;
using Content.Shared.Examine;
using Content.Shared.Localizations;
2023-08-07 17:21:04 +03:00
using Content.Shared.Materials;
using Content.Shared.Research.Prototypes;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Lathe;
/// <summary>
/// This handles...
/// </summary>
public abstract class SharedLatheSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedMaterialStorageSystem _materialStorage = default!;
public readonly Dictionary<string, List<LatheRecipePrototype>> InverseRecipes = new();
2022-10-30 03:12:11 -04:00
public override void Initialize()
{
base.Initialize();
2023-08-07 17:21:04 +03:00
SubscribeLocalEvent<EmagLatheRecipesComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<LatheComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
BuildInverseRecipeDictionary();
2022-10-30 03:12:11 -04:00
}
private void OnExamined(Entity<LatheComponent> ent, ref ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
if (ent.Comp.ReagentOutputSlotId != null)
args.PushMarkup(Loc.GetString("lathe-menu-reagent-slot-examine"));
}
[PublicAPI]
public bool CanProduce(EntityUid uid, string recipe, int amount = 1, LatheComponent? component = null)
{
return _proto.TryIndex<LatheRecipePrototype>(recipe, out var proto) && CanProduce(uid, proto, amount, component);
}
public bool CanProduce(EntityUid uid, LatheRecipePrototype recipe, int amount = 1, LatheComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
if (!HasRecipe(uid, recipe, component))
return false;
foreach (var (material, needed) in recipe.Materials)
{
2023-01-15 21:57:59 +13:00
var adjustedAmount = AdjustMaterial(needed, recipe.ApplyMaterialDiscount, component.MaterialUseMultiplier);
if (_materialStorage.GetMaterialAmount(uid, material) < adjustedAmount * amount)
return false;
}
return true;
}
2023-08-07 17:21:04 +03:00
private void OnEmagged(EntityUid uid, EmagLatheRecipesComponent component, ref GotEmaggedEvent args)
{
args.Handled = true;
}
2023-01-15 21:57:59 +13:00
public static int AdjustMaterial(int original, bool reduce, float multiplier)
=> reduce ? (int) MathF.Ceiling(original * multiplier) : original;
protected abstract bool HasRecipe(EntityUid uid, LatheRecipePrototype recipe, LatheComponent component);
private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj)
{
if (!obj.WasModified<LatheRecipePrototype>())
return;
BuildInverseRecipeDictionary();
}
private void BuildInverseRecipeDictionary()
{
InverseRecipes.Clear();
foreach (var latheRecipe in _proto.EnumeratePrototypes<LatheRecipePrototype>())
{
if (latheRecipe.Result is not {} result)
continue;
InverseRecipes.GetOrNew(result).Add(latheRecipe);
}
}
public bool TryGetRecipesFromEntity(string prototype, [NotNullWhen(true)] out List<LatheRecipePrototype>? recipes)
{
recipes = new();
if (InverseRecipes.TryGetValue(prototype, out var r))
recipes.AddRange(r);
return recipes.Count != 0;
}
public string GetRecipeName(ProtoId<LatheRecipePrototype> proto)
{
return GetRecipeName(_proto.Index(proto));
}
public string GetRecipeName(LatheRecipePrototype proto)
{
if (!string.IsNullOrWhiteSpace(proto.Name))
return Loc.GetString(proto.Name);
if (proto.Result is {} result)
{
return _proto.Index(result).Name;
}
if (proto.ResultReagents is { } resultReagents)
{
return ContentLocalizationManager.FormatList(resultReagents
.Select(p => Loc.GetString("lathe-menu-result-reagent-display", ("reagent", _proto.Index(p.Key).LocalizedName), ("amount", p.Value)))
.ToList());
}
return string.Empty;
}
[PublicAPI]
public string GetRecipeDescription(ProtoId<LatheRecipePrototype> proto)
{
return GetRecipeDescription(_proto.Index(proto));
}
public string GetRecipeDescription(LatheRecipePrototype proto)
{
if (!string.IsNullOrWhiteSpace(proto.Description))
return Loc.GetString(proto.Description);
if (proto.Result is {} result)
{
return _proto.Index(result).Description;
}
if (proto.ResultReagents is { } resultReagents)
{
// We only use the first one for the description since these descriptions don't combine very well.
var reagent = resultReagents.First().Key;
return _proto.Index(reagent).LocalizedDescription;
}
return string.Empty;
}
}