CatStation/Content.Shared/Kitchen/EntitySystems/MicrowaveSystem.API.cs
portfiend b9b794deef
Microwave Refactor and Prediction (#43129)
* adjust fields of meal recipe prototype

* document microwave recipe datafields

* use file scoped namespace for microwave recipes

* more of this

* localize every microwave recipe

* make microwave system shared

* refactor recipe reagent contents

* refactor recipe solids + add material ingredient datafield

* im making this a bit less silly

* awa

* move this shit out

* move this shit out also

* refactor Wzhzhzh

* im gonna shorten this parameter by 1 actually

* move this into recipes

* make processcontents a little better(?)

* clean imports

* rename a struct

* use cooking ingredients struct

* comment

* use whitelist for this

* unused dependency

* rearranged the parameters of get recipe portions

* rearrange some stuff

* clean up more imports

* move container related functions into separate file

* move this function out of ProcessContents

* prevent microwaves from having items inserted while active

* use IsPowered in microwave system

* use byref syntax and Entity<>

* moving the override up
sorry i like it being up here

* refactor microwave updating

* clean up recipe finish logic

* im renaming this function actually

* move some functions around

* move more stuff around

* some comments

* fix compiler error

* fix the yml of all microwave recipes

* use materials instead of solids for this stuff

* oops

* fix this

* turn microwave sparks into datafield

* move activelymicrowaved events into separate folder

* make UpdateUserInterfaceState use entity only

* rearrange some functions

* move UI functions out

* comment microwave UI stuff

* more comments

* comment recipe functions

* refactor GetTotalIngredients to use an operator

* give CookingIngredients addition methods

* comment the rest of CookingIngredients

* make subtraction functions clearer?

* remove redundant comment

* handle zero/negative ingredient cases

* simplify subtract contents logic

* fix these comments

* typo

* add microwave recipe test

* move active microwave methods

* comment active microwave methods

* comment cooking methods

* comment microwave methods

* change setappearance to use entity<>

* add some public api decorators

* comment all of MicrowaveComponent

* use file-scoped namespace for MicrowaveComponent

* comment

* fix baguette sword

* maybe...?

* oh oops

* document the rest of the owl

* kill the massive YML breaking change

* fix protoman dependency

* move shared microwave stuff to SharedMicrowaveSystem

* fix usings

* move microwave components to shared

* fix up microwave datafields

* move APIs to shared

* move activemicrowave methods to shared

* initialize on shared

* move ui functions to shared

* add actor here

* stuff

* predict microwave update loop

* make clientside microwave system

* adjust the update loop for microwaves

* clean up

* add field deltas to microwaves

* dirty fields instead of whole component

* move this to the partial

* move GetRecipesForMicrowave to shared

* comments

* predict microwave BUI

* oops

* refactor microwave bui update

* move UI messages to shared

* move messages to UI file

* move init to shared

* move container methosd to shared

* oops my using

* move cooking functions into partial

* move cooking methods to shared

* move microwave recipe methods to shared

* move the rest of the microwave events to shared

* update imports

* remove custom type serializer

* better (?) microwave UI prediction

* fix list notations

* OK IT WAS THE DATAFIELDS

* add IOC inject

* dont use IOC for htis

* sealed

* prediction fixes pt1

* make microwave cooking updates more consistent

* datafield tweaks

* test for multiple portions in microwave recipes

* test for multiple recipe portions

* better(?) prediction for malfunctions

* remove redundant comment

* prevent solution reactions in microwave

* cleanup some microwave stuff

* eh i think this should be container actually

* this can be API i think

* reduce system redundancy

* this has to be public

* use resolve pattern for explode

* use GetMicrowaveContents in microwave system

* this can be an API too

* add conversion script for unlocalized recipes

* use EnsureComp instead of AddComp

* suffering

* fix recipe prediction

* fix the audio

* reduce redundant networking

* i think this should be autopaused?

* nevermind we use dynamic portions in the test

* oops i wrote the test wrong

* make lemoon cake take 2 lemoons again
sorry test fail

* rename "materials" to "stacks"
realized theyre different

* add stack guidebook quantities

* Kill audio

* fix lemoon cake recipe for real?

* mark PortionedRecipe as a datadefinition

* fix the microwave timer

* review

* cleanup

* Update SharedMicrowaveSystem.Recipes.cs

* update

* add audio params

* review: inject microwavesystem

* review: named variable

* review: move dependencies

* review: use gametest for recipe test

* review: make access attributes standalone

* rename prototypes and datafields

* review: rename this var

* review: move this function down

* review: remove these scripts

* actually this should stay

* refactor cook time buttons

* review: use a color name for this

* fix timer reset

* Dirty

* review: make ActivelyMicrowavedComponent networked

* review: slarti comments part 1

* review: remove serializable attribute from CookingIngredients

* rename file

* review: more microwave audio boilerplate. yippee!

* review: use resolve pattern for TryGetStackId
i dont like the way this makes the rest of the code look but we ball maybe. i dont know what else to do here

* review: move ui update to constructor

* review: kill LastCookUpdated

* actually i do this instead

* fixed up the PR

* cleanup

* ///

* Update microwave.yml

---------

Co-authored-by: Princess Cheeseballs <66055347+Princess-Cheeseballs@users.noreply.github.com>
Co-authored-by: Pok245 <113675512+Pok27@users.noreply.github.com>
2026-10-04 23:38:48 +00:00

154 lines
6.1 KiB
C#

using System.Linq;
using Content.Shared.Kitchen.Components;
using JetBrains.Annotations;
using Robust.Shared.Utility;
namespace Content.Shared.Kitchen.EntitySystems;
public abstract partial class MicrowaveSystem
{
/// <summary>
/// Helper function to get if an entity is an active microwave.
/// </summary>
/// <param name="microwave">The microwave entity.</param>
/// <returns>Whether or not this entity is an active microwave.</returns>
[PublicAPI]
public bool IsActiveMicrowave(Entity<MicrowaveComponent?> microwave)
{
return MicrowaveQuery.Resolve(microwave.Owner, ref microwave.Comp)
&& ActiveMicrowaveQuery.HasComp(microwave.Owner);
}
/// <summary>
/// Helper function to get the contents of a microwave.
/// </summary>
/// <param name="microwave">The microwave entity.</param>
/// <returns>A list of microwave contents.</returns>
[PublicAPI]
public IReadOnlyList<EntityUid> GetMicrowaveContents(Entity<MicrowaveComponent?> microwave)
{
if (!MicrowaveQuery.Resolve(microwave.Owner, ref microwave.Comp))
return new List<EntityUid>();
return microwave.Comp.Storage.ContainedEntities;
}
/// <summary>
/// Helper function to check if a microwave has ingredient contents.
/// </summary>
/// <param name="microwave">The microwave entity.</param>
/// <returns>Whether or not this microwave contains anything.</returns>
[PublicAPI]
public bool HasContents(Entity<MicrowaveComponent?> microwave)
{
return GetMicrowaveContents(microwave).Any();
}
/// <summary>
/// Gets a complete ordered list of usable recipes for this appliance.
/// </summary>
/// <remarks>
/// Note that the order of recipes is meaningful. When a valid recipe is chosen, the first item
/// in the list that satisfies the conditions of the recipe is selected.
///
/// Recipe prototypes in the recipe manager are pre-sorted based on complexity, so more "specific"
/// recipes will be selected first. Secret recipes come before all non-secret prototype recipes.
/// Do not sort the result of this function!
/// </remarks>
/// <param name="uid">The appliance to get recipes for.</param>
/// <returns>A complete list of usable recipe prototypes.</returns>
[PublicAPI]
public IReadOnlyList<MicrowaveMealRecipePrototype> GetAvailableRecipes(EntityUid uid)
{
var getRecipesEv = new GetSecretRecipesEvent();
RaiseLocalEvent(uid, ref getRecipesEv);
var recipes = getRecipesEv.Recipes
.OrderByDescending(recipe => recipe.Priority)
.ThenByDescending(recipe => recipe.Ingredients.Count())
.ToList();
recipes.AddRange(_recipes.Recipes);
return recipes;
}
/// <summary>
/// Given a recipe, a list of available ingredients, and a cooking time, this functions
/// gets how many times we can make this given recipe.
/// </summary>
/// <param name="recipe">A cooking recipe.</param>
/// <param name="ingredients">The ingredients we have available.</param>
/// <param name="cookTime">How long we are cooking for.</param>
/// <returns>How many portions of the recipe can be made.</returns>
[PublicAPI]
public static uint GetRecipePortions(MicrowaveMealRecipePrototype recipe,
CookingIngredients ingredients,
uint cookTime)
{
// Our cooking time must be a multiple of the recipe's cooking time.
// For example: If a recipe takes 10 seconds to cook, then you can't make it with a 15 second timer.
// However, if you use a 30 second timer, you could make three of that recipe on one timer.
if (cookTime % recipe.Time != 0)
return 0;
// TODO: there's actually a kind of nasty edge case microwave economics issue here,
// all reagents / stacks / solids will be included, but when the recipe is actually made,
// solids are used first, then stacks, then reagents.
// thus, recipe detection might thing you have "more" ingredients than you actually do.
//
// moral of the story: I hate microwaves
var portionCount = cookTime / recipe.Time;
var ingredientPortions = ingredients.PortionForRecipe(recipe.Ingredients);
portionCount = Math.Min(portionCount, ingredientPortions);
return portionCount;
}
/// <summary>
/// Given an appliance entity and a list of available ingredients, this function gets the first valid
/// usable recipe for cooking.
/// </summary>
/// <remarks>
/// The appliance entity itself is used to get secret recipes.
/// </remarks>
/// <param name="uid">An appliance to fetch a recipe for.</param>
/// <param name="ingredients">A list of available ingredients.</param>
/// <param name="cookTime">How long we plan to cook for.</param>
/// <returns>
/// The first valid recipe we can use. If there is none, this is (null, 0).
/// </returns>
[PublicAPI]
public PortionedRecipe? GetRecipe(EntityUid uid,
CookingIngredients ingredients,
uint cookTime)
{
var recipes = GetAvailableRecipes(uid);
var recipePortions = recipes.Select(recipe =>
{
var portions = GetRecipePortions(recipe, ingredients, cookTime);
return new PortionedRecipe(recipe.ID, portions);
});
return recipePortions.FirstOrNull(r => r.Count > 0)
?? null;
}
/// <summary>
/// Gets a complete list of recipe-usable ingredients from a list of items, including solids,
/// stacks, and reagents.
/// </summary>
/// <param name="items">The list of items to use as ingredients.</param>
/// <returns>Cooking ingredient quantities representing the total usable ingredient list.</returns>
[PublicAPI]
public CookingIngredients GetTotalIngredients(List<EntityUid> items)
{
var ingredients = new CookingIngredients();
foreach (var item in items)
{
AddItemIngredients(item, ref ingredients);
}
return ingredients;
}
}