using System.Collections.Frozen; using System.Linq; using Content.Shared.Administration.Logs; using Content.Shared.Chemistry.Components; using Content.Shared.Chemistry.Reagent; using Content.Shared.Database; using Content.Shared.EntityEffects; using Content.Shared.FixedPoint; using Robust.Shared.Audio.Systems; using Robust.Shared.Network; using Robust.Shared.Prototypes; using Robust.Shared.Utility; namespace Content.Shared.Chemistry.Reaction { public sealed class ChemicalReactionSystem : EntitySystem { /// /// Foam reaction protoId. /// public static readonly ProtoId FoamReaction = "Foam"; /// /// The maximum number of reactions that may occur when a solution is changed. /// private const int MaxReactionIterations = 20; [Dependency] private readonly INetManager _netMan = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!; [Dependency] private readonly SharedAudioSystem _audio = default!; [Dependency] private readonly SharedTransformSystem _transformSystem = default!; [Dependency] private readonly SharedEntityEffectsSystem _entityEffects = default!; /// /// A cache of all reactions indexed by at most ONE of their required reactants. /// I.e., even if a reaction has more than one reagent, it will only ever appear once in this dictionary. /// private FrozenDictionary> _reactionsSingle = default!; /// /// A cache of all reactions indexed by one of their required reactants. /// private FrozenDictionary> _reactions = default!; public override void Initialize() { base.Initialize(); InitializeReactionCache(); SubscribeLocalEvent(OnPrototypesReloaded); } /// /// Handles building the reaction cache. /// private void InitializeReactionCache() { // Construct single-reaction dictionary. var dict = new Dictionary>(); foreach (var reaction in _prototypeManager.EnumeratePrototypes()) { // For this dictionary we only need to cache based on the first reagent. var reagent = reaction.Reactants.Keys.First(); var list = dict.GetOrNew(reagent); list.Add(reaction); } _reactionsSingle = dict.ToFrozenDictionary(); dict.Clear(); foreach (var reaction in _prototypeManager.EnumeratePrototypes()) { foreach (var reagent in reaction.Reactants.Keys) { var list = dict.GetOrNew(reagent); list.Add(reaction); } } _reactions = dict.ToFrozenDictionary(); } /// /// Updates the reaction cache when the prototypes are reloaded. /// /// The set of modified prototypes. private void OnPrototypesReloaded(PrototypesReloadedEventArgs eventArgs) { if (eventArgs.WasModified()) InitializeReactionCache(); } /// /// Checks if a solution can undergo a specified reaction. /// /// The solution to check. /// The reaction to check. /// How many times this reaction can occur. /// private bool CanReact(Entity soln, ReactionPrototype reaction, ReactionMixerComponent? mixerComponent, out FixedPoint2 lowestUnitReactions) { var solution = soln.Comp.Solution; lowestUnitReactions = FixedPoint2.MaxValue; if (solution.Temperature < reaction.MinimumTemperature) { lowestUnitReactions = FixedPoint2.Zero; return false; } if (solution.Temperature > reaction.MaximumTemperature) { lowestUnitReactions = FixedPoint2.Zero; return false; } if ((mixerComponent == null && reaction.MixingCategories != null) || mixerComponent != null && reaction.MixingCategories != null && reaction.MixingCategories.Except(mixerComponent.ReactionTypes).Any()) { lowestUnitReactions = FixedPoint2.Zero; return false; } var attempt = new ReactionAttemptEvent(reaction, soln); RaiseLocalEvent(soln, ref attempt); if (attempt.Cancelled) { lowestUnitReactions = FixedPoint2.Zero; return false; } foreach (var reactantData in reaction.Reactants) { var reactantName = reactantData.Key; var reactantCoefficient = reactantData.Value.Amount; var reactantQuantity = solution.GetTotalPrototypeQuantity(reactantName); if (reactantQuantity <= FixedPoint2.Zero) return false; if (reactantData.Value.Catalyst) { // catalyst is not consumed, so will not limit the reaction. But it still needs to be present, and // for quantized reactions we need to have a minimum amount if (reactantQuantity == FixedPoint2.Zero || reaction.Quantized && reactantQuantity < reactantCoefficient) return false; continue; } var unitReactions = reactantQuantity / reactantCoefficient; if (unitReactions < lowestUnitReactions) { lowestUnitReactions = unitReactions; } } if (reaction.Quantized) lowestUnitReactions = (int) lowestUnitReactions; return lowestUnitReactions > 0; } /// /// Perform a reaction on a solution. This assumes all reaction criteria are met. /// Removes the reactants from the solution, adds products, and returns a list of products. /// private List PerformReaction(Entity soln, ReactionPrototype reaction, FixedPoint2 unitReactions) { var (uid, comp) = soln; var solution = comp.Solution; var energy = reaction.ConserveEnergy ? solution.GetThermalEnergy(_prototypeManager) : 0; //Remove reactants foreach (var reactant in reaction.Reactants) { if (!reactant.Value.Catalyst) { var amountToRemove = unitReactions * reactant.Value.Amount; solution.RemoveReagent(reactant.Key, amountToRemove, ignoreReagentData: true); } } //Create products var products = new List(); foreach (var product in reaction.Products) { products.Add(product.Key); solution.AddReagent(product.Key, product.Value * unitReactions); } if (reaction.ConserveEnergy) { var newCap = solution.GetHeatCapacity(_prototypeManager); if (newCap > 0) solution.Temperature = energy / newCap; } OnReaction(soln, reaction, null, unitReactions); return products; } private void OnReaction(Entity soln, ReactionPrototype reaction, ReagentPrototype? reagent, FixedPoint2 unitReactions) { var posFound = _transformSystem.TryGetMapOrGridCoordinates(soln, out var gridPos); _adminLogger.Add(LogType.ChemicalReaction, reaction.Impact, $"Chemical reaction {reaction.ID:reaction} occurred with strength {unitReactions:strength} on entity {ToPrettyString(soln):metabolizer} at Pos:{(posFound ? $"{gridPos:coordinates}" : "[Grid or Map not Found]")}"); _entityEffects.ApplyEffects(soln, reaction.Effects, unitReactions.Float()); // Someday, some brave soul will thread through an optional actor // argument in from every call of OnReaction up, all just to pass // it to PlayPredicted. I am not that brave soul. if (_netMan.IsServer) _audio.PlayPvs(reaction.Sound, soln); } /// /// Performs all chemical reactions that can be run on a solution. /// Removes the reactants from the solution, then returns a solution with all products. /// WARNING: Does not trigger reactions between solution and new products. /// private bool ProcessReactions(Entity soln, SortedSet reactions, ReactionMixerComponent? mixerComponent) { List? products = null; // attempt to perform any applicable reaction foreach (var reaction in reactions) { if (!CanReact(soln, reaction, mixerComponent, out var unitReactions)) { continue; } products = PerformReaction(soln, reaction, unitReactions); break; } // did any reaction occur? if (products == null) return false; if (products.Count == 0) return true; // Add any reactions associated with the new products. This may re-add reactions that were already iterated // over previously. The new product may mean the reactions are applicable again and need to be processed. foreach (var product in products) { if (_reactions.TryGetValue(product, out var reactantReactions)) reactions.UnionWith(reactantReactions); } return true; } /// /// Continually react a solution until no more reactions occur, with a volume constraint. /// public void FullyReactSolution(Entity soln, ReactionMixerComponent? mixerComponent = null) { // construct the initial set of reactions to check. SortedSet reactions = new(); foreach (var reactant in soln.Comp.Solution.Contents) { if (_reactionsSingle.TryGetValue(reactant.Reagent.Prototype, out var reactantReactions)) reactions.UnionWith(reactantReactions); } // Repeatedly attempt to perform reactions, ending when there are no more applicable reactions, or when we // exceed the iteration limit. for (var i = 0; i < MaxReactionIterations; i++) { if (!ProcessReactions(soln, reactions, mixerComponent)) return; } Log.Error($"{nameof(Solution)} {soln.Owner} could not finish reacting in under {MaxReactionIterations} loops."); } } /// /// Raised directed at the owner of a solution to determine whether the reaction should be allowed to occur. /// /// /// Some solution containers (e.g., bloodstream, smoke, foam) use this to block certain reactions from occurring. /// [ByRefEvent] public record struct ReactionAttemptEvent(ReactionPrototype Reaction, Entity Solution) { public readonly ReactionPrototype Reaction = Reaction; public readonly Entity Solution = Solution; public bool Cancelled = false; } }