Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,13 @@
#nullable enable
using Robust.Shared.GameObjects;
namespace Content.Shared.Chemistry.Reaction
{
/// <summary>
/// Chemical reaction effect on the world such as an explosion, EMP, or fire.
/// </summary>
public interface IReactionEffect
{
void React(IEntity solutionEntity, double intensity);
}
}

View File

@@ -0,0 +1,11 @@
#nullable enable
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.Map;
namespace Content.Shared.Chemistry.Reaction
{
public interface ITileReaction
{
ReagentUnit TileReact(TileRef tile, ReagentPrototype reagent, ReagentUnit reactVolume);
}
}

View File

@@ -0,0 +1,64 @@
#nullable enable
using System.Collections.Generic;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Chemistry.Reaction
{
/// <summary>
/// Prototype for chemical reaction definitions
/// </summary>
[Prototype("reaction")]
public class ReactionPrototype : IPrototype
{
[DataField("reactants")] private Dictionary<string, ReactantPrototype> _reactants = new();
[DataField("products")] private Dictionary<string, ReagentUnit> _products = new();
[DataField("effects", serverOnly: true)] private List<IReactionEffect> _effects = new();
[ViewVariables]
[DataField("id", required: true)]
public string ID { get; } = default!;
[DataField("name")]
public string Name { get; } = string.Empty;
/// <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;
// TODO SERV3: Empty on the client, (de)serialize on the server with module manager is server module
[DataField("sound", serverOnly: true)] public string? Sound { get; private set; } = "/Audio/Effects/Chemistry/bubbles.ogg";
}
/// <summary>
/// Prototype for chemical reaction reactants.
/// </summary>
[DataDefinition]
public class ReactantPrototype
{
[DataField("amount")]
private ReagentUnit _amount = ReagentUnit.New(1);
[DataField("catalyst")]
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;
}
}

View File

@@ -0,0 +1,16 @@
using System;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Shared.Chemistry.Reaction
{
[RegisterComponent]
public class ReactiveComponent : Component
{
public override string Name => "Reactive";
[DataField("reactions", true, serverOnly:true)]
public ReagentEntityReaction[] Reactions { get; } = Array.Empty<ReagentEntityReaction>();
}
}

View File

@@ -0,0 +1,155 @@
#nullable enable
using System.Collections.Generic;
using Content.Shared.Chemistry.Reagent;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
namespace Content.Shared.Chemistry.Reaction
{
public abstract class SharedChemicalReactionSystem : EntitySystem
{
private IEnumerable<ReactionPrototype> _reactions = default!;
private const int MaxReactionIterations = 20;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
_reactions = _prototypeManager.EnumeratePrototypes<ReactionPrototype>();
}
/// <summary>
/// Checks if a solution can undergo a specified reaction.
/// </summary>
/// <param name="solution">The solution to check.</param>
/// <param name="reaction">The reaction to check.</param>
/// <param name="lowestUnitReactions">How many times this reaction can occur.</param>
/// <returns></returns>
private static bool CanReact(Solution.Solution solution, ReactionPrototype reaction, out ReagentUnit lowestUnitReactions)
{
lowestUnitReactions = ReagentUnit.MaxValue;
foreach (var reactantData in reaction.Reactants)
{
var reactantName = reactantData.Key;
var reactantCoefficient = reactantData.Value.Amount;
if (!solution.ContainsReagent(reactantName, out var reactantQuantity))
return false;
var unitReactions = reactantQuantity / reactantCoefficient;
if (unitReactions < lowestUnitReactions)
{
lowestUnitReactions = unitReactions;
}
}
return true;
}
/// <summary>
/// Perform a reaction on a solution. This assumes all reaction criteria are met.
/// Removes the reactants from the solution, then returns a solution with all products.
/// </summary>
private Solution.Solution PerformReaction(Solution.Solution solution, IEntity owner, ReactionPrototype reaction, ReagentUnit unitReactions)
{
//Remove reactants
foreach (var reactant in reaction.Reactants)
{
if (!reactant.Value.Catalyst)
{
var amountToRemove = unitReactions * reactant.Value.Amount;
solution.RemoveReagent(reactant.Key, amountToRemove);
}
}
//Create products
var products = new Solution.Solution();
foreach (var product in reaction.Products)
{
products.AddReagent(product.Key, product.Value * unitReactions);
}
// Trigger reaction effects
OnReaction(reaction, owner, unitReactions);
return products;
}
protected virtual void OnReaction(ReactionPrototype reaction, IEntity owner, ReagentUnit unitReactions)
{
foreach (var effect in reaction.Effects)
{
effect.React(owner, unitReactions.Double());
}
}
/// <summary>
/// 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.
/// </summary>
private Solution.Solution ProcessReactions(Solution.Solution solution, IEntity owner)
{
//TODO: make a hashmap at startup and then look up reagents in the contents for a reaction
var overallProducts = new Solution.Solution();
foreach (var reaction in _reactions)
{
if (CanReact(solution, reaction, out var unitReactions))
{
var reactionProducts = PerformReaction(solution, owner, reaction, unitReactions);
overallProducts.AddSolution(reactionProducts);
break;
}
}
return overallProducts;
}
/// <summary>
/// Continually react a solution until no more reactions occur.
/// </summary>
public void FullyReactSolution(Solution.Solution solution, IEntity owner)
{
for (var i = 0; i < MaxReactionIterations; i++)
{
var products = ProcessReactions(solution, owner);
if (products.TotalVolume <= 0)
return;
solution.AddSolution(products);
}
Logger.Error($"{nameof(Solution.Solution)} on {owner} (Uid: {owner.Uid}) could not finish reacting in under {MaxReactionIterations} loops.");
}
/// <summary>
/// Continually react a solution until no more reactions occur, with a volume constraint.
/// If a reaction's products would exceed the max volume, some product is deleted.
/// </summary>
public void FullyReactSolution(Solution.Solution solution, IEntity owner, ReagentUnit maxVolume)
{
for (var i = 0; i < MaxReactionIterations; i++)
{
var products = ProcessReactions(solution, owner);
if (products.TotalVolume <= 0)
return;
var totalVolume = solution.TotalVolume + products.TotalVolume;
var excessVolume = totalVolume - maxVolume;
if (excessVolume > 0)
{
products.RemoveSolution(excessVolume); //excess product is deleted to fit under volume limit
}
solution.AddSolution(products);
}
Logger.Error($"{nameof(Solution.Solution)} on {owner} (Uid: {owner.Uid}) could not finish reacting in under {MaxReactionIterations} loops.");
}
}
}