490 lines
20 KiB
C#
490 lines
20 KiB
C#
using System.Linq;
|
|
using Content.Server.Chemistry.Components.SolutionManager;
|
|
using Content.Server.Chemistry.EntitySystems;
|
|
using Content.Server.Construction;
|
|
using Content.Server.Kitchen.Components;
|
|
using Content.Shared.Destructible;
|
|
using Content.Shared.Interaction;
|
|
using Content.Shared.Item;
|
|
using Content.Shared.Kitchen.Components;
|
|
using Robust.Shared.Player;
|
|
using Content.Shared.Interaction.Events;
|
|
using Content.Shared.Body.Components;
|
|
using Content.Shared.Body.Part;
|
|
using Content.Server.Hands.Systems;
|
|
using Content.Server.Power.Components;
|
|
using Content.Server.Temperature.Components;
|
|
using Content.Server.Temperature.Systems;
|
|
using Content.Shared.FixedPoint;
|
|
using Content.Shared.Kitchen;
|
|
using Content.Shared.Popups;
|
|
using Content.Shared.Power;
|
|
using Content.Shared.Tag;
|
|
using Robust.Server.Containers;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Shared.Audio;
|
|
using Robust.Shared.Containers;
|
|
|
|
namespace Content.Server.Kitchen.EntitySystems
|
|
{
|
|
public sealed class MicrowaveSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly ContainerSystem _container = default!;
|
|
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
|
|
[Dependency] private readonly RecipeManager _recipeManager = default!;
|
|
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
|
|
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
|
[Dependency] private readonly SharedContainerSystem _sharedContainer = default!;
|
|
[Dependency] private readonly SolutionContainerSystem _solutionContainer = default!;
|
|
[Dependency] private readonly TagSystem _tag = default!;
|
|
[Dependency] private readonly TemperatureSystem _temperature = default!;
|
|
[Dependency] private readonly UserInterfaceSystem _userInterface = default!;
|
|
[Dependency] private readonly HandsSystem _handsSystem = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<MicrowaveComponent, ComponentInit>(OnInit);
|
|
SubscribeLocalEvent<MicrowaveComponent, SolutionChangedEvent>(OnSolutionChange);
|
|
SubscribeLocalEvent<MicrowaveComponent, InteractUsingEvent>(OnInteractUsing, after: new[]{typeof(AnchorableSystem)});
|
|
SubscribeLocalEvent<MicrowaveComponent, BreakageEventArgs>(OnBreak);
|
|
SubscribeLocalEvent<MicrowaveComponent, PowerChangedEvent>(OnPowerChanged);
|
|
SubscribeLocalEvent<MicrowaveComponent, SuicideEvent>(OnSuicide);
|
|
|
|
SubscribeLocalEvent<MicrowaveComponent, MicrowaveStartCookMessage>((u,c,_) => Wzhzhzh(u,c));
|
|
SubscribeLocalEvent<MicrowaveComponent, MicrowaveEjectMessage>(OnEjectMessage);
|
|
SubscribeLocalEvent<MicrowaveComponent, MicrowaveEjectSolidIndexedMessage>(OnEjectIndex);
|
|
SubscribeLocalEvent<MicrowaveComponent, MicrowaveSelectCookTimeMessage>(OnSelectTime);
|
|
|
|
SubscribeLocalEvent<ActiveMicrowaveComponent, ComponentStartup>(OnCookStart);
|
|
SubscribeLocalEvent<ActiveMicrowaveComponent, ComponentShutdown>(OnCookStop);
|
|
}
|
|
|
|
private void OnCookStart(EntityUid uid, ActiveMicrowaveComponent component, ComponentStartup args)
|
|
{
|
|
if (!TryComp<MicrowaveComponent>(uid, out var microwaveComponent))
|
|
return;
|
|
SetAppearance(microwaveComponent, MicrowaveVisualState.Cooking);
|
|
|
|
microwaveComponent.PlayingStream =
|
|
_audio.Play(microwaveComponent.LoopingSound, Filter.Pvs(uid), uid, AudioParams.Default.WithLoop(true).WithMaxDistance(5));
|
|
}
|
|
|
|
private void OnCookStop(EntityUid uid, ActiveMicrowaveComponent component, ComponentShutdown args)
|
|
{
|
|
if (!TryComp<MicrowaveComponent>(uid, out var microwaveComponent))
|
|
return;
|
|
SetAppearance(microwaveComponent, MicrowaveVisualState.Idle);
|
|
|
|
microwaveComponent.PlayingStream?.Stop();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds temperature to every item in the microwave,
|
|
/// based on the time it took to microwave.
|
|
/// </summary>
|
|
/// <param name="component">The microwave that is heating up.</param>
|
|
/// <param name="time">The time on the microwave, in seconds.</param>
|
|
private void AddTemperature(MicrowaveComponent component, float time)
|
|
{
|
|
var heatToAdd = time * 100;
|
|
foreach (var entity in component.Storage.ContainedEntities)
|
|
{
|
|
if (TryComp<TemperatureComponent>(entity, out var tempComp))
|
|
_temperature.ChangeHeat(entity, heatToAdd, false, tempComp);
|
|
|
|
if (!TryComp<SolutionContainerManagerComponent>(entity, out var solutions))
|
|
continue;
|
|
foreach (var (_, solution) in solutions.Solutions)
|
|
{
|
|
if (solution.Temperature > component.TemperatureUpperThreshold)
|
|
continue;
|
|
|
|
_solutionContainer.AddThermalEnergy(entity, solution, heatToAdd);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SubtractContents(MicrowaveComponent component, FoodRecipePrototype recipe)
|
|
{
|
|
var totalReagentsToRemove = new Dictionary<string, FixedPoint2>(recipe.IngredientsReagents);
|
|
|
|
// this is spaghetti ngl
|
|
foreach (var item in component.Storage.ContainedEntities)
|
|
{
|
|
if (!TryComp<SolutionContainerManagerComponent>(item, out var solMan))
|
|
continue;
|
|
|
|
// go over every solution
|
|
foreach (var (_, solution) in solMan.Solutions)
|
|
{
|
|
foreach (var (reagent, _) in recipe.IngredientsReagents)
|
|
{
|
|
// removed everything
|
|
if (!totalReagentsToRemove.ContainsKey(reagent))
|
|
continue;
|
|
|
|
if (!solution.ContainsReagent(reagent))
|
|
continue;
|
|
|
|
var quant = solution.GetReagentQuantity(reagent);
|
|
|
|
if (quant >= totalReagentsToRemove[reagent])
|
|
{
|
|
quant = totalReagentsToRemove[reagent];
|
|
totalReagentsToRemove.Remove(reagent);
|
|
}
|
|
else
|
|
{
|
|
totalReagentsToRemove[reagent] -= quant;
|
|
}
|
|
|
|
_solutionContainer.TryRemoveReagent(item, solution, reagent, quant);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var recipeSolid in recipe.IngredientsSolids)
|
|
{
|
|
for (var i = 0; i < recipeSolid.Value; i++)
|
|
{
|
|
foreach (var item in component.Storage.ContainedEntities)
|
|
{
|
|
var metaData = MetaData(item);
|
|
if (metaData.EntityPrototype == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (metaData.EntityPrototype.ID == recipeSolid.Key)
|
|
{
|
|
component.Storage.Remove(item);
|
|
EntityManager.DeleteEntity(item);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnInit(EntityUid uid, MicrowaveComponent component, ComponentInit ags)
|
|
{
|
|
component.Storage = _container.EnsureContainer<Container>(uid,"microwave_entity_container");
|
|
}
|
|
|
|
private void OnSuicide(EntityUid uid, MicrowaveComponent component, SuicideEvent args)
|
|
{
|
|
if (args.Handled)
|
|
return;
|
|
|
|
args.SetHandled(SuicideKind.Heat);
|
|
var victim = args.Victim;
|
|
var headCount = 0;
|
|
|
|
if (TryComp<SharedBodyComponent>(victim, out var body))
|
|
{
|
|
var headSlots = body.GetSlotsOfType(BodyPartType.Head);
|
|
|
|
foreach (var slot in headSlots)
|
|
{
|
|
var part = slot.Part;
|
|
|
|
if (part == null ||
|
|
!body.TryDropPart(slot, out var dropped))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (var droppedPart in dropped.Values)
|
|
{
|
|
if (droppedPart.PartType != BodyPartType.Head)
|
|
{
|
|
continue;
|
|
}
|
|
component.Storage.Insert(droppedPart.Owner);
|
|
headCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
var othersMessage = headCount > 1
|
|
? Loc.GetString("microwave-component-suicide-multi-head-others-message", ("victim", victim))
|
|
: Loc.GetString("microwave-component-suicide-others-message", ("victim", victim));
|
|
|
|
var selfMessage = headCount > 1
|
|
? Loc.GetString("microwave-component-suicide-multi-head-message")
|
|
: Loc.GetString("microwave-component-suicide-message");
|
|
|
|
_popupSystem.PopupEntity(othersMessage, victim, Filter.PvsExcept(victim));
|
|
_popupSystem.PopupEntity(selfMessage, victim, Filter.Entities(victim));
|
|
|
|
_audio.PlayPvs(component.ClickSound, uid, AudioParams.Default.WithVolume(-2));
|
|
component.CurrentCookTimerTime = 10;
|
|
Wzhzhzh(uid, component);
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
private void OnSolutionChange(EntityUid uid, MicrowaveComponent component, SolutionChangedEvent args)
|
|
{
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
private void OnInteractUsing(EntityUid uid, MicrowaveComponent component, InteractUsingEvent args)
|
|
{
|
|
if(args.Handled)
|
|
return;
|
|
if (!(TryComp<ApcPowerReceiverComponent>(uid, out var apc) && apc.Powered))
|
|
{
|
|
_popupSystem.PopupEntity(Loc.GetString("microwave-component-interact-using-no-power"), uid, Filter.Entities(args.User));
|
|
return;
|
|
}
|
|
|
|
if (component.Broken)
|
|
{
|
|
_popupSystem.PopupEntity(Loc.GetString("microwave-component-interact-using-broken"), uid, Filter.Entities(args.User));
|
|
return;
|
|
}
|
|
|
|
if (!HasComp<ItemComponent>(args.Used))
|
|
{
|
|
_popupSystem.PopupEntity(Loc.GetString("microwave-component-interact-using-transfer-fail"), uid, Filter.Entities(args.User));
|
|
return;
|
|
}
|
|
|
|
args.Handled = true;
|
|
_handsSystem.TryDropIntoContainer(args.User, args.Used, component.Storage);
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
private void OnBreak(EntityUid uid, MicrowaveComponent component, BreakageEventArgs args)
|
|
{
|
|
component.Broken = true;
|
|
SetAppearance(component, MicrowaveVisualState.Broken);
|
|
RemComp<ActiveMicrowaveComponent>(uid);
|
|
_sharedContainer.EmptyContainer(component.Storage);
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
private void OnPowerChanged(EntityUid uid, MicrowaveComponent component, PowerChangedEvent args)
|
|
{
|
|
if (!args.Powered)
|
|
{
|
|
SetAppearance(component, MicrowaveVisualState.Idle);
|
|
RemComp<ActiveMicrowaveComponent>(uid);
|
|
_sharedContainer.EmptyContainer(component.Storage);
|
|
}
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
public void UpdateUserInterfaceState(EntityUid uid, MicrowaveComponent component)
|
|
{
|
|
var ui = _userInterface.GetUiOrNull(uid, MicrowaveUiKey.Key);
|
|
if (ui == null)
|
|
return;
|
|
var state = new MicrowaveUpdateUserInterfaceState(
|
|
component.Storage.ContainedEntities.ToArray(),
|
|
HasComp<ActiveMicrowaveComponent>(uid),
|
|
component.CurrentCookTimeButtonIndex,
|
|
component.CurrentCookTimerTime
|
|
);
|
|
_userInterface.SetUiState(ui, state);
|
|
}
|
|
|
|
public void SetAppearance(MicrowaveComponent component, MicrowaveVisualState state)
|
|
{
|
|
var display = component.Broken ? MicrowaveVisualState.Broken : state;
|
|
_appearance.SetData(component.Owner, PowerDeviceVisuals.VisualState, display);
|
|
}
|
|
|
|
public bool HasContents(MicrowaveComponent component)
|
|
{
|
|
return component.Storage.ContainedEntities.Any();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts Cooking
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// It does not make a "wzhzhzh" sound, it makes a "mmmmmmmm" sound!
|
|
/// -emo
|
|
/// </remarks>
|
|
public void Wzhzhzh(EntityUid uid, MicrowaveComponent component)
|
|
{
|
|
if (!HasContents(component) || HasComp<ActiveMicrowaveComponent>(uid))
|
|
return;
|
|
|
|
var solidsDict = new Dictionary<string, int>();
|
|
var reagentDict = new Dictionary<string, FixedPoint2>();
|
|
foreach (var item in component.Storage.ContainedEntities)
|
|
{
|
|
// special behavior when being microwaved ;)
|
|
var ev = new BeingMicrowavedEvent(uid);
|
|
RaiseLocalEvent(item, ev);
|
|
|
|
if (ev.Handled)
|
|
{
|
|
UpdateUserInterfaceState(uid, component);
|
|
return;
|
|
}
|
|
|
|
// destroy microwave
|
|
if (_tag.HasTag(item, "MicrowaveMachineUnsafe") || _tag.HasTag(item, "Metal"))
|
|
{
|
|
component.Broken = true;
|
|
SetAppearance(component, MicrowaveVisualState.Broken);
|
|
_audio.PlayPvs(component.ItemBreakSound, uid);
|
|
return;
|
|
}
|
|
|
|
if (_tag.HasTag(item, "MicrowaveSelfUnsafe") || _tag.HasTag(item, "Plastic"))
|
|
{
|
|
var junk = Spawn(component.BadRecipeEntityId, Transform(uid).Coordinates);
|
|
component.Storage.Insert(junk);
|
|
QueueDel(item);
|
|
}
|
|
|
|
var metaData = MetaData(item); //this simply begs for cooking refactor
|
|
if (metaData.EntityPrototype == null)
|
|
continue;
|
|
|
|
if (solidsDict.ContainsKey(metaData.EntityPrototype.ID))
|
|
{
|
|
solidsDict[metaData.EntityPrototype.ID]++;
|
|
}
|
|
else
|
|
{
|
|
solidsDict.Add(metaData.EntityPrototype.ID, 1);
|
|
}
|
|
|
|
if (!TryComp<SolutionContainerManagerComponent>(item, out var solMan))
|
|
continue;
|
|
|
|
foreach (var (_, solution) in solMan.Solutions)
|
|
{
|
|
foreach (var reagent in solution.Contents)
|
|
{
|
|
if (reagentDict.ContainsKey(reagent.ReagentId))
|
|
reagentDict[reagent.ReagentId] += reagent.Quantity;
|
|
else
|
|
reagentDict.Add(reagent.ReagentId, reagent.Quantity);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check recipes
|
|
var portionedRecipe = _recipeManager.Recipes.Select(r =>
|
|
CanSatisfyRecipe(component, r, solidsDict, reagentDict)).FirstOrDefault(r => r.Item2 > 0);
|
|
|
|
_audio.PlayPvs(component.StartCookingSound, uid);
|
|
var activeComp = AddComp<ActiveMicrowaveComponent>(uid); //microwave is now cooking
|
|
activeComp.CookTimeRemaining = component.CurrentCookTimerTime * component.CookTimeMultiplier;
|
|
activeComp.TotalTime = component.CurrentCookTimerTime * component.CookTimeMultiplier;
|
|
activeComp.PortionedRecipe = portionedRecipe;
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
public (FoodRecipePrototype, int) CanSatisfyRecipe(MicrowaveComponent component, FoodRecipePrototype recipe, Dictionary<string, int> solids, Dictionary<string, FixedPoint2> reagents)
|
|
{
|
|
var portions = 0;
|
|
|
|
if(component.CurrentCookTimerTime % recipe.CookTime != 0)
|
|
{
|
|
//can't be a multiple of this recipe
|
|
return (recipe, 0);
|
|
}
|
|
|
|
foreach (var solid in recipe.IngredientsSolids)
|
|
{
|
|
if (!solids.ContainsKey(solid.Key))
|
|
return (recipe, 0);
|
|
|
|
if (solids[solid.Key] < solid.Value)
|
|
return (recipe, 0);
|
|
|
|
portions = portions == 0
|
|
? solids[solid.Key] / solid.Value.Int()
|
|
: Math.Min(portions, solids[solid.Key] / solid.Value.Int());
|
|
}
|
|
|
|
foreach (var reagent in recipe.IngredientsReagents)
|
|
{
|
|
if (!reagents.ContainsKey(reagent.Key))
|
|
return (recipe, 0);
|
|
|
|
if (reagents[reagent.Key] < reagent.Value)
|
|
return (recipe, 0);
|
|
|
|
portions = portions == 0
|
|
? reagents[reagent.Key].Int() / reagent.Value.Int()
|
|
: Math.Min(portions, reagents[reagent.Key].Int() / reagent.Value.Int());
|
|
}
|
|
|
|
//cook only as many of those portions as time allows
|
|
return (recipe, (int)Math.Min(portions, component.CurrentCookTimerTime / recipe.CookTime));
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
foreach (var (active, microwave) in EntityManager.EntityQuery<ActiveMicrowaveComponent, MicrowaveComponent>())
|
|
{
|
|
//check if there's still cook time left
|
|
active.CookTimeRemaining -= frameTime;
|
|
if (active.CookTimeRemaining > 0)
|
|
continue;
|
|
|
|
//this means the microwave has finished cooking.
|
|
AddTemperature(microwave, active.TotalTime);
|
|
|
|
if (active.PortionedRecipe.Item1 != null)
|
|
{
|
|
var coords = Transform(microwave.Owner).Coordinates;
|
|
for (var i = 0; i < active.PortionedRecipe.Item2; i++)
|
|
{
|
|
SubtractContents(microwave, active.PortionedRecipe.Item1);
|
|
Spawn(active.PortionedRecipe.Item1.Result, coords);
|
|
}
|
|
}
|
|
|
|
_sharedContainer.EmptyContainer(microwave.Storage);
|
|
UpdateUserInterfaceState(microwave.Owner, microwave);
|
|
EntityManager.RemoveComponentDeferred<ActiveMicrowaveComponent>(active.Owner);
|
|
_audio.PlayPvs(microwave.FoodDoneSound, microwave.Owner, AudioParams.Default.WithVolume(-1));
|
|
}
|
|
}
|
|
|
|
#region ui
|
|
private void OnEjectMessage(EntityUid uid, MicrowaveComponent component, MicrowaveEjectMessage args)
|
|
{
|
|
if (!HasContents(component) || HasComp<ActiveMicrowaveComponent>(uid))
|
|
return;
|
|
|
|
_sharedContainer.EmptyContainer(component.Storage);
|
|
_audio.PlayPvs(component.ClickSound, uid, AudioParams.Default.WithVolume(-2));
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
private void OnEjectIndex(EntityUid uid, MicrowaveComponent component, MicrowaveEjectSolidIndexedMessage args)
|
|
{
|
|
if (!HasContents(component) || HasComp<ActiveMicrowaveComponent>(uid))
|
|
return;
|
|
|
|
component.Storage.Remove(args.EntityID);
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
|
|
private void OnSelectTime(EntityUid uid, MicrowaveComponent component, MicrowaveSelectCookTimeMessage args)
|
|
{
|
|
if (!HasContents(component) || HasComp<ActiveMicrowaveComponent>(uid) || !(TryComp<ApcPowerReceiverComponent>(uid, out var apc) && apc.Powered))
|
|
return;
|
|
|
|
component.CurrentCookTimeButtonIndex = args.ButtonIndex;
|
|
component.CurrentCookTimerTime = args.NewCookTime;
|
|
_audio.PlayPvs(component.ClickSound, uid, AudioParams.Default.WithVolume(-2));
|
|
UpdateUserInterfaceState(uid, component);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|