Mutate plant chemical and gas production (#19455)

Co-authored-by: Kevin Zheng <kevinz5000@gmail.com>
This commit is contained in:
drteaspoon420
2023-09-04 19:24:39 +03:00
committed by GitHub
parent 818f0bb231
commit a2c9ef04b3

View File

@@ -1,10 +1,21 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Random; using Robust.Shared.Random;
using Content.Shared.Chemistry.Reagent;
using System.Linq;
using Content.Shared.Atmos;
namespace Content.Server.Botany; namespace Content.Server.Botany;
public sealed class MutationSystem : EntitySystem public sealed class MutationSystem : EntitySystem
{ {
[Dependency] private readonly IRobustRandom _robustRandom = default!; [Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private List<ReagentPrototype> _allChemicals = default!;
public override void Initialize()
{
_allChemicals = _prototypeManager.EnumeratePrototypes<ReagentPrototype>().ToList();
}
/// <summary> /// <summary>
/// Main idea: Simulate genetic mutation using random binary flips. Each /// Main idea: Simulate genetic mutation using random binary flips. Each
@@ -25,7 +36,7 @@ public sealed class MutationSystem : EntitySystem
} }
// Add up everything in the bits column and put the number here. // Add up everything in the bits column and put the number here.
const int totalbits = 245; const int totalbits = 270;
// Tolerances (55) // Tolerances (55)
MutateFloat(ref seed.NutrientConsumption , 0.05f , 1.2f , 5 , totalbits , severity); MutateFloat(ref seed.NutrientConsumption , 0.05f , 1.2f , 5 , totalbits , severity);
@@ -62,6 +73,13 @@ public sealed class MutationSystem : EntitySystem
seed.BioluminescentColor = RandomColor(seed.BioluminescentColor, 10, totalbits, severity); seed.BioluminescentColor = RandomColor(seed.BioluminescentColor, 10, totalbits, severity);
// ConstantUpgade (10) // ConstantUpgade (10)
MutateHarvestType(ref seed.HarvestRepeat , 10 , totalbits , severity); MutateHarvestType(ref seed.HarvestRepeat , 10 , totalbits , severity);
// Gas (5)
MutateGasses(ref seed.ExudeGasses, 0.01f, 0.5f, 4, totalbits, severity);
MutateGasses(ref seed.ConsumeGasses, 0.01f, 0.5f, 1, totalbits, severity);
// Chems (20)
MutateChemicals(ref seed.Chemicals, 5, 20, totalbits, severity);
} }
public SeedData Cross(SeedData a, SeedData b) public SeedData Cross(SeedData a, SeedData b)
@@ -97,6 +115,8 @@ public sealed class MutationSystem : EntitySystem
CrossBool(ref result.Bioluminescent, a.Bioluminescent); CrossBool(ref result.Bioluminescent, a.Bioluminescent);
CrossBool(ref result.TurnIntoKudzu, a.TurnIntoKudzu); CrossBool(ref result.TurnIntoKudzu, a.TurnIntoKudzu);
CrossBool(ref result.CanScream, a.CanScream); CrossBool(ref result.CanScream, a.CanScream);
result.ExudeGasses = Random(0.5f) ? a.ExudeGasses : result.ExudeGasses;
result.ConsumeGasses = Random(0.5f) ? a.ConsumeGasses : result.ConsumeGasses;
result.BioluminescentColor = Random(0.5f) ? a.BioluminescentColor : result.BioluminescentColor; result.BioluminescentColor = Random(0.5f) ? a.BioluminescentColor : result.BioluminescentColor;
// Hybrids have a high chance of being seedless. Balances very // Hybrids have a high chance of being seedless. Balances very
@@ -200,6 +220,59 @@ public sealed class MutationSystem : EntitySystem
val = HarvestType.SelfHarvest; val = HarvestType.SelfHarvest;
} }
private void MutateGasses(ref Dictionary<Gas, float> gasses, float min, float max, int bits, int totalbits, float mult)
{
float p = mult * bits / totalbits;
p = Math.Clamp(p, 0, 1);
if (!Random(p))
return;
// Add a random amount of a random gas to this gas dictionary
float amount = _robustRandom.NextFloat(min, max);
Gas gas = _robustRandom.Pick(Enum.GetValues(typeof(Gas)).Cast<Gas>().ToList());
if (gasses.ContainsKey(gas))
{
gasses[gas] += amount;
}
else
{
gasses.Add(gas, amount);
}
}
private void MutateChemicals(ref Dictionary<string, SeedChemQuantity> chemicals, int max, int bits, int totalbits, float mult)
{
float p = mult * bits / totalbits;
p = Math.Clamp(p, 0, 1);
if (!Random(p))
return;
// Add a random amount of a random chemical to this set of chemicals
ReagentPrototype selected_chemical = _robustRandom.Pick(_allChemicals);
if (selected_chemical != null)
{
string chemical_id = selected_chemical.ID;
int amount = _robustRandom.Next(1, max);
SeedChemQuantity seed_chem_quantity = new SeedChemQuantity();
if (chemicals.ContainsKey(chemical_id))
{
seed_chem_quantity.Min = chemicals[chemical_id].Min;
seed_chem_quantity.Max = chemicals[chemical_id].Max + amount;
int potency = (int) Math.Ceiling(100.0f / (float) seed_chem_quantity.Max);
seed_chem_quantity.PotencyDivisor = potency;
chemicals[chemical_id] = seed_chem_quantity;
}
else
{
seed_chem_quantity.Min = 1;
seed_chem_quantity.Max = 1 + amount;
int potency = (int) Math.Ceiling(100.0f / (float) seed_chem_quantity.Max);
seed_chem_quantity.PotencyDivisor = potency;
chemicals.Add(chemical_id, seed_chem_quantity);
}
}
}
private Color RandomColor(Color color, int bits, int totalbits, float mult) private Color RandomColor(Color color, int bits, int totalbits, float mult)
{ {
float p = mult*bits/totalbits; float p = mult*bits/totalbits;
@@ -214,8 +287,7 @@ public sealed class MutationSystem : EntitySystem
Color.Purple, Color.Purple,
Color.Pink Color.Pink
}; };
var rng = IoCManager.Resolve<IRobustRandom>(); return _robustRandom.Pick(colors);
return rng.Pick(colors);
} }
return color; return color;
} }