Files
tbd-station-14/Content.Shared/Chemistry/ReactionPrototype.cs
py01 2b195fccb9 Chemical reaction refactor (#2936)
* Moves ContainsReagent from SolutionContainer to Solution

GetMajorReagentId from SOlutionContainer to Solution

Makes capability checks use HasFlag

Moves Solution Color calculation from SolutionContainer to Solution

Replaces SolutionContainerCaps.NoExamine with CanExamine

Misc SolutionContainer.Capabilities yaml cleanup

* Moves IReactionEffect from server to shared

* Moves ReactionPrototype from server to shared

* Moves SolutionValidReaction from SolutionContainer to ChemicalReactionSystem

* Moves PerformReaction from SolutionContainer to ChemicalReactionSystem

* Moves CheckForReaction from SolutionContainer to ChemicalReactionSystem

* Removes unused SolutionContainer methods

* Removes now-unused GetMajorReagentId from SOlutionContainer

* ChemicalReactionSystem comments

* Replaces usage of SolutionContainer.ContainsReagent and replaces it with SolutionContainer.Solution.ContainsReagent

* ChemicalReactionSystem ProcessReactions

* Moves ExplosionReactionEffect to shared, comments out server code, TODO: figure out how to let ReactionEffects in shared do server stuff

* Fixes SolutionContainer.CheckForReaction infinite recursion

* Moves IReactionEffect and ExplosionReactionEffect back to server

* Moves ChemicalReactionSystem and ReactionPrototype back to server

* Uncomments out Explosion code

* namespace fixes

* Moves ReactionPrototype and IReactionEffect from Server to Shared

* Moves ChemicalReactionSystem from Server to Shared

* ChemicalReaction code partial rewrite

* Moves CanReact and PerformReaction to Solution

* Revert "Moves CanReact and PerformReaction to Solution"

This reverts commit bab791c3ebd0ff39d22f2610e27ca04f0d46d6b8.

* Moves ChemistrySystem from Server to Shared

* diff fix

* TODO warning

Co-authored-by: py01 <pyronetics01@gmail.com>
2021-01-07 17:31:43 +11:00

83 lines
3.1 KiB
C#

using System.Collections.Generic;
using Content.Server.Interfaces.Chemistry;
using Content.Shared.Interfaces;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using YamlDotNet.RepresentationModel;
namespace Content.Shared.Chemistry
{
/// <summary>
/// Prototype for chemical reaction definitions
/// </summary>
[Prototype("reaction")]
public class ReactionPrototype : IPrototype, IIndexedPrototype
{
private string _id;
private string _name;
private Dictionary<string, ReactantPrototype> _reactants;
private Dictionary<string, ReagentUnit> _products;
private List<IReactionEffect> _effects;
public string ID => _id;
public string Name => _name;
/// <summary>
/// Reactants required for the reaction to occur.
/// </summary>
public IReadOnlyDictionary<string, ReactantPrototype> Reactants => _reactants;
/// <summary>
/// Reagents created when the reaction occurs.
/// </summary>
public IReadOnlyDictionary<string, ReagentUnit> Products => _products;
/// <summary>
/// Effects to be triggered when the reaction occurs.
/// </summary>
public IReadOnlyList<IReactionEffect> Effects => _effects;
[Dependency] private readonly IModuleManager _moduleManager = default!;
public void LoadFrom(YamlMappingNode mapping)
{
var serializer = YamlObjectSerializer.NewReader(mapping);
serializer.DataField(ref _id, "id", string.Empty);
serializer.DataField(ref _name, "name", string.Empty);
serializer.DataField(ref _reactants, "reactants", new Dictionary<string, ReactantPrototype>());
serializer.DataField(ref _products, "products", new Dictionary<string, ReagentUnit>());
if (_moduleManager.IsServerModule)
{
//TODO: Don't have a check for if this is the server
//Some implementations of IReactionEffect can't currently be moved to shared, so this is here to prevent the client from breaking when reading server-only IReactionEffects.
serializer.DataField(ref _effects, "effects", new List<IReactionEffect>());
}
}
}
/// <summary>
/// Prototype for chemical reaction reactants.
/// </summary>
public class ReactantPrototype : IExposeData
{
private ReagentUnit _amount;
private bool _catalyst;
/// <summary>
/// Minimum amount of the reactant needed for the reaction to occur.
/// </summary>
public ReagentUnit Amount => _amount;
/// <summary>
/// Whether or not the reactant is a catalyst. Catalysts aren't removed when a reaction occurs.
/// </summary>
public bool Catalyst => _catalyst;
public void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _amount, "amount", ReagentUnit.New(1));
serializer.DataField(ref _catalyst, "catalyst", false);
}
}
}