Files
OldThink/Content.Server/Kitchen/Components/MicrowaveComponent.cs

462 lines
16 KiB
C#
Raw Normal View History

using System.Linq;
2022-02-12 17:53:54 -07:00
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
2021-06-09 22:19:39 +02:00
using Content.Server.Power.Components;
2022-02-12 17:53:54 -07:00
using Content.Server.Temperature.Components;
using Content.Server.Temperature.Systems;
2021-06-09 22:19:39 +02:00
using Content.Server.UserInterface;
using Content.Shared.FixedPoint;
using Content.Shared.Kitchen;
2021-06-09 22:19:39 +02:00
using Content.Shared.Kitchen.Components;
using Content.Shared.Power;
using Content.Shared.Sound;
2022-02-12 17:53:54 -07:00
using Content.Shared.Tag;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.Player;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Kitchen.Components
{
[RegisterComponent]
public sealed class MicrowaveComponent : SharedMicrowaveComponent
{
2021-12-05 21:02:04 +01:00
[Dependency] private readonly IEntityManager _entities = default!;
[Dependency] private readonly RecipeManager _recipeManager = default!;
#region YAMLSERIALIZE
[DataField("cookTime")] private uint _cookTimeDefault = 5;
[DataField("cookTimeMultiplier")] private int _cookTimeMultiplier = 1000; //For upgrades and stuff I guess?
[DataField("failureResult")] private string _badRecipeName = "FoodBadRecipe";
[DataField("beginCookingSound")] private SoundSpecifier _startCookingSound =
new SoundPathSpecifier("/Audio/Machines/microwave_start_beep.ogg");
[DataField("foodDoneSound")] private SoundSpecifier _cookingCompleteSound =
new SoundPathSpecifier("/Audio/Machines/microwave_done_beep.ogg");
[DataField("clickSound")]
private SoundSpecifier _clickSound = new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg");
2022-02-12 17:53:54 -07:00
public SoundSpecifier ItemBreakSound = new SoundPathSpecifier("/Audio/Effects/clang.ogg");
#endregion YAMLSERIALIZE
[ViewVariables] private bool _busy = false;
2022-04-15 17:20:20 -04:00
public bool Broken;
/// <summary>
/// This is a fixed offset of 5.
/// The cook times for all recipes should be divisible by 5,with a minimum of 1 second.
/// For right now, I don't think any recipe cook time should be greater than 60 seconds.
/// </summary>
[ViewVariables] private uint _currentCookTimerTime = 1;
2022-02-12 17:53:54 -07:00
/// <summary>
/// The max temperature that this microwave can heat objects to.
/// </summary>
[DataField("temperatureUpperThreshold")]
public float TemperatureUpperThreshold = 373.15f;
2022-04-15 17:20:20 -04:00
public bool Powered => !_entities.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
2022-04-15 17:20:20 -04:00
private bool HasContents => Storage.ContainedEntities.Count > 0;
public bool UIDirty = true;
private bool _lostPower;
private int _currentCookTimeButtonIndex;
public void DirtyUi()
{
UIDirty = true;
}
2022-04-15 17:20:20 -04:00
public Container Storage = default!;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(MicrowaveUiKey.Key);
protected override void Initialize()
{
base.Initialize();
_currentCookTimerTime = _cookTimeDefault;
2022-04-15 17:20:20 -04:00
Storage = ContainerHelpers.EnsureContainer<Container>(Owner, "microwave_entity_container",
out _);
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
}
}
public void SetCookTime(uint cookTime)
{
_currentCookTimerTime = cookTime;
UIDirty = true;
}
2020-05-01 23:34:04 -05:00
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage message)
{
if (!Powered || _busy)
{
return;
}
2020-05-01 23:34:04 -05:00
switch (message.Message)
{
case MicrowaveStartCookMessage:
Wzhzhzh();
2020-05-01 23:34:04 -05:00
break;
case MicrowaveEjectMessage:
if (HasContents)
{
EjectSolids();
ClickSound();
UIDirty = true;
}
2020-05-01 23:34:04 -05:00
break;
2020-05-03 01:34:00 -05:00
case MicrowaveEjectSolidIndexedMessage msg:
if (HasContents)
{
EjectSolid(msg.EntityID);
ClickSound();
UIDirty = true;
}
break;
case MicrowaveSelectCookTimeMessage msg:
_currentCookTimeButtonIndex = msg.ButtonIndex;
_currentCookTimerTime = msg.NewCookTime;
ClickSound();
UIDirty = true;
2020-05-03 01:34:00 -05:00
break;
}
2020-05-01 23:34:04 -05:00
}
public void OnUpdate()
2020-05-03 01:34:00 -05:00
{
if (!Powered)
{
//TODO:If someone cuts power currently, microwave magically keeps going. FIX IT!
SetAppearance(MicrowaveVisualState.Idle);
}
if (_busy && !Powered)
{
//we lost power while we were cooking/busy!
_lostPower = true;
EjectSolids();
_busy = false;
UIDirty = true;
}
2022-04-15 17:20:20 -04:00
if (_busy && Broken)
{
SetAppearance(MicrowaveVisualState.Broken);
//we broke while we were cooking/busy!
_lostPower = true;
EjectSolids();
_busy = false;
UIDirty = true;
}
if (UIDirty)
{
UserInterface?.SetState(new MicrowaveUpdateUserInterfaceState
(
2022-04-15 17:20:20 -04:00
Storage.ContainedEntities.Select(item => item).ToArray(),
_busy,
_currentCookTimeButtonIndex,
_currentCookTimerTime
));
UIDirty = false;
}
2020-05-03 01:34:00 -05:00
}
2022-05-03 01:43:25 +03:00
public void SetAppearance(MicrowaveVisualState state)
2020-05-03 01:34:00 -05:00
{
var finalState = state;
2022-04-15 17:20:20 -04:00
if (Broken)
{
finalState = MicrowaveVisualState.Broken;
}
2021-12-05 21:02:04 +01:00
if (_entities.TryGetComponent(Owner, out AppearanceComponent? appearance))
2020-05-03 01:34:00 -05:00
{
appearance.SetData(PowerDeviceVisuals.VisualState, finalState);
2020-05-03 01:34:00 -05:00
}
}
2020-05-01 23:34:04 -05:00
// ReSharper disable once InconsistentNaming
// ReSharper disable once IdentifierTypo
public void Wzhzhzh()
{
if (!HasContents)
{
return;
}
2020-05-01 23:34:04 -05:00
_busy = true;
2020-05-03 01:34:00 -05:00
// Convert storage into Dictionary of ingredients
var solidsDict = new Dictionary<string, int>();
2022-02-12 17:53:54 -07:00
var reagentDict = new Dictionary<string, FixedPoint2>();
2022-04-15 17:20:20 -04:00
foreach (var item in Storage.ContainedEntities)
2020-05-03 01:34:00 -05:00
{
2022-02-12 17:53:54 -07:00
// special behavior when being microwaved ;)
var ev = new BeingMicrowavedEvent(Owner);
_entities.EventBus.RaiseLocalEvent(item, ev, false);
if (ev.Handled)
{
_busy = false;
UIDirty = true;
2022-02-12 17:53:54 -07:00
return;
}
2022-02-12 17:53:54 -07:00
var tagSys = EntitySystem.Get<TagSystem>();
if (tagSys.HasTag(item, "MicrowaveMachineUnsafe")
|| tagSys.HasTag(item, "Metal"))
{
// destroy microwave
2022-04-15 17:20:20 -04:00
Broken = true;
2022-02-12 17:53:54 -07:00
SetAppearance(MicrowaveVisualState.Broken);
SoundSystem.Play(ItemBreakSound.GetSound(), Filter.Pvs(Owner), Owner);
2022-02-12 17:53:54 -07:00
return;
}
if (tagSys.HasTag(item, "MicrowaveSelfUnsafe")
|| tagSys.HasTag(item, "Plastic"))
{
_entities.SpawnEntity(_badRecipeName,
_entities.GetComponent<TransformComponent>(Owner).Coordinates);
_entities.QueueDeleteEntity(item);
}
2021-12-05 21:02:04 +01:00
var metaData = _entities.GetComponent<MetaDataComponent>(item);
if (metaData.EntityPrototype == null)
{
continue;
}
2021-12-05 21:02:04 +01:00
if (solidsDict.ContainsKey(metaData.EntityPrototype.ID))
2020-05-03 01:34:00 -05:00
{
2021-12-05 21:02:04 +01:00
solidsDict[metaData.EntityPrototype.ID]++;
2020-05-03 01:34:00 -05:00
}
else
{
2021-12-05 21:02:04 +01:00
solidsDict.Add(metaData.EntityPrototype.ID, 1);
2020-05-03 01:34:00 -05:00
}
2022-02-12 17:53:54 -07:00
if (!_entities.TryGetComponent<SolutionContainerManagerComponent>(item, out var solMan))
continue;
2022-02-12 17:53:54 -07:00
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);
}
}
}
2020-05-03 01:34:00 -05:00
// Check recipes
FoodRecipePrototype? recipeToCook = null;
foreach (var r in _recipeManager.Recipes.Where(r =>
2022-02-12 17:53:54 -07:00
CanSatisfyRecipe(r, solidsDict, reagentDict)))
{
recipeToCook = r;
}
SetAppearance(MicrowaveVisualState.Cooking);
2022-02-12 17:53:54 -07:00
var time = _currentCookTimerTime * _cookTimeMultiplier;
SoundSystem.Play(_startCookingSound.GetSound(), Filter.Pvs(Owner), Owner, AudioParams.Default);
Owner.SpawnTimer((int) (_currentCookTimerTime * _cookTimeMultiplier), () =>
2021-08-10 15:39:24 -07:00
{
if (_lostPower)
{
return;
}
2022-02-12 17:53:54 -07:00
AddTemperature(time);
if (recipeToCook != null)
2021-08-10 15:39:24 -07:00
{
2022-02-12 17:53:54 -07:00
SubtractContents(recipeToCook);
_entities.SpawnEntity(recipeToCook.Result,
_entities.GetComponent<TransformComponent>(Owner).Coordinates);
2021-08-10 15:39:24 -07:00
}
2022-02-12 17:53:54 -07:00
EjectSolids();
SoundSystem.Play(_cookingCompleteSound.GetSound(), Filter.Pvs(Owner),
Owner, AudioParams.Default.WithVolume(-1f));
2021-08-10 15:39:24 -07:00
SetAppearance(MicrowaveVisualState.Idle);
_busy = false;
UIDirty = true;
});
_lostPower = false;
UIDirty = true;
}
2020-05-01 23:34:04 -05:00
2022-02-12 17:53:54 -07:00
/// <summary>
/// Adds temperature to every item in the microwave,
/// based on the time it took to microwave.
/// </summary>
/// <param name="time">The time on the microwave, in seconds.</param>
public void AddTemperature(float time)
2020-05-01 23:34:04 -05:00
{
2022-02-12 17:53:54 -07:00
var solutionContainerSystem = EntitySystem.Get<SolutionContainerSystem>();
2022-04-15 17:20:20 -04:00
foreach (var entity in Storage.ContainedEntities)
{
2022-02-12 17:53:54 -07:00
if (_entities.TryGetComponent(entity, out TemperatureComponent? temp))
{
EntitySystem.Get<TemperatureSystem>().ChangeHeat(entity, time / 10, false, temp);
}
2020-05-03 01:34:00 -05:00
2022-02-12 17:53:54 -07:00
if (_entities.TryGetComponent(entity, out SolutionContainerManagerComponent? solutions))
{
foreach (var (_, solution) in solutions.Solutions)
{
if (solution.Temperature > TemperatureUpperThreshold)
continue;
2020-05-03 01:34:00 -05:00
2022-02-12 17:53:54 -07:00
solutionContainerSystem.AddThermalEnergy(entity, solution, time / 10);
}
}
2020-05-03 01:34:00 -05:00
}
2020-05-01 23:34:04 -05:00
}
private void EjectSolids()
{
2022-04-15 17:20:20 -04:00
for (var i = Storage.ContainedEntities.Count - 1; i >= 0; i--)
{
2022-04-15 17:20:20 -04:00
Storage.Remove(Storage.ContainedEntities.ElementAt(i));
}
2020-05-03 01:34:00 -05:00
}
private void EjectSolid(EntityUid entityId)
2020-05-03 01:34:00 -05:00
{
2021-12-05 21:02:04 +01:00
if (_entities.EntityExists(entityId))
{
2022-04-15 17:20:20 -04:00
Storage.Remove(entityId);
}
2020-05-03 01:34:00 -05:00
}
private void SubtractContents(FoodRecipePrototype recipe)
{
2022-02-12 17:53:54 -07:00
var totalReagentsToRemove = new Dictionary<string, FixedPoint2>(recipe.IngredientsReagents);
var solutionContainerSystem = EntitySystem.Get<SolutionContainerSystem>();
2022-02-12 17:53:54 -07:00
// this is spaghetti ngl
2022-04-15 17:20:20 -04:00
foreach (var item in Storage.ContainedEntities)
2020-05-03 01:34:00 -05:00
{
2022-02-12 17:53:54 -07:00
if (!_entities.TryGetComponent<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;
}
solutionContainerSystem.TryRemoveReagent(item, solution, reagent, quant);
}
}
}
foreach (var recipeSolid in recipe.IngredientsSolids)
2020-05-03 01:34:00 -05:00
{
for (var i = 0; i < recipeSolid.Value; i++)
{
2022-04-15 17:20:20 -04:00
foreach (var item in Storage.ContainedEntities)
{
2021-12-05 21:02:04 +01:00
var metaData = _entities.GetComponent<MetaDataComponent>(item);
if (metaData.EntityPrototype == null)
{
continue;
}
2021-12-05 21:02:04 +01:00
if (metaData.EntityPrototype.ID == recipeSolid.Key)
{
2022-04-15 17:20:20 -04:00
Storage.Remove(item);
2021-12-05 21:02:04 +01:00
_entities.DeleteEntity(item);
break;
}
}
}
2020-05-03 01:34:00 -05:00
}
}
2020-05-03 01:34:00 -05:00
2022-02-12 17:53:54 -07:00
private bool CanSatisfyRecipe(FoodRecipePrototype recipe, Dictionary<string, int> solids, Dictionary<string, FixedPoint2> reagents)
{
2021-12-05 18:09:01 +01:00
if (_currentCookTimerTime != recipe.CookTime)
{
2022-02-12 17:53:54 -07:00
return false;
}
2022-02-12 17:53:54 -07:00
foreach (var solid in recipe.IngredientsSolids)
{
2022-02-12 17:53:54 -07:00
if (!solids.ContainsKey(solid.Key))
return false;
2022-02-12 17:53:54 -07:00
if (solids[solid.Key] < solid.Value)
return false;
}
2022-02-12 17:53:54 -07:00
foreach (var reagent in recipe.IngredientsReagents)
{
2022-02-12 17:53:54 -07:00
if (!reagents.ContainsKey(reagent.Key))
return false;
2022-02-12 17:53:54 -07:00
if (reagents[reagent.Key] < reagent.Value)
return false;
}
2022-02-12 17:53:54 -07:00
return true;
}
public void ClickSound()
{
SoundSystem.Play(_clickSound.GetSound(), Filter.Pvs(Owner), Owner, AudioParams.Default.WithVolume(-2f));
}
}
2022-02-12 17:53:54 -07:00
public sealed class BeingMicrowavedEvent : HandledEntityEventArgs
2022-02-12 17:53:54 -07:00
{
public EntityUid Microwave;
public BeingMicrowavedEvent(EntityUid microwave)
{
Microwave = microwave;
}
}
}