* Add basic chemical reaction system What it adds: - Reactions defined in yaml with an arbitrary amount of reactants (can be catalysts), products, and effects. What it doesn't add: - Temperature dependent reactions - Metabolism or other medical/health effects * Add many common SS13 chemicals and reactions Added many of the common SS13 medicines and other chemicals, and their chemical reactions. Note that many of them are lacking their effects since we don't have medical yet. * Add ExplosiveReactionEffect Shows how IReactionEffect can be implemented to have effects that occur with a reaction by adding ExplosionReactionEffect and the potassium + water explosion reaction. * Move ReactionSystem logic into SolutionComponent No need for this to be a system currently so the behavior for reaction checking has been moved into SolutionComponent. Now it only checks for reactions when a reagent or solution is added to a solution. Also fixed a bug with SolutionValidReaction incorrectly returning true. * Move explosion logic out of ExplosiveComponent Allows you to create explosions without needing to add an ExplosiveComponent to an entity first. * Add SolutionComponent.SolutionChanged event. Trigger dispenser ui updates with it. This removes the need for SolutionComponent having a reference to the dispenser it's in. Instead the dispenser subscribes to the event and updates it's UI whenever the event is triggered. * Add forgotten checks `SolutionComponent.TryAddReagent` and `SolutionComponent.TryAddSolution` now check to see if `skipReactionCheck` is false before checking for a chemical reaction to avoid unnecessarily checking themselves for a reaction again when `SolutionComponent.PerformReaction` calls either of them. * Change SolutionComponent.SolutionChanged to an Action The arguments for event handler have no use here, and any class that can access SolutionChanged can access the SolutionComponent so it can just be an Action with no arguments instead.
213 lines
6.7 KiB
C#
213 lines
6.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Content.Shared.Chemistry;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Maths;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Serialization;
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
namespace Content.Shared.GameObjects.Components.Chemistry
|
|
{
|
|
public class SolutionComponent : Component
|
|
{
|
|
#pragma warning disable 649
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager;
|
|
#pragma warning restore 649
|
|
|
|
[ViewVariables]
|
|
protected Solution _containedSolution;
|
|
protected int _maxVolume;
|
|
private SolutionCaps _capabilities;
|
|
|
|
/// <summary>
|
|
/// Triggered when the solution contents change.
|
|
/// </summary>
|
|
public event Action SolutionChanged;
|
|
|
|
/// <summary>
|
|
/// The maximum volume of the container.
|
|
/// </summary>
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
public int MaxVolume
|
|
{
|
|
get => _maxVolume;
|
|
set => _maxVolume = value; // Note that the contents won't spill out if the capacity is reduced.
|
|
}
|
|
|
|
/// <summary>
|
|
/// The total volume of all the of the reagents in the container.
|
|
/// </summary>
|
|
[ViewVariables]
|
|
public int CurrentVolume => _containedSolution.TotalVolume;
|
|
|
|
/// <summary>
|
|
/// The current blended color of all the reagents in the container.
|
|
/// </summary>
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
public Color SubstanceColor { get; private set; }
|
|
|
|
/// <summary>
|
|
/// The current capabilities of this container (is the top open to pour? can I inject it into another object?).
|
|
/// </summary>
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
public SolutionCaps Capabilities
|
|
{
|
|
get => _capabilities;
|
|
set => _capabilities = value;
|
|
}
|
|
|
|
public IReadOnlyList<Solution.ReagentQuantity> ReagentList => _containedSolution.Contents;
|
|
|
|
/// <inheritdoc />
|
|
public override string Name => "Solution";
|
|
|
|
/// <inheritdoc />
|
|
public sealed override uint? NetID => ContentNetIDs.SOLUTION;
|
|
|
|
/// <inheritdoc />
|
|
public sealed override Type StateType => typeof(SolutionComponentState);
|
|
|
|
/// <inheritdoc />
|
|
public override void ExposeData(ObjectSerializer serializer)
|
|
{
|
|
base.ExposeData(serializer);
|
|
|
|
serializer.DataField(ref _maxVolume, "maxVol", 0);
|
|
serializer.DataField(ref _containedSolution, "contents", new Solution());
|
|
serializer.DataField(ref _capabilities, "caps", SolutionCaps.None);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Startup()
|
|
{
|
|
base.Startup();
|
|
|
|
RecalculateColor();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
|
|
_containedSolution.RemoveAllSolution();
|
|
_containedSolution = new Solution();
|
|
}
|
|
|
|
public void RemoveAllSolution()
|
|
{
|
|
_containedSolution.RemoveAllSolution();
|
|
OnSolutionChanged();
|
|
}
|
|
|
|
public bool TryRemoveReagent(string reagentId, int quantity)
|
|
{
|
|
if (!ContainsReagent(reagentId, out var currentQuantity)) return false;
|
|
|
|
_containedSolution.RemoveReagent(reagentId, quantity);
|
|
OnSolutionChanged();
|
|
return true;
|
|
}
|
|
|
|
public bool TryRemoveSolution(int quantity)
|
|
{
|
|
if (CurrentVolume == 0) return false;
|
|
|
|
_containedSolution.RemoveSolution(quantity);
|
|
OnSolutionChanged();
|
|
return true;
|
|
}
|
|
|
|
public Solution SplitSolution(int quantity)
|
|
{
|
|
var solutionSplit = _containedSolution.SplitSolution(quantity);
|
|
OnSolutionChanged();
|
|
return solutionSplit;
|
|
}
|
|
|
|
protected void RecalculateColor()
|
|
{
|
|
if(_containedSolution.TotalVolume == 0)
|
|
SubstanceColor = Color.White;
|
|
|
|
Color mixColor = default;
|
|
float runningTotalQuantity = 0;
|
|
|
|
foreach (var reagent in _containedSolution)
|
|
{
|
|
runningTotalQuantity += reagent.Quantity;
|
|
|
|
if(!_prototypeManager.TryIndex(reagent.ReagentId, out ReagentPrototype proto))
|
|
continue;
|
|
|
|
if (mixColor == default)
|
|
mixColor = proto.SubstanceColor;
|
|
|
|
mixColor = BlendRGB(mixColor, proto.SubstanceColor, reagent.Quantity / runningTotalQuantity);
|
|
}
|
|
}
|
|
|
|
private Color BlendRGB(Color rgb1, Color rgb2, float amount)
|
|
{
|
|
var r = (float)Math.Round(rgb1.R + (rgb2.R - rgb1.R) * amount, 1);
|
|
var g = (float)Math.Round(rgb1.G + (rgb2.G - rgb1.G) * amount, 1);
|
|
var b = (float)Math.Round(rgb1.B + (rgb2.B - rgb1.B) * amount, 1);
|
|
var alpha = (float)Math.Round(rgb1.A + (rgb2.A - rgb1.A) * amount, 1);
|
|
|
|
return new Color(r, g, b, alpha);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override ComponentState GetComponentState()
|
|
{
|
|
return new SolutionComponentState();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
|
|
{
|
|
base.HandleComponentState(curState, nextState);
|
|
|
|
if(curState == null)
|
|
return;
|
|
|
|
var compState = (SolutionComponentState)curState;
|
|
|
|
//TODO: Make me work!
|
|
}
|
|
|
|
[Serializable, NetSerializable]
|
|
public class SolutionComponentState : ComponentState
|
|
{
|
|
public SolutionComponentState() : base(ContentNetIDs.SOLUTION) { }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if the solution contains the specified reagent.
|
|
/// </summary>
|
|
/// <param name="reagentId">The reagent to check for.</param>
|
|
/// <param name="quantity">Output the quantity of the reagent if it is contained, 0 if it isn't.</param>
|
|
/// <returns>Return true if the solution contains the reagent.</returns>
|
|
public bool ContainsReagent(string reagentId, out int quantity)
|
|
{
|
|
foreach (var reagent in _containedSolution.Contents)
|
|
{
|
|
if (reagent.ReagentId == reagentId)
|
|
{
|
|
quantity = reagent.Quantity;
|
|
return true;
|
|
}
|
|
}
|
|
quantity = 0;
|
|
return false;
|
|
}
|
|
|
|
protected virtual void OnSolutionChanged()
|
|
{
|
|
SolutionChanged?.Invoke();
|
|
}
|
|
}
|
|
}
|