Files
OldThink/Content.Shared/Lathe/SharedLatheSystem.cs
ThereDrD0 cdf16c6119 Фиксы (#228)
* fix: morgues and bodybags can store more than 1 entity

* fix: rename BluespaceSilo to BluespaceStorage

* fix: fix felinid`s missing sound

* fix: fix missing hatch airlock`s states

* add: translation for hatch airlocks

* add: texture localization for airlocks

* add: add new word to slang.json

* fix: fix wrong letter on cargo airlock`s texture

* fix: fix material whitelist part one

* git commit

* fix: fix lathe whitelist
2024-03-21 23:20:09 +03:00

100 lines
3.5 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Content.Shared._White.ShitSilo;
using Content.Shared.Emag.Systems;
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!;
private readonly Dictionary<string, List<LatheRecipePrototype>> _inverseRecipeDictionary = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EmagLatheRecipesComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
BuildInverseRecipeDictionary();
}
[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.RequiredMaterials)
{
var adjustedAmount = AdjustMaterial(needed, recipe.ApplyMaterialDiscount, component.MaterialUseMultiplier);
var gridUid =
HasComp<BluespaceStorageComponent>(uid) &&
TryComp<TransformComponent>(uid, out var transformComponent)
? transformComponent.GridUid
: null;
var gridStorage = gridUid.HasValue &&
TryComp<MaterialStorageComponent>(gridUid, out var materialStorageComponent)
? materialStorageComponent
: null;
if (_materialStorage.GetMaterialAmount(uid, material, gridUid: gridUid, gridStorage: gridStorage) < adjustedAmount * amount)
return false;
}
return true;
}
private void OnEmagged(EntityUid uid, EmagLatheRecipesComponent component, ref GotEmaggedEvent args)
{
args.Handled = true;
}
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()
{
_inverseRecipeDictionary.Clear();
foreach (var latheRecipe in _proto.EnumeratePrototypes<LatheRecipePrototype>())
{
_inverseRecipeDictionary.GetOrNew(latheRecipe.Result).Add(latheRecipe);
}
}
public bool TryGetRecipesFromEntity(string prototype, [NotNullWhen(true)] out List<LatheRecipePrototype>? recipes)
{
recipes = new();
if (_inverseRecipeDictionary.TryGetValue(prototype, out var r))
recipes.AddRange(r);
return recipes.Count != 0;
}
}